VBA remove items on sketch

VBA remove items on sketch

Anonymous
Not applicable
906 Views
4 Replies
Message 1 of 5

VBA remove items on sketch

Anonymous
Not applicable

Hello, I create by VBA some points by using following code snipe:

 

Dim oSketch As PlanarSketch
Set oSketch = ThisApplication.ActiveEditObject
Dim oPoint As Point2d

Dim dblX As Double
Dim dblY As Double

' [...] define dblX and dblY
Set oPoint = ThisApplication.TransientGeometry.CreatePoint2d(CDbl(dblX), CDbl(dblY))
oSketch.SketchPoints.Add oPoint, True

Now I want to create a foreach loop to delete all points.

delete.png
Maybe some one has a code snipe for this? Thanks a lot!

0 Likes
Accepted solutions (1)
907 Views
4 Replies
Replies (4)
Message 2 of 5

Sergio.D.Suárez
Mentor
Mentor

Hi, with this macro code you can erase all the points of the sketch that you identify through the item, I hope you serve some segment of the program for your particular case. regards!

 

Sub Delete_All_Points()

Dim doc As PartDocument
Set doc = ThisApplication.ActiveDocument

'Define planar sketch
Dim oSketch As PlanarSketch
Set oSketch = doc.ComponentDefinition.Sketches.Item(1) 'Specify the sketch through the item

'Get a reference to the SketchPoint collection.
Dim oPoints As Inventor.SketchPoints
Set oPoints = oSketch.SketchPoints

For Each oPoint In oPoints
oPoint.Delete
Next

End Sub


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 3 of 5

Anonymous
Not applicable

@Anonymous: Thank you for your code!

 

I tryed it and get a "Run-time Error: '5': Invalid procedure call or argument" on line oPoint.Delete

So I checked the number of points:

For Each oPoint In oPoints
'oPoint.Delete ' Run-time Error: '5': Invalid procedure call or argument
i = i + 1
Next oPoint
MsgBox (i)

This MsgBox show the right quantity of my points. So far it works, but will not delete.
Do you have a idea? big thx!

0 Likes
Message 4 of 5

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Try these lines maybe pass the problem and can be identified, I have tried it in my file and I do not see an error. I will be attentive to your answer. regards

 

Sub Delete_All_Points()

Dim doc As PartDocument
Set doc = ThisApplication.ActiveDocument

'Define planar sketch
Dim oSketch As PlanarSketch
Set oSketch = doc.ComponentDefinition.Sketches.Item(1) 'Specify the sketch through the item

'Get a reference to the SketchPoint collection.
Dim oPoints As Inventor.SketchPoints
Set oPoints = oSketch.SketchPoints

For Each oPoint In oPoints
   On Error Resume Next
   Call oPoint.Delete
Next

End Sub


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 5 of 5

Anonymous
Not applicable

Thats it! You made my day!

0 Likes