コード例 #1
0
        private static void RegisterDocument(Document document)
        {
            // If the document has an existing GUID saved to ExtensibleStorage we retrieve this, otherwise we register it with a new GUID
            string documentGuid = DocumentGuidUtils.GetDocumentGuidFromExtensibleStorage(document) ??
                                  DocumentGuidUtils.RegisterDocumentGuidToExtensibleStorage(document);

            // Creates an accessible, stable reference to the Revit document
            DocumentCacheService.AddDocument(documentGuid, document);

            // Setting a static, accessible reference to the Revit application just this once, for everything to use
            if (RevitApplication == null)
            {
                RevitApplication = document.Application;
            }

            // Getting all of the saved rules in the document
            ObservableCollection <RegexRule> existingRegexRules = ExtensibleStorageUtils.GetAllRegexRulesInExtensibleStorage(documentGuid);

            // If there are no saved rules we return, otherwise we establish the updaters
            if (existingRegexRules == null || existingRegexRules.Count < 1)
            {
                return;
            }

            // If we found rules, load them into the rules cache
            foreach (RegexRule existingRegexRule in existingRegexRules)
            {
                RegexRuleCacheService.AddRule(documentGuid, existingRegexRule);
            }
        }
コード例 #2
0
        private static void DeRegisterDocument(Document document)
        {
            string documentGuid = DocumentGuidUtils.GetDocumentGuidFromExtensibleStorage(document) ?? DocumentGuidUtils.RegisterDocumentGuidToExtensibleStorage(document);

            // If there are any saved rules in the application-wide cache, we can remove them
            RegexRuleCacheService.ClearDocumentRules(documentGuid);

            DocumentCacheService.RemoveDocument(documentGuid);
        }
コード例 #3
0
 public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
 {
     try
     {
         if (RegularApp.DialogShowing)
         {
             return(Result.Cancelled);
         }
         string          documentGuid    = DocumentGuidUtils.GetDocumentGuidFromExtensibleStorage(commandData.Application.ActiveUIDocument.Document);
         RuleManagerView ruleManagerView = new RuleManagerView(documentGuid);
         ruleManagerView.ShowDialog();
         RegularApp.DialogShowing = false;
         return(Result.Succeeded);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
         return(Result.Failed);
     }
 }
コード例 #4
0
ファイル: ExportRules.cs プロジェクト: wojciechteclaw/Regular
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                Document document     = commandData.Application.ActiveUIDocument.Document;
                string   documentGuid = DocumentGuidUtils.GetDocumentGuidFromExtensibleStorage(document);

                List <RegexRule> regexRules = RegularApp.RegexRuleCacheService.GetDocumentRules(documentGuid).ToList();
                if (regexRules.Count < 1)
                {
                    new InfoWindowView
                    (
                        "Regular DataSpec",
                        "No Rules Found",
                        "No DataSpec rules were found in the document",
                        true
                    ).ShowDialog();
                    return(Result.Cancelled);
                }

                // Prompting the user to specify which rules are to be exported
                ObservableCollection <ObservableObject> observableObjects = new ObservableCollection <ObservableObject>();
                foreach (RegexRule regexRule in regexRules)
                {
                    observableObjects.Add(new ObservableObject(regexRule)
                    {
                        DisplayName = regexRule.RuleName,
                        IsChecked   = true,
                    });
                }

                SelectElementsInfo selectElementsInfo = new SelectElementsInfo
                {
                    Document          = document,
                    ObservableObjects = observableObjects,
                    UIDocument        = commandData.Application.ActiveUIDocument,
                    UserMessage       = "Select one or more DataSpec rules to export to the .json file format.",
                };

                SelectElementsView selectElementsView = new SelectElementsView(selectElementsInfo);
                selectElementsView.ShowDialog();
                if (selectElementsView.DialogResult != true)
                {
                    new InfoWindowView
                    (
                        "Regular DataSpec",
                        "Rule Export Was Cancelled",
                        "The user cancelled the export.",
                        true
                    ).ShowDialog();

                    return(Result.Cancelled);
                }

                List <RegexRule> selectedRegexRules = selectElementsView.SelectElementsViewModel.InputObservableObjects
                                                      .Where(x => x.IsChecked)
                                                      .Select(x => x.OriginalObject)
                                                      .OfType <RegexRule>()
                                                      .ToList();


                // Prompting the user to specify where the JSON file should be exported to
                string filePath = IOUtils.PromptUserToSelectDestination("DataSpec Rule Export", ".json");
                if (string.IsNullOrWhiteSpace(filePath))
                {
                    new InfoWindowView
                    (
                        "Regular DataSpec",
                        "Rule Export Was Cancelled",
                        "The user cancelled the export.",
                        true
                    ).ShowDialog();

                    return(Result.Cancelled);
                }

                // Writing JSON information to the selected location
                JSONFileUtils.WriteTextToJSON(selectedRegexRules, filePath);

                return(Result.Succeeded);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(Result.Failed);
            }
        }
コード例 #5
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                Document document     = commandData.Application.ActiveUIDocument.Document;
                string   documentGuid = DocumentGuidUtils.GetDocumentGuidFromExtensibleStorage(document);

                string filePath = IOUtils.PromptUserToSelectFile(".json");
                if (string.IsNullOrWhiteSpace(filePath))
                {
                    new InfoWindowView
                    (
                        "Regular DataSpec",
                        "Rule Export Was Cancelled",
                        "The given file path was invalid.",
                        true
                    ).ShowDialog();
                    return(Result.Cancelled);
                }

                string           serialisedRegexRules = JSONFileUtils.ReadJSONFromFile(filePath);
                List <RegexRule> regexRules           = SerializationUtils.DeserializeRegexRules(serialisedRegexRules);

                if (regexRules == null || regexRules.Count < 1)
                {
                    new InfoWindowView
                    (
                        "Regular DataSpec",
                        "No Rules Found",
                        "No DataSpec rules were found in the document",
                        true
                    ).ShowDialog();
                    return(Result.Cancelled);
                }

                // Prompting the user to specify which rules are to be exported
                ObservableCollection <ObservableObject> observableObjects = new ObservableCollection <ObservableObject>();
                foreach (RegexRule regexRule in regexRules)
                {
                    observableObjects.Add(new ObservableObject(regexRule)
                    {
                        DisplayName = regexRule.RuleName,
                        IsChecked   = true,
                    });
                }

                SelectElementsInfo selectElementsInfo = new SelectElementsInfo
                {
                    Document          = document,
                    ObservableObjects = observableObjects,
                    UIDocument        = commandData.Application.ActiveUIDocument,
                    UserMessage       = "Select one or more DataSpec rules to export to the .json file format.",
                };

                SelectElementsView selectElementsView = new SelectElementsView(selectElementsInfo);
                selectElementsView.ShowDialog();

                if (selectElementsView.DialogResult != true)
                {
                    new InfoWindowView
                    (
                        "Regular DataSpec",
                        "Rule Export Was Cancelled",
                        "The user cancelled the export.",
                        true
                    ).ShowDialog();
                    return(Result.Cancelled);
                }

                List <RegexRule> selectedRulesToImport = selectElementsView.SelectElementsViewModel.InputObservableObjects
                                                         .Where(x => x.IsChecked)
                                                         .Select(x => x.OriginalObject)
                                                         .OfType <RegexRule>()
                                                         .ToList();

                List <RegexRule> existingRegexRules = RegularApp.RegexRuleCacheService
                                                      .GetDocumentRules(documentGuid)
                                                      .ToList();

                List <string> existingRegexRuleNames = existingRegexRules
                                                       .Select(x => x.RuleName)
                                                       .ToList();

                List <string> conflictingRegexRuleGuids = selectedRulesToImport
                                                          .Where(x => existingRegexRuleNames.Contains(x.RuleName))
                                                          .Select(x => x.RuleGuid)
                                                          .ToList();

                OverrideMode overrideMode = OverrideMode.None;

                foreach (RegexRule selectedRegexRule in selectedRulesToImport)
                {
                    // Resetting validation score
                    selectedRegexRule.ValidationScore = "";

                    // If there's no conflict then we save and iterate
                    if (!conflictingRegexRuleGuids.Contains(selectedRegexRule.RuleGuid))
                    {
                        RegexRule.Save(documentGuid, selectedRegexRule);
                        continue;
                    }

                    RegexRule conflictingRegexRule = existingRegexRules.FirstOrDefault(x => x.RuleName == selectedRegexRule.RuleName);

                    // Otherwise we can go one of 3 ways
                    switch (overrideMode)
                    {
                    case OverrideMode.None:
                        // Only used for the initial conflicting rule to fall through to the dialog
                        break;

                    case OverrideMode.ReplaceAll:
                        if (conflictingRegexRule == null)
                        {
                            continue;
                        }
                        RegexRule.ReplaceRegexRule(documentGuid, conflictingRegexRule, selectedRegexRule);
                        continue;

                    case OverrideMode.RenameAll:
                        RegexRule.SaveRenamedRegexRule(documentGuid, selectedRegexRule);
                        continue;

                    case OverrideMode.SkipAll:
                        continue;

                    default:
                        continue;
                    }

                    ImportRuleInfo importRuleInfo = new ImportRuleInfo
                    {
                        DocumentGuid      = documentGuid,
                        ExistingRegexRule = conflictingRegexRule,
                        NewRegexRule      = selectedRegexRule
                    };

                    ImportRuleView importRuleView = new ImportRuleView(importRuleInfo);
                    importRuleView.ShowDialog();

                    if (importRuleView.DialogResult != true)
                    {
                        break;
                    }
                    overrideMode = importRuleView.ImportRuleViewModel.OverrideMode;
                }

                return(Result.Succeeded);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(Result.Failed);
            }
        }