首先新建一个Part文件,插入一个几何图形集,在该几何图形集中插入一个点,坐标为(100,0,0),如下图所示:
下面通过python来测量该点的坐标:
import win32com.client
import pywintypes # 导入pywintypes模块
# 启动CATIA应用
catia = win32com.client.Dispatch('CATIA.Application')
catia.visible=1
# 定义一个测量函数
def measurepoint(wb,ref):
# catiascrip里的测量代码
code='''Function MeasurePoint(Wb, Ref)
set mes = Wb.GetMeasurable(Ref)
Dim Coord(2)
mes.getpoint Coord
MeasurePoint = Coord
End Function'''
srv=catia.SystemService
coord = srv.Evaluate(code,0,'MeasurePoint',(wb, ref))
return coord
try:
doc = catia.activedocument
part = doc.part
wb = doc.getworkbench('SPAWorkbench')
hb = part.hybridbodies[0] # 获取几何图形集
pt = hb.hybridshapes[0] # 获取点
ref = part.createreferencefromobject(pt) # 创建参考
coord = measurepoint(wb, ref) # 调用自定义函数,测量坐标
for num in coord:
print(num) # 打印坐标进行验证
except pywintypes.com_error as e:
# 如果出现错误,可能是因为没有活动文档
print("无法获取活动文档,请确保CATIA应用程序中已有打开的文档。")
print(e)
100.0
0.0
0.0