×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

API - Create named sketch

API - Create named sketch

API - Create named sketch

(OP)
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

RE: API - Create named sketch

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"

RE: API - Create named sketch

(OP)
Thanks handleman, that worked great... but now how do I recall that sketch if I want to delete it?

RE: API - Create named sketch

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.

RE: API - Create named sketch

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources