Revit API 3 創建標高(Level) - innowantai/Note GitHub Wiki

using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.UI.Selection;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _3_CreateBeam
{
    [Transaction(TransactionMode.Manual)]
    public class Class1 : IExternalCommand
    {

        public Application revitApp;
        public Document revitDoc;
        public UIDocument uidoc;

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            revitDoc = commandData.Application.ActiveUIDocument.Document;
            uidoc = commandData.Application.ActiveUIDocument;
            ElementId ele = null;
            CreateNewLevel(); 
            return Result.Succeeded;
        }

    }


    void CreateNewLevel()
        {
            Level level = null;
            using (Transaction trans = new Transaction(revitDoc))
            {
                trans.Start("Create New Level");
                level = Level.Create(revitDoc, UnitUtils.ConvertToInternalUnits(1000,DisplayUnitType.DUT_MILLIMETERS));
                trans.Commit();  
            }

            var classFilter = new ElementClassFilter(typeof(ViewFamilyType));
            FilteredElementCollector filterElements = new FilteredElementCollector(revitDoc);
            filterElements = filterElements.WherePasses(classFilter);
            foreach (ViewFamilyType viewFamilyType in filterElements)
            {
                if (viewFamilyType.ViewFamily == ViewFamily.FloorPlan || viewFamilyType.ViewFamily == ViewFamily.CeilingPlan)
                {
                    using (Transaction trans = new Transaction(revitDoc))
                    {
                        try
                        {
                            trans.Start("CreateView");
                            ViewPlan view = ViewPlan.Create(revitDoc, viewFamilyType.Id, level.Id);
                            trans.Commit();

                        }
                        catch (Exception e)
                        {

                            TaskDialog.Show("ee", e.Message);
                        }
                    }
                }
            }

        }
}