![]() |
||||||||||||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||||||||||||
|
One of the best ways to get started with custom programming using the SolidWorks API is by recording a macro in SolidWorks. When a macro is recorded, it creates a file containing program code. The contents of the macro file contains a record of all the commands that took place from the time you started recording to the time when you stopped recording. Old macro files had an .swb file extension and were text files. Starting with SolidWorks 2001, macros are saved with an .swp extension and are Visual Basic for Applications (VBA) project files that cannot be edited with a simple text editor. The following program code is an example of a macro that will open a part file from a specific folder (in this case, a file named gasket.sldprt from the spinkler in the SolidWorks folder).
Dim swApp As Object
Dim Part As Object Dim Gtol As Object Dim FeatureData As Object Dim Feature As Object Dim Component As Object Sub main() Set swApp = CreateObject ("SldWorks.Application") Set Part = swApp.OpenDoc ("C:\Program files\SolidWorks\samples\examples\Sprinkler\Gasket.SLDPRT", 1) Set Part = swApp.ActivateDoc ("Gasket") swApp.ActiveDoc.ActiveView.FrameWidth = 921 swApp.ActiveDoc.ActiveView.FrameHeight = 560 End Sub As you can see, the API commands that opened the part were recorded (swAPP.OpenDoc). Recording a macro for the function you want to accomplish in your program is the very first step you should take. The macro already has written the program code out for you so all you have to do is copy and paste it into your program. This means that you don't have to go search the API Documentation for the code yourself. The SolidWorks API Documentation that comes with your copy of SolidWorks is also very useful. It contains all of the API commands for SolidWorks and examples of program code that you can use in your own programs. Again, if its already been written, don't try and write it yourself. Another resource on the SW API is the SolidWorks API FAQs on the SolidWorks Homepage. The API FAQs has a wealth of information organized by categories and search function, too. The URL is http://www.solidworks.com/html/Products/api. There is also a file named apihelp.chm found in the SolidWorks/lang directory, which describes all of the API functions. Tutorials
Here are some other links to Visual Basic tutorials to get you started.
API Sample Code
During the installation of SolidWorks, there was an option for installing some sample files. If you installed these, you will have some Visual Basic examples in a directory named samples/visualbasic in your SolidWorks directory. If you are a SolidWorks Subscription Customer, you can access the Model Library. There are plenty of very good examples that use the SolidWorks API written in both Visual Basic and C++ as well. | |||||||||||||||||||||||||||||||||||||
© 2008 Cadimensions Inc. | ||||||||||||||||||||||||||||||||||||||