[python-win32] howto get the typename of a comobject

Andreas Holtz A.Holtz at gmx.net
Thu Feb 5 22:07:03 CET 2015


Hi Anton,

I think you need to rely to generated Python-Com-objects aka early binding.
See
http://timgolden.me.uk/python/win32_how_do_i/generate-a-static-com-proxy.html
for details.
There was another page explaining the differences really nicely between late and early binding and 
how to enforce the generation of the Python files.
Unfortunately I can't find it right now. I think it was on Tim Goldens or Mark Hammonds page. 
Perhaps they can help.

Nevertheless I can't recommend this way for CATIA! My experience is, that at least for CATIA the 
amount of functions you can use is smaller from early-binding-objects than from 
late-binding-objects. Which means you can't use a function although it should be there.

For CATIA I recommend instead using the ".Name"-property which you can use on every object. This 
helps at least in an interactive session, so you know what you are currently dealing with.

If you want to identify what the user selected or what kind of document the active document is, use 
the ".Name"-method of catia.ActiveDocument and check the file extension:

catia = win32com.client.Dispatch("CATIA.Application")
ext = os.path.splitext(catia.ActiveDocument.Name)[1].lower()

if ext == ".catpart": handleCatPart()
elif ....

If this is too unsafe for you, try getting specific properties and catch exceptions:

try:
   catia.ActiveDocument.Part
   isPart = True
except pywintypes.com_error:
   isPart = False

Another disadvantage of using EnsureDispatch instead of Dispatch is, that the user has to have 
write-rights on the Python-folder as the files are generated into a subfolder.

Good luck with CATIA, you will need it!

Andreas

anton schrieb am 03.02.2015 um 23:49:
> Hi,
>
> I an using python
> using win32com.client to
> pilot CATIA.
>
> I try to get the structure of the loaded CATProducts
> CATParts and CATDrawings.
>
> For this I would like to get the typename of the com
> objects like "Document" or "Product".
>
> In the vba example people use the VBA function TypeName()
> to get the name of the type as string.
>
> Unfortunately I didn't find an equivalent function for python
> (or at least a way to achieve the same result).
>
> The python type function is here not of use (I get always "COMObject").
>
> Do you have a hint or small example?
>
> Thanks
>
> Anton
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> https://mail.python.org/mailman/listinfo/python-win32
>


More information about the python-win32 mailing list