独立于 CATIA 中的子程序将计数代码修改为 运行
Modifying a count code to run independently from a sub in 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 来为一个不存在的用户创建一个新用户 属性。 returns 一个错误,指出程序需要一个 =。非常感谢任何帮助。
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 returns 一个 属性 对象。尝试像这样使用它:
' 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
我正在努力修改大量现有代码,在尝试完成这些代码后,我对我所了解的 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 来为一个不存在的用户创建一个新用户 属性。 returns 一个错误,指出程序需要一个 =。非常感谢任何帮助。
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 returns 一个 属性 对象。尝试像这样使用它:
' 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