/// <summary>
        /// Overload this method to implement an external command within Revit.
        /// </summary>
        /// <returns>
        /// The result indicates if the execution fails, succeeds, or was canceled by user. If it does not
        /// succeed, Revit will undo any changes made by the external command. 
        /// </returns>
        /// <param name="commandData">An ExternalCommandData object which contains reference to Application and View
        /// needed by external command.</param><param name="message">Error message can be returned by external command. This will be displayed only if the command status
        /// was "Failed".  There is a limit of 1023 characters for this message; strings longer than this will be truncated.</param><param name="elements">Element set indicating problem elements to display in the failure dialog.  This will be used
        /// only if the command status was "Failed".</param>
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // FIXME: somehow fetch back message after script execution...
            var executor = new ScriptExecutor(GetConfig(), commandData, message, elements);

            string source;
            using (var reader = File.OpenText(_scriptSource))
            {
                source = reader.ReadToEnd();
            }

            var result = executor.ExecuteScript(source, _scriptSource);
            message = executor.Message;
            switch (result)
            {
                case (int)Result.Succeeded:
                    return Result.Succeeded;
                case (int)Result.Cancelled:
                    return Result.Cancelled;
                case (int)Result.Failed:
                    return Result.Failed;
                default:
                    return Result.Succeeded;
            }
        }
        /// <summary>
        /// Overload this method to implement an external command within Revit.
        /// </summary>
        /// <returns>
        /// The result indicates if the execution fails, succeeds, or was canceled by user. If it does not
        /// succeed, Revit will undo any changes made by the external command.
        /// </returns>
        /// <param name="message">Error message can be returned by external command. This will be displayed only if the command status
        /// was "Failed".  There is a limit of 1023 characters for this message; strings longer than this will be truncated.</param><param name="elements">Element set indicating problem elements to display in the failure dialog.  This will be used
        /// only if the command status was "Failed".</param>
        public int Execute(params string[] parameters)
        {
            string message = "";
            // FIXME: somehow fetch back message after script execution...
            var executor = new ScriptExecutor(GetConfig(), message);

            string source;

            using (var reader = File.OpenText(_scriptSource))
            {
                source = reader.ReadToEnd();
            }

            var result = executor.ExecuteScript(source, _scriptSource);

            message = executor.Message;
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Execute the startup script (specified under /RpsAddin/StartupScript/@src)
        /// </summary>
        /// <param name="uiControlledApplication"></param>
        private void ExecuteStartupScript(UIControlledApplication uiControlledApplication, XDocument addinXml, Assembly addinAssembly)
        {
            // we need a UIApplication object to assign as `__revit__` in python...
            var fi            = uiControlledApplication.GetType().GetField("m_application", BindingFlags.NonPublic | BindingFlags.Instance);
            var uiApplication = (UIApplication)fi.GetValue(uiControlledApplication);
            // execute StartupScript
            var startupScript = GetStartupScript(addinXml, addinAssembly);

            if (startupScript != null)
            {
                var executor = new ScriptExecutor(GetConfig(), uiApplication, uiControlledApplication);
                var result   = executor.ExecuteScript(startupScript);
                if (result == (int)Result.Failed)
                {
                    // FIXME: make the TaskDialog show the addins name.
                    TaskDialog.Show("RevitPythonShell - StartupScript", executor.Message);
                }
            }
        }
        /// <summary>
        /// Execute the startup script (specified under /RpsAddin/StartupScript/@src)
        /// </summary>
        /// <param name="uiControlledApplication"></param>
        private void ExecuteStartupScript(UIControlledApplication uiControlledApplication, XDocument addinXml, Assembly addinAssembly)
        {
            // we need a UIApplication object to assign as `__revit__` in python...
            var versionNumber = uiControlledApplication.ControlledApplication.VersionNumber;
            var fieldName     = int.Parse(versionNumber) >= 2017 ? "m_uiapplication" : "m_application";
            var fi            = uiControlledApplication.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
            var uiApplication = (UIApplication)fi.GetValue(uiControlledApplication);
            // execute StartupScript
            var scriptName    = GetStartupScriptName(addinXml);
            var startupScript = GetEmbeddedScript(scriptName, addinAssembly);

            if (startupScript != null)
            {
                var executor = new ScriptExecutor(GetConfig(), uiApplication, uiControlledApplication);
                var result   = executor.ExecuteScript(startupScript, Path.Combine(addinAssembly.Location, scriptName));
                if (result == (int)Result.Failed)
                {
                    // FIXME: make the TaskDialog show the addins name.
                    TaskDialog.Show("RevitPythonShell - StartupScript", executor.Message);
                }
            }
        }
        /// <summary>
        /// Find the script in the resources and run it.
        /// </summary>
        Result IExternalCommand.Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
        {
            var executor = new ScriptExecutor(GetConfig(), commandData, message, elements);

            var className = this.GetType().Name;            // e.g. "ec_helloworld"
            var scriptName = className.Substring(3) + ".py"; // e.g. "helloworld.py
            var assembly = this.GetType().Assembly;

            var source = new StreamReader(assembly.GetManifestResourceStream(scriptName)).ReadToEnd();

            var result = executor.ExecuteScript(source, Path.Combine(assembly.Location, scriptName));
            message = executor.Message;
            switch (result)
            {
                case (int)Result.Succeeded:
                    return Result.Succeeded;
                case (int)Result.Cancelled:
                    return Result.Cancelled;
                case (int)Result.Failed:
                    return Result.Failed;
                default:
                    return Result.Succeeded;
            }
        }
 /// <summary>
 /// Execute the startup script (specified under /RpsAddin/StartupScript/@src)
 /// </summary>
 /// <param name="uiControlledApplication"></param>
 private void ExecuteStartupScript(UIControlledApplication uiControlledApplication, XDocument addinXml, Assembly addinAssembly)
 {
     // we need a UIApplication object to assign as `__revit__` in python...
     var fi = uiControlledApplication.GetType().GetField("m_application", BindingFlags.NonPublic | BindingFlags.Instance);
     var uiApplication = (UIApplication)fi.GetValue(uiControlledApplication);
     // execute StartupScript
     var scriptName = GetStartupScriptName(addinXml);
     var startupScript = GetEmbeddedScript(scriptName, addinAssembly);
     if (startupScript != null)
     {
         var executor = new ScriptExecutor(GetConfig(), uiApplication, uiControlledApplication);
         var result = executor.ExecuteScript(startupScript, Path.Combine(addinAssembly.Location, scriptName));
         if (result == (int)Result.Failed)
         {
             // FIXME: make the TaskDialog show the addins name.
             TaskDialog.Show("RevitPythonShell - StartupScript", executor.Message);
         }
     }
 }