CAD+ Toolset | Utilities for SOLIDWORKS automation
SOLIDWORKS utilities for improving your productivity in SOLIDWORKS. Automate properties, geometry, export and much more

Try Future Version Download Buy

Accessing CAD+ Toolset Macro+ framework form the VBA Macro

In order to access Macro+ from the VBA macro, add the reference to Xarial.CadPlus.MacroPlus.tlb (Macro+ COM API for CAD+ Toolset for SOLIDWORKS) type library from the installation folder of CAD+ Toolset.


In order to connect to the macro operation use the code below

Dim operMgr As IMacroOperationManager
Set operMgr = CreateObject("CadPlus.MacroOperationManager")

Dim macroOper As IMacroOperation
Set macroOper = operMgr.PopOperation(swApp)

Accessing IModelDoc2

In order to access the model currently processed by the macro use the code below

Dim swModel As SldWorks.ModelDoc2
Set swModel = macroOper.Model

This is a preferred approach for reading the pointer to the IModelDoc2 instead of ISldWorks::ActiveDoc, because depending on the settings the model may be in the invisible mode

Accessing Arguments

In order to access arguments and the resolved values use the code below

Dim i As Integer

For i = 0 To UBound(macroOper.Arguments)

    Dim arg As IMacroArgument
    Set arg = macroOper.Arguments(i)
    Dim argVal As String
    argVal = arg.GetValue()

Registering Custom Variables

It is also possible to register custom variables in the Expression Box Control that can be resolved within the macro.

Create a class library that will resolve the custom variable

Option Explicit

Implements IMacroCustomVariableValueProvider

Function IMacroCustomVariableValueProvider_Provide(ByVal varName As String, ByVal args As Variant, ByVal context As Variant) As Variant

    Select Case varName
        Case "myVar":
            IMacroCustomVariableValueProvider_Provide = "My Variable Value"
        Case Else
            Err.Raise vbError, "", "Not supported variable: " & varName
    End Select

End Function

Supply the instance of the value provider when resolving the expression

Dim customVarValProv As IMacroCustomVariableValueProvider
Set customVarValProv = New MyCustomVariableValueProvider

Dim arg As IMacroArgument
Set arg = macroOper.Arguments(0)

'Any value to be passed to the IMacroCustomVariableValueProvider (e.g. pointer to IBody2)
Dim context As Variant
context = "MyContext"

Dim argVal As String
argVal = arg.GetValue(customVarValProv, context)

If macro produces the files (e.g. exports) it is recommended to register the special user result (result files). This will allow application to properly render the results and prepare the input and set the individual result for each produced file.

Registering Results

Use the code below to register the result file and set the individual results

Dim resFilePaths(1) As String
resFilePaths(0) = "D:\file1.stp"
resFilePaths(1) = "D:\file2.stp"

Dim vResFiles As Variant
vResFiles = macroOper.SetResultFiles(resFilePaths)

For i = 0 To UBound(vResFiles)
    Dim resFile As IMacroOperationResultFile
    Set resFile = vResFiles(i)

    'TODO: process the result file (e.g. export)

    resFile.Status = MacroOperationResultFileStatus_e_Succeeded


It is also possible to report the status and progress messages to the application via various ways

'Report operation issue
macroOper.ReportIssue "Something went wrong", MacroIssueType_e_Error

'Adds the trace message
macroOper.Trace "Starting the process", TraceMessageSeverity_e_Information

'Adds the log message into the log window
macroOper.Log "Outputting the file"

'Sets the user result to be displayed in the report grid
macroOper.SetResult 50

Custom Errors

All runtime errors will be handled by Macro+ framework and displayed in the report.

VBA runtime error
VBA runtime error

Use Err.Raise to raise a user error

Err.Raise vbError, "", "Custom error from the VBA macro"

Custom VBA macro error
Custom VBA macro error


Attaching To Debug Session

In order to attach to the running session of Macro+ (e.g. when macro is running from Batch+ or Toolbar+) add the following line to the macro and execution will break allowing to debug the macro.

Debug.Assert False

Mocking Macro Operation

When connecting to Macro+ framework it is required to have a running session of macro executor (e.g. macro is running via Batch+ or Toolbar+). This makes debugging harder as it is required to run the executor for each change.

To resolve this issue, Macro+ allows mocking of the IMacroOperation by providing the custom arguments

Dim swCadPlusFact As Object
Set swCadPlusFact = CreateObject("CadPlusFactory.Sw")

Dim swCadPlus As Object

Set swCadPlus = swCadPlusFact.Create(swApp, False)

Dim args(1) As String
args(0) = "Output\{ path [FileNameWithoutExtension] }-{ myVar }.step"
args(1) = "Output\{ path [FileNameWithoutExtension] }-{ myVar }.igs"

Dim macroOper As IMacroOperation
Set macroOper = swCadPlus.CreateMacroOperation(swApp.ActiveDoc, "", args)

Product of Xarial Product of Xarial