MACRO LIBRARY

Create an extrude from a sketch

Free public access. No account or subscription required.

Files

Create an extrude from a sketch

Code

'Demonstrates how to programmatically create a sketch and extrude feature from that sketch
    
'Preconditions: new part is open
'Results: extrude feature is created
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Option Explicit
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    'insert sketch on front plane
    swModel.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
    swModel.SketchManager.InsertSketch True
    
    'change settings
    swModel.SketchManager.AddToDB = True
    swApp.SetUserPreferenceToggle swInputDimValOnCreate, False
    
    'create rectangle from four lines
    'stratgy: draw rectangle on piece of paper and write out coordinates
    'note: could Dim skSeg As SldWorks.SketchSegment and then Set skSeg = ...
    swModel.SketchManager.CreateLine 0, 0, 0, 0, 0.01, 0
    swModel.SketchAddConstraints "sgVERTICAL2D"
    swModel.AddDimension2 -0.01, 0.01 / 2, 0
    
    swModel.SketchManager.CreateLine 0, 0.01, 0, 0.01, 0.01, 0
    swModel.SketchAddConstraints "sgHORIZONTAL2D"
    
    swModel.SketchManager.CreateLine 0.01, 0.01, 0, 0.01, 0, 0
    swModel.SketchAddConstraints "sgVERTICAL2D"
    
    swModel.SketchManager.CreateLine 0, 0, 0, 0.01, 0, 0
    swModel.SketchAddConstraints "sgHORIZONTAL2D"
    swModel.AddDimension2 0.01 / 2, -0.01, 0
    
    'make bottom left point coincident with origin
    
    'note: the reason we don't need clearselection above is because the apend argument is false, _
    which means that the previous selection will not be kept. however when we select the point, _
    the selection of the origin will be kept
    
    'select bottom left point
    swModel.Extension.SelectByID2 "", "SKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0
    'select origin
    swModel.Extension.SelectByID2 "", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0
    swModel.SketchAddConstraints "sgCOINCIDENT"
    
    'leave sketch
    swModel.SketchManager.InsertSketch True
    
    'create extrude; depth = 9mm
    swModel.FeatureManager.FeatureExtrusion2 True, False, False, 0, 0, 0.009, 0.01, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False
    
    'revert to original settings
    swModel.SketchManager.AddToDB = True
    swApp.SetUserPreferenceToggle swInputDimValOnCreate, True
End Sub
    
    
'NOTES
'Create corner rectangle must be made equal to a variant; do not use 'set'
'API Help says that CreateCornerRectangle returns an ARRAY of sketch segments, which is why variant must be used
'Apparently you don't use 'set' with
    
'You do not need to use Set (when creating a circle, for example) but you will need to set that _
sketch equal to something if you plan on selecting it later
    
'Is there an offical reason for when Set should or should not be used? An advantage?