Cancel
Showing results for 
Search instead for 
Did you mean: 

Sorting through occurrences

Pioneer
Pioneer

Hello,

I have a large assembly and I need to sort through the occurrences. I'm currently using the recursive method. A simplified example given below:

 

Dim  objOccs As SolidEdgeAssembly.Occurrences

...

For i = 1 to objOccs.count

If objOccs.item(i).Filename = "part.par"

   do stuff

End if 

 

I don't necessarily need to sort through every occurrence. This can be very time consuming. I only need to perform actions on certain occurrences that meet certain criteria. Is it possible to search for an occurrence so that not as much time is spent evaluating information from every single occurrence in the assembly? Referencing the occurrence's order in the pathfinder directly, i.e objOccs.item(3) works quickly. However, if the order of occurences is changed my program wouldn't work anymore. Sorry if this is a bit open-ended. Any input is greatly  appreciated! 

4 REPLIES 4

Re: Sorting through occurrences

Solution Partner Genius Solution Partner Genius
Solution Partner Genius

Hi,

If your list of Occurrences is not going to change while running your program you could create a list at the beginning and use it to search occurrences very quickly without going through the Occurrences collection again.

If it changes, you should be updating the list accordingly.

 

In this example I created a class "myOccurrence" with only the info i need. It is just 2 strings, not heavy at all.

'In myOccurrence Class I only store the info I need. In this example, edgebar name and filename 
Public Class myOccurrence
    Public myName As String
    Public myFilename As String

    Public Sub New(myOcc As SolidEdgeAssembly.Occurrence)
        myName = myOcc.Name
        myFilename = myOcc.OccurrenceFileName
    End Sub
End Class

I use this routine to create the List:

    'Create a list with objects type "myOccurrence"
    Private Function createOccList(oasm As SolidEdgeAssembly.AssemblyDocument) As List(Of myOccurrence)
        Dim l As New List(Of myOccurrence)

        For Each oOcc As SolidEdgeAssembly.Occurrence In oasm.Occurrences
            l.Add(New myOccurrence(oOcc))
        Next
        Return l
    End Function
End Class

I use and filter the list during my program and access the real Occurrences by name:

 

 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim oSE As SolidEdgeFramework.Application = GetObject(, "solidedge.application")
        Dim oASM As SolidEdgeAssembly.AssemblyDocument = oSE.ActiveDocument

        'Create the list only once at the beginning of your program.
        Dim loCCs As List(Of myOccurrence) = createOccList(oASM)

        'You can query for a subset of those objects that satisfy a condition.
        'Example 1: Occurrences which edgebarname start with "part1"
        Dim query = From oneOcc In loCCs Where oneOcc.myName Like "part1*"
        ' Another example:
        'Occurrences which filename end with ".par"
        'Dim query = From oneOcc In loCCs Where oneOcc.myFilename.EndsWith(".par")

        If query.Count > 0 Then
            'You found at least one.
            For Each occ As myOccurrence In query
                'You can reference a real Occurrence in your assembly by name.
                Dim myOccurrence As SolidEdgeAssembly.Occurrence = oASM.Occurrences.Item(occ.myName)
                '... do your stuff in myOccurrence
            Next
        End If
    End Sub

(Examples not tested)

Hope this helps,

Julian

Julian Guillo
Pixel Sistemas SL
Spain

Re: Sorting through occurrences

Solution Partner Genius Solution Partner Genius
Solution Partner Genius

mmm... I answered this yesterday but it seems to have disappeared my answer...

Maybe I didn't save it correctly. Anyway:

 

You can set a list of objects with the info you need. In this case I created a class which stores edgebarname and filename:

'In myOccurrence Class I only store the info I need. In this example, edgebar name and filename 
Public Class myOccurrence
    Public myName As String
    Public myFilename As String

    Public Sub New(myOcc As SolidEdgeAssembly.Occurrence)
        myName = myOcc.Name
        myFilename = myOcc.OccurrenceFileName
    End Sub

Then, at the beginning of your program you can fill this list going through the occurrences with a function like this:

    'Create a list with objects type "myOccurrence"
    Private Function createOccList(oasm As SolidEdgeAssembly.AssemblyDocument) As List(Of myOccurrence)
        Dim l As New List(Of myOccurrence)

        For Each oOcc As SolidEdgeAssembly.Occurrence In oasm.Occurrences
            l.Add(New myOccurrence(oOcc))
        Next
        Return l
    End Function

This would be your program using those above:

Having the info in a list is very quick to find, and not heavy to store (it is an object with 2 strings)

You can filter, query and access the Occurrence objects with the stored names:

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim oSE As SolidEdgeFramework.Application = GetObject(, "solidedge.application")
        Dim oASM As SolidEdgeAssembly.AssemblyDocument = oSE.ActiveDocument

        'Create the list only once at the beginning of your program.
        Dim loCCs As List(Of myOccurrence) = createOccList(oASM)

        'You can query for a subset of those objects that satisfy a condition.
        'Example 1: Occurrences which edgebarname starts with "part1"
        Dim query = From oneOcc In loCCs Where oneOcc.myName Like "part1*"
        'Example 2:
        'Occurrences which filename ends with ".par"
        'Dim query = From oneOcc In loCCs Where oneOcc.myFilename.EndsWith(".par")

        If query.Count > 0 Then
            'You found at least one.
            For Each occ As myOccurrence In query
                'You can reference a real Occurrence in your assembly by name.
                Dim myOccurrence As SolidEdgeAssembly.Occurrence = oASM.Occurrences.Item(occ.myName)
                '... do your stuff in myOccurrence
            Next
        End If
    End Sub
Julian Guillo
Pixel Sistemas SL
Spain

Re: Sorting through occurrences

Pioneer
Pioneer

Julian,
Thank you for you response. I'm wondering what kind of variable 'query' is in your private sub Form1_Load. Also, I don't see where oneOcc is declared. Does this need to be declared? Is this in vb.net. I'm writing in visual basic.

Thank you

Re: Sorting through occurrences

Solution Partner Genius Solution Partner Genius
Solution Partner Genius

Hi,

This works in Vb.Net, not in VB6. (I understand you are working with Visual Basic 6)

The example is using the "LINQ" functionallity. 

See:

https://docs.microsoft.com/es-es/dotnet/visual-basic/programming-guide/language-features/linq/introd...

 

It is not necessary to declare the variable Query or OneOcc.

It is just the syntax of the command. In fact OneOcc is not a variable, just an identifier inside the command.

 

Anyway, the purpose of the thread is to speed up the process while going through occurrences, the way that you filter the Occurrences collection is another thing. You could use Linq, or use a function that loops through the list, or any other method.

The advantage will be taken from getting only the info you need from Occurrences and process it as an in-memory list of objects. 

 

IMHO I would consider starting working with VB.Net instead VB6.

It is easier when you need to create classes, design forms, etc... although accessing COM technology is a little more tricky.

 

Hope this helps.

Julian

 

Julian Guillo
Pixel Sistemas SL
Spain