link Catia to Matlab
79 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Réponse acceptée
Friedrich
le 7 Sep 2011
Modifié(e) : Friedrich
le 12 Fév 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:
- MATLAB makes a copy of the variable “array”
- The copy is passed to GetGravityCenter
- The values of the copy are modified
- 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 commentaires
farzad
le 11 Fév 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
le 26 Juil 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
Plus de réponses (8)
Friedrich
le 6 Sep 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 commentaires
farzad
le 22 Jan 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
le 7 Mar 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?
Alessio
le 10 Sep 2011
1 commentaire
Friedrich
le 12 Sep 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.
XAVIER
le 20 Déc 2013
Modifié(e) : XAVIER
le 20 Déc 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!
0 commentaires
yasmine yas
le 9 Mai 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
0 commentaires
Voir également
Catégories
En savoir plus sur Java Package Integration dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!