revit 二次开发 利用墙饰条创建踢脚线

 public class KickingLine:IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document doc = uidoc.Document;

            Reference refer = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new roomFilter(), "");//过滤出房间
            Family family=CreateProfileFamily(uiapp);
            Material mate = new FilteredElementCollector(doc).OfClass(typeof(Material)).Where(o => o.Name == "面层 - 白色").First() as Material;
            ElementType wallSweepType = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Cornices).WhereElementIsElementType().First() as ElementType;
            //ElementType wallSweepType = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Cornices).WhereElementIsElementType().Cast().First();
            using (Transaction transaction = new Transaction(doc, "BIM"))
            {
                transaction.Start();
                //复制一个墙饰条类型
                ElementType type = wallSweepType.Duplicate("踢脚线");
                Room room = doc.GetElement(refer) as Room;
                SpatialElementBoundaryOptions options = new SpatialElementBoundaryOptions();
                options.SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Finish;
           
                IList<IList<Autodesk.Revit.DB.BoundarySegment>> boundList = room.GetBoundarySegments(options);

                foreach (IList loop in boundList)
                {
                    foreach (BoundarySegment seg in loop)
                    {
                        Element elem = doc.GetElement(seg.ElementId);
                        if (elem is Wall)
                        {
                            Wall wall = elem as Wall;
                            WallSweepInfo wallSweepInfo = new WallSweepInfo(WallSweepType.Sweep, false);//false水平布置,true垂直布置
                            //墙的正反或里外面
                            wallSweepInfo.WallSide = WallSide.Interior;
                            //距墙底高度
                            wallSweepInfo.Distance = 0;
                            //偏离墙距离
                            wallSweepInfo.WallOffset = 0;
                            foreach (Parameter o in type.Parameters)
                            {
                                if (o.Definition.Name == "轮廓")
                                {
                                    // o.Set(FamilyUtils.GetProfileSymbols(doc, ProfileFamilyUsage.WallSweep, false).ElementAt(0));
                                    o.Set(family.Id);
                                }
                                if (o.Definition.Name == "材质")
                                {
                                    o.Set(mate.Id);
                                }

                            }
                            WallSweep ws = WallSweep.Create(wall, type.Id, wallSweepInfo);//创建函数

                        }
                    }
                    transaction.Commit();
                }
            }
             

            return Result.Succeeded;
        }

       private Family CreateProfileFamily(UIApplication uiapp)
       {
           Document familyDoc=uiapp.Application.NewFamilyDocument(@"C:\Users\018193\Desktop\公制轮廓.rft");
           Family family=null;
           using (Transaction transFamily=new Transaction(familyDoc))
           {
               //找到默认的平面,为详图线服务
               View view=new FilteredElementCollector(familyDoc).OfClass(typeof(View)).Where(o=>o.Name=="参照标高").First() as View;
               transFamily.Start("familyCreation");
               //创建四条首尾相连的曲线,为详图线服务
               Curve curve1=Line.CreateBound(new XYZ(0,0,0),new XYZ(0,150/304.8,0));
               Curve curve2=Line.CreateBound(new XYZ(0,150/304.8,0),new XYZ(25/304.8,150/304.8,0));
               Curve curve3=Line.CreateBound(new XYZ(25/304.8,150/304.8,0),new XYZ(25/304.8,0,0));
               Curve curve4=Line.CreateBound(new XYZ(25/304.8,0,0),new XYZ(0,0,0));

               //创建四条首尾相连的详图线也就是真正的墙饰条轮廓
               DetailCurve dc1=familyDoc.FamilyCreate.NewDetailCurve(view,curve1);
               DetailCurve dc2=familyDoc.FamilyCreate.NewDetailCurve(view,curve2);
               DetailCurve dc3=familyDoc.FamilyCreate.NewDetailCurve(view,curve3);
               DetailCurve dc4=familyDoc.FamilyCreate.NewDetailCurve(view,curve4);

               transFamily.Commit();
               //族另存选项
               SaveAsOptions option=new SaveAsOptions();
               //覆盖已有同名文件
               option.OverwriteExistingFile=true;
               //保存在本地桌面上,可以删除也可以保留为其他项目使用
               familyDoc.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop,Environment.SpecialFolderOption.None)+@"\踢脚线.rfa",option );
               //保存后族就有名字了可以直接使用familyDoc导入到目标项目中了
               family=familyDoc.LoadFamily(uiapp.ActiveUIDocument.Document,new LoadOptions());
               //记得关闭 要不然在桌面上临时生成的族文件无法顺利删除也就是要资源释放
               familyDoc.Close();
 
           }
           return family;
 	 
       }

        public class roomFilter:ISelectionFilter
        {
            public bool AllowElement(Element elem)
            {
                return elem is Room;
            }

            public bool AllowReference(Reference reference,XYZ position)
            {
                return false;
            }
        }

        public class LoadOptions : IFamilyLoadOptions
        {
            public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues)
            {
                overwriteParameterValues = true;
                return true;
            }



            public bool OnSharedFamilyFound(Family sharedFamily, bool familyInUse, out FamilySource source, out bool overwriteParameterValues)
            {
                source = FamilySource.Project;
                overwriteParameterValues = true;
                return true;

            }
        }
    }

原文链接:http://blog.sina.com.cn/s/blog_16796559b0102y90h.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值