/// <summary>
        /// Event when OK button is pressed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void buttonOK_Click(object sender, RoutedEventArgs args)
        {
            // Save the user selected/input Exchange Requirement
            if (!string.IsNullOrEmpty(comboBoxExchangeRequirement.Text) && !comboBoxExchangeRequirement.Text.Equals(m_selectedExchangeRequirement))
            {
                m_selectedExchangeRequirement = comboBoxExchangeRequirement.Text;
                if (!exchangeRequirementList.Contains(m_selectedExchangeRequirement))
                {
                    exchangeRequirementList.Add(m_selectedExchangeRequirement);
                }
            }

            string erName = IFCExchangeRequirements.GetERName(m_selectedExchangeRequirement);

            m_newFileHeaderItem.FileDescription += "ExchangeRequirement [" + erName + "]";

            // Saved changes to both Address Tab items and File Header Tab items if they have changed
            if (m_newFileHeaderItem.isUnchanged(m_savedFileHeaderItem) == false)
            {
                m_newFileHeader.UpdateFileHeader(IFCCommandOverrideApplication.TheDocument, m_newFileHeaderItem);
            }

            Close();
        }
        /// <summary>
        /// Adds the saved configuration from document to the map.
        /// </summary>
        public void AddSavedConfigurations()
        {
            try
            {
                // find the config in old schema.
                if (m_OldSchema == null)
                {
                    m_OldSchema = Schema.Lookup(s_OldSchemaId);

                    if (m_OldSchema != null)
                    {
                        foreach (DataStorage storedSetup in GetSavedConfigurations(m_OldSchema))
                        {
                            Entity configEntity = storedSetup.GetEntity(m_OldSchema);
                            IFCExportConfiguration configuration = IFCExportConfiguration.CreateDefaultConfiguration();
                            configuration.Name                            = configEntity.Get <String>(s_setupName);
                            configuration.IFCVersion                      = (IFCVersion)configEntity.Get <int>(s_setupVersion);
                            configuration.ExchangeRequirement             = IFCExchangeRequirements.ParseEREnum(configEntity.Get <String>(s_exchangeRequirement));
                            configuration.IFCFileType                     = (IFCFileFormat)configEntity.Get <int>(s_setupFileFormat);
                            configuration.SpaceBoundaries                 = configEntity.Get <int>(s_setupSpaceBoundaries);
                            configuration.ExportBaseQuantities            = configEntity.Get <bool>(s_setupQTO);
                            configuration.SplitWallsAndColumns            = configEntity.Get <bool>(s_splitWallsAndColumns);
                            configuration.Export2DElements                = configEntity.Get <bool>(s_setupExport2D);
                            configuration.ExportInternalRevitPropertySets = configEntity.Get <bool>(s_setupExportRevitProps);
                            Field fieldIFCCommonPropertySets = m_OldSchema.GetField(s_setupExportIFCCommonProperty);
                            if (fieldIFCCommonPropertySets != null)
                            {
                                configuration.ExportIFCCommonPropertySets = configEntity.Get <bool>(s_setupExportIFCCommonProperty);
                            }
                            configuration.Use2DRoomBoundaryForVolume       = configEntity.Get <bool>(s_setupUse2DForRoomVolume);
                            configuration.UseFamilyAndTypeNameForReference = configEntity.Get <bool>(s_setupUseFamilyAndTypeName);
                            Field fieldPartsAsBuildingElements = m_OldSchema.GetField(s_setupExportPartsAsBuildingElements);
                            if (fieldPartsAsBuildingElements != null)
                            {
                                configuration.ExportPartsAsBuildingElements = configEntity.Get <bool>(s_setupExportPartsAsBuildingElements);
                            }
                            Field fieldExportBoundingBox = m_OldSchema.GetField(s_setupExportBoundingBox);
                            if (fieldExportBoundingBox != null)
                            {
                                configuration.ExportBoundingBox = configEntity.Get <bool>(s_setupExportBoundingBox);
                            }
                            Field fieldExportSolidModelRep = m_OldSchema.GetField(s_setupExportSolidModelRep);
                            if (fieldExportSolidModelRep != null)
                            {
                                configuration.ExportSolidModelRep = configEntity.Get <bool>(s_setupExportSolidModelRep);
                            }
                            Field fieldExportSchedulesAsPsets = m_OldSchema.GetField(s_setupExportSchedulesAsPsets);
                            if (fieldExportSchedulesAsPsets != null)
                            {
                                configuration.ExportSchedulesAsPsets = configEntity.Get <bool>(s_setupExportSchedulesAsPsets);
                            }
                            Field fieldExportUserDefinedPsets = m_OldSchema.GetField(s_setupExportUserDefinedPsets);
                            if (fieldExportUserDefinedPsets != null)
                            {
                                configuration.ExportUserDefinedPsets = configEntity.Get <bool>(s_setupExportUserDefinedPsets);
                            }
                            Field fieldExportUserDefinedPsetsFileName = m_OldSchema.GetField(s_setupExportUserDefinedPsetsFileName);
                            if (fieldExportUserDefinedPsetsFileName != null)
                            {
                                configuration.ExportUserDefinedPsetsFileName = configEntity.Get <string>(s_setupExportUserDefinedPsetsFileName);
                            }

                            Field fieldExportUserDefinedParameterMapingTable = m_OldSchema.GetField(s_setupExportUserDefinedParameterMapping);
                            if (fieldExportUserDefinedParameterMapingTable != null)
                            {
                                configuration.ExportUserDefinedParameterMapping = configEntity.Get <bool>(s_setupExportUserDefinedParameterMapping);
                            }

                            Field fieldExportUserDefinedParameterMappingFileName = m_OldSchema.GetField(s_setupExportUserDefinedParameterMappingFileName);
                            if (fieldExportUserDefinedParameterMappingFileName != null)
                            {
                                configuration.ExportUserDefinedParameterMappingFileName = configEntity.Get <string>(s_setupExportUserDefinedParameterMappingFileName);
                            }

                            Field fieldExportLinkedFiles = m_OldSchema.GetField(s_setupExportLinkedFiles);
                            if (fieldExportLinkedFiles != null)
                            {
                                configuration.ExportLinkedFiles = configEntity.Get <bool>(s_setupExportLinkedFiles);
                            }
                            Field fieldIncludeSiteElevation = m_OldSchema.GetField(s_setupIncludeSiteElevation);
                            if (fieldIncludeSiteElevation != null)
                            {
                                configuration.IncludeSiteElevation = configEntity.Get <bool>(s_setupIncludeSiteElevation);
                            }
                            Field fieldStoreIFCGUID = m_OldSchema.GetField(s_setupStoreIFCGUID);
                            if (fieldStoreIFCGUID != null)
                            {
                                configuration.StoreIFCGUID = configEntity.Get <bool>(s_setupStoreIFCGUID);
                            }
                            Field fieldActivePhase = m_OldSchema.GetField(s_setupActivePhase);
                            if (fieldActivePhase != null)
                            {
                                configuration.ActivePhaseId = int.Parse(configEntity.Get <string>(s_setupActivePhase));
                            }
                            Field fieldExportRoomsInView = m_OldSchema.GetField(s_setupExportRoomsInView);
                            if (fieldExportRoomsInView != null)
                            {
                                configuration.ExportRoomsInView = configEntity.Get <bool>(s_setupExportRoomsInView);
                            }
                            Field fieldIncludeSteelElements = m_OldSchema.GetField(s_includeSteelElements);
                            if (fieldIncludeSteelElements != null)
                            {
                                configuration.IncludeSteelElements = configEntity.Get <bool>(s_includeSteelElements);
                            }
                            Field fieldUseOnlyTriangulation = m_OldSchema.GetField(s_useOnlyTriangulation);
                            if (fieldUseOnlyTriangulation != null)
                            {
                                configuration.UseOnlyTriangulation = configEntity.Get <bool>(s_useOnlyTriangulation);
                            }
                            Field fieldUseTypeNameOnlyForIfcType = m_OldSchema.GetField(s_useTypeNameOnlyForIfcType);
                            if (fieldUseTypeNameOnlyForIfcType != null)
                            {
                                configuration.UseTypeNameOnlyForIfcType = configEntity.Get <bool>(s_useTypeNameOnlyForIfcType);
                            }
                            Field fieldUseVisibleRevitNameAsEntityName = m_OldSchema.GetField(s_useVisibleRevitNameAsEntityName);
                            if (fieldUseVisibleRevitNameAsEntityName != null)
                            {
                                configuration.UseVisibleRevitNameAsEntityName = configEntity.Get <bool>(s_useVisibleRevitNameAsEntityName);
                            }
                            Field fieldTessellationLevelOfDetail = m_OldSchema.GetField(s_setupTessellationLevelOfDetail);
                            if (fieldTessellationLevelOfDetail != null)
                            {
                                configuration.TessellationLevelOfDetail = configEntity.Get <double>(s_setupTessellationLevelOfDetail);
                            }

                            AddOrReplace(configuration);
                        }
                    }
                }

                // This is the newer schema
                if (m_mapSchema == null)
                {
                    m_mapSchema = Schema.Lookup(s_mapSchemaId);

                    if (m_mapSchema != null)
                    {
                        foreach (DataStorage storedSetup in GetSavedConfigurations(m_mapSchema))
                        {
                            Entity configEntity = storedSetup.GetEntity(m_mapSchema);
                            IDictionary <string, string> configMap     = configEntity.Get <IDictionary <string, string> >(s_configMapField);
                            IFCExportConfiguration       configuration = IFCExportConfiguration.CreateDefaultConfiguration();
                            if (configMap.ContainsKey(s_setupName))
                            {
                                configuration.Name = configMap[s_setupName];
                            }
                            if (configMap.ContainsKey(s_setupVersion))
                            {
                                configuration.IFCVersion = (IFCVersion)Enum.Parse(typeof(IFCVersion), configMap[s_setupVersion]);
                            }
                            if (configMap.ContainsKey(s_exchangeRequirement))
                            {
                                configuration.ExchangeRequirement = IFCExchangeRequirements.ParseEREnum(configMap[s_exchangeRequirement]);
                            }
                            if (configMap.ContainsKey(s_setupFileFormat))
                            {
                                configuration.IFCFileType = (IFCFileFormat)Enum.Parse(typeof(IFCFileFormat), configMap[s_setupFileFormat]);
                            }
                            if (configMap.ContainsKey(s_setupSpaceBoundaries))
                            {
                                configuration.SpaceBoundaries = int.Parse(configMap[s_setupSpaceBoundaries]);
                            }
                            if (configMap.ContainsKey(s_setupActivePhase))
                            {
                                configuration.ActivePhaseId = int.Parse(configMap[s_setupActivePhase]);
                            }
                            if (configMap.ContainsKey(s_setupQTO))
                            {
                                configuration.ExportBaseQuantities = bool.Parse(configMap[s_setupQTO]);
                            }
                            if (configMap.ContainsKey(s_setupCurrentView))
                            {
                                configuration.VisibleElementsOfCurrentView = bool.Parse(configMap[s_setupCurrentView]);
                            }
                            if (configMap.ContainsKey(s_splitWallsAndColumns))
                            {
                                configuration.SplitWallsAndColumns = bool.Parse(configMap[s_splitWallsAndColumns]);
                            }
                            if (configMap.ContainsKey(s_setupExport2D))
                            {
                                configuration.Export2DElements = bool.Parse(configMap[s_setupExport2D]);
                            }
                            if (configMap.ContainsKey(s_setupExportRevitProps))
                            {
                                configuration.ExportInternalRevitPropertySets = bool.Parse(configMap[s_setupExportRevitProps]);
                            }
                            if (configMap.ContainsKey(s_setupExportIFCCommonProperty))
                            {
                                configuration.ExportIFCCommonPropertySets = bool.Parse(configMap[s_setupExportIFCCommonProperty]);
                            }
                            if (configMap.ContainsKey(s_setupUse2DForRoomVolume))
                            {
                                configuration.Use2DRoomBoundaryForVolume = bool.Parse(configMap[s_setupUse2DForRoomVolume]);
                            }
                            if (configMap.ContainsKey(s_setupUseFamilyAndTypeName))
                            {
                                configuration.UseFamilyAndTypeNameForReference = bool.Parse(configMap[s_setupUseFamilyAndTypeName]);
                            }
                            if (configMap.ContainsKey(s_setupExportPartsAsBuildingElements))
                            {
                                configuration.ExportPartsAsBuildingElements = bool.Parse(configMap[s_setupExportPartsAsBuildingElements]);
                            }
                            if (configMap.ContainsKey(s_useActiveViewGeometry))
                            {
                                configuration.UseActiveViewGeometry = bool.Parse(configMap[s_useActiveViewGeometry]);
                            }
                            if (configMap.ContainsKey(s_setupExportSpecificSchedules))
                            {
                                configuration.ExportSpecificSchedules = bool.Parse(configMap[s_setupExportSpecificSchedules]);
                            }
                            if (configMap.ContainsKey(s_setupExportBoundingBox))
                            {
                                configuration.ExportBoundingBox = bool.Parse(configMap[s_setupExportBoundingBox]);
                            }
                            if (configMap.ContainsKey(s_setupExportSolidModelRep))
                            {
                                configuration.ExportSolidModelRep = bool.Parse(configMap[s_setupExportSolidModelRep]);
                            }
                            if (configMap.ContainsKey(s_setupExportSchedulesAsPsets))
                            {
                                configuration.ExportSchedulesAsPsets = bool.Parse(configMap[s_setupExportSchedulesAsPsets]);
                            }
                            if (configMap.ContainsKey(s_setupExportUserDefinedPsets))
                            {
                                configuration.ExportUserDefinedPsets = bool.Parse(configMap[s_setupExportUserDefinedPsets]);
                            }
                            if (configMap.ContainsKey(s_setupExportUserDefinedPsetsFileName))
                            {
                                configuration.ExportUserDefinedPsetsFileName = configMap[s_setupExportUserDefinedPsetsFileName];
                            }
                            if (configMap.ContainsKey(s_setupExportUserDefinedParameterMapping))
                            {
                                configuration.ExportUserDefinedParameterMapping = bool.Parse(configMap[s_setupExportUserDefinedParameterMapping]);
                            }
                            if (configMap.ContainsKey(s_setupExportUserDefinedParameterMappingFileName))
                            {
                                configuration.ExportUserDefinedParameterMappingFileName = configMap[s_setupExportUserDefinedParameterMappingFileName];
                            }
                            if (configMap.ContainsKey(s_setupExportLinkedFiles))
                            {
                                configuration.ExportLinkedFiles = bool.Parse(configMap[s_setupExportLinkedFiles]);
                            }
                            if (configMap.ContainsKey(s_setupIncludeSiteElevation))
                            {
                                configuration.IncludeSiteElevation = bool.Parse(configMap[s_setupIncludeSiteElevation]);
                            }
                            if (configMap.ContainsKey(s_setupStoreIFCGUID))
                            {
                                configuration.StoreIFCGUID = bool.Parse(configMap[s_setupStoreIFCGUID]);
                            }
                            if (configMap.ContainsKey(s_setupExportRoomsInView))
                            {
                                configuration.ExportRoomsInView = bool.Parse(configMap[s_setupExportRoomsInView]);
                            }
                            if (configMap.ContainsKey(s_includeSteelElements))
                            {
                                configuration.IncludeSteelElements = bool.Parse(configMap[s_includeSteelElements]);
                            }
                            if (configMap.ContainsKey(s_useTypeNameOnlyForIfcType))
                            {
                                configuration.UseTypeNameOnlyForIfcType = bool.Parse(configMap[s_useTypeNameOnlyForIfcType]);
                            }
                            if (configMap.ContainsKey(s_useVisibleRevitNameAsEntityName))
                            {
                                configuration.UseVisibleRevitNameAsEntityName = bool.Parse(configMap[s_useVisibleRevitNameAsEntityName]);
                            }
                            if (configMap.ContainsKey(s_useOnlyTriangulation))
                            {
                                configuration.UseOnlyTriangulation = bool.Parse(configMap[s_useOnlyTriangulation]);
                            }
                            if (configMap.ContainsKey(s_setupTessellationLevelOfDetail))
                            {
                                configuration.TessellationLevelOfDetail = double.Parse(configMap[s_setupTessellationLevelOfDetail]);
                            }
                            if (configMap.ContainsKey(s_setupSitePlacement))
                            {
                                SiteTransformBasis siteTrfBasis = SiteTransformBasis.Shared;
                                if (Enum.TryParse(configMap[s_setupSitePlacement], out siteTrfBasis))
                                {
                                    configuration.SitePlacement = siteTrfBasis;
                                }
                            }
                            // Geo Reference info
                            if (configMap.ContainsKey(s_selectedSite))
                            {
                                configuration.SelectedSite = configMap[s_selectedSite];
                            }
                            if (configMap.ContainsKey(s_geoRefCRSName))
                            {
                                configuration.GeoRefCRSName = configMap[s_geoRefCRSName];
                            }
                            if (configMap.ContainsKey(s_geoRefCRSDesc))
                            {
                                configuration.GeoRefCRSDesc = configMap[s_geoRefCRSDesc];
                            }
                            if (configMap.ContainsKey(s_geoRefEPSGCode))
                            {
                                configuration.GeoRefEPSGCode = configMap[s_geoRefEPSGCode];
                            }
                            if (configMap.ContainsKey(s_geoRefGeodeticDatum))
                            {
                                configuration.GeoRefGeodeticDatum = configMap[s_geoRefGeodeticDatum];
                            }
                            if (configMap.ContainsKey(s_geoRefMapUnit))
                            {
                                configuration.GeoRefMapUnit = configMap[s_geoRefMapUnit];
                            }

                            AddOrReplace(configuration);
                        }
                    }
                }

                // In this latest schema, the entire configuration for one config is stored as a json string in the entirety
                if (m_jsonSchema == null)
                {
                    m_jsonSchema = Schema.Lookup(s_jsonSchemaId);
                    if (m_jsonSchema != null)
                    {
                        foreach (DataStorage storedSetup in GetSavedConfigurations(m_jsonSchema))
                        {
                            Entity configEntity                  = storedSetup.GetEntity(m_jsonSchema);
                            string configData                    = configEntity.Get <string>(s_configMapField);
                            JavaScriptSerializer   ser           = new JavaScriptSerializer();
                            IFCExportConfiguration configuration = ser.Deserialize <IFCExportConfiguration>(configData);
                            AddOrReplace(configuration);
                        }
                    }
                }

                // Add the last selected configurations if any
                if (IFCExport.LastSelectedConfig != null && IFCExport.LastSelectedConfig.Count > 0)
                {
                    foreach (KeyValuePair <string, IFCExportConfiguration> lastSelConfig in IFCExport.LastSelectedConfig)
                    {
                        AddOrReplace(lastSelConfig.Value);
                    }
                }
            }
            catch (System.Exception)
            {
                // to avoid fail to show the dialog if any exception throws in reading schema.
            }
        }