Re: [Tutor] How to create array of variants?

2008-09-12 Thread Alan Gauld


"Krasyn" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

# -*- coding:UTF-8 -*-
from comtypes.client import *
from array import array


Any reason why you are using array here?
Why not just a Python list?


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


If a imple list works here will it not work for the two numbers?

Just a thought,

Alan G 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-09-12 Thread Krasyn


Alan Gauld wrote:
> 
> 
> "Krasyn" <[EMAIL PROTECTED]> 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@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 . ) (0 . "CIRCLE") (330 . ) (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.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-09-11 Thread Alan Gauld


"Krasyn" <[EMAIL PROTECTED]> 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@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-09-11 Thread Krasyn


Kelie-2 wrote:
> 
> Hello group,
> 
> I'm trying to translate the following VB code into Python and not sure how
> to
> create an array of variants. 
> 
> Thanks for your help!
> 
> 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
>
> lineObj.SetXdata DataType, Data
> End Sub
> 
> 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)
> 
> if __name__ == "__main__":
> SetXData()
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
dataType = array("h",[1001,1070])
dataValue = VARIANT(['Test_Application', 600])
-- 
View this message in context: 
http://www.nabble.com/How-to-create-array-of-variants--tp18331322p19441514.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-07-09 Thread Kelie
Monika Jisswel  googlemail.com> writes:

> 
> 
> Comment : I never did any VB so I am not sure if I understand you.supposing
your data comes like this :python code : 
> 
> 
> Data = ( ('A', 1), ('B', 2), ('C', 3), ('D', 4) )#you can create a list of the
items like this : List_Letters = [ x[0] for x in Data]List_Numbers = [ x[1] for
x in Data]
> 
> hope this helps.
> 
> 

Monika, Thanks for your reply. I've tried using the list data type, but it does
not work in this case.



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-07-08 Thread John Fouhy
On 09/07/2008, Kelie <[EMAIL PROTECTED]> wrote:
> I think comtypes or pywin32 do take care of some conversion between Python
>  types and VB types. But they don't work with AutoCAD.

Hi Kelie,

This is a short excerpt from _Python Programming on Win32_:

"""In many cases, PythonCOM can translate between Python objects and
VARIANT structures seamlessly.  When you call a COM object and pass a
Python object, PythonCOM automatically creates a VARIANT of the right
type and passes the VARIANT to COM.
[...]
Python object type: Any other python sequence.
VARIANT type: An array of VARIANTs; each element of the sequence is
translated using this table.
[...]
In some cases, these translations aren't suitable; for example, a COM
object may be picky about the VARIANT types passed and accept only a
VT_I2 integer, not a VT_I4 integer.  This should be considered a bug
in the COM object, but it does happen.  In this case, you must use
earlybound COM by using MakePy. [...] If you can't use MakePy for your
COM object, you must get your hands dirty and use the
PyIDispatch.InvokeTypes() method manually; this is how MakePy gets the
behaviour it does.  The use of InvokeTypes() is beyond the scope of
this book."""

I think your question is beyond the scope of this mailing list :-)  I
think there's a mailing list dedicated to python on Windows; you might
do better there.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-07-08 Thread Kelie
Alan Gauld  btinternet.com> writes:
> 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)
> 

Thanks Alan. This is the error I got:

Traceback (most recent call last):
  File "C:\Python25\codes\autocad\setxdata2.py", line 13, in 
SetXData()
  File "C:\Python25\codes\autocad\setxdata2.py", line 10, in SetXData
line.SetXData(dataType, dataValue)
_ctypes.COMError: (-2145320939, None, (u'Invalid argument type in SetXData
method', u'AutoCAD.Application', u'C:\\Program Files\\AutoCAD
2008\\HELP\\OLE_ERR.CHM', -2145320939, None))

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

I think comtypes or pywin32 do take care of some conversion between Python 
types and VB types. But they don't work with AutoCAD.

Btw, this is the working code to draw a point in AutoCAD and you can't replace
pt = array.array('d', [0,0,0]) with pt = [0,0,0]. The latter gives an error:
COMError: (-2147024809, 'The parameter is incorrect.', (None, None, None, 0, 
None))

import array
import comtypes.client
acadApp = comtypes.client.GetActiveObject("AutoCAD.Application")
ms = acadApp.ActiveDocument.ModelSpace
pt = array.array('d', [0,0,0])
ms.AddPoint(pt)
print "Done."

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-07-08 Thread Alan Gauld


"Kelie" <[EMAIL PROTECTED]> 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



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-07-08 Thread Kelie
Andre Engels  gmail.com> writes:
> 
> So what does the code of line.SetXData(dataType, dataValue) look like?
> >From that code you should be able to discern what argument type is
> wanted.
> 

Thanks Andre. I don't know how the correct code should look like in Python. In
VB, I've posted the code in my original question. Since I'm using comtypes, I
looked up the generated module and this is what I can find about this SetXData
method:

COMMETHOD([dispid(1027), helpstring(u'Sets the extended data (XData) associated
with an object')], HRESULT, 'SetXData',
  ( ['in'], VARIANT, 'XDataType' ),
  ( ['in'], VARIANT, 'XDataValue' )),



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-07-08 Thread Monika Jisswel
Comment : I never did any VB so I am not sure if I understand you.

supposing your data comes like this :

python code :

Data = ( ('A', 1), ('B', 2), ('C', 3), ('D', 4) )
> #you can create a list of the items like this :
>
> List_Letters = [ x[0] for x in Data]
> List_Numbers = [ x[1] for x in Data]
>

hope this helps.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-07-08 Thread Andre Engels
On Tue, Jul 8, 2008 at 11:00 AM, Kelie <[EMAIL PROTECTED]> wrote:
> John,
>
> Thanks for your reply. I'm aware of list, tuple, sets, etc. and have tried 
> them,
> which results in an error: Invalid argument type in SetXData method. My
> understanding is that I do need an array here. Just don't know the correct way
> of doing it.

So what does the code of line.SetXData(dataType, dataValue) look like?
>From that code you should be able to discern what argument type is
wanted.


-- 
Andre Engels, [EMAIL PROTECTED]
ICQ: 6260644 -- Skype: a_engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-07-08 Thread Kelie
John,

Thanks for your reply. I'm aware of list, tuple, sets, etc. and have tried them,
which results in an error: Invalid argument type in SetXData method. My
understanding is that I do need an array here. Just don't know the correct way
of doing it.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-07-07 Thread John Fouhy
On 08/07/2008, Kelie <[EMAIL PROTECTED]> wrote:
>  I'm trying to translate the following VB code into Python and not sure how to
>  create an array of variants.

I'm not sure what an array of variants in VB is -- perhaps an array
that can contain objects of any type?

>  Python code
>  import array

You may not need to use the array module -- the array module is
specifically for arrays of numerics in situations where performance
matters.  In general in Python, you get array-like behaviour using
lists.  Lists can hold objects of any type.

For example:

>>> myList = [100, 'Test application']
>>> myList[0]
100
>>> myList[1]
'Test application'

I recommend reading a python tutorial if this is new to you.

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor