示例#1
0
        /// <summary>
        /// Implementation of the command binding event for the IFC export command.
        /// </summary>
        /// <param name="sender">The event sender (Revit UIApplication).</param>
        /// <param name="args">The arguments (command binding).</param>
        public void OnIFCExport(object sender, CommandEventArgs args)
        {
            try
            {
                // Prepare basic objects
                UIApplication uiApp     = sender as UIApplication;
                UIDocument    uiDoc     = uiApp.ActiveUIDocument;
                Document      activeDoc = uiDoc.Document;

                TheDocument = activeDoc;

                // Note that when exporting multiple documents, we are still going to use the configurations from the
                // active document.
                IFCExportConfigurationsMap configurationsMap = new IFCExportConfigurationsMap();
                configurationsMap.AddOrReplace(IFCExportConfiguration.GetInSession());
                configurationsMap.AddBuiltInConfigurations();
                configurationsMap.AddSavedConfigurations();

                String mruSelection = null;
                if (m_mruConfiguration != null && configurationsMap.HasName(m_mruConfiguration))
                {
                    mruSelection = m_mruConfiguration;
                }

                PotentiallyUpdatedConfigurations = false;
                IFCExport mainWindow = new IFCExport(uiApp, configurationsMap, mruSelection);

                mainWindow.ShowDialog();

                // If user chose to continue
                if (mainWindow.Result == IFCExportResult.ExportAndSaveSettings)
                {
                    int docsToExport = mainWindow.DocumentsToExport.Count;

                    // This shouldn't happen, but just to be safe.
                    if (docsToExport == 0)
                    {
                        return;
                    }

                    bool multipleFiles = docsToExport > 1;

                    // If user chooses to continue


                    // change options
                    IFCExportConfiguration selectedConfig = mainWindow.GetSelectedConfiguration();

                    // Prompt the user for the file location and path
                    string defaultExt = mainWindow.DefaultExt;
                    String fullName   = mainWindow.ExportFilePathName;
                    String path       = Path.GetDirectoryName(fullName);
                    String fileName   = multipleFiles ? Properties.Resources.MultipleFiles : Path.GetFileName(fullName);


                    // This option should be rarely used, and is only for consistency with old files.  As such, it is set by environment variable only.
                    String use2009GUID = Environment.GetEnvironmentVariable("Assign2009GUIDToBuildingStoriesOnIFCExport");
                    bool   use2009BuildingStoreyGUIDs = (use2009GUID != null && use2009GUID == "1");

                    string unsuccesfulExports = string.Empty;

                    // In rare occasions, there may be two projects loaded into Revit with the same name.  This isn't supposed to be allowed, but can happen if,
                    // e.g., a user creates a new project, exports it to IFC, and then calls Open IFC.  In this case, if we export both projects, we will overwrite
                    // one of the exports.  Prevent that by keeping track of the exported file names.
                    ISet <string> exportedFileNames = new HashSet <string>();

                    foreach (Document document in mainWindow.DocumentsToExport)
                    {
                        TheDocument = document;

                        // Call this before the Export IFC transaction starts, as it has its own transaction.
                        IFCClassificationMgr.DeleteObsoleteSchemas(document);

                        Transaction transaction = new Transaction(document, "Export IFC");
                        transaction.Start();

                        FailureHandlingOptions failureOptions = transaction.GetFailureHandlingOptions();
                        failureOptions.SetClearAfterRollback(false);
                        transaction.SetFailureHandlingOptions(failureOptions);

                        // Normally the transaction will be rolled back, but there are cases where we do update the document.
                        // There is no UI option for this, but these two options can be useful for debugging/investigating
                        // issues in specific file export.  The first one supports export of only one element
                        //exportOptions.AddOption("SingleElement", "174245");
                        // The second one supports export only of a list of elements
                        //exportOptions.AddOption("ElementsForExport", "174245;205427");

                        if (multipleFiles)
                        {
                            fileName = IFCUISettings.GenerateFileNameFromDocument(document, exportedFileNames) + "." + defaultExt;
                            fullName = path + "\\" + fileName;
                        }

                        // Prepare the export options
                        IFCExportOptions exportOptions = new IFCExportOptions();

                        ElementId activeViewId = GenerateActiveViewIdFromDocument(document);
                        selectedConfig.ActiveViewId = selectedConfig.UseActiveViewGeometry ? activeViewId : ElementId.InvalidElementId;
                        selectedConfig.UpdateOptions(exportOptions, activeViewId);

                        bool result = document.Export(path, fileName, exportOptions);

                        Dictionary <ElementId, string> linksGUIDsCache = new Dictionary <ElementId, string>();
                        if (result)
                        {
                            // Cache for links guids
                            if (selectedConfig.ExportLinkedFiles == true)
                            {
                                Autodesk.Revit.DB.FilteredElementCollector collector = new FilteredElementCollector(document);
                                collector.WhereElementIsNotElementType().OfCategory(BuiltInCategory.OST_RvtLinks);
                                System.Collections.Generic.ICollection <ElementId> rvtLinkInstanceIds = collector.ToElementIds();
                                foreach (ElementId linkId in rvtLinkInstanceIds)
                                {
                                    Element linkInstance = document.GetElement(linkId);
                                    if (linkInstance == null)
                                    {
                                        continue;
                                    }
                                    Parameter parameter = linkInstance.get_Parameter(BuiltInParameter.IFC_GUID);
                                    if (parameter != null && parameter.HasValue && parameter.StorageType == StorageType.String)
                                    {
                                        String sGUID = parameter.AsString(), sGUIDlower = sGUID.ToLower();
                                        foreach (KeyValuePair <ElementId, string> value in linksGUIDsCache)
                                        {
                                            if (value.Value.ToLower().IndexOf(sGUIDlower) == 0)
                                            {
                                                sGUID += "-";
                                            }
                                        }
                                        linksGUIDsCache.Add(linkInstance.Id, sGUID);
                                    }
                                }
                            }
                        }
                        else
                        {
                            unsuccesfulExports += fullName + "\n";
                        }

                        // Roll back the transaction started earlier, unless certain options are set.
                        if (result && (use2009BuildingStoreyGUIDs || selectedConfig.StoreIFCGUID))
                        {
                            transaction.Commit();
                        }
                        else
                        {
                            transaction.RollBack();
                        }

                        // Export links
                        if (selectedConfig.ExportLinkedFiles == true)
                        {
                            exportOptions.AddOption("ExportingLinks", true.ToString());
                            ExportLinkedDocuments(document, fullName, linksGUIDsCache, exportOptions);
                            exportOptions.AddOption("ExportingLinks", false.ToString());
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(unsuccesfulExports))
                    {
                        using (TaskDialog taskDialog = new TaskDialog(Properties.Resources.IFCExport))
                        {
                            taskDialog.MainInstruction = string.Format(Properties.Resources.IFCExportProcessError, unsuccesfulExports);
                            taskDialog.MainIcon        = TaskDialogIcon.TaskDialogIconWarning;
                            TaskDialogResult taskDialogResult = taskDialog.Show();
                        }
                    }

                    // Remember last successful export location
                    m_mruExportPath = path;
                }

                // The cancel button should cancel the export, not any "OK"ed setup changes.
                if (mainWindow.Result == IFCExportResult.ExportAndSaveSettings || mainWindow.Result == IFCExportResult.Cancel)
                {
                    m_mruConfiguration = mainWindow.GetSelectedConfiguration().Name;
                }
            }
            catch (Exception e)
            {
                using (TaskDialog taskDialog = new TaskDialog(Properties.Resources.IFCExport))
                {
                    taskDialog.MainInstruction = Properties.Resources.IFCExportProcessGenericError;
                    taskDialog.MainIcon        = TaskDialogIcon.TaskDialogIconWarning;
                    taskDialog.ExpandedContent = e.ToString();
                    TaskDialogResult result = taskDialog.Show();
                }
            }
        }
示例#2
0
 /// <summary>
 /// Add a configuration to the map list to show in dialog.
 /// </summary>
 /// <param name="configuration">The configuration to add.</param>
 private void AddToConfigList(IFCExportConfiguration configuration)
 {
     m_configMap.AddOrReplace(configuration);
 }