Passing the parameters to SOLIDWORKS Macro using the SWBasic macro

Edit ArticleEdit Article

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


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

Product of Xarial Product of Xarial