public static bool CreateUI(ScriptPkg pkg, RibbonHandler ribbon)
        {
            // --------------------------------------------------------------------
            // FIND SCRIPTS
            // --------------------------------------------------------------------
            var items = pkg.FindLinkedItems();

            // --------------------------------------------------------------------
            // CREATE ASSEMBLY
            // --------------------------------------------------------------------
            // generate assembly containing script command types
            var lsa = new LinkedScriptAssembly();

            // create types for all the scripts in the structure
            ProcessLinkedScripts(items, (script) =>
            {
                script.ScriptCommandType = lsa.MakeScriptCommandType(script);
            });

            // save and load the created assembly
            lsa.SaveAndLoad();

            // --------------------------------------------------------------------
            // CREATE UI
            // --------------------------------------------------------------------
            RibbonPanel panel;

            try { panel = ribbon.CreateAddinPanel(pkg.Name); }
            catch { return(false); }

            // Currently only supporting two levels in the UI:
            // 1) Pushbuttons on panel for every LinkedScript at the root level
            // 2) Pulldowns containing pushbuttons for all the LinkedScripts recursively found under their directory
            // Lets make the pulldowns first so they are first on the panel
            items.OfType <LinkedItemGroup>().ToList().ForEach((group) =>
            {
                var pullDownData = new PulldownButtonData(group.Name, group.Name)
                {
                    Image      = ImageBuilder.LoadRibbonButtonImage("Ribbon.Grasshopper.GhFolder.png", true),
                    LargeImage = ImageBuilder.LoadRibbonButtonImage("Ribbon.Grasshopper.GhFolder.png"),
                    ToolTip    = group.Tooltip,
                };
                if (panel.AddItem(pullDownData) is PulldownButton pulldown)
                {
                    ProcessLinkedScripts(group.Items, (script) =>
                    {
                        AddPullDownButton(pulldown, script, lsa);
                    });
                }
            });
            // now make pushbuttons
            items.OfType <LinkedScript>().ToList().ForEach((script) =>
            {
                AddPanelButton(panel, script, lsa);
            });

            return(true);
        }
Esempio n. 2
0
        private static void UpdateScriptPkgUI(RibbonHandler ribbon)
        {
            // determine which packages need to be loaded
            var curState = new HashSet <ScriptPkg>();

            if (AddinOptions.Current.LoadInstalledScriptPackages)
            {
                curState.UnionWith(CommandGrasshopperPackageManager.GetInstalledScriptPackages());
            }
            if (AddinOptions.Current.LoadUserScriptPackages)
            {
                curState.UnionWith(ScriptPkg.GetUserScriptPackages());
            }

            // create a combined set of both last and current states to iterate over
            var pkgs = new HashSet <ScriptPkg>(curState);

            pkgs.UnionWith(_lastState);

            // update the package ui
            foreach (var pkg in pkgs)
            {
                // skip existing packages
                if (curState.Contains(pkg) && _lastState.Contains(pkg))
                {
                    continue;
                }

                // create new packages
                else if (curState.Contains(pkg) && !_lastState.Contains(pkg))
                {
                    if (LinkedScripts.HasUI(pkg, ribbon))
                    {
                        TaskDialog.Show(
                            Addin.AddinName,
                            $"Package \"{pkg.Name}\" has been previously loaded in to the Revit UI." +
                            "Restart Revit for changes to take effect."
                            );
                    }
                    else
                    {
                        LinkedScripts.CreateUI(pkg, ribbon);
                    }
                }

                // or remove, removed packages
                else if (!curState.Contains(pkg) && _lastState.Contains(pkg))
                {
                    if (LinkedScripts.HasUI(pkg, ribbon))
                    {
                        LinkedScripts.RemoveUI(pkg, ribbon);
                    }
                }
            }

            _lastState = curState;
        }
 public static bool RemoveUI(ScriptPkg pkg, RibbonHandler ribbon)
 {
     return(ribbon.RemoveAddinPanel(pkg.Name));
 }
 public static bool HasUI(ScriptPkg pkg, RibbonHandler ribbon)
 {
     return(ribbon.HasAddinPanel(pkg.Name));
 }