通过 makepy 添加了 AutoCAD 的 typelib,现在 win32com 不适用于 AutoCAD

发布于 2024-12-29 07:58:35 字数 1156 浏览 2 评论 0原文

我已经运行 win32com 来访问 AutoCAD 相当长一段时间了,没有出现任何问题。我了解到应用 makepy 实用程序可以在交互式提示下创建更加用户友好的体验,因此我运行它并添加了“AutoCAD 2006 类型库”。现在,我以前访问的一些常用属性不再可用。考虑一下代码:

acad = win32com.client("AutoCAD.Application")
doc = acad.Documents.Open('mydoc.dwg')
ms = doc.ModelSpace
count = ms.Count #counts all entities in dwg

for i in range(count):
    item = ms.Item(i)
    if 'block' in item.ObjectName.lower():
        print item.Name
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 462, in 
  __getattr__ raise AttributeError("'%s' object has no attribute '%s'" 
  (repr(self),attr))
AttributeError: '<win32com.gen_py.AutoCAD 2006 Type Library.IAcadEntity instance 
  at 0x34894552>' object has no attribute 'Name'

在我运行 makepy 之前,Name 一直是块的可访问属性。我尝试重新安装 Windows 版 Python,看看是否会重置它,但没有成功。

ma​​kepy 是否会在实现时创建另一个我需要删除的文件?

我的许多脚本都依赖于 Name 属性。如果您能提供任何建议或帮助,我们将不胜感激。

I have been running win32com to access AutoCAD for quite some time without issue. I learned that applying the makepy utility could create a more user friendly experience at the interactive prompt, so I ran it and added the "AutoCAD 2006 Type Library". Now, some common attributes that I used to access are no longer available. Consider the code:

acad = win32com.client("AutoCAD.Application")
doc = acad.Documents.Open('mydoc.dwg')
ms = doc.ModelSpace
count = ms.Count #counts all entities in dwg

for i in range(count):
    item = ms.Item(i)
    if 'block' in item.ObjectName.lower():
        print item.Name
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 462, in 
  __getattr__ raise AttributeError("'%s' object has no attribute '%s'" 
  (repr(self),attr))
AttributeError: '<win32com.gen_py.AutoCAD 2006 Type Library.IAcadEntity instance 
  at 0x34894552>' object has no attribute 'Name'

Name has always been an accessible attribute of a block until I ran makepy. I've tried reinstalling Python for windows to see if that would reset it, but it didn't work.

Does makepy create another file upon implementation that I need to remove?

So many of my scripts depend upon the Name attribute. Any suggestions or help you could offer would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流年里的时光 2025-01-05 07:58:35

此属性错误的主要原因是您的 COM 服务器已从后期绑定(动态)转变为早期绑定(静态)。

  • 在后期绑定中,每当调用方法时,都会在对象中查询该方法,如果成功,则可以进行调用。
  • 在早期绑定中,对象模型的信息是根据对象调用提供的类型信息预先确定的。早期绑定使用 MakePy。此外,早期绑定区分大小写。

有两种方法可以解决此问题:

  1. 使用动态模块强制代码以面向后期绑定的方式工作。使用示例:

    win32com.client.dynamic.Dispatch() 而不是 win32com.client.Dispatch()

  2. 使用驼峰式敏感关键字进行早期绑定导向方式。使用示例:

    excel.Visible() 而不是 excel.VISIBLE()excel.visible()

因此,请尝试使用 dynamic。调度或区分大小写的变量名称。

The main reason for this attribute error is because your COM-server has shifted from late-binding (dynamic) to early binding (static).

  • In Late Binding, whenever a method is called, the object is queried for the method and if it succeeds, then the call can be made.
  • In Early Binding, the information of the object model is determined in advance from type information supplied by the object call. Early binding makes use of MakePy. Also, early binding is case sensitive.

There are two ways to fix this issue:

  1. Use the dynamic module to force your code to work in a late-bound oriented way. Example use:

    win32com.client.dynamic.Dispatch() instead of win32com.client.Dispatch()

  2. Use camelcase sensitive keywords for the early bound oriented way. Example use:

    excel.Visible() instead of excel.VISIBLE() or excel.visible()

So try using dynamic.Dispatch or case-sensitive variable names.

孤者何惧 2025-01-05 07:58:35

当我运行 win32com 访问 DELMIA 时遇到类似的问题。我发现 delmia 有很多 com 内容作为 .tlb 文件。当我输入:

from win32com.client.gencache import EnsureDispatch
EnsureDispatch('DELMIA.Application')

Python 会自动在 win32com\gen_py 目录中生成一些内容,与 Word 或 Excel 的示例相同。但是,不同之处在于生成的内容仅来自 delmia 的一个 .tlb 文件。如果我访问`.tlb中的某个变量,那就没问题:

docs = delmia.Documents

如果我访问其他.tlb中的某个变量,我会得到类似的错误:

pdoc = docs.Add('Process')

Python说:

...对象没有属性...

所以我删除了 win32com\gen_py 目录中的所有文件,但保留 gen_py 目录,现在就可以了。

I encounter a similar problem when I run win32com to access DELMIA. I find out that delmia has lots of com stuff as .tlb files. When I type:

from win32com.client.gencache import EnsureDispatch
EnsureDispatch('DELMIA.Application')

Python will automatically generate some stuff in win32com\gen_py directory, same as example of Word or Excel. But, the difference is that the generated stuff is from only one .tlb file of delmia. If I access some variable in the `.tlb, it's ok:

docs = delmia.Documents

If I access some variable in other .tlb, I get a similar error:

pdoc = docs.Add('Process')

Python says that:

... object has no attribute ...

So I delete all files in the win32com\gen_py directory but keep gen_py directory, and it is ok now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文