Passing the parameters to SOLIDWORKS Macro using the SWBasic macro

Edit ArticleEdit Article
Comments

SWBasic (*.swb) macros is a legacy type of macros still supported in SOLIDWORKS applications.

One of the benefits of this type of macro is that it is saved in the plain text. This enables 3rd party application to create macros on the fly. In particular this technique can be employed to emulate the passing of parameters to the SOLIDWORKS macro.

For example, the following template macro can be created

template.swb

Dim swApp As SldWorks.SldWorks

Sub main()
        
    Set swApp = Application.SldWorks
        
     swApp.SendMsgToUser "Specified argument: {{Argument1}}"
    
End Sub

where {{Argument1}} is a placeholder to the argument value to be filled by external application or script:

static void Main(string[] args)
{
    var macroPath = args[0];
    var param = args[1];
    
    var templateMacro = File.ReadAllText(macroPath);
    var macro = templateMacro.Replace("{{Argument1}}", param);

    var tempMacroPath = Path.Combine(Path.GetTempPath(), Path.GetFileName(macroPath));
    File.WriteAllText(tempMacroPath, macro);

The resulting file can be run as a normal SOLIDWORKS macro

Notifications

All articles and code at CodeStack are now open-source and hosted on GitHub. If you want to contribute by modifying existing articles and code snippets, submitting new ones, reporting errors and bugs etc. please follow this blog post for more information. We appreciate any contribution.


Product of Xarial Product of Xarial