API skips deleting sketch geometry

API skips deleting sketch geometry

Anonymous
Not applicable
840 Views
3 Replies
Message 1 of 4

API skips deleting sketch geometry

Anonymous
Not applicable

I am trying to create code to delete all of the circle geometry in a given sketch.  The code I have works... sort of.

 

Dim oPD As PartDocument
oPD = ThisApplication.ActiveDocument
Dim oCD As ComponentDefinition
oCD = oPD.ComponentDefinition
Dim oS As PlanarSketch 
oS = oCD.Sketches.Item("HOLE SKETCH")
Dim oSC As SketchCircle
For i = 1 To HOLES
	oSC = oS.SketchCircles.Item(i)
	oSC.Delete
	MessageBox.Show("Hole " & i & " deleted", "Note")
Next

The strange behavior I have noticed is that this bit of code seems to skip every second instance.  For example if I create a row of circles, the first one is deleted, then it deletes the third one and says the second is deleted, then the 5th one etc.  When it gets to the end of the half deleted row of circles it then errors out letting me know that it couldn't find any more.

hole error.jpg

Why is this happening? Am I doing something wrong?

 

If I set this rule to delete just one circle, and then create another rule to trigger this rule for however many circles exist, then everything works fine.  But I still feel like I am doing something wrong with this band-aid approach.

0 Likes
Accepted solutions (1)
841 Views
3 Replies
Replies (3)
Message 2 of 4

lmc.engineering
Advocate
Advocate
Accepted solution

Hi @Anonymous 

 

I'd do it like this:

 

Sub Main()
	DeleteCircleGeo("HOLE SKETCH")
End Sub

Sub DeleteCircleGeo(sSketchName As String)
	Dim oDoc As Document = ThisApplication.ActiveDocument
	If oDoc.DocumentType <> 12290 Then 
		MessageBox.Show("This rule can only be run in a part document", "iLogic")
		Exit Sub
	End If

	Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
	Dim oSketch As Sketch = oDef.Sketches.Item(sSketchName)
	For Each oCircle As SketchCircle In oSketch.SketchCircles
		oCircle.Delete
	Next
End Sub


0 Likes
Message 3 of 4

WCrihfield
Mentor
Mentor

Within your code, I moved a couple things around and changed a couple things.

First of all, I don't see what "HOLES" is referring to in you loop statement, so I replaced that with the collection of SketchCircles within the sketch.  I then replaced the Integer counter strategy with a "Each" strategy.  And since the SketchCircle entities don't have individual names or identifiers, I just deleted your MessageBox.

Dim oPD As PartDocument = ThisApplication.ActiveDocument
Dim oCD As ComponentDefinition = oPD.ComponentDefinition
Dim oS As PlanarSketch = oCD.Sketches.Item("HOLE SKETCH")
For Each oSC As SketchCircle In oS.SketchCircles.Count
	oSC.Delete
Next

Using 'Each' is almost always easier to use than the Integer method, when it can be used.

Perhaps if you had defined:   Dim oHOLES As SketchCircles = oS.SketchCircles

it may have worked.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 4

Anonymous
Not applicable

Thank you both.

 

The second suggestion was nice and simple however oS.SketchCircles.count should just be Os.SketchCircles

 

Cheers,

-Robin

0 Likes