-
-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question about comtypes.client typing for Autocad automation #516
Comments
Hello. I'm excited that there's growing interest in static typing for this project. Certainly, it is possible to use the features of Python typing (or features committed to the current However, I don't have AutoCAD. In order to provide appropriate advice, I'd need some information (assuming no license or NDA problems):
Once we have these information, I might be able to do static typing with your script. |
To be honest, I use ZWCad, not Autocad, but ZWCad interface, commands and even API are identical to those from Autocad. I created a public gist with generated module. Types of from array import array
import comtypes.client as cc
from pathlib import Path
zwcad_tlib_path: Path = Path('C:/Program Files/Common Files/ZWSoft Shared/zwcad21.tlb')
assert zwcad_tlib_path.exists()
ZWCAD = cc.GetModule(str(zwcad_tlib_path))
app = cc.GetActiveObject('ZWCAD.Application.2024')
app.Visible = True
print(f'{type(app) = }') # type(app) = <class 'comtypes.POINTER(IZcadApplication)'>
ms = app.ActiveDocument.ModelSpace
print(f'{type(ms) = }') # type(ms) = <class 'comtypes.POINTER(IZcadModelSpace)'>
pt = array('d', [0, 0, 0])
point = ms.AddPoint(pt)
print(f'{type(point) = }') # type(point) = <class 'comtypes.POINTER(IZcadPoint)'>
# app.ActiveDocument.Close()
# app.Quit() |
Surprisingly for me, gist is very slow. I created a new repo. Now generated module is here: https://github.com/sindzicat/zwcad-comtypes/blob/main/_2F671C10_669F_11E7_91B7_BC5FF42AC839_0_1_0.py |
Thank you for sharing the information. This is an intriguing use case. In from array import array
import comtypes.client as cc
from pathlib import Path
zwcad_tlib_path: Path = Path('C:/Program Files/Common Files/ZWSoft Shared/zwcad21.tlb')
assert zwcad_tlib_path.exists()
- ZWCAD = cc.GetModule(str(zwcad_tlib_path))
+ cc.GetModule(str(zwcad_tlib_path)) # Generate the module or ensure its existence.
+ from comtypes.gen import ZWCAD # importing statically to analyze static typing
- app = cc.GetActiveObject('ZWCAD.Application.2024')
+ # Type checkers can perform static type inference by specifying the interface.
+ # In this case, type checkers infer `app` as an `IZcadApplication` instance.
+ app = cc.GetActiveObject('ZWCAD.Application.2024', interface=ZWCAD.IZcadApplication)
app.Visible = True
print(f'{type(app) = }') # type(app) = <class 'comtypes.POINTER(IZcadApplication)'>
- ms = app.ActiveDocument.ModelSpace
+ ms: ZWCAD.IZcadModelSpace = app.ActiveDocument.ModelSpace
print(f'{type(ms) = }') # type(ms) = <class 'comtypes.POINTER(IZcadModelSpace)'>
pt = array('d', [0, 0, 0])
- point = ms.AddPoint(pt)
+ point: ZWCAD.IZcadPoint = ms.AddPoint(pt)
print(f'{type(point) = }') # type(point) = <class 'comtypes.POINTER(IZcadPoint)'>
# app.ActiveDocument.Close()
# app.Quit() In Currently, You might be concerned about the runtime type ( If you are interested in these matter, your contributions to enhance the functionality of |
We have released comtypes==1.3.1. Members of the COM interfaces in the If your questions have been resolved, please close this issue. |
@junkmd, many thanks for your help!
I have only basis knowledges about type hints in Python, but saw this answer on StackOverflow about hinting for COM objects. I hope the following may be useful:
|
Thank you! The approach using the protocol outlined in the answer on Stack Overflow that you shared requires developers to be familiar with the COM interface they intend to use. It seems necessary to enhance parsers and code generators. |
Sorry, it seems, I was unclear. I meant to use Protocol in comtypes library itself in generated modules, not for comtypes users. |
Never mind! I have considered including There was a time when I thought overriding methods of executable COM interfaces to define statically typed methods. Instead, I was attempting to auto-generate typestubs that define After that, I realized that static typehints for methods without overriding could be applied using This approach might be frustrating for those who wish to perform metaprogramming based on the runtime behavior of type annotations. Your question allowed me to articulate my philosophy. Python's static typing is intended for asynchronous communication among community members, team colleagues, future maintainers, or even future versions of oneself. Moreover, I believe that information about "why such implementations?" should be preserved for the community. You provided me with an opportunity to do just that. Thank you. |
Hello!
There is a code sample in comtypes docs:
It's possible to use Python typing module for the code above (especially for
app
andms
variables)? If so, could you give me example of such code, please?The text was updated successfully, but these errors were encountered: