[Macro Catia ] Rename all instances and references
[Macro Catia ] Rename all instances and references
(OP)
Hello ,
I try to write a macro to rename all instances and references in an assembly.
I find some subjects in this forum about the same problem but I can't rename all the assembly tree.
This is the macro :
***************
Sub CATMain()
Dim Input1 , Input2 As string
Set productDocument1 = CATIA.ActiveDocument
If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
MsgBox "Error: Active CATIA Document is not a Product", , msgboxtext
Exit Sub
End If
Input1 = InputBox("Old string ","Modify name")
If (Input1=False) Then
Exit Sub
Else
Input2= InputBox("Replace <"&Input1&"> with :","Modify name")
End If
If (Input2=False) Then
Exit Sub
Else
Call ReplaceString(productDocument1.Product)
End If
End Sub
Sub ReplaceString(actif_prod)
If actif_prod.Products.Count > 0 Then
actif_prod.ReferenceProduct.PartNumber = Replace(actif_prod.ReferenceProduct.PartNumber,Input1,Input2)
actif_prod.Name = Replace(actif_prod.Name,Input1,Input2)
For i = 1 To actif_prod.Products.Count
Call ReplaceString(actif_prod.Products.Item(i))
Next
Else
actif_prod.ReferenceProduct.PartNumber = Replace(actif_prod.ReferenceProduct.PartNumber,Input1,Input2)
actif_prod.Name = Replace(actif_prod.Name,Input1,Input2)
End If
End Sub
***********************
I have a tree like this :
1.documentname
--1.1.PartNumber (partname 1) ---> simple part
-----1.1.1 PartNumber
--1.2.PartNumber (Name) ---> product (sub-assembly)
-----1.2.1PartNumber (partName 2)
---------3.1.1 PartNumber
The macro can't rename partName 2. It can rename all others.
Can someone help me please ?
Thanks a lot.
I try to write a macro to rename all instances and references in an assembly.
I find some subjects in this forum about the same problem but I can't rename all the assembly tree.
This is the macro :
***************
Sub CATMain()
Dim Input1 , Input2 As string
Set productDocument1 = CATIA.ActiveDocument
If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
MsgBox "Error: Active CATIA Document is not a Product", , msgboxtext
Exit Sub
End If
Input1 = InputBox("Old string ","Modify name")
If (Input1=False) Then
Exit Sub
Else
Input2= InputBox("Replace <"&Input1&"> with :","Modify name")
End If
If (Input2=False) Then
Exit Sub
Else
Call ReplaceString(productDocument1.Product)
End If
End Sub
Sub ReplaceString(actif_prod)
If actif_prod.Products.Count > 0 Then
actif_prod.ReferenceProduct.PartNumber = Replace(actif_prod.ReferenceProduct.PartNumber,Input1,Input2)
actif_prod.Name = Replace(actif_prod.Name,Input1,Input2)
For i = 1 To actif_prod.Products.Count
Call ReplaceString(actif_prod.Products.Item(i))
Next
Else
actif_prod.ReferenceProduct.PartNumber = Replace(actif_prod.ReferenceProduct.PartNumber,Input1,Input2)
actif_prod.Name = Replace(actif_prod.Name,Input1,Input2)
End If
End Sub
***********************
I have a tree like this :
1.documentname
--1.1.PartNumber (partname 1) ---> simple part
-----1.1.1 PartNumber
--1.2.PartNumber (Name) ---> product (sub-assembly)
-----1.2.1PartNumber (partName 2)
---------3.1.1 PartNumber
The macro can't rename partName 2. It can rename all others.
Can someone help me please ?
Thanks a lot.





RE: [Macro Catia ] Rename all instances and references
See code below for renaming all of the instance names in an assembly. Hopefully this helps as a reference.
CODE --> catvba
Option Explicit 'ensures all variables have been declared 'declare global variables Dim objActiveProductDoc As ProductDocument Dim objCurrentProduct As Product Dim strList() As String Dim lngLstCtr As Long Dim lngIntCtr As Long Dim lngQtyCtr As Long Dim objTempCurrentProduct As Product Dim strTempList() As String Dim lngTempLstCtr As Long Dim lngTempIntCtr As Long Dim lngTempQtyCtr As Long Public Sub CATMain() 'initialize global variables Set objActiveProductDoc = Nothing Set objCurrentProduct = Nothing lngLstCtr = -1 lngIntCtr = 0 lngQtyCtr = 0 Set objTempCurrentProduct = Nothing lngTempLstCtr = -1 lngTempIntCtr = 0 lngTempQtyCtr = 0 On Error Resume Next 'tell the processing to go to the next line if an error occurs Set objActiveProductDoc = CATIA.ActiveDocument 'attempt to store the active product doc If Err.Number <> 0 Then 'check if an error has been thrown from the above line CATIA.StatusBar = "The active document must be a product." MsgBox ("The active document must be a product."), vbExclamation 'if it cant find an active product doc then throw an error msg CATIA.StatusBar = "" End 'end processing End If On Error GoTo 0 'go back to handling errors normally instead of suppressing them by using Resume Next 'call a procedure to give temporary names to all instances Call RenameTemporary(objActiveProductDoc.Product) 'call a recursive procedure to sort through the current product doc Call SortThroughProductList(objActiveProductDoc.Product) CATIA.StatusBar = "Done renaming." 'upadte the status bar 'set the catia application interactivity to true in order to refresh the tree and viewer 'CATIA.RefreshDisplay does not work unless it is within a VB script module within the product tree using KWA 'CATIA.ActiveWindow.ActiveViewer.Update does not work to refresh the product tree CATIA.Interactive = True End Sub Public Sub RenameTemporary(ByRef objTempCurrentProduct) 'declare local variables Dim objTempChildProduct As Product 'loop through all of the components in the current product For Each objTempChildProduct In objTempCurrentProduct.Products 'store part number in an array lngTempLstCtr = lngTempLstCtr + 1 ReDim Preserve strTempList(lngTempLstCtr) strTempList(lngTempLstCtr) = objTempChildProduct.PartNumber 'get appropriate instance number lngTempQtyCtr = 0 For lngTempIntCtr = 0 To lngTempLstCtr If strTempList(lngTempIntCtr) = objTempChildProduct.PartNumber Then lngTempQtyCtr = lngTempQtyCtr + 1 End If Next 'if this product has already been looped through then rename this instance but skip its components If lngTempQtyCtr > 1 And objTempChildProduct.Products.Count > 0 Then objTempChildProduct.Name = "Renaming" & "." & (UBound(strTempList) + 1) CATIA.StatusBar = "Renaming" & "." & (UBound(strTempList) + 1) Else objTempChildProduct.Name = "Renaming" & "." & (UBound(strTempList) + 1) CATIA.StatusBar = "Renaming" & "." & (UBound(strTempList) + 1) If objTempChildProduct.Products.Count > 0 Then 'if there are components in the child then loop through them Call RenameTemporary(objTempChildProduct.ReferenceProduct) 'call the temp rename procedure from within itself End If End If Next End Sub Public Sub SortThroughProductList(ByRef objCurrentProduct) 'declare local variables Dim objChildProduct As Product 'loop through all of the components in the current product For Each objChildProduct In objCurrentProduct.Products 'store part number in an array lngLstCtr = lngLstCtr + 1 ReDim Preserve strList(lngLstCtr) strList(lngLstCtr) = objChildProduct.PartNumber 'get appropriate instance number lngQtyCtr = 0 For lngIntCtr = 0 To lngLstCtr If strList(lngIntCtr) = objChildProduct.PartNumber Then lngQtyCtr = lngQtyCtr + 1 End If Next 'if this product has already been looped through then rename this instance but skip its components If lngQtyCtr > 1 And objChildProduct.Products.Count > 0 Then objChildProduct.Name = objChildProduct.PartNumber & "." & lngQtyCtr CATIA.StatusBar = objChildProduct.PartNumber & "." & lngQtyCtr Else objChildProduct.Name = objChildProduct.PartNumber & "." & lngQtyCtr CATIA.StatusBar = objChildProduct.PartNumber & "." & lngQtyCtr If objChildProduct.Products.Count > 0 Then 'if there are components in the child then loop through them Call SortThroughProductList(objChildProduct.ReferenceProduct) 'call the procedure from within itself End If End If Next End SubRegards,
Drew Mumaw
http://www.drewmumaw.com/
http://www.textsketcher.com/