【问题标题】:How to get "Instance name" of the selected parts in tree如何获取树中选定零件的“实例名称”
【发布时间】:2019-07-09 23:19:16
【问题描述】:

我正在编写宏,它将根据装配中的选定零件制作 BOM 列表。

我可以获得装配中零件的“零件编号”,但无法获得所选零件的“实例名称”。

这里的代码调用选择选项卡,然后尝试获取名称。

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

我做错了什么?

【问题讨论】:

    标签: vba catia


    【解决方案1】:

    如果您需要实例,您需要选择产品。 所以...

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

    当有人选择一个装配体/子装配体时会发生什么?没有什么不同,因为子程序集也有实例名称。

    但是,如果您只想包含实际的 CATPart,则必须在选择后过滤值,例如...

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

    如果您使用缓存模式,ReferenceProduct 属性会给您带来麻烦(它会引发错误)。但如果您需要,它们是一种解决方法。

    【讨论】:

    • 谢谢!这很好用。关于错误,我在代码中有一个错误处理程序。
    猜你喜欢
    • 2019-07-08
    • 2013-08-22
    • 1970-01-01
    • 2013-04-13
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多