2

I need to find a selected text object's coordinates in Autocad with python. For this purpose, i use comtypes. But all i found on the net was about getting the line and polyline objects' coordinates. Can you please supply me with the method i can use for this purpose? or in general, is there any source that i can learn about all attributes of an Autocad object and how i can benefit from them?

Many thanks in advance.

Shansal
  • 1,369
  • 5
  • 16
  • 26

1 Answers1

4

I'm an old-time AutoCAD user, and I think I can help you with this. It really helps if you have access to AutoCAD. But I think there is a reference somewhere. Try my blog posts:

http://tomsthird.blogspot.com/2009/07/autocad-and-python-part-2.html

http://tomsthird.blogspot.com/2009/07/accessing-autocad-civil-3d-object-model.html

http://tomsthird.blogspot.com/2009/07/autocad-civil-3d-activex-object-model.html

In one of those posts, it looks like I have a link to a reference that's a chm file that comes with AutoCAD. There ought to be better.

Here's how I did it in Visual LISP:

  (VLAX-GET-PROPERTY
    (VLAX-INVOKE
      (VLAX-GET-PROPERTY
        (VLAX-GET-PROPERTY (VLAX-GET-ACAD-OBJECT) 'ACTIVEDOCUMENT)
        'ACTIVESELECTIONSET
      )
      'ITEM
      0
    )
    'INSERTIONPOINT
  )

I assume that in something like Python you would want to do this:

import win32com.client
acad = win32com.client.Dispatch("AutoCAD.Application")
doc = acad.ActiveDocument
inspoint = doc.ActiveSelectionSet.Item.0.InsertionPoint

If this doesn't work for you, let me know, and we will work something out. I want to see you succeed, and I'd really appreciate it if you could share your finished code snippet here. As you can see from my blog posts, I, too, am interested in learning how to automate AutoCAD using Python. Maybe you can contact me and we can work together.

Tom Haws
  • 1,322
  • 1
  • 11
  • 23
  • Thanks, Mr. Haws. I will check out your blog. Lately, i tried the DXF way to manipulate Autocad. But comtypes way will be much more efficient. I am new to Autocad automation. As a first step, my goal is to develop a program which is capable of converting a text table inside Autocad to Excel table. It is actually for my own use in my work. Not for commercial purposes. I always liked sharing my achievements. So if i succeed, it will be no problem for me to share what i did. It would be a pleasure for me to work with an experienced partner like you. – Shansal Mar 23 '12 at 06:56
  • @Shansal, Tom Haws, I'm interested in AutoCAD Automation with Python too. I'm using Python scripts for automating tasks ffrom my electrical engineering work. I've made a small library (`comtypes` required). Feautures: simplifies work with coordinates (3D points), do efficient objects iteration (with casting to correct type), multitext format/unformat, optional Excel tables support (through `xlrd` and `tablib`), etc. [PyPI](http://pypi.python.org/pypi/pyautocad/), [examples](https://bitbucket.org/reclosedev/pyautocad/src/tip/examples) (Russian specific, sorry). But it has not documentation yet. – reclosedev Mar 23 '12 at 20:39
  • Shansal and reclosedev, this is very interesting. reclosedev, I see that you commented (answered) at http://stackoverflow.com/questions/9325162/win32com-connection-failure-to-autocad-via-python that comtypes is required. If you are successfully doing this, particularly if you are community-minded, I (and, I assume, Shansal) am very interested in talking further with you and beginning a library of AutoCAD Python code, and definitely going a little further with the solutions at my blog. I am a Python newbie. But I can help you guys with the nuances of AutoCAD. – Tom Haws Mar 24 '12 at 14:42