[Tutor] How to create array of variants?

Alan Gauld alan.gauld at btinternet.com
Tue Jul 8 22:20:53 CEST 2008


"Kelie" <kf9150 at gmail.com> wrote

> I'm trying to translate the following VB code into Python and not 
> sure how to
> create an array of variants.

An array of variants in Python is a list.
But I'm not sure why you think you need one?

> VB Code:
> Sub SetXdata()
>    Dim lineObj As AcadLine
>    Set lineObj = ThisDrawing.ModelSpace.Item(0)
>
>    Dim DataType(0 To 1) As Integer
>    Dim Data(0 To 1) As Variant
>
>    DataType(0) = 1001: Data(0) = "Test_Application"
>    DataType(1) = 1070: Data(1) = 600

So this is basically setting up two lists where
the DataType is an integer and the Data is  - in
these cases - a string or an integer.

>    lineObj.SetXdata DataType, Data

So if you pass in two Python lists containing:

DataType = [1001,1070]
Data = ["Test_Application", 600]

Does it work?

If not what error do you get? (The full text please)

> import comtypes.client
>
> def SetXData():
>    activedoc =
> comtypes.client.GetActiveObject("AutoCAD.Application").ActiveDocument
>    line = activedoc.ModelSpace.Item(0)
>
>    dataType = array.array('i', [1001, 1070])
>    dataValue = array.array('?', ['Test_Application', 600]) #What 
> should I use
> for the type code?

I don;t think you use an array here. An array in this context expects
a homogenous set of data. You need a mixed set. So a normal list
is most likely construct to use.

Alternatively does comtypes have a mechanism for converting Python
types to VB/COM Variants?

Alan G





More information about the Tutor mailing list