Revit secondary development-Create Circle openning in wall

1. Ideas

1) Create a family of wall-based metric regular models in Revit
Insert picture description here
2) Add instance parameters to the family
I only added the diameter parameter here.
Insert picture description here
3) Load the family into the project
4) Create a family instance
5) Modify the diameter parameter

Two, the code

        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;
            }

        }

Guess you like

Origin blog.csdn.net/hhhhhhenrik/article/details/105539340