Ejemplo n.º 1
0
        internal SwVstaMacro(ISwApplication app, string path) : base(app.Sw, path)
        {
            m_App = app;

            Version     = null; //TODO: identify version of VSTA macro
            EntryPoints = new MacroEntryPoint[] { new MacroEntryPoint("", "Main") };
        }
Ejemplo n.º 2
0
        public override void Run(MacroEntryPoint entryPoint, MacroRunOptions_e opts)
        {
            var stopDebugVstaFlag = m_SwApp.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swStopDebuggingVstaOnExit);

            bool?isVsta3 = null;

            if (Version.HasValue)
            {
                if (Version == VstaMacroVersion_e.Vsta1 && m_App.Version.Major >= Enums.SwVersion_e.Sw2021)
                {
                    throw new NotSupportedException("VSTA1 is not supported in Sw2021 or newer");
                }

                isVsta3 = m_SwApp.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swEnableVSTAVersion3);
                m_SwApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swEnableVSTAVersion3, Version == VstaMacroVersion_e.Vsta3);
            }

            m_SwApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swStopDebuggingVstaOnExit, opts == MacroRunOptions_e.UnloadAfterRun);

            try
            {
                base.Run(entryPoint, MacroRunOptions_e.Default);
            }
            finally
            {
                m_SwApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swStopDebuggingVstaOnExit, stopDebugVstaFlag);

                if (isVsta3.HasValue)
                {
                    m_SwApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swEnableVSTAVersion3, isVsta3.Value);
                }
            }
        }
Ejemplo n.º 3
0
        public override void Run(MacroEntryPoint entryPoint, MacroRunOptions_e opts)
        {
            var stopDebugVstaFlag = m_App.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swStopDebuggingVstaOnExit);

            bool?isVsta3 = null;

            if (Version.HasValue)
            {
                isVsta3 = m_App.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swEnableVSTAVersion3);
                m_App.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swEnableVSTAVersion3, Version == VstaMacroVersion_e.Vsta3);
            }

            m_App.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swStopDebuggingVstaOnExit, opts == MacroRunOptions_e.UnloadAfterRun);

            try
            {
                base.Run(entryPoint, MacroRunOptions_e.Default);
            }
            finally
            {
                m_App.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swStopDebuggingVstaOnExit, stopDebugVstaFlag);

                if (isVsta3.HasValue)
                {
                    m_App.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swEnableVSTAVersion3, isVsta3.Value);
                }
            }
        }
Ejemplo n.º 4
0
 protected virtual void ValidateMacro(MacroEntryPoint entryPoint)
 {
     if (!File.Exists(Path))
     {
         throw new MacroFileNotFoundException(Path);
     }
 }
Ejemplo n.º 5
0
 public override void Run(MacroEntryPoint entryPoint, MacroRunOptions_e options)
 {
     if (EntryPoints.Contains(entryPoint, new MacroEntryPointEqualityComparer()))
     {
         base.Run(entryPoint, options);
     }
     else
     {
         throw new Exception($"Entry point '{entryPoint}' is not available in the macro '{m_Path}'");
     }
 }
Ejemplo n.º 6
0
        public void RunMacro(IXApplication app, string macroPath, MacroEntryPoint entryPoint,
                             MacroRunOptions_e opts, string args, IXDocument doc)
        {
            try
            {
                if (entryPoint == null)
                {
                    var macro = app.OpenMacro(macroPath);
                    entryPoint = macro.EntryPoints.First();
                }

                if (!string.IsNullOrEmpty(args) || doc != null)
                {
                    if (m_Runner != null)
                    {
                        var param = new MacroParameter();

                        if (doc != null)
                        {
                            param.Set("Model", GetDocumentDispatch(doc));
                        }
                        if (!string.IsNullOrEmpty(args))
                        {
                            param.Set("Args", ParseCommandLine(args));
                        }

                        var res = m_Runner.Run(GetAppDispatch(app),
                                               macroPath, entryPoint.ModuleName,
                                               entryPoint.ProcedureName, (int)opts, param, true);

                        if (!res.Result)
                        {
                            throw new MacroRunnerResultError(res.Message);
                        }
                    }
                    else
                    {
                        throw new UserException("Macro runner is not installed. Cannot run the macro with arguments");
                    }
                }
                else
                {
                    var macro = app.OpenMacro(macroPath);
                    macro.Run(entryPoint, opts);
                }
            }
            catch (MacroUserInterruptException) //do not consider this as an error
            {
            }
            catch (MacroRunnerResultError resEx)
            {
                throw new MacroRunFailedException(macroPath, -1, resEx.Message);
            }
        }
Ejemplo n.º 7
0
        public void RunMacro(string macroPath, MacroEntryPoint entryPoint, bool unloadAfterRun)
        {
            int err;

            var opts = unloadAfterRun ? swRunMacroOption_e.swRunMacroUnloadAfterRun : swRunMacroOption_e.swRunMacroDefault;

            if (!m_App.RunMacro2(macroPath, entryPoint.ModuleName, entryPoint.SubName,
                                 (int)opts, out err))
            {
                throw new MacroRunFailedException((swRunMacroError_e)err);
            }
        }
Ejemplo n.º 8
0
        protected override void ValidateMacro(MacroEntryPoint entryPoint)
        {
            base.ValidateMacro(entryPoint);

            if (EntryPoints == null)
            {
                throw new MacroHasNoEntryPointsException();
            }

            if (!EntryPoints.Contains(entryPoint, new MacroEntryPointEqualityComparer()))
            {
                throw new MacroEntryPointNotFoundException(m_Path, entryPoint);
            }
        }
Ejemplo n.º 9
0
        public override void Run(MacroEntryPoint entryPoint, MacroRunOptions_e opts)
        {
            var stopDebugVstaFlag = m_App.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swStopDebuggingVstaOnExit);

            m_App.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swStopDebuggingVstaOnExit, opts == MacroRunOptions_e.UnloadAfterRun);

            try
            {
                base.Run(entryPoint, MacroRunOptions_e.Default);
            }
            finally
            {
                m_App.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swStopDebuggingVstaOnExit, stopDebugVstaFlag);
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Run macro with specirfied entry point and default options
 /// </summary>
 /// <param name="macro">Macro to run</param>
 /// <param name="entryPoint">Entry point</param>
 public static void Run(this IXMacro macro, MacroEntryPoint entryPoint)
 {
     macro.Run(entryPoint, MacroRunOptions_e.Default);
 }
Ejemplo n.º 11
0
 internal MacroEntryPointNotFoundException(string macroPath, MacroEntryPoint entryPoint)
     : base($"Entry point '{entryPoint}' is not available in the macro '{macroPath}'")
 {
 }
Ejemplo n.º 12
0
        public virtual void Run(MacroEntryPoint entryPoint, MacroRunOptions_e opts)
        {
            swRunMacroOption_e swOpts = swRunMacroOption_e.swRunMacroDefault;

            switch (opts)
            {
            case MacroRunOptions_e.Default:
                swOpts = swRunMacroOption_e.swRunMacroDefault;
                break;

            case MacroRunOptions_e.UnloadAfterRun:
                swOpts = swRunMacroOption_e.swRunMacroUnloadAfterRun;
                break;
            }

            int err;

            if (!m_SwApp.RunMacro2(m_Path, entryPoint.ModuleName, entryPoint.ProcedureName, (int)swOpts, out err))
            {
                string errDesc;

                switch ((swRunMacroError_e)err)
                {
                case swRunMacroError_e.swRunMacroError_InvalidArg:
                    errDesc = "Invalid argument";
                    break;

                case swRunMacroError_e.swRunMacroError_MacrosAreDisabled:
                    errDesc = "Macros are disabled";
                    break;

                case swRunMacroError_e.swRunMacroError_NotInDesignMode:
                    errDesc = "Not in design mode";
                    break;

                case swRunMacroError_e.swRunMacroError_OnlyCodeModules:
                    errDesc = "Only code modules";
                    break;

                case swRunMacroError_e.swRunMacroError_OutOfMemory:
                    errDesc = "Out of memory";
                    break;

                case swRunMacroError_e.swRunMacroError_InvalidProcname:
                    errDesc = "Invalid procedure name";
                    break;

                case swRunMacroError_e.swRunMacroError_InvalidPropertyType:
                    errDesc = "Invalid property type";
                    break;

                case swRunMacroError_e.swRunMacroError_SuborfuncExpected:
                    errDesc = "Sub or function expected";
                    break;

                case swRunMacroError_e.swRunMacroError_BadParmCount:
                    errDesc = "Bad parameter count";
                    break;

                case swRunMacroError_e.swRunMacroError_BadVarType:
                    errDesc = "Bad variable type";
                    break;

                case swRunMacroError_e.swRunMacroError_UserInterrupt:
                    throw new MacroUserInterruptException(m_Path, err);

                case swRunMacroError_e.swRunMacroError_Exception:
                    errDesc = "Exception";
                    break;

                case swRunMacroError_e.swRunMacroError_Overflow:
                    errDesc = "Overflow";
                    break;

                case swRunMacroError_e.swRunMacroError_TypeMismatch:
                    errDesc = "Type mismatch";
                    break;

                case swRunMacroError_e.swRunMacroError_ParmNotOptional:
                    errDesc = "Parameter not optional";
                    break;

                case swRunMacroError_e.swRunMacroError_UnknownLcid:
                    errDesc = "Unknown LCID";
                    break;

                case swRunMacroError_e.swRunMacroError_Busy:
                    errDesc = "Busy";
                    break;

                case swRunMacroError_e.swRunMacroError_ConnectionTerminated:
                    errDesc = "Connection terminated";
                    break;

                case swRunMacroError_e.swRunMacroError_CallRejected:
                    errDesc = "Call rejected";
                    break;

                case swRunMacroError_e.swRunMacroError_CallFailed:
                    errDesc = "Call failed";
                    break;

                case swRunMacroError_e.swRunMacroError_Zombied:
                    errDesc = "Zombied";
                    break;

                case swRunMacroError_e.swRunMacroError_Invalidindex:
                    errDesc = "Invalid index";
                    break;

                case swRunMacroError_e.swRunMacroError_NoPermission:
                    errDesc = "No permission";
                    break;

                case swRunMacroError_e.swRunMacroError_Reverted:
                    errDesc = "Reverted";
                    break;

                case swRunMacroError_e.swRunMacroError_TooManyOpenFiles:
                    errDesc = "Too many open files";
                    break;

                case swRunMacroError_e.swRunMacroError_DiskError:
                    errDesc = "Disk error";
                    break;

                case swRunMacroError_e.swRunMacroError_CantSave:
                    errDesc = "Cannot save";
                    break;

                case swRunMacroError_e.swRunMacroError_OpenFileFailed:
                    errDesc = "Open file failed";
                    break;

                default:
                    throw new UnknownMacroRunFailedException(m_Path);
                }

                throw new MacroRunFailedException(m_Path, err, errDesc);
            }
        }
Ejemplo n.º 13
0
 internal SwVstaMacro(ISldWorks app, string path) : base(app, path)
 {
     Version     = null; //TODO: identify version of VSTA macro
     EntryPoints = new MacroEntryPoint[] { new MacroEntryPoint("", "Main") };
 }
Ejemplo n.º 14
0
 internal SwVstaMacro(ISldWorks app, string path) : base(app, path)
 {
     EntryPoints = new MacroEntryPoint[] { new MacroEntryPoint("", "Main") };
 }
Ejemplo n.º 15
0
        public void RunMacro(IXApplication app, string macroPath, MacroEntryPoint entryPoint,
                             MacroRunOptions_e opts, string args, IXDocument doc)
        {
            try
            {
                var argsArr = !string.IsNullOrEmpty(args) ? CommandLineUtils.ParseCommandLine(args) : null;

                var xCadMacro = GetXCadMacroIfExists(macroPath);

                if (xCadMacro == null)
                {
                    if (entryPoint == null)
                    {
                        var macro = app.OpenMacro(macroPath);

                        if (macro.EntryPoints != null)
                        {
                            entryPoint = macro.EntryPoints.First();
                        }
                        else
                        {
                            throw new MacroRunFailedException(macro.Path, -1, "Failed to extract entry point");
                        }
                    }

                    if (!string.IsNullOrEmpty(args) || doc != null)
                    {
                        if (m_Runner != null)
                        {
                            var param = new MacroParameter();

                            if (doc != null)
                            {
                                param.Set("Model", GetDocumentDispatch(doc));
                            }
                            if (!string.IsNullOrEmpty(args))
                            {
                                param.Set("Args", argsArr);
                            }

                            var res = m_Runner.Run(GetAppDispatch(app),
                                                   macroPath, entryPoint.ModuleName,
                                                   entryPoint.ProcedureName, (int)opts, param, true);

                            if (!res.Result)
                            {
                                throw new MacroRunnerResultError(res.Message);
                            }
                        }
                        else
                        {
                            throw new UserException("Macro runner is not installed. Cannot run the macro with arguments");
                        }
                    }
                    else
                    {
                        var macro = app.OpenMacro(macroPath);
                        macro.Run(entryPoint, opts);
                    }
                }
                else
                {
                    var macroDoc = doc ?? app.Documents.Active;

                    if (macroDoc is IXUnknownDocument)
                    {
                        macroDoc = ((IXUnknownDocument)macroDoc).GetSpecific();
                    }

                    xCadMacro.Run(app, macroDoc, m_Logger, argsArr);
                }
            }
            catch (MacroUserInterruptException) //do not consider this as an error
            {
            }
            catch (MacroRunnerResultError resEx)
            {
                throw new MacroRunFailedException(macroPath, -1, resEx.Message);
            }
        }
Ejemplo n.º 16
0
        public virtual void Run(MacroEntryPoint entryPoint, MacroRunOptions_e opts)
        {
            ValidateMacro(entryPoint);

            ExecuteMacro(entryPoint, opts);
        }