void propertyGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            XbimReferencedModelViewModel modelWrapper = (XbimReferencedModelViewModel)e.Row.Item;

            try
            {
                //if build fails, cancel add row
                if (!modelWrapper.TryBuildAndAddTo(DataContext as IfcStore))
                {
                    e.Cancel = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Something went wrong, object might not have been added\n\n" + ex.Message);
            }
        }
        private void OpenFederationCmdExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            var dlg = new OpenFileDialog();
            dlg.Filter = "Xbim Federation Files|*.xbimf|Xbim Model Files|*.ifc"; // Filter files by extension
            dlg.CheckFileExists = true;
            dlg.Multiselect = true;
            var done = dlg.ShowDialog(this);
            if (done.Value)
            {
                if (dlg.FileNames.Any()) // collection is not empty
                {
                    //use the first filename it's extension to decide which action should happen
                    string s = Path.GetExtension(dlg.FileNames[0]);
                    if (s != null)
                    {
                        var firstExtension = s.ToLower();

                        XbimModel fedModel = null;
                        if (firstExtension == ".xbimf")
                        {
                            if (dlg.FileNames.Length > 1)
                            {
                                var res = MessageBox.Show("Multiple files selected, open " + dlg.FileNames[0] + "?", "Cannot open multiple Xbim files",
                                    MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                                if (res == MessageBoxResult.Cancel)
                                    return;
                            }
                            fedModel = new XbimModel();
                            fedModel.Open(dlg.FileNames[0], XbimDBAccess.ReadWrite);
                        }
                        else if (firstExtension == ".ifc")
                        {
                            //create temp file as a placeholder for the temperory xbim file
                            var filePath = Path.GetTempFileName();
                            filePath = Path.ChangeExtension(filePath, "xbimf");
                            fedModel = XbimModel.CreateModel(filePath);
                            fedModel.Initialise("Default Author", "Default Organization");
                            using (var txn = fedModel.BeginTransaction())
                            {
                                fedModel.IfcProject.Name = "Default Project Name";
                                txn.Commit();
                            }

                            var informUser = true;
                            for (var i = 0; i < dlg.FileNames.Length; i++)
                            {
                                var fileName = dlg.FileNames[i];
                                var builder = new XbimReferencedModelViewModel();
                                builder.Name = fileName;
                                builder.OrganisationName = "OrganisationName " + i;
                                builder.OrganisationRole = "Undefined";

                                var buildRes = false;
                                Exception exception = null;
                                try
                                {
                                    buildRes = builder.TryBuild(fedModel);
                                }
                                catch (Exception ex)
                                {
                                    //usually an EsentDatabaseSharingViolationException, user needs to close db first
                                    exception = ex;
                                }

                                if (!buildRes && informUser)
                                {
                                    var msg = exception == null ? "" : "\r\nMessage: " + exception.Message;
                                    var res = MessageBox.Show(fileName + " couldn't be opened." + msg + "\r\nShow this message again?",
                                        "Failed to open a file", MessageBoxButton.YesNoCancel, MessageBoxImage.Error);
                                    if (res == MessageBoxResult.No)
                                        informUser = false;
                                    else if (res == MessageBoxResult.Cancel)
                                    {
                                        fedModel = null;
                                        break;
                                    }
                                }
                            }
                        }
                        if (fedModel != null)
                        {
                            CloseAndDeleteTemporaryFiles();
                            ModelProvider.ObjectInstance = fedModel;
                            ModelProvider.Refresh();

                        }
                    }
                }
            }
        }
        private void FederationFromDialogbox(OpenFileDialog dlg)
        {
            if (!dlg.FileNames.Any())
                return;
            //use the first filename it's extension to decide which action should happen
            var s = Path.GetExtension(dlg.FileNames[0]);
            if (s == null)
                return;
            var firstExtension = s.ToLower();

            XbimModel fedModel = null;
            switch (firstExtension)
            {
                case ".xbimf":
                    if (dlg.FileNames.Length > 1)
                    {
                        var res = MessageBox.Show("Multiple files selected, open " + dlg.FileNames[0] + "?",
                            "Cannot open multiple Xbim files",
                            MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                        if (res == MessageBoxResult.Cancel)
                            return;
                    }
                    fedModel = new XbimModel();
                    fedModel.Open(dlg.FileNames[0], XbimDBAccess.ReadWrite);
                    break;
                case ".ifc":
                case ".ifczip":
                case ".ifcxml":
                    //create temp file as a placeholder for the temperory xbim file
                    var filePath = Path.GetTempFileName();
                    filePath = Path.ChangeExtension(filePath, "xbimf");
                    fedModel = XbimModel.CreateModel(filePath);
                    fedModel.Initialise("Default Author", "Default Organization");
                    using (var txn = fedModel.BeginTransaction())
                    {
                        fedModel.IfcProject.Name = "Default Project Name";
                        txn.Commit();
                    }

                    var informUser = true;
                    for (var i = 0; i < dlg.FileNames.Length; i++)
                    {
                        var fileName = dlg.FileNames[i];
                        var builder = new XbimReferencedModelViewModel
                        {
                            Name = fileName,
                            OrganisationName = "OrganisationName " + i,
                            OrganisationRole = "Undefined"
                        };

                        var buildRes = false;
                        Exception exception = null;
                        try
                        {
                            buildRes = builder.TryBuild(fedModel);
                        }
                        catch (Exception ex)
                        {
                            //usually an EsentDatabaseSharingViolationException, user needs to close db first
                            exception = ex;
                        }

                        if (buildRes || !informUser)
                            continue;
                        var msg = exception == null ? "" : "\r\nMessage: " + exception.Message;
                        var res = MessageBox.Show(fileName + " couldn't be opened." + msg + "\r\nShow this message again?",
                            "Failed to open a file", MessageBoxButton.YesNoCancel, MessageBoxImage.Error);
                        if (res == MessageBoxResult.No)
                            informUser = false;
                        else if (res == MessageBoxResult.Cancel)
                        {
                            fedModel = null;
                            break;
                        }
                    }
                    break;
            }
            if (fedModel == null)
                return;
            CloseAndDeleteTemporaryFiles();
            ModelProvider.ObjectInstance = fedModel;
            ModelProvider.Refresh();
        }