I am trying to create a macro that will generate a part from a simple sketch. Nothing too complex, I have gotten it to work with the sketch that I recorded the macro with but when I try it on another part its not working as intended (particularly editing the sketch and adding the offset sketch)
Here is my code:
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
'Offset sketch
boolstatus = Part.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
Part.EditSketch
boolstatus = Part.Extension.SketchBoxSelect("-0.063401", "0.022647", "0.000000", "-0.063349", "0.022596", "0.000000")
boolstatus = Part.SketchManager.SketchOffset2(0.0022, False, False, 0, 0, True)
Part.SketchManager.InsertSketch True
boolstatus = Part.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
' Create Extrude
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, True, 0, 0, 0.016, 0.01, True, False, False, False, 4.36332312998583E-02, 4.36332312998583E-02, False, False, False, False, True, True, True, 0, 0, False)
Part.SelectionManager.EnableContourSelection = False
End Sub
I used "select all" but it seems to have created a box selection and I think this may be the issue.
What im trying to achieve:
- From an already created sketch on the front plane, add 2.2mm offset outwards
- Use this sketch to create extrude, 16mm long with 2.5 degrees draft
Once I get that working on ANY sketch, I then need to:
- create sketch on the base face of the above extrude (can be front plane if the face is not suitable), offset 4mm outwards and 1mm inwards on that same face.
- From this sketch create 1.2mm extrude
- Create a set of configurations based off a list of sizes where "D1@Sketch1" is the dimension to be changed for each configuration
Im pretty new to macros so any help is appreciated, thanks.
A recorded macro will almost only ever work for the specific scenario in which it was recorded for the part that it was recorded on. It takes some manipulation to get it to be more generalized.
Try this as a start. It still needs work but I got it functioning on my test part.
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swExtension As ModelDocExtension
Dim swSelMan As SelectionMgr
Dim selection As Object
Dim swFeatureMan As FeatureManager
Dim swSketchMan As SketchManager
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swExtension = swModel.Extension
Set swSelMan = swModel.SelectionManager
Set swFeatureMan = swModel.FeatureManager
Set swSketchMan = swModel.SketchManager
Set selection = swSelMan.GetSelectedObject6(1, 0)
If selection Is Nothing Then
Exit Sub
End If
swModel.EditSketch
swExtension.SelectAll
swSketchMan.SketchOffset2 0.0022, False, False, 0, 0, True
swFeatureMan.FeatureExtrusion3 True, False, True, 0, 0, 0.016, 0.01, True, False, False, False, 4.36332312998583E-02, 4.36332312998583E-02, False, False, False, False, True, True, True, 0, 0, False
End Sub