Pr e-R elea ded se
Lesson 4 Automating Part DesignAutomating Part Design
Upon successful completion of this lesson, you will be able to:
I Design a macro around the part creation process.
I Automate sketching entities.
I Add dimensions to sketch entities.
I Automatically create features such as extrudes and revolves.
I Learn how to enable and use contour selection with the API.
Pr e-R elea se
Do no t co py or dis trib ute
Lesson 4 API Fundamentals
Automating Part Design
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Lesson 4
Automating Part Design
Case Study: Automation Tool for Parts 129
Case Study:
Automation Tool for Parts
This macro is designed to explore the various API calls used when creating parts. It automates user preferences, sketch commands,
dimensioning, contour selection and feature definitions.
The macro has one form containing a multi-page tab control. Each tab allows the user to choose which option to set: with contour selection, or revolution).
1 Edit macro.
Open the macro PartAutomation.swp.
2 Store values to database and set units.
Double-click the command button Build Part and enter the following lines of code:
Private Sub cmdBuild_Click() Set swApp = Application.SldWorks Set swModel = swApp.NewPart swModel.SetAddToDB True
swModel.SetUserPreferenceIntegerValue swUnitsLinear, swMM swModel.SetAddToDB False
End Sub
Note Use ModelDoc2::SetAddToDB to add sketch entities while avoiding grid and entity snapping. This method will also improve system performance when adding sketch entities with the API. Itis a toggle that is turned on by passing True to the parameter list. After the
sketching APIs have finished, turn it off by calling SetAddToDB again, and pass False to it’s parameter list.
Pr e-R elea se
Do no t co py or dis trib ute
Lesson 4 API Fundamentals
Automating Part Design
Setting Material For the first tab, call ModelDoc2::SetUserPreferenceDoubleValue to set the material density and crosshatch pattern.
3 Add code to set the material.
Private Sub cmdBuild_Click() Set swApp = Application.SldWorks Set swModel = swApp.NewPart swModel.SetAddToDB True
swModel.SetUserPreferenceIntegerValue swUnitsLinear, swMM
‘MATERIAL
If optAl.Value = True Then
swModel.SetUserPreferenceDoubleValue _ swConst.swMaterialPropertyDensity, 2700 swModel.SetUserPreferenceStringValue _
swConst.swMaterialPropertyCrosshatchPattern, _
"ISO (Aluminum)"
Else
swModel.SetUserPreferenceDoubleValue _ swConst.swMaterialPropertyDensity, 8600 swModel.SetUserPreferenceStringValue _
swConst.swMaterialPropertyCrosshatchPattern, _
"ISO (Bronze Brass)"
End If
‘PROFILE
‘MACHINE OPERATION swModel.SetAddToDB False End Sub
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Lesson 4
Automating Part Design
Case Study: Automation Tool for Parts 131
Creating the Sketch Rectangle
For the second tab, call ModelDoc2::SketchRectangle to create the first profile, ModelDoc2::SketchOffset2 to offset the profile and
ModelDoc2::CreateLine2( ).ConstructionGeometry to create the axis.
Adding Dimensions
ModelDoc2:: AddDimension2, AddVerticalDimension2 and AddHorizontalDimension2 are used to dimension sketch entities. A dimension is only added if an entity is selected. In this next section we will create the rectangle line by line, and dimension the sketch.
Selection on Creation
When you use the API to create any thing in a SolidWorks document, it will automatically be selected. In the next section of code notice that the first time a sketched line is created, the method call
ModelDoc2::AddDimension2 immediately follows. By design, the API will automatically select and highlight each sketch entity when it is created. Because of this, no selection code needs to be written to add the dimension to the sketched line.
The same is true for feature creation.
For instance, if you create a reference plane with a method like
CreatePlaneAtOffset3, or any of the plane creation APIs, the plane will be selected and highlighted in the FeatureManager design tree. No selection APIs would be needed to select the plane if the program made a call to ModelDoc2::InsertSketch2 immediately after it was created.
Note A detailed review of the selection APIs are covered in Selection and Traversal Techniques on page 177.
1. Create rectangle 2. Create offset 3. Add axis
Pr e-R elea se
Do no t co py or dis trib ute
Lesson 4 API Fundamentals
Automating Part Design
4 Add code to sketch and dimension a rectangle.
Add code to sketch and dimension the profiles.
swModel.InsertSketch2 False
If optRectangular.Value = True Then txtRadius.enabled = False
Dim Height As Double Dim Width As Double
Height = CDbl(txtHeight.text) / 1000 Width = CDbl(txtWidth.text) / 1000 'Turn off dimension dialogs
swApp.SetUserPreferenceToggle swInputDimValonCreate, False 'Create the first line in the profile
swModel.CreateLine2 0.05, 0.05, 0, 0.05, 0.05 + Height, 0 'Add a dimension to the selected entity
swModel.AddDimension2 0, 0.05 + Height / 2, 0
swModel.CreateLine2 0.05, 0.05 + Height, 0, 0.05 + Width, _ 0.05 + Height, 0
swModel.CreateLine2 0.05 + Width, 0.05 + Height, 0, _ 0.05 + Width, 0.05, 0
swModel.CreateLine2 0.05 + Width, 0.05, 0, 0.05, 0.05, 0 swModel.AddDimension2 0.05 + Width / 2, 0, 0
swModel.ClearSelection 'Select the origin
swModel.Extension.SelectByID2 "", "EXTSKETCHPOINT", _ 0, 0, 0, False, 0, Nothing, 0
'Select an end point on the profile
swModel.Extension.SelectByID2 "", "SKETCHPOINT", _ 0.05, 0.05, 0, True, 0, Nothing, 0
'Add a vertical dimension
swModel.AddVerticalDimension2 0, 0.025, 0 swModel.ClearSelection
'Select the origin
swModel.Extension.SelectByID2 "", "EXTSKETCHPOINT", _ 0, 0, 0, False, 0, Nothing, 0
'Select an end point on the profile
swModel.Extension.SelectByID2 "", "SKETCHPOINT", _ 0.05, 0.05, 0, True, 0, Nothing, 0
'Add a horizontal dimension to fully constrain the sketch swModel.AddHorizontalDimension2 0.025, 0, 0
swModel.ClearSelection
'Select all four profile edges
swModel.Extension.SelectByID2 "Line1", "SKETCHSEGMENT", _ 0, 0, 0, False, 0, Nothing, 0
swModel.Extension.SelectByID2 "Line2", "SKETCHSEGMENT", _ 0, 0, 0, True, 0, Nothing, 0
swModel.Extension.SelectByID2 "Line3", "SKETCHSEGMENT", _ 0, 0, 0, True, 0, Nothing, 0
swModel.Extension.SelectByID2 "Line4", "SKETCHSEGMENT", _ 0, 0, 0, True, 0, Nothing, 0
'Create the offset sketch profile from the selected edges swModel.SketchOffset2 0.002, 0, 1
swModel.ViewZoomtofit2 Else
End if
swModel.CreateLine2 _
(0, 0, 0, 0, 0.05, 0).ConstructionGeometry = True swModel.ViewZoomtofit2
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Lesson 4
Automating Part Design
Case Study: Automation Tool for Parts 133
Creating the Sketch Circle
Call ModelDoc2::CreateCircleByRadius2 to create the second profile.
5 Add code to sketch circle.
Creating the construction line is the same for the rectangle or the circle.
Therefore, this call can fall outside the IF..Then statement.
Else
Dim Radius As Double
Radius = CDbl(txtRadius.text) / 1000 swModel.CreateCircleByRadius2 _
0.05 + Radius, 0.05 + Radius, 0, Radius swModel.SketchOffset2 0.002, 0, 1
swModel.ViewZoomtofit2 End If
swModel.CreateLine2 _
(0, 0, 0, 0, 0.05, 0).ConstructionGeometry = True swModel.ViewZoomtofit2
Creating Extruded Features
Call FeatureManager::FeatureExtrusion to create the extrude feature.
1. Create circle 2. Create offset 3. Use axis from previous step
Pr e-R elea se
Do no t co py or dis trib ute
Lesson 4 API Fundamentals
Automating Part Design
6 Add code to create an extruded feature.
‘MACHINE OPERATION
Dim swFeatMgr As SldWorks.FeatureManager If optExtrude.Value = True Then
Dim Depth As Double
Depth = CDbl(txtDepth.text) / 1000 Set swFeatMgr = swModel.FeatureManager
swFeatMgr.FeatureExtrusion True, False, True, 0, 0, Depth, _ 0, False, False, False, False, 0, 0, 0, 0, 0, 0, _
False, False, False swModel.ViewZoomtofit2 End If
Enabling Contour Selection for the Extrusion
Contour selection is used to select multiple contours when creating features. In the SolidWorks user interface, a user must put SolidWorks into contour select mode and then manually select the contours that they want to extrude or cut. The API provides access to the contour select capabilities and allows the programmer to automate the selection process for multiple contours.
Call the SelectionManager::EnableContourSelection to put SolidWorks into contour selection mode.
Then use ModelDocExtention::SelectByID2 to select the contours used to extrude or revolve the new feature. When finished, turn off contour selection.
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Lesson 4
Automating Part Design
Case Study: Automation Tool for Parts 135
7 Add code to enable contour selection.
‘MACHINE OPERATION
Dim swFeatMgr As SldWorks.FeatureManager If optExtrude.Value = True Then
Dim Depth As Double
Depth = CDbl(txtDepth.text) / 1000 Set swFeatMgr = swModel.FeatureManager If optRectangular.Value = True Then
If chkContour1.Value = True Then
swModel.SelectionManager.EnableContourSelection = 1 swModel.Extension.SelectByID2 "", "SKETCHCONTOUR", _
0.05, 0.05, 0, True, 4, Nothing, 0 End If
If chkContour2.Value = True Then
swModel.SelectionManager.EnableContourSelection = 1 swModel.Extension.SelectByID2 "", "SKETCHCONTOUR", _
0.05 - 0.002, 0.05, 0, True, 4, Nothing, 0 End If
Else
If chkContour1.Value = True Then
swModel.SelectionManager.EnableContourSelection = 1 swModel.Extension.SelectByID2 "", "SKETCHCONTOUR", _
0.05 + Radius, 0.05, 0, True, 4, Nothing, 0 End If
If chkContour2.Value = True Then
swModel.SelectionManager.EnableContourSelection = 1 swModel.Extension.SelectByID2 "", "SKETCHCONTOUR", _
0.05 + Radius, 0.05 - 0.002, 0, True, 4, Nothing, 0 End If
End If
swFeatMgr.FeatureExtrusion True, False, True, 0, 0, Depth, _ 0, False, False, False, False, 0, 0, 0, 0, 0, 0, _
Call FeatureManager::FeatureRevolve to create the revolve feature.
Pr e-R elea se
Do no t co py or dis trib ute
Lesson 4 API Fundamentals
Automating Part Design
8 Add code to create revolved feature.
‘MACHINE OPERATION
Dim swFeatMgr As SldWorks.FeatureManager If optExtrude.Value = True Then
Dim Depth As Double
Depth = CDbl(txtDepth.text) / 1000 Set swFeatMgr = swModel.FeatureManager If optRectangular.Value = True Then
If chkContour1.Value = True Then
swModel.SelectionManager.EnableContourSelection = 1 swModel.Extension.SelectByID2 "", "SKETCHCONTOUR", _
0.05, 0.05, 0, True, 4, Nothing, 0 End If
If chkContour2.Value = True Then
swModel.SelectionManager.EnableContourSelection = 1 swModel.Extension.SelectByID2 "", "SKETCHCONTOUR", _
0.05 - 0.002, 0.05, 0, True, 4, Nothing, 0 End If
Else
If chkContour1.Value = True Then
swModel.SelectionManager.EnableContourSelection = 1 swModel.Extension.SelectByID2 "", "SKETCHCONTOUR", _
0.05 + Radius, 0.05, 0, True, 4, Nothing, 0 End If
If chkContour2.Value = True Then
swModel.SelectionManager.EnableContourSelection = 1 swModel.Extension.SelectByID2 "", "SKETCHCONTOUR", _
0.05 + Radius, 0.05 - 0.002, 0, True, 4, Nothing, 0 End If
End If
swFeatMgr.FeatureExtrusion True, False, True, 0, 0, Depth, _ 0, False, False, False, False, 0, 0, 0, 0, 0, 0, _
False, False, False
swModel.SelectionManager.EnableContourSelection = 0 swModel.ViewZoomtofit2
Else
Dim Angle As Double
Angle = CDbl(txtAngle.text * 3.14 / 180) Set swFeatMgr = swModel.FeatureManager
swFeatMgr.FeatureRevolve Angle, True, 0, 0, 0, True, _ False, True
End If
9 Save and run macro.
Test the various options and combinations on the user form. Return to VBA when finished.
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Lesson 4
Automating Part Design
Case Study: Automation Tool for Parts 137
Use the tables on the next few pages as a quick reference for
identifying which methods are used for sketching, feature and viewing commands.
Note Also available through the API:
ShowNamedView2 "*Trimetric", 8 ShowNamedView2 "*Dimetric", 9
Standard Commands
NewDocument PrintDirect
PrintOut OpenDoc6
LoadFile2
Rebuild EditRebuild3 ForceRebuild3 Save3
SaveAs4 SaveBMP
Standard View Commands
ShowNamedView2 "*Front", 1 ShowNamedView2 "*Top", 5
ShowNamedView2 "*Back", 2 ShowNamedView2 "*Bottom", 6
ShowNamedView2 "*Left", 3 ShowNamedView2 "*Isometric", 7
ShowNamedView2 "*Right", 4 ShowNamedView2 "*Normal To", 0
View Commands
TranslateBy ViewDisplayWireframe
ViewOrientationUndo ViewDisplayHiddengreyed
ViewZoomtofit2 ViewDisplayHiddenremoved
ViewZoomTo2 HlrQuality = 1
ZoomByFactor HlrQuality = 0
ViewZoomToSelection ViewDisplayShaded
RotateAboutCenter AddPerspective
Pr e-R elea se
Do no t co py or dis trib ute
Lesson 4 API Fundamentals
Automating Part Design
Sketch Commands
SelectByID2 SelectByRay
Insert3DSketch2
GridOptions SketchModifyFlip
SketchModifyRotate SketchModifyScale SketchModifyTranslate
InsertSketch2 AutoSolveToggle
Sketch Tools Commands
CreateLine2 SketchUseEdge2
CreateArc2 Sketch3DIntersections
CreateTangentArc2 InsertProjectedSketch2
Create3PointArc SketchMirror
CreateCircle2
CreateCircleByRadius2
SketchFillet2
CreateEllipse2 CreateEllipticalArc2
SketchChamfer
SketchParabola SketchOffset2
SketchSpline CreateSpline
Insert3DSplineCurve
MakeStyledCurves
SketchPolygon SketchTrim, 1
SketchRectangle SketchTrim, 2
SketchRectangleAtAnyAngle SplitOpenSegment SplitClosedSegment
CreatePoint2 CreateLinearSketchStepAndRepeat
CreateCenterLine
CreateLine2.ConstructionGeometry
CreateCircularSketchStepAndRepeat
InsertSketchText InsertSketchPicture
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Lesson 4
Automating Part Design
Case Study: Automation Tool for Parts 139
Features Commands
Lesson 4 API Fundamentals
Automating Part Design
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Exercise 8:
Automating the Part Creation Process
141
Exercise 8:
Automating the Part Creation Process
Objective To design your own part automation tool using the techniques and methods from the case study. The forms are prebuilt, but missing controls. Add these controls, and capture the user-entered values before launching the machine command.
The tool should set the material to Steel (density = 7.8 grams/cm3), and sketch a profile (L bracket) with adjustable height and width. It should also allow the user to extrude or revolve the profile as a thin feature (with adjustable depth or angle).
Skills Learned I Automating sketch entities and dimensions
I Automating features and set user preferences.
APIs Used ModelDoc2.SetUserPreferenceDoubleValue ModelDoc2.SetUserPreferenceStringValue SldWorks.SetUserPreferenceToggle ModelDoc2.CreateLine2
ModelDoc2.ViewZoomtofit2 ModelDoc2.AddDimension2
FeatureManager.FeatureExtrusionThin FeatureManager.FeatureRevolveThin
Procedure 1. Open the macro AutomatingPartDesign.swp.
2. Add missing controls to the form.
3. Program the click event for the Build Part command button.
4. Use the APIs listed above to automate the design of an L bracket.
5. Save and run macro.
Pr e-R elea se
Do no t co py or dis trib ute
Exercise 8: API Fundamentals
Automating the Part Creation Process
Solution Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeatMgr As SldWorks.FeatureManager
Private Sub cmdBuild_Click() Set swApp = Application.SldWorks Set swModel = swApp.NewPart
swModel.SetUserPreferenceIntegerValue swConst.swUnitsLinear,_
swConst.swMETER
If chkSteel.Value = True Then
swModel.SetUserPreferenceDoubleValue _
Dim Height As Double Dim Width As Double
Height = CDbl(txtHeight.text) / 1000 Width = CDbl(txtWidth.text) / 1000
swApp.SetUserPreferenceToggle swConst.swInputDimValOnCreate,_
False
swModel.CreateLine2 0.01, 0.01, 0, 0.01, 0.01 + Height, 0 swModel.ViewZoomtofit2
swModel.AddDimension2 0, 0.01 + Height / 2, 0
swModel.CreateLine2 0.01, 0.01, 0, 0.01 + Width, 0.01, 0 swModel.ViewZoomtofit2
swModel.AddDimension2 0.01 + Width / 2, 0, 0 swModel.CreateLine2(0, 0, 0, 0, 0.01, 0). _
ConstructionGeometry = True swModel.ClearSelection
swModel.ViewZoomtofit2
Dim Thick As Double
Thick = CDbl(txtThick.text) / 1000 If optExtrude.Value = True Then
Dim Depth As Double
Depth = CDbl(txtDepth.text) / 1000 Set swFeatMgr = swModel.FeatureManager
swFeatMgr.FeatureExtrusionThin True, False, True, 0, 0, _ Depth, 0, False, False, False, False, 0, 0, False, _ False, False, False, False, Thick, 0, 0, 0, 0, False, _ False, False, False
swModel.ViewZoomtofit2 Else
Dim Angle As Double
Angle = CDbl(txtAngle.text * 3.14 / 180) Set swFeatMgr = swModel.FeatureManager
swFeatMgr.FeatureRevolveThin Angle, 0, 0, 0, Thick, 0, 0, _ 1, 1, 1
API Fundamentals
143