免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8716 | 回复: 6
打印 上一主题 下一主题

Python如何通过COM向autoCAD传递坐标 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-21 15:49 |只看该作者 |倒序浏览
安装上pythonwin32模块后,用Python连接上了AutoCAD,一些查询的方法和属性使用正常,也可以查询到实体的坐标,返回的坐标是Tuple类型的。但是在涉及到向CAD传输坐标的时候就会报错。
例如,在AutoCAD中加点的方法是object.AddPoint(Point) ,手册中对这个方法的Point参数是这样描述的:

Point:Variant (three-element array of doubles); input-only
The coordinates of the point to be created.

我无论把Point定义为Tuple、List、字符串类型,都无法通过,在网上也没查到结果,有哪位朋友能给我一些帮助吗?谢谢了。

错误信息是(Python3.01 + pywin32-213.win32-py3.0):
>>> ms.addpoint(pt)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<COMObject <unknown>>", line 2, in addpoint
pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147024809), None)

[ 本帖最后由 风雨中... 于 2009-4-21 17:42 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-04-21 15:58 |只看该作者
CAD 模块 ?
python 很好很强大
这方面不太了解
顺便帮问一下,报的错误是什么呀 ?

论坛徽章:
0
3 [报告]
发表于 2009-04-21 16:02 |只看该作者
是不是Point的数据类型是Array或是元组,长度为3(对应 x, y, z 三坐标),而元素类型是double

[ 本帖最后由 izhier 于 2009-4-21 16:04 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2009-04-21 17:40 |只看该作者
原帖由 izhier 于 2009-4-21 15:58 发表
CAD 模块 ?
python 很好很强大
这方面不太了解
顺便帮问一下,报的错误是什么呀 ?


不是CAD模块,那叫什么?是COM吧,装上PythonWin模块就可以操作支持COM编程的程序。

错误信息是:
>>> ms.addpoint(pt)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<COMObject <unknown>>", line 2, in addpoint
pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147
024809), None)

按照Python的解释,在COM编程时它可以把数据“自动转换”(英文不好,我个人的理解)为所需要的variant类型,但不知道为什么会出错……

论坛徽章:
0
5 [报告]
发表于 2009-04-21 17:44 |只看该作者
原帖由 izhier 于 2009-4-21 16:02 发表
是不是Point的数据类型是Array或是元组,长度为3(对应 x, y, z 三坐标),而元素类型是double


基本上是这样的,我试过Array类型了,也不行,不知道是不是PythonWin的Com功能有局限啊?

论坛徽章:
0
6 [报告]
发表于 2011-08-16 18:03 |只看该作者
我也遇到类似的问题

pt=array.array("d",[0,0,0])
point=ms.AddPoint(pt)

错误为:
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    point=ms.AddPoint(pt)
  File "<COMObject <unknown>>", line 2, in AddPoint
TypeError: Internal error - the buffer length is not the sequence length!

我是新手菜鸟,希望高手们能帮帮忙,先谢谢了。

论坛徽章:
0
7 [报告]
发表于 2013-05-31 18:15 |只看该作者
之前我也遇到这个问题,没法处理,只能耐心等待+换用pyautocad模块
终于pywin32的作者出手解决了这个问题。

看例程:

import win32com.client
import pythoncom

def POINT(x,y,z):
   return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x,y,z))  
   
acad = win32com.client.Dispatch("AutoCAD.Application")  
doc = acad.ActiveDocument  
ms = doc.ModelSpace  

doc.Utility.Prompt("hello World\n")
pt1= POINT(0.0,0.0,0.0)
pt2= POINT(100.0,100.0,0.0)
ms.AddLine( pt1, pt2 )

这个POINT结构是关键,pywin32作者亲自说明,用于com中需要VARIANT类型变量的时候
参看此页 http://pywin32.hg.sourceforge.ne ... m/HTML/variant.html
版本 Python3.3  pywin32-218.win32-py3.3 AutoCAD2004

不是我翻老帖,只是这个贴google 百度收录的早,我相信看的人很多。这里放上答案会比较圆满。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP