[Ironpython-users] abstract class conversion?

Michael Colonno mcolonno at stanford.edu
Fri Dec 2 22:08:48 CET 2011


            Hi Jeff ~

 

            Thanks for your help - it sounds like I have to do a lot more
digging in the library itself to see what's going on. The VB.NET code
(though paraphrased) does work; perhaps there is something internal to the
library taking care of the translation to the subclass. No secret on the
library; I'm working with the SolidWorks API. I felt it would be easier to
understand and more useful to others in the future if I just made the names
generic. In terms of a "missing middle" it's difficult to tell since the API
is usually used via add-ins wizards for Visual Studio that likely take care
of some things under the hood. The instructions for the simplest form of
VB.NET add-in (a stand-alone executable that talks to the application) from
the official documentation are as follows: 

 

Standalone Applications (.exe files)

To create an instance of the SolidWorks software, your project should
contain lines of code similar to the following: 

 

Sub Main

    Dim swApp As SldWorks.SldWorks          

    swApp = New SldWorks.SldWorks()

      code...

    swApp.ExitApp

    swApp = Nothing

End Sub

 

Additionally, you must have added references to the SolidWorks type
libraries.

 

            The type libraries mentioned are added as COM references through
Visual Studio and then imported, e.g.: 

 

Imports System

Imports SldWorks

Imports SWPublished

Imports SwConst

Imports System.Runtime.InteropServices 

 

      Finally the documentation for the function I was using as an example
is: 

 

Dim instance As ISldWorks

Dim Name As String 

Dim Silent As Boolean 

Dim Errors As Integer 

Dim value As Object 

 

value = instance.ActivateDoc2(Name, Silent, Errors)

 

            After this it's quite easy to build up VB.NET programs that talk
to the application. I will try and rebuild a concrete example line by line
to see if I can't identify the abstract from the non-abstract classes. 

 

            Thanks,

            ~Mike C. 

 

-----Original Message-----
From: Jeff Hardy [mailto:jdhardy at gmail.com] 
Sent: Friday, December 02, 2011 12:08 PM
To: Michael Colonno
Cc: ironpython-users at python.org
Subject: Re: [Ironpython-users] abstract class conversion?

 

Well, as you guessed, 'Type' and 'function' are both abstract (and
'function' is not static). You can't instantiate an abstract class directly
(i.e., a = Type()). You need to either instantiate a subclass, or the
library you're using may provide a factory function that does that
internally and returns a Type reference.

 

I can't see how the VB code you gave could work, so if there's something
missing that piece is probably important.

 

I take it this library is internal so that you can't share what it is?

 

- Jeff

 

On Thu, Dec 1, 2011 at 5:28 PM, Michael Colonno <
<mailto:mcolonno at stanford.edu> mcolonno at stanford.edu> wrote:

>             Thanks for the reply, Jeff.

> 

> 

> 

> ILSpy reports for "Type" (the "IL" version):

> 

> 

> 

>             .class interface public auto ansi abstract import 

> [relevant dll name]

> 

>       {

> 

>       .custom instance void

> [mscorlib]System.Runtime.InteropServices.TypeLibTypeAttribute::.ctor(i

> nt16)

> = (

> 

>             01 00 50 10 00 00

> 

>       )

> 

>       .custom instance void

> [mscorlib]System.Runtime.InteropServices.GuidAttribute::.ctor(string) 

> = (

> 

>             01 00 ... (lots of numbers)

> 

> 

> 

>             And for "function":

> 

> 

> 

>             .method public hidebysig newslot abstract virtual

> 

>       instance object marshal(idispatch) function (

> 

>             [in] string marshal(bstr) Name,

> 

>             [in] bool Option,

> 

>             [in] [out] int32& Errors

> 

>       ) runtime managed internalcall

> 

>       {

> 

>       .custom instance void

> [mscorlib]System.Runtime.InteropServices.DispIdAttribute::.ctor(int32) 

> = (

> 

>             01 00 5b 00 00 00 00 00

> 

>       )

> 

> 

> 

>             I confess this does not mean a whole lot to me, though 

> both Type and function are declared as abstract. Hopefully this sheds 

> light on the behavior in IronPython.

> 

> 

> 

>             Thanks,

> 

>             ~Mike C.

> 

> 

> 

> -----Original Message-----

> From: Jeff Hardy  <mailto:[mailto:jdhardy at gmail.com]>
[mailto:jdhardy at gmail.com]

> Sent: Thursday, December 01, 2011 10:36 AM

> To: Michael Colonno

> Cc:  <mailto:ironpython-users at python.org> ironpython-users at python.org

> Subject: Re: [Ironpython-users] abstract class conversion?

> 

> 

> 

> On Thu, Dec 1, 2011 at 10:14 AM, Michael Colonno 

> < <mailto:mcolonno at stanford.edu> mcolonno at stanford.edu>

> wrote:

> 

>>             In VB.NET, this works just fine:

> 

>> 

> 

>> 

> 

>> 

> 

>> Dim A As Type

> 

>> Dim Name As String

> 

>> Dim Option As Boolean

> 

>> Dim Err As Integer

> 

>> Dim value As Object

> 

>> 

> 

>> value = A.function(Name, Option, Err)

> 

>> 

> 

>>             In IronPython I am trying (pseudo-code):

> 

>> 

> 

>> import clr

> 

>> clr.AddReferenceToFileAndPath("relevant dll") from

> 

>> API.Interop.whatever import Type

> 

>> 

> 

>> A = Type()

> 

>> value = A.function("Name", True, err)

> 

>> 

> 

>>             Two issues / questions:

> 

>> 

> 

>> 1.     The IronPython code above produces: TypeError: Cannot create

> 

>> instances of Type because it is abstract. Why is an abstract Python

> 

>> class being created and do I have any control over this?

> 

> 

> 

> Calling `A = Type()` tries to create an instance of Type, which your 

> VB code doesn't do. Is `Type.function` a static method?

> 

> 

> 

>> 

> 

>> 2.     Changing the code to access the functions of Type directly (e.g.

> 

>> Type.function()) results in a "self" argument being expressed as the

> 

>> first argument in the function. i.e. the syntax expected is now

> 

>> Type.function(self, string, boolean, int). I'm familiar with "self"

> 

>> only in the context of defining class functions and not outside of a

> 

>> class definition.

> 

> 

> 

> It really depends on how what the signature of the function is. If 

> it's static, calling `Type.function` from Python shouldn't need a self
argument.

> Can you find the signature of `Type.function` (ILSpy is your friend here).

> Feel free to elide the names; they're not really important.

> 

> 

> 

> - Jeff

> 

> 

> _______________________________________________

> Ironpython-users mailing list

>  <mailto:Ironpython-users at python.org> Ironpython-users at python.org

>  <http://mail.python.org/mailman/listinfo/ironpython-users>
http://mail.python.org/mailman/listinfo/ironpython-users

> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20111202/7aa5567b/attachment.html>


More information about the Ironpython-users mailing list