完善主體資料,免費(fèi)贈(zèng)送VIP會(huì)員!
* 主體類型
* 企業(yè)名稱
* 信用代碼
* 所在行業(yè)
* 企業(yè)規(guī)模
* 所在職位
* 姓名
* 所在行業(yè)
* 學(xué)歷
* 工作性質(zhì)
請(qǐng)先選擇行業(yè)
您還可以選擇以下福利:
行業(yè)福利,領(lǐng)完即止!

下載app免費(fèi)領(lǐng)取會(huì)員

NULL

5cdd2dc095060.jpg

二次開(kāi)發(fā)教程:Revit開(kāi)發(fā)通過(guò)API 創(chuàng)建族

發(fā)布于:2019-08-23 17:20:00

網(wǎng)友投稿

更多

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Autodesk.Revit.UI;

using Autodesk.Revit.DB;

using Autodesk.Revit.Attributes;

using Autodesk.Revit.ApplicationServices;



namespace CreateFamily

{

    [Transaction(TransactionMode.Manual)]

    public class Class1:IExternalCommand

    {

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)

        {

            string rftPath = @"C:\ProgramData\Autodesk\RVT 2016\Family Templates\Chinese\公制柱.rft";

            UIApplication uiapp = commandData.Application;

            Application app = uiapp.Application;

            UIDocument uidoc = commandData.Application.ActiveUIDocument;

            Document doc = uidoc.Document;



            //創(chuàng)建族文件

            Document faDoc = app.NewFamilyDocument(rftPath);


            Transaction trans = new Transaction(faDoc, "Create Family");

            trans.Start();

            FamilyManager manager = faDoc.FamilyManager;

            //添加材質(zhì)參數(shù)

            FamilyParameter mfp = manager.AddParameter("材質(zhì)", BuiltInParameterGroup.PG_MATERIALS, ParameterType.Material, false);


            //創(chuàng)建拉伸

            CurveArrArray arry = GetCurves();

            SketchPlane skplane = GetSketchPlane(faDoc);

            Extrusion extrusion = faDoc.FamilyCreate.NewExtrusion(true, arry, skplane, 4000 / 304.8);

            faDoc.Regenerate();


            //創(chuàng)建約束

            Reference topFaceRef = null;

            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 v = GetView(faDoc);

            Reference r =GetTopLevel(faDoc);

            Dimension d = faDoc.FamilyCreate.NewAlignment(v, r, topFaceRef);

            d.IsLocked = true;

            faDoc.Regenerate();


            //關(guān)聯(lián)材質(zhì)參數(shù)

            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, "CreateColumn");

            trans.Start();

            fa.Name = "我的柱";

            trans.Commit();

            return Result.Succeeded;

        }


        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;

        }


        private SketchPlane GetSketchPlane(Document doc)

        {

            FilteredElementCollector temc = new FilteredElementCollector(doc);

            temc.OfClass(typeof(SketchPlane));

            SketchPlane sketchPlane = temc.First(m => m.Name == "低于參照標(biāo)高") as SketchPlane;

            return sketchPlane;

        }


        private View GetView(Document doc)

        {

            FilteredElementCollector viewFilter = new FilteredElementCollector(doc);

            viewFilter.OfClass(typeof(View));

            View v = viewFilter.First(m => m.Name == "前") as View;

            return v;

        }


        private Reference GetTopLevel(Document doc)

        {

            FilteredElementCollector temc = new FilteredElementCollector(doc);

            temc.OfClass(typeof(Level));

            Level lvl = temc.First(m => m.Name == "高于參照標(biāo)高") as Level;

            return new Reference(lvl);

        }

    }

}

本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。

未標(biāo)題-1.jpg

上一篇:二次開(kāi)發(fā)教程:Revit開(kāi)發(fā)樓梯創(chuàng)建

下一篇:二次開(kāi)發(fā)教程:Revit開(kāi)發(fā)之標(biāo)注創(chuàng)建

60acb4e0ef112.png