revit 二次開發——在牆上挖圓形洞(Create Circle openning in wall)

一、思路

1)在revit中創建一個基於牆的公制常規模型 的族
在這裏插入圖片描述
2)爲族添加實例參數
我這裏只添加了直徑參數。
在這裏插入圖片描述
3)將族載入項目中
4)創建族實例
5)修改直徑參數

二、代碼

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try {
                UIDocument uiDoc = commandData.Application.ActiveUIDocument;
                Document doc = uiDoc.Document;
                Selection sel = uiDoc.Selection;
                Autodesk.Revit.Creation.Application acreation = commandData.Application.Application.Create;
                var creation = doc.Create;

                Transaction ts = new Transaction(doc, "loadrfa");
                ts.Start();
                var rfaname = "空心圓";//族類型的名字
                FamilySymbol circle = LoadRfa(doc, rfaname);

                XYZ p0 = new XYZ(30, 0, 0);
                XYZ p1 = new XYZ(30, 10, 0);
                XYZ p3 = new XYZ(30, 0, 50);
                XYZ p2 = new XYZ(30, 10, 50);

                Curve l1 = Line.CreateBound(p0, p1);
                Curve l2 = Line.CreateBound(p1, p2);
                Curve l3 = Line.CreateBound(p2, p3);
                Curve l4 = Line.CreateBound(p3, p0);
                var profile = new List<Curve>();
                profile.Add(l1);
                profile.Add(l2);
                profile.Add(l3);
                profile.Add(l4);

                var wall = Wall.Create(doc, profile, true);

                XYZ insertP = new XYZ(30, 5, 25);//插入的中心點
                int d = 1000;//直徑
                Level lv = Level.Create(doc, 0);
                CreateCirleHole(doc, insertP, d, lv, wall, circle, "2020");
                ts.Commit();
                return Result.Succeeded;
            } catch (Exception ex) {
                var r = 0;
                return Result.Succeeded;

            }

        }

        private  FamilySymbol LoadRfa(Document doc, string rfaname)
        {
            FamilySymbol familySymbol = null;
           
            var isLoad = CheckLoadRfa(doc, rfaname, out familySymbol);
            if (!isLoad) {
               
                string familyPath = @"C:\Users\xxx\Desktop\空心圓族.rfa";
                Family family = null;
                bool flag = doc.LoadFamily(familyPath, out family);
                if (!flag) {
                    return null;
                } else {
                    foreach (ElementId s in family.GetFamilySymbolIds()) {
                        familySymbol = doc.GetElement(s) as FamilySymbol;
                        if (!familySymbol.IsActive)
                            familySymbol.Activate();
                        break;
                    }
                }
            }
            return familySymbol;
        }

        private  bool CheckLoadRfa(Document RevitDoc, string rfaName, out FamilySymbol familySymbol)
        {
            FamilySymbol type = null;

            ElementFilter categoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_GenericModel);
            ElementFilter familySymbolFilter = new ElementClassFilter(typeof(FamilySymbol));
            LogicalAndFilter andFilter = new LogicalAndFilter(categoryFilter, familySymbolFilter);
            FilteredElementCollector symbols = new FilteredElementCollector(RevitDoc);
            symbols = symbols.WherePasses(andFilter);

            bool symbolFound = false;
            foreach (FamilySymbol element in symbols) {
                if (element.Name == rfaName) {
                    symbolFound = true;
                    type = element;
                    break;
                }
            }
            familySymbol = type;
            return symbolFound;
        }
		//創建族實例,並修改直徑參數
        public  FamilyInstance CreateCirleHole(Document doc, XYZ insertPt, int d, Level level, Wall wall, FamilySymbol circle)
        {
            try {
               
                FamilyInstance airhole = doc.Create.NewFamilyInstance(insertPt, circle, wall, level, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                var paramD = airhole.LookupParameter("空心圓直徑");
                paramD.Set(ConvertToDecimalFeet(d));

                return airhole;

            } catch (Exception ex) {
                return null;
            }

        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章