/// <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> /// 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); }