I am newish with python (ActivePython v2.6), and I am trying to automate AutoCAD 2010 (Acad) using module win32com (using acad's COM/ActiveX interfaces). Most of the interface is pretty straight forward, but I am getting hung up on functions that need to use VARIANTS to pass numerical arrays. Im not sure if my problem is on the python side or my misuse of Acads ActiveX.

For several functions python handles variant conversion automagically, ex, for VARIANT strings I just use python strings and the function works as expected. It is with 2D/3D coordinate sets that I seem to be getting type exceptions.

A simple example would be (acad).ActiveDocument.ModelSpace.AddLine(point1, point2), with point1 and point2 as 3D VARIANTs of type "acDouble".
Currently I cannot get this method to execute without an error or exception (Normally argument mismatch or invalid type). I have tried passing values as python lists,arrays, and even numerical strings, with none working.

So then, Acad has another method that helps you create VARIANT arrays, and its interface is defined as, CreateTypedArray(VarArr, Type, Value1, [value2, value3, ...valueN]), with 'VarArr' as the output variant array, 'Type' as "acDouble", and 'ValueN' as the numerical data to be converted.
I CAN get this function to return quietly, but the actual return value is "None" and the supposed 'VarArr' output array is always the same value I initialized it with. It seems that this is the magic function I am missing, but I cannot get useful return values from it.

On one more note, there is a GetPoint method that returns a 3D variant array after coordinate selection within Acad. I can get this to work properly, and it will return the correct coordinate values from Acad, but when trying to assign the return values and then reusing in the AddLine method, I am still getting the same ol errors, as if the return value is NOT a variant.

I almost feel as if I am misusing python's types, ecspecially dynamic types created from win32com utilities.

Any help is much appreciated.
Thanks.

Research update:

Seems that python and AutoCAD are not the most popular mix, and of the few references I could find, this was a common problem.

One post from 2005-ish that using "strict" arrays fixed his problem.

import array
3dpoint = array.array('d', [1.0, 1.0, 0.0])

Although, this did not change anything for me. I really think it has to do with the array structure I am trying to pass win32com. Reasoning is that my VARIANT strings are handled properly, and when I call a method that returns a numeric VARIANT, the value is correctly converted to tuple.

Thanks again.

Ultimately, problem solved, but my solution was rather dirty.

It had to do with makepy's interface generation. On the problematic methods, Autocad reported that it was expecting an argument of generic type VARIANT ARRAY, but it would not accept this type when passed.

Solution was to override makepy's generated output to force these types to be DOUBLE ARRAY.

I do not know of a way to perform this automatically with makepy. I just went through the interface module and performed the edits by hand on the methods I knew I needed to use (there are over 1000 instances if you wanted to correct them all, and then, Im not sure if they all even should be corrected)

Hope this helps someone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.