【发布时间】:2020-04-28 16:09:48
【问题描述】:
我正在修改大量现有代码,在尝试完成之后,我对 VBA 的了解完全超出了我的想象。我的编码经验主要是在 Python 中,我很难理解对象结构以及 VBA 中可接受和不可接受的内容。
我正在尝试在用户选择的给定项目上修改用户添加的属性(“属性”菜单下的“添加其他属性”)。这段代码,作为一个独立的,将做我正在寻找的,将它集成到我现有的代码中是困难的。如何将以下代码修改为我可以使用的东西,这样它就不必在它自己的子代码中了?
Sub CATMain()
GetNextNode CATIA.ActiveDocument.Product
End Sub
Sub GetNextNode(oCurrentProduct As Product)
Dim oCurrentTreeNode As Product
Dim i As Integer
' Loop through every tree node for the current product
For i = 1 To oCurrentProduct.Products.Count
Set oCurrentTreeNode = oCurrentProduct.Products.Item(i)
' Determine if the current node is a part, product or component
If IsPart(oCurrentTreeNode) = True Then
MsgBox oCurrentTreeNode.PartNumber & " is a part"
ElseIf IsProduct(oCurrentTreeNode) = True Then
MsgBox oCurrentTreeNode.PartNumber & " is a product" & i
Else
MsgBox oCurrentTreeNode.PartNumber & " is a component"
End If
' if sub-nodes exist below the current tree node, call the sub recursively
If oCurrentTreeNode.Products.Count > 0 Then
GetNextNode oCurrentTreeNode
End If
If oCurrentTreeNode.Products.Count = 0 Then
oCurrentTreeNode.ReferenceProduct.UserRefProperties.Item(1).Value = "Yippee!!!!!"
End If
Next
End Sub
这是我迄今为止的尝试,当我将它放入当前文本时,它似乎被忽略了。该计划是替换我们修改现有属性的当前方式,以便它能够读取 CATIA 树并修改单个零件和产品。此外,我尝试添加一个 createstring 来为不存在的用户创建一个新的用户属性。这将返回一个错误,指出程序需要一个 =。非常感谢任何帮助。
Dim oCurrentProduct As Product
Dim oCurrentTreeNode As Product
Dim i As Integer
' Loop through every tree node for the current product
For i = 1 To oCurrentProduct.Products.Count
Set oCurrentTreeNode = oCurrentProduct.Products.Item(i)
' Determine if the current node is a part, product or component
If IsPart(oCurrentTreeNode) = True Then
MsgBox oCurrentTreeNode.PartNumber & " is a part"
ElseIf IsProduct(oCurrentTreeNode) = True Then
MsgBox oCurrentTreeNode.PartNumber & " is a product" & i
Else
MsgBox oCurrentTreeNode.PartNumber & " is a component"
End If
' if sub-nodes exist below the current tree node, call the sub recursively
If oCurrentTreeNode.Products.Count > 0 Then
GetNextNode oCurrentTreeNode
End If
If oCurrentTreeNode.Products.Count = 0 Then
oCurrentTreeNode.ReferenceProduct.UserRefProperties.Item(1).Value = "Yippee!!!!!"
oCurrentTreeNode.ReferenceProduct.UserRefProperties.CreateString(Value, "Input")
End If
Next
【问题讨论】:
-
为什么只在没有子产品的情况下设置值?您不想在确定对象类型的 IF 分支中设置属性设置代码吗?例如,您只想为零件、产品或组件设置属性?
-
啊,很好。我非常专注于尝试使输入起作用,以至于我没有意识到这一点。谢谢!
-
我重读了你写的东西。无论父母或孩子拥有什么,我都希望只能分配给 1 个产品。