vbacatia

How to activate the specific part in CATIA assembly using VBA


This is something I am trying for very long, but not able to do.

There is assembly (product) is CATIA sessions |-Main Product |-SubProduct1 (level1) |- Part 1 |-SubProduct2 (level1) |-SubProduct3 (level2) |- Part 2

I want to make part1 and part2 active (not just select) so that I can make changes in assembly mode. I know, I can make only one of them active at given moment.

I tried partDocument.Activate, but this does not work.

Dim documents1 As documents
Set documents1 = CATIA.documents

Dim partDocument1 As PartDocument
Set partDocument1 = documents1.item("SampleBottomBlock_ref.CATPart")
partDocument1.Activate

Is this possible using VBA?

I want to activate because I am handling selection for some operations like -

Dim partDocument1 As PartDocument
    Set partDocument1 = partDoc
    
    Dim part1 As part
    Set part1 = partDocument1.part
    Dim selection1 As selection
    Set selection1 = partDocument1.selection
    selection1.Clear
    Dim hybridBodies1 As HybridBodies
    Set hybridBodies1 = part1.HybridBodies
    
    Dim hybridBody1 As HybridBody
    Set hybridBody1 = hybridBodies1.item("External References")
    
    Dim hybridShapes1 As HybridShapes
    Set hybridShapes1 = hybridBody1.HybridShapes
    
    Dim myDict As Object
    Set myDict = CreateObject("Scripting.Dictionary")
    
    For Each Shape In hybridShapes1
        If myDict.exists(Shape.name) Then
            selection1.Add Shape
        Else
            myDict.Add Shape.name, Shape.name
        End If
    Next
    
    For Each Shape In hybridBody1.HybridBodies
        If myDict.exists(Shape.name) Then
            selection1.Add Shape
        Else
            myDict.Add Shape.name, Shape.name
        End If
    Next
    
    selection1.Delete
    selection1.Clear
    part1.Update

Solution

  • To "activate" a part instance in a product structure, select the part in the tree and switch the Workbench. (check first if assembly workbench is active)

    CATIA.StartWorkbench("PrtCfg")
    

    But the selection still refers to the root document which is open in the active window.
    Also the ActiveDocument doesn't change, is is still the root document.