You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pyautocad创建AutoCAD组时调用AppendItems方法触发TypeError错误的求助

使用pyautocad创建AutoCAD组时调用AppendItems方法触发TypeError错误的求助

我目前在使用pyautocad库编写AutoCAD绘图脚本,想把绘制好的线条对象归到同一个组里,但调用AppendItems方法时总是报错,看起来是传入的对象格式不符合COM对象的要求。

我的代码如下:

from pyautocad import Autocad, APoint, aDouble
import win32com.client

acadApp = win32com.client.Dispatch("AutoCAD.Application")

for doc in acadApp.Documents:
    if doc.Name == "Template.dwg":
        acadApp.ActiveDocument = doc

acad = Autocad()

group_name = "MyGroup"
group = doc.Groups.Add(group_name)

line1 = acad.model.AddLine(APoint(0, 0), APoint(100, 100))
line2 = acad.model.AddLine(APoint(100, 100), APoint(200, 0))

group.AppendItems([line1, line2])

运行后收到的错误提示是:

File "COMObject Add", line 2, in AppendItems

TypeError: Objects for SAFEARRAYS must be sequences (of sequences), or a buffer object.

我尝试过多种格式调整,比如:

group.AppendItems([[line1], [line2]])
group.AppendItems(((line1,), (line2,)))

或者逐个添加的方式:

for obj in (line1, line2):
    try:
        group.AppendItems([[obj]])  # Each object wrapped as a list inside a list
        print(f"Successfully appended object with Handle: {obj.Handle}")
    except Exception as e:
        print(f"Failed to append object with Handle {obj.Handle}: {e}")

甚至尝试手动创建SAFEARRAY:

sa = SafeArrayCreateVector(VT_VARIANT, 0, 2)
SafeArrayPutElement(sa, 0, line1)
SafeArrayPutElement(sa, 1, line2)

# Append the SAFEARRAY to the group
group.AppendItems(sa)

但这些方法都没能解决问题,有没有大佬能帮我看看应该怎么正确传入对象列表?

备注:内容来源于stack exchange,提问作者HG73

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 10:48:11