Automating Your Repetitive Task
Automating Your Repetitive Task
Hi there, are you doing the same repeating process everyday with SOLIDWORKS? Ever thought of writing your own API macro to automate your design process and make your life easier?
“Oh no, API macro seems to be far-fetched.”
“I can’t do this.”
“This requires programming knowledge.”
Above is the typical response when normal people would react to the situation.
Today, I’m going to show a simple way to write your own API macro. So, let’s get started.
1. First, let us enable our “Macro Toolbar” first. Navigate to Tools > Customize > Toolbars > Macro.
2. The Macro Toolbar will pop out upon the configuration is done.
3. Then, we will start by clicking “Record/Pause Macro”. This will record every action/command you execute in SOLIDWORKS.
4. Just proceed by creating a cube 100mmx100mmx100mm.
Here’s how I did it.
i) Sketch a rectangle on Top Plane by using center rectangle.
ii) Smart dimension the edges to 100mm x 100mm.
iii) Make an extrusion of 100 mm.
5. Click the button again to stop the recording and save the macro (.swp).
6. Click the Edit Macro.
7. Browse to the location and open the .swp file.
8. Here’s how it looked.
It might look complicated now, but not to worry.
Not all of the commands recorded are useful to us.
Let’s take a closer look.
Red Font: Command such as navigation, zoom in/out, orientation and etc.
These lines can be removed.
Blue Font: Description and my comments.
Declaration
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Programming main body
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
‘ Pan
Dim swModelView As Object
Set swModelView = Part.ActiveView
swModelView.TranslateBy -5.44138211382114E-02, 1.40422764227642E-02
Select Top plane
boolstatus = Part.Extension.SelectByID2(“Top”, “PLANE”, 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2(“Top”, “PLANE”, 0, 0, 0, False, 0, Nothing, 0)
Create new sketch
Part.SketchManager.InsertSketch True
Part.ClearSelection2 True
Rectangle method
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstToRectEntity, swUserPreferenceOption_e.swDetailingNoOptionSpecified, True)
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType, swUserPreferenceOption_e.swDetailingNoOptionSpecified, True)
Dim vSkLines As Variant
vSkLines = Part.SketchManager.CreateCenterRectangle(0, 0, 0, 6.57860557231552E-02, 3.72371080139373E-02, 0)
Change the this line to:
vSkLines = Part.SketchManager.CreateCenterRectangle(0, 0, 0, 0.05, 0.05, 0)
Part.SketchManager.CreateCenterRectangle(X1, Y1, Z1, X2, Y2, Z2)
X1: X coordinate for point 1
Y1: Y coordinate for point 1
Z1: Z coordinate for point 1
X2: X coordinate for point 2
Y2: Y coordinate for point 2
Z2: Z coordinate for point 2
Note: Unit is in meter. 50mm = 0.05m
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2(“Line3”, “SKETCHSEGMENT”, 0.045725660833492, 0, -3.74878629500581E-02, False, 0, Nothing, 0)
Smart dimensioning, these lines can be removed.
Dim myDisplayDim As Object
Set myDisplayDim = Part.AddDimension2(3.46924436441772E-02, 0, -4.97748548199768E-02)
Part.ClearSelection2 True
Dim myDimension As Object
Set myDimension = Part.Parameter(“D1@Sketch1”)
myDimension.SystemValue = 0.1
boolstatus = Part.Extension.SelectByID2(“Line4”, “SKETCHSEGMENT”, 4.92376653886166E-02, 0, -1.59137350126284E-02, False, 0, Nothing, 0)
Set myDisplayDim = Part.AddDimension2(6.86771979788932E-02, 0, -1.36267311784782E-02)
Part.ClearSelection2 True
Set myDimension = Part.Parameter(“D2@Sketch1”)
myDimension.SystemValue = 0.1
Orienting the view
‘ Named View
Part.ShowNamedView2 “*Trimetric”, 8
Part.ViewZoomtofit2
Extrusion
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.1, 0.01, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False)
Part.FeatureManager.FeatureExtrusion2(Sd, Flip, Dir, T1, T2, D1, D2, Dchk1, Dchk2, Ddir1, Ddir2, Dang1, Dang2, OffsetReverse1, OffsetReverse2, TranslateSurface1, TranslateSurface2, Merge, UseFeatScope, UseAutoSelect, T0, StartOffset, FlipStartOffset)
We don’t have to change anything in here. Just be noted that D1 is your extrusion depth.
Part.SelectionManager.EnableContourSelection = False
‘ Zoom In/Out (MouseWheel)
Set swModelView = Part.ActiveView
swModelView.Scale2 = 1.23042274534665
Dim swTranslation() As Double
ReDimswTranslation(0 To 2) As Double
swTranslation(0) = 0.015763782810772
swTranslation(1) = -2.03275121193373E-04
swTranslation(2) = 6.11078829631178E-04
Dim swTranslationVar As Variant
swTranslationVar = swTranslation
Dim swMathUtils As Object
Set swMathUtils = swApp.GetMathUtility()
Dim swTranslationVector As MathVector
Set swTranslationVector = swMathUtils.CreateVector((swTranslationVar))
swModelView.Translation3 = swTranslationVector
‘ Zoom In/Out (MouseWheel)
Set swModelView = Part.ActiveView
swModelView.Scale2 = 1.02535228778888
ReDimswTranslation(0 To 2) As Double
swTranslation(0) = 0.018235169376766
swTranslation(1) = -1.69395934327809E-04
swTranslation(2) = 5.09232358025981E-04
swTranslationVar = swTranslation
Set swMathUtils = swApp.GetMathUtility()
Set swTranslationVector = swMathUtils.CreateVector((swTranslationVar))
swModelView.Translation3 = swTranslationVector
‘ Zoom In/Out (MouseWheel)
Set swModelView = Part.ActiveView
swModelView.Scale2 = 0.854460239824065
ReDimswTranslation(0 To 2) As Double
swTranslation(0) = 2.02946581817611E-02
swTranslation(1) = -1.41163278606512E-04
swTranslation(2) = 4.24360298354985E-04
swTranslationVar = swTranslation
Set swMathUtils = swApp.GetMathUtility()
Set swTranslationVector = swMathUtils.CreateVector((swTranslationVar))
swModelView.Translation3 = swTranslationVector
Part.ClearSelection2 True
boolstatus = Part.Extension.SketchBoxSelect(“0.000000”, “0.000000”, “0.000000”, “0.000000”, “0.000000”, “0.000000”)
‘ Pan
Set swModelView = Part.ActiveView
swModelView.TranslateBy -1.62990708478513E-02, -2.68307781649245E-02
End Sub
It should look something like this in the end:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Dim swModelView As Object
Set swModelView = Part.ActiveView
boolstatus = Part.Extension.SelectByID2(“Top”, “PLANE”, 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch True
Part.ClearSelection2 True
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstToRectEntity, swUserPreferenceOption_e.swDetailingNoOptionSpecified, True)
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType, swUserPreferenceOption_e.swDetailingNoOptionSpecified, True)
Dim vSkLines As Variant
vSkLines = Part.SketchManager.CreateCenterRectangle(0, 0, 0, 0.05, 0.05, 0)
Part.ClearSelection2 True
Part.ShowNamedView2 “*Trimetric”, 8
Part.ViewZoomtofit2
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.1, 0.01, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False)
Part.SelectionManager.EnableContourSelection = False
End Sub
9. Now, let us create a macro button for this action. Navigate to Tools > Customize > Commands > Macro. Drag the “New Macro Button” to your toolbar.
10. Then, press the “…” and browse to the previous .swp file. Under Tooltip, type in Cube. And press okay.
11. Previously the file is in blank icon. And now it should look like this.
Now this is a really simple task for you to try the macro out.
What API macro can do is far more than this.
Feel free to explore more on API macro capabilities.
You can download the .swp file from here:
Have fun!
Let us know if you would like to know more. =)