link Catia to Matlab

107 visualizaciones (últimos 30 días)
Alessio
Alessio el 6 de Sept. de 2011
Comentada: Edward Wilson el 26 de Jul. de 2019
Hi I'm try to connect Matlab to Catia creating a Catia COM server using the command:
C=actxserver('Catia.Application')
C =
COM.Catia_Application
but after this i can't do anything because matlab doesn't know the catia's properties, infact I tried to used the function:
C.get
I obtain the answer:
1x1 struct array with no fields.
And so i can't use any Catia's functions.
What can I do ?
  1 comentario
supriya la
supriya la el 20 de Dic. de 2018
You can refer some youtube videos.

Iniciar sesión para comentar.

Respuesta aceptada

Friedrich
Friedrich el 7 de Sept. de 2011
Editada: Friedrich el 12 de Feb. de 2014
Hi,
I attached the file to that post.
But please read the full article first.
  • I used Catia v5 R19 and this version comes with a shipped example model “Robot_ABB_S2-Irbl6_2.CATProduct”.I hard link to this in my code. Maybe you have to change this path on your machine.
  • I hard linked to the .NET DLL from the MATLAB side. You have to modify this path so it matches your system.
  • In the VisualBasic I added the references to INFITF and CATV4IInteropTypeLib. You may have to update this reference so they match you system.
Now let’s start with the actual code.
Please note that this example is extremely trimmed on the shipped example “Robot_ABB_S2-Irbl6_2.CATProduct”.
First of all I like to show you how you can do this in MATLAB without the .NET DLL
%%open and make visible
catia = actxserver('catia.application');
set(catia,'visible',1);
%%get ready to open file
prj_name = 'C:\Program Files (x86)\Dassault Systemes\B19\intel_a\startup\components\arcWeld\Robot_ABB_S2-Irbl6_2.CATProduct';
invoke(get(catia,'Documents'),'Open',prj_name);
doc = get(catia,'activedocument');
rootproduct = get(doc,'product');
mass = get(get(rootproduct,'Analyze'),'Mass');
area = get(get(rootproduct,'Analyze'),'WetArea'); %is in mm3
%%Wont work because of the pass by reference
feature('COM_SafeArraySingleDim', 1)
array = {1;2;3};
invoke(get(rootproduct,'Analyze'),'GetGravityCenter',array);
%%close catia
invoke(catia,'quit');
invoke(catia,'delete');
You see that you don’t get the GravityCenter because the input is passed by reference.
What happens here is:
  1. MATLAB makes a copy of the variable “array”
  2. The copy is passed to GetGravityCenter
  3. The values of the copy are modified
  4. MATLAB doesn’t read the copy back
MATLAB can’t handle this correctly because MATLAB doesn’t know that the input is passed by reference (the interface is not open).
The only way out here is to move to VB.NET.
You make a Class Library written in VB.NET to communicate with CATIA. The code is more readable as in MATLAB and you can work with the dot notations:
Public Class CatiaLinkLibrary
Dim CATIA As INFITF.Application
Dim rootproduct
Sub StartCatia()
CATIA = CreateObject("CATIA.Application")
End Sub
Sub CloseCatia()
CATIA.Quit()
End Sub
Sub Visible(ByRef mode As Integer)
If mode = 1 Or mode = 0 Then
CATIA.Visible = mode
End If
End Sub
Sub OpenFile(ByRef filename As String)
CATIA.Documents.Open(filename)
rootproduct = CATIA.ActiveDocument.Product()
End Sub
Function GetMass() As Double
Return rootproduct.Analyze.Mass()
End Function
Function GetVolume() As Double
Return rootproduct.Analyze.Volume()
End Function
Function GetArea() As Double
Return rootproduct.Analyze.WetArea()
End Function
Function GetGravityCenter()
Dim gravitycenter(2)
rootproduct.Analyze.GetGravityCenter(gravitycenter)
GetGravityCenter = gravitycenter
End Function
Function GetIntertia()
Dim inertia(8)
rootproduct.Analyze.GetInertia(inertia)
GetIntertia = inertia
End Function
End Class
What on the MATLAB side was a get,set and invoke becomes a nice dot notation. All the command and how they work can be obtained under the link I post earlier. The hardest part for me was to figure out what command to use since the interface CATIA provides is really complex.
You can compile this into a .NET DLL which you call from MATLAB through the NET.addAssembly command. I wrapped a MATLAB class around it to make easier to use on the MATLAB side. The class looks as follows:
classdef CatiaLink < handle
properties
catia;
end
methods
function obj = CatiaLink()
%modify this path to your .NET DLL
NET.addAssembly('C:\Users\Friedrich\Documents\Visual Studio 2008\Projects\CatiaLinkLibrary\CatiaLinkLibrary\bin\Debug\CatiaLinkLibrary.dll');
obj.catia = CatiaLinkLibrary.CatiaLinkLibrary;
obj.catia.StartCatia;
disp('Catia started')
end
function Visible(obj,mode)
obj.catia.Visible(mode);
end
function Quit(obj)
obj.catia.CloseCatia;
end
function Open(obj,filename)
obj.catia.OpenFile(filename);
end
function mass = GetMass(obj)
mass = obj.catia.GetMass;
end
function vol = GetVolume(obj)
vol = obj.catia.GetVolume;
end
function area = GetArea(obj)
area = obj.catia.GetArea;
end
function cog = GetCenterOfGravity(obj)
tmp = obj.catia.GetGravityCenter;
cog = [tmp(1),tmp(2),tmp(3)];
end
function inertia = GetInertia(obj)
tmp = obj.catia.GetIntertia;
inertia = [tmp(1), tmp(2), tmp(3); ...
tmp(4), tmp(5), tmp(6); ...
tmp(7), tmp(8), tmp(9)];
end
end
end
It’s not a good idea to call NET.addAssembly in a constructor of a class, but I assume for the demo purpose its okay.
Once the DLL is loaded you can access the function of that DLL pretty easily. Most of the functions of the MATLAB class are simply wrapper functions. In the last two functions one has to convert the VB.NET data type into a MATLAB data type so you can work with it.
So the main work is to write a nice working .NET DLL
Once you have the DLL and the MATLAB Class you can call all your functions very nicely:
%%create catia
catia = CatiaLink;
catia.Visible(1)
prj_name = 'C:\Program Files (x86)\Dassault Systemes\B19\intel_a\startup\components\arcWeld\Robot_ABB_S2-Irbl6_2.CATProduct';
catia.Open(prj_name)
area = catia.GetArea
mass = catia.GetMass
volu = catia.GetVolume
cog = catia.GetCenterOfGravity
inertia = catia.GetInertia
catia.Quit
So here a several steps to do but in the end you will have a nice interface on the MATLAB side.
Since the CATIA interface is really complex it’s hard to find a good point to start and the needed functions.
I hope I could clarify the things a bit and I hope this will help you with your project.
  3 comentarios
farzad
farzad el 11 de Feb. de 2014
Dear Friedrich , the link is expired , maybe containing the models , please refresh if possible thank you so much , also in VB.net , we use Dim command ? like CATScript ?
Edward Wilson
Edward Wilson el 26 de Jul. de 2019
Hi Friedrich,
I am very interested in this process and can recreate it using the .dll in the files attached to your original answer, however I am having issues compiling my own .dll file from the CatiaLinkLibrary. When I try to compiling it with the Library Compiler in Matlab R2017B it gives me an error saying it has too many entry points and cannot be added. If you could step me through the compilling process or point me to where to find more help I'd appreciate it.
Thanks

Iniciar sesión para comentar.

Más respuestas (8)

Friedrich
Friedrich el 6 de Sept. de 2011
Hi,
The communication with CATIA is tricky. The automation interface is not open so you won’t be able to see it. Even from Visual Basic you can’t see much. So you have to program blind.
The interface which CATIA provide can be found here:
You have to work with the set,get and invoke command from MATLAB to call these functions/ methods (e.g. set(C,'visbile',1))
But not all methods will work, e.g. GetInertia
The input is a passed by reference and MATLAB won’t recognize this and won’t return the values.
I think the best way here is to use VB.net and create a .NET DLL which handles the communication with CATIA. You can use this DLL in MATLAB than. In that way are not limited on the MATLAB side.
  3 comentarios
farzad
farzad el 22 de En. de 2014
I am really in search of linking Matlab to Catia to pilot the parameters , or it's better to link MATLAB >> EXCEL >> CATIA ?
Jakob Pettersson
Jakob Pettersson el 7 de Mzo. de 2018
Hey!
I am also searching for a way to change the parameters in Catia through Matlab. I am also thinking about linking Matlab to Excel to Catia like farzad suggested.
Have you learned anything about this topic farzad?

Iniciar sesión para comentar.


Alessio
Alessio el 10 de Sept. de 2011
Hi Friedrich! Thanks a lot for your suggests!!They were very usefull!! Now I can use Catia from Matlab. But I have a little problem.... I have to add some components in an assembly. I made a macro with catia and the VB code is this:
Sub CATMain()
Set documents1 = CATIA.Documents
Set productDocument1 = documents1.Add("Product")
Set product1 = productDocument1.Product
Set products1 = product1.Products
Dim arrayOfVariantOfBSTR1(0)
arrayOfVariantOfBSTR1(0) = "C:\Documents and Settings\LELE\Desktop\cilindro.CATPart"
products1.AddComponentsFromFiles arrayOfVariantOfBSTR1, "All"
Dim arrayOfVariantOfBSTR2(0)
arrayOfVariantOfBSTR2(0) = "C:\Documents and Settings\LELE\Desktop\cubo.CATPart"
products1.AddComponentsFromFiles arrayOfVariantOfBSTR2, "All"
Following the the interface which CATIA provide (which you suggested me) I wrote this Matlab code:
catia=actxserver('catia.application');
set(catia,'visible',1);
doc=get(catia,'documents');
prod=invoke(doc,'add','Product');
prods=get(prod,'Product');
prods1=get(prods,'Products');
cilindro=invoke(prods1,'AddComponentsFromFiles','D:\LELE\Università\TESI\PROVE\cilindro.CATPart');
cubo=invoke(prods1,'AddComponentsFromFiles','D:\LELE\Università\TESI\PROVE\cubo.CATPart');
but it doesn't work and I don't know why....do you have an idea?

Alessio
Alessio el 6 de Sept. de 2011
Thank you Friedrich!!! I tried to use get and invoke command but Matlab said me that there are no fields, so I can't use any Catia function, for example
C.ActiveDocument.Activate;
partDocument1 = C.ActiveDocument;
part1 = partDocument1.Part;
and so on.
I'd like to use your suggest to create a .net dll but I'm not so expert....can you be more specific??How can I do to create it? and how can I use it?
Thank's a lot!!!!
  1 comentario
Friedrich
Friedrich el 7 de Sept. de 2011
Hi,
I will post a small example later (made with CATIA v5). I have to look it up again. It's somewhere on my other PC. So stay tuned ;)

Iniciar sesión para comentar.


Alessio
Alessio el 10 de Sept. de 2011
p.s.: The error is:
??? Error: Type mismatch, argument 1
  1 comentario
Friedrich
Friedrich el 12 de Sept. de 2011
Hi,
why the VB code when you use ML for communicating with CATIA?
Looking at the interface discription of AddComponentsFromFiles it seems you forget an inputargument:
http://www.catiadesign.org/_doc/catia/catia_vb/generated/interfaces/ProductStructureInterfaces/interface_products.htm#AddComponentsFromFiles
So I would guess it should like this:
cubo=invoke(prods1,'AddComponentsFromFiles','D:\LELE\Università\TESI\PROVE\cubo.CATPart','All');
In addition you will get troubles with the CATSafeArrayVariant datatype on the CATIA side since "CATSafeArrayVariant are one-dimensional arrays of CATVariants.":
http://www.catiadesign.org/_doc/catia/catia_vb/generated/interfaces/System/typedef_catsafearrayvariant.htm
MATLAB passes 2dimensional arrays by default. To get MATLAB passing only one dimensional arrays you have to enable it through:
feature('COM_SafeArraySingleDim', 1)
But all this code I would do in the VB side and not on ML.

Iniciar sesión para comentar.


Alessio
Alessio el 12 de Sept. de 2011
hi, sorry for annoying you.... I wrote 'All' in my matlab code...I only forgot to write it in my post... I tried to use feature('COM_SafeArraySingleDim', 1) as they say also here:
but it doesn't work...it gives me the same error.
  1 comentario
Friedrich
Friedrich el 12 de Sept. de 2011
mhhhh, try to cast it into a cell:
cubo=invoke(prods1,'AddComponentsFromFiles',{'D:\LELE\Università\TESI\PROVE\cubo.CATPart'},'All');
I had done this in my ML code above too.

Iniciar sesión para comentar.


Alessio
Alessio el 12 de Sept. de 2011
THANK YOU FRIEDRICH!!!! YOU SAVED ME!!!
Now, with the brackets, it works!!!
  1 comentario
Friedrich
Friedrich el 13 de Sept. de 2011
So everything works for you? Can you mark it as answered (accept an answer)?

Iniciar sesión para comentar.


XAVIER
XAVIER el 20 de Dic. de 2013
Editada: XAVIER el 20 de Dic. de 2013
Hello,
This topic was very useful!
I got a matlab script which sets parameters into CATIA and then saving it. But CATIA doesn't update the parameters before saving. I tried to update the CAD model in MATLAB thanks to :
invoke(catia,'update')
But that didn't work. Does anyone has an idea? Thanks a lot!

yasmine yas
yasmine yas el 9 de Mayo de 2016
heloo plllz help me How can i import design in catia to matlab simulink i found programme which is called SANEON but i don't how it does work . Thank you

Categorías

Más información sobre Java Package Integration en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by