从选择中获取零件号CATIA VBA的方法?错误:灾难性故障

huangapple go评论82阅读模式
英文:

How to the Part Number from Selection CATIA VBA? Error: Catastrophic failure

问题

我想从“Selection”中获取“零件号”。我猜因为我从Enovia5获取信息,所以我遇到了一个问题:有些可以获取“零件号”,有些返回“灾难性故障”。我确保选择始终只有1个项目。

CATIA.ActiveDocument.Selection.Item(1).LeafProduct.PartNumber ~有时有效,有时无效。

注意:我使用“TypeName”来检查所选文件的类型,所有返回“Document”,但返回错误的文件实际上在查看其属性时都有“.CATPart”。如果尝试将其声明为“Part”或“PartDocument”,将返回错误不匹配。

Dim myPart as Part
Set myPart = CATIA.ActiveDocument.Selection.Item(1).value ~不匹配
Set myPart = CATIA.ActiveDocument.Selection.Item(1).LeafProduct ~不匹配

Dim myPartDoc as PartDocument
Set myPart = CATIA.ActiveDocument.Selection.Item(1).value ~不匹配
Set myPart = CATIA.ActiveDocument.Selection.Item(1).LeafProduct ~不匹配

英文:

I would like to get the Part Number from Selection. I guess because I get the info from Enovia5 so I encounter an issue: some I can get the Part Number and some return Catastrophic failure. I ensure the selection always has 1 item only.

   CATIA.ActiveDocument.Selection.Item(1).LeafProduct.PartNumber '<~~~ sometimes works, sometimes does not. 

Note: I use TypeName to check what type of file gets selected, all return Document but the ones return error actually have .CATPart when I view their properties. Error mismatch will return if I try to declare it as Part or PartDocument.

Dim myPart as Part
Set myPart = CATIA.ActiveDocument.Selection.Item(1).value '<~~~ Mismatch
Set myPart = CATIA.ActiveDocument.Selection.Item(1).LeafProduct'<~~~ Mismatch

Dim myPartDoc as PartDocument
Set myPart = CATIA.ActiveDocument.Selection.Item(1).value '<~~~ Mismatch
Set myPart = CATIA.ActiveDocument.Selection.Item(1).LeafProduct'<~~~ Mismatch

答案1

得分: 2

LeafProduct 不返回 Part 或 PartDocument,它返回 Product 对象。在你的示例中,你正在使用错误类型的对象填充变量。

Product 对象类似于包围 Part 或 Product(类似于 Catia 中的装配体)的容器。

可能会出现几个问题。首先,我建议尝试单独使用选择对象。

Dim oDoc As Document
Set oDoc = CATIA.ActiveDocument

Dim oSel as Selection
Set oSel = oDoc.Selection

If oSel.Count = 1 Then
    Dim oProd as Product
    Set oProd = oSel.Item2(1).LeafProduct

    MsgBox oProd.PartNumber
Else
    MsgBox "选择了更多对象!!!"
End If

然后,如果你想从该产品中获取 Part 对象,并且确信它是包围部件而不是 Product(装配体)的产品,你可以轻松地这样获取:

Dim oPartDocument As PartDocument
Set oPartDocument = oProd.ReferenceProduct.Parent

Dim oPart As Part
Set oPart = oPartDocument.Part

在你的 Catia 安装中,有一个文件可以帮助你了解如何使用 API 进行自动化。这是自动化的文档。你可以在这里找到它:

从选择中获取零件号CATIA VBA的方法?错误:灾难性故障

英文:

LeafProduct does not return Part nor PartDocument. It returns Product object. In your example, you are filling variables with objects of wrong type.

Product object is something like container around Part or Product(Product like assembly in catia)

There can be several problems. At first, I'd try to work with selection object separately.

Dim oDoc As Document
Set oDoc = CATIA.ActiveDocument

Dim oSel as Selection
Set oSel = oDoc.Selection

If oSel.Count = 1 Then
    Dim oProd as Product
    Set oProd = oSel.Item2(1).LeafProduct

    MsgBox oProd.PartNumber
Else
    MsgBox "More objects selected!!!"
End If

Then if you want to get Part object from this product and you are sure it is product which is container around part and not around Product(Assembly), you can get it easily like that:

Dim oPartDocument As PartDocument
Set oPartDocument = oProd.ReferenceProduct.Parent

Dim oPart As Part
Set oPart = oPartDocument.Part    

In your catia installation is file which can helps you to understand how to use API for automation. It is documentation for automation. You can find it here:

从选择中获取零件号CATIA VBA的方法?错误:灾难性故障

huangapple
  • 本文由 发表于 2023年3月7日 08:27:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定