15 Replies Latest reply on Sep 9, 2017 11:52 AM by M. Doga Dogan

    "Please wait" message until macro finishes executing

    M. Doga Dogan

      Hello,

       

      I have an add-in for SolidWorks. It runs a macro by calling RunMacro2. I would like to display a "Please Wait" message and/or a progress bar while the macro is executed.

      Based on the discussion here, I think I can start an EXE that has a progress bar, and close it when the macro is finished. Is there an easier way to do something like this?

       

      Also, how can I know if the macro has completely executed?

       

      Thank you,

      Doga

        • Re: "Please wait" message until macro finishes executing
          Andrew Johnson

          I don't believe VBA has the ability to display a working progress form and continue execution. It would require a separate thread, hence the reason for launching a separate application. Have you considered using the SolidWorks Progress Bar? I know it's not right in the middle of the screen, but you can set the title and update its value. See the attached example macro.

          • Re: "Please wait" message until macro finishes executing
            Christian Chu

            I haven't tried it but I think you can use a userform as a message box and kill it soon it'd done

            • Re: "Please wait" message until macro finishes executing
              Ivana Kolin

              what is your add-in  C#, VB.net?

              • Re: "Please wait" message until macro finishes executing
                Amen Allah Jlili

                Here's how to do in an asynchronous fashion (it won't block your UI) in C#. You can convert this to VB.NET too.

                 

                using System.Threading.Tasks;
                using SolidWorks.Interop.swconst;
                using SolidWorks.Interop.sldworks;
                namespace ExecuteMacroAsync.Console
                {
                    class Program
                    {
                        static async void Main(string[] args)
                        {
                            // todo: get solidworks session
                            SldWorks swApp = default(SldWorks);
                
                
                        // run macro async
                         var ret =  await swApp.RunMacroAsync("FileName", "ModuleName", "Procedure", 0);
                
                
                        // do something with ret 
                
                
                        if (!ret.Value)
                            {
                                // something went wrong
                                // check error and dotexception
                
                
                            }
                            else
                            {
                                // macro executed
                                // move on
                            }
                        }
                    } 
                
                
                    public static class Extension {
                
                
                        /// <summary>
                        /// Run macro asynchrously - return a RunMacroReturn object
                        /// </summary>
                        /// <param name="swApp"></param>
                        /// <param name="FilePathName">File path</param>
                        /// <param name="ModuleName">Module name</param>
                        /// <param name="ProcedureName">procedure name</param>
                        /// <param name="Options">Options</param>
                        /// <returns>RunMacroReturn</returns>
                        public static Task<RunMacroReturn> RunMacroAsync(this SldWorks swApp, string FilePathName,
                              string ModuleName,
                              string ProcedureName,
                              int Options)
                        {
                            return Task<RunMacroReturn>.Run(() => 
                            {
                                // var defs
                
                
                                int Error = default(int);
                                var ret = default(RunMacroReturn);
                
                
                                try
                                {
                                    var RunMacro2Ret = swApp.RunMacro2(FilePathName, ModuleName, ProcedureName, Options, out Error);
                                }
                                catch (Exception e)
                                {
                                    ret.DotNetException = e;
                                    ret.Error = default(swRunMacroError_e);
                                    ret.Value = false;
                                    return ret; 
                                }
                                             
                                // get error enum
                                ret.Error = (swRunMacroError_e)Enum.ToObject(typeof(swRunMacroError_e), Error);
                
                
                                // return macro result
                                return ret;
                
                
                            }); 
                
                
                
                
                        }
                    }
                
                
                
                public class RunMacroReturn
                    {
                        public bool Value { get; set; }
                        public swRunMacroError_e Error { get; set; }
                        public Exception DotNetException { get; set; }
                        public RunMacroReturn()
                        {
                
                
                        }
                    }
                }