VB,Python,Matlab,连接CATIA 原理,二次开发。
标签:
vbpythonmatlab连接catia |
相信,用catia 的童鞋们,对catia macro(宏) 有一定的了解。当然,这些都可以在 你安装文件夹下搜
V5Automation.chm 找到非常详细的指导。该文件主要是 catia 基于VB的 api。
我们先看第一个通过catia -> tool->macro->VB例子:
1. 看官方例子
打开 V5Automation.chm -> Content ->Getting Started with
CAA V5 Visual Basic -> working with infrastructure -> use
cases -> A script sample (其实很多中文的二次开发,就只是翻译这个帮助文件的例子而已)
2. 读懂程序
根据上面创建的顺序,会有如下的code:
Dim
documents1 As Documents (Dim
表示定义,这是VB的语法,此句为:定义documents1 是一个文件类型。读者可以百度 VB 语法)
Set
documents1 = CATIA.Documents (set 对对象赋值,VB
语法。此句为:把CATIA的属性Documents值 赋给documents1。Documents
就是CATIA提供的api)
一下是 其余部分程序:
Dim part1 As
Part
Set
part1 = partDocument1.Part
Dim
bodies1 As
Bodies
Set
bodies1 = part1.Bodies
Dim
body1 As
Body
Set
body1 = bodies1.Item("PartBody")
Dim sketches1
As
Sketches
Set
sketches1 = body1.Sketches
Dim originElements1
As
OriginElements
Set
originElements1 = part1.OriginElements
Dim
reference1 As AnyObject
Set
reference1 = originElements1.PlaneXY
Dim
sketch1 As
Sketch
Set
sketch1 = sketches1.Add(reference1)
程序主体理解完毕(其余的 如 sub, module,等语法 请自行百度)
Python例子:
1. 外部连接 :
由于CATIA 中的vb代码是在 catia中运行,所以省了
vb与catia连接的code(代码)。因此,我们需要先把python与catia连接起来。第一件事就是下载相应版本的pywin,再import
win32com.client.
也可以根据这个文章http://win32com.goermezer.de/content/view/29/291/
所以,在python调用windows api前,需要写如下代码:
import win32com.client
catia =
win32com.client.Dispatch('catia.application')
如果看不见 catia窗口 可以 再输入 catia.visible = 1
相应的VB 接口代码 在 V5Automation.chm -> Content
->Getting Started with CAA V5 Visual Basic -> working with
infrastructure -> General Information -> Invoking CATIA from
a scripting language 里面找到。
2.用Python 仿写VB代码:
在仿写之前,请笔者注意python语法。
一下为仿写代码:
documents1 = CATIA.Documents
partDocument1 = documents1.Add("Part")
part1 = partDocument1.Part
bodies1 = part1.Bodies
body1 = bodies1.Item("PartBody")
sketches1 = body1.Sketches
originElements1 = part1.OriginElements
reference1 = originElements1.PlaneXY
sketch1 = sketches1.Add(reference1)
运行成功,没有错误。catia 正常打开,并且定义了 reference。
附图:
Matlab例子:
1. 外部连接:
读者可以根据这个文章来
帮助理解matlab与catia的连接。http://www.mathworks.com/matlabcentral/answers/15228-link-catia-to-matlab
连接代码如下:
catia = actxserver('catia.application');
set(catia,'visible',1);
2.仿写代码:
在matlab中,使用invoke进行属性定义,get把属性值付给变量。matlab代码不做演示。请读者自行解决。
总结:只要理解了api的意义,理论上来说可以使用任何语言对 caita等3维软件操作。
前一篇:欢迎您在新浪博客安家

加载中…