Ejemplo n.º 1
0
 public static int Execute(EventHook eventHook, object eventSender, object eventArgs)
 {
     return(ScriptExecutor.ExecuteScript(
                scriptData: new ScriptData {
         ScriptPath = eventHook.Script,
         ConfigScriptPath = eventHook.Script,
         CommandUniqueId = eventHook.UniqueId,
         CommandName = string.Format("hooks.{0}", Path.GetFileNameWithoutExtension(eventHook.Script)),
         CommandBundle = string.Format("{0}.hooks", eventHook.ExtensionName),
         CommandExtension = eventHook.ExtensionName,
         HelpSource = "",
     },
                scriptRuntimeCfg: new ScriptRuntimeConfigs {
         CommandData = null,
         SelectedElements = new ElementSet(),
         SearchPaths = new List <string>(eventHook.SearchPaths),
         Arguments = new List <string>(),
         EventSender = eventSender,
         EventArgs = eventArgs,
         EngineConfigs = " { \"full_frame\" : true } ",
         RefreshEngine = false,
         ConfigMode = false,
         DebugMode = false,
         ExecutedFromUI = false
     },
                scriptExecConfigs: new ScriptExecutorConfigs {
         SendTelemetry = false
     }
                ));
 }
Ejemplo n.º 2
0
        public void Execute(UIApplication uiApp)
        {
            if (ScriptData != null && ScriptRuntimeConfigs != null)
            {
                // provide the given uiapp
                ScriptRuntimeConfigs.UIApp = uiApp;

                // request execution and set results
                Result = ScriptExecutor.ExecuteScript(ScriptData, ScriptRuntimeConfigs);
            }
        }
Ejemplo n.º 3
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // 1: ----------------------------------------------------------------------------------------------------
            #region Processing modifier keys
            // Processing modifier keys

            bool refreshEngine    = false;
            bool configScriptMode = false;
            bool forcedDebugMode  = false;

            bool ALT   = Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt);
            bool SHIFT = Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift);
            bool CTRL  = Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);
            bool WIN   = Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin);


            // If Ctrl+Alt+Shift clicking on the tool run in clean engine
            if (CTRL && ALT && SHIFT)
            {
                refreshEngine = true;
            }

            // If Alt+Shift clicking on button, open the context menu with options.
            else if (SHIFT && WIN)
            {
                // start creating context menu
                ContextMenu pyRevitCmdContextMenu = new ContextMenu();

                // menu item to open help url if exists
                if (ScriptData.HelpSource != null && ScriptData.HelpSource != "")
                {
                    MenuItem openHelpSource = new MenuItem();
                    openHelpSource.Header  = "Open Help";
                    openHelpSource.ToolTip = ScriptData.HelpSource;
                    openHelpSource.Click  += delegate { System.Diagnostics.Process.Start(ScriptData.HelpSource); };
                    pyRevitCmdContextMenu.Items.Add(openHelpSource);
                }

                // menu item to copy script path to clipboard
                MenuItem copyScriptPath = new MenuItem();
                copyScriptPath.Header  = "Copy Script Path";
                copyScriptPath.ToolTip = ScriptData.ScriptPath;
                copyScriptPath.Click  += delegate { System.Windows.Forms.Clipboard.SetText(ScriptData.ScriptPath); };
                pyRevitCmdContextMenu.Items.Add(copyScriptPath);

                // menu item to copy config script path to clipboard, if exists
                if (ScriptData.ConfigScriptPath != null && ScriptData.ConfigScriptPath != "")
                {
                    MenuItem copyAltScriptPath = new MenuItem();
                    copyAltScriptPath.Header  = "Copy Config Script Path";
                    copyAltScriptPath.ToolTip = ScriptData.ConfigScriptPath;
                    copyAltScriptPath.Click  += delegate { System.Windows.Forms.Clipboard.SetText(ScriptData.ConfigScriptPath); };
                    pyRevitCmdContextMenu.Items.Add(copyAltScriptPath);
                }

                // menu item to copy bundle path to clipboard
                var      bundlePath     = Path.GetDirectoryName(ScriptData.ScriptPath);
                MenuItem copyBundlePath = new MenuItem();
                copyBundlePath.Header  = "Copy Bundle Path";
                copyBundlePath.ToolTip = bundlePath;
                copyBundlePath.Click  += delegate { System.Windows.Forms.Clipboard.SetText(bundlePath); };
                pyRevitCmdContextMenu.Items.Add(copyBundlePath);

                // menu item to copy command unique name (assigned by pyRevit) to clipboard
                MenuItem copyUniqueName = new MenuItem();
                copyUniqueName.Header  = "Copy Unique Id";
                copyUniqueName.ToolTip = ScriptData.CommandUniqueId;
                copyUniqueName.Click  += delegate { System.Windows.Forms.Clipboard.SetText(ScriptData.CommandUniqueId); };
                pyRevitCmdContextMenu.Items.Add(copyUniqueName);

                // menu item to copy command unique name (assigned by pyRevit) to clipboard
                MenuItem copyControlIdName = new MenuItem();
                copyControlIdName.Header  = "Copy Control Id";
                copyControlIdName.ToolTip = ScriptData.CommandControlId;
                copyControlIdName.Click  += delegate { System.Windows.Forms.Clipboard.SetText(ScriptData.CommandControlId); };
                pyRevitCmdContextMenu.Items.Add(copyControlIdName);

                // menu item to copy ;-separated sys paths to clipboard
                // Example: "path1;path2;path3"
                MenuItem copySysPaths = new MenuItem();
                string   sysPathsText = string.Join(Environment.NewLine, ScriptRuntimeConfigs.SearchPaths);
                copySysPaths.Header  = "Copy Sys Paths";
                copySysPaths.ToolTip = sysPathsText;
                copySysPaths.Click  += delegate { System.Windows.Forms.Clipboard.SetText(sysPathsText); };
                pyRevitCmdContextMenu.Items.Add(copySysPaths);

                // menu item to copy ;-separated arguments to clipboard
                // Example: "path1;path2;path3"
                MenuItem copyArguments = new MenuItem();
                string   argumentsText = string.Join(Environment.NewLine, ScriptRuntimeConfigs.Arguments);
                copyArguments.Header  = "Copy Arguments";
                copyArguments.ToolTip = argumentsText;
                copyArguments.Click  += delegate { System.Windows.Forms.Clipboard.SetText(argumentsText); };
                pyRevitCmdContextMenu.Items.Add(copyArguments);
                if (argumentsText == null || argumentsText == string.Empty)
                {
                    copyArguments.IsEnabled = false;
                }

                // menu item to copy engine configs
                MenuItem copyEngineConfigs = new MenuItem();
                string   engineCfgs        = ScriptRuntimeConfigs.EngineConfigs;
                copyEngineConfigs.Header  = "Copy Engine Configs";
                copyEngineConfigs.ToolTip = engineCfgs;
                copyEngineConfigs.Click  += delegate { System.Windows.Forms.Clipboard.SetText(engineCfgs); };
                pyRevitCmdContextMenu.Items.Add(copyEngineConfigs);
                if (engineCfgs == null || engineCfgs == string.Empty)
                {
                    copyEngineConfigs.IsEnabled = false;
                }

                // menu item to copy help url
                MenuItem copyHelpSource = new MenuItem();
                copyHelpSource.Header  = "Copy Help Url";
                copyHelpSource.ToolTip = ScriptData.HelpSource;
                copyHelpSource.Click  += delegate { System.Windows.Forms.Clipboard.SetText(ScriptData.HelpSource); };
                pyRevitCmdContextMenu.Items.Add(copyHelpSource);
                if (ScriptData.HelpSource == null || ScriptData.HelpSource == string.Empty)
                {
                    copyHelpSource.IsEnabled = false;
                }

                // open the menu
                pyRevitCmdContextMenu.IsOpen = true;

                return(Result.Succeeded);
            }

            // If Ctrl+Shift clicking on button, run the script in debug mode and run config script instead.
            else if (CTRL && (SHIFT || ExecConfigs.UseConfigScript))
            {
                configScriptMode = true;
                forcedDebugMode  = true;
            }

            // If Alt clicking on button, open the script in explorer and return.
            else if (SHIFT && ALT)
            {
                // combine the arguments together
                // it doesn't matter if there is a space after ','
                if (ScriptExecutor.EnsureTargetScript(ScriptData.ConfigScriptPath))
                {
                    string argument = "/select, \"" + ScriptData.ConfigScriptPath + "\"";

                    System.Diagnostics.Process.Start("explorer.exe", argument);
                }
                return(Result.Succeeded);
            }

            else if (ALT)
            {
                // combine the arguments together
                // it doesn't matter if there is a space after ','
                if (ScriptExecutor.EnsureTargetScript(ScriptData.ScriptPath))
                {
                    string argument = "/select, \"" + ScriptData.ScriptPath + "\"";

                    System.Diagnostics.Process.Start("explorer.exe", argument);
                }
                return(Result.Succeeded);
            }

            // If Shift clicking on button, run config script instead
            else if (SHIFT || ExecConfigs.UseConfigScript)
            {
                configScriptMode = true;
            }

            // If Ctrl clicking on button, set forced debug mode.
            else if (CTRL)
            {
                forcedDebugMode = true;
            }
            #endregion

            // 2: ----------------------------------------------------------------------------------------------------
            #region Setup pyRevit Command Runtime Configs
            // fill in the rest of runtime info
            ScriptRuntimeConfigs.CommandData      = commandData;
            ScriptRuntimeConfigs.SelectedElements = elements;
            ScriptRuntimeConfigs.RefreshEngine    = refreshEngine;
            ScriptRuntimeConfigs.ConfigMode       = configScriptMode;
            ScriptRuntimeConfigs.DebugMode        = forcedDebugMode;
            ScriptRuntimeConfigs.ExecutedFromUI   = ExecConfigs.MimicExecFromUI;

            #endregion

            // 3: ----------------------------------------------------------------------------------------------------
            #region Execute and log results
            // Executing the script and logging the results
            // Get script executor and Execute the script
            var env    = new EnvDictionary();
            int result = ScriptExecutor.ExecuteScript(
                ScriptData,
                ScriptRuntimeConfigs,
                new ScriptExecutorConfigs {
                SendTelemetry = env.TelemetryState
            }
                );

            // Return results to Revit. Don't report errors since we don't want Revit popup with error results
            if (result == 0)
            {
                return(Result.Succeeded);
            }
            else
            {
                return(Result.Cancelled);
            }
            #endregion
        }