Beispiel #1
1
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument    uiDoc   = commandData.Application.ActiveUIDocument;
            Document      doc     = uiDoc.Document;
            UIApplication uiApp   = commandData.Application;
            Application   app     = uiApp.Application;
            string        rfePath = @"C:\ProgramData\Autodesk\RVT 2020\Family Templates\Chinese\公制柱.rft";
            Document      faDoc   = app.NewFamilyDocument(rfePath);
            Transaction   trans   = new Transaction(faDoc, "创建族");

            trans.Start();
            FamilyManager   manager = faDoc.FamilyManager;
            FamilyParameter mfp     =
                manager.AddParameter("材质", BuiltInParameterGroup.PG_MATERIALS, ParameterType.Material, false);
            CurveArrArray arrArray    = GetCurves();
            SketchPlane   sketchPlane = GetSketchPlane(faDoc);
            Extrusion     extrusion   = faDoc.FamilyCreate.NewExtrusion(true, arrArray, sketchPlane, 4000 / 304.8);

            faDoc.Regenerate();
            Reference topFaceReference = null;
            Options   opt = new Options {
                ComputeReferences = true,
                DetailLevel       = ViewDetailLevel.Fine
            };
            GeometryElement geometry = extrusion.get_Geometry(opt);

            foreach (GeometryObject o in geometry)
            {
                if (o is Solid)
                {
                    Solid s = o as Solid;
                    foreach (Face face in s.Faces)
                    {
                        if (face.ComputeNormal(new UV()).IsAlmostEqualTo(new XYZ(0, 0, 1)))
                        {
                            topFaceReference = face.Reference;
                        }
                    }
                }
            }

            View      v = GetView(faDoc);
            Reference r = GetTopLevel(faDoc);
            Dimension d = faDoc.FamilyCreate.NewAlignment(v, r, topFaceReference);

            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);
        }
        public void addMaterials(Extrusion pSolid)
        {
            // We assume Material type "Glass" exists. Template "Metric Column.rft" include "Glass",
            // which in fact is the only interesting one to see the effect.
            // In practice, you will want to include in your template.
            //
            // To Do: For the exersize, create it with more appropriate ones in UI, then use the name here.
            //

            // (1)  get the materials id that we are intersted in (e.g., "Glass")
            //
            Material pMat = findElement(typeof(Material), "Glass") as Material;

            if (pMat != null)
            {
                ElementId idMat = pMat.Id;
                // (2a) this add a material to the solid base.  but then, we cannot change it for each column.
                //
                //pSolid.Parameter("Material").Set(idMat)

                // (2b) add a parameter for material finish
                //
                // this time we use instance parameter so that we can change it at instance level.
                //
                FamilyManager   pFamilyMgr        = _doc.FamilyManager;
                ForgeTypeId     builtinParamGroup = new ForgeTypeId(GroupTypeId.Materials.TypeId);
                ForgeTypeId     parametertype     = SpecTypeId.Reference.Material;
                FamilyParameter famParamFinish    = pFamilyMgr.AddParameter("ColumnFinish", builtinParamGroup, parametertype, true);

                // (2b.1) associate material parameter to the family parameter we just added
                //
                //  // 'Autodesk.Revit.DB.Element.get_Parameter(string)' is obsolete in Revit 2015
                //Parameter paramMat = pSolid.get_Parameter("Material");

                /// Updated for Revit 2015
                ///
                Parameter paramMat = pSolid.LookupParameter("Material");

                pFamilyMgr.AssociateElementParameterToFamilyParameter(paramMat, famParamFinish);

                // (2b.2) for our convenience, let's add another type with Glass finish
                //
                addType("Glass", 600.0, 600.0);
                pFamilyMgr.Set(famParamFinish, idMat);
            }
        }