How To Delete Occurrence Pattern Element

How To Delete Occurrence Pattern Element

tim11_manhhieu
Advocate Advocate
111 Views
4 Replies
Message 1 of 5

How To Delete Occurrence Pattern Element

tim11_manhhieu
Advocate
Advocate

I want to delete the OIL OUTLET object as shown in the picture but when running the code, it gives an error at the line occPatternElem.occurrences.item(i).Delete2 (False)

Need expert help or is there another way, iLogic or VBA is fine.

 

tim11_manhhieu_0-1757038425148.png

 

Option Explicit

Sub SuppressPatternOccurrencesByName()

    Dim asmDoc As AssemblyDocument
    Dim asmDef As AssemblyComponentDefinition
    Dim occPattern As OccurrencePattern
    Dim targetName As String
    Dim i As Integer
    
    Dim rectPattern As RectangularOccurrencePattern
    Dim circPattern As CircularOccurrencePattern
    Dim compOcc As ComponentOccurrence
    Dim occPatternElem As OccurrencePatternElement
    
    targetName = "OIL OUTLET"
    
    Set asmDoc = ThisApplication.ActiveDocument
    If asmDoc.DocumentType <> kAssemblyDocumentObject Then
        MsgBox "Please open an Assembly file before running this macro!", vbExclamation
        Exit Sub
    End If
    
    Set asmDef = asmDoc.ComponentDefinition
    
    For Each occPattern In asmDef.OccurrencePatterns
        
        If TypeOf occPattern Is RectangularOccurrencePattern Then
            Set rectPattern = occPattern
            
            For Each occPatternElem In rectPattern.OccurrencePatternElements
                For i = occPatternElem.occurrences.count To 1 Step -1
                    If InStr(1, occPatternElem.occurrences.item(i).name, targetName, vbTextCompare) <> 0 Then
                        occPatternElem.occurrences.item(i).Delete2 (False)
                    End If
                Next i
            Next occPatternElem

        End If
        
    Next occPattern

End Sub

 

 

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

JLH_Bigadan
Explorer
Explorer

You can’t delete pattern members directly with Delete2, since they belong to the pattern definition. Instead, you need to suppress the occurrence. Try replacing the Delete2 call with:

occPatternElem.Occurrences.Item(i).Suppressed = True

Message 3 of 5

tim11_manhhieu
Advocate
Advocate

thank you for reply.

0 Likes
Message 4 of 5

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi @tim11_manhhieu.
As @JLH_Bigadan  wrote earlier, it is not possible remove the component from PatternElements, but this can be done at the OccurrencePattern level for all PatternElements. This is done by re-saving the ObjectCollection. Example iLogic code:

Sub main()

    Dim asmDoc As AssemblyDocument = ThisApplication.ActiveDocument
    Dim asmDef As AssemblyComponentDefinition = asmDoc.ComponentDefinition
    Dim occPattern As OccurrencePattern
    Dim targetName As String
    
    Dim rectPattern As RectangularOccurrencePattern
    Dim circPattern As CircularOccurrencePattern
    Dim compOcc As ComponentOccurrence
    Dim occPatternElem As OccurrencePatternElement
    
    targetName = "OIL OUTLET"
    
    If asmDoc.DocumentType <> kAssemblyDocumentObject Then
        MsgBox("Please open an Assembly file before running this macro!", vbExclamation)
        Exit Sub
    End If
	
    For Each occPattern In asmDef.OccurrencePatterns
        
        If TypeOf occPattern Is RectangularOccurrencePattern Then
            rectPattern = occPattern
			Dim i As Integer = 0
			For k As Integer = 1 To rectPattern.ParentComponents.Count
				If rectPattern.ParentComponents(k).Name.Contains(targetName) Then
					i = k
					Exit For
				End If
			Next k
			If i = 0 Then Continue For
			Dim oObjs As ObjectCollection
			oObjs = ThisApplication.TransientObjects.CreateObjectCollection(rectPattern.ParentComponents)
            oObjs.Remove(i)
			rectPattern.ParentComponents = oObjs

        End If
        
    Next occPattern

End Sub

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 5 of 5

tim11_manhhieu
Advocate
Advocate

Many thanks.

0 Likes