在CATIA中将计数代码修改为独立于子系统运行



我正在修改许多现有的代码,在尝试完成它之后,我对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

看起来CreateString返回了一个属性对象。试着这样使用:

' Use this line where you want to make a new property
SetUserProperty oCurrentTreeNode.ReferenceProduct, "Input", "Yippee!!!!!"

Public Sub SetUserProperty(ByVal myProduct As Product, ByVal code as String, ByVal newValue as String)
Dim myUserProperties As Object 'As Parameters
Dim myUserProperty As StrParam
Set myUserProperties = myProduct.UserRefProperties
Set myUserProperty = myUserProperties.CreateString(code, "")
myUserProperty.ValuateFromString newValue
End Sub 

最新更新