问题
I'm working on modifying a lot of existing code and after attempting to go through it, I'm way in over my head from what I know about VBA. My coding experience is primarily in Python and I'm having difficulty wrapping my head around the object structure and what is and isn't acceptable in VBA.
I'm attempting to modify user added properties (Add Additional Properties under the Properties menu) on a given item that is chosen by the user. This code, as a stand alone will do what I'm looking for, integrating it into my existing code is proving difficult though. How do I modify the following code to being something that I can use so that it doesn't have to be in it's own sub?
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
This is my attempt so far and it appears to get ignored when I put it into our current text. The plan is to replace the current way we modify the existing properties so that it's able to read through CATIA trees and modify individual parts and products. Additionally, I attempted to add a createstring to make a new user property for one that is not there. This returns an error saying that the program is expecting a =. Any help is greatly appreciated.
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
回答1:
Looks like CreateString returns a property object. Try using it like this:
' 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
来源:https://stackoverflow.com/questions/61484776/modifying-a-count-code-to-run-independently-from-a-sub-in-catia