0

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

aaron
  • 81
  • 1
  • 10
  • Why are you only setting the value if there are no child products? Don't you want your property set code up in the IF branches where the type of object is determined? For example, you want to set properties for only parts, or products, or components? – HackSlash Apr 28 '20 at 16:29
  • Ahh, good catch. I got so focused on trying to make the inputs work that I didn't catch that. Thank you! – aaron Apr 28 '20 at 16:37
  • I reread what you wrote. I want to be able to only assign to 1 product, regardless of what the parent or child have. – aaron Apr 28 '20 at 17:14

1 Answers1

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 
HackSlash
  • 4,944
  • 2
  • 18
  • 44
  • I have to say, I know nothing about how subs operate with what you just gave me. I understand what's going on and didn't realize that they could be utilized this way. – aaron Apr 28 '20 at 16:54
  • Oh, this is what I mean when I say break your problem down in to parts. This is procedural programming. Your CATMain should read like a list of steps, each step as a Sub or Function. When everything is broken down in to parts like this there should be no duplicated code. – HackSlash Apr 28 '20 at 18:51
  • I scrubbed all our code and I'm rewriting it my way using the code you've helped with. This program is over 10 years old and the guy that wrote it had..... many..... redundancies in the coding. I don't fully understand how you bring up the code, such as the SetUserProperty line for creating a new property. I'm assuming the code in the () after the sub is set is the parameter declaration and then you type in the name and attributes for it to run? – aaron Apr 28 '20 at 19:03
  • Is it possible to modify SetUserPropery CATIA.ActiveDocument.product to target the root level? The current code will only createstring and modify on sublevels. Also, how do you put code in comments? Everything I tried based on the mini-markdown formatting doesn't work. – aaron Apr 30 '20 at 17:40