Discussion:
[comtypes-users] problem using comtypes with AutoCAD
Kelie
2008-04-03 17:51:09 UTC
Permalink
Hello group,

I'm getting an error when trying to launch AutoCAD using comtypes. The
application does get started. But the variable 'app' does not seem to hold a
valid instance of it, because I was not able to access its properties such as
ActiveDocument, WindowState, etc..
import comtypes
comtypes.__version__
'0.4.2'
app = comtypes.client.CreateObject("AutoCAD.Application")
# Generating comtypes.gen.AutoCAD
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Programming\Python\Install\comtypes\comtypes\client\__init__.py",
line 202, in CreateObject
return _manage(obj, clsid, interface=interface)
File "C:\Programming\Python\Install\comtypes\comtypes\client\__init__.py",
line 170, in _manage
obj = GetBestInterface(obj)
File "C:\Programming\Python\Install\comtypes\comtypes\client\__init__.py",
line 133, in GetBestInterface
interface = getattr(mod, itf_name)
AttributeError: 'module' object has no attribute 'IAcadApplication'

Any idea why?

(btw, if I remember correctly, I didn't used to get this error. But I was using
an earlier version comtypes and an earlier version of AutoCAD.)

Thanks.
Thomas Heller
2008-04-03 18:18:02 UTC
Permalink
Post by Kelie
Hello group,
I'm getting an error when trying to launch AutoCAD using comtypes. The
application does get started. But the variable 'app' does not seem to hold a
valid instance of it, because I was not able to access its properties such as
ActiveDocument, WindowState, etc..
import comtypes
comtypes.__version__
'0.4.2'
app = comtypes.client.CreateObject("AutoCAD.Application")
# Generating comtypes.gen.AutoCAD
File "<interactive input>", line 1, in <module>
File "C:\Programming\Python\Install\comtypes\comtypes\client\__init__.py",
line 202, in CreateObject
return _manage(obj, clsid, interface=interface)
File "C:\Programming\Python\Install\comtypes\comtypes\client\__init__.py",
line 170, in _manage
obj = GetBestInterface(obj)
File "C:\Programming\Python\Install\comtypes\comtypes\client\__init__.py",
line 133, in GetBestInterface
interface = getattr(mod, itf_name)
AttributeError: 'module' object has no attribute 'IAcadApplication'
Any idea why?
(btw, if I remember correctly, I didn't used to get this error. But I was using
an earlier version comtypes and an earlier version of AutoCAD.)
You should remove *all* the files in the comtyes\gen directory and try again.

Thomas
Kelie
2008-04-03 19:28:41 UTC
Permalink
Post by Thomas Heller
You should remove *all* the files in the comtyes\gen directory and try again.
Thanks Thomas. That didn't work. When I said "I didn't used to get this error",
I forgot to mention that I was using a different computer too.
Thomas Heller
2008-04-04 15:51:43 UTC
Permalink
Post by Kelie
Hello group,
I'm getting an error when trying to launch AutoCAD using comtypes. The
application does get started. But the variable 'app' does not seem to hold a
valid instance of it, because I was not able to access its properties such as
ActiveDocument, WindowState, etc..
import comtypes
comtypes.__version__
'0.4.2'
app = comtypes.client.CreateObject("AutoCAD.Application")
# Generating comtypes.gen.AutoCAD
File "<interactive input>", line 1, in <module>
File "C:\Programming\Python\Install\comtypes\comtypes\client\__init__.py",
line 202, in CreateObject
return _manage(obj, clsid, interface=interface)
File "C:\Programming\Python\Install\comtypes\comtypes\client\__init__.py",
line 170, in _manage
obj = GetBestInterface(obj)
File "C:\Programming\Python\Install\comtypes\comtypes\client\__init__.py",
line 133, in GetBestInterface
interface = getattr(mod, itf_name)
AttributeError: 'module' object has no attribute 'IAcadApplication'
Any idea why?
Hm, the exception seems to be raised when the module is generated. Please look
into the comtypes\gen directory, do you find a (valid) AutoCAD.py file, and the file
that is imported by AutoCad.py?

If so, can you please look into the latter if there is a 'IAcadApplication' interface
defined?

If not, you should probably set a breakpoint on line 132 in comtypes\client\__init__.py,
and inspect the generated module (which has not yet been written to disk). Or put a line
print dir(mod)
must before line 133 in this file. This should provide some insight.

Thomas
Ed Blake
2008-04-04 16:23:54 UTC
Permalink
I just tested under comtypes 0.3 and 0.4 with Autocad 2008. I get this
exception for both when using CreateObject:
######################################################################
Post by Kelie
Post by Kelie
acad = client.CreateObject("AutoCAD.Application")
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line 203,
in CreateObject
return _manage(obj, clsid, interface=interface)
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line 171,
in _manage
obj = wrap(obj)
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line 122,
in wrap
typeattr = tinfo.GetTypeAttr()
File "D:\Python25\Lib\site-packages\comtypes\typeinfo.py", line 262, in
GetTypeAttr
return _deref_with_release(self._GetTypeAttr(), self.ReleaseTypeAttr)
COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0,
None))
######################################################################

I think this happens because comtypes is trying to talk to Autocad before it
is finished its post startup initialization (loading modules, CUI, lisp
files).

Using client.GetActiveObject on the other hand successfully generates the
Autocad modules and returns a pointer to the first running instance. It
doesn't work if Autocad isn't running though.

BTW Thomas: I had to switch back to version 0.3.3 because something broke
between that version and the current. When I try to get a pointer to
Microstation under 0.4.1 I get this:
######################################################################
Post by Kelie
Post by Kelie
usta = client.GetActiveObject("MicroStationDGN.Application")
# Generating comtypes.gen._CF9F97BF_39F2_4B8E_835C_8BE9E99DAF5B_0_8_0
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line 166,
in GetActiveObject
return _manage(obj, clsid, interface=interface)
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line 171,
in _manage
obj = wrap(obj)
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line 134,
in wrap
mod = GetModule(tlib)
File "D:\Python25\lib\site-packages\comtypes\client\_generate.py", line
100, in GetModule
is_current, mod = _CreateWrapper(tlib, pathname)
File "D:\Python25\lib\site-packages\comtypes\client\_generate.py", line
218, in _CreateWrapper
mod = _my_import(fullname)
File "D:\Python25\lib\site-packages\comtypes\client\_generate.py", line 14,
in _my_import
return __import__(fullname, globals(), locals(), ['DUMMY'])
File
"D:\Python25\lib\site-packages\comtypes\gen\_CF9F97BF_39F2_4B8E_835C_8BE9E99DAF5B_0_8_0.py",
line 223, in <module>
( ['retval', 'out'], POINTER(_midlSAFEARRAY(POINTER(_DataBlock))), 'pVal'
)),
File "D:\Python25\Lib\site-packages\comtypes\safearray.py", line 18, in
_midlSAFEARRAY
sa_type = _make_safearray_type(itemtype)
File "D:\Python25\Lib\site-packages\comtypes\safearray.py", line 46, in
_make_safearray_type
raise TypeError(itemtype)
TypeError: <class 'comtypes.POINTER(_DataBlock)'>
######################################################################

Something to do with the safearray refactor?

Ed Blake
Post by Kelie
Post by Kelie
Hello group,
I'm getting an error when trying to launch AutoCAD using comtypes. The
application does get started. But the variable 'app' does not seem to
hold a
Post by Kelie
valid instance of it, because I was not able to access its properties
such as
Post by Kelie
ActiveDocument, WindowState, etc..
import comtypes
comtypes.__version__
'0.4.2'
app = comtypes.client.CreateObject("AutoCAD.Application")
# Generating comtypes.gen.AutoCAD
File "<interactive input>", line 1, in <module>
File
"C:\Programming\Python\Install\comtypes\comtypes\client\__init__.py",
Post by Kelie
line 202, in CreateObject
return _manage(obj, clsid, interface=interface)
File
"C:\Programming\Python\Install\comtypes\comtypes\client\__init__.py",
Post by Kelie
line 170, in _manage
obj = GetBestInterface(obj)
File
"C:\Programming\Python\Install\comtypes\comtypes\client\__init__.py",
Post by Kelie
line 133, in GetBestInterface
interface = getattr(mod, itf_name)
AttributeError: 'module' object has no attribute 'IAcadApplication'
Any idea why?
Hm, the exception seems to be raised when the module is generated. Please
look
into the comtypes\gen directory, do you find a (valid) AutoCAD.py file, and
the file
that is imported by AutoCad.py?
If so, can you please look into the latter if there is a 'IAcadApplication'
interface
defined?
If not, you should probably set a breakpoint on line 132 in
comtypes\client\__init__.py,
and inspect the generated module (which has not yet been written to disk).
Or put a line
print dir(mod)
must before line 133 in this file. This should provide some insight.
Thomas
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
Post by Kelie
_______________________________________________
comtypes-users mailing list
https://lists.sourceforge.net/lists/listinfo/comtypes-users
Thomas Heller
2008-04-04 17:41:25 UTC
Permalink
Post by Ed Blake
I just tested under comtypes 0.3 and 0.4 with Autocad 2008. I get this
######################################################################
acad = client.CreateObject("AutoCAD.Application")
File "<input>", line 1, in <module>
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line 203,
in CreateObject
return _manage(obj, clsid, interface=interface)
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line 171,
in _manage
obj = wrap(obj)
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line 122,
in wrap
typeattr = tinfo.GetTypeAttr()
File "D:\Python25\Lib\site-packages\comtypes\typeinfo.py", line 262, in
GetTypeAttr
return _deref_with_release(self._GetTypeAttr(), self.ReleaseTypeAttr)
COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0,
None))
######################################################################
I think this happens because comtypes is trying to talk to Autocad before it
is finished its post startup initialization (loading modules, CUI, lisp
files).
Hm, is this a bug in Autocad, or is comtypes behaving wrong here; does this
error code mean we have to wait a little and retry the call? How do other
COM clients, like VB or pywin32, behave?
Post by Ed Blake
Using client.GetActiveObject on the other hand successfully generates the
Autocad modules and returns a pointer to the first running instance. It
doesn't work if Autocad isn't running though.
BTW Thomas: I had to switch back to version 0.3.3 because something broke
between that version and the current.
With this remark I assume you mean the code below, right?
Post by Ed Blake
When I try to get a pointer to
######################################################################
usta = client.GetActiveObject("MicroStationDGN.Application")
# Generating comtypes.gen._CF9F97BF_39F2_4B8E_835C_8BE9E99DAF5B_0_8_0
File "<input>", line 1, in <module>
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line 166,
in GetActiveObject
return _manage(obj, clsid, interface=interface)
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line 171,
in _manage
obj = wrap(obj)
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line 134,
in wrap
mod = GetModule(tlib)
File "D:\Python25\lib\site-packages\comtypes\client\_generate.py", line
100, in GetModule
is_current, mod = _CreateWrapper(tlib, pathname)
File "D:\Python25\lib\site-packages\comtypes\client\_generate.py", line
218, in _CreateWrapper
mod = _my_import(fullname)
File "D:\Python25\lib\site-packages\comtypes\client\_generate.py", line 14,
in _my_import
return __import__(fullname, globals(), locals(), ['DUMMY'])
File
"D:\Python25\lib\site-packages\comtypes\gen\_CF9F97BF_39F2_4B8E_835C_8BE9E99DAF5B_0_8_0.py",
line 223, in <module>
( ['retval', 'out'], POINTER(_midlSAFEARRAY(POINTER(_DataBlock))), 'pVal'
)),
File "D:\Python25\Lib\site-packages\comtypes\safearray.py", line 18, in
_midlSAFEARRAY
sa_type = _make_safearray_type(itemtype)
File "D:\Python25\Lib\site-packages\comtypes\safearray.py", line 46, in
_make_safearray_type
raise TypeError(itemtype)
TypeError: <class 'comtypes.POINTER(_DataBlock)'>
######################################################################
Something to do with the safearray refactor?
In comtypes 0.4.1, safearrays containing com pointers are not implemented.
They are, though, in the current SVN version. I wanted to release the next version
after another MicroStation bug was examined, but the OP did not yet respond to
my question. Maybe you can help; or I should release anyway? See
http://sourceforge.net/tracker/index.php?func=detail&aid=1919272&group_id=115265&atid=692940

Thomas
Kelie
2008-04-04 20:09:53 UTC
Permalink
Post by Thomas Heller
Hm, is this a bug in Autocad, or is comtypes behaving wrong here; does this
error code mean we have to wait a little and retry the call? How do other
COM clients, like VB or pywin32, behave?
Thomas, pywin32 does not have such as problem. I can launch AutoCAD with:

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

Btw, AutoCAD has built-in VBA support.
Kelie
2008-04-05 20:16:21 UTC
Permalink
Post by Thomas Heller
In comtypes 0.4.1, safearrays containing com pointers are not implemented.
They are, though, in the current SVN version. I wanted to release the next version
after another MicroStation bug was examined, but the OP did not yet respond to
my question.
Thomas, Sorry I didn't get a chance to try what you suggested. I'll do that
Monday. Thanks.
Kelie
2008-04-07 17:46:26 UTC
Permalink
Post by Kelie
Thomas, Sorry I didn't get a chance to try what you suggested. I'll do that
Monday. Thanks.
Thomas, I just installed the lastest svn version and seems the problem has been
fixed.
Thank you!
Post by Kelie
import comtypes.client
comtypes.client.CreateObject("AutoCAD.Application")
# Generating comtypes.gen._851A4561_F4EC_4631_9B0C_E7DC407512C9_0_1_0
# Generating comtypes.gen._00020430_0000_0000_C000_000000000046_0_2_0
# Generating comtypes.gen.stdole
# Generating comtypes.gen.AutoCAD
<POINTER(IAcadApplication) ptr=0x2a72ec at 3c46c60>
Ed Blake
2008-04-05 19:19:33 UTC
Permalink
Post by Ed Blake
Post by Ed Blake
I just tested under comtypes 0.3 and 0.4 with Autocad 2008. I get this
######################################################################
acad = client.CreateObject("AutoCAD.Application")
File "<input>", line 1, in <module>
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line
203,
Post by Ed Blake
in CreateObject
return _manage(obj, clsid, interface=interface)
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line
171,
Post by Ed Blake
in _manage
obj = wrap(obj)
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line
122,
Post by Ed Blake
in wrap
typeattr = tinfo.GetTypeAttr()
File "D:\Python25\Lib\site-packages\comtypes\typeinfo.py", line 262, in
GetTypeAttr
return _deref_with_release(self._GetTypeAttr(), self.ReleaseTypeAttr)
COMError: (-2147418111, 'Call was rejected by callee.', (None, None,
None, 0,
Post by Ed Blake
None))
######################################################################
I think this happens because comtypes is trying to talk to Autocad before
it
Post by Ed Blake
is finished its post startup initialization (loading modules, CUI, lisp
files).
Hm, is this a bug in Autocad, or is comtypes behaving wrong here; does this
error code mean we have to wait a little and retry the call? How do other
COM clients, like VB or pywin32, behave?
I think this is their (Autodesk's) intended behaviour. This is the same
error you receive if a modal dialog is shown by the program or a command is
already running.
Post by Ed Blake
Post by Ed Blake
Using client.GetActiveObject on the other hand successfully generates the
Autocad modules and returns a pointer to the first running instance. It
doesn't work if Autocad isn't running though.
BTW Thomas: I had to switch back to version 0.3.3 because something broke
between that version and the current.
With this remark I assume you mean the code below, right?
Right
Post by Ed Blake
Post by Ed Blake
When I try to get a pointer to
######################################################################
usta = client.GetActiveObject("MicroStationDGN.Application")
# Generating comtypes.gen._CF9F97BF_39F2_4B8E_835C_8BE9E99DAF5B_0_8_0
File "<input>", line 1, in <module>
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line
166,
Post by Ed Blake
in GetActiveObject
return _manage(obj, clsid, interface=interface)
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line
171,
Post by Ed Blake
in _manage
obj = wrap(obj)
File "D:\Python25\Lib\site-packages\comtypes\client\__init__.py", line
134,
Post by Ed Blake
in wrap
mod = GetModule(tlib)
File "D:\Python25\lib\site-packages\comtypes\client\_generate.py", line
100, in GetModule
is_current, mod = _CreateWrapper(tlib, pathname)
File "D:\Python25\lib\site-packages\comtypes\client\_generate.py", line
218, in _CreateWrapper
mod = _my_import(fullname)
File "D:\Python25\lib\site-packages\comtypes\client\_generate.py", line
14,
Post by Ed Blake
in _my_import
return __import__(fullname, globals(), locals(), ['DUMMY'])
File
"D:\Python25\lib\site-packages\comtypes\gen\_CF9F97BF_39F2_4B8E_835C_8BE9E99DAF5B_0_8_0.py",
Post by Ed Blake
Post by Ed Blake
line 223, in <module>
( ['retval', 'out'], POINTER(_midlSAFEARRAY(POINTER(_DataBlock))),
'pVal'
Post by Ed Blake
)),
File "D:\Python25\Lib\site-packages\comtypes\safearray.py", line 18, in
_midlSAFEARRAY
sa_type = _make_safearray_type(itemtype)
File "D:\Python25\Lib\site-packages\comtypes\safearray.py", line 46, in
_make_safearray_type
raise TypeError(itemtype)
TypeError: <class 'comtypes.POINTER(_DataBlock)'>
######################################################################
Something to do with the safearray refactor?
In comtypes 0.4.1, safearrays containing com pointers are not implemented.
They are, though, in the current SVN version. I wanted to release the next
version
after another MicroStation bug was examined, but the OP did not yet respond
to
my question. Maybe you can help; or I should release anyway? See
http://sourceforge.net/tracker/index.php?func=detail&aid=1919272&group_id=115265&atid=692940
Post by Ed Blake
Thomas
I can't figure out what that uuid is for, but it looks similar to the id of
one of my generated modules? I don't think you can interpret unique
identifiers that way, but if I'm right it means his modules didn't generate
correctly in the first place. I looked in my generated modules and in the
typelib but couldn't find anything relevant. Even though I have access to
Microstation I can't reproduce the error because I have never and probably
will not use the database linkage feature. I would suggest the user look at
using an alternate method if regenerating his Microstation modules doesn't
work - either send keyin commands, use xdata/datablocks/tag data, or access
the database directly from Python...

I think you should make a release, if nothing else it will shake out some new
bugs. BTW is there any new server side stuff in the next release? I've got
this crazy idea about automating Microstation by calling out to Python that
I'd like to try. I think it would be easier to get distributed where I work
if I can just package up a server and a (dummy) vba script...

Ed Blake
Thomas Heller
2008-04-09 19:22:04 UTC
Permalink
Post by Ed Blake
I think you should make a release, if nothing else it will shake out some new
bugs.
Done, see separate announcement.
Post by Ed Blake
BTW is there any new server side stuff in the next release? I've got
this crazy idea about automating Microstation by calling out to Python that
I'd like to try. I think it would be easier to get distributed where I work
if I can just package up a server and a (dummy) vba script...
Not yet. I plan to integrate the server-side stuff from my upstream
repository in the 0.5.x series. Apart from that, I have some documentation
to write and prepare some server samples.

Thomas

Loading...