/// <summary>
 /// Add a material info entry to a new or existing material name.
 /// </summary>
 /// <param name="name">The material name.</param>
 /// <param name="info">The material information.</param>
 public void Add(string name, IFCMaterialInfo info)
 {
     IList<IFCMaterialInfo> createdMaterials;
     if (!MaterialCache.TryGetValue(name, out createdMaterials))
     {
         createdMaterials = new List<IFCMaterialInfo>();
         MaterialCache[name] = createdMaterials;
     }
     createdMaterials.Add(info);
 }
        /// <summary>
        /// Add a material info entry to a new or existing material name.
        /// </summary>
        /// <param name="name">The material name.</param>
        /// <param name="info">The material information.</param>
        public void Add(string name, IFCMaterialInfo info)
        {
            IList <IFCMaterialInfo> createdMaterials;

            if (!MaterialCache.TryGetValue(name, out createdMaterials))
            {
                createdMaterials    = new List <IFCMaterialInfo>();
                MaterialCache[name] = createdMaterials;
            }
            createdMaterials.Add(info);
        }
        /// <summary>
        /// Finds a material with the same name and information.
        /// </summary>
        /// <param name="name">The material name.</param>
        /// <param name="id">The id of the material.  We will look for potential matches with the id included in the name.</param>
        /// <param name="info">The material information.</param>
        /// <returns></returns>
        public ElementId FindMatchingMaterial(string name, int id, IFCMaterialInfo info)
        {
            IList<IFCMaterialInfo> createdMaterials;
            if (!MaterialCache.TryGetValue(name, out createdMaterials))
                return ElementId.InvalidElementId;

            int infoTransparency = info.Transparency.HasValue ? info.Transparency.Value : 0;
            foreach (IFCMaterialInfo createdMaterial in createdMaterials)
            {
                if (info.Color != null)
                {
                    if (createdMaterial.Color == null)
                        continue;
                    if ((createdMaterial.Color.Red != info.Color.Red) ||
                        (createdMaterial.Color.Green != info.Color.Green) ||
                        (createdMaterial.Color.Blue != info.Color.Blue))
                        continue;
                }

                int createdMaterialTransparency = createdMaterial.Transparency.HasValue ? createdMaterial.Transparency.Value : 0;
                if (infoTransparency != createdMaterialTransparency)
                    continue;

                if (info.Shininess.HasValue)
                {
                    if (!createdMaterial.Shininess.HasValue)
                        continue;
                    if (info.Shininess.Value != createdMaterial.Shininess.Value)
                        continue;
                }

                if (info.Smoothness.HasValue)
                {
                    if (!createdMaterial.Smoothness.HasValue)
                        continue;
                    if (info.Smoothness.Value != createdMaterial.Smoothness.Value)
                        continue;
                }

                return createdMaterial.ElementId;
            }

            // We found a name match, but it didn't have the right materials.  Try again with id appended to the name.
            string newMaterialName = Importer.TheCache.CreatedMaterials.GetUniqueMaterialName(name, id);
            return FindMatchingMaterial(newMaterialName, id, info);
        }
Example #4
0
        /// <summary>
        /// Set a transparent material (by default) for spaces and openings.
        /// </summary>
        /// <param name="doc">The document.</param>
        /// <param name="category">The category class.</param>
        /// <param name="id">The id of the generating entity.</param>
        /// <param name="subCategoryName">The name of the created (sub-)category.</param>
        private static void SetMaterialForSpacesAndOpenings(Document doc, int id, Category category, string subCategoryName)
        {
            IFCMaterialInfo materialInfo = null;

            if (String.Compare(subCategoryName, "IfcOpeningElement", true) == 0)
            {
                materialInfo = IFCMaterialInfo.Create(new Color(255, 165, 0), 64, null, null, ElementId.InvalidElementId);
            }
            else if (String.Compare(subCategoryName, "IfcSpace", true) == 0)
            {
                materialInfo = IFCMaterialInfo.Create(new Color(164, 232, 232), 64, null, null, ElementId.InvalidElementId);
            }

            if (materialInfo != null)
            {
                ElementId createdElementId = IFCMaterial.CreateMaterialElem(doc, id, subCategoryName, materialInfo);
                if (createdElementId != ElementId.InvalidElementId)
                {
                    Material material = doc.GetElement(createdElementId) as Material;
                    category.Material = material;
                }
            }
        }
        /// <summary>
        /// Finds a material with the same name and information.
        /// </summary>
        /// <param name="name">The material name.</param>
        /// <param name="id">The id of the material.  We will look for potential matches with the id included in the name.</param>
        /// <param name="info">The material information.</param>
        /// <returns></returns>
        public ElementId FindMatchingMaterial(string name, int id, IFCMaterialInfo info)
        {
            IList <IFCMaterialInfo> createdMaterials;

            if (!MaterialCache.TryGetValue(name, out createdMaterials))
            {
                return(ElementId.InvalidElementId);
            }

            int infoTransparency = info.Transparency.HasValue ? info.Transparency.Value : 0;

            foreach (IFCMaterialInfo createdMaterial in createdMaterials)
            {
                if (info.Color != null)
                {
                    if (createdMaterial.Color == null)
                    {
                        continue;
                    }
                    if ((createdMaterial.Color.Red != info.Color.Red) ||
                        (createdMaterial.Color.Green != info.Color.Green) ||
                        (createdMaterial.Color.Blue != info.Color.Blue))
                    {
                        continue;
                    }
                }

                int createdMaterialTransparency = createdMaterial.Transparency.HasValue ? createdMaterial.Transparency.Value : 0;
                if (infoTransparency != createdMaterialTransparency)
                {
                    continue;
                }

                if (info.Shininess.HasValue)
                {
                    if (!createdMaterial.Shininess.HasValue)
                    {
                        continue;
                    }
                    if (info.Shininess.Value != createdMaterial.Shininess.Value)
                    {
                        continue;
                    }
                }

                if (info.Smoothness.HasValue)
                {
                    if (!createdMaterial.Smoothness.HasValue)
                    {
                        continue;
                    }
                    if (info.Smoothness.Value != createdMaterial.Smoothness.Value)
                    {
                        continue;
                    }
                }

                return(createdMaterial.ElementId);
            }

            // We found a name match, but it didn't have the right materials.  Try again with id appended to the name.
            string newMaterialName = Importer.TheCache.CreatedMaterials.GetUniqueMaterialName(name, id);

            return(FindMatchingMaterial(newMaterialName, id, info));
        }
        /// <summary>
        /// Create a Revit Material.
        /// </summary>
        /// <param name="doc">The document.</param>
        /// <param name="id">The id of the IFCEntity, used to avoid creating duplicate material names.</param>
        /// <param name="originalName">The base name of the material.</param>
        /// <param name="materialInfo">The material information.</param>
        /// <returns>The element id.</returns>
        public static ElementId CreateMaterialElem(Document doc, int id, string originalName, IFCMaterialInfo materialInfo)
        {
            ElementId createdElementId = Importer.TheCache.CreatedMaterials.FindMatchingMaterial(originalName, id, materialInfo);
            if (createdElementId != ElementId.InvalidElementId)
                return createdElementId;

            string revitMaterialName = GetMaterialName(id, originalName);
            
            createdElementId = Material.Create(doc, revitMaterialName);
            if (createdElementId == ElementId.InvalidElementId)
                return createdElementId;

            materialInfo.ElementId = createdElementId;
            Importer.TheCache.CreatedMaterials.Add(originalName, materialInfo);

            // Get info.
            Material materialElem = doc.GetElement(createdElementId) as Material;
            if (materialElem == null)
                return ElementId.InvalidElementId;

            bool materialHasValidColor = false;

            // We don't want an invalid value set below to prevent creating an element; log the message and move on.
            try
            {
                if (materialInfo.Color != null)
                {
                    materialElem.Color = materialInfo.Color;
                    materialHasValidColor = true;
                }
                else
                {
                    materialElem.Color = new Color(127, 127, 127);
                }

                if (materialInfo.Transparency.HasValue)
                    materialElem.Transparency = materialInfo.Transparency.Value;

                if (materialInfo.Shininess.HasValue)
                    materialElem.Shininess = materialInfo.Shininess.Value;

                if (materialInfo.Smoothness.HasValue)
                    materialElem.Smoothness = materialInfo.Smoothness.Value;
            }
            catch (Exception ex)
            {
                Importer.TheLog.LogError(id, "Couldn't set some Material values: " + ex.Message, false);
            }

            if (!materialHasValidColor)
                Importer.TheCache.MaterialsWithNoColor.Add(createdElementId);

            if (Importer.TheOptions.VerboseLogging)
            {
                string comment = "Created Material: " + revitMaterialName
                    + " with color: (" + materialElem.Color.Red + ", " + materialElem.Color.Green + ", " + materialElem.Color.Blue + ") "
                    + "transparency: " + materialElem.Transparency + " shininess: " + materialElem.Shininess + " smoothness: " + materialElem.Smoothness;
                Importer.TheLog.LogComment(id, comment, false);
            }

            Importer.TheLog.AddCreatedMaterial(doc, createdElementId);
            return createdElementId;
        }