/// <summary> /// Allow for the creation of an Importer class for external API use. /// </summary> /// <param name="originalDocument">The document to import into.</param> /// <param name="ifcFileName">The name of the IFC file.</param> /// <param name="importOptions">The import options associated with this Importer.</param> /// <returns>The Importer class.</returns> public static Importer CreateImporter(Document originalDocument, string ifcFileName, IDictionary <string, string> importOptions) { if (originalDocument == null || ifcFileName == null || importOptions == null) { return(null); } Importer importer = new Importer(); TheImporter = importer; TheCache = IFCImportCache.Create(originalDocument, ifcFileName); TheOptions = importer.m_ImportOptions = IFCImportOptions.Create(importOptions); TheLog = IFCImportLog.CreateLog(ifcFileName, "log.html", !TheOptions.DisableLogging); return(importer); }
/// <summary> /// Import an IFC file into a given document for Reference only. /// </summary> /// <param name="document">The host document for the import.</param> /// <param name="origFullFileName">The full file name of the document.</param> /// <param name="options">The list of configurable options for this import.</param> public void ReferenceIFC(Document document, string origFullFileName, IDictionary <String, String> options) { // We need to generate a local file name for all of the intermediate files (the log file, the cache file, and the shared parameters file). string localFileName = ImporterIFCUtils.GetLocalFileName(document, origFullFileName); if (localFileName == null) { throw new InvalidOperationException("Could not generate local file name for: " + origFullFileName); } // An early check, based on the options set - if we are allowed to use an up-to-date existing file on disk, use it. // It is possible that the log file may have been created in CreateImporter above, // if it is used by an external developer. if (TheLog == null) { m_ImportLog = IFCImportLog.CreateLog(localFileName, "log.html", !m_ImportOptions.DisableLogging); } Document originalDocument = document; Document ifcDocument = null; if (TheOptions.Action == IFCImportAction.Link) { string linkedFileName = IFCImportFile.GetRevitFileName(localFileName); ifcDocument = LoadOrCreateLinkDocument(originalDocument, linkedFileName); } else { ifcDocument = originalDocument; } bool useCachedRevitFile = DocumentUpToDate(ifcDocument, localFileName); // In the case where the document is already opened as a link, but it has been updated on disk, // give the user a warning and use the cached value. if (!useCachedRevitFile && ifcDocument.IsLinked) { useCachedRevitFile = true; Importer.AddDelayedLinkError(BuiltInFailures.ImportFailures.IFCCantUpdateLinkedFile); } if (!useCachedRevitFile) { m_ImportCache = IFCImportCache.Create(ifcDocument, localFileName); // Limit creating the cache to Link, but may either remove limiting or make it more restrict (reload only) later. if (TheOptions.Action == IFCImportAction.Link) { TheCache.CreateExistingElementMaps(ifcDocument); } // TheFile will contain the same value as the return value for this function. IFCImportFile.Create(localFileName, m_ImportOptions, ifcDocument); } if (useCachedRevitFile || IFCImportFile.TheFile != null) { IFCImportFile theFile = IFCImportFile.TheFile; if (theFile != null) { if (theFile.IFCProject != null) { IFCObjectDefinition.CreateElement(ifcDocument, theFile.IFCProject); } // Also process any other entities to create. foreach (IFCObjectDefinition objDef in IFCImportFile.TheFile.OtherEntitiesToCreate) { IFCObjectDefinition.CreateElement(ifcDocument, objDef); } theFile.EndImport(ifcDocument, localFileName); } if (TheOptions.Action == IFCImportAction.Link) { // If we have an original Revit link file name, don't create a new RevitLinkType - // we will use the existing one. bool useExistingType = (TheOptions.RevitLinkFileName != null); ElementId revitLinkTypeId = IFCImportFile.LinkInFile(origFullFileName, localFileName, ifcDocument, originalDocument, useExistingType, !useCachedRevitFile); } } if (m_ImportCache != null) { m_ImportCache.Reset(ifcDocument); } }
/// <summary> /// Import an IFC file into a given document for Reference only. /// </summary> /// <param name="document">The host document for the import.</param> /// <param name="fullFileName">The full file name of the document.</param> /// <param name="options">The list of configurable options for this import.</param> public void ReferenceIFC(Document document, string fullFileName, IDictionary <String, String> options) { // An early check, based on the options set - if we are allowed to use an up-to-date existing file on disk, use it. m_ImportLog = IFCImportLog.CreateLog(fullFileName, "log.html"); Document originalDocument = document; Document ifcDocument = null; if (TheOptions.Action == IFCImportAction.Link) { string linkedFileName = IFCImportFile.GetRevitFileName(fullFileName); ifcDocument = LoadOrCreateLinkDocument(originalDocument, linkedFileName); if (ifcDocument == null) { return; } } else { ifcDocument = originalDocument; } bool useCachedRevitFile = DocumentUpToDate(ifcDocument, fullFileName); // In the case where the document is already opened as a link, but it has been updated on disk, // give the user a warning and use the cached value. if (!useCachedRevitFile && ifcDocument.IsLinked) { useCachedRevitFile = true; Importer.AddDelayedLinkError(BuiltInFailures.ImportFailures.IFCCantUpdateLinkedFile); } if (!useCachedRevitFile) { m_ImportCache = IFCImportCache.Create(ifcDocument, fullFileName); // Limit creating the cache to Link, but may either remove limiting or make it more restrict (reload only) later. if (TheOptions.Action == IFCImportAction.Link) { TheCache.CreateExistingElementMaps(ifcDocument); } // TheFile will contain the same value as the return value for this function. IFCImportFile.Create(fullFileName, m_ImportOptions, ifcDocument); } if (useCachedRevitFile || IFCImportFile.TheFile != null) { IFCImportFile theFile = IFCImportFile.TheFile; if (theFile != null) { if (theFile.IFCProject != null) { IFCObjectDefinition.CreateElement(ifcDocument, theFile.IFCProject); } // Also process any other entities to create. foreach (IFCObjectDefinition objDef in IFCImportFile.TheFile.OtherEntitiesToCreate) { IFCObjectDefinition.CreateElement(ifcDocument, objDef); } theFile.EndImport(ifcDocument, fullFileName); } if (TheOptions.Action == IFCImportAction.Link) { // If we have an original Revit link file name, don't create a new RevitLinkType - // we will use the existing one. bool useExistingType = (TheOptions.RevitLinkFileName != null); ElementId revitLinkTypeId = IFCImportFile.LinkInFile(fullFileName, ifcDocument, originalDocument, useExistingType, !useCachedRevitFile); } } if (m_ImportCache != null) { m_ImportCache.Reset(ifcDocument); } }
private bool Process(string ifcFilePath, IFCImportOptions options, Document doc) { // Manually set to false if necessary for debugging. CleanEntitiesAfterCreate = true; TheFileName = ifcFilePath; TheBrepCounter = 0; Log = IFCImportLog.CreateLog(ifcFilePath + ".log.html"); try { IFCSchemaVersion schemaVersion; m_IfcFile = CreateIFCFile(ifcFilePath, out schemaVersion); SchemaVersion = schemaVersion; } catch (Exception ex) { Log.LogError(-1, "There was an error reading the IFC file: " + ex.Message + ". Aborting import.", false); return(false); } Options = options; Document = doc; ShapeLibrary = DirectShapeLibrary.GetDirectShapeLibrary(doc); ShapeLibrary.Reset(); IFCFileReadOptions readOptions = new IFCFileReadOptions(); readOptions.FileName = ifcFilePath; readOptions.XMLConfigFileName = Path.Combine(RevitProgramPath, "EDM\\ifcXMLconfiguration.xml"); int numErrors = 0; int numWarnings = 0; try { Importer.TheCache.StatusBar.Set(String.Format(Resources.IFCReadingFile, TheFileName)); m_IfcFile.Read(readOptions, out numErrors, out numWarnings); } catch (Exception ex) { Log.LogError(-1, "There was an error reading the IFC file: " + ex.Message + ". Aborting import.", false); return(false); } if (numErrors > 0 || numWarnings > 0) { if (numErrors > 0) { if (numWarnings > 0) { Log.LogError(-1, "There were " + numErrors + " errors and " + numWarnings + " reading the IFC file. Please look at the log information at the end of this report for more information.", false); } else { Log.LogError(-1, "There were " + numErrors + " errors reading the IFC file. Please look at the log information at the end of this report for more information.", false); } } else { Log.LogWarning(-1, "There were " + numWarnings + " warnings reading the IFC file. Please look at the log information at the end of this report for more information.", false); } } m_Transaction = new Transaction(doc); switch (options.Intent) { case IFCImportIntent.Reference: InitializeOpenTransaction("Open IFC Reference File"); //If there is more than one project, we will be ignoring all but the first one. IList <IFCAnyHandle> projects = IFCImportFile.TheFile.GetInstances(IFCEntityType.IfcProject, false); if (projects.Count == 0) { Log.LogError(-1, "There were no IfcProjects found in the file. Aborting import.", false); return(false); } else { IFCProject.ProcessIFCProject(projects[0]); } break; } return(true); }