Add pattern to browser folder

Add pattern to browser folder

A_Geistmann
Explorer Explorer
211 Views
2 Replies
Message 1 of 3

Add pattern to browser folder

A_Geistmann
Explorer
Explorer

Hello everyone,

 

I'm working with iLogic for a couple of months now. Most problems while learning are quite managable using the forum, but this time I'm really stuck...

I built myself a function, that I'm already using in some rules.

 

It's going through all components in an assembly, collects the ones containing a certain name in an object list and then puts all components in a folder. To be exact: it checks if a browser folder is there and creates one if not.

This works fine and reliably, but it doesn't work for patterns. I keep failing to also move a pattern into the folder. I can do it manually by drag&drop, so it should work after all. But simply adding the pattern as an object to the list, so it also loops the pattern, doesn't do the trick...

 

Here's my code:

Sub Main()

PartName = "Finger protection"
AddToFolder("Fingerplates", PartName)

End Sub

Function AddToFolder(FolderName As String, PartName As String)
    ' Add all components and patterns containing the PartName into a list
    Dim oADoc As AssemblyDocument = ThisDoc.Document
    Dim oPane As BrowserPane = oADoc.BrowserPanes.Item("Model")
    Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
    Dim oOccs As ComponentOccurrences = oADef.Occurrences
    Dim oPatterns As OccurrencePatterns = oADef.OccurrencePatterns
    Dim oItemsToAdd As New List(Of Object)
    Dim oPFolder As BrowserFolder = Nothing

    ' Search for all occurrences containing the PartName
    For Each occ As ComponentOccurrence In oOccs
        If occ.Name.Contains(PartName) Then
            oItemsToAdd.Add(occ)
            Logger.Trace("Occurrence found: " & occ.Name)
	Else
	    Logger.Trace("No component found")
        End If
    Next

    ' Search for all patterns containing the PartName
    For Each pattern As OccurrencePattern In oPatterns
        If pattern.Name.Contains(PartName) Then
            oItemsToAdd.Add(pattern)
	    Logger.Trace("Occurrence found: " & pattern.Name)
	Else
	    Logger.Trace("No component found")
	End If
    Next
   

    ' Add all items from the list to the folder
    If oItemsToAdd.Count > 0 Then

        ' Create the folder if it doesn't exist
        Try
            oPFolder = oPane.TopNode.BrowserFolders.Item(FolderName)
        Catch
            oPFolder = oPane.AddBrowserFolder(FolderName, Nothing)
        End Try

        ' Loop through the list and add each item to the folder
        For Each item In oItemsToAdd
            Dim oNode As BrowserNode = oPane.GetBrowserNodeFromObject(item)
            If oNode IsNot Nothing Then
                Try
                    oPFolder.Add(oNode)
                Catch
                    Logger.Trace(item.Name)
                End Try
            End If
        Next
    
    End If

End Function

 Hope someone has had something like this already. Thanks in advance!

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

Stakin
Collaborator
Collaborator
Accepted solution
Sub Main()
PartName = "Finger protection"
AddToFolder("Fingerplates", PartName)
End Sub


Function AddToFolder(FolderName As String, PartName As String)
    Dim oADoc As AssemblyDocument = ThisDoc.Document
    Dim oPane As BrowserPane 
	oPane = oADoc.BrowserPanes.Item("model")
    Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
    Dim oOccs As ComponentOccurrences = oADef.Occurrences
    Dim oPatterns As OccurrencePatterns = oADef.OccurrencePatterns
    Dim oPFolder As BrowserFolder = Nothing
	Dim oNode As BrowserNode
	Dim oOccurrenceNodes As ObjectCollection
    oOccurrenceNodes = ThisApplication.TransientObjects.CreateObjectCollection
    For Each occ As ComponentOccurrence In oOccs
        If occ.Name.Contains(PartName)  And occ.IsPatternElement =False Then
   			oNode = oPane.GetBrowserNodeFromObject(occ)
			oOccurrenceNodes.Add(oNode)
            Logger.Trace("Occurrence found: " & occ.Name)
	Else
	    Logger.Trace("No component found")
        End If
    Next
	For Each pattern As OccurrencePattern In oPatterns
        If pattern.OccurrencePatternElements.Item(1).Occurrences.Item(1).Name.Contains(PartName) Then
			oNode = oPane.GetBrowserNodeFromObject(pattern)
			oOccurrenceNodes.Add(oNode )
	Else
	    Logger.Trace("No component found")
	End If
    Next
    Try
        oPFolder = oPane.TopNode.BrowserFolders.Item(FolderName)
    Catch
        oPFolder = oPane.AddBrowserFolder(FolderName, oOccurrenceNodes)
    End Try
End Function
Message 3 of 3

A_Geistmann
Explorer
Explorer

Thanks, this worked great!

I just fined tuned minor things to work with my complete rule again.

I didn't know about the object collection yet, this is really cool!

0 Likes