SolidWorks Macros
The quickest and easiest way to start programming with the SolidWorks
API is to record a SolidWorks macro, which contains the SolidWorks API
calls that correspond to the actions performed in the user interface.
You can modify the macro in Microsoft Visual Basic for Applications (VBA)
or Microsoft Visual Studio Tools for Applications (VSTA) to fit your work
site's needs.
Microsoft VBA
is a toolset based on Microsoft Visual Basic for Applications (VBA) and
is embedded in the SolidWorks software. Microsoft VBA lets you record,
run, and edit Microsoft VBA macros in the SolidWorks software. Recorded
macros are saved as .swp files.
Microsoft VSTA
is a toolset based on Microsoft VB.NET and C# and is embedded in the SolidWorks
software. Microsoft VSTA lets you record, run, and edit VB.NET and C#
code in the SolidWorks software. Recorded Microsoft VSTA macros are saved
as either Microsoft VB.NET or C# projects. Building or debugging a Microsoft
VSTA macro creates an executable file that ends in the filename extension
.dll, which can be used by the
SolidWorks software in the same manner as a .swp
file.
NOTES:
-
VSTA Requires Microsoft .NET
Framework 3.5.
- When debugging Microsoft VSTA macros with user-interface
components such as PropertyManager pages, manipulators, or other objects
that use events or handler objects, the debugger must continue to run
after the main() method of the VSTA macro exits. Either deselect the user-interface
system option Stop VSTA debugger on macro
exit (located on the Tools >
Options >
System
Options dialog) or set swUserPreferenceToggle_e.swStopDebuggingVstaOnExit
to false to keep the debugger running after the main() method of the Microsoft
VSTA macro exits.
Typically you would follow these steps to create a SolidWorks API application
using SolidWorks macros:
Plan the user-interface actions before recording
them.
Record
the user-interface actions.
NOTE: If you are not familiar with recording SolidWorks macros,
record the macro a couple of times to eliminate interactive changes to
the view such as changing the view orientation, zooming in or out, panning,
rotating the model, and so on.
Edit
the macro to delete extra lines of code and explicitly declare and early
bind variables.
Run the
macro to test it.
Debug
the macro and test it again.
If the application has a user interface, create
it in Microsoft VBA or Microsoft VSTA and then modify the macro to work
with the user interface, run the modified macro, and debug it.
When done recording, testing, and debugging the macro, you can assign
the macro to button.