[Tutor] How to create array of variants?
Krasyn
mndrk at mail.ru
Fri Sep 12 09:03:42 CEST 2008
Alan Gauld wrote:
>
>
> "Krasyn" <mndrk at mail.ru> wrote
>
>>>
>>> I'm trying to translate the following VB code into Python and not
>>> sure how
>>> to create an array of variants.
>
> All variables in Python are effectively variants - variables that can
> store any type. So an array of variants equates to a Python list
>
>>> VB Code:
>>> Sub SetXdata()
>
> def SetXData():
>
>>> Dim lineObj As AcadLine
>>> Set lineObj = ThisDrawing.ModelSpace.Item(0)
>
>
> lineObj = ThisDrawing.ModelSpace.Item(0)
>
> Where ThisDrawing.ModelSpace.Item(0) is s0ome kind of
> data structure you have defined elsewhere. Or fetch using COM.
>
>>> Dim DataType(0 To 1) As Integer
>>> Dim Data(0 To 1) As Variant
>
> DataType = []
> Data = []
>
> But it looks like you are trying to fake a dictionary - although VB
> has dictionaries!
>
>>> DataType(0) = 1001: Data(0) = "Test_Application"
>>> DataType(1) = 1070: Data(1) = 600
>
> Data = {'Test_application' : 1001, 600 : 1070}
>
>>> lineObj.SetXdata DataType, Data
>
> lineObj.SetXdata( Data )
>
>>> Python code
>>> import array
>>> 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?
>>>
>>> line.SetXData(dataType, dataValue)
>
> Here's the snag with the dictionary approach so its back to
> two lists. I wouldn't use the array module just lists.
>
> It might be enough to just pass DataType and Data as two lists
> into the COM object. I don't know enough about Python's COM
> integration to be sure that it will sort it all out though. But I
> suspect
> it will.
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
# -*- coding:UTF-8 -*-
from comtypes.client import *
from array import array
acad = GetActiveObject("AutoCAD.Application")
dwg = acad.ActiveDocument
mspace = dwg.ModelSpace
circle = mspace.AddCircle(array("d",[0,0,0]),100)
circle.Color = 3
acad.ZoomExtents()
dtype = array("h",[1001,1070]) # [1001,1070] doesnt' work (comtypes 0.5.1)
dvalue = ['Test_Application', 600] #OK VARIANT is not needed
circle.SetXData(dtype,dvalue)
#test: (entget (car (entsel)) '("*"))
#result:((-1 . <Entity name: 7ef98150>) (0 . "CIRCLE") (330 . <Entity
name: 7ef01cf8>) (5 . "12A") (100 . "AcDbEntity") (67 . 0) (410 . "Model")
(8 .
"0") (62 . 3) (100 . "AcDbCircle") (10 0.0 0.0 0.0) (40 . 100.0) (210 0.0
0.0
1.0) (-3 ("Test_Application" (1070 . 600))))
--
View this message in context: http://www.nabble.com/How-to-create-array-of-variants--tp18331322p19450606.html
Sent from the Python - tutor mailing list archive at Nabble.com.
More information about the Tutor
mailing list