vbacatia

Check Selection for Part or empty Product in Assembly


I want to know if I have selected a CATPart or a CATProduct in an assembly.

I couldn't find a suitable check to differentiate between an empty product like "Product4__" and "Part1_ASD2_". That is because either one, if selected, will result in Products.Count = 0 and TypeName()="Product"

Selcetion of "Product1__", "Part1" "Product3__" result in correct differentiations to CATPart or CATProduct

Catia Assembly Tree Structure

I tried to find a way to get the child of the selection but only managed to get the child of a Product. But when a product does not have a child I am at the same problem as before, where "Product4__" and "Part1_ASD2_" behave the same.

A question I have for better understanding the Structure is how "Part1" is referenced in "Part1_ASD_"? Maybe that can help me resolve this.
Otherwise is there a difference between an empty product like "Product4__" and a part like "Part1_ASD2_" which I can use?

Sub CatiaCheckSelectionType()

    msgBox("Startet")
    
    Set oSelection = CATIA.ActiveDocument.Selection
        if oSelection.Count = 1 Then

            msgBox(TypeName(oSelection.Item(1).Value))

            if TypeName(oSelection.Item(1).Value) = "Product" Then
                msgBox("Maybe a Product")
                Set CATProduct = oSelection.Item(1).Value
                If CATProduct.Products.Count > 0 Then
                    Set CATProductChild = CATProduct.Products.Item(1)
                    If TypeName(CATProductChild) = "Product" Then
                        msgBox ("Definetly a Product")
                    ElseIf TypeName(CATProductChild) = "Part" Then
                        msgBox ("Part")
                    Else
                        msgBox ("something else in here"))
                    EndIf
                Else
                    msgBox("Empty Produkt")
                EndIf
            ElseIf TypeName(oSelection.Item(1).Value) = "Part" then
                Set CATPart = oSelection.Item(1).Value
                msgBox("En Part it is")
            End if
        Else
            msgBox("Pls Select a Part or a Product")
        EndIf
End Sub

Solution

  • You can use ReferenceProduct to switch from the instance to the product in its document.

        Set oSelection = CATIA.ActiveDocument.Selection
        if oSelection.Count = 1 Then
            if TypeName(oSelection.Item(1).Value) = "Product" Then
                Set CATProduct = oSelection.Item(1).Value
                Set oRefProduct = CATProduct.ReferenceProduct
                if TypeName(oRefProduct.Parent) = "ProductDocument" then 'switch to the document
                    MsgBox ("It's an Instance of a Product or a Component")
                elseif TypeName(oRefProduct.Parent) = "PartDocument" then           
                    msgBox ("It's an Instance of a Part")
                end if           
            ElseIf TypeName(oSelection.Item(1).Value) = "Part" then
                Set CATPart = oSelection.Item(1).Value
                msgBox("En Part it is")
            End if
        Else
            msgBox("Pls Select a Part or a Product")
        End If