[Ironpython-users] Calling a method on the Interop COM object

Djordje Spasic issworld2000 at yahoo.com
Sat Jan 30 10:35:07 EST 2016


Thank you Stéphane!!

That solved the problem!
Do you mind if I ask another question?Right now I am trying to call another method: FixUP.The problem with it, is that it requires a single "out" argument.

Here is how I tried to call it:
MapWinGIS.ShapeClass.FixUp(shape)  # raises an error below

But it raises this error message:
Message: expected ShapeClass, got __ComObject
The same happens if I call it like so:
shape.FixUp()
The __ComObject in this case is shape variable. Any object I get, has this kind of type. 
Is there a way to somehow cast the shape to MapWinGIS.ShapeClass type?I tried this:
shapeCast = clr.Reference[MapWinGIS.ShapeClass](shape)  # raises the same upper error on this line

But the same upper message appears ("expected ShapeClass, got __ComObject")

Any advice on how to supply the upper "out" argument to "FixUp" method?Or maybe how to cast the shape to MapWinGIS.ShapeClass type?
Thank you.

Djordje Spasic 

      From: Stéphane Lozier <stephane.lozier at gmail.com>
 To: Djordje Spasic <issworld2000 at yahoo.com> 
Cc: "ironpython-users at python.org" <ironpython-users at python.org>
 Sent: Friday, January 29, 2016 9:02 PM
 Subject: Re: [Ironpython-users] Cannot create instances of that class because it is abstract
   
I can't say I really know what's going on, but you could try using unbound class instance methods (see http://ironpython.net/documentation/dotnet/dotnet.html#using-unbound-class-instance-methods).

For example:
MapWinGIS.Shapefile.Open(sf, shpfilename, None)

On Fri, Jan 29, 2016 at 11:55 AM, Djordje Spasic <issworld2000 at yahoo.com> wrote:

Thank you for the explanation Stéphane.
When I use MapWinGIS.ShapefileClass() instead of MapWinGIS.Shapefile():
    import clr
   
    clr.AddReferenceToFileAndPath("C:/mapwindow_dlls/Interop.MapWinGIS.dll")

    import MapWinGIS
   
    sf = MapWinGIS.ShapefileClass()
    shpfilename = "C:/example.shp"
    sf.Open(shpfilename, None)  # raises Error

It raises an error:
Could not convert argument 0 for call to Open.
​I googled a couple of times, and found one place where the same issue has emerged: here on stackoverflow.​
​Stackoverflow issue is actually not related with ironpython, but with ironruby. The author states that the solution is to supply the CLR string as an argument to "Open" function, instead of regular ruby string. I tried that replicating that in ironpython:

shpfilename = clr.Reference[System.String]("c:/example.shp")
But again the upper error message, did not go away.
Further googling found that this kind of issues may happen when trying to call methods in .NET from "Interop COM objects". But only if that method has "ref parameters". Which Open method does not. Here is the signature of the Open method:

Help on method-descriptor Open Open(...)
    Open(self: ShapefileClass, ShapefileName: str, cBack: Callback) -> bool 

// MapWinGIS.ShapefileClass
[DispId(11)]
[MethodImpl(MethodImplOptions.InternalCall)]
public virtual extern bool Open([MarshalAs(UnmanagedType.BStr)] [In] string ShapefileName, [MarshalAs(UnmanagedType.Interface)] [In] ICallback cBack = null);

I would be very grateful for any kind of advice Stéphane.

Kind regards,
Djordje Spasic


      From: Stéphane Lozier <stephane.lozier at gmail.com>
 To: Djordje Spasic <issworld2000 at yahoo.com> 
Cc: "ironpython-users at python.org" <ironpython-users at python.org>
 Sent: Friday, January 29, 2016 5:24 PM
 Subject: Re: [Ironpython-users] Cannot create instances of that class because it is abstract
  
Shapefile is an interface with a CoClassAttribute to ShapefileClass (which means when you do new Shapefile() in C# it creates an instance of ShapefileClass). I have no idea what the correct behaviour for IronPython should be, but in this case MapWinGIS.Shapefile gives you the interface.

You could try MapWinGIS.ShapefileClass() instead of MapWinGIS.Shapefile().



On Mon, Jan 25, 2016 at 11:03 AM, Djordje Spasic via Ironpython-users <ironpython-users at python.org> wrote:

Hello,

I am trying to call a specific method ("Open") from the .NET assembly.The .NET assembly along with all other .dlls and necessary files, can be downloaded from here. It's free an open source project: MapWindow.
In their examples page, by loading the Interop.MapWinGIS.dll file, they call the MapWinGIS.Shapefile.Open() method like so:

using MapWinGIS;
string shpfilename = "C:/example.shp";
Shapefile sf = new Shapefile();
if (sf.Open(shpfilename, null))
{
}


However, when I try the same thing in ironpython:
    import clr
    import os
    
    shpfilename = "C:/example.shp"
    dllsfilename = "C:/mapwindow_dlls"
    
    clr.AddReferenceToFileAndPath(os.path.join(dllsfilename, "Interop.MapWinGIS.dll"))
    print "Interop.MapWinGIS.dll loaded: ", "Interop.MapWinGIS" in [assembly.GetName().Name for assembly in clr.References]  # prints: True
    import MapWinGIS
    
    sf = MapWinGIS.Shapefile()  # raises Error
    sf.Open(shpfilename, None)


I am getting an error message:
   "Message: Cannot create instances of Shapefile because it is abstract"
Why is this happening?
I contacted the author of the project, but couldn't solve the issue.
Any kind of advice would be helpful.
Kind regards,
Djordje Spasic


_______________________________________________
Ironpython-users mailing list
Ironpython-users at python.org
https://mail.python.org/mailman/listinfo/ironpython-users





   



  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20160130/11a2a657/attachment.html>


More information about the Ironpython-users mailing list