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!

Macro to Change Part Number and Instance Name to File Name 3

Status
Not open for further replies.

CatiaWiz

Mechanical
Mar 12, 2013
7
US
Hello,

This is my first thread on this site, so any help someone can give me would be greatly appreciated. I am looking for a macro that would automatically rename a CatParts Part Number and Instance Name to match the filename. However, I need to be able to run the macro when the CatPart exists inside of a CatProduct. We are starting to use a design catalog for all of our components. We bring in a component which is stored under a generic name in the catalog and we then rename it and save it into our job folder under a more specific name. It is very time consuming to manually go in and change the Part Number and Instance Name to match the file name so we would like a macro to this automatically. Any help that someone can give me on this would be greatly appreciated. Just want to note that I have limited script writing experience.

Thank You.
Jeff
 
Replies continue below

Recommended for you

Is there more than 1 instance of any part?




If You are interested in Macros in Catia (Catscripts, Catvba) feel free to send me a message. I will wrote specified macros for free and post it on eng-tips forum
 
I am thinking that we will bring the CatPart into the design and then run the macro. If we need any additional instances, we typically just use the fast multi instantiation button which will automatically add the .1 to the end of the instance name based on the amount of instances. If possible we would like the macro to only rename the CatPart that is the active document in the tree. Please let me know if you need more information than this. Thank you for taking the time to try and help us.
 
Code:
Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

  If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
     MsgBox "Active CATIA Document is not a Product. Open a Product file and run this script again.", , msgboxtext
     Exit Sub
   End If
   Call ListingNames(productDocument1.Product)


End Sub

Sub ListingNames(current_prod)
If current_prod.Products.Count > 0 Then
For i = 1 To current_prod.Products.Count
Call ListingNames(current_prod.Products.Item(i))
Next i
Else
MsgBox current_prod.ReferenceProduct.Parent.Name
End If
End Sub

This Macro will list All of Yours Parts filenames



Code:
Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

  If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
     MsgBox "Active CATIA Document is not a Product. Open a Product file and run this script again.", , msgboxtext
     Exit Sub
   End If
   Call ListingNames(productDocument1.Product)


End Sub

Sub ListingNames(current_prod)
If current_prod.Products.Count > 0 Then
For i = 1 To current_prod.Products.Count
Call ListingNames(current_prod.Products.Item(i))
If InStr(current_prod.Products.Item(i).ReferenceProduct.Parent.Name, ".CATProduct") > 0 Then
current_prod.Products.Item(i).PartNumber = Left(current_prod.Products.Item(i).ReferenceProduct.Parent.Name, Len(current_prod.Products.Item(i).ReferenceProduct.Parent.Name) - 11)
End If
Next i
Else
If InStr(current_prod.ReferenceProduct.Parent.Name, ".CATPart") > 0 Then
current_prod.PartNumber = Left(current_prod.ReferenceProduct.Parent.Name, Len(current_prod.ReferenceProduct.Parent.Name) - 8)
End If
End If
End Sub

This one will scan All CATProduct tree and rename PartNumber or ProductNumber with respect to filename

I will post Macro to rename instance names soon!


If You are interested in Macros in Catia (Catscripts, Catvba) feel free to send me a message. I will wrote specified macros for free and post it on eng-tips forum
 
I tried to use both of these macros and they both give me an error saying as follows:
Description: Expected end of statement
Statement: Next i
Line: 21
Column: 5

Any ideas on what the problem might be?
 
I'm using Catia v5r21 and external VBA compiler, and it causes some differences between typical CatScripts


If You simply change
Code:
Next i
to
Code:
Next
it will be working.
Let me know if You have any other problems with those macros

I will post macro to change instance names on saturday

/The first one is just simply macro for testing alghoritm, the second one is the proper macro. Both of them You need run under Your assembly - it works with Active Product and its child components

If You are interested in Macros in Catia (Catscripts, Catvba) feel free to send me a message. I will wrote specified macros for free and post it on eng-tips forum
 
Code:
Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

  If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
     MsgBox "Active CATIA Document is not a Product. Open a Product file and run this script again.", , msgboxtext
     Exit Sub
   End If


   Call ListingNames(productDocument1.Product)


End Sub

Sub ListingNames(current_prod)
Dim selected()
Dim New_instance As String
If current_prod.Products.Count > 0 Then

ReDim selected(current_prod.Products.Count)
For i = 1 To current_prod.Products.Count
selected(i) = False
Next

For i = 1 To current_prod.Products.Count
instances = 1

If selected(i - 1) = False Then

For j = i To current_prod.Products.Count
If current_prod.Products.Item(j).PartNumber = current_prod.Products.Item(i).PartNumber Then
selected(j - 1) = True
New_instance=CStr(current_prod.Products.Item(i).PartNumber + "." +CStr(instances))
'current_prod.Products.Item(j).Name =New_instance
instances = instances + 1
MsgBox New_instance
End If
Next 
End If

Call ListingNames(current_prod.Products.Item(i))

Next
End If

End Sub

CATSrcipt posted above correctly list all components form product tree (products and parts) and display proposed new instance names according to theirs PartNumbers, but line
Code:
current_prod.Products.Item(j).Name =New_instance
is not working correctly
I can't find any mistakes in this line. I'm still working on it :)

If You are interested in Macros in Catia (Catscripts, Catvba) feel free to send me a message. I will wrote specified macros for free and post it on eng-tips forum
 
I used the macro that "Scans All CATProduct tree and rename PartNumber or ProductNumber with respect to filename". It works great!!! If you can get the instance macro to work, is there a way that you can combine the two macros so that i can assign an icon to the macro and only have to push one button?
 
is there a way that you can combine the two macros so that i can assign an icon to the macro and only have to push one button?



Code:
Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

  If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
     MsgBox "Active CATIA Document is not a Product. Open a Product file and run this script again.", , msgboxtext
     Exit Sub
   End If


	Call PartNumberAsFileName(productDocument1.Product)
	Call Rename_PartName(productDocument1.Product)
End Sub


Sub PartNumberAsFileName(current_prod)

If InStr(current_prod.ReferenceProduct.Parent.Name, ".CATProduct") > 0 Then
current_prod.PartNumber = Left(current_prod.ReferenceProduct.Parent.Name, Len(current_prod.ReferenceProduct.Parent.Name) - 11)
End If

If current_prod.Products.Count > 0 Then
For i = 1 To current_prod.Products.Count
Call PartNumberAsFileName(current_prod.Products.Item(i))
If InStr(current_prod.Products.Item(i).ReferenceProduct.Parent.Name, ".CATProduct") > 0 Then
current_prod.Products.Item(i).PartNumber = Left(current_prod.Products.Item(i).ReferenceProduct.Parent.Name, Len(current_prod.Products.Item(i).ReferenceProduct.Parent.Name) - 11)
End If
Next
Else
If InStr(current_prod.ReferenceProduct.Parent.Name, ".CATPart") > 0 Then
current_prod.PartNumber = Left(current_prod.ReferenceProduct.Parent.Name, Len(current_prod.ReferenceProduct.Parent.Name) - 8)
End If
End If
End Sub 




Sub Rename_PartName(current_prod)
Dim selected()
Dim New_instance As String
If current_prod.Products.Count > 0 Then

ReDim selected(current_prod.Products.Count)
For i = 1 To current_prod.Products.Count
selected(i) = False
Next

For i = 1 To current_prod.Products.Count
instances = 1

If selected(i - 1) = False Then

For j = i To current_prod.Products.Count
If current_prod.Products.Item(j).PartNumber = current_prod.Products.Item(i).PartNumber Then
selected(j - 1) = True
New_instance=CStr(current_prod.Products.Item(i).PartNumber + "." +CStr(instances))
[b]current_prod.Products.Item(j).Name =New_instance[/b]
instances = instances + 1
'MsgBox New_instance
End If
Next 
End If

Call Rename_PartName(current_prod.Products.Item(i))

Next
End If

End Sub

Is it working correctly with products with non-flat structure, eg:
Product1
-Product2
--Product3
---Part1
---Part2

?

I've spend few hours to test this macro on various types of products configurations and still have no idea, why it is not working correctly...
Maybe more experienced Catia Scripter will be able to re-write this macro

Anyway, I'm glad that I was able to solve some of Yours problems


If You are interested in Macros in Catia (Catscripts, Catvba) feel free to send me a message. I will wrote specified macros for free and post it on eng-tips forum
 
I tested the macro today, i created a new cat product and then inserted a catpart. I saved the catpart under a different name and ran the macro, it worked perfectly. It does exactly what i was looking for except it doesn't only rename the selected catpart. Also, i brought in a new product to simulate bringing in a sub-assembly. I saved this sub-assembly under a new name, then i ran the macro and it still did exactly what i wanted except the exception listed above. Thank you for all of your help.
 
it doesn't only rename the selected catpart

I Tought You'd like macro that rename all parts/products in assembly:)

Tell me more about quantity of selected catparts, are they more or less than 50% of all parts in assembly?

If You are interested in Macros in Catia (Catscripts, Catvba) feel free to send me a message. I will wrote specified macros for free and post it on eng-tips forum
 
Hello lukaszsz,

i think i have a similar problem like CatiaWiz, but your macro didnt really work with it.

Perhaps you can help me with this one.
I have created a Product with several Parts. They have following naming structure:
xxxxxxx_A_1_A_ProductName
xxxxxxx_0001_PartName1 (xxxxxxx_0001_PartName1)
xxxxxxx_0002_Partname2 (xxxxxxx_0002_PartName2)
....

I need to change the PartName and the Instance name of every Part to any number i want, but only the first seven digits should change. I have a macro with wich i can change the Instance name but nothing that can rename the Partname with the Instance name.

Is there anything one could do to help me?

Thanks, Rob
 
I'm not a regular coder for Catia and I am late with the answer ... but I hope it helps somebody.

Code:
Option Explicit
Sub CATMain()

    'part declaration________________
    Dim catDocument As PartDocument
    Dim catPart As Part
    Set catDocument = CATIA.ActiveDocument
    Set catPart = catDocument.Part
    'other declaration_______________
    Dim HSF As HybridShapeFactory
    Dim se1 As Object
    Dim result As Variant
    Dim hBs1 As HybridBodies
    Dim hB1 As HybridBody	
    Dim i As Integer
    'asociation_____________________
    Set HSF = catPart.HybridShapeFactory    
    Set se1 = catDocument.Selection
    se1.Search "CATGmoSearch.Point,sel"

    Set hBs1 = catPart.HybridBodies
    Set hB1 = hBs1.Item("GeomSet") 'change the name with yours
    se1.Add hB1
    se1.Search "CATGmoSearch.Point,sel"
            
    If (se1.Count > 0) Then    
        For i = 1 To se1.Count
		
			'write your code

        Next
        se1.Clear                
    Else
        MsgBox prompt:="no points in GeomSet, Title:="Aleluia"
        Exit Sub
    End If

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top