Example 1 - The Basics
This simple example demonstrates how a user can write a macro that, when executed, will generate a Maxwell scene file (mxs) from the current open SolidWorks model and launch mxcl to generate the rendered image.
It covers all the necessary basic operations required for developing a macro using the Maxwell API and covers:
- Adding References to the Visual Basic editor environment
- Code Initialisation
- Calling Maxwell API functions
References
To reference the Maxwell API functions, you will need to ensure that they are enabled in the Visual Basic Editor (which is launched when you create a new macro in SolidWorks).

Use the Editor command "Tools...References" to verify that the Maxwell Type Library is included in the list of available references.

Did you know?The Type Library is built into the Maxwell Addin swMaxwell2006.dll
Code Initialisation
Writing an application using the Maxwell API typically involves:
- Instantiating a SolidWorks connection.
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks
- Loading the Maxwell DLL, if it is not already loaded, and/or getting the Maxwell object.
Dim strDllFileName As String
strDllPathName = “c:\MyPlugins\swMaxwell2006.dll”
‘ Get the Maxwell add-in
Dim maxwellAddin As swMaxwell2006.CswMaxwell2006
Set maxwellAddin = swApp.GetAddInObject(“swMaxwell2006.swMaxwell2006”)
‘ Check to see if the add-in is already loaded
Dim lStatus As Long
If maxwellAddin Is Nothing Then
‘ Load the DLL
lStatus = swApp.LoadAddIn(strDllFileName)
If lStatus <> 0 Then
‘ DLL was not loaded
Select Case lStatus
Case -1:
Debug.Print ” Not loaded: unknown error.”
Case 1:
Debug.Print ” Not loaded: not an error.”
Case 2:
Debug.Print ” Not loaded: already loaded.”
End Select
Else
‘ DLL was loaded
EndIf
Calling Maxwell API functions
Simply a case of invoking the method on the Maxwell object.
Here is an example:
Dim res As Long
res = maxwellAddin.ExportToMxs(“test.mxs”, “test.jpg”, True)
Complete Example
A complete macro is included here. This has been simplified to assume the Maxwell Addin has already been loaded and that there is a current, open, SolidWorks model.
OptionExplicit
Dim swApp As SldWorks.SldWorks
Sub main()
Dim swObject As Object
Dim maxwellAddin As swMaxwell2006.CswMaxwell2006
Dim res As Long
Dim strCLSID As String
strCLSID = “swMaxwell2006.swMaxwell2006″
Set swApp = Application.SldWorks
Set swObject = swApp.GetAddInObject(strCLSID)
If (Not (swObject Is Nothing)) Then
Set maxwellAddin = swObject
If (Not (maxwellAddin Is Nothing)) Then
res = maxwellAddin.ExportToMxs(“test.mxs”, “test.jpg”, True)
End If
End If
End Sub