Remove an occurrence from an ObjectEnumerator (or ObjectCollection)

Remove an occurrence from an ObjectEnumerator (or ObjectCollection)

oransen
Collaborator Collaborator
1,116 Views
4 Replies
Message 1 of 5

Remove an occurrence from an ObjectEnumerator (or ObjectCollection)

oransen
Collaborator
Collaborator

I have an object collection obtained from the first Element of a pattern. I'd like to remove one object occurrence from that object collection, but I get a syntax error whatever I try. Here's the code:

 

 

Dim i As Integer
Dim sCell as String
Dim oDoc = ThisDoc.Document
Dim oDef As AssemblyComponentDefinition
oDef = oDoc.ComponentDefinition
Dim oPattern As RectangularOccurrencePattern
oPattern = oDef.OccurrencePatterns.Item("NomePattern")
Dim oCompOcc As ComponentOccurrence 

Dim Elements as OccurrencePatternElements
Elements = oPattern.OccurrencePatternElements()

Trace.WriteLine("the pattern has " + Str(Elements.Count) + " elements" )
 
' 1,1 is the first set of objects in the pattern
Dim OPT as OccurrencePatternElement
OPT = oPattern.OccurrencePatternElement(1,1)  
 
Dim Components As ObjectsEnumerator ' ObjectCollection 
Dim CompOcc As ComponentOccurrence 
Dim CompDef As PartComponentDefinition
Dim sToDelete As String 
Dim sOccName As String
Dim iColonPos As Integer
Dim sInstanceName As String
 
Components = OPT.Components()  
 
sToDelete = "30295480" 
 
' Look at each occurrence and remove that with the sToDelete name
For Each CompOcc in Components  
' For i = 1 To Components.Count
  	
	'CompOcc = Components.Item (i) 	
	CompDef = CompOcc.Definition() 
	sOccName = CompDef.Document.FullFileName()
	sInstanceName = CompOcc.Name () 
	iColonPos = InStrRev (sInstanceName,":") ' "ABCDE:1" iColonPos becomes 6 
	sInstanceName = Left(sInstanceName,iColonPos-1) 
	
    Trace.WriteLine("Component " + Str(i)+ " filename: " + sOccName + " in pattern name: " + sInstanceName) 

    If sInstanceName = sToDelete Then
	   ' ***HERE*** .... how to I remove the ith occurrence from the ObjectsEnumerator
	   ' Trace.WriteLine ("Remove..." + Str(i)) 
	   '  Components.Remove (i) 
	   ' CompOcc.Delete
    End If 
	
Next 
 

All seems to work, I get the names written out correctly, but I cannot remove any of the objects from the first Element. Manually this would move the occurrence from the first Pattern's element into the root of the assembly. But niether Delete nor Remove seemt to work.

 

I bet I've missed something obvious...

 

 

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

HermJan.Otterman
Advisor
Advisor

maybe this helps..

 

https://forums.autodesk.com/t5/inventor-customization/exclude-remove-component-occurrence-from-exist...

 

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


Message 3 of 5

oransen
Collaborator
Collaborator

Thanks for that link. Now I seem to have taken a step forward:

 

    If sInstanceName = sToDelete Then

        ' This now compiles at least...           
	Components.RemoveByObject (CompOcc) 
	  
	Exit For
	   
End If Next OPT.Components() = Components ' ERROR because OPT is read only

So Components.RemoveByObject compiles, but when I try to replace the old list with the new list (last line above) I get an error because OTP is read only...

 

 

0 Likes
Message 4 of 5

HermJan.Otterman
Advisor
Advisor
Accepted solution

are you using iLogic? or vb.net?

 

in iLogic try this code,

 

 

		 Dim oAsmDoc As Inventor.AssemblyDocument
        oAsmDoc = ThisApplication.ActiveDocument


        Dim oAsmCompDef As AssemblyComponentDefinition
        oAsmCompDef = oAsmDoc.ComponentDefinition

        Dim oAsmOccPattern As OccurrencePattern
        oAsmOccPattern = oAsmCompDef.OccurrencePatterns.Item(1)

        Dim oAsmCompOcc As ComponentOccurrence
        oAsmCompOcc = oAsmOccPattern.ParentComponents.Item(1)

        Dim oOccurrences As ObjectCollection
        oOccurrences = oAsmOccPattern.ParentComponents

        oOccurrences.RemoveByObject(oAsmCompOcc)

        oAsmOccPattern.ParentComponents = oOccurrences

 

 

 

does it remove an element ?

 

 

maybe remove the ( ) in the last line

(what is OPT?)

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


Message 5 of 5

oransen
Collaborator
Collaborator

Hi Herm Jan Otterman

 

Magic, perfect, thanks for pointing me in the right direction!

 

 

0 Likes