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