| |
Learn
to use macros to create or modify a program understanding
VB syntax
| Level: |
Intermediate |
| ID#: |
10220527 |
| Category: |
API, Best Practice,
Tech Tip |
| Products/Version: |
SolidWorks 2003 |
| Last revised: |
10/22/05 |

This article explains how macros can be used to create or modify a program. The example shown provides insight into the actual syntax of the API.
|
Phase I
- Change the hole diameter from 1.25" to 1.00"
Phase II
- Create a dialog box to select holes
- Add a tolerance value to feature
|
 |
The first step is to create a macro to modify the hole diameter. We will then edit this macro to create the final version. Several steps can be eliminated to make the macro easy to understand. The first macro was recorded to get the syntax and structure. The second macro was edited and commented to eliminate unnecessary functions and make the macro easier to read.
Original macro
' ********************************************************
' Macro1.swb macro recorded on 10/09/01 by Administrator
' ********************************************************
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long
Dim Annotation As Object
Dim Gtol As Object
Dim DatumTag As Object
Dim FeatureData As Object
Dim Feature As Object
Dim Component As Object
Sub main()
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
Part.SelectByID "", "FACE", 0.02066426363137, 0.00879066642193, 0.01313760031121
Part.ActivateSelectedFeature
Part.SelectByID "D1@Sketch4@Part1.SLDPRT", "DIMENSION", 0.05246257051435, 0.04563116669652, 0.02661487949333
Part.Parameter("D1@Sketch4").SystemValue = 0.0254
Part.EditDimensionProperties2 4, 0.000381, 0, "", "", 0, 2, 2, 1, 11, 11, "", "", 1, "", "", 0
Part.EditDimensionProperties2 4, 0.000508, 0, "", "", 0, 2, 2, 1, 11, 11, "", "", 1, "", "", 0
Part.ClearSelection
End Sub
The following is an edited version of the recorded macro fully commented with the extra functions removed.
Edited macro
' ******************************************************************************
' Edithole.swp
' This macro will modify the hole from 1.25 to 1.00 dia and rebuild the part
' ******************************************************************************
Dim swApp As Object 'Initialize the SolidWorks object
Dim Part As Object 'Initialize the part object
Sub main() 'Start the main sub-routine
'Attach the macro to SolidWorks and use the active part
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
'Select the hole diameter
Part.SelectByID "D1@Sketch2@Part1.SLDPRT", "DIMENSION", 0.01095010187308, 0.0498653958722, 0.04454557515595
'This changes the diameter from 1.25" (.03175m) to 1.00"(.0254m).
'Note: All values in macros are in meters
'If the dimension is not selected, the tolerance value can not be changed.
Part.Parameter("D1@Sketch2").SystemValue = 0.0254
'Add a tolerance value and change the precision to 3 places. See the API Help file for a complete
'description of the arguments and their data types
Part.EditDimensionProperties2 2, 0.000381, -0.000254, "", "", 0, 3, 2, 1, 11, 11, "", "", 1, "", "", 0
'Clear selected item
Part.ClearSelection
'Rebuild the part with the new dimension
Part.EditRebuild
End Sub 'End the main sub-routine
|
As you can see, the edited macro is more easily understood. The hole diameter has been changed, a tolerance added, and the part rebuilt.
To make this macro more useful, we could add a dialog box that allows the holes to be selected and tolerance value to be added.
|
 |
Try it yourself download the part and run the macro.
|
|