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!

CATIA V5 automation help and resources

Status
Not open for further replies.

Dustin_H

Aerospace
Mar 31, 2021
4
Hello,

I am trying to learn to write Catia scripts to help automate some of our most repetitive, basic tasks. I have been searching online, and I've even found some websites dedicated to this topic, but they are defunct and the creators have quit teaching this. So I am trying to find a good way to get started by picking the simplest idea I have and try to write a script.

I am trying to write a macro that I can run and save each sheet of a drawing as a separate .CGM file. This would be done with the drawing open. I think I know conceptually what needs to be done, here is how I think the macro should flow:

1. Store the filename of the drawing to be used at a later step.
2. Go to Sheet 1.
3. Store the sheet name to be used at a later step.
4. Save the sheet as .cgm. The saved filename would be the filename of the drawing as a base and then an "_" and add the sheet title in CATIA after it.
Example: filename of drawing is "Drawing1.CATDrawing" and Sheet 1 name is "Sheet1". The finished .cgm being saved should be "Drawing1_Sheet1.cgm".

5. Create a loop to repeat steps 2 through 4 for each sheet and have it cycle through the other sheets until the end and exit the loop.

This can be done manually but for larger drawings with dozens of sheets this can take quite some time, and also I'd like to get started writing scripts and this seems like a great way to get started. I am rusty when it comes to programming, please forgive me. If there is a better

Any help is appreciated!

Thanks,
Dustin

 
Replies continue below

Recommended for you

Your best resources are:
- macro recording;
- V5Automation.chm available in the ..\bin directory of your CATIA installation;
- this forum;
- COE forum.

Your needed code would be roughly along these lines:

Code:
Sub CATMain()
    Dim oDrwDoc As DrawingDocument
    Set oDrwDoc = CATIA.ActiveDocument
    
    Dim oSheet As DrawingSheet
    For Each oSheet In oDrwDoc.Sheets
        oDrwDoc.ExportData oDrwDoc.Path & "\" & oDrwDoc.Name & "_" & oSheet.Name & ".cgm", "cgm"
    Next
End Sub
 
Hello and thanks for the reply Cilici! I know quite some time has passed but work/life have been crazy lately so I haven't had time to work on this optional project.

I tried that macro and it does a good job of cycling through and naming the files correctly. The issue it has is that every saved .cgm file is of sheet 1. I can't believe how compact that script is...I was imagining something at least twice as complicated! If I can figure out this one issue, this script should be good to go!

To fix this issue, do I need to create a loop that changes the active sheet as it goes through the drawing sheets and saves each .cgm?

Thanks again,
Dustin
 
I can't test it but try oSheet.Activate before exporting, first line in for loop.
 
Need to activate each sheet.

Code:
Sub CATMain()
    Dim oDrwDoc As DrawingDocument
    Set oDrwDoc = CATIA.ActiveDocument
    
    Dim oSheet As DrawingSheet
    For Each oSheet In oDrwDoc.sheets
        [highlight #FCE94F]oSheet.Activate[/highlight]
        oDrwDoc.ExportData oDrwDoc.Path & "\" & oDrwDoc.Name & "_" & oSheet.Name & ".cgm", "cgm"
    Next
End Sub
 
Thanks for the help! Earlier when Cilici replied I added that line and it works great now. Still can't believe how compact this script ended up being. After you see it, it looks so simple, but I don't even want to say how long I spent looking for solutions online!

Thanks again,
Dustin
 
Don't forget standard error-proofing; the code will fail if the Active Document is NOT a drawing (check for the type of the file), or if no document is opened at all.

regards,
LWolf
 
LWolf - do you have an example of the error-proofing code? It would be nice if it didn't crash if someone didn't have the drawing active.

Anyone willing to help with something new with this macro? I decided to try and make the macro a little bit better, but I am just apparently no good at finding information about this stuff.

I am trying to make it to where it does not generate at .cgm file for the detail sheet(s) of the drawing.

I think I need to add something similar to:

"If oSheet.IsDetail Then"

Sorry I don't see how to add in the code box like you guys have been doing.

Also I would like it to return the drawing to the sheet that was originally being viewed after the macro is done, or at least the first page. It currently leaves the drawing on the last activated sheet at the end of the macro.

To do this, I think I need to set the current or first sheet as it's own object and then activate it after exiting the loop sorta like:

"oSheet1.Activate"

I keep searching and experimenting but most of this stuff is either over my head or just irrelevant to what I'm trying to do and the only way I have made progress is this forum!

Thanks,
Dustin
 
oDrwDoc.Sheets.item(1).Activate will return you to sheet 1, just put that at the end after the loop. If you want it to be which ever random sheet was being viewed, that will be tricky.

2021-04-26_13_07_29-CATIA_V5_automation_help_and_resources_-_DASSAULT__CATIA_products_-_Eng-Tips_M_op5d3k.png


Code:
Err.Clear

'Check if a file is open
If CATIA.Windows.Count = 0 Then
MsgBox "There are no CATIA Files open."
Exit Sub
End If

'check if a CATDrawing is open
If InStr(drawingDocument1.Name, ".CATDrawing") = 0 Then
MsgBox "The Active Document must be a CATDrawing."
Exit Sub
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor