Delete Features and Sketches

Delete Features and Sketches

Anonymous
Not applicable
1,745 Views
5 Replies
Message 1 of 6

Delete Features and Sketches

Anonymous
Not applicable

 

I'm trying to create a code that deletes all the sketches in a part. I'd also like to delete all the features  and their consumed sketches as well. I'm using the below code to try and delete the sketches. It runs without error but also doesn't delete anything.

 

SyntaxEditor Code Snippet

Sub Main()

Dim oPartDoc As PartDocument
        oPartDoc = ThisApplication.ActiveDocument
Dim oPartDef As PartComponentDefinition
        oPartDef = oPartDoc.ComponentDefinition

        
Dim sketch1 As Sketch3D    

For Each sketch1 in oPartDef.Sketches
    sketch1.Delete
Next
        
        
End Sub        

The sketches are all named sketches created using a different code. 

0 Likes
Accepted solutions (2)
1,746 Views
5 Replies
Replies (5)
Message 2 of 6

Owner2229
Advisor
Advisor
Accepted solution

Hi, try this one:

 

Sub Main()

' Get Document and Definition Dim oDoc As Document = ThisApplication.ActiveDocument Dim oCD As ComponentDefinition= oDoc.ComponentDefinition
' Delete Features Dim oFeat As PartFeature For Each oFeat In oCD.Features oFeat.Delete Next
' Delete Sketches Dim oSketch As PlanarSketch For Each oSketch in oCD.Sketches oSketch.Delete Next End Sub

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 3 of 6

Anonymous
Not applicable

Thank you for the help. That works great. 

 

What if I want to delete a specific named sketch?

 

For example if I have 

SyntaxEditor Code Snippet

Dim oSketch1 As Inventor.PlanarSketch
    oSketch1 = oPartDef.Sketches.Add(oPartDef.WorkPlanes(2))
        oSketch.Name = "Feature Sketch"

Is there a way to just delete the sketch titled Feature Sketch? 

0 Likes
Message 4 of 6

Owner2229
Advisor
Advisor
Accepted solution
' Get Document and Definition
Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oCD As ComponentDefinition= oDoc.ComponentDefinition
' Delete Sketch by name Dim oSketch As PlanarSketch = oCD.Sketches.Item("Feature Sketch") oSketch.Delete

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 5 of 6

Anonymous
Not applicable

Can this be used just to activate the sketch?

Or delete a sketch and recreate one?

0 Likes
Message 6 of 6

Owner2229
Advisor
Advisor

Hey, to activate a Sketch you can use this:

oSketch.Edit()

To exit the editing you can use this:

oSketch.ExitEdit()

But to create new sketch you need a face or plane to add the sketch on. E.g. like in acurtinT3UX6's sample above:

Dim oSketch As Inventor.PlanarSketch
oSketch = oPartDef.Sketches.Add(oPartDef.WorkPlanes(2)) oSketch.Name = "Feature Sketch"

 

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes