/// <summary> /// Creates an IFCImportFile from a file on the disk. /// </summary> /// <param name="ifcFilePath">The path of the file.</param> /// <param name="options">The IFC import options.</param> /// <param name="doc">The optional document argument. If importing into Revit, not supplying a document may reduce functionality later.</param> /// <returns>The IFCImportFile.</returns> public static IFCImportFile Create(string ifcFilePath, IFCImportOptions options, Document doc) { m_sIFCImportFile = new IFCImportFile(); bool success = TheFile.Process(ifcFilePath, options, doc); if (success) { // Store the original levels in the template file for Open IFC. On export, we will delete these levels if we created any. // Note that we always have to preserve one level, regardless of what the ActiveView is. // TODO: Only for open, not import. if (doc != null) { IFCBuildingStorey.ExistingLevelIdToReuse = ElementId.InvalidElementId; View activeView = doc.ActiveView; if (activeView != null) { Level genLevel = activeView.GenLevel; if (genLevel != null) { IFCBuildingStorey.ExistingLevelIdToReuse = genLevel.Id; } } FilteredElementCollector levelCollector = new FilteredElementCollector(doc); ICollection <Element> levels = levelCollector.OfClass(typeof(Level)).ToElements(); ICollection <ElementId> levelIdsToDelete = new HashSet <ElementId>(); foreach (Element level in levels) { if (IFCBuildingStorey.ExistingLevelIdToReuse == ElementId.InvalidElementId) { IFCBuildingStorey.ExistingLevelIdToReuse = level.Id; } else if (level.Id != IFCBuildingStorey.ExistingLevelIdToReuse) { levelIdsToDelete.Add(level.Id); } } doc.Delete(levelIdsToDelete); // Collect material names, to avoid reusing. FilteredElementCollector materialCollector = new FilteredElementCollector(doc); ICollection <Element> materials = materialCollector.OfClass(typeof(Material)).ToElements(); foreach (Element materialAsElem in materials) { Material material = materialAsElem as Material; IFCMaterialInfo info = IFCMaterialInfo.Create(material.Color, material.Transparency, material.Shininess, material.Smoothness, material.Id); Importer.TheCache.CreatedMaterials.Add(material.Name, info); } } } else { // Close up the log file, set m_sIFCImportFile to null. TheFile.Close(); } return(m_sIFCImportFile); }
/// <summary> /// Creates a Revit material based on the information contained in this class. /// </summary> /// <param name="doc">The document.</param> public void Create(Document doc) { // TODO: support cut pattern id and cut pattern color. try { string name = Name; if (string.IsNullOrEmpty(name)) { name = String.Format(Resources.IFCDefaultMaterialName, Id); } if (m_CreatedElementId == ElementId.InvalidElementId && IsValidForCreation) { IFCSurfaceStyle surfaceStyle = GetSurfaceStyle(); if (surfaceStyle != null) { m_CreatedElementId = surfaceStyle.Create(doc, name, null, Id); } else { IFCMaterialInfo materialInfo = IFCMaterialInfo.Create(null, null, null, null, ElementId.InvalidElementId); m_CreatedElementId = CreateMaterialElem(doc, Id, Name, materialInfo); } } else { IsValidForCreation = false; } } catch (Exception ex) { IsValidForCreation = false; Importer.TheLog.LogCreationError(this, ex.Message, false); } }
/// <summary> /// Create the material associated to the element, and return the id. /// </summary> /// <param name="doc">The document.</param> /// <param name="forcedName">An optional name that sets the name of the material created, regardless of surface style name.</param> /// <param name="suggestedName">An optional name that suggests the name of the material created, if the surface style name is null.</param> /// <param name="idOverride">The id of the parent item, used if forcedName is used.</param> /// <returns>The material id.</returns> /// <remarks>If forcedName is not null, this will not store the created element id in this class.</remarks> public ElementId Create(Document doc, string forcedName, string suggestedName, int idOverride) { try { bool overrideName = (forcedName != null) && (string.Compare(forcedName, Name) != 0); if (!overrideName && m_CreatedElementId != ElementId.InvalidElementId) { return(m_CreatedElementId); } string name = overrideName ? forcedName : Name; if (string.IsNullOrEmpty(name)) { if (!string.IsNullOrEmpty(suggestedName)) { name = suggestedName; } else { name = "IFC Surface Style"; } } int id = overrideName ? idOverride : Id; if (IsValidForCreation) { Color color = null; int? transparency = null; int? shininess = null; int? smoothness = null; IFCSurfaceStyleShading shading = ShadingStyle; if (shading != null) { color = shading.GetSurfaceColor(); transparency = (int)(shading.Transparency * 100 + 0.5); shininess = shading.GetShininess(); smoothness = shading.GetSmoothness(); } IFCMaterialInfo materialInfo = IFCMaterialInfo.Create(color, transparency, shininess, smoothness, ElementId.InvalidElementId); ElementId createdElementId = IFCMaterial.CreateMaterialElem(doc, id, name, materialInfo); if (!overrideName) { m_CreatedElementId = createdElementId; } return(createdElementId); } else { IsValidForCreation = false; } } catch (Exception ex) { IsValidForCreation = false; Importer.TheLog.LogCreationError(this, ex.Message, false); } return(ElementId.InvalidElementId); }
/// <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); }
/// <summary> /// Creates an IFCImportFile from a file on the disk. /// </summary> /// <param name="ifcFilePath">The path of the file.</param> /// <param name="options">The IFC import options.</param> /// <param name="doc">The optional document argument. If importing into Revit, not supplying a document may reduce functionality later.</param> /// <returns>The IFCImportFile.</returns> public static IFCImportFile Create(string ifcFilePath, IFCImportOptions options, Document doc) { m_sIFCImportFile = new IFCImportFile(); bool success = TheFile.Process(ifcFilePath, options, doc); if (success) { // Store the original levels in the template file for Open IFC. On export, we will delete these levels if we created any. // Note that we always have to preserve one level, regardless of what the ActiveView is. if (doc != null) { IFCBuildingStorey.ExistingLevelIdToReuse = ElementId.InvalidElementId; View activeView = doc.ActiveView; if (activeView != null) { Level genLevel = activeView.GenLevel; if (genLevel != null) { IFCBuildingStorey.ExistingLevelIdToReuse = genLevel.Id; } } // For Link IFC, we will delete any unused levels at the end. Instead, we want to try to reuse them. // The for loop does a little unnecessary work if deleteLevelsNow, but the performance implications are very small. bool deleteLevelsNow = (Importer.TheOptions.Action != IFCImportAction.Link); FilteredElementCollector levelCollector = new FilteredElementCollector(doc); ICollection <Element> levels = levelCollector.OfClass(typeof(Level)).ToElements(); ICollection <ElementId> levelIdsToDelete = new HashSet <ElementId>(); foreach (Element level in levels) { if (level == null) { continue; } if (IFCBuildingStorey.ExistingLevelIdToReuse == ElementId.InvalidElementId) { IFCBuildingStorey.ExistingLevelIdToReuse = level.Id; } else if (level.Id != IFCBuildingStorey.ExistingLevelIdToReuse) { levelIdsToDelete.Add(level.Id); } } if (deleteLevelsNow) { doc.Delete(levelIdsToDelete); } // Collect material names, to avoid reusing. FilteredElementCollector materialCollector = new FilteredElementCollector(doc); ICollection <Element> materials = materialCollector.OfClass(typeof(Material)).ToElements(); foreach (Element materialAsElem in materials) { Material material = materialAsElem as Material; IFCMaterialInfo info = IFCMaterialInfo.Create(material.Color, material.Transparency, material.Shininess, material.Smoothness, material.Id); Importer.TheCache.CreatedMaterials.Add(material.Name, info); } } } else { // Close up the log file, set m_sIFCImportFile to null. TheFile.Close(); } return(m_sIFCImportFile); }