Exclude/remove component occurrence from existing rectangular/circular pattern

Exclude/remove component occurrence from existing rectangular/circular pattern

Anonymous
Not applicable
1,376 Views
1 Reply
Message 1 of 2

Exclude/remove component occurrence from existing rectangular/circular pattern

Anonymous
Not applicable

Hi everybody,

 

I am trying to create VBA macro in IV2011 which will delete all occurrences of content center parts from an assembly.  I found the way how to delete occurrences from an assembly, but I can't find the way how to delete occurrences when they are members of  rectangular or circular pattern. I was trying to exclude them from pattern but code which I wrote doesn't work (see below). It doesn't give me any error message.  Any suggestion what I did wrong?

 

Public Sub RemoveOcc()
    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oAsmDoc As AssemblyDocument
    Set oAsmDoc = oDoc
    
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = oAsmDoc.ComponentDefinition
    
    Dim oAsmOccPattern As OccurrencePattern
    Set oAsmOccPattern = oAsmCompDef.OccurrencePatterns.Item(1)
    
    Dim oAsmCompOcc As ComponentOccurrence
    Set oAsmCompOcc = oAsmOccPattern.ParentComponents.Item(1)
    
    Call oAsmOccPattern.ParentComponents.RemoveByObject(oAsmCompOcc)
End Sub

Thank you,

0 Likes
1,377 Views
1 Reply
Reply (1)
Message 2 of 2

TerryWen
Alumni
Alumni

Stan,

 

Your code should not work, because many Inventor APIs, which return a collection, return a temporary colletion, which means event you changes the collection you get, it will not affect the object from which the collection you get.

 

The only thing you need to do is to set the collection back to the object. so following code will work, i only added the code marked as green.

 

Public Sub RemoveOcc()
    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
   
    Dim oAsmDoc As AssemblyDocument
    Set oAsmDoc = oDoc
   
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = oAsmDoc.ComponentDefinition
   
    Dim oAsmOccPattern As OccurrencePattern
    Set oAsmOccPattern = oAsmCompDef.OccurrencePatterns.Item(1)
   
    Dim oAsmCompOcc As ComponentOccurrence
    Set oAsmCompOcc = oAsmOccPattern.ParentComponents.Item(1)
   


    Dim oOccurrences As ObjectCollection
    Set oOccurrences = oAsmOccPattern.ParentComponents
   
    Call oOccurrences.RemoveByObject(oAsmCompOcc)
   
    oAsmOccPattern.ParentComponents = oOccurrences
   
   
End Sub

 

hope it's useful to you.

 

-terry