Continue to Site

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!

Instance Renaming Macro 1

Status
Not open for further replies.

jzecha

Aerospace
Jan 20, 2016
235
US
I have used a bunch of macros to rename the instances of my parts to the part number.
I have ran into two big issues:
1.I have found macros that will rename every instance no matter how many different levels of products there are, but they fail when they run into an instance name that already exists further down the tree. Like the shown code:
Code:
Sub CATMain()

 Dim documentlist
 Dim MainProduct As Product
 Dim MainDocument As ProductDocument

 Set documentlist = CATIA.Documents
 Set MainDocument = CATIA.ActiveDocument
 Set oTopProduct = MainDocument.Product

 If (InStr(MainDocument.Name, "CATProduct") <> 0) Then
 Call RenameSingleLevelProduct(oTopProduct)
 Else
 MsgBox "Active document should be a Product"
 Exit Sub
 End If

 End Sub

 Sub RenameSingleLevelProduct(oTopProduct)

 Dim ItemToRename As Product
 Dim ItemToRenamePartNumber As String
 Dim lNumberOfItems As Long
 Dim myArray(4000) As String
 Dim i, j, k As Integer

 lNumberOfItems = oTopProduct.Products.Count
 For i = 1 to lNumberOfItems
 myArray(i) = ""
 Next
 For i = 1 to lNumberOfItems
 Set ItemToRename = oTopProduct.Products.Item(i)
 k = 0
 'Rename Instance
 ItemToRenamePartNumber = ItemToRename.PartNumber
 myArray(i) = ItemToRenamePartNumber
 For j = 1 to i
 If myArray(j) = ItemToRenamePartNumber Then
 k = k + 1
 End If
 Next

 ItemToRename.Name = ItemToRenamePartNumber & "." & k
 If (ItemToRename.Products.Count <> 0) Then
 Call RenameSingleLevelProduct(ItemToRename.ReferenceProduct)
 End If
 Next
 End Sub

2.I have also found a macro shown below that has code written into it that prevents it from failing when running into an instance name that is already in use.

Code:
Option Explicit

Dim oTopProductDoc As ProductDocument
Dim oTopProduct As Product
Dim ItemToRename As Product
Dim ItemToRenamePartNumber As String
Dim oDictionary 'Dictionary Object
Dim lNumberOfItems As Long
Dim i As Integer
Dim CatchError As Integer

Sub CATMain()

Set oTopProductDoc = CATIA.ActiveDocument
CatchError = 0

'Document type check
If (InStrRev(oTopProductDoc.Name, ".CATProduct", -1) = 0) Then
 MsgBox "Active Document must be a CATProduct"
 Exit Sub
End If

Set oTopProduct = oTopProductDoc.Product 'The top product containing the items to rename
Set oDictionary = CreateObject("Scripting.Dictionary")

lNumberOfItems = oTopProduct.Products.Count

For i = 1 to lNumberOfItems

 Set ItemToRename = oTopProduct.Products.Item(i)
 '~ ItemToRenamePartNumber = ItemToRename.PartNumber
 
 ItemToRenamePartNumber = ItemToRename.PartNumber

 'See if the item is already in list, in that case increment the suffix number by 1 before renaming
 If oDictionary.Exists(ItemToRenamePartNumber) Then
  oDictionary.Item(ItemToRenamePartNumber) = oDictionary.Item(ItemToRenamePartNumber)+1
 Else 
  oDictionary.Add (ItemToRenamePartNumber) ,"1"
 End If

 'Trap errors because of pre-existing Instance names
 On Error Resume Next
 'Rename the items instance name like this: Part Number + "." + suffix number
 ItemToRename.Name = ItemToRenamePartNumber & "." & oDictionary.Item(ItemToRenamePartNumber)

 If Err.Number <> 0 Then
  CatchError = 1
 End If  
Next

'If there was an error, then repeat the rename to fix it.
If CatchError = 1 Then
 oDictionary.RemoveAll
 RepeatRename()
End If

Set oDict1 = Nothing

End Sub

Sub RepeatRename()

For i = 1 to lNumberOfItems

 Set ItemToRename = oTopProduct.Products.Item(i)
 ItemToRenamePartNumber = ItemToRename.PartNumber

 If oDictionary.Exists(ItemToRenamePartNumber) Then
  oDictionary.Item(ItemToRenamePartNumber) = oDictionary.Item(ItemToRenamePartNumber)+1
 Else 
  oDictionary.Add (ItemToRenamePartNumber) ,"1"
 End If

 ItemToRename.Name = ItemToRenamePartNumber & "." & oDictionary.Item(ItemToRenamePartNumber)

Next

End Sub

Can someone help me combine these two codes together?
I tried, but have quickly gotten lost in the complexity of the second macro.
If this macro exists somewhere else, could someone please point me in the right direction?
 
Replies continue below

Recommended for you

Take a look at this code. I think I started from the same place as you did. My strategy to handle other existing names was to make two passes. First pass: rename everything to a Temporary instance name. Second pass: finally set the instance names. I think there's even a check to avoid processing the same subassembly more than once.

Code:
Public oList As Variant

Option Explicit

Sub CATMain()
On Error Resume Next

'Declarations
Dim oTopDoc As Document
Dim oTopProd As ProductDocument
Dim oCurrentProd As Product
Dim n As Integer

'Check if the active document is an assembly, else exit
Set oTopDoc = CATIA.ActiveDocument

 If oTopDoc Is Nothing Then
        MsgBox "Must have an assembly open"
        Exit Sub
    End If
   
If Right(oTopDoc.Name, 7) <> "Product" Then
    MsgBox "Active document should be a product"
    Exit Sub
End If

'CATIA.RefreshDisplay = False
Set oCurrentProd = oTopDoc.Product

Set oList = CreateObject("Scripting.dictionary")


CATIA.StatusBar = "Working On" & " " & oCurrentProd.Name

Call RenameSingleLevel(oCurrentProd)    'Call the subroutine, it is a recursive loop

'CATIA.RefreshDisplay = True
CATIA.StatusBar = "Done"

End Sub

Private Sub RenameSingleLevel(ByRef oCurrentProd As Product)

On Error Resume Next

'More declarations
Dim ItemToRename As Product
Dim ToRenamePartNumber As String
Dim lNumberOfItems As Long
Dim RenameArray(2000) As String
Dim i As Integer
Dim j As Integer
Dim k As Integer

Set oCurrentProd = oCurrentProd.ReferenceProduct    'You have to work with the "ReferenceProduct" object
lNumberOfItems = oCurrentProd.Products.Count

'For i = 1 To lNumberOfItems                         'Clear out the rename array
'    RenameArray(i) = ""                             'Don't know if this is necessary
'Next

'Run through this loop once, to set everything to a dummy name, to avoid naming conflicts
For i = 1 To lNumberOfItems                             'Cycle through the assembly's children
    Set ItemToRename = oCurrentProd.Products.Item(i)    'Declare which item we are working on
    
    ToRenamePartNumber = ItemToRename.PartNumber        'Get the Part Number
    'ToRenamePartNumber = ItemToRename.DescriptionRef   'Toggle these two lines for testing
            
    RenameArray(i) = ToRenamePartNumber                 'Building the list of part names for the numbering loop
    
    k = 0                                               'Numbering Loop
    For j = 1 To i                                      'This loop checks and sets the instance number
        If RenameArray(j) = ToRenamePartNumber Then
            k = k + 1
        End If
    Next
    CATIA.StatusBar = ItemToRename.Name & " > " & ToRenamePartNumber & "." & k
    'MsgBox ItemToRename.Name & " / " & ToRenamePartNumber & "." & k    'This line is for testing only
    ItemToRename.Name = ToRenamePartNumber & "TEMP." & k    'Set the new instance name, to a TEMP dummy value

Next

'Run through this loop to set the name finally, then the recursion call
For i = 1 To lNumberOfItems
    Set ItemToRename = oCurrentProd.Products.Item(i)
       
    ToRenamePartNumber = ItemToRename.PartNumber        'Toggle these two lines for testing
    'ToRenamePartNumber = ItemToRename.DescriptionRef   'Toggle these two lines for testing
          
    RenameArray(i) = ToRenamePartNumber
    
    k = 0
    For j = 1 To i
        If RenameArray(j) = ToRenamePartNumber Then
            k = k + 1
        End If
    Next
    
    CATIA.StatusBar = ItemToRename.Name & " > " & ToRenamePartNumber & "." & k
    'MsgBox ItemToRename.Name & " / " & ToRenamePartNumber & "." & k    'For testing
    
    ItemToRename.Name = ToRenamePartNumber & "." & k    'Set the new instance name final
    
    If ItemToRename.Products.Count <> 0 Then        'Recursive Call for version 0.1.2
        If oList.exists(ItemToRename.PartNumber) Then GoTo Finish
        If ItemToRename.PartNumber = ItemToRename.ReferenceProduct.Parent.Product.PartNumber Then oList.Add ItemToRename.PartNumber, 1
        Call RenameSingleLevel(ItemToRename)
    End If

Finish:
Next

End Sub
 
This works perfectly from what I can tell.
I appreciate the help, now I have to figure out how to convert this to CATScript.
Why does everyone seem to prefer VBA over CATScript?
I am very new to coding and have run into VBA code responses to all my queries.
 
I think Ferdo's Portable Script Center had a discussion on CATScript vs. CATVBA, which would be more comprehensive than my reply. For my perspective, the big advantage is the built in debugger environment. This is a big help when I don't know what I'm doing and I try to figure it out...like many, I have no real training in this area. Also, some methods are available in VBA that are not available in script. The dictionary object might have been one, but it's actually been a while so I forgot! CATVBA also can transition over into other VBA projects. For example, you can interact with Excel VBA controlling CATIA from Excel or controlling Excel from CATIA.

Even shorter summary: CATScript runs faster and lighter, but CATVBA has more power and more options available.
 
Yea, I have started to dig into his Portable Script Center for answers.
Your response was pretty much what I figured with CATScript vs VBA.
VBA is more powerfull and CATScript is faster.
 
the reason i switched to VB is interface/forms. yea works slower. i can even see that by running titleblock macro that consists of lines and text..
 
When converting VBA to CATScript you need to comment out your item types when you Dim. i.e. Dim oSelection 'As Selection. Also, CATScript does not have functionalities like Collections, dictionaries, or GoTo (except for error handling). The nice thing about VBA is the debugging...with CATScript you have to put message boxes throughout your code to try and figure out errors, not so with VBA. Also, VBA offers intellisence where, if you dim a variable with a type, it will automatically expose the methods and properties for that type of variable.

Forgot to add...so you cannot use the second code in CATScript because it utilizes a dictionary.
 
Hello. I need help with VBA macro to change the instance name for the part number. my proble is that the tree instance name is like this:
PRT-00004256(PRT-00004256.1.1.2)
So i want to change that for my name file that it is: CEL14086020 but every time i try to run the macro it crash because the .1.1.2. i try your macro but donsn't work eader. do you have any solution for that? thank you for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top