Revit 二次开发 族的练习

摘要:
学习地址:https://www.bilibili.com/video/BV1mf4y1S72o?p=18示例练习1(创建柱族)使用系统;使用System.Collections。通用的使用系统。Linq;使用系统。文本使用System.Threading。任务;使用Autodesk.Revit。数据库;乌辛

学习地址:https://www.bilibili.com/video/BV1mf4y1S72o?p=18

实例练习一(创建一个柱的族)

Revit 二次开发 族的练习第1张

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.ApplicationServices;

namespace FamilyAPI
{
    [TransactionAttribute(TransactionMode.Manual)]
    public class FamilyAPI : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //族样板位置
            string rftPath = @"C:ProgramDataAutodeskRVT 2018Family TemplatesChinese公制柱.rft";
            UIApplication uiapp = commandData.Application;
            Application app = uiapp.Application;
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            //创建族文件
            Document faDoc = app.NewFamilyDocument(rftPath);
            //开启事务
            Transaction trans = new Transaction(faDoc,"创建族");
            trans.Start();
            //创建FamilyManager
            FamilyManager manager = faDoc.FamilyManager;
            //添加材质参数
            FamilyParameter mfp = manager.AddParameter("材质",BuiltInParameterGroup.PG_MATERIALS,ParameterType.Material,false);
            //创建拉伸
            CurveArrArray array = GetCurves();
            SketchPlane skplane = GetSketchPlane(faDoc);
            //创建Extrusion
            Extrusion extrusion = faDoc.FamilyCreate.NewExtrusion(true, array, skplane, 4000 / 304.8);
            faDoc.Regenerate();
            //创建约束
            Reference topFaceRef = null;
            //创建Options
            Options opt = new Options();
            opt.ComputeReferences = true;
            opt.DetailLevel = ViewDetailLevel.Fine;
            GeometryElement gelm = extrusion.get_Geometry(opt);
            foreach (GeometryObject gobj in gelm)
            {
                if (gobj is Solid)
                {
                    Solid s = gobj as Solid;
                    foreach (Face face in s.Faces) //找面
                    {
                        if (face.ComputeNormal(new UV()).IsAlmostEqualTo(new XYZ(0,0,1)))
                        {
                            topFaceRef = face.Reference;
                        }
                    }
                }
            }
            //创建View
            View v = GetView(faDoc);
            //创建Reference
            Reference r = GetTopLevel(faDoc);
            //创建
            Dimension d = faDoc.FamilyCreate.NewAlignment(v, r, topFaceRef);
            d.IsLocked = true;
            faDoc.Regenerate();
            //设置材质参数
            Parameter p = extrusion.get_Parameter(BuiltInParameter.MATERIAL_ID_PARAM);
            manager.AssociateElementParameterToFamilyParameter(p, mfp);
            //提交事务
            trans.Commit();
            Family fa = faDoc.LoadFamily(doc);
            faDoc.Close(false);
            //创建新事务
            trans = new Transaction(doc,"创建柱");
            trans.Start();
            fa.Name = "我的柱子";
            trans.Commit();//事务提交
            return Result.Succeeded;
        }

        private Reference GetTopLevel(Document faDoc)
        {
            //创建过滤器
            FilteredElementCollector temc = new FilteredElementCollector(faDoc);
            //过滤出标高
            temc.OfClass(typeof(Level));
            Level lvl = temc.First(m => m.Name == "高于参照标高") as Level;
            return new Reference(lvl);
        }

        private View GetView(Document faDoc)
        {
            FilteredElementCollector viewFilter = new FilteredElementCollector(faDoc);
            viewFilter.OfClass(typeof(View));
            View v = viewFilter.First(m => m.Name == "") as View;
            return v;
        }

        private SketchPlane GetSketchPlane(Document faDoc)
        {
            FilteredElementCollector temc = new FilteredElementCollector(faDoc);
            //过滤
            temc.OfClass(typeof(SketchPlane));
            SketchPlane sketchPlane = temc.First(m=>m.Name== "低于参照标高") as SketchPlane;
            return sketchPlane;
        }

        private CurveArrArray GetCurves()
        {
            double len = 300 / 304.8;
            //创建坐标
            XYZ p1 = new XYZ(-len,-len,0);
            XYZ p2 = new XYZ(len,-len,0);
            XYZ p3 = new XYZ(len,len,0);
            XYZ p4 = new XYZ(-len,len,0);
            //创建线
            Line l1 = Line.CreateBound(p1, p2);
            Line l2 = Line.CreateBound(p2, p3);
            Line l3 = Line.CreateBound(p3, p4);
            Line l4 = Line.CreateBound(p4, p1);
            CurveArrArray ary = new CurveArrArray();
            CurveArray arry = new CurveArray();
            arry.Append(l1);
            arry.Append(l2);
            arry.Append(l3);
            arry.Append(l4);
            ary.Append(arry);
            return ary;
        }
    }
}

免责声明:文章转载自《Revit 二次开发 族的练习》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇App接口如何保证安全C++读取配置文件下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章