Thomas Heller | 8 Jan 2009 18:54

Re: Problem with AutoCAD

Kelie schrieb:
> Ronan,
> 
> There is a cast function. But the syntax isn't much shorter.
> 
> import comtypes
> import comtypes.gen.AutoCAD as ACAD
> 
> app = comtypes.client.GetActiveObject("AutoCAD.Application")
> obj = app.ActiveDocument.ModelSpace(1)
> 
> #Assume this obj is a block
> block = comtypes.cast(obj, comtypes.POINTER(ACAD.IAcadBlockReference))
> # I hope this would work
> # block = comtypes.cast(obj, ACAD.IAcadBlockReference)
> 
> print "Layer name: %s" % block.Layer
> print "Color: %d" % block.Color
> print block.InsertionPoint
> print block.XScaleFactor
> 
> But I'm getting error when trying to access various block properties. See output
> below. Apparently it was OK to access the "Layer" and "Color" properties. Don't
> know why. Waiting for Thomas...

I haven't followed this thread so I do not really know what
all this is about.

Anyway, you should NOT use 'cast' with COM object pointers. Last,
but not least, 'cast' doesn't handle the COM reference count correctly.
(Continue reading)

Kelie | 8 Jan 2009 23:50
Picon

Re: Problem with AutoCAD

Thomas Heller <theller <at> ...> writes:
> 
> Anyway, you should NOT use 'cast' with COM object pointers. Last,
> but not least, 'cast' doesn't handle the COM reference count correctly.
> 
> Please use 'QueryInterface' with them, like so:
> 
> block = obj.QueryInterface(ACAD.IAcadBlockReference)
> 

Thanks Thomas. I'm getting an error using QueryInterface.

> "C:\Python25\pythonw.exe"  "C:\Python25\codes\autocad\temp\cast_test.py" 
Traceback (most recent call last):
  File "C:\Python25\codes\autocad\temp\cast_test.py", line 9, in <module>
    block = obj.QueryInterface(ACAD.IAcadBlockReference)
  File "C:\Python25\Lib\site-packages\comtypes\__init__.py", line 1069, in
QueryInterface
    self.__com_QueryInterface(byref(iid), byref(p))
_ctypes.COMError: (-2147467262, 'No such interface supported', (None, None,
None, 0, None))

This is what I got from shell:
>>> import comtypes.gen.AutoCAD as ACAD
>>> ACAD.IAcadBlockReference
<class
'comtypes.gen._851A4561_F4EC_4631_9B0C_E7DC407512C9_0_1_0.IAcadBlockReference'>

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
(Continue reading)

Thomas Heller | 9 Jan 2009 19:19

Re: Problem with AutoCAD

Kelie schrieb:
> Thomas Heller <theller <at> ...> writes:
>> 
>> Anyway, you should NOT use 'cast' with COM object pointers. Last,
>> but not least, 'cast' doesn't handle the COM reference count correctly.
>> 
>> Please use 'QueryInterface' with them, like so:
>> 
>> block = obj.QueryInterface(ACAD.IAcadBlockReference)
>> 
> 
> Thanks Thomas. I'm getting an error using QueryInterface.
> 
>> "C:\Python25\pythonw.exe"  "C:\Python25\codes\autocad\temp\cast_test.py" 
> Traceback (most recent call last):
>   File "C:\Python25\codes\autocad\temp\cast_test.py", line 9, in <module>
>     block = obj.QueryInterface(ACAD.IAcadBlockReference)
>   File "C:\Python25\Lib\site-packages\comtypes\__init__.py", line 1069, in
> QueryInterface
>     self.__com_QueryInterface(byref(iid), byref(p))
> _ctypes.COMError: (-2147467262, 'No such interface supported', (None, None,
> None, 0, None))

I guess this means, well, that the interface is not supported.

BTW:  Sometimes, interfaces ARE supported by COM objects but cannot
be marshalled across process boundaries.  You also get the above error
in this case.

--

-- 
(Continue reading)


Gmane