RevitAPI之如何载入族再创建族实例(以门为例)

如何放置类型为“0762 x 2032 mm”的门实例?

思路:

首先在文档中找到名字为“0762x2032 mm”的门类型,如果没有找到,则加载一个名称 为“M_单-嵌板4.rfa”的族文件,这样就得到一个Family;

然后从Family获取名为“0762x2032 mm”的门类型;

最后在文档中获取一个直线形的墙,计算墙的中点位置,在此位置插入类型为“0762x 2032 mm”的门。


代码如下:

 	public void CreatDoorInstance(Document RevitDoc, string doorTypeName = "0762 x 2032 mm")
        {
            FamilySymbol doorType = null;

            // 在文档中找到名字为"0762 x 2032 mm"的门类型 
            ElementFilter doorCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);
            ElementFilter familySymbolFilter = new ElementClassFilter(typeof(FamilySymbol));
            LogicalAndFilter andFilter = new LogicalAndFilter(doorCategoryFilter, familySymbolFilter);
            FilteredElementCollector doorSymbols = new FilteredElementCollector(RevitDoc);
            doorSymbols = doorSymbols.WherePasses(andFilter);
            bool symbolFound = false;
            foreach (FamilySymbol element in doorSymbols)
            {
                if (element.Name == doorTypeName)
                {
                    symbolFound = true;
                    doorType = element;
                    break;
                }
            }

            // 如果没有找到,就加载一个族文件 
            if (!symbolFound)
            {
                string file = @"C:\ProgramData\Autodesk\RVT  2014\Libraries\Chinese_INTL\门\M_单-嵌板 4.rfa";
                Family family;
                bool loadSuccess = RevitDoc.LoadFamily(file, out family);
                if (loadSuccess)
                {
                    foreach (ElementId doorTypeId in family.GetValidTypes())
                    {
                        doorType = RevitDoc.GetElement(doorTypeId) as FamilySymbol;
                        if (doorType != null)
                        {
                            if (doorType.Name == doorTypeName)
                            {
                                break;
                            }
                        }
                    }
                }
                else
                {
                    Autodesk.Revit.UI.TaskDialog.Show("Load family failed", "Could not load family file '" + file + "'");
                }
            }

            // 使用族类型创建门 
            if (doorType != null)
            {
                // 首先找到线形的墙 
                ElementFilter wallFilter = new ElementClassFilter(typeof(Wall));
                FilteredElementCollector filteredElements = new FilteredElementCollector(RevitDoc);
                filteredElements = filteredElements.WherePasses(wallFilter);
                Wall wall = null;
                Line line = null;
                foreach (Wall element in filteredElements)
                {
                    LocationCurve locationCurve = element.Location as LocationCurve;
                    if (locationCurve != null)
                    {
                        line = locationCurve.Curve as Line;
                        if (line != null)
                        {
                            wall = element;
                            break;
                        }
                    }
                }

                // 在墙的中心位置创建一个门 
                if (wall != null)
                {
                    XYZ midPoint = (line.GetEndPoint(0) + line.GetEndPoint(1)) / 2;
                    Level wallLevel = RevitDoc.GetElement(wall.LevelId) as Level;
                    //创建门:传入标高参数,作为门的默认标高 
                    FamilyInstance door = RevitDoc.Create.NewFamilyInstance(midPoint, doorType, wall,
              wallLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                    Autodesk.Revit.UI.TaskDialog.Show("Succeed", door.Id.ToString());
                    System.Diagnostics.Trace.WriteLine("Door created: " + door.Id.ToString());
                }
                else
                {
                    Autodesk.Revit.UI.TaskDialog.Show("元素不存在", "没有找到符合条件的墙");
                }
            }
            else
            {
                Autodesk.Revit.UI.TaskDialog.Show("族类型不存在", "没有找到族类型'" + doorTypeName + "'");
            }
        }


注意:这里的门并不是插入到墙的中点位置,RevitAPI会自动调整门的Z轴的值,直至它和墙平齐。

另外,创建的时候,传入的标高会作为门的标高,如果不传,则门就没有标高。


=========【更多高级应用请关注公众号】========


==================================


你可能感兴趣的:(RevitAPI,元素编辑d)