加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

VB,Python,Matlab,连接CATIA 原理,二次开发。

(2014-05-14 11:36:24)
标签:

vb

python

matlab

连接

catia


用程序语言 如 Visual Basic,Python,或Matlab 操作CATIA 等绘图软件,其最重要的就是对API的理解。当然,对于程序猿来说,本帖就可以无视了。本帖重要面向 做工程的人,普及一下非常浅的程序知识。

相信,用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。
附图:
http://s2/mw690/0026nWu7gy6IRXHsWhHe1&690原理,二次开发。" TITLE="VB,Python,Matlab,连接CATIA 原理,二次开发。" />


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维软件操作。



 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有