Solidworks API Programming, Part 1:
Automatically generated VBA source code analysis
Let's start Solidworks API Programming with macro source code analysis. I just explain what I have done in person. Although I have not read the book, "Automating SOLIDWORKS 2006 using MACROS (http://www.amazon.com/Automating-SOLIDWORKS-2006-using-MACROS/dp/1585032638)", I think it could be a good reference for serious users. If you know better or best book for Solidworks API Programming, please leave the comment.
1. Macro Recording: Tools - Macro - Record
This is the common function of MS-based programs which support VBA just like Excel. This function record the user action itself converting it into VBA source code.
1) Just click Tools - Macro - Record
2) Do what you want to do within solidworks like sketching and extruding
3) After finishing your work, just click Stop
4) Save window pops up. Write the filename and save it.
2. Macro Editing: Tools - Macro - Edit...
Let's try to see if code is saved propertly and it is what you want to do.
1) Click File - New
2) Click Tools - Macro - Record
3) Sketch a rectangle and extrude it to make a plate
4) Click Tools - Macro - Edit...
5) You can see VBA source as the following picture.
However this code is too specific. With this code, you can just make exactly the same feature. If you make it general, you need to add the general algorithm to this code. Let's analyze the source code
3. Source code analysis
' ****************************************************************************** ' C:\DOCUME~1\user\LOCALS~1\Temp\swx2016\Macro1.swb ' - macro recorded on 07/04/07 by user ' ****************************************************************************** Dim swApp As Object Dim Part As Object Dim SelMgr As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim Feature As Object Sub main() Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc Set SelMgr = Part.SelectionManager boolstatus = Part.Extension.SelectByID2("Front", "PLANE", 0, 0, 0, False, 0, Nothing, 0) Part.SketchManager.InsertSketch True Part.ClearSelection2 True Part.SketchRectangle 0, 0, 0, 0.1280011254206, 0.09855665428066, 0, 1 Part.ClearSelection2 True Part.SketchManager.InsertSketch True Part.ShowNamedView2 "*Trimetric", 8 Part.ClearSelection2 True boolstatus = Part.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, _ False, 0, Nothing, 0) Part.FeatureManager.FeatureExtrusion2 True, False, False, 0, 0, 0.01, _ 0.01, False, False, False, False, _ 0.01745329251994, 0.01745329251994, _ False, False, False, False, 1, 1, 1, 0, 0, False Part.SelectionManager.EnableContourSelection = 0 End Sub
swApp is Solidworks Object, Part is Part Object and Feature is Feature Object. All three variables are Object type which is most general of all object classes, kind of first ancestor. Type-checking is not strict in VBA but, it is in VB.NET. You should consider it carefully if you want to make the program using VB.NET. I try to keep track of VBA source code with brief comments.
swApp takes solidworks runtime object to access its inner functions.
Part takes open part object of swApp.
SelMgr takes selection manager object of Part.
Front plane is selected.
Sketch manager insert skech
Rectangle is drawn.
Sketch manager finish skech
Sketch is selected
Feature manager make the rectangle extruded with a solid feature.
Next time, I will post more complex VBA API Programming example
Comments
contact to me :weixieming@qq.com
Thank you~