/// <summary> /// Creates an IFCFile object from an IFC file. /// </summary> /// <param name="path">The IFC file path.</param> /// <param name="schemaVersion">The schema version.</param> /// <returns>The IFCFile.</returns> static IFCFile CreateIFCFile(string path, out IFCSchemaVersion schemaVersion) { schemaVersion = IFCSchemaVersion.IFC2x3; if (!File.Exists(path)) { throw new ArgumentException("File does not exist."); } string fileExt = Path.GetExtension(path); if (string.Compare(fileExt, ".ifc", true) == 0) { return(CreateIFCFileFromIFC(path, out schemaVersion)); } if (string.Compare(fileExt, ".ifcxml", true) == 0) { return(CreateIFCFileFromIFCXML(path, out schemaVersion)); } if (string.Compare(fileExt, ".ifczip", true) == 0) { return(CreateIFCFileFromIFCZIP(path, out schemaVersion)); } throw new ArgumentException("Unknown file format."); }
/// <summary> /// Gets IFCFileModelOptions from schema name. /// </summary> /// <param name="schemaName">The schema name.</param> /// <param name="schemaVersion">The calculated schema version from the schema name. Default is IFC2x3.</param> /// <returns>The IFCFileModelOptions.</returns> static IFCFileModelOptions GetIFCFileModelOptions(string schemaName, out IFCSchemaVersion schemaVersion) { IFCFileModelOptions modelOptions = new IFCFileModelOptions(); modelOptions.SchemaName = schemaName; schemaVersion = IFCSchemaVersion.IFC2x3; // Default, should be overridden. if (OverrideSchemaFileName != null) { modelOptions.SchemaFile = OverrideSchemaFileName; } else if (string.Compare(schemaName, "IFC2X3", true) == 0) { modelOptions.SchemaFile = Path.Combine(RevitProgramPath, "EDM\\IFC2X3_TC1.exp"); schemaVersion = IFCSchemaVersion.IFC2x3; } else if (string.Compare(schemaName, "IFC2X_FINAL", true) == 0) { modelOptions.SchemaFile = Path.Combine(RevitProgramPath, "EDM\\IFC2X_PROXY.exp"); schemaVersion = IFCSchemaVersion.IFC2x; } else if (string.Compare(schemaName, "IFC2X2_FINAL", true) == 0) { modelOptions.SchemaFile = Path.Combine(RevitProgramPath, "EDM\\IFC2X2_ADD1.exp"); schemaVersion = IFCSchemaVersion.IFC2x2; } else if (string.Compare(schemaName, "IFC4", true) == 0) { // We will still temporarily support the old IFC4.exp file. string ifc4Add1Path = Path.Combine(RevitProgramPath, "EDM\\IFC4_ADD1.exp"); string ifc4Add2Path = Path.Combine(RevitProgramPath, "EDM\\IFC4_ADD2.exp"); if (File.Exists(ifc4Add2Path)) { modelOptions.SchemaFile = ifc4Add2Path; schemaVersion = IFCSchemaVersion.IFC4Add2; } else if (File.Exists(ifc4Add1Path)) { modelOptions.SchemaFile = ifc4Add1Path; schemaVersion = IFCSchemaVersion.IFC4Add1; } else { modelOptions.SchemaFile = Path.Combine(RevitProgramPath, "EDM\\IFC4.exp"); schemaVersion = IFCSchemaVersion.IFC4; } } else { throw new ArgumentException("Invalid or unsupported schema: " + schemaName); } return(modelOptions); }
/// <summary> /// Creates an IFCFile object from an IFC Zip file. /// </summary> /// <param name="path">The file path.</param> /// <param name="schemaVersion">The schema version.</param> /// <returns>The IFCFile.</returns> static IFCFile CreateIFCFileFromIFCZIP(string path, out IFCSchemaVersion schemaVersion) { string tempFolderName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); try { string extractedFileName = ExtractZipFile(path, null, tempFolderName); return(CreateIFCFile(extractedFileName, out schemaVersion)); } finally { try { Directory.Delete(tempFolderName, true); } catch { } // best effort } }
/// <summary> /// Gets IFCFileModelOptions from schema name. /// </summary> /// <param name="schemaName">The schema name.</param> /// <param name="schemaVersion">The calculated schema version from the schema name. Default is IFC2x3.</param> /// <returns>The IFCFileModelOptions.</returns> static IFCFileModelOptions GetIFCFileModelOptions(string schemaName, out IFCSchemaVersion schemaVersion) { IFCFileModelOptions modelOptions = new IFCFileModelOptions(); modelOptions.SchemaName = schemaName; schemaVersion = IFCSchemaVersion.IFC2x3; // Default, should be overridden. if (OverrideSchemaFileName != null) { modelOptions.SchemaFile = OverrideSchemaFileName; } else if (string.Compare(schemaName, "IFC2X3", true) == 0) { modelOptions.SchemaFile = Path.Combine(RevitProgramPath, "EDM\\IFC2X3_TC1.exp"); schemaVersion = IFCSchemaVersion.IFC2x3; } else if (string.Compare(schemaName, "IFC2X_FINAL", true) == 0) { modelOptions.SchemaFile = Path.Combine(RevitProgramPath, "EDM\\IFC2X_PROXY.exp"); schemaVersion = IFCSchemaVersion.IFC2x; } else if (string.Compare(schemaName, "IFC2X2_FINAL", true) == 0) { modelOptions.SchemaFile = Path.Combine(RevitProgramPath, "EDM\\IFC2X2_ADD1.exp"); schemaVersion = IFCSchemaVersion.IFC2x2; } else if (string.Compare(schemaName, "IFC4", true) == 0) { modelOptions.SchemaFile = Path.Combine(RevitProgramPath, "EDM\\IFC4.exp"); schemaVersion = IFCSchemaVersion.IFC4; } else { throw new ArgumentException("Invalid or unsupported schema: " + schemaName); } return(modelOptions); }
/// <summary> /// Do a Parametric import operation. /// </summary> /// <param name="importer">The internal ImporterIFC class that contains necessary information for the import.</param> /// <remarks>This is a thin wrapper to the native code that still handles Open IFC. This should be eventually obsoleted.</remarks> public static void Import(ImporterIFC importer) { IFCFile ifcFile = null; try { IFCSchemaVersion schemaVersion = IFCSchemaVersion.IFC2x3; ifcFile = CreateIFCFile(importer.FullFileName, out schemaVersion); IFCFileReadOptions readOptions = new IFCFileReadOptions(); readOptions.FileName = importer.FullFileName; readOptions.XMLConfigFileName = Path.Combine(DirectoryUtil.RevitProgramPath, "EDM\\ifcXMLconfiguration.xml"); ifcFile.Read(readOptions); importer.SetFile(ifcFile); //If there is more than one project, we will be ignoring all but the first one. IList <IFCAnyHandle> projects = ifcFile.GetInstances(IFCAnyHandleUtil.GetIFCEntityTypeName(IFCEntityType.IfcProject), false); if (projects.Count == 0) { throw new InvalidOperationException("Failed to import IFC to Revit."); } IFCAnyHandle project = projects[0]; importer.ProcessIFCProject(project); StoreIFCCreatorInfo(ifcFile, importer.Document.ProjectInformation); } finally { if (ifcFile != null) { ifcFile.Close(); ifcFile = null; } } }
/// <summary> /// Creates an IFCFile object from an IFC file. /// </summary> /// <param name="path">The IFC file path.</param> /// <param name="schemaVersion">The schema version.</param> /// <returns>The IFCFile.</returns> static IFCFile CreateIFCFile(string path, out IFCSchemaVersion schemaVersion) { schemaVersion = IFCSchemaVersion.IFC2x3; if (!File.Exists(path)) { throw new ArgumentException("File does not exist"); } IFCFile ifcFile = null; string fileExt = Path.GetExtension(path); if (string.Compare(fileExt, ".ifc", true) == 0) { ifcFile = CreateIFCFileFromIFC(path, out schemaVersion); } else if (string.Compare(fileExt, ".ifcxml", true) == 0) { ifcFile = CreateIFCFileFromIFCXML(path, out schemaVersion); } else if (string.Compare(fileExt, ".ifczip", true) == 0) { ifcFile = CreateIFCFileFromIFCZIP(path, out schemaVersion); } else { throw new ArgumentException("Unknown file format"); } if (ifcFile == null) { throw new ArgumentException("Invalid IFC file"); } return(ifcFile); }
/// <summary> /// Creates an IFCFile object from an IFC XML file. /// </summary> /// <param name="path">The file path.</param> /// <param name="schemaVersion">The schema version.</param> /// <returns>The IFCFile.</returns> static IFCFile CreateIFCFileFromIFCXML(string path, out IFCSchemaVersion schemaVersion) { IFCFile file = null; string schemaName = null; schemaVersion = IFCSchemaVersion.IFC2x3; // This is an optional location to find the schema name - it may not be supplied. using (XmlReader reader = XmlReader.Create(new StreamReader(path))) { reader.ReadToFollowing("doc:express"); reader.MoveToAttribute("schema_name"); schemaName = reader.Value.Replace(" ", "").Replace("\t", "").Replace("\r", "").Replace("\n", ""); } // This is an alternate location compatible with some MAP ifcXML files. if (string.IsNullOrEmpty(schemaName)) { using (XmlReader reader = XmlReader.Create(new StreamReader(path))) { reader.ReadToFollowing("doc:iso_10303_28"); reader.MoveToAttribute("xmlns:schemaLocation"); int ifcLoc = reader.Value.IndexOf("IFC"); if (ifcLoc >= 0) { string tmpName = reader.Value.Substring(ifcLoc); int ifcEndLoc = tmpName.IndexOf('/'); if (ifcEndLoc > 0) { schemaName = tmpName.Substring(0, ifcEndLoc); } } } } // This checks to see if we have an unsupported IFC2X3_RC1 file. if (string.IsNullOrEmpty(schemaName)) { using (XmlReader reader = XmlReader.Create(new StreamReader(path))) { reader.ReadToFollowing("ex:iso_10303_28"); reader.MoveToAttribute("xmlns:ifc"); int ifcLoc = reader.Value.IndexOf("IFC"); if (ifcLoc >= 0) { schemaName = reader.Value.Substring(ifcLoc); } } } if (!string.IsNullOrEmpty(schemaName)) { IFCFileModelOptions modelOptions = GetIFCFileModelOptions(schemaName, out schemaVersion); file = IFCFile.Create(modelOptions); } if (file == null) { throw new InvalidOperationException("Can't determine XML file schema."); } return(file); }
/// <summary> /// Creates an IFCFile object from a standard IFC file. /// </summary> /// <param name="path">The file path.</param> /// <param name="schemaVersion">The schema version.</param> /// <returns>The IFCFile.</returns> static IFCFile CreateIFCFileFromIFC(string path, out IFCSchemaVersion schemaVersion) { string schemaString = string.Empty; string schemaName = null; using (StreamReader sr = new StreamReader(path)) { string schemaKeyword = "FILE_SCHEMA(("; bool found = false; while (sr.Peek() >= 0) { string lineString = schemaString + sr.ReadLine(); lineString = lineString.Replace(" ", "").Replace("\t", "").Replace("\r", "").Replace("\n", ""); string[] schemaNames = lineString.Split(';'); for (int ii = 0; ii < schemaNames.Length; ii++) { schemaString = schemaNames[ii]; int idx = schemaString.IndexOf(schemaKeyword); int schemaIdxStart = -1; int schemaIdxEnd = -1; if (idx != -1) { idx += schemaKeyword.Length; if (idx < schemaString.Length && schemaString[idx] == '\'') { schemaIdxStart = ++idx; for (; idx < schemaString.Length; idx++) { if (schemaString[idx] == '\'') { schemaIdxEnd = idx; found = true; break; } } } } if (found) { schemaName = schemaString.Substring(schemaIdxStart, schemaIdxEnd - schemaIdxStart); break; } } if (found) { break; } } } IFCFile file = null; schemaVersion = IFCSchemaVersion.IFC2x3; if (!string.IsNullOrEmpty(schemaName)) { IFCFileModelOptions modelOptions = GetIFCFileModelOptions(schemaName, out schemaVersion); file = IFCFile.Create(modelOptions); } return(file); }
/// <summary> /// Gets IFCFileModelOptions from schema name. /// </summary> /// <param name="schemaName">The schema name.</param> /// <param name="schemaVersion">The calculated schema version from the schema name. Default is IFC2x3.</param> /// <returns>The IFCFileModelOptions.</returns> static IFCFileModelOptions GetIFCFileModelOptions(string schemaName, out IFCSchemaVersion schemaVersion) { IFCFileModelOptions modelOptions = new IFCFileModelOptions(); modelOptions.SchemaName = schemaName; schemaVersion = IFCSchemaVersion.IFC2x3; // Default, should be overridden. if (OverrideSchemaFileName != null) { modelOptions.SchemaFile = OverrideSchemaFileName; } else if (string.Compare(schemaName, "IFC2X3", true) == 0) { modelOptions.SchemaFile = Path.Combine(RevitProgramPath, "EDM\\IFC2X3_TC1.exp"); schemaVersion = IFCSchemaVersion.IFC2x3; } else if (string.Compare(schemaName, "IFC2X_FINAL", true) == 0) { modelOptions.SchemaFile = Path.Combine(RevitProgramPath, "EDM\\IFC2X_PROXY.exp"); schemaVersion = IFCSchemaVersion.IFC2x; } else if (string.Compare(schemaName, "IFC2X2_FINAL", true) == 0) { modelOptions.SchemaFile = Path.Combine(RevitProgramPath, "EDM\\IFC2X2_ADD1.exp"); schemaVersion = IFCSchemaVersion.IFC2x2; } else if (string.Compare(schemaName, "IFC4", true) == 0) { modelOptions.SchemaFile = Path.Combine(RevitProgramPath, "EDM\\IFC4.exp"); schemaVersion = IFCSchemaVersion.IFC4; } else throw new ArgumentException("Invalid or unsupported schema: " + schemaName); return modelOptions; }
/// <summary> /// Creates an IFCFile object from an IFC Zip file. /// </summary> /// <param name="path">The file path.</param> /// <param name="schemaVersion">The schema version.</param> /// <returns>The IFCFile.</returns> static IFCFile CreateIFCFileFromIFCZIP(string path, out IFCSchemaVersion schemaVersion) { string tempFolderName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); try { string extractedFileName = ExtractZipFile(path, null, tempFolderName); return CreateIFCFile(extractedFileName, out schemaVersion); } finally { try { Directory.Delete(tempFolderName, true); } catch { } // best effort } }
/// <summary> /// Creates an IFCFile object from an IFC XML file. /// </summary> /// <param name="path">The file path.</param> /// <param name="schemaVersion">The schema version.</param> /// <returns>The IFCFile.</returns> static IFCFile CreateIFCFileFromIFCXML(string path, out IFCSchemaVersion schemaVersion) { IFCFile file = null; string schemaName = null; schemaVersion = IFCSchemaVersion.IFC2x3; // This is an optional location to find the schema name - it may not be supplied. using (XmlReader reader = XmlReader.Create(new StreamReader(path))) { reader.ReadToFollowing("doc:express"); reader.MoveToAttribute("schema_name"); schemaName = reader.Value.Replace(" ", "").Replace("\t", "").Replace("\r", "").Replace("\n", ""); } // This is an alternate location compatible with some MAP ifcXML files. if (string.IsNullOrEmpty(schemaName)) { using (XmlReader reader = XmlReader.Create(new StreamReader(path))) { reader.ReadToFollowing("doc:iso_10303_28"); reader.MoveToAttribute("xmlns:schemaLocation"); int ifcLoc = reader.Value.IndexOf("IFC"); if (ifcLoc >= 0) { string tmpName = reader.Value.Substring(ifcLoc); int ifcEndLoc = tmpName.IndexOf('/'); if (ifcEndLoc > 0) schemaName = tmpName.Substring(0, ifcEndLoc); } } } // This checks to see if we have an unsupported IFC2X3_RC1 file. if (string.IsNullOrEmpty(schemaName)) { using (XmlReader reader = XmlReader.Create(new StreamReader(path))) { reader.ReadToFollowing("ex:iso_10303_28"); reader.MoveToAttribute("xmlns:ifc"); int ifcLoc = reader.Value.IndexOf("IFC"); if (ifcLoc >= 0) schemaName = reader.Value.Substring(ifcLoc); } } if (!string.IsNullOrEmpty(schemaName)) { IFCFileModelOptions modelOptions = GetIFCFileModelOptions(schemaName, out schemaVersion); file = IFCFile.Create(modelOptions); } if (file == null) throw new InvalidOperationException("Can't determine XML file schema."); return file; }
/// <summary> /// Creates an IFCFile object from a standard IFC file. /// </summary> /// <param name="path">The file path.</param> /// <param name="schemaVersion">The schema version.</param> /// <returns>The IFCFile.</returns> static IFCFile CreateIFCFileFromIFC(string path, out IFCSchemaVersion schemaVersion) { string schemaString = string.Empty; string schemaName = null; using (StreamReader sr = new StreamReader(path)) { string schemaKeyword = "FILE_SCHEMA(("; bool found = false; while (sr.Peek() >= 0) { string lineString = schemaString + sr.ReadLine(); lineString = lineString.Replace(" ", "").Replace("\t", "").Replace("\r", "").Replace("\n", ""); string[] schemaNames = lineString.Split(';'); for (int ii = 0; ii < schemaNames.Length; ii++) { schemaString = schemaNames[ii]; int idx = schemaString.IndexOf(schemaKeyword); int schemaIdxStart = -1; int schemaIdxEnd = -1; if (idx != -1) { idx += schemaKeyword.Length; if (idx < schemaString.Length && schemaString[idx] == '\'') { schemaIdxStart = ++idx; for (; idx < schemaString.Length; idx++) { if (schemaString[idx] == '\'') { schemaIdxEnd = idx; found = true; break; } } } } if (found) { schemaName = schemaString.Substring(schemaIdxStart, schemaIdxEnd - schemaIdxStart); break; } } if (found) break; } } IFCFile file = null; schemaVersion = IFCSchemaVersion.IFC2x3; if (!string.IsNullOrEmpty(schemaName)) { IFCFileModelOptions modelOptions = GetIFCFileModelOptions(schemaName, out schemaVersion); file = IFCFile.Create(modelOptions); } return file; }
/// <summary> /// Creates an IFCFile object from an IFC file. /// </summary> /// <param name="path">The IFC file path.</param> /// <param name="schemaVersion">The schema version.</param> /// <returns>The IFCFile.</returns> static IFCFile CreateIFCFile(string path, out IFCSchemaVersion schemaVersion) { schemaVersion = IFCSchemaVersion.IFC2x3; if (!File.Exists(path)) { throw new ArgumentException("File does not exist."); } string fileExt = Path.GetExtension(path); if (string.Compare(fileExt, ".ifc", true) == 0) return CreateIFCFileFromIFC(path, out schemaVersion); if (string.Compare(fileExt, ".ifcxml", true) == 0) return CreateIFCFileFromIFCXML(path, out schemaVersion); if (string.Compare(fileExt, ".ifczip", true) == 0) return CreateIFCFileFromIFCZIP(path, out schemaVersion); throw new ArgumentException("Unknown file format."); }