Exemple #1
0
        public static void CreateUI(RibbonPanel ribbonPanel)
        {
            const string CommandName = "Rhino";
            string       CommandId   = $"CustomCtrl_%CustomCtrl_%Add-Ins%{ribbonPanel.Name}%{typeof(CommandRhinoInside).Name}";
            const string Shortcuts   = "R#Ctrl+R";

            var buttonData = NewPushButtonData <CommandRhinoInside, AllwaysAvailable>(CommandName);

            if (ribbonPanel.AddItem(buttonData) is PushButton pushButton)
            {
                Button = pushButton;

                if (Addin.RhinoVersionInfo == null)
                {
                    pushButton.SetContextualHelp(new ContextualHelp(ContextualHelpType.Url, "https://www.rhino3d.com/download/rhino/wip"));
                    pushButton.Image      = ImageBuilder.LoadBitmapImage("RhinoInside.Resources.Rhino-logo.png", true);
                    pushButton.LargeImage = ImageBuilder.LoadBitmapImage("RhinoInside.Resources.Rhino-logo.png");
                }
                else
                {
                    pushButton.SetContextualHelp(new ContextualHelp(ContextualHelpType.Url, "https://www.rhino3d.com/inside"));
                    using (var icon = System.Drawing.Icon.ExtractAssociatedIcon(Addin.RhinoExePath))
                    {
                        pushButton.Image      = icon.ToBitmapSource(true);
                        pushButton.LargeImage = icon.ToBitmapSource();
                    }

                    try
                    {
                        var versionInfo = Addin.RhinoVersionInfo;
                        pushButton.ToolTip         = $"Loads {versionInfo.ProductName} inside this Revit session";
                        pushButton.LongDescription = $"{versionInfo.FileDescription} ({versionInfo.ProductVersion})\n{versionInfo.LegalCopyright}";
                    }
                    catch (Exception) { }
                }

                // Register keyboard shortcut
                {
                    string keyboardShortcutsPath = Path.Combine(Revit.CurrentUsersDataFolderPath, "KeyboardShortcuts.xml");
                    if (!File.Exists(keyboardShortcutsPath))
                    {
                        keyboardShortcutsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Autodesk", $"RVT {Revit.ApplicationUI.ControlledApplication.VersionNumber}", "UserDataCache", "KeyboardShortcuts.xml");
                    }

                    if (!Revit.KeyboardShortcuts.LoadFrom(keyboardShortcutsPath, out var shortcuts))
                    {
                        Revit.KeyboardShortcuts.LoadFromResources($"RhinoInside.Resources.RVT{Revit.ApplicationUI.ControlledApplication.VersionNumber}.KeyboardShortcuts.xml", out shortcuts);
                    }

#if DEBUG
                    // Those lines generate the KeyboardShortcuts.xml template file when new Revit version is supported
                    string keyboardShortcutsTemplatePath = Path.Combine(Addin.SourceCodePath, "Resources", $"RVT{Revit.ApplicationUI.ControlledApplication.VersionNumber}", "KeyboardShortcuts.xml");
                    var    info = new FileInfo(keyboardShortcutsTemplatePath);
                    if (info.Length == 0)
                    {
                        var shortcutsSummary = new Revit.KeyboardShortcuts.Shortcuts();
                        foreach (var shortcut in shortcuts.OrderBy(x => x.CommandId))
                        {
                            if (!string.IsNullOrEmpty(shortcut.Shortcuts))
                            {
                                var shortcutDefinition = new Revit.KeyboardShortcuts.ShortcutItem
                                {
                                    CommandId = shortcut.CommandId,
                                    Shortcuts = shortcut.Shortcuts
                                };
                                shortcutsSummary.Add(shortcutDefinition);
                            }
                        }

                        Revit.KeyboardShortcuts.SaveAs(shortcutsSummary, keyboardShortcutsTemplatePath);
                    }
#endif

                    try
                    {
                        var shortcutItem = shortcuts.Where(x => x.CommandId == CommandId).First();
                        if (shortcutItem.Shortcuts == null)
                        {
                            shortcutItem.Shortcuts      = Shortcuts;
                            Rhinoceros.ModalScope.Exit += ModalScope_Exit;
                        }
                    }
                    catch (InvalidOperationException)
                    {
                        var shortcutItem = new Revit.KeyboardShortcuts.ShortcutItem()
                        {
                            CommandName = CommandName,
                            CommandId   = CommandId,
                            Shortcuts   = Shortcuts,
                            Paths       = $"Add-Ins>{ribbonPanel.Name}"
                        };
                        shortcuts.Add(shortcutItem);
                        Rhinoceros.ModalScope.Exit += ModalScope_Exit;
                    }

                    Revit.KeyboardShortcuts.SaveAs(shortcuts, Path.Combine(Revit.CurrentUsersDataFolderPath, "KeyboardShortcuts.xml"));
                }
            }
        }
Exemple #2
0
        public static void CreateUI(RibbonPanel ribbonPanel)
        {
            const string CommandName = "Rhino";
            string       CommandId   = $"CustomCtrl_%CustomCtrl_%Add-Ins%{ribbonPanel.Name}%{typeof(CommandRhinoInside).Name}";
            const string Shortcuts   = "R#Ctrl+R";

            var buttonData = NewPushButtonData <CommandRhinoInside, AllwaysAvailable>(CommandName);

            if (ribbonPanel.AddItem(buttonData) is PushButton pushButton)
            {
                Button = pushButton;

                if (Addin.RhinoVersionInfo == null)
                {
                    pushButton.SetContextualHelp(new ContextualHelp(ContextualHelpType.Url, "https://www.rhino3d.com/download/rhino/wip"));
                    pushButton.Image      = ImageBuilder.LoadBitmapImage("RhinoInside.Resources.Rhino-logo.png", true);
                    pushButton.LargeImage = ImageBuilder.LoadBitmapImage("RhinoInside.Resources.Rhino-logo.png");
                }
                else
                {
                    pushButton.SetContextualHelp(new ContextualHelp(ContextualHelpType.Url, "https://www.rhino3d.com/inside"));
                    using (var icon = System.Drawing.Icon.ExtractAssociatedIcon(Addin.RhinoExePath))
                    {
                        pushButton.Image      = icon.ToBitmapSource(true);
                        pushButton.LargeImage = icon.ToBitmapSource();
                    }

                    try
                    {
                        var versionInfo = Addin.RhinoVersionInfo;
                        pushButton.ToolTip         = $"Loads {versionInfo.ProductName} inside this Revit session";
                        pushButton.LongDescription = $"{versionInfo.FileDescription} ({versionInfo.ProductVersion})\n{versionInfo.LegalCopyright}";
                    }
                    catch (Exception) { }
                }

                // Register keyboard shortcut
                {
                    string keyboardShortcutsPath = Path.Combine(Revit.CurrentUsersDataFolderPath, "KeyboardShortcuts.xml");

                    if (!Revit.KeyboardShortcuts.LoadFrom(keyboardShortcutsPath, out var shortcuts))
                    {
                        shortcuts = new Revit.KeyboardShortcuts.Shortcuts();
                    }

                    try
                    {
                        var shortcutItem = shortcuts.Where(x => x.CommandId == CommandId).First();
                        if (shortcutItem.Shortcuts == null)
                        {
                            shortcutItem.Shortcuts      = Shortcuts;
                            Rhinoceros.ModalScope.Exit += ModalScope_Exit;
                        }
                    }
                    catch (InvalidOperationException)
                    {
                        var shortcutItem = new Revit.KeyboardShortcuts.ShortcutItem()
                        {
                            CommandName = CommandName,
                            CommandId   = CommandId,
                            Shortcuts   = Shortcuts,
                            Paths       = $"Add-Ins>{ribbonPanel.Name}"
                        };
                        shortcuts.Add(shortcutItem);
                        Rhinoceros.ModalScope.Exit += ModalScope_Exit;
                    }

                    Revit.KeyboardShortcuts.SaveAs(shortcuts, keyboardShortcutsPath);
                }
            }
        }