Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

API - Create named sketch

Status
Not open for further replies.

jloeff

Mechanical
Feb 13, 2002
29
0
0
US
I have a macro that creates a 3d sketch around a part and I would like to give it a name so that it can be recalled by another macro. The following is the current code to insert the sketch:

Part.Insert3DSketch2 True
Part.SetAddToDB True
Part.SetDisplayWhenAdded False

'Draw bounding box
...

Part.SetDisplayWhenAdded True
Part.SetAddToDB False
Part.Insert3DSketch2 True

 
Replies continue below

Recommended for you

Code:
'Add to variable declarations:
Dim PartExt as SldWorks.ModelDocExtension
Dim MyFeat as SldWorks.Feature


'Immediately after Part.Insert3DSketch2:
Set PartExt = Part.Extension
Set MyFeat = PartExt.GetLastFeatureAdded
MyFeat.Name = "WhateverYouWantItToBe"
 
You could iterate through the Feature Manager Design Tree looking for features with the name "WhateverYouWantItToBe". I think there are some examples in the API help of iteration through the design tree.
 
Got it to work with Model.Extension.DeleteSelection2


Sub main()
'This macro will delete a sketch named "whatever"
Dim swApp As SldWorks.SldWorks
Dim Model As ModelDoc2
Dim feature As feature
Dim boolstatus As Variant
Dim bValue As Boolean

Set swApp = Application.SldWorks
Set Model = swApp.ActiveDoc

' Select the feature named "whatever"
boolstatus = Model.Extension.SelectByID2("whatever", "SKETCH", 0, 0, 0, False, 0, Nothing, swSelectOptionDefault)

' If the selection was successful, that is, "whatever" was
' selected and it is a "SKETCH", then get that feature; otherwise,
' indicate failure

If boolstatus = True Then
Dim SelMgr As SelectionMgr
Set SelMgr = Model.SelectionManager
Set feature = SelMgr.GetSelectedObject6(1, 0)
' Now delete the feature:
bValue = feature.Select2(True, 0)
bValue = Model.Extension.DeleteSelection2(swDelete_Children)
Else
MsgBox "Sketch not found."
End If

End Sub

-Jeff
SW2006 SP4.1
 
Status
Not open for further replies.
Back
Top