Using Python to automate AutoCAD

Discussion in 'AutoCAD' started by no, Jul 31, 2005.

  1. no

    no Guest

    Hi people,

    Hope there are some Python users around in this group ;)

    I'm learning Python and in the process am trying to automate AutoCAD
    with Python via COM.


    with the following code I can successfully start an AutoCAD session,
    display the program on screen and start a new drawing:
    from win32com.client import *

    acad = Dispatch( 'AutoCAD.Application')
    acad.Visible = 1

    DWG = acad.Documents.Add('acadiso.dwt')
    MS = DWG.ModelSpace
    <<<

    and the following statements let me pick a point in the drawing window
    and return the coordinates as a tuple:
    UTIL = DWG.Utility
    punt1 = UTIL.GetPoint()
    <<<

    the value of the variable punt1 is e.g. (-5.3, 6.5, 0.0)


    But sadly, that's about as far i can get things to work. For example:
    when I try to draw a point in the drawing using another tuple as an
    argument:
    punt2 = MS.AddPoint((10.0, 10.0, 0.0))
    <<<

    I get the following error message:
    Traceback (most recent call last):
    File "<input>", line 1, in ?
    File "<COMObject <unknown>>", line 2, in AddPoint
    com_error: (-2147352567, 'Er is een uitzondering opgetreden.', (0,
    None, None, None, 0, -2147024809), None)
    <<<


    reading the AutoCAD Developer Documentation, I discovered the
    CreateTypedArray method which creates a variant that contains an
    array, the method doesn't return the value, so I presume it changes
    the argument VarArr in-place:

    CreateTypedArray(VarArr, Type, Value1, [value2, value3, ...valueN])
    VarArr = Variant; output-only; The array of values as a
    variant.

    Type = Visual Basic 6 Constant; input-only; The type of values
    you are supplying. vbBoolean, vbInteger, vbLong,
    vbSingle, or vbDouble.

    Value1 [Value2, ...ValueN.] = Of the type specified in the
    Type parameter above; input-only; The value(s) to be
    included in the variant.

    My thinking here is, that the AddPoint method expects a Variant
    argument and not a tuple. But when I use this code to assign a Variant
    array to variable punt3:
    UTIL = DWG.Utility
    punt3=()
    UTIL.CreateTypedArray(punt3, pythoncom.VT_R8, 10.0, 10.0, 0.0)
    <<<

    I don't receive any error message, but variable punt3 still has his
    old value of (), so nothing happened at all.

    I was hoping to use this variable in the AddPoint method as follows:

    punt2 = MS.AddPoint(punt3)


    So, is there anybody who can help me with this?

    Kind regards,

    Gert
     
    no, Jul 31, 2005
    #1
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.