Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

CATIA VBA: Instance rename -multi level

Status
Not open for further replies.

ProgNovice20

Aerospace
Sep 19, 2016
6
GB
Hello, I am new catia vba user and I have tried to find a solution to my problem on this forum and elsewhere but to no avail. I am sure it's a simple problem but it has me stumped.

I have written a code using VBA to rename the instance names and part numbers in a catia product. It does this for all the products within the top level product and all the parts within the individual products. The code works fine except for renaming the instance names in level 2 of the product tree. Eg.:

Code:
oprods.item(1).name = "Top level instance"
works perfectly. But
Code:
oprods.item(1).products.item(1).name = "2nd level instance"
doesn't do anything at all.

It's weird because i can change the part number in the level 2 easily using
Code:
oprods.item(1).products.item(1).partnumber = "2nd level part number"

Any guidance will be appreciated.

Thanks.
 
Replies continue below

Recommended for you

@drewmumaw

Thanks for this. It works but I don't understand why my method doesn't. Would you be kind enough to shed light on this? I am genuinely curious.

Let's say i don't want loops, and i only want to change a specific instance name of a specific part within a product. Why doesn't the below work?

Code:
oprods.item(1).products.item(1).name = "2nd level instance"

Thanks again.
 
what is oprods? what is oprods.item(1)? and finally what is oprods.item(1).products.item(1)

please give info like CATProduct, CATPart or Component

Eric N.
indocti discant et ament meminisse periti
 
@itsmyjob

Code:
Dim oProdDoc As ProductDocument
Set oProdDoc = CATIA.ActiveDocument
Dim oProd As Product
Set oProd = oProdDoc.Product
Dim oProds As Products
Set oProds = oProd.Products

oprods.item(1) is the first product in the CATIA tree. oprods.item(1).products.item(1) is the first part within the product.

I can change the part number of the part using oprods.item(1).products.item(1).partnumber = "Some part number" but I can't change the instance name using the same logic (using .name instead of .partnumber). It doesn't give any error. It just doesn't do it and skips it.
 
To give you a better idea of what I was trying to do, here is my full original code before @drewmumaw provided me with his code:

Code:
Sub CATMain()
Set CATIA = GetObject(, "CATIA.application")
'Msg.Show
GetNextNode CATIA.ActiveDocument.Product
'Unload Msg
End Sub

Sub GetNextNode(oCurrentProd As Product)
'On Error Resume Next
Set oProdDoc = CATIA.ActiveDocument
oCurrentProd.ApplyWorkMode DESIGN_MODE
Set oProd = oProdDoc.Product
Set oProds = oProd.Products

Dim oCurrentTreeNode As Product

Dim instnosarray(2000) As String

For i = 1 To oCurrentProd.Products.Count
    Set oCurrentTreeNode = oCurrentProd.Products.Item(i)
    oitem = InStr(1, oCurrentTreeNode.Name, "ITEM", vbBinaryCompare)
    oitm = InStr(1, oCurrentTreeNode.Name, "ITM", vbBinaryCompare)
    y = Mid(oCurrentTreeNode.PartNumber, 1, 14)
    If oitem > 0 Then
        x = Mid(oCurrentTreeNode.Name, oitem, 8)
        x2 = Mid(x, 5, 4)
    ElseIf oitm > 0 Then
        x = Mid(oCurrentTreeNode.Name, oitm, 7)
        x2 = Mid(x, 4, 4)
    Else
        x = oCurrentTreeNode.Name
        x2 = x
    End If
    instnosarray(i) = x
    k = 0

    For j = 1 To i
         If instnosarray(j) = x Then
            k = k + 1
        End If
    Next


    oCurrentTreeNode.Name = x & "." & k             --------------Doesn't do anything
    oCurrentTreeNode.PartNumber = y & x2            --------------Works fine

    If oCurrentTreeNode.Products.Count > 0 Then
        GetNextNode oCurrentTreeNode
    End If

Next

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top