jeudi 2 mai 2013

Introduction to CATIA V5 Automation



CATIA V5 is a powerful tool in the CAO field. With some work you could be able to design almost everything. But how many time will you need, for example, to create ALL screws in your assembly ? With each type ? Each diameter ? Hopefully a tool named VBA exist to get rid of this waste of time. 
I will present  you the very basics applications of using VBA macros with CATIA V5.

First of all CATIA V5 can be piloted with mainly three possible languages :
  1. Visual Basic
  2. VBA
  3. CATScript
I choosed to use VBA for a simple reason : It allows me to use easyly Excel to pilot CATIA. That's mean I could use datasheets or graphics to create Parts in CATIA V5. Moreover it offers a better interface with the user (command button, listbox...) and a priceless programming help (automatical completion, help to find errors). However VBA has disavantages like a weak protection and it could be difficult to export VBA macros.

I suppose in the following that you have some prerequisites in VBA. If not, you can look at this article which present briefly VBA for Excel. If it's not enough, you can find plenty of tutorials in Internet.

Before beginning I introduce your new best friend : the CATIA help. It's very dense and complexe but it should be your reference. You find it here : \Catia_V5\intel_a\code\bin\V5Automation.chm. I suggest you to make immediatly a shortcut on your desktop.

1) CATIA V5 OLE (Object Linking and Embedding)

If you want to have an interactive review in Word for example, you can use the link between CATIA and Word : just drag and drop a CATPart on your word document and you have a picture of your part. Double-click on it and you can edit it in CATIA V5 (Figure 1).

Picture 1 : OLE operation


2) IN- and OUT macros

a) IN process application

You have two choices to pilot CATIA.

First you can use the Visual Basic Editor included in CATIA V5 (TOOLS\MACRO\Visual Basic Editor Picture 2)
You create this way IN process application.

Picture 2 : Run the VB Editor

You have the same Macro menu as in VBA Excel where you can see all the macro you have writed. You can run, edit, rename or delete this macro. You can also record a macro if you have a specifical need or when you did'nt remember the right syntax. Then you can find it in the module name you have choosed. 

How to add a macro as a command in a Toolbar ?

Go to Tools\customize\Commands and click on Macro. Select the macro you want and then drag and drop it in the toolbar you wish. You can also use a personalised icon if you don't like the default icon by clicking on Show Properties (Picture 3)

Picture 3 : How to add a macro on your toolbar


b) OUT process application

Otherwise, you can pilot CATIA through Excel.
Personnaly, I found this solution more effective for two reasons :

  • You can use Excel datas
  • You have more freedom (VB Editor in CATIA need a CATMain sub whereas VBA in Excel didn't)
However you have to activate the CATIA functions, propertys and types. Just check the boxes beginning by catia in TOOL\REFERENCES (Picture 3)

Picture 3 : Activate the CATIA libraries in VBA Excel



























However, as you aren't in CATIA VB Editor, you have to declare CATIA as Object in your sub.

You have two cases :

  • If CATIA is already running
The GetObject function allows to set CATIA application as the object you defined.
The code is so :

[...]
Dim CATIA As Object
Set CATIA = GetObject("CATIA.Application")
[...]

  • If CATIA is not running 
You have to create the object using CreateObject :

[...]
Dim CATIA As Object
Set CATIA = CreateObject("CATIA.Application")
[...]

Now you are able to create your first macro, which launch CATIA if not.

sub CATRun()

Dim CATIA As Object

'In Error case CATIA is not already open

On Error Resume Next
Set CATIA = GetObject("CATIA.Application")
If Err.Number <> 0 Then
 Set CATIA = CreateObject("CATIA.Application")
 CATIA.Visible=True
End if
On Error GoTo 0

End sub

Now you're able to run CATIA V5 through Excel. Before going further you need to have a introduction to the CATIA structure.






0 commentaires:

Enregistrer un commentaire