【问题标题】:Variant Errors with Win32com, Python and AutoCADWin32com、Python 和 AutoCAD 的变体错误
【发布时间】:2021-04-11 20:38:31
【问题描述】:

this answer 的启发,我正在尝试使用 python 和 win32com.client 来操作打开的 AutoCAD 文件,并将给定图层中的所有对象收集到一个选择集中:

from comtypes.client import *
from comtypes.automation import *
import win32com.client

acad = GetActiveObject("AutoCAD.Application")
doc = acad.ActiveDocument
SSet = doc.SelectionSets[0]

FilterType = win32com.client.VARIANT(VT_ARRAY|VT_I2, [8]) 
FilterData = win32com.client.VARIANT(VT_ARRAY|VT_VARIANT, ["Layer1"])
SSet.Select(5, FilterType, FilterData)

选择命令会出现以下错误消息:

ArgumentError: argument 2: <class 'TypeError'>: Cannot put win32com.client.VARIANT(8194, [8]) in VARIANT

我模糊地理解错误,因为它抱怨第二个参数的类型/格式(可能还有第三个,如果它已经那么远了)但我不明白为什么:它似乎告诉我它不能在需要 VARIANT 的插槽中接受特定的 VARIANT,但我不知道为什么。

请记住,我精通 python、AutoCAD 和老式 AutoLISP 编码,但对 win32com(或任何其他 com)、特别是变体或让 AutoCAD 工作几乎一无所知与蟒蛇。

(对于其他老派:我正在尝试模仿 SSGET 命令。)

【问题讨论】:

  • @LeeMac 抓住机会并伸出援手——刚刚注意到我引用的答案是你的。这些年来,我从您的网站学到了很多东西。

标签: python win32com autocad


【解决方案1】:

就我个人而言,我对选择集不是很有经验,所以我偶然发现了一个没有使用它们的解决方案。下面的代码是一个循环遍历模型空间中每个对象的示例,检查它是否具有特定层,并构建一个字符串,该字符串将通过SendCommand 选择所有对象。

我相信您实际上也可以使用SendCommand 来操作选择集。 (类似于 autolisp 中的 (command "ssget"))我只是个人发现这个解决方案更容易解决。

# I personally had better luck with comtypes than with win32com
import comtypes.client
try:
    # Get an active instance of AutoCAD
    app = comtypes.client.GetActiveObject('AutoCAD.Application', dynamic=True)
except WindowsError: # No active instance found, create a new instance.
    app = comtypes.client.CreateObject('AutoCAD.Application', dynamic=True)
    # if you receive dispatch errors on the line after this one, a sleep call can
    # help so it's not trying to dispatch commands while AutoCAD is still starting up.
    # you could put it in a while statement for a fuller solution

app.Visible = True
doc = app.ActiveDocument
# if you get the best interface on an object, you can investigate its properties with 'dir()'
m = comtypes.client.GetBestInterface(doc.ModelSpace)
handle_string = 'select'
for entity in m:
    if entity.Layer == 'Layer1':
        handle_string += ' (handent "'+entity.Handle+'")'

handle_string += '\n\n'
doc.SendCommand(handle_string)

【讨论】:

  • 这不是我想要的答案,但却是我真正需要的答案。
【解决方案2】:

在 VBA 引用中,签名是:

object.Select Mode[, Point1][, Point2][, FilterType][, FilterData]

试试这个:

SSet.Select(5, None,  None,  FilterType, FilterData)

【讨论】:

    猜你喜欢
    • 2012-03-15
    • 2023-03-24
    • 2012-03-08
    • 1970-01-01
    • 2022-07-21
    • 2018-06-23
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多