Search code examples
vbacatia

How to get "Instance name" of the selected parts in tree


I'm writing macro that will make BOM list from the selected parts in assembly.

I can get a "Part Number" of the part in assembly, I can't get a "Instance name" of the selected parts.

Here code that call Selection tab and then try to get a names.

Set ItemSelection = CATIA.ActiveDocument.Selection   
InputObjectType(0) = "Part"
SelectionStatus = ItemSelection.SelectElement3(InputObjectType, "Choose parts", false, CATMultiSelTriggWhenUserValidatesSelection, true) 
         If SelectionStatus = "Cancel" Then 
            Exit Sub
        End If

        If ItemSelection.Count >= 1000 Then
            MsgBox "You select more then 1000 parts.", vbExclamation, MsgTextBox & "."
            Exit Sub
        End If

        For i = 1 To ItemSelection.Count 
            k = k + 1
            BOMTable(1,k) = ItemSelection.Item(i).PartNumber
            BOMTable(2,k) = ItemSelection.Item(i).Value.Name
            MsgBox BOMTable(1,k)
        Next

What I do wrong?


Solution

  • You need to select Products if you want instance-ness. So...

    InputObjectType(0) = "Product"
    ...
    sInstanceName = ItemSelection.Item(i).Value.Name
    

    What happens when someone selects an Assembly/Sub-Assembly? Nothing different because Sub-Assemblies have instance names too.

    However, if you want to include ONLY actual CATParts, then you have to filter value post-selection something like...

    Dim oInstProd as product
    set oInstProd = ItemSelection.Item(i).Value
    if TypeName(oInstProd.ReferenceProduct.Parent) = "PartDocument" Then
    .... do stuff with only parts...
    end If
    

    The ReferenceProduct property will give you trouble if you use cache mode (it will throw an error). But their is a workaround for that if you need it.