Python with IESRIScriptEngine

2145
4
02-02-2016 08:54 AM
RolandMartin
New Contributor III

Hi,

Has anyone figured out how to correctly use Python with the IESRIScriptEngine in .NET? The documentation isn't particularly detailed. Through guesswork, I've come up with this:

Dim pointer(1) As IntPtr

pointer(0) = Runtime.InteropServices.Marshal.AllocHGlobal(4)

Dim engine As IESRIScriptEngine = New ESRIScriptEngine

With engine

    .Language = "python"

    .AddCode("import arcpy")

    .AddCode("mxd = arcpy.mapping.MapDocument(""CURRENT"")")

    .AddCode("mxd.dataDrivenPages.currentPageID = " & id)

    .Run("", pointer)

End With

This code runs if you encapsulate it in a Try/Catch block, but the pointer isn't working, which means it throws an error every time it runs. Not a major problem in this instance, but it means I can't get any variables passed back from the script.

Any ideas?

Thanks!

Roland.

Tags (3)
0 Kudos
4 Replies
AlexanderNohe1
Occasional Contributor III

Hi Roland,

You are not returning anything from your script.  I believe that you may want to place a return statement in there to see some sort of output.

Hope this helps!

DuncanHornby
MVP Notable Contributor

This is an interesting question as I have never used that Interface. You are right there is no help in the API about this, in fact a quick Google returned this question as the top ranking page about IESRIScriptEngine! So I'm guessing no one in ESRI or the entire planet is using this interface other than you Roland! You are clearly ahead of the curve!

So I tried to have a play and I could not even create the object, it would return a Com Exception error on the second line

        Dim pESRIScriptEngine As IESRIScriptEngine

        pESRIScriptEngine = New ESRIScriptEngine

I've not attempted to delve too deep into this but this page suggests one possible cause is that it is referencing other DLL's. As there is no information, no discussion (other than this page), no advice, no sample code, nothing about this Interface I wonder if it is sensible to continue with it?

This page​ talks about running arcpy from within a .Net application, may be that is the way to go?

RolandMartin
New Contributor III

Hi both,

Sorry for not responding sooner - I've only just worked out how to reply (the reply button doesn't work!)

I actually opened this up as a support ticket with Esri, and they have filed a couple of bugs/enhancements relating to it. As you say, it looks as though this interface isn't actually supported for Python.

Alexander - thanks, well spotted! I was assuming that any output that's normally written to the screen by Python would be automatically passed back, but I can see that doesn't entirely make sense...

Duncan - what I'm trying to do is dependent on having information pass from Python to VB.net within the same ArcMap session (for example, so I can tell which Data Driven Page is currently in view). I think I'm right in saying that all the methodologies described at your first link launch a new ArcMap session? Or am I missing something obvious?

Thanks for your help!

Roland.

0 Kudos
DuncanHornby
MVP Notable Contributor

Yes it is not obvious if you can run the code from within an application and reference the opened MXD. The only thing I can say is give it a go. In arcpy you reference the opened MXD with the word CURRENT. That may work? More information can be found here​.

0 Kudos