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
SolidworksApi macros
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.
I haven't tried it but I think you can use a userform as a message box and kill it soon it'd done
@Andrew: This look great but I wish it was in the middle of the screen. Not exactly what I wanted
Thank you for the idea. This was also suggested in this thread.
I think Luke Malpass's most recent answer in that thread worked fine, but I didn't completely get it. Do you guys understand where the actual code is supposed to be in that macro? It's as follows:
Sub main()
UserForm1.Show (False)
MyCounter = 1
Do Until MyCounter = 10000000
Dim a
UserForm1.Label1.Caption = MyCounter
DoEvents
a = 5 * MyCounter
MyCounter = MyCounter + 1
Loop
UserForm1.Hide
End Sub
Your code would go between UserForm.Show and UserForm.Hide. The "Do Until" loop is just there representing the code that takes time to execute. He is updating the label in the form inside the loop to report progress to the user. This would be optional. The form could just have a static label that reads Please Wait or something similar.
If this works then my statement in my earlier reply was incorrect. I didn't realize Form.Show had that optional parameter to make it non-modal. I don't use VBA much.
I don't really need to have a progress bar, but of course it would look nicer with one. But I don't know exactly how long the process will take. What should be the value of MyCounter?
Furthermore, in this case, should the actual code look as follows?
Sub main()
UserForm1.Show (False)
MyCounter = 1
Do Until MyCounter = 10000000
Dim a
UserForm1.Label1.Caption = MyCounter
DoEvents
a = 5 * MyCounter
MyCounter = MyCounter + 1
Loop
CODE GOES HERE
UserForm1.Hide
End Sub
Or should it go before the Do Until loop?
It didn't work this way. The user form shows up, the number increases but the actual process doesn't start until the counter reaches its final value. How can I make sure the actual macro is executed in the background as the counter works?
Thanks.
Your code needs to replace that Do Until loop, its only there as a representation of long running code. The counter update needs to be within your code. If your code is a loop, you could increment the counter there. If not, there is probably not a way to show progress numerically. You could update the label with text stating what the code is currently doing.
Reading custom properties...
Processing properties...
Creating feature xyz...
If you're able to share code I can get more specific.
what is your add-in C#, VB.net?
@Ivana Good question, it's in VB.NET.
Oh, I see! Now I get it, thanks
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.
<p>using System.Threading.Tasks;</p><p>using SolidWorks.Interop.swconst;</p><p>using SolidWorks.Interop.sldworks;</p><p>namespace ExecuteMacroAsync.Console</p><p>{</p><p> class Program</p><p> {</p><p> static async void Main(string[] args)</p><p> {</p><p> // todo: get solidworks session</p><p> SldWorks swApp = default(SldWorks);</p><p></p><p></p><p> // run macro async</p><p> var ret = await swApp.RunMacroAsync("FileName", "ModuleName", "Procedure", 0);</p><p></p><p></p><p> // do something with ret </p><p></p><p></p><p> if (!ret.Value)</p><p> {</p><p> // something went wrong</p><p> // check error and dotexception</p><p></p><p></p><p> }</p><p> else</p><p> {</p><p> // macro executed</p><p> // move on</p><p> }</p><p> }</p><p> } </p><p></p><p></p><p> public static class Extension {</p><p></p><p></p><p> /// <summary></p><p> /// Run macro asynchrously - return a RunMacroReturn object</p><p> /// </summary></p><p> /// <param name="swApp"></param></p><p> /// <param name="FilePathName">File path</param></p><p> /// <param name="ModuleName">Module name</param></p><p> /// <param name="ProcedureName">procedure name</param></p><p> /// <param name="Options">Options</param></p><p> /// <returns>RunMacroReturn</returns></p><p> public static Task<RunMacroReturn> RunMacroAsync(this SldWorks swApp, string FilePathName,</p><p> string ModuleName,</p><p> string ProcedureName,</p><p> int Options)</p><p> {</p><p> return Task<RunMacroReturn>.Run(() => </p><p> {</p><p> // var defs</p><p></p><p></p><p> int Error = default(int);</p><p> var ret = default(RunMacroReturn);</p><p></p><p></p><p> try</p><p> {</p><p> var RunMacro2Ret = swApp.RunMacro2(FilePathName, ModuleName, ProcedureName, Options, out Error);</p><p> }</p><p> catch (Exception e)</p><p> {</p><p> ret.DotNetException = e;</p><p> ret.Error = default(swRunMacroError_e);</p><p> ret.Value = false;</p><p> return ret; </p><p> }</p><p> </p><p> // get error enum</p><p> ret.Error = (swRunMacroError_e)Enum.ToObject(typeof(swRunMacroError_e), Error);</p><p></p><p></p><p> // return macro result</p><p> return ret;</p><p></p><p></p><p> }); </p><p></p><p></p><p></p><p></p><p> }</p><p> }</p><p></p><p></p><p></p><p>public class RunMacroReturn</p><p> {</p><p> public bool Value { get; set; }</p><p> public swRunMacroError_e Error { get; set; }</p><p> public Exception DotNetException { get; set; }</p><p> public RunMacroReturn()</p><p> {</p><p></p><p></p><p> }</p><p> }</p><p>}</p>Very nice.
You can study the code. I have not tested it though but it should work. You can convert this to VB.NET through one of the online converters.
Cheers,
Amen
CADHERO consulting SUARL – CAD Design Automation & Training – SOLIDWORKS API
A further question: Do you know how I can do this in an add-in, rather than a macro? I never used a UserForm in a VB.NET add-in itself, so I was wondering how to do it.