public void Execute(UIApplication uiapp)
        {
            try
            {
                ICollection <ElementId> elementIds = uiapp.ActiveUIDocument.Selection.GetElementIds();
                string   path     = Path.GetTempPath();
                Document document = uiapp.ActiveUIDocument.Document;
                if (elementIds.Count == 0)
                {
                    mBrowser.mPropertyGrid.SelectedObject = null;
                    return;
                }
                string           ids     = string.Join(";", elementIds.ToList().ConvertAll(x => x.ToString()));
                IFCExportOptions options = new IFCExportOptions();
                options.AddOption("ElementsForExport", ids);
                string      fileName    = Path.GetFileNameWithoutExtension(document.PathName) + ".ifc";
                Transaction transaction = new Transaction(document, "Export IFC");
                transaction.Start();

                document.Export(path, fileName, options);
                transaction.RollBack();
                DatabaseIfc db = new DatabaseIfc(Path.Combine(path, fileName));

                List <IfcElement>    elements   = db.Context.Extract <IfcElement>();
                IEnumerable <object> properties = elements.ConvertAll(x => new ObjectIfcProperties(x));
                mBrowser.mPropertyGrid.SelectedObjects = properties.ToArray();
            }
            finally
            {
            }
            return;
        }
Beispiel #2
0
        public static void ExportToIFC(DesignAutomationData data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            Document doc = data.RevitDoc;

            if (doc == null)
            {
                throw new InvalidOperationException("Could not open document.");
            }

            using (Transaction transaction = new Transaction(doc))
            {
                transaction.Start("Export to IFC");

                IFCExportOptions opt = new IFCExportOptions();

                doc.Export(@".\", "output.ifc", opt);
                transaction.Commit();
            }

            LogTrace("Saving IFC file...");
        }
Beispiel #3
0
        //Rvt导出为Ifc 事件
        private void Uiapp_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e)
        {
            if (count != 0)
            {
                return;
            }
            if (File.Exists(rvtName))
            {
                try
                {
                    Document doc = uiapp.Application.OpenDocumentFile(rvtName);
                    using (Transaction transaction = new Transaction(doc, "ifcexporter"))
                    {
                        transaction.Start();
                        IFCExportOptions opt = null;
                        doc.Export(path, ifcName, opt);
                        transaction.Commit();
                    }
                    doc.Close();
                    return;
                }
                catch (Exception ex)
                {
                    TaskDialog.Show("error", ex.ToString());
                }
                finally
                {
                    #region 发送生成的ifc
                    if (File.Exists(rvtName.Replace(".rvt", ".ifc")))
                    {
                        using (var fs = new FileStream(rvtName.Replace(".rvt", ".ifc"), FileMode.Open))
                        {
                            // 封包体
                            byte[] bodyBytes = new byte[fs.Length];
                            fs.Read(bodyBytes, 0, bodyBytes.Length);
                            fs.Close();
                            // 封包头
                            PkgHeader header = new PkgHeader();
                            header.Id       = ++id;
                            header.BodySize = bodyBytes.Length;
                            byte[] headerBytes = server.StructureToByte <PkgHeader>(header);


                            // 组合最终发送的封包 (封包头+封包体)
                            byte[] sendBytes = GetSendBuffer(headerBytes, bodyBytes);
                            server.Send(ID, sendBytes, sendBytes.Length);
                        }
                        delDir(path);//发送完成,删除文件
                    }
                    else
                    {
                        TaskDialog.Show("", rvtName.Replace(".rvt", ".ifc"));
                    }
                    #endregion
                }
                count++;
            }
        }
        /// <summary>
        /// Updates the IFCExportOptions with the settings in this configuration.
        /// </summary>
        /// <param name="options">The IFCExportOptions to update.</param>
        /// <param name="filterViewId">The id of the view that will be used to select which elements to export.</param>
        public void UpdateOptions(IFCExportOptions options, ElementId filterViewId)
        {
            options.FileVersion            = IFCVersion;
            options.SpaceBoundaryLevel     = SpaceBoundaries;
            options.ExportBaseQuantities   = ExportBaseQuantities;
            options.WallAndColumnSplitting = SplitWallsAndColumns;
            options.FilterViewId           = VisibleElementsOfCurrentView ? filterViewId : ElementId.InvalidElementId;
            options.AddOption("ExportInternalRevitPropertySets", ExportInternalRevitPropertySets.ToString());
            options.AddOption("ExportIFCCommonPropertySets", ExportIFCCommonPropertySets.ToString());
            options.AddOption("ExportAnnotations", Export2DElements.ToString());
            options.AddOption("Use2DRoomBoundaryForVolume", Use2DRoomBoundaryForVolume.ToString());
            options.AddOption("UseFamilyAndTypeNameForReference", UseFamilyAndTypeNameForReference.ToString());
            options.AddOption("ExportVisibleElementsInView", VisibleElementsOfCurrentView.ToString());
            options.AddOption("ExportPartsAsBuildingElements", ExportPartsAsBuildingElements.ToString());
            options.AddOption("UseActiveViewGeometry", UseActiveViewGeometry.ToString());
            options.AddOption("ExportSpecificSchedules", ExportSpecificSchedules.ToString());
            options.AddOption("ExportBoundingBox", ExportBoundingBox.ToString());
            options.AddOption("ExportSolidModelRep", ExportSolidModelRep.ToString());
            options.AddOption("ExportSchedulesAsPsets", ExportSchedulesAsPsets.ToString());
            options.AddOption("ExportUserDefinedPsets", ExportUserDefinedPsets.ToString());
            options.AddOption("ExportUserDefinedParameterMapping", ExportUserDefinedParameterMapping.ToString());
            options.AddOption("ExportLinkedFiles", ExportLinkedFiles.ToString());
            options.AddOption("IncludeSiteElevation", IncludeSiteElevation.ToString());
            options.AddOption("SitePlacement", SitePlacement.ToString());
            options.AddOption("TessellationLevelOfDetail", TessellationLevelOfDetail.ToString());
            options.AddOption("UseOnlyTriangulation", UseOnlyTriangulation.ToString());
            options.AddOption("ActiveViewId", ActiveViewId.ToString());
            options.AddOption("StoreIFCGUID", StoreIFCGUID.ToString());

            // The active phase may not be valid if we are exporting multiple projects. However, if projects share a template that defines the phases,
            // then the ActivePhaseId would likely be valid for all.  There is some small chance that the ActivePhaseId would be a valid, but different, phase
            // in different projects, but that is unlikely enough that it seems worth warning against it but allowing the better functionality in general.
            if (IFCPhaseAttributes.Validate(ActivePhaseId))
            {
                options.AddOption("ActivePhase", ActivePhaseId.ToString());
            }

            options.AddOption("FileType", IFCFileType.ToString());
            string uiVersion = IFCUISettings.GetAssemblyVersion();

            options.AddOption("AlternateUIVersion", uiVersion);

            options.AddOption("ConfigName", Name);   // Add config name into the option for use in the exporter
            options.AddOption("ExportUserDefinedPsetsFileName", ExportUserDefinedPsetsFileName);
            options.AddOption("ExportUserDefinedParameterMappingFileName", ExportUserDefinedParameterMappingFileName);
            options.AddOption("ExportRoomsInView", ExportRoomsInView.ToString());
            options.AddOption("ExcludeFilter", ExcludeFilter.ToString());
            options.AddOption("COBieCompanyInfo", COBieCompanyInfo);
            options.AddOption("COBieProjectInfo", COBieProjectInfo);
            options.AddOption("IncludeSteelElements", IncludeSteelElements.ToString());
            options.AddOption("UseTypeNameOnlyForIfcType", UseTypeNameOnlyForIfcType.ToString());
            options.AddOption("UseVisibleRevitNameAsEntityName", UseVisibleRevitNameAsEntityName.ToString());
        }
Beispiel #5
0
        private void UpdateOptionDeprecated(IFCExportOptions IFCOptions, ElementId activeViewId)
        {
            IFCExportConfigurationDeprecated config = _IFCExportConfiguration as IFCExportConfigurationDeprecated;

            IFCOptions.ExportBaseQuantities = config.ExportBaseQuantities;
            IFCOptions.FileVersion          = config.IFCVersion;
            if (config.CurrentViewOnly)
            {
                IFCOptions.FilterViewId = activeViewId;
            }
            IFCOptions.SpaceBoundaryLevel     = 1;
            IFCOptions.WallAndColumnSplitting = config.SplitWall;
        }
        public void Execute(UIApplication uiapp)
        {
            try
            {
                ICollection <ElementId> elementIds = uiapp.ActiveUIDocument.Selection.GetElementIds();
                string   path     = Path.GetTempPath();
                Document document = uiapp.ActiveUIDocument.Document;
                if (document.IsFamilyDocument || elementIds.Count == 0)
                {
                    mBrowser.mBrowserControl.propertyGrid.SelectedObject = null;
                    return;
                }
                string           ids     = string.Join(";", elementIds.ToList().ConvertAll(x => x.ToString()));
                IFCExportOptions options = new IFCExportOptions();
                options.AddOption("ElementsForExport", ids);
                string      fileName    = Path.GetFileNameWithoutExtension(document.PathName) + ".ifc";
                Transaction transaction = new Transaction(document, "Export IFC");
                transaction.Start();

                document.Export(path, fileName, options);
                transaction.RollBack();
                DatabaseIfc db = new DatabaseIfc(Path.Combine(path, fileName));

                List <IfcElement> elements = db.Context.Extract <IfcElement>();
                if (elements.Count > 0)
                {
                    IEnumerable <object> properties = elements.ConvertAll(x => new ElementIfcProperties(x));
                    IfcElementType       type       = elements[0].RelatingType as IfcElementType;
                    if (type != null)
                    {
                        foreach (IfcElement e in elements)
                        {
                            IfcElementType t = e.RelatingType as IfcElementType;
                            if (t == null || type.Index != t.Index)
                            {
                                type = null;
                                break;
                            }
                        }
                    }
                    mBrowser.mBrowserControl.propertyGrid.SelectedObjects = properties.ToArray();
                    mBrowser.mBrowserControl.ElementType = type;
                }
            }
            finally
            {
            }
            return;
        }
Beispiel #7
0
 public void UpdateOptions(IFCExportOptions IFCOptions, ElementId activeViewId)
 {
     if (_IFCLibrary == IFCLibrary.Standard)
     {
         UpdateOptionStandard(IFCOptions, activeViewId);
     }
     else if (_IFCLibrary == IFCLibrary.Override)
     {
         UpdateOptionOverrided(IFCOptions, activeViewId);
     }
     else if (_IFCLibrary == IFCLibrary.Deprecated)
     {
         UpdateOptionDeprecated(IFCOptions, activeViewId);
     }
 }
Beispiel #8
0
        public void Execute(UIApplication app)
        {
            doc = app.ActiveUIDocument.Document;
            try
            {
                Dictionary <string, byte[]> ifcData = new Dictionary <string, byte[]>();
                foreach (Service service in services)
                {
                    // Skip creation of ifcData generation when this already exists
                    // (checked by ifcexportconfiguration name of the service)
                    //
                    if (ifcData.ContainsKey(service.IfcExportConfiguration))
                    {
                        continue;
                    }

                    // Export to IFC according to all used Ifc export configurations
                    IFCExportConfigurationsMapCustom configurationsMap = new IFCExportConfigurationsMapCustom();
                    foreach (IFCExportConfigurationCustom config in configurationsMap.Values)
                    {
                        if (config.Name.Equals(service.IfcExportConfiguration))
                        {
                            IFCExportOptions IFCOptions = new IFCExportOptions();

                            //Get the current view Id, or -1 if you want to export the entire model
                            config.ActiveViewId = -1;

                            //Update the IFCExportOptions
                            config.UpdateOptions(IFCOptions, null);

                            string filename = IfcUtils.ExportProjectToIFC(doc, IFCOptions);
                            ifcData.Add(config.Name, File.ReadAllBytes(filename));
                            File.Delete(filename);
                        }
                    }
                }

                BackgroundWorker bgw = new BackgroundWorker();
                bgw.DoWork             += new DoWorkEventHandler(bgWorker_DoWork);
                bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_Completed);
                bgw.RunWorkerAsync(new Tuple <Document, Dictionary <string, byte[]> >(doc, ifcData));
            }
            catch (Exception e)
            {
                MessageBox.Show("Failed to create IFC data to send to services.");
            }
        }
Beispiel #9
0
        private bool ExportIFC(Document doc, string folder, string name, ElementId viewid, string settings)
        {
            //Create an instance of IFCExportOptions
            IFCExportOptions IFCOptions = new IFCExportOptions();

            IFCExportConfigurationsMap configurationsMap = new IFCExportConfigurationsMap();

            configurationsMap.AddBuiltInConfigurations();
            configurationsMap.Add(IFCExportConfiguration.GetInSession());
            configurationsMap.AddSavedConfigurations();
            int n = configurationsMap.Values.Count();

            AddSavedConfigurations(configurationsMap, doc);


            for (int j = 0; j < configurationsMap.Values.Count(); j++)
            {
                List <IFCExportConfiguration> val = configurationsMap.Values.ToList();
                if (val[j].Name == settings)
                {
                    val[j].VisibleElementsOfCurrentView = true;
                    try
                    {
                        val[j].UpdateOptions(IFCOptions, viewid);
                    }
                    catch
                    {
                    }
                    val[j].VisibleElementsOfCurrentView = true;
                }
            }
            try
            {
                Transaction k = new Transaction(doc, "export ifc");
                k.Start();
                doc.Export(folder, name, IFCOptions);
                k.Commit();
                return(true);
            }
            catch (Exception e)
            {
                string error = e.Message;
                return(false);
            }
        }
Beispiel #10
0
        private static void ExportProjectToIFC(Document doc, string path)
        {
            IFCExportOptions ifcOptions = new IFCExportOptions();
            {
                ifcOptions.FileVersion            = IFCVersion.IFC2x3;
                ifcOptions.WallAndColumnSplitting = false;
                ifcOptions.SpaceBoundaryLevel     = 1;
                ifcOptions.ExportBaseQuantities   = false;

                ifcOptions.AddOption("ExportInternalRevitPropertySets", "false");
                ifcOptions.AddOption("ExportIFCCommonPropertySets", "true");
                ifcOptions.AddOption("ExportAnnotations", "false");
                ifcOptions.AddOption("Use2DRoomBoundaryForVolume", "false");
                ifcOptions.AddOption("UseFamilyAndTypeNameForReference", "false");
                ifcOptions.AddOption("ExportVisibleElementsInView", "false");
                ifcOptions.AddOption("ExportPartsAsBuildingElements", "false");
                ifcOptions.AddOption("UseActiveViewGeometry", "false");
                ifcOptions.AddOption("ExportSpecificSchedules", "false");
                ifcOptions.AddOption("ExportBoundingBox", "false");
                ifcOptions.AddOption("ExportSolidModelRep", "false");
                ifcOptions.AddOption("ExportSchedulesAsPsets", "false");
                ifcOptions.AddOption("ExportUserDefinedPsets", "false");
                ifcOptions.AddOption("ExportUserDefinedParameterMapping", "false");
                ifcOptions.AddOption("ExportLinkedFiles", "false");
                ifcOptions.AddOption("IncludeSiteElevation", "false");
                ifcOptions.AddOption("TessellationLevelOfDetail", "0.5");
                ifcOptions.AddOption("StoreIFCGUID", "true");
            }

            /*            // get the revit form and set its cursor to busy
             *          System.Windows.Forms.Control form = System.Windows.Forms.Control.FromHandle(Process.GetCurrentProcess().MainWindowHandle);
             *          if (null != form) form.Cursor = Cursors.WaitCursor;
             *
             *          //RevitStatusText.Set("Exporting Revit Project to IFC");
             *          Application.DoEvents();
             */
            Transaction trans = new Transaction(doc, "export model");

            trans.Start();

            // revit doesn't allow the export to run in a different thread
            doc.Export(path, "tmp.ifc", ifcOptions);

            trans.Commit();
        }
Beispiel #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="path"></param>
        /// <param name="useTrans">是否使用事务</param>
        public static void ExportIFC(Document doc, string path, bool useTrans)
        {
            Transaction ts = null;

            if (useTrans)
            {
                ts = new Transaction(doc, "ifcExport" + DateTime.Now.ToString("yyyyMMddHHmmss"));
            }
            try
            {
                if (ts != null)
                {
                    ts.Start();
                }
                var options = new IFCExportOptions
                {
                    FileVersion            = IFCVersion.IFC2x3,
                    SpaceBoundaryLevel     = 2,
                    WallAndColumnSplitting = true,
                    ExportBaseQuantities   = true,
                    FamilyMappingFile      = String.Empty
                };
                var location    = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                var mappingFile = Path.Combine(location, "exportlayers-ifc-IAI-grid.txt");
                if (File.Exists(mappingFile))
                {
                    options.FamilyMappingFile = mappingFile;
                }

                doc.Export(Path.GetDirectoryName(path), Path.GetFileName(path), options);
                if (ts != null)
                {
                    ts.Commit();
                }
            }
            finally
            {
                if (ts != null)
                {
                    ts.Dispose();
                }
            }
        }
Beispiel #12
0
        public void ExportToIFC(Document doc, UI.ModelSelection modelSelection)
        {
            // Prepare the export options
            IFCExportOptions IFCOptions = new IFCExportOptions();

            UI.IFCExportConfigurationCustom selectedConfig = modelSelection.Configuration;

            ElementId activeViewId = GenerateActiveViewIdFromDocument(doc);

            selectedConfig.ActiveViewId = selectedConfig.UseActiveViewGeometry ? activeViewId.IntegerValue : -1;
            selectedConfig.UpdateOptions(IFCOptions, activeViewId);

            string folder = System.IO.Path.GetTempPath();
            string name   = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + System.IO.Path.GetFileNameWithoutExtension(doc.PathName) + ".ifc";

            _path = Path.Combine(folder, name);

            doc.Export(folder, name, IFCOptions);
        }
        /// <summary>
        /// Updates the IFCExportOptions with the settings in this configuration.
        /// </summary>
        /// <param name="options">The IFCExportOptions to update.</param>
        /// <param name="filterViewId">The filter view.</param>
        public void UpdateOptions(IFCExportOptions options, ElementId filterViewId)
        {
            options.FileVersion            = IFCVersion;
            options.SpaceBoundaryLevel     = SpaceBoundaries;
            options.ExportBaseQuantities   = ExportBaseQuantities;
            options.WallAndColumnSplitting = SplitWallsAndColumns;
            if (VisibleElementsOfCurrentView)
            {
                options.FilterViewId = filterViewId;
            }
            else
            {
                options.FilterViewId = ElementId.InvalidElementId;
            }
            options.AddOption("ExportInternalRevitPropertySets", ExportInternalRevitPropertySets.ToString());
            options.AddOption("ExportIFCCommonPropertySets", ExportIFCCommonPropertySets.ToString());
            options.AddOption("ExportAnnotations", Export2DElements.ToString());
            options.AddOption("Use2DRoomBoundaryForVolume", Use2DRoomBoundaryForVolume.ToString());
            options.AddOption("UseFamilyAndTypeNameForReference", UseFamilyAndTypeNameForReference.ToString());
            options.AddOption("ExportVisibleElementsInView", VisibleElementsOfCurrentView.ToString());
            options.AddOption("ExportPartsAsBuildingElements", ExportPartsAsBuildingElements.ToString());
            options.AddOption("ExportBoundingBox", ExportBoundingBox.ToString());
            options.AddOption("ExportSolidModelRep", ExportSolidModelRep.ToString());
            options.AddOption("ExportSchedulesAsPsets", ExportSchedulesAsPsets.ToString());
            options.AddOption("ExportUserDefinedPsets", ExportUserDefinedPsets.ToString());
            options.AddOption("ExportLinkedFiles", ExportLinkedFiles.ToString());
            options.AddOption("IncludeSiteElevation", IncludeSiteElevation.ToString());
            options.AddOption("UseCoarseTessellation", UseCoarseTessellation.ToString());
            options.AddOption("TessellationLevelOfDetail", TessellationLevelOfDetail.ToString());
            options.AddOption("StoreIFCGUID", StoreIFCGUID.ToString());
            options.AddOption("ActivePhase", ActivePhaseId.ToString());

            options.AddOption("FileType", IFCFileType.ToString());
            string uiVersion = IFCUISettings.GetAssemblyVersion();

            options.AddOption("AlternateUIVersion", uiVersion);

            options.AddOption("ConfigName", Name);      // Add config name into the option for use in the exporter
            options.AddOption("ExportUserDefinedPsetsFileName", ExportUserDefinedPsetsFileName);

            options.AddOption("ExportRoomsInView", ExportRoomsInView.ToString());
        }
        /// <summary>
        /// Export current view to IFC for
        /// https://forums.autodesk.com/t5/revit-api-forum/ifc-export-using-document-export-not-working/m-p/8118082
        /// </summary>
        static Result ExportToIfc(Document doc)
        {
            Result r = Result.Failed;

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Export IFC");

                string desktop_path = Environment.GetFolderPath(
                    Environment.SpecialFolder.Desktop);

                IFCExportOptions opt = null;

                doc.Export(desktop_path, doc.Title, opt);

                tx.RollBack();

                r = Result.Succeeded;
            }
            return(r);
        }
Beispiel #15
0
        /// <summary>
        /// Updates the IFCExportOptions with the settings in this configuration.
        /// </summary>
        /// <param name="options">The IFCExportOptions to update.</param>
        /// <param name="filterViewId">The filter view.</param>
        public void UpdateOptions(IFCExportOptions options, ElementId filterViewId)
        {
            options.FileVersion            = IFCVersion;
            options.SpaceBoundaryLevel     = SpaceBoundaries;
            options.ExportBaseQuantities   = ExportBaseQuantities;
            options.WallAndColumnSplitting = SplitWallsAndColumns;
            if (VisibleElementsOfCurrentView)
            {
                options.FilterViewId = filterViewId;
            }
            else
            {
                options.FilterViewId = ElementId.InvalidElementId;
            }
            options.AddOption("ExportInternalRevitPropertySets", ExportInternalRevitPropertySets.ToString());
            options.AddOption("ExportAnnotations", Export2DElements.ToString());
            options.AddOption("Use2DRoomBoundaryForVolume", Use2DRoomBoundaryForVolume.ToString());
            options.AddOption("UseFamilyAndTypeNameForReference", UseFamilyAndTypeNameForReference.ToString());
            options.AddOption("ExportPartsAsBuildingElements", ExportPartsAsBuildingElements.ToString());

            options.AddOption("FileType", IFCFileType.ToString());
        }
        public static string ExportProjectToIFC(Document doc, IFCExportOptions ifcOptions)
        {
            /*            // get the revit form and set its cursor to busy
             * System.Windows.Forms.Control form = System.Windows.Forms.Control.FromHandle(Process.GetCurrentProcess().MainWindowHandle);
             * if (null != form) form.Cursor = Cursors.WaitCursor;
             *
             * //RevitStatusText.Set("Exporting Revit Project to IFC");
             * Application.DoEvents();
             */

            Transaction trans = new Transaction(doc, "export model");

            trans.Start();

            string folder = Path.GetTempPath();
            string name   = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileNameWithoutExtension(doc.PathName) + ".ifc";

            // revit doesn't allow the export to run in a different thread
            doc.Export(folder, name, ifcOptions);

            trans.Commit();
            return(Path.Combine(folder, name));
        }
        /// <summary>
        /// Updates the IFCExportOptions with the settings in this configuration.
        /// </summary>
        /// <param name="options">The IFCExportOptions to update.</param>
        /// <param name="filterViewId">The filter view.</param>
        public void UpdateOptions(IFCExportOptions options, ElementId filterViewId)
        {
            options.FileVersion = IFCVersion;
            options.SpaceBoundaryLevel = SpaceBoundaries;
            options.ExportBaseQuantities = ExportBaseQuantities;
            options.WallAndColumnSplitting = SplitWallsAndColumns;
            if (VisibleElementsOfCurrentView) 
                options.FilterViewId = filterViewId;
            else
                options.FilterViewId = ElementId.InvalidElementId;
            options.AddOption("ExportInternalRevitPropertySets", ExportInternalRevitPropertySets.ToString());
            options.AddOption("ExportIFCCommonPropertySets", ExportIFCCommonPropertySets.ToString());
            options.AddOption("ExportAnnotations", Export2DElements.ToString());
            options.AddOption("Use2DRoomBoundaryForVolume",Use2DRoomBoundaryForVolume.ToString());
            options.AddOption("UseFamilyAndTypeNameForReference",UseFamilyAndTypeNameForReference.ToString());
            options.AddOption("ExportPartsAsBuildingElements", ExportPartsAsBuildingElements.ToString());
            options.AddOption("ExportSurfaceStyles", ExportSurfaceStyles.ToString());
            options.AddOption("ExportAdvancedSweptSolids", ExportAdvancedSweptSolids.ToString());
            options.AddOption("ExportBoundingBox", ExportBoundingBox.ToString());

            options.AddOption("FileType", IFCFileType.ToString());
            string uiVersion = IFCUISettings.GetAssemblyVersion();
            options.AddOption("AlternateUIVersion", uiVersion);

            options.AddOption("ConfigName", Name);      // Add config name into the option for use in the exporter
        }
        /// <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      doc   = uiDoc.Document;

                TheDocument = doc;

                IFCExportConfigurationsMap configurationsMap = new IFCExportConfigurationsMap();
                configurationsMap.Add(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(doc, configurationsMap, mruSelection);
                mainWindow.ShowDialog();

                // If user chose to continue
                if (mainWindow.Result == IFCExportResult.ExportAndSaveSettings)
                {
                    // change options
                    IFCExportConfiguration selectedConfig = mainWindow.GetSelectedConfiguration();

                    // Prepare the export options
                    IFCExportOptions exportOptions = new IFCExportOptions();
                    selectedConfig.UpdateOptions(exportOptions, uiDoc.ActiveView.Id);

                    // prompt for the file name
                    SaveFileDialog fileDialog = new SaveFileDialog();
                    fileDialog.AddExtension = true;

                    String defaultDirectory = m_mruExportPath != null ? m_mruExportPath : null;

                    if (defaultDirectory == null)
                    {
                        String revitFilePath = doc.PathName;
                        if (!String.IsNullOrEmpty(revitFilePath))
                        {
                            defaultDirectory = Path.GetDirectoryName(revitFilePath);
                        }
                    }

                    if (defaultDirectory == null)
                    {
                        defaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    }

                    String defaultFileName = doc.Title;
                    if (String.IsNullOrEmpty(defaultFileName))
                    {
                        defaultFileName = "Project";
                    }
                    else
                    {
                        defaultFileName = Path.GetFileNameWithoutExtension(defaultFileName);
                    }
                    String defaultExtension = mainWindow.GetFileExtension();

                    fileDialog.FileName         = defaultFileName;
                    fileDialog.DefaultExt       = defaultExtension;
                    fileDialog.Filter           = mainWindow.GetFileFilter();
                    fileDialog.InitialDirectory = defaultDirectory;
                    bool?fileDialogResult = fileDialog.ShowDialog();

                    // If user chose to continue
                    if (fileDialogResult.HasValue && fileDialogResult.Value)
                    {
                        // Prompt the user for the file location and path
                        String fullName = fileDialog.FileName;
                        String path     = Path.GetDirectoryName(fullName);
                        String fileName = Path.GetFileName(fullName);

                        // IFC export requires an open transaction, although no changes should be made
                        Transaction transaction = new Transaction(doc, "Export IFC");
                        transaction.Start();
                        FailureHandlingOptions failureOptions = transaction.GetFailureHandlingOptions();
                        failureOptions.SetClearAfterRollback(false);
                        transaction.SetFailureHandlingOptions(failureOptions);

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

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

                        // Roll back the transaction started earlier, unless certain options are set.
                        bool commitTransaction = (use2009BuildingStoreyGUIDs || selectedConfig.StoreIFCGUID);

                        if (commitTransaction)
                        {
                            IFCStoredGUID.Clear();
                        }

                        bool result = doc.Export(path, fileName, exportOptions); // pass in the options here

                        if (!result)
                        {
                            //TODO localization
                            TaskDialog taskDialog = new TaskDialog("Error exporting IFC file");
                            taskDialog.MainInstruction = "The IFC export process encountered an error.";
                            taskDialog.MainIcon        = TaskDialogIcon.TaskDialogIconWarning;
                            taskDialog.Show();
                        }

                        // Roll back the transaction started earlier, unless certain options are set.
                        if (commitTransaction)
                        {
                            foreach (KeyValuePair <ElementId, string> elementIdToGUID in IFCStoredGUID.ElementIdToGUID)
                            {
                                Element element = doc.GetElement(elementIdToGUID.Key);
                                if (element == null)
                                {
                                    continue;
                                }

                                BuiltInParameter builtInParameter = (element is ElementType) ? BuiltInParameter.IFC_TYPE_GUID : BuiltInParameter.IFC_GUID;
                                SetGUIDParameter(element, builtInParameter, elementIdToGUID.Value);
                            }

                            ProjectInfo projectInfo = doc.ProjectInformation;
                            if (projectInfo != null)
                            {
                                foreach (KeyValuePair <BuiltInParameter, string> elementIdToGUID in IFCStoredGUID.ProjectInfoParameterGUID)
                                {
                                    SetGUIDParameter(projectInfo, elementIdToGUID.Key, elementIdToGUID.Value);
                                }
                            }

                            transaction.Commit();
                        }
                        else
                        {
                            transaction.RollBack();
                        }

                        // 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)
                {
                    if (PotentiallyUpdatedConfigurations)
                    {
                        configurationsMap = mainWindow.GetModifiedConfigurations();
                        configurationsMap.UpdateSavedConfigurations();
                    }

                    // Remember last selected configuration
                    m_mruConfiguration = mainWindow.GetSelectedConfiguration().Name;
                }
            }
            catch (Exception e)
            {
                //TODO localization
                TaskDialog taskDialog = new TaskDialog("Error exporting IFC file");
                taskDialog.MainInstruction = "The IFC export process encountered an error.";
                taskDialog.MainIcon        = TaskDialogIcon.TaskDialogIconWarning;
                taskDialog.ExpandedContent = e.ToString();
                taskDialog.Show();
            }
        }
Beispiel #19
0
        /// <summary>
        /// save the given document to the given path (may be the document save path as the extension will be forced to ifc)
        /// </summary>
        /// <param name="path">Path to save the ifc document to</param>
        /// <param name="exportConfiguration">Optional name of the IFC export configuration to use. Ignored if null, empty or unknown</param>
        /// <param name="document">Revit Document to save to IFC</param>
        internal void SaveIfcDocument(string path, string exportConfiguration, Document document)
        {
            if (null == document || string.IsNullOrEmpty(path))
            {
                throw new Exception("Missing parameter(s) in call to SaveIfcDocument");
            }

            IFCExporterConfiguration config = null;

            if (!string.IsNullOrEmpty(exportConfiguration))
            {
                IFCConfigurationManager configs = new IFCConfigurationManager();
                config = configs[exportConfiguration];
            }

            string folder = Path.GetDirectoryName(path);

            if (string.IsNullOrEmpty(folder))
            {
                throw new Exception("Missing directory name in call to SaveIfcDocument");
            }

            // run this in a separate thread so it at least will keep the GUI responsive
            //Thread thread = new Thread(() =>
            //{
            string fname = Path.GetFileNameWithoutExtension(path);

            if (string.IsNullOrEmpty(fname))
            {
                throw new Exception("Missing file name in call to SaveIfcDocument");
            }

            IFCExportOptions ifcOptions = new IFCExportOptions {
                FileVersion = IFCVersion.IFC2x3
            };

            ifcOptions.WallAndColumnSplitting = false;
            ifcOptions.SpaceBoundaryLevel     = 1;

            if (null != config)
            {
                config.UpdateOptions(ElementId.InvalidElementId, document, ref ifcOptions);
            }

            // get the revit form and set its cursor to busy
            System.Windows.Forms.Control form = System.Windows.Forms.Control.FromHandle(Process.GetCurrentProcess().MainWindowHandle);
            if (null != form)
            {
                form.Cursor = Cursors.WaitCursor;
            }
            RevitStatusText.Set("Exporting Revit Project to IFC");
            Cmd.ShowResult("Exporting Revit Project to IFC");
            Application.DoEvents();

            // revit doesn't allow the export to run in a different thread
            document.Export(folder, fname + ".ifc", ifcOptions);

            //});

            //thread.IsBackground = true;
            //thread.Priority = ThreadPriority.Highest;
            //thread.Start();

            //// get the revit form and set its cursor to busy
            //System.Windows.Forms.Control form = System.Windows.Forms.Control.FromHandle(Process.GetCurrentProcess().MainWindowHandle);
            //if (null != form) form.Cursor = Cursors.WaitCursor;
            //RevitStatusText.Set("Exporting Revit Project to IFC");

            //while (thread.IsAlive)
            //{
            //	// check for progress and display it
            //	Application.DoEvents();
            //}

            if (null != form)
            {
                form.Cursor = Cursors.Default;
            }
            //if (null != ctSource && ctSource.IsCancellationRequested)
            if (!File.Exists(Path.Combine(folder, fname + ".ifc")))
            {
                RevitStatusText.Set("Export to IFC cancelled");
            }
            else
            {
                RevitStatusText.Set("Export to IFC completed");
            }
        }
Beispiel #20
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      doc   = uiDoc.Document;

                IFCExportConfigurationsMap configurationsMap = new IFCExportConfigurationsMap();
                configurationsMap.Add(IFCExportConfiguration.GetInSession());
                configurationsMap.AddBuiltInConfigurations();
                configurationsMap.AddSavedConfigurations(doc);

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

                IFCExport mainWindow = new IFCExport(configurationsMap, mruSelection);
                mainWindow.ShowDialog();

                // If user chose to continue
                if (mainWindow.Result == IFCExportResult.ExportAndSaveSettings)
                {
                    // change options
                    IFCExportConfiguration selectedConfig = mainWindow.GetSelectedConfiguration();

                    // Prepare the export options
                    IFCExportOptions exportOptions = new IFCExportOptions();
                    selectedConfig.UpdateOptions(exportOptions, uiDoc.ActiveView.Id);

                    // prompt for the file name
                    SaveFileDialog fileDialog = new SaveFileDialog();
                    fileDialog.AddExtension = true;

                    String defaultDirectory = m_mruExportPath != null ? m_mruExportPath : null;

                    if (defaultDirectory == null)
                    {
                        String revitFilePath = doc.PathName;
                        if (!String.IsNullOrEmpty(revitFilePath))
                        {
                            defaultDirectory = Path.GetDirectoryName(revitFilePath);
                        }
                    }

                    if (defaultDirectory == null)
                    {
                        defaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    }

                    String defaultFileName = doc.Title;
                    if (String.IsNullOrEmpty(defaultFileName))
                    {
                        defaultFileName = "Project";
                    }
                    else
                    {
                        defaultFileName = Path.GetFileNameWithoutExtension(defaultFileName);
                    }
                    String defaultExtension = mainWindow.GetFileExtension();

                    fileDialog.FileName         = defaultFileName;
                    fileDialog.DefaultExt       = defaultExtension;
                    fileDialog.Filter           = mainWindow.GetFileFilter();
                    fileDialog.InitialDirectory = defaultDirectory;
                    bool?fileDialogResult = fileDialog.ShowDialog();

                    // If user chose to continue
                    if (fileDialogResult.HasValue && fileDialogResult.Value)
                    {
                        // Prompt the user for the file location and path
                        String fullName = fileDialog.FileName;
                        String path     = Path.GetDirectoryName(fullName);
                        String fileName = Path.GetFileName(fullName);

                        // IFC export requires an open transaction, although no changes should be made
                        Transaction transaction = new Transaction(doc, "Export IFC");
                        transaction.Start();
                        FailureHandlingOptions failureOptions = transaction.GetFailureHandlingOptions();
                        failureOptions.SetClearAfterRollback(false);
                        transaction.SetFailureHandlingOptions(failureOptions);

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

                        bool result = doc.Export(path, fileName, exportOptions); // pass in the options here

                        if (!result)
                        {
                            //TODO localization
                            TaskDialog taskDialog = new TaskDialog("Error exporting IFC file");
                            taskDialog.MainInstruction = "The IFC export process encountered an error.";
                            taskDialog.MainIcon        = TaskDialogIcon.TaskDialogIconWarning;
                            taskDialog.Show();
                        }

                        // Always roll back the transaction started earlier
                        transaction.RollBack();

                        // Remember last successful export location
                        m_mruExportPath = path;
                    }
                }
                if (mainWindow.Result == IFCExportResult.SaveSettings || mainWindow.Result == IFCExportResult.ExportAndSaveSettings)
                {
                    configurationsMap = mainWindow.GetModifiedConfigurations();
                    configurationsMap.UpdateSavedConfigurations(doc);

                    // Remember last selected configuration
                    m_mruConfiguration = mainWindow.GetSelectedConfiguration().Name;
                }
            }
            catch (Exception e)
            {
                //TODO localization
                TaskDialog taskDialog = new TaskDialog("Error exporting IFC file");
                taskDialog.MainInstruction = "The IFC export process encountered an error.";
                taskDialog.MainIcon        = TaskDialogIcon.TaskDialogIconWarning;
                taskDialog.ExpandedContent = e.ToString();
                taskDialog.Show();
            }
        }
Beispiel #21
0
        public void ExportLinkedDocuments(Autodesk.Revit.DB.Document document, string fileName, Dictionary <ElementId, string> linksGUIDsCache, IFCExportOptions exportOptions)
        {
            // get the extension
            int index = fileName.LastIndexOf('.');

            if (index <= 0)
            {
                return;
            }
            string sExtension = fileName.Substring(index);

            fileName = fileName.Substring(0, index);

            // get all the revit link instances
            FilteredElementCollector collector        = new FilteredElementCollector(document);
            ElementFilter            elementFilter    = new ElementClassFilter(typeof(RevitLinkInstance));
            List <RevitLinkInstance> rvtLinkInstances = collector.WherePasses(elementFilter).Cast <RevitLinkInstance>().ToList();

            IDictionary <String, int> rvtLinkNamesDict = new Dictionary <String, int>();
            IDictionary <String, List <RevitLinkInstance> > rvtLinkNamesToInstancesDict = new Dictionary <String, List <RevitLinkInstance> >();

            try
            {
                // get the link types
                foreach (RevitLinkInstance rvtLinkInstance in rvtLinkInstances)
                {
                    // get the instance
                    if (rvtLinkInstance == null)
                    {
                        continue;
                    }

                    // check the cache
                    if (linksGUIDsCache.Keys.Contains(rvtLinkInstance.Id) == false)
                    {
                        continue;
                    }

                    // get the link document
                    Document linkDocument = rvtLinkInstance.GetLinkDocument();
                    if (linkDocument == null)
                    {
                        continue;
                    }

                    // get the link file path and name
                    String    linkPathName          = "";
                    Parameter originalFileNameParam = linkDocument.ProjectInformation.get_Parameter("Original IFC File Name");
                    if (originalFileNameParam != null && originalFileNameParam.StorageType == StorageType.String)
                    {
                        linkPathName = originalFileNameParam.AsString();
                    }
                    else
                    {
                        linkPathName = linkDocument.PathName;
                    }

                    // get the link file name
                    String linkFileName = "";
                    index = linkPathName.LastIndexOf("\\");
                    if (index > 0)
                    {
                        linkFileName = linkPathName.Substring(index + 1);
                    }
                    else
                    {
                        linkFileName = linkDocument.Title;
                    }

                    // remove the extension
                    index = linkFileName.LastIndexOf('.');
                    if (index > 0)
                    {
                        linkFileName = linkFileName.Substring(0, index);
                    }

                    // add to names count dictionary
                    if (!rvtLinkNamesDict.Keys.Contains(linkFileName))
                    {
                        rvtLinkNamesDict.Add(linkFileName, 0);
                    }
                    rvtLinkNamesDict[linkFileName]++;

                    // add to names instances dictionary
                    if (!rvtLinkNamesToInstancesDict.Keys.Contains(linkPathName))
                    {
                        rvtLinkNamesToInstancesDict.Add(linkPathName, new List <RevitLinkInstance>());
                    }
                    rvtLinkNamesToInstancesDict[linkPathName].Add(rvtLinkInstance);
                }
            }
            catch
            {
            }

            // get the link instances
            // We will keep track of the instances we can't export.
            // Reasons we can't export:
            // 1. The path for the linked instance doesn't exist.
            // 2. Couldn't create a temporary document for exporting the linked instance.
            // 3. The document for the linked instance can't be found.
            // 4. The linked instance is mirrored, non-conformal, or scaled.
            IList <string>    pathDoesntExist   = new List <string>();
            IList <string>    noTempDoc         = new List <string>();
            IList <ElementId> cantFindDoc       = new List <ElementId>();
            IList <ElementId> nonConformalInst  = new List <ElementId>();
            IList <ElementId> scaledInst        = new List <ElementId>();
            IList <ElementId> instHasReflection = new List <ElementId>();

            foreach (String linkPathName in rvtLinkNamesToInstancesDict.Keys)
            {
                // get the name of the copy
                String linkPathNameCopy = System.IO.Path.GetTempPath();
                index = linkPathName.LastIndexOf("\\");
                if (index > 0)
                {
                    linkPathNameCopy += linkPathName.Substring(index + 1);
                }
                else
                {
                    linkPathNameCopy += linkPathName;
                }
                index = linkPathNameCopy.LastIndexOf('.');
                if (index <= 0)
                {
                    index = linkPathNameCopy.Length;
                }
                linkPathNameCopy = linkPathNameCopy.Insert(index, " - Copy");
                int i = 1;
                while (File.Exists(linkPathNameCopy))
                {
                    linkPathNameCopy = linkPathNameCopy.Insert(index, "(" + (++i).ToString() + ")");
                }

                // copy the file
                File.Copy(linkPathName, linkPathNameCopy);
                if (!File.Exists(linkPathNameCopy))
                {
                    pathDoesntExist.Add(linkPathName);
                    continue;
                }

                // open the document
                Document documentCopy = null;
                try
                {
                    if ((linkPathName.Length >= 4 && linkPathName.Substring(linkPathName.Length - 4).ToLower() == ".ifc") ||
                        (linkPathName.Length >= 7 && linkPathName.Substring(linkPathName.Length - 7).ToLower() == ".ifcxml") ||
                        (linkPathName.Length >= 7 && linkPathName.Substring(linkPathName.Length - 7).ToLower() == ".ifczip"))
                    {
                        documentCopy = document.Application.OpenIFCDocument(linkPathNameCopy);
                    }
                    else
                    {
                        documentCopy = document.Application.OpenDocumentFile(linkPathNameCopy);
                    }
                }
                catch
                {
                    documentCopy = null;
                }

                if (documentCopy == null)
                {
                    noTempDoc.Add(linkPathName);
                    continue;
                }

                // get the link document unit scale
                DisplayUnitType dutLink = documentCopy.GetUnits().GetFormatOptions(UnitType.UT_Length).DisplayUnits;
                double          lengthScaleFactorLink = UnitUtils.ConvertFromInternalUnits(1.0, dutLink);

                // get the link instances
                List <RevitLinkInstance> currRvtLinkInstances = rvtLinkNamesToInstancesDict[linkPathName];
                IList <string>           serTransforms        = new List <string>();
                IList <string>           linkFileNames        = new List <string>();

                foreach (RevitLinkInstance currRvtLinkInstance in currRvtLinkInstances)
                {
                    // Nothing to report if the element itself is null.
                    if (currRvtLinkInstance == null)
                    {
                        continue;
                    }

                    // get the link document
                    Document linkDocument = currRvtLinkInstance.GetLinkDocument();
                    if (linkDocument == null)
                    {
                        cantFindDoc.Add(currRvtLinkInstance.Id);
                        continue;
                    }

                    // get the link transform
                    Transform tr = currRvtLinkInstance.GetTransform();

                    // We can't handle non-conformal, scaled, or mirrored transforms.
                    if (!tr.IsConformal)
                    {
                        nonConformalInst.Add(currRvtLinkInstance.Id);
                        continue;
                    }

                    if (tr.HasReflection)
                    {
                        instHasReflection.Add(currRvtLinkInstance.Id);
                        continue;
                    }

                    if (!MathUtil.IsAlmostEqual(tr.Determinant, 1.0))
                    {
                        scaledInst.Add(currRvtLinkInstance.Id);
                        continue;
                    }

                    // get the link file path and name
                    String linkFileName = "";
                    index = linkPathName.LastIndexOf("\\");
                    if (index > 0)
                    {
                        linkFileName = linkPathName.Substring(index + 1);
                    }
                    else
                    {
                        linkFileName = linkDocument.Title;
                    }

                    // remove the extension
                    index = linkFileName.LastIndexOf('.');
                    if (index > 0)
                    {
                        linkFileName = linkFileName.Substring(0, index);
                    }

                    //if link was an IFC file then make a different formating to the file name
                    if ((linkPathName.Length >= 4 && linkPathName.Substring(linkPathName.Length - 4).ToLower() == ".ifc") ||
                        (linkPathName.Length >= 7 && linkPathName.Substring(linkPathName.Length - 7).ToLower() == ".ifcxml") ||
                        (linkPathName.Length >= 7 && linkPathName.Substring(linkPathName.Length - 7).ToLower() == ".ifczip"))
                    {
                        String fName = fileName;

                        //get output path and add to the new file name
                        index = fName.LastIndexOf("\\");
                        if (index > 0)
                        {
                            fName = fName.Substring(0, index + 1);
                        }
                        else
                        {
                            fName = "";
                        }

                        //construct IFC file name
                        linkFileName = fName + linkFileName + "-";

                        //add guid
                        linkFileName += linksGUIDsCache[currRvtLinkInstance.Id];
                    }
                    else
                    {
                        // check if there are multiple instances with the same name
                        bool bMultiple = (rvtLinkNamesDict[linkFileName] > 1);

                        // add the path
                        linkFileName = fileName + "-" + linkFileName;

                        // add the guid
                        if (bMultiple)
                        {
                            linkFileName += "-";
                            linkFileName += linksGUIDsCache[currRvtLinkInstance.Id];
                        }
                    }

                    // add the extension
                    linkFileName += sExtension;

                    linkFileNames.Add(linkFileName);

                    // scale the transform origin
                    tr.Origin *= lengthScaleFactorLink;

                    // serialize transform
                    serTransforms.Add(SerializeTransform(tr));
                }

                // IFC export requires an open transaction, although no changes should be made
                Transaction transaction = new Transaction(documentCopy, "Export IFC Link");
                transaction.Start();
                FailureHandlingOptions failureOptions = transaction.GetFailureHandlingOptions();
                failureOptions.SetClearAfterRollback(false);
                transaction.SetFailureHandlingOptions(failureOptions);

                // export
                try
                {
                    int numLinkInstancesToExport = linkFileNames.Count;
                    exportOptions.AddOption("NumberOfExportedLinkInstances", numLinkInstancesToExport.ToString());

                    for (int ii = 0; ii < numLinkInstancesToExport; ii++)
                    {
                        string optionName = (ii == 0) ? "ExportLinkInstanceTransform" : "ExportLinkInstanceTransform" + (ii + 1).ToString();
                        exportOptions.AddOption(optionName, serTransforms[ii]);

                        // Don't pass in file name for the first link instance.
                        if (ii == 0)
                        {
                            continue;
                        }

                        optionName = "ExportLinkInstanceFileName" + (ii + 1).ToString();
                        exportOptions.AddOption(optionName, linkFileNames[ii]);
                    }

                    // Pass in the first value; the rest will  be in the options.
                    String path_     = Path.GetDirectoryName(linkFileNames[0]);
                    String fileName_ = Path.GetFileName(linkFileNames[0]);
                    bool   result    = documentCopy.Export(path_, fileName_, exportOptions); // pass in the options here
                }
                catch
                {
                }

                // rollback the transaction
                transaction.RollBack();

                // close the document
                documentCopy.Close(false);

                // delete the copy
                try
                {
                    File.Delete(linkPathNameCopy);
                }
                catch
                {
                }

                // Show user errors, if any.
                int numBadInstances = pathDoesntExist.Count + noTempDoc.Count + cantFindDoc.Count + nonConformalInst.Count
                                      + scaledInst.Count + instHasReflection.Count;
                if (numBadInstances > 0)
                {
                    using (TaskDialog taskDialog = new TaskDialog(Properties.Resources.IFCExport))
                    {
                        taskDialog.MainInstruction = string.Format(Properties.Resources.LinkInstanceExportErrorMain, numBadInstances);
                        taskDialog.MainIcon        = TaskDialogIcon.TaskDialogIconWarning;
                        taskDialog.TitleAutoPrefix = false;

                        string expandedContent = "";
                        AddExpandedStringContent(ref expandedContent, Properties.Resources.LinkInstanceExportErrorPath, pathDoesntExist);
                        AddExpandedStringContent(ref expandedContent, Properties.Resources.LinkInstanceExportCantCreateDoc, noTempDoc);
                        AddExpandedElementIdContent(ref expandedContent, Properties.Resources.LinkInstanceExportCantFindDoc, cantFindDoc);
                        AddExpandedElementIdContent(ref expandedContent, Properties.Resources.LinkInstanceExportNonConformal, nonConformalInst);
                        AddExpandedElementIdContent(ref expandedContent, Properties.Resources.LinkInstanceExportScaled, scaledInst);
                        AddExpandedElementIdContent(ref expandedContent, Properties.Resources.LinkInstanceExportHasReflection, instHasReflection);

                        taskDialog.ExpandedContent = expandedContent;
                        TaskDialogResult result = taskDialog.Show();
                    }
                }
            }
        }
        /// <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 doc = uiDoc.Document;

                TheDocument = doc;

                IFCExportConfigurationsMap configurationsMap = new IFCExportConfigurationsMap();
                configurationsMap.Add(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(doc, configurationsMap, mruSelection);
                mainWindow.ShowDialog();

                // If user chose to continue
                if (mainWindow.Result == IFCExportResult.ExportAndSaveSettings)
                {
                    // change options
                    IFCExportConfiguration selectedConfig = mainWindow.GetSelectedConfiguration();

                    // Prepare the export options
                    IFCExportOptions exportOptions = new IFCExportOptions();
                    selectedConfig.UpdateOptions(exportOptions, uiDoc.ActiveView.Id);  

                    // prompt for the file name
                    SaveFileDialog fileDialog = new SaveFileDialog();
                    fileDialog.AddExtension = true;

                    String defaultDirectory = m_mruExportPath != null ? m_mruExportPath : null;
                    
                    if (defaultDirectory == null)
                    {
                        String revitFilePath = doc.PathName;
                        if (!String.IsNullOrEmpty(revitFilePath))
                        {
                            defaultDirectory = Path.GetDirectoryName(revitFilePath);
                        }
                    }

                    if (defaultDirectory == null)
                    {
                        defaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    }

                    String defaultFileName = doc.Title;
                    if (String.IsNullOrEmpty(defaultFileName))
                    {
                        defaultFileName = "Project";
                    }
                    else
                    {
                        defaultFileName = Path.GetFileNameWithoutExtension(defaultFileName);
                    }
                    String defaultExtension = mainWindow.GetFileExtension();

                    fileDialog.FileName = defaultFileName;
                    fileDialog.DefaultExt = defaultExtension;
                    fileDialog.Filter = mainWindow.GetFileFilter();
                    fileDialog.InitialDirectory = defaultDirectory;
                    bool? fileDialogResult = fileDialog.ShowDialog();
                
                    // If user chose to continue
                    if (fileDialogResult.HasValue && fileDialogResult.Value)
                    {
                        // Prompt the user for the file location and path
                        String fullName = fileDialog.FileName;
                        String path = Path.GetDirectoryName(fullName);
                        String fileName = Path.GetFileName(fullName);

                        // IFC export requires an open transaction, although no changes should be made
                        Transaction transaction = new Transaction(doc, "Export IFC");
                        transaction.Start();
                        FailureHandlingOptions failureOptions = transaction.GetFailureHandlingOptions();
                        failureOptions.SetClearAfterRollback(false);
                        transaction.SetFailureHandlingOptions(failureOptions);

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

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

                        // Roll back the transaction started earlier, unless certain options are set.
                        bool commitTransaction = (use2009BuildingStoreyGUIDs || selectedConfig.StoreIFCGUID);
                        
                        if (commitTransaction)
                            IFCStoredGUID.Clear();

                        bool result = doc.Export(path, fileName, exportOptions); // pass in the options here

                        if (!result)
                        {
                            //TODO localization
                            TaskDialog taskDialog = new TaskDialog("Error exporting IFC file");
                            taskDialog.MainInstruction = "The IFC export process encountered an error.";
                            taskDialog.MainIcon = TaskDialogIcon.TaskDialogIconWarning;
                            taskDialog.Show();
                        }

                        // Roll back the transaction started earlier, unless certain options are set.
                        if (commitTransaction)
                        {
                            foreach (KeyValuePair<ElementId, string> elementIdToGUID in IFCStoredGUID.ElementIdToGUID)
                            {
                                Element element = doc.GetElement(elementIdToGUID.Key);
                                if (element == null)
                                    continue;

                                BuiltInParameter builtInParameter = (element is ElementType) ? BuiltInParameter.IFC_TYPE_GUID : BuiltInParameter.IFC_GUID;
                                SetGUIDParameter(element, builtInParameter, elementIdToGUID.Value);
                            }

                            ProjectInfo projectInfo = doc.ProjectInformation;
                            if (projectInfo != null)
                            {
                                foreach (KeyValuePair<BuiltInParameter, string> elementIdToGUID in IFCStoredGUID.ProjectInfoParameterGUID)
                                {
                                    SetGUIDParameter(projectInfo, elementIdToGUID.Key, elementIdToGUID.Value);
                                }
                            }

                            transaction.Commit();
                        }
                        else
                            transaction.RollBack();
                        
                        // 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)
                {
                    if (PotentiallyUpdatedConfigurations)
                    {
                        configurationsMap = mainWindow.GetModifiedConfigurations();
                        configurationsMap.UpdateSavedConfigurations();
                    }

                    // Remember last selected configuration
                    m_mruConfiguration = mainWindow.GetSelectedConfiguration().Name;
                }
            }
            catch (Exception e)
            {
                //TODO localization
                TaskDialog taskDialog = new TaskDialog("Error exporting IFC file");
                taskDialog.MainInstruction = "The IFC export process encountered an error.";
                taskDialog.MainIcon = TaskDialogIcon.TaskDialogIconWarning;
                taskDialog.ExpandedContent = e.ToString();
                taskDialog.Show();
            }
        }
        /// <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.Add(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.IntegerValue : -1;
                        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)
                {
                    if (PotentiallyUpdatedConfigurations)
                    {
                        configurationsMap = mainWindow.GetModifiedConfigurations();
                        configurationsMap.UpdateSavedConfigurations();
                    }

                    // Remember last selected configuration
                    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();
                }
            }
        }
        /// <summary>
        /// Updates the IFCExportOptions with the settings in this configuration.
        /// </summary>
        /// <param name="options">The IFCExportOptions to update.</param>
        /// <param name="filterViewId">The filter view.</param>
        public void UpdateOptions(IFCExportOptions options, ElementId filterViewId)
        {
            options.FileVersion = IFCVersion;
            options.SpaceBoundaryLevel = SpaceBoundaries;
            options.ExportBaseQuantities = ExportBaseQuantities;
            options.WallAndColumnSplitting = SplitWallsAndColumns;
            if (VisibleElementsOfCurrentView) 
                options.FilterViewId = filterViewId;
            else
                options.FilterViewId = ElementId.InvalidElementId;
            options.AddOption("ExportInternalRevitPropertySets", ExportInternalRevitPropertySets.ToString());
            options.AddOption("ExportAnnotations", Export2DElements.ToString());
            options.AddOption("Use2DRoomBoundaryForVolume",Use2DRoomBoundaryForVolume.ToString());
            options.AddOption("UseFamilyAndTypeNameForReference",UseFamilyAndTypeNameForReference.ToString());
            options.AddOption("ExportPartsAsBuildingElements", ExportPartsAsBuildingElements.ToString());

            options.AddOption("FileType", IFCFileType.ToString());
        }
        /// <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 doc = uiDoc.Document;

                TheDocument = doc;

                IFCExportConfigurationsMap configurationsMap = new IFCExportConfigurationsMap();
                configurationsMap.Add(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(doc, configurationsMap, mruSelection);
                mainWindow.ShowDialog();

                // If user chose to continue
                if (mainWindow.Result == IFCExportResult.ExportAndSaveSettings)
                {
                    // change options
                    IFCExportConfiguration selectedConfig = mainWindow.GetSelectedConfiguration();

                    // Prepare the export options
                    IFCExportOptions exportOptions = new IFCExportOptions();
                    selectedConfig.UpdateOptions(exportOptions, uiDoc.ActiveView.Id);  

                    // prompt for the file name
                    SaveFileDialog fileDialog = new SaveFileDialog();
                    fileDialog.AddExtension = true;

                    String defaultDirectory = m_mruExportPath != null ? m_mruExportPath : null;
                    
                    if (defaultDirectory == null)
                    {
                        String revitFilePath = doc.PathName;
                        if (!String.IsNullOrEmpty(revitFilePath))
                        {
                            defaultDirectory = Path.GetDirectoryName(revitFilePath);
                        }
                    }

                    if (defaultDirectory == null)
                    {
                        defaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    }

                    String defaultFileName = doc.Title;
                    if (String.IsNullOrEmpty(defaultFileName))
                    {
                        defaultFileName = "Project";
                    }
                    else
                    {
                        defaultFileName = Path.GetFileNameWithoutExtension(defaultFileName);
                    }
                    String defaultExtension = mainWindow.GetFileExtension();

                    fileDialog.FileName = defaultFileName;
                    fileDialog.DefaultExt = defaultExtension;
                    fileDialog.Filter = mainWindow.GetFileFilter();
                    fileDialog.InitialDirectory = defaultDirectory;
                    bool? fileDialogResult = fileDialog.ShowDialog();
                
                    // If user chose to continue
                    if (fileDialogResult.HasValue && fileDialogResult.Value)
                    {
                        // Prompt the user for the file location and path
                        String fullName = fileDialog.FileName;
                        String path = Path.GetDirectoryName(fullName);
                        String fileName = Path.GetFileName(fullName);

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

                        // IFC export requires an open transaction, although no changes should be made
                        Transaction transaction = new Transaction(doc, "Export IFC");
                        transaction.Start();
                        FailureHandlingOptions failureOptions = transaction.GetFailureHandlingOptions();
                        failureOptions.SetClearAfterRollback(false);
                        transaction.SetFailureHandlingOptions(failureOptions);

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

                        bool result = doc.Export(path, fileName, exportOptions); // pass in the options here

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

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

                        // Cache for links guids
                        Dictionary<ElementId, string> linksGUIDsCache = new Dictionary<ElementId, string>();
                        if (selectedConfig.ExportLinkedFiles == true)
                        {
                            Autodesk.Revit.DB.FilteredElementCollector collector = new FilteredElementCollector(doc);
                            collector.WhereElementIsNotElementType().OfCategory(BuiltInCategory.OST_RvtLinks);
                            System.Collections.Generic.ICollection<ElementId> rvtLinkInstanceIds = collector.ToElementIds();
                            foreach (ElementId linkId in rvtLinkInstanceIds)
                            {
                                Element linkInstance = doc.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);
                                }
                            }
                        }

                        // Roll back the transaction started earlier, unless certain options are set.
                        if (use2009BuildingStoreyGUIDs || selectedConfig.StoreIFCGUID)
                            transaction.Commit();
                        else
                            transaction.RollBack();
                        
                        // Export links
                        if (selectedConfig.ExportLinkedFiles == true)
                        {
                            exportOptions.AddOption("ExportingLinks", true.ToString());
                            ExportLinkedDocuments(doc, fullName, linksGUIDsCache, exportOptions);
                            exportOptions.AddOption("ExportingLinks", false.ToString());
                        }
                        
                        // 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)
                {
                    if (PotentiallyUpdatedConfigurations)
                    {
                        configurationsMap = mainWindow.GetModifiedConfigurations();
                        configurationsMap.UpdateSavedConfigurations();
                    }

                    // Remember last selected configuration
                    m_mruConfiguration = mainWindow.GetSelectedConfiguration().Name;
                }
            }
            catch (Exception e)
            {
                using (TaskDialog taskDialog = new TaskDialog(Properties.Resources.IFCExport))
                {
                    taskDialog.MainInstruction = Properties.Resources.IFCExportProcessError;
                    taskDialog.MainIcon = TaskDialogIcon.TaskDialogIconWarning;
                    taskDialog.ExpandedContent = e.ToString();
                    TaskDialogResult result = taskDialog.Show();
                }
            }
        }
Beispiel #26
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            var document = commandData?.Application?.ActiveUIDocument?.Document;

            if (document == null || document.IsFamilyDocument)
            {
                TaskDialog.Show("Information", "This tool only works for models, not for the family editor.");
                return(Result.Cancelled);
            }

            var path = document.PathName;

            if (string.IsNullOrEmpty(path) || !File.Exists(path))
            {
                TaskDialog.Show("Information", "File must be saved first.");
                return(Result.Cancelled);
            }
            Init.SetLogger(path);

            //export to IFC4 DTV
            var ifcPath     = Path.ChangeExtension(document.PathName, ".ifc");
            var ifcFileName = Path.GetFileName(ifcPath);
            var ifcDir      = Path.GetDirectoryName(ifcPath);
            var ifcOptions  = new IFCExportOptions();
            var options     = GetConfiguration();

            options.UpdateOptions(ifcOptions, null);
            using (var txn = new Transaction(document))
            {
                txn.Start("Exporting to IFC");
                Log.Information($"Exporting IFC: {ifcPath}");
                document.Export(ifcDir, ifcFileName, ifcOptions);
                Log.Information($"Exported IFC: {ifcPath}");

                // keep it clean
                txn.RollBack();
            }

            Log.Information("Transforming the file (changing schema, removing unnecessary comments)");
            FileTransformations.MakeIfcRail(ifcPath);

            using (var model = ModelHelper.GetModel(ifcPath))
            {
                var w = Stopwatch.StartNew();
                using (var txn = model.BeginTransaction("Model enhancements"))
                {
                    Log.Information("Transforming the model: Changing entity types to IFC Rail entities");
                    TypeChanger.ChangeTypes(model);
                    w.Stop();
                    Log.Information($"Types changed in {w.ElapsedMilliseconds}ms");


                    Log.Information("Enriching the model: Exporting IfcAlignments from imported DWGs");
                    var alignment = new AlignmentExporter(document, model);
                    alignment.Export();

                    // enhance the model
                    txn.Commit();
                }


                Log.Information("Saving transformed and enhanced IFC.");
                using (var stream = File.Create(ifcPath))
                {
                    //// after a lot of transformations is it a good idea to purge
                    //using (var clean = ModelHelper.GetCleanModel(model))
                    //{
                    //    clean.SaveAsStep21(stream);
                    //}
                    model.SaveAsStep21(stream);
                }
            }

            TaskDialog.Show("Finished", "Export and data transformation finished");
            return(Result.Succeeded);
        }
        /// <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 doc = uiDoc.Document;

                IFCExportConfigurationsMap configurationsMap = new IFCExportConfigurationsMap();
                configurationsMap.Add(IFCExportConfiguration.GetInSession());
                configurationsMap.AddBuiltInConfigurations();
                configurationsMap.AddSavedConfigurations(doc);

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

                IFCExport mainWindow = new IFCExport(configurationsMap, mruSelection);
                mainWindow.ShowDialog();

                // If user chose to continue
                if (mainWindow.Result == IFCExportResult.ExportAndSaveSettings)
                {
                    // change options
                    IFCExportConfiguration selectedConfig = mainWindow.GetSelectedConfiguration();

                    // Prepare the export options
                    IFCExportOptions exportOptions = new IFCExportOptions();
                    selectedConfig.UpdateOptions(exportOptions, uiDoc.ActiveView.Id);  

                    // prompt for the file name
                    SaveFileDialog fileDialog = new SaveFileDialog();
                    fileDialog.AddExtension = true;

                    String defaultDirectory = m_mruExportPath != null ? m_mruExportPath : null;
                    
                    if (defaultDirectory == null)
                    {
                        String revitFilePath = doc.PathName;
                        if (!String.IsNullOrEmpty(revitFilePath))
                        {
                            defaultDirectory = Path.GetDirectoryName(revitFilePath);
                        }
                    }

                    if (defaultDirectory == null)
                    {
                        defaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    }

                    String defaultFileName = doc.Title;
                    if (String.IsNullOrEmpty(defaultFileName))
                    {
                        defaultFileName = "Project";
                    }
                    else
                    {
                        defaultFileName = Path.GetFileNameWithoutExtension(defaultFileName);
                    }
                    String defaultExtension = mainWindow.GetFileExtension();

                    fileDialog.FileName = defaultFileName;
                    fileDialog.DefaultExt = defaultExtension;
                    fileDialog.Filter = mainWindow.GetFileFilter();
                    fileDialog.InitialDirectory = defaultDirectory;
                    bool? fileDialogResult = fileDialog.ShowDialog();
                
                    // If user chose to continue
                    if (fileDialogResult.HasValue && fileDialogResult.Value)
                    {
                        // Prompt the user for the file location and path
                        String fullName = fileDialog.FileName;
                        String path = Path.GetDirectoryName(fullName);
                        String fileName = Path.GetFileName(fullName);

                        // IFC export requires an open transaction, although no changes should be made
                        Transaction transaction = new Transaction(doc, "Export IFC");   
                        transaction.Start();
                        FailureHandlingOptions failureOptions = transaction.GetFailureHandlingOptions();
                        failureOptions.SetClearAfterRollback(false);
                        transaction.SetFailureHandlingOptions(failureOptions);

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

                        bool result = doc.Export(path, fileName, exportOptions); // pass in the options here
                   
                        if (!result)
                        {
                            //TODO localization
                            TaskDialog taskDialog = new TaskDialog("Error exporting IFC file");
                            taskDialog.MainInstruction = "The IFC export process encountered an error.";
                            taskDialog.MainIcon = TaskDialogIcon.TaskDialogIconWarning;
                            taskDialog.Show();
                        }

                        // Always roll back the transaction started earlier
                        transaction.RollBack();

                        // Remember last successful export location
                        m_mruExportPath = path;    
                    }      
                }
                if (mainWindow.Result == IFCExportResult.SaveSettings || mainWindow.Result == IFCExportResult.ExportAndSaveSettings)
                {
                    configurationsMap = mainWindow.GetModifiedConfigurations();
                    configurationsMap.UpdateSavedConfigurations(doc);

                    // Remember last selected configuration
                    m_mruConfiguration = mainWindow.GetSelectedConfiguration().Name;
                }
            }
            catch (Exception e)
            {
                //TODO localization
                TaskDialog taskDialog = new TaskDialog("Error exporting IFC file");
                taskDialog.MainInstruction = "The IFC export process encountered an error.";
                taskDialog.MainIcon = TaskDialogIcon.TaskDialogIconWarning;
                taskDialog.ExpandedContent = e.ToString();
                taskDialog.Show();
            }
        }
      /// <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.Add(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;

               // prompt for the file name
               SaveFileDialog fileDialog = new SaveFileDialog();
               fileDialog.AddExtension = true;

               String defaultDirectory = m_mruExportPath != null ? m_mruExportPath : null;

               if (defaultDirectory == null)
               {
                  String revitFilePath = TheDocument.PathName;
                  if (!String.IsNullOrEmpty(revitFilePath))
                  {
                     defaultDirectory = Path.GetDirectoryName(revitFilePath);
                  }
               }

               if (defaultDirectory == null)
                  defaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

               string defaultExt = mainWindow.GetFileExtension();

               fileDialog.FileName = multipleFiles ? Properties.Resources.MultipleFiles : GenerateFileNameFromDocument(mainWindow.DocumentsToExport[0], null);
               fileDialog.DefaultExt = defaultExt;
               fileDialog.Filter = mainWindow.GetFileFilter();
               fileDialog.InitialDirectory = defaultDirectory;

               bool? fileDialogResult = fileDialog.ShowDialog();

               // If user chooses to continue
               if (fileDialogResult.HasValue && fileDialogResult.Value)
               {
                  // change options
                  IFCExportConfiguration selectedConfig = mainWindow.GetSelectedConfiguration();

                  // Prompt the user for the file location and path
                  String fullName = fileDialog.FileName;
                  String path = Path.GetDirectoryName(fullName);
                  String fileName = multipleFiles ? string.Empty : 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 = GenerateFileNameFromDocument(document, exportedFileNames) + "." + defaultExt;
                        fullName = path + "\\" + fileName;
                     }

                     // Prepare the export options
                     IFCExportOptions exportOptions = new IFCExportOptions();
                     
                     ElementId activeViewId = GenerateActiveViewIdFromDocument(document);
                     selectedConfig.UpdateOptions(exportOptions, activeViewId);
                     selectedConfig.ActiveViewId = selectedConfig.UseActiveViewGeometry ? activeViewId.IntegerValue : -1;

                     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)
            {
               if (PotentiallyUpdatedConfigurations)
               {
                  configurationsMap = mainWindow.GetModifiedConfigurations();
                  configurationsMap.UpdateSavedConfigurations();
               }

               // Remember last selected configuration
               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();
            }
         }
      }
      public void ExportLinkedDocuments(Autodesk.Revit.DB.Document document, string fileName, Dictionary<ElementId, string> linksGUIDsCache, IFCExportOptions exportOptions)
      {
         // get the extension
         int index = fileName.LastIndexOf('.');
         if (index <= 0)
            return;
         string sExtension = fileName.Substring(index);
         fileName = fileName.Substring(0, index);

         // get all the revit link instances
         FilteredElementCollector collector = new FilteredElementCollector(document);
         ElementFilter elementFilter = new ElementClassFilter(typeof(RevitLinkInstance));
         List<RevitLinkInstance> rvtLinkInstances = collector.WherePasses(elementFilter).Cast<RevitLinkInstance>().ToList();

         IDictionary<String, int> rvtLinkNamesDict = new Dictionary<String, int>();
         IDictionary<String, List<RevitLinkInstance>> rvtLinkNamesToInstancesDict = new Dictionary<String, List<RevitLinkInstance>>();

         try
         {
            // get the link types
            foreach (RevitLinkInstance rvtLinkInstance in rvtLinkInstances)
            {
               // get the instance
               if (rvtLinkInstance == null)
                  continue;

               // check the cache
               if (linksGUIDsCache.Keys.Contains(rvtLinkInstance.Id) == false)
                  continue;

               // get the link document
               Document linkDocument = rvtLinkInstance.GetLinkDocument();
               if (linkDocument == null)
                  continue;

               // get the link file path and name
               String linkPathName = "";
               Parameter originalFileNameParam = linkDocument.ProjectInformation.LookupParameter("Original IFC File Name");
               if (originalFileNameParam != null && originalFileNameParam.StorageType == StorageType.String)
                  linkPathName = originalFileNameParam.AsString();
               else
                  linkPathName = linkDocument.PathName;

               // get the link file name
               String linkFileName = "";
               index = linkPathName.LastIndexOf("\\");
               if (index > 0)
                  linkFileName = linkPathName.Substring(index + 1);
               else
                  linkFileName = linkDocument.Title;

               // remove the extension
               index = linkFileName.LastIndexOf('.');
               if (index > 0)
                  linkFileName = linkFileName.Substring(0, index);

               // add to names count dictionary
               if (!rvtLinkNamesDict.Keys.Contains(linkFileName))
                  rvtLinkNamesDict.Add(linkFileName, 0);
               rvtLinkNamesDict[linkFileName]++;

               // add to names instances dictionary
               if (!rvtLinkNamesToInstancesDict.Keys.Contains(linkPathName))
                  rvtLinkNamesToInstancesDict.Add(linkPathName, new List<RevitLinkInstance>());
               rvtLinkNamesToInstancesDict[linkPathName].Add(rvtLinkInstance);
            }
         }
         catch
         {
         }

         // get the link instances
         // We will keep track of the instances we can't export.
         // Reasons we can't export:
         // 1. The path for the linked instance doesn't exist.
         // 2. Couldn't create a temporary document for exporting the linked instance.
         // 3. The document for the linked instance can't be found.
         // 4. The linked instance is mirrored, non-conformal, or scaled.
         IList<string> pathDoesntExist = new List<string>();
         IList<string> noTempDoc = new List<string>();
         IList<ElementId> cantFindDoc = new List<ElementId>();
         IList<ElementId> nonConformalInst = new List<ElementId>();
         IList<ElementId> scaledInst = new List<ElementId>();
         IList<ElementId> instHasReflection = new List<ElementId>();

         foreach (String linkPathName in rvtLinkNamesToInstancesDict.Keys)
         {
            // get the name of the copy
            String linkPathNameCopy = System.IO.Path.GetTempPath();
            index = linkPathName.LastIndexOf("\\");
            if (index > 0)
               linkPathNameCopy += linkPathName.Substring(index + 1);
            else
               linkPathNameCopy += linkPathName;
            index = linkPathNameCopy.LastIndexOf('.');
            if (index <= 0)
               index = linkPathNameCopy.Length;
            linkPathNameCopy = linkPathNameCopy.Insert(index, " - Copy");
            int i = 1;
            while (File.Exists(linkPathNameCopy))
               linkPathNameCopy = linkPathNameCopy.Insert(index, "(" + (++i).ToString() + ")");

            // copy the file
            File.Copy(linkPathName, linkPathNameCopy);
            if (!File.Exists(linkPathNameCopy))
            {
               pathDoesntExist.Add(linkPathName);
               continue;
            }

            // open the document
            Document documentCopy = null;
            try
            {
               if ((linkPathName.Length >= 4 && linkPathName.Substring(linkPathName.Length - 4).ToLower() == ".ifc") ||
                   (linkPathName.Length >= 7 && linkPathName.Substring(linkPathName.Length - 7).ToLower() == ".ifcxml") ||
                   (linkPathName.Length >= 7 && linkPathName.Substring(linkPathName.Length - 7).ToLower() == ".ifczip"))
                  documentCopy = document.Application.OpenIFCDocument(linkPathNameCopy);
               else
                  documentCopy = document.Application.OpenDocumentFile(linkPathNameCopy);
            }
            catch
            {
               documentCopy = null;
            }

            if (documentCopy == null)
            {
               noTempDoc.Add(linkPathName);
               continue;
            }

            // get the link document unit scale
            DisplayUnitType dutLink = documentCopy.GetUnits().GetFormatOptions(UnitType.UT_Length).DisplayUnits;
            double lengthScaleFactorLink = UnitUtils.ConvertFromInternalUnits(1.0, dutLink);

            // get the link instances
            List<RevitLinkInstance> currRvtLinkInstances = rvtLinkNamesToInstancesDict[linkPathName];
            IList<string> serTransforms = new List<string>();
            IList<string> linkFileNames = new List<string>();

            foreach (RevitLinkInstance currRvtLinkInstance in currRvtLinkInstances)
            {
               // Nothing to report if the element itself is null.
               if (currRvtLinkInstance == null)
                  continue;

               // get the link document
               Document linkDocument = currRvtLinkInstance.GetLinkDocument();
               if (linkDocument == null)
               {
                  cantFindDoc.Add(currRvtLinkInstance.Id);
                  continue;
               }

               // get the link transform
               Transform tr = currRvtLinkInstance.GetTransform();

               // We can't handle non-conformal, scaled, or mirrored transforms.
               if (!tr.IsConformal)
               {
                  nonConformalInst.Add(currRvtLinkInstance.Id);
                  continue;
               }

               if (tr.HasReflection)
               {
                  instHasReflection.Add(currRvtLinkInstance.Id);
                  continue;
               }

               if (!MathUtil.IsAlmostEqual(tr.Determinant, 1.0))
               {
                  scaledInst.Add(currRvtLinkInstance.Id);
                  continue;
               }

               // get the link file path and name
               String linkFileName = "";
               index = linkPathName.LastIndexOf("\\");
               if (index > 0)
                  linkFileName = linkPathName.Substring(index + 1);
               else
                  linkFileName = linkDocument.Title;

               // remove the extension
               index = linkFileName.LastIndexOf('.');
               if (index > 0)
                  linkFileName = linkFileName.Substring(0, index);

               //if link was an IFC file then make a different formating to the file name
               if ((linkPathName.Length >= 4 && linkPathName.Substring(linkPathName.Length - 4).ToLower() == ".ifc") ||
                   (linkPathName.Length >= 7 && linkPathName.Substring(linkPathName.Length - 7).ToLower() == ".ifcxml") ||
                   (linkPathName.Length >= 7 && linkPathName.Substring(linkPathName.Length - 7).ToLower() == ".ifczip"))
               {
                  String fName = fileName;

                  //get output path and add to the new file name 
                  index = fName.LastIndexOf("\\");
                  if (index > 0)
                     fName = fName.Substring(0, index + 1);
                  else
                     fName = "";

                  //construct IFC file name
                  linkFileName = fName + linkFileName + "-";

                  //add guid
                  linkFileName += linksGUIDsCache[currRvtLinkInstance.Id];
               }
               else
               {
                  // check if there are multiple instances with the same name
                  bool bMultiple = (rvtLinkNamesDict[linkFileName] > 1);

                  // add the path
                  linkFileName = fileName + "-" + linkFileName;

                  // add the guid
                  if (bMultiple)
                  {
                     linkFileName += "-";
                     linkFileName += linksGUIDsCache[currRvtLinkInstance.Id];
                  }
               }

               // add the extension
               linkFileName += sExtension;

               linkFileNames.Add(linkFileName);

               // scale the transform origin
               tr.Origin *= lengthScaleFactorLink;

               // serialize transform
               serTransforms.Add(SerializeTransform(tr));
            }

            // IFC export requires an open transaction, although no changes should be made
            Transaction transaction = new Transaction(documentCopy, "Export IFC Link");
            transaction.Start();
            FailureHandlingOptions failureOptions = transaction.GetFailureHandlingOptions();
            failureOptions.SetClearAfterRollback(false);
            transaction.SetFailureHandlingOptions(failureOptions);

            // export
            try
            {
               int numLinkInstancesToExport = linkFileNames.Count;
               exportOptions.AddOption("NumberOfExportedLinkInstances", numLinkInstancesToExport.ToString());

               for (int ii = 0; ii < numLinkInstancesToExport; ii++)
               {
                  string optionName = (ii == 0) ? "ExportLinkInstanceTransform" : "ExportLinkInstanceTransform" + (ii + 1).ToString();
                  exportOptions.AddOption(optionName, serTransforms[ii]);

                  // Don't pass in file name for the first link instance.
                  if (ii == 0)
                     continue;

                  optionName = "ExportLinkInstanceFileName" + (ii + 1).ToString();
                  exportOptions.AddOption(optionName, linkFileNames[ii]);
               }

               // Pass in the first value; the rest will  be in the options.
               String path_ = Path.GetDirectoryName(linkFileNames[0]);
               String fileName_ = Path.GetFileName(linkFileNames[0]);
               bool result = documentCopy.Export(path_, fileName_, exportOptions); // pass in the options here
            }
            catch
            {
            }

            // rollback the transaction
            transaction.RollBack();

            // close the document
            documentCopy.Close(false);

            // delete the copy
            try
            {
               File.Delete(linkPathNameCopy);
            }
            catch
            {
            }

            // Show user errors, if any.
            int numBadInstances = pathDoesntExist.Count + noTempDoc.Count + cantFindDoc.Count + nonConformalInst.Count
                + scaledInst.Count + instHasReflection.Count;
            if (numBadInstances > 0)
            {
               using (TaskDialog taskDialog = new TaskDialog(Properties.Resources.IFCExport))
               {
                  taskDialog.MainInstruction = string.Format(Properties.Resources.LinkInstanceExportErrorMain, numBadInstances);
                  taskDialog.MainIcon = TaskDialogIcon.TaskDialogIconWarning;
                  taskDialog.TitleAutoPrefix = false;

                  string expandedContent = "";
                  AddExpandedStringContent(ref expandedContent, Properties.Resources.LinkInstanceExportErrorPath, pathDoesntExist);
                  AddExpandedStringContent(ref expandedContent, Properties.Resources.LinkInstanceExportCantCreateDoc, noTempDoc);
                  AddExpandedElementIdContent(ref expandedContent, Properties.Resources.LinkInstanceExportCantFindDoc, cantFindDoc);
                  AddExpandedElementIdContent(ref expandedContent, Properties.Resources.LinkInstanceExportNonConformal, nonConformalInst);
                  AddExpandedElementIdContent(ref expandedContent, Properties.Resources.LinkInstanceExportScaled, scaledInst);
                  AddExpandedElementIdContent(ref expandedContent, Properties.Resources.LinkInstanceExportHasReflection, instHasReflection);

                  taskDialog.ExpandedContent = expandedContent;
                  TaskDialogResult result = taskDialog.Show();
               }
            }
         }
      }
Beispiel #30
0
        /// <summary>
        /// Updates the IFCExportOptions with the settings in this configuration.
        /// </summary>
        /// <param name="options">The IFCExportOptions to update.</param>
        /// <param name="filterViewId">The id of the view that will be used to select which elements to export.</param>
        public void UpdateOptions(IFCExportOptions options, ElementId filterViewId)
        {
            JavaScriptSerializer ser = new JavaScriptSerializer();

            foreach (var prop in GetType().GetProperties())
            {
                switch (prop.Name)
                {
                case "Name":
                    options.AddOption("ConfigName", Name);    // Add config name into the option for use in the exporter
                    break;

                case "IFCVersion":
                    options.FileVersion = IFCVersion;
                    break;

                case "ActivePhaseId":
                    if (IFCPhaseAttributes.Validate(ActivePhaseId))
                    {
                        options.AddOption(prop.Name, ActivePhaseId.ToString());
                    }
                    break;

                case "SpaceBoundaries":
                    options.SpaceBoundaryLevel = SpaceBoundaries;
                    break;

                case "SplitWallsAndColumns":
                    options.WallAndColumnSplitting = SplitWallsAndColumns;
                    break;

                case "ExportBaseQuantities":
                    options.ExportBaseQuantities = ExportBaseQuantities;
                    break;

                case "ProjectAddress":
                    string projectAddrJsonString = ser.Serialize(ProjectAddress);
                    options.AddOption(prop.Name, projectAddrJsonString);
                    break;

                case "ClassificationSettings":
                    string classificationJsonStr = ser.Serialize(ClassificationSettings);
                    options.AddOption(prop.Name, classificationJsonStr);
                    break;

                default:
                    var propVal = prop.GetValue(this, null);
                    if (propVal != null)
                    {
                        options.AddOption(prop.Name, propVal.ToString());
                    }
                    break;
                }
            }

            options.FilterViewId = VisibleElementsOfCurrentView ? filterViewId : ElementId.InvalidElementId;

            string uiVersion = IFCUISettings.GetAssemblyVersion();

            options.AddOption("AlternateUIVersion", uiVersion);
        }
Beispiel #31
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      doc   = uiDoc.Document;

                TheDocument = doc;

                IFCExportConfigurationsMap configurationsMap = new IFCExportConfigurationsMap();
                configurationsMap.Add(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(doc, configurationsMap, mruSelection);
                mainWindow.ShowDialog();

                // If user chose to continue
                if (mainWindow.Result == IFCExportResult.ExportAndSaveSettings)
                {
                    // change options
                    IFCExportConfiguration selectedConfig = mainWindow.GetSelectedConfiguration();

                    // Prepare the export options
                    IFCExportOptions exportOptions = new IFCExportOptions();
                    selectedConfig.UpdateOptions(exportOptions, uiDoc.ActiveView.Id);

                    // prompt for the file name
                    SaveFileDialog fileDialog = new SaveFileDialog();
                    fileDialog.AddExtension = true;

                    String defaultDirectory = m_mruExportPath != null ? m_mruExportPath : null;

                    if (defaultDirectory == null)
                    {
                        String revitFilePath = doc.PathName;
                        if (!String.IsNullOrEmpty(revitFilePath))
                        {
                            defaultDirectory = Path.GetDirectoryName(revitFilePath);
                        }
                    }

                    if (defaultDirectory == null)
                    {
                        defaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    }

                    String defaultFileName = doc.Title;
                    if (String.IsNullOrEmpty(defaultFileName))
                    {
                        defaultFileName = "Project";
                    }
                    else
                    {
                        defaultFileName = Path.GetFileNameWithoutExtension(defaultFileName);
                    }
                    String defaultExtension = mainWindow.GetFileExtension();

                    fileDialog.FileName         = defaultFileName;
                    fileDialog.DefaultExt       = defaultExtension;
                    fileDialog.Filter           = mainWindow.GetFileFilter();
                    fileDialog.InitialDirectory = defaultDirectory;
                    bool?fileDialogResult = fileDialog.ShowDialog();

                    // If user chose to continue
                    if (fileDialogResult.HasValue && fileDialogResult.Value)
                    {
                        // Prompt the user for the file location and path
                        String fullName = fileDialog.FileName;
                        String path     = Path.GetDirectoryName(fullName);
                        String fileName = Path.GetFileName(fullName);

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

                        // IFC export requires an open transaction, although no changes should be made
                        Transaction transaction = new Transaction(doc, "Export IFC");
                        transaction.Start();
                        FailureHandlingOptions failureOptions = transaction.GetFailureHandlingOptions();
                        failureOptions.SetClearAfterRollback(false);
                        transaction.SetFailureHandlingOptions(failureOptions);

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

                        bool result = doc.Export(path, fileName, exportOptions); // pass in the options here

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

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

                        // Cache for links guids
                        Dictionary <ElementId, string> linksGUIDsCache = new Dictionary <ElementId, string>();
                        if (selectedConfig.ExportLinkedFiles == true)
                        {
                            Autodesk.Revit.DB.FilteredElementCollector collector = new FilteredElementCollector(doc);
                            collector.WhereElementIsNotElementType().OfCategory(BuiltInCategory.OST_RvtLinks);
                            System.Collections.Generic.ICollection <ElementId> rvtLinkInstanceIds = collector.ToElementIds();
                            foreach (ElementId linkId in rvtLinkInstanceIds)
                            {
                                Element linkInstance = doc.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);
                                }
                            }
                        }

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

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

                        // 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)
                {
                    if (PotentiallyUpdatedConfigurations)
                    {
                        configurationsMap = mainWindow.GetModifiedConfigurations();
                        configurationsMap.UpdateSavedConfigurations();
                    }

                    // Remember last selected configuration
                    m_mruConfiguration = mainWindow.GetSelectedConfiguration().Name;
                }
            }
            catch (Exception e)
            {
                using (TaskDialog taskDialog = new TaskDialog(Properties.Resources.IFCExport))
                {
                    taskDialog.MainInstruction = Properties.Resources.IFCExportProcessError;
                    taskDialog.MainIcon        = TaskDialogIcon.TaskDialogIconWarning;
                    taskDialog.ExpandedContent = e.ToString();
                    TaskDialogResult result = taskDialog.Show();
                }
            }
        }
        /// <summary>
        /// Updates the IFCExportOptions with the settings in this configuration.
        /// </summary>
        /// <param name="options">The IFCExportOptions to update.</param>
        /// <param name="filterViewId">The id of the view that will be used to select which elements to export.</param>
        public void UpdateOptions(IFCExportOptions options, ElementId filterViewId)
        {
            options.FileVersion = IFCVersion;
            options.SpaceBoundaryLevel = SpaceBoundaries;
            options.ExportBaseQuantities = ExportBaseQuantities;
            options.WallAndColumnSplitting = SplitWallsAndColumns;
           options.FilterViewId = VisibleElementsOfCurrentView ? filterViewId : ElementId.InvalidElementId;
            options.AddOption("ExportInternalRevitPropertySets", ExportInternalRevitPropertySets.ToString());
            options.AddOption("ExportIFCCommonPropertySets", ExportIFCCommonPropertySets.ToString());
            options.AddOption("ExportAnnotations", Export2DElements.ToString());
            options.AddOption("Use2DRoomBoundaryForVolume",Use2DRoomBoundaryForVolume.ToString());
            options.AddOption("UseFamilyAndTypeNameForReference",UseFamilyAndTypeNameForReference.ToString());
            options.AddOption("ExportVisibleElementsInView", VisibleElementsOfCurrentView.ToString());
            options.AddOption("ExportPartsAsBuildingElements", ExportPartsAsBuildingElements.ToString());
            options.AddOption("UseActiveViewGeometry", UseActiveViewGeometry.ToString());
            options.AddOption("ExportSpecificSchedules", ExportSpecificSchedules.ToString());
            options.AddOption("ExportBoundingBox", ExportBoundingBox.ToString());
            options.AddOption("ExportSolidModelRep", ExportSolidModelRep.ToString());
            options.AddOption("ExportSchedulesAsPsets", ExportSchedulesAsPsets.ToString());
            options.AddOption("ExportUserDefinedPsets", ExportUserDefinedPsets.ToString());
            options.AddOption("ExportLinkedFiles", ExportLinkedFiles.ToString());
            options.AddOption("IncludeSiteElevation", IncludeSiteElevation.ToString());
            options.AddOption("TessellationLevelOfDetail", TessellationLevelOfDetail.ToString());
            options.AddOption("ActiveViewId", ActiveViewId.ToString());
            options.AddOption("StoreIFCGUID", StoreIFCGUID.ToString());

           // The active phase may not be valid if we are exporting multiple projects. However, if projects share a template that defines the phases,
           // then the ActivePhaseId would likely be valid for all.  There is some small chance that the ActivePhaseId would be a valid, but different, phase
           // in different projects, but that is unlikely enough that it seems worth warning against it but allowing the better functionality in general.
           if (IFCPhaseAttributes.Validate(ActivePhaseId))
            options.AddOption("ActivePhase", ActivePhaseId.ToString());

            options.AddOption("FileType", IFCFileType.ToString());
            string uiVersion = IFCUISettings.GetAssemblyVersion();
            options.AddOption("AlternateUIVersion", uiVersion);

            options.AddOption("ConfigName", Name);      // Add config name into the option for use in the exporter
            options.AddOption("ExportUserDefinedPsetsFileName", ExportUserDefinedPsetsFileName);
            options.AddOption("ExportRoomsInView", ExportRoomsInView.ToString());
        }
Beispiel #33
0
 private void UpdateOptionOverrided(IFCExportOptions IFCOptions, ElementId activeViewId)
 {
     IFCExportUIOverride::BIM.IFC.Export.UI.IFCExportConfiguration config = _IFCExportConfiguration as IFCExportUIOverride::BIM.IFC.Export.UI.IFCExportConfiguration;
     config.UpdateOptions(IFCOptions, activeViewId);
 }
        /// <summary>
        /// Updates the IFCExportOptions with the settings in this configuration.
        /// </summary>
        /// <param name="options">The IFCExportOptions to update.</param>
        /// <param name="filterViewId">The filter view.</param>
        public void UpdateOptions(IFCExportOptions options, ElementId filterViewId)
        {
            options.FileVersion = IFCVersion;
            options.SpaceBoundaryLevel = SpaceBoundaries;
            options.ExportBaseQuantities = ExportBaseQuantities;
            options.WallAndColumnSplitting = SplitWallsAndColumns;
            if (VisibleElementsOfCurrentView) 
                options.FilterViewId = filterViewId;
            else
                options.FilterViewId = ElementId.InvalidElementId;
            options.AddOption("ExportInternalRevitPropertySets", ExportInternalRevitPropertySets.ToString());
            options.AddOption("ExportIFCCommonPropertySets", ExportIFCCommonPropertySets.ToString());
            options.AddOption("ExportAnnotations", Export2DElements.ToString());
            options.AddOption("Use2DRoomBoundaryForVolume",Use2DRoomBoundaryForVolume.ToString());
            options.AddOption("UseFamilyAndTypeNameForReference",UseFamilyAndTypeNameForReference.ToString());
            options.AddOption("ExportVisibleElementsInView", VisibleElementsOfCurrentView.ToString());
            options.AddOption("ExportPartsAsBuildingElements", ExportPartsAsBuildingElements.ToString());
            options.AddOption("ExportBoundingBox", ExportBoundingBox.ToString());
            options.AddOption("ExportSolidModelRep", ExportSolidModelRep.ToString());
            options.AddOption("ExportSchedulesAsPsets", ExportSchedulesAsPsets.ToString());
            options.AddOption("ExportUserDefinedPsets", ExportUserDefinedPsets.ToString());
            options.AddOption("ExportLinkedFiles", ExportLinkedFiles.ToString());
            options.AddOption("IncludeSiteElevation", IncludeSiteElevation.ToString());
            options.AddOption("UseCoarseTessellation", UseCoarseTessellation.ToString());
            options.AddOption("StoreIFCGUID", StoreIFCGUID.ToString());
            options.AddOption("ActivePhase", ActivePhaseId.ToString());

            options.AddOption("FileType", IFCFileType.ToString());
            string uiVersion = IFCUISettings.GetAssemblyVersion();
            options.AddOption("AlternateUIVersion", uiVersion);

            options.AddOption("ConfigName", Name);      // Add config name into the option for use in the exporter
            options.AddOption("ExportUserDefinedPsetsFileName", ExportUserDefinedPsetsFileName);
        }