コード例 #1
0
        public static IList <GeometryObject> ToRevitType(
            this Autodesk.DesignScript.Geometry.Solid solid,
            TessellatedShapeBuilderTarget target     = TessellatedShapeBuilderTarget.Mesh,
            TessellatedShapeBuilderFallback fallback = TessellatedShapeBuilderFallback.Salvage,
            ElementId MaterialId           = null,
            bool performHostUnitConversion = true)
        {
            var rp = new DefaultRenderPackage();

            if (performHostUnitConversion)
            {
                var newSolid = solid.InHostUnits();
                newSolid.Tessellate(rp, new TessellationParameters());
                newSolid.Dispose();
            }
            else
            {
                solid.Tessellate(rp, new TessellationParameters());
            }

            var tsb = new TessellatedShapeBuilder()
            {
                Fallback = fallback, Target = target, GraphicsStyleId = ElementId.InvalidElementId
            };

            tsb.OpenConnectedFaceSet(false);

            var v = rp.MeshVertices.ToList();

            for (int i = 0; i < v.Count; i += 9)
            {
                var a = new XYZ(v[i], v[i + 1], v[i + 2]);
                var b = new XYZ(v[i + 3], v[i + 4], v[i + 5]);
                var c = new XYZ(v[i + 6], v[i + 7], v[i + 8]);

                var face = new TessellatedFace(new List <XYZ>()
                {
                    a, b, c
                }, MaterialId != null ? MaterialId : MaterialsManager.Instance.DynamoMaterialId);
                tsb.AddFace(face);
            }

            tsb.CloseConnectedFaceSet();
            tsb.Build();
            var result = tsb.GetBuildResult();

            return(result.GetGeometricalObjects());
        }
コード例 #2
0
        /// <summary>
        /// Convert a DS Solid to a Revit FamilySymbol containing
        /// Revit FreeForms via SAT Export/Import
        /// </summary>
        /// <param name="solidGeometry"></param>
        /// <param name="name"></param>
        /// <param name="category"></param>
        /// <param name="templatePath">Revit Template to use for Family Creation</param>
        /// <param name="material">Can be null for Voids</param>
        /// <param name="isVoid">Create Void</param>
        /// <param name="subcategory">Can be string.Empty for Voids</param>
        /// <returns></returns>
        public static Autodesk.Revit.DB.FamilySymbol ToRevitFamilyType(
            this Autodesk.DesignScript.Geometry.Solid solidGeometry,
            string name,
            Revit.Elements.Category category,
            string templatePath,
            Revit.Elements.Material material,
            bool isVoid,
            string subcategory = "")
        {
            // Keep the current document and close the open transaction
            Autodesk.Revit.DB.Document document = DocumentManager.Instance.CurrentDBDocument;
            TransactionManager.Instance.ForceCloseTransaction();

            // create a temp sat file
            string tempFile = System.IO.Path.GetTempFileName() + ".sat";

            // create a temp family file
            string tempDir        = System.IO.Path.GetTempPath();
            string tempFamilyFile = System.IO.Path.Combine(tempDir, name + ".rfa");

            // scale the incoming geometry
            solidGeometry = solidGeometry.InHostUnits();

            // get a displacement vector
            Vector vector = Vector.ByTwoPoints(Autodesk.DesignScript.Geometry.BoundingBox.ByGeometry(solidGeometry).MinPoint, Autodesk.DesignScript.Geometry.Point.Origin());

            // translate the geometry to origin
            solidGeometry = solidGeometry.Translate(vector) as Autodesk.DesignScript.Geometry.Solid;

            // export geometry to SAT
            solidGeometry.ExportToSAT(tempFile);
            solidGeometry.Dispose();

            // create a new family document using the supplied template
            Autodesk.Revit.DB.Document familyDocument = document.Application.NewFamilyDocument(templatePath);

            // Get the families 3d view
            var collector = new Autodesk.Revit.DB.FilteredElementCollector(familyDocument).OfClass(typeof(Autodesk.Revit.DB.View));

            Autodesk.Revit.DB.View view = null;
            foreach (Autodesk.Revit.DB.View v in collector.ToElements())
            {
                if (!v.IsTemplate && v.ViewType == ViewType.ThreeD)
                {
                    view = v;
                }
            }

            // Open a Transaction with the FamilyDocument
            TransactionManager.Instance.EnsureInTransaction(familyDocument);

            // Import the sat file to origin in feet
            ElementId importedElementId = familyDocument.Import(tempFile, new SATImportOptions()
            {
                Placement = ImportPlacement.Origin, Unit = ImportUnit.Foot
            }, view);

            // get the solid element from the imported sat file
            var solids = GetSolidsFromElement(familyDocument.GetElement(importedElementId));

            // delete imported sat
            familyDocument.Delete(importedElementId);
            System.IO.File.Delete(tempFile);

            // Set the families category
            familyDocument.OwnerFamily.FamilyCategory = familyDocument.Settings.Categories.get_Item(category.Name);

            foreach (var solid in solids)
            {
                // Create Freeform Element
                var freeform = FreeFormElement.Create(familyDocument, solid);

                // if the geometry should be void set parameters accordingly
                if (isVoid)
                {
                    ApplyVoidSettingsToFreeForm(freeform);
                }
                else
                {
                    // Apply material if supplied
                    ApplyMaterialToFreeForm(familyDocument, material, freeform);

                    // Apply Subcategory if supplied
                    if (subcategory != string.Empty)
                    {
                        ApplySubCategoryToFreeForm(familyDocument, subcategory, freeform);
                    }
                }
            }

            // Close the FamilyDocument Transaction
            TransactionManager.Instance.ForceCloseTransaction();

            // Save Family document and load it into the project
            familyDocument.SaveAs(tempFamilyFile, new SaveAsOptions()
            {
                OverwriteExistingFile = true
            });
            var family = familyDocument.LoadFamily(document, new FamilyImportOptions());

            // close and delete family
            familyDocument.Close(false);
            System.IO.File.Delete(tempFamilyFile);

            // get first imported family symbol
            var symbols = family.GetFamilySymbolIds();

            // Restore the Project Document Transaction
            TransactionManager.Instance.EnsureInTransaction(document);

            if (symbols.Count > 0)
            {
                FamilySymbol symbol = (FamilySymbol)document.GetElement(symbols.First());

                // activate symbol
                if (!symbol.IsActive)
                {
                    symbol.Activate();
                }

                return(symbol);
            }
            else
            {
                return(null);
            }
        }