Exemple #1
0
        /// <summary>
        /// Apply Material to Solid
        /// </summary>
        /// <param name="familyDocument"></param>
        /// <param name="material"></param>
        /// <param name="element"></param>
        private static void ApplyMaterialToFreeForm(
            Autodesk.Revit.DB.Document familyDocument,
            Revit.Elements.Material material,
            Autodesk.Revit.DB.FreeFormElement freeform)
        {
            var materialCollector = new Autodesk.Revit.DB.FilteredElementCollector(familyDocument)
                                    .OfClass(typeof(Autodesk.Revit.DB.Material));

            foreach (Autodesk.Revit.DB.Material mat in materialCollector)
            {
                if (mat.Name == material.Name)
                {
                    var materialParam = freeform.get_Parameter(BuiltInParameter.MATERIAL_ID_PARAM);
                    if (materialParam != null && !materialParam.IsReadOnly)
                    {
                        Revit.Elements.InternalUtilities.ElementUtils.SetParameterValue(materialParam, material);
                    }
                }
            }
        }
Exemple #2
0
        public static Dictionary <string, object> GetMaterialProperties(string materialname)
        {
            Revit.Elements.Material mat = Revit.Elements.Material.ByName(materialname);

            RVT.Material material = (RVT.Material)mat.InternalElement;


            // Get the active Document
            Autodesk.Revit.DB.Document document = DocumentManager.Instance.CurrentDBDocument;

            string appearancename = "None";
            List <Autodesk.Revit.DB.Parameter> appearances = new List <Autodesk.Revit.DB.Parameter>();

            if (material.AppearanceAssetId != Autodesk.Revit.DB.ElementId.InvalidElementId)
            {
                RVT.AppearanceAssetElement appearance = (RVT.AppearanceAssetElement)document.GetElement(material.AppearanceAssetId);
                appearancename = appearance.Name;

                foreach (RVT.Parameter parameter in appearance.Parameters)
                {
                    if (!appearances.Contains(parameter))
                    {
                        appearances.Add(parameter);
                    }
                }
            }

            string thermalname = "None";
            List <Autodesk.Revit.DB.Parameter> thermals = new List <Autodesk.Revit.DB.Parameter>();

            if (material.ThermalAssetId != Autodesk.Revit.DB.ElementId.InvalidElementId)
            {
                RVT.PropertySetElement thermal = (RVT.PropertySetElement)document.GetElement(material.ThermalAssetId);
                thermalname = thermal.Name;

                foreach (RVT.Parameter parameter in thermal.Parameters)
                {
                    if (!thermals.Contains(parameter))
                    {
                        thermals.Add(parameter);
                    }
                }
            }

            string structuralname = "None";
            List <Autodesk.Revit.DB.Parameter> structurals = new List <Autodesk.Revit.DB.Parameter>();

            if (material.StructuralAssetId != Autodesk.Revit.DB.ElementId.InvalidElementId)
            {
                RVT.PropertySetElement structural = (RVT.PropertySetElement)document.GetElement(material.StructuralAssetId);
                structuralname = structural.Name;

                foreach (RVT.Parameter parameter in structural.Parameters)
                {
                    if (!structurals.Contains(parameter))
                    {
                        structurals.Add(parameter);
                    }
                }
            }

            return(new Dictionary <string, object>
            {
                { "Name", material.Name },
                { "Id", material.Id.IntegerValue },
                { "Category", material.MaterialCategory },
                { "Class", material.MaterialClass },
                { "Transparency", material.Transparency },
                { "Smoothness", material.Smoothness },
                { "Shininess", material.Shininess },
                { "Color", material.Color },
                { "Appearance Name", appearancename },
                { "Appearance Parameters", appearances },
                { "Structural Name", structuralname },
                { "Structural Parameters", structurals },
                { "Thermal Name", thermalname },
                { "Thermal Parameters", thermals },
            });
        }
Exemple #3
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);
            }
        }