·         SolidWorks API

 

·         We will

·         Record a Macro

·         Examine the Macro

·         Modify the Macro

 

 

·         Open cstick.sldprt or create a solid cube (100x100x100) mm

 

·         Record a Macro

 

·         Click Record\Pause Macro (Macro toolbar)

 

·         Select top face

·         Click Sketch

·         Click Normal To

 

·         Click Circle

·         Draw circle with radius 15 mm

·         Click OK

 

·         Click Extruded Cut

·         Set End Condition to Blind

·         Set Depth  to 25

·         Click Draft On/Off

·         Set Draft Angle to 15

·         Click OK (creates the cut)

 

·         Click Stop Macro

·         Save macro as CreateCutExtrude.swp

 

·         Click Edit Macro (Macro toolbar)

·         Open CreateCutExtrude.swp

 

·         Add line of code to top of code

·         Option Explicit

 

·         Delete this line

·         Dim longstatus As Long, longwarnings As Long

 

·         Modify Dim swApp and Dim Part lines to

·         Dim SwApp As SldWorks.SldWorks

·         Dim Part As SldWorks.ModelDoc2

 

·         Test modified macro by recreating cut-extrude in candlestick holder

 

·         In SolidWorks

·         Drag the rollback bar up the FeatureManager design tree until it is above the Extrude1 feature

·         In the Visual Basic Editor, click Run Sub/UserForm  on the Debug toolbar

·         In the Macros dialog box, click Run

·         A new cut-extrude is created in the candlestick holder

·         Delete cut-extrude and sketch just created

·         Drag the rollback bar back to bottom of the FeatureManager design tree

·         Delete the original cut-extrude and sketch

 

·         Debugging a Program

·         Simple debug with deliberate errors (document later)

 

 

·         Unmodified Macro

 

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

boolstatus = Part.Extension.SelectByID2("", "FACE", 0.01572903146831, 0.22, -0.001880817767869, False, 0, Nothing, 0)

Part.SketchManager.InsertSketch True

Part.ClearSelection2 True

Dim skSegment As Object

Set skSegment = Part.SketchManager.CreateCircle(0#, 0#, 0#, 0.002747, -0.003398, 0#)

Part.ClearSelection2 True

boolstatus = Part.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)

Dim myFeature As Object

Set myFeature = Part.FeatureManager.FeatureCut(True, False, False, 0, 0, 0.025, 0.025, True, False, False, False, 0.2617993877992, 0.2617993877992, False, False, False, False, False, True, True)

Part.SelectionManager.EnableContourSelection = False

End Sub

 

 

·         Creating a Generic Cut-Extrude Program

 

·         A cut-extrude with a radius of 15mm and a depth of 25mm is created on pre-selected face and is centered on sketch point

 

·         Click Run on Debug toolbar to test above macro

 

 

·         Modified Macro

 

' STATEMENT TO KEEP

 

Option Explicit

 

 

 

' VARIABLES TO KEEP

 

' Declare all variables

 

' as early bound variables

 

Dim swApp As SldWorks.SldWorks

 

Dim Part As SldWorks.ModelDoc2

 

Dim boolstatus As Boolean

 

 

 

' VARIABLES TO ADD

 

Dim SelectedObject As Object

 

Dim SelectCoords As Variant

 

Dim CircleSketch As SldWorks.Sketch

 

Dim MathUtil As SldWorks.MathUtility

 

Dim SketchPoint As SldWorks.MathPoint

 

Dim Transform As SldWorks.MathTransform

 

Dim dx As Double

 

Dim dy As Double

 

Dim dz As Double

 

 

 

' CODE TO KEEP

 

Sub main()

 

 

 

' Connect to currently active SolidWorks document

 

Set swApp = Application.SldWorks

 

Set Part = swApp.ActiveDoc

 

 

 

' CODE TO COMMENT OUT

 

'boolstatus = Part.Extension.SelectByID2("", "FACE", _

 

'    -0.001804050742408, 0.2199999999999, _

 

'    0.001241411439707, False, 0, Nothing, 0)

 

   

 

' CODE TO ADD

 

' Get coordinates of the point selected

 

SelectCoords = Part.SelectionManager.GetSelectionPoint2(1, -1)

 

' If face is selected, then create the cut-extrude;

 

' else, stop execution

 

Set SelectedObject = Part.SelectionManager.GetSelectedObject6(1, 0)

 

If Part.SelectionManager.GetSelectedObjectType3(1, -1) = _

    swSelFACES Then

 

   

 

' CODE TO KEEP

 

'Insert new sketch

 

Part.SketchManager.InsertSketch True

 

 

' CODE TO COMMENT OUT

 

'Part.ClearSelection2 True

 

'Dim skSegment As Object

 

'Set skSegment = Part.SketchManager.CreateCircle(0#, _

 

'    0#, 0#, 0.003959, -0.008946, 0#)

 

'Part.ClearSelection2 True

 

'boolstatus = Part.Extension.SelectByID2("Arc1", _

 

'    "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)

 

'Dim myFeature As Object

 

'Set myFeature = Part.FeatureManager.FeatureCut(True, _

 

'    False, False, 0, 0, 0.025, 0.01, True, False, _

 

'        False, False, 0.2617993877992, _

 

'        0.01745329251994, False, False, False, False, _

 

'        False, True, True)

 

'Part.SelectionManager.EnableContourSelection = False

 

 

 

' CODE TO ADD

 

' Get MathPoint to use when transforming point from

 

' model space to sketch space

 

Set MathUtil = swApp.GetMathUtility

 

Set SketchPoint = MathUtil.CreatePoint(SelectCoords)

 

 

 

' Get reference to the sketch

 

Set CircleSketch = Part.SketchManager.ActiveSketch

 

 

 

' Translate sketch point into sketch space

 

Set Transform = CircleSketch.ModelToSketchTransform

 

Set SketchPoint = SketchPoint.MultiplyTransform(Transform)

 

 

 

' Retrieve coordinates of the sketch point

 

dx = SketchPoint.ArrayData(0)

 

dy = SketchPoint.ArrayData(1)

 

dz = SketchPoint.ArrayData(2)

 

 

 

' Use Part.SketchManager.CreateCircleByRadius instead of

 

' Part.SketchManager.CreateCircle because

 

' Part.SketchManager.CreateCircleByRadius sketches a

 

' circle centered on a sketch point and lets you specify

 

' a radius

 

Part.SketchManager.CreateCircleByRadius dx, dy, dz, 0.015

 

 

 

' Create the cut-extrude

 

Part.FeatureManager.FeatureCut2 True, False, False, 0, 0, 0.025, _

    0.01, True, False, False, False, 0, 0, False, False, _

    False, False, False, True, True, False, False, False

 

End If

 

 

' CODE TO KEEP

 

End Sub

 

****END MODIFIED MACRO

 

 

·         Creating a Form

 

·         Create a Visual Basic form where users can specify values for radius and depth

 

·         Declarations

 

Public depth As Double

Public radius As Double

 

·         Code for form

 

' Make sure that the text in the two text boxes on the

' form, txtDepth and txtRadius, is numerical

If IsNumeric(txtDepth.text) And IsNumeric(txtRadius.text) Then

depth = txtDepth.text

radius = txtRadius.text

' Close the dialog box and continue

Hide

Else

' Display a message box telling users to enter numerical

' values for both depth and radius

MsgBox "You must enter numeric values for both depth and radius."

End If

 

·         Add two variables after Dim dz As Double

 

Dim HoleRadius As Double

Dim HoleDepth As Double

 

·         Add code

·         After Set Part = swApp.ActiveDoc

·         Before ' CODE TO COMMENT OUT to display the form and retrieve the depth and radius values entered by users

 

' Create an instance of the user form

Dim myForm As New frmCutExtrude

 

' Set the caption for the form

myForm.Caption  = "Size of Cut-Extrude in Millimeters"

 

 

' Display the user form and retrieve radius and depth values entered by users

' Divide the values by 1000 to change millimeters to meters

myForm.Show

HoleRadius = myForm.radius / 1000

HoleDepth = myForm.depth / 1000

 

'Destroy the user form and remove it from memory because it is no longer needed

Set myForm = Nothing

 

 

·         Replace all of the code

·         Appearing after dz = SketchPoint.ArrayData(2) with this code:

 

'Sketch circle centered on the sketch point

 

Part.SketchManager.CreateCircleByRadius dx, dy, dz, HoleRadius

 

' Create cut-extrude without draft

 

Part.FeatureManager.FeatureCut2 True, False, False, 0, 0, HoleDepth, _

0.01, True, False, False, False, 0, 0, False, False, False, False, _

 

False, True, True, False, False, False

 

End If

 

End Sub

 

 

·         Click Save CreateCutExtrude  to save the modified code

 

·         Click File, Close and Return to SolidWorks

 

 

·         In SolidWorks

 

·         Select any face

·         Click Run Macro

·         Open CreateCutExtrude.swp

·         Type a value in Radius

·         Type a value in Depth

·         Click OK

 

·         A cut-extrude of specified depth and radius is created on the face

·         Repeat steps to create additional cut-extrudes on face