win32com and Passing SafeArrays of Doubles

Jeffrey N. Shelton shelton at custommachine.com
Sat Feb 16 07:30:27 CET 2002


Success, at last!

Orginally, the makepy-generated code which didn't work was:

return self._oleobj_.InvokeTypes(0x10153, 0x0, 1, (11, 0),
                                              ((12, 1), (3, 1)),
doubleInfoIn, typeWanted)
                                               ^^^
where doubleInfoIn was the array that was being mangled.

Using a little brute force, I determined that the call will work correctly
with either:

return self._oleobj_.InvokeTypes(0x10153, 0x0, 1, (11, 0),
                                              ((8197, 1), (3, 1)),
doubleInfoIn, typeWanted)
                                                ^^^
or

return self._oleobj_.InvokeTypes(0x10153, 0x0, 1, (11, 0),
                                              ((12293, 1), (3, 1)),
doubleInfoIn, typeWanted)
                                                 ^^^

This indicates that a type flag of either VT_ARRAY | VT_R8  or
VT_ARRAY | VT_VECTOR | VT_R8 is required.

(I can also get things to work with type values of 8198, 8199, 12294, and
12295, although it's beyond me why they should work...)

With this correction, I can pass in the array as a tuple, a list, or a tuple
within a
tuple, and it all works fine.

The type library information for this particular call is:

[id(0x00010153), helpstring("Select enities by ray fire")]
VARIANT_BOOL SelectByRay(
[in] VARIANT doubleInfoIn,
[in] long typeWanted);

and the SolidWorks C++ code for handling this function is:

BOOL IModelDoc2::SelectByRay(const VARIANT& doubleInfoIn, long typeWanted)
{
 BOOL result;
 static BYTE parms[] =
  VTS_VARIANT VTS_I4;
 InvokeHelper(0x10153, DISPATCH_METHOD, VT_BOOL, (void*)&result, parms,
  &doubleInfoIn, typeWanted);
 return result;
}

Since the type library doesn't indicate anything other than a VARIANT for
input, I don't know how one is supposed to figure this out in advance.
However, if I can provide more information that would help allow makepy to
better handle these types of arrays, please let me know.

And thank you, Mark, for your generous time and assistance!

Jeff

----- Original Message -----
From: "Mark Hammond" <mhammond at skippinet.com.au>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Friday, February 15, 2002 8:33 PM
Subject: Re: win32com and Passing SafeArrays of Doubles


> I believe it will simply be a matter of getting the correct "type tuple"
> in the generated code.  I am afraid I don't know VB well enough to know
> what the extra set of parens used by VB is.  It could be some special
> "byref" annotation, or maybe even specifying another level of
> indirection (ie, an array of arrays of doubles).
>
> I suggest you try and determine exactly what that VB syntax does - you
> may then need to craft a type typle that indicates a 2 dimensional
> array, and actually pass ((1,2,3),) in your Python code.
>
> Good luck, and feel free to mail if you need more help - getting this
> sorted would be good - we may even identify that pythoncom is being less
> then optimal <wink> in these edge cases.
>
> Mark.
>
> Jeffrey N. Shelton wrote:
> > In the process of using win32com to hook Python into SolidWorks (a 3D
> > modeler), I've run into the same problem as others before me -- namely,
some
> > strange things happen when passing SafeArrays of doubles into
SolidWorks.
> > Having created the appropriate gen_py file, I'm usually successful when
I
> > pass in a tuple with the appropriate values. But when passing SafeArrays
of
> > doubles into SolidWorks, it thinks I've inserted an extra value of 0.0
> > between each parameter I've passed. So it thinks that an array (1, 1, 1)
is
> > instead (0, 1, 0, 1, 0, 1).
> >
> > Past discussion at:
> >
http://groups.google.com/groups?hl=en&selm=39943819.65368645%40news.onlineis
> > p.com
> >
> > Mark Hammond's reply (back in August of 2000) indicated that he thought
the
> > problem was with the SolidWorks code, and I'm quite willing to accept
that.
> > However, I've noted from the SolidWorks API documentation (quoted below)
> > that, even when using VB, some special massaging is required.
> >
> > So, do I have to do any special 'wrapping' to get SolidWorks to accept
my
> > arrays, or does this still look like its just a shortcoming in the
> > SolidWorks implementation?
> >
> > Any suggestions would be appreciated!
> >
> > Jeff
> >
> >>From SolidWorks API Documentation:
> >
> >>Passing array data to SolidWorks in Visual Basic requires the use of a
> >>Variant variable to hold the data. The procedure is to put the data
> >>into an array then to assign a Variant to the array. The variant is then
> >>passed to an API function with parentheses round it:
> >>
> >>     Dim varArray As Variant
> >>     Dim DataArray(9) As double
> >>
> >>     ' Assign values
> >>     DataArray(0) = 0.1
> >>     ...
> >>
> >>     ' Pass the data to SolidWorks
> >>     varArray = DataArray
> >>     Result = Object.Method( (varArray) )  ' Note the extra parentheses
> >>
> > are required
> >
> >
> >
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>





More information about the Python-list mailing list