Wednesday, April 18, 2018

How to draw CATIA with MATLAB?

This post intends to show how it is possible a communication between CATIA and MATLAB. Whatever motivation you have to do with that. The software technology we're going to use for this communication (COM Objects) is straightforward to make it possible interoperability between many Windows Applications.

How two different softwares can talk?

By using filesystem is the simplest way to make possible the communication between softwares. For example, suppose one program calculates aerodynamic parameters (says AVL) and you need these parameters for flight performance calculations through a MATLAB code. AVL software is a command line program that can receive a text file as input and return another text file as output. Then, the easiest (maybe the only) way to make this communication is making use of files: MATLAB builds an input text file, calls AVL and reads text file returned by AVL.

Writing files on disk is a bottleneck for computational performance. When you are using optimization algorithms, for example, this is going to demand calling your little flight performance MATLAB code many times. Filesystem readings can mean a high execution time cost in this case.

When using command line programs, a little bit faster approach could be using the direct return of the program, without a bridging file. For example, in MATLAB, over Windows platform, we could get the current directory through the system program cd, using the command:

[resultstatus, currentdir] = system('cd');

Although, a most elegant and standardized way to connect to a compiled software (source code not available) is through a public Application Programming Interface (API), i.e., roughly speaking, an interface to call compiled software functionalities. In Microsoft Windows, an interesting standard for exposing API is Component Object Model (COM).

Using Windows Component Object Model (COM) to make your dreams come true

COM is an interface that enables interprocess communication and dynamic object creation in a large range of programming languages (Wikipedia). This standard allows the creation of a set of entities called objects, which are composed by properties and methods (these terms will be explained better later). For example, Microsoft Excel (as all the other programs from Microsoft Office Suite) has COM Objects, whose properties and methods allows to create and change spreadsheets from a code in any language implementing COM interface. The xlsread MATLAB command, which read an Excel spreadsheet, makes use of Excel COM object (see the code at [MATLAB PATH]\toolbox\matlab\iofun\xlsread.m).

In MATLAB we use actxserver command to handle a COM Object. Let's make a little bit of magic! In the code below, we are going to get a COM interface with CATIA and make its window appear (if CATIA is not openned before), changing the property Visible of CATIA COM object (this was tested with the very old 7.9.0, R2009b, but it works with the newer ones).

>> catia_com = actxserver('CATIA.Application')

catia_com =
COM.CATIA_Application

>> set(catia_com,'Visible',true)


ans =
NaN


Then you're going to see CATIA window in your task bar.

(If you do not have CATIA, you can test actxserver with other COM Object as Excel.Application.)

In software engineering, catia_com variable in the code above is called object. This is an entity composed by properties and methods. Property is a changeable characteristic of the entity: catia COM object can be visible or not, for example. Method is a function which changes object behaviour. As we can see with the code below, we can add a new document CATIA Part through method Add of property Documents. To obtain the value of a property, we use MATLAB get command; to change the value of a property, we use set; to call a method, we use invoke.

catia = actxserver('CATIA.Application');

set(catia,'Visible',1);
docs = get(catia,'Documents');

doc = invoke(docs,'Add','Part');

From now on, you can draw an entire aircraft from your Matlab script. Enjoy it!

No comments:

Post a Comment