Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Part Renaming According to New Parameter 2

Status
Not open for further replies.

nainh

Automotive
Nov 9, 2022
10
Hi I'm a junior designer and still new with CATIA Automation. I wrote a cat vbscript to rename my part once it has a new parameter "Dimension". The part number shall have additional Dimension.value into the name. As per image below I want the part name to become '01-01_16x33x50'

Screenshot_2022-11-26_111259_zfifyc.png


somehow my code doesn't work and i'm clueless why

code as per below:

Sub CATMain()

Set catDoc = CATIA.ActiveDocument
Set catPart = CatDoc.Part
Set objParameters = catPart.Parameters

For i = 1 To obj.Parameters.Count
Set objParameter = objParameter.Item(i)
str ParmName = objParameter.Name
str ParmValue = objParameter.ValueAsString

If Instr(i, strParmName, "Dimension") Then
CatDoc.Part.PartNumber = PartNumber & "_" & strParmValue

End If

End Sub

Hope I can have review from you guys. and I'm sorry if I'm complicating things up since I'm not sure if there is any simpler way of doing it so.
 
Replies continue below

Recommended for you

Hi ,
they are some mistakes in your code:
-a Next is missing in the loop (For ... Next)
-to retrieve the partnumber you have to use the CatDoc.Product instead of CatDoc.Part
-remove the i in the instr fonction. This argument is optional and sets the starting position for each search. If omitted, search begins at the first character position
Code:
Sub CATMain()

Set CatDoc = CATIA.ActiveDocument
Set catPart = CatDoc.Part
Set objParameters = catPart.Parameters

For i = 1 To objParameters.Count
    Set objParameter = objParameters.Item(i)
    parmName = objParameter.Name
    StrParmValue = objParameter.ValueAsString
    If InStr(parmName, "Dimension") <> 0 Then
        CatDoc.Product.PartNumber = CatDoc.Product.PartNumber & "_" & StrParmValue
    End If
Next

End Sub

Regards
Marc
 
if you write your code in CATIA's own VBA (Tools-Macro-Visual Basic Editor) you will be able to check for errors whilst writing the code...
if you wish to have a CATScript as the final result, just copy the code (some minor changes will be required, though).

regards,
LWolf
 
Hi Marc,

thank you so much on the explanations.

LWolf, I am starting to code more in VBA to use the debugger and learn bit by bit. thank you for bringing it up.

So based on the revised code, the code catch all the "Dimension" and rename the product level:

Code:
Sub CATMain()

Set CatDoc = CATIA.ActiveDocument
Set CatProduct = CatDoc.Product
'Set CatPart = CatProduct.Products
Set objParameters = CatProduct.Parameters

For i = 1 To objParameters.Count
    Set objParameter = objParameters.Item(i)
    parmName = objParameter.Name
    StrParmValue = objParameter.ValueAsString
    If InStr(parmName, "Dimension") <> 0 Then
        CatDoc.Product.PartNumber = CatDoc.Product.PartNumber & "_" & StrParmValue
    End If
Next

End Sub

Screenshot_2022-12-01_092633_zadjza.png


However, I kinda need the parts to be renamed instead. I've searched around how to code to get to the parts level but I haven't found yet.

But then i have a source where we can grab the parts thru 'selection' and I've change the code a bit to suits my objective;

Code:
Sub CATMain()

'Dim PartNumber As String
PartNumber = InputBox("Please enter part number")

'Dim documents1 As Documents
Set PartDocument = CATIA.ActiveDocument
'Dim Selection As Selection
Set Selection = CATIA.Activedocument.selection
Set CatProduct = PartDocument.Product

Dim ElementsArray(0)
ElementsArray(0) = "AnyObject"
'Dim Status As String
Status = Selection.SelectElement3(ElementsArray, "Select Elements for name change", False, CATMultiSelectionMode.CATMultiSelTriggWhenUserValidatesSelection, False)

'Dim i As Single
Set objParameters = CatProduct.Parameters

'Dim j As Single
For i = 1 To objParameters.Count
For j = 1 To Selection.Count
    Set objParameter = objParameters.Item(i)
    parmName = objParameter.Name
    StrParmValue = objParameter.ValueAsString
    If InStr(parmName, "Dimension") <> 0 Then
	Selection.Item(j).Value.PartNumber = PartNumber &  "_0" & j & "_" &  StrParmValue
	End If
Next
Next
End Sub

But now I'm facing problem with the 'StrParmValue', based on the image below look like the dimension is looping and end on the last dimension resulting in the part name to be like highlighted.
Screenshot_2022-12-01_093556_gymki0.png


I'm still learning to see if i can use 'do while' instead but if you guys willing to enlighten me on this. I'll be grateful.
 
hi,
I'd loop thru the parts of your assy:
get each part's Dimension parameter (e.g MyPart.Parameters.RootParameterSet...)
rename the part accordingly,
move to the next one
if you have a nested structure (i.e assys within assys with parts that you want to rename as well) then set it up recursively for all the parts...

regards,
LWolf
 
Hi LWolf,

I managed to code to get the renaming to ParmValue. i just need some final touch ups to execute it properly. I will try your commented advice above too.

Screenshot_2022-12-01_193233_yq6ngx.png


I've learnt a lot from you all. thank you so muchhh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor