Search code examples
vbaperformancecatia

Isolate parts in the document


I have this code for catia v5 and it works fine. The only problem is that when you do "selection1.Add" it goes through all the components of the document whether they are visible or not, which makes the code slow when executing this line. Does anyone have a suggestion to improve?

I thought about filtering the "invertedSelection" only with visible parts within visible products, that is, if a product is visible, look for visible parts in that product. If you have visible parts in this visible product, add them to "InvertedSelection". If the product is not visible, do not look for parts inside the product.

Sub CATMain()

Dim selection1 As Selection
Set selection1 = CATIA.ActiveDocument.Selection

Dim visPropertySet1 As VisPropertySet
Set visPropertySet1 = selection1.VisProperties

If selection1.Count > 0 Then

    ' Retrieve the Groups collection
    Dim cGroups As Object
    Set cGroups = CATIA.ActiveDocument.product.GetTechnologicalObject("Groups")

    ' Create a group with selected products
    Dim oGroup As Group
    Set oGroup = cGroups.AddFromSel

    ' Fill the selection with the inverted group
    oGroup.FillSelWithInvert


    visPropertySet1.SetShow 1
    
    
    ' Store the inverted selection in a collection
    Dim invertedSelection As New Collection
    Dim i As Integer
    For i = 1 To selection1.Count
        invertedSelection.Add selection1.Item(i).Value
    Next i

    ' Clear the selection
    selection1.Clear


    UserForm1.Show
    

    ' Code will continue here while the UserForm is open
    Do While UserForm1.Visible
        DoEvents
    Loop

    ' Make the components in the inverted selection visible again
    For i = 1 To invertedSelection.Count
        selection1.Add invertedSelection.Item(i)
    Next i

    Dim visPropertySet2 As VisPropertySet
    Set visPropertySet2 = selection1.VisProperties

    visPropertySet2.SetShow 0

    selection1.Clear

Else

    MsgBox "Nenhum componente selecionado", vbExclamation, "Isolate Components"
    Exit Sub

End If

End Sub

Solution

  • To improving performance of working with the selection you can switch off the highlighting of the selected elements.

    'switch highlighting off
    Catia.HSOSynchronized = false
    
    'do stuff with the selection
    
    'turn highlighting on (e.g. end of your code)
    Catia.HSOSynchronized = true