Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Catia V5 Macro to Rename Boolean Operations 1

Status
Not open for further replies.

Daniel.M

Automotive
Feb 9, 2022
2
0
0
AU
Hi there!

I'm wondering if anyone has a macro code for renaming Boolean Operations based on their bodies...

For example, Catia does this.

PartBody
Add.1
[Body]_Boss

Remove.1
[Body]_Holes

However, I would like to automatically rename the Boolean to this:

PartBody
Add_Boss
[Body]_Boss

Remove_Holes
[Body]_Holes

I'm greatly appreciative for a reply, thanks for you help!

-Daniel
 
Replies continue below

Recommended for you

I had a similar case, where I wanted to name a geometrical element (extracted surface) based on the name of the body where it came from. I don't know enough to trace and follow links, so I just used two selections. How it works is you run the macro, select the body you want to reference (to get its name), and then select the element to target (to apply the name we got in the first step).

Later I expanded this to Boolean operations because it's convenient. It's not automated, because I haven't had the need yet, but I'm sure that's a reasonable next step to get and follow the parent child relationship.

Here is the code, which may have some clues to assist you in your development:

Code:
Sub CATMain()


Dim oDoc As Document
Dim sDocName As String
Dim oPart As Part
Dim oSel 'As Selection
Dim sSourceName As String
Dim sTarget
Dim Status

On Error GoTo ErrMsg

Set oDoc = CATIA.ActiveDocument

'Check if the current document is a part
sDocName = LCase(oDoc.Name)
If Right(sDocName, 7) = "catpart" Then
    GoTo Start
    Else: MsgBox "Active Document Must Be A Part", vbOKOnly, "Document Type Check"
        Exit Sub
End If



Start:

Set oSel = oDoc.Selection

oSel.Clear

Dim InputObjectType(2)
InputObjectType(0) = "HybridShape"
InputObjectType(2) = "Body"
InputObjectType(1) = "Assemble"

Status = oSel.SelectElement2(InputObjectType, "Select a Body to get its Name", False)

If Status = "Cancel" Then
    oSel.Clear
    Exit Sub
End If
    

sSourceName = oSel.Item2(1).Value.Name
'MsgBox sSourceName

oSel.Clear

Status = oSel.SelectElement2(InputObjectType, "Select the Target Shape to Name", False)

If Status = "Cancel" Then
    oSel.Clear
    Exit Sub
End If

Set sTarget = oSel.Item2(1).Value
sTarget.Name = sSourceName
oSel.Clear

GoTo EndLine


ErrMsg:
MsgBox "Some Error Occurred"
Exit Sub


EndLine:

End Sub
 
I should put a little more work into this before posting sometimes. It was indeed a very small modification to get the right relationship for the Boolean objects, as well as to find all of them. This simple code searches the Part for all Assemble objects and then applies the names of the children bodies.

Code:
Sub CatMain()

Dim oDoc As Document
Dim sDocName As String
Dim oPart As Part
Dim oSel 'As Selection
Dim sSourceName As String
Dim sTargetName As String
Dim Status

On Error GoTo ErrMsg

Set oDoc = CATIA.ActiveDocument

'Check if the current document is a part
sDocName = LCase(oDoc.Name)
If Right(sDocName, 7) = "catpart" Then
    GoTo Start
    Else: MsgBox "Active Document Must Be A Part", vbOKOnly, "Document Type Check"
        Exit Sub
End If

Start:

Set oSel = oDoc.Selection

oSel.Clear

oSel.Search "CATPrtSearch.Assemble"

For N = 1 To oSel.Count2
    sSourceName = oSel.Item2(N).Value.Body.Name
    oSel.Item2(N).Value.Name = UCase(sSourceName)
Next

oSel.Clear

GoTo EndLine


ErrMsg:
MsgBox "Some Error Occurred"
Exit Sub


EndLine:

End Sub
 
Hi Mark,

Your second code has solved me some issues.

However, you code does not rename the Boolean to Add_[Children body name] and instead just changes the whole operation to just the body name. It also only seems works for the assemble operation rather than all of them (Add, Remove, Etc...). I made a minor change to rename 'Add' operations but now it doesn't rename assemble operations (and the others still too)... lol.

What do you this is the cause of these issues? Once I can get these sorted it should work and be perfect for me.

I really value your help!

-Daniel
 
Hi Daniel,
At this point I suggest you continue to study the code and find out how it works. It would also be beneficial to learn about string manipulation in VBA. It is easy to concatenate whatever text you want onto the value for the name. Then, since I am rather simple about coding, I would just run through this routine several times, each time searching for the different feature type. Copy / Paste / Edit sections of the code. I'm sure there's better ways, but that's the simple way I know about.

Hope that's helpful,

Mark
 
Status
Not open for further replies.
Back
Top