Run Python Script from VB.Net?

6669
6
06-06-2014 10:26 AM
DaleShearer
Occasional Contributor
Hi, thanks for talking the time to look at my question.

I am attempting to execute a Python script from and existing form in VB.net.

I just select a radiobutton on the form and on the checkedchanged event I am calling a shell command to execute the script.

Shell("C:\Python27\arcGIS10.2\python.exe ""C:\QualityCheck\Comparison.py""", AppWinStyle.MinimizedNoFocus) 

When this runs the window opens blank for a few seconds, a message is then displayed, but it closes before I can read it.

The script is not executed.

If you have any ideas, just point me in the right direction.

Thanks, Dale
Tags (2)
0 Kudos
6 Replies
GISDev1
Occasional Contributor III
Hi, thanks for talking the time to look at my question.

I am attempting to execute a Python script from and existing form in VB.net.

I just select a radiobutton on the form and on the checkedchanged event I am calling a shell command to execute the script.

Shell("C:\Python27\arcGIS10.2\python.exe ""C:\QualityCheck\Comparison.py""", AppWinStyle.MinimizedNoFocus) 

When this runs the window opens blank for a few seconds, a message is then displayed, but it closes before I can read it.

The script is not executed.

If you have any ideas, just point me in the right direction.

Thanks, Dale

This is how I've done it within an Add-In (in C#) in the past and it worked well.
using System.Diagnostics;
System.Diagnostics.Process.Start("C:\\temp\\temp.py");
0 Kudos
DaleShearer
Occasional Contributor
Thanks for the response, unfortunately I tried that with no success.  But I have made progress, I kept running it until I figured out what the message was in the window.  It said:  "Object:  Createobject cannot open map document".  I went in and changed the mxd from "Current" to the name of the mxd I am attempting to run it in "GeneralMap10.2"

mxd = arcpy.mapping.MapDocument("GeneralMap10.2")

Then I got an "Invalid mxd Filename"

In Arcmap I also went in and disabled Background Processing.

Geoprocessing - Geoprocessing Options - Background Processing.

Not sure where to go from here, I will keep digging on it.

Dale
0 Kudos
MichaelVolz
Esteemed Contributor
mxd = arcpy.mapping.MapDocument("GeneralMap10.2")


Might you need the full path to the map document (e.g. "C:\gis\GeneralMap10.2").
0 Kudos
DaleShearer
Occasional Contributor
Thanks for the reply, I am not sure how to get full path name.  On our ArcGIS it is all add-ins so we fire up ArcMap and it loads up whatever data is needed per user.

I can see if it was a standalone mxd how to get the location.

As we are starting with a blank document (mxd) I get the same results.

I am thinking that this is the correct path to the mxd, but it is not working.

mxd = arcpy.mapping.MapDocument("C:\Program Files (x86)\ArcGIS\Desktop10.2\bin\ArcMap.exe")?

I can run the code in the Python window and it works fine.

Dale,
0 Kudos
GISDev1
Occasional Contributor III
Thanks for the response, unfortunately I tried that with no success. 

So did the supplied code not run your Python Script from your Add-In? It sounds like your error is related to your Python script and not your Add-In code.

Please post your Python code that you are executing from your Add-In and we can help you debug your Python code that is giving an error.
0 Kudos
DaleShearer
Occasional Contributor
Thanks you so much for your help, below is the code I am attempting to run from my VB.Net add-in:

Private Sub radioAddressPinParcel_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radioAddressPinParcel.CheckedChanged
        Try
            If radioAddressPinParcel.Checked = True Then
                Shell("C:\Python27\arcGIS10.2\python.exe ""C:\GIS\QualityCheck\QAQCComparison.py""", AppWinStyle.MinimizedNoFocus)  ' arg1 arg2)  ', AppWinStyle.MinimizedFocus, True, -1)  ', Nothd:\path\to\your\script.py arg1 arg2")
            End If
        Catch ex As Exception
            GISErrorEMail(Err.Description, Err.Source, Err.Erl, Err.HelpContext, Err.Number, "QualityCheck.QualityCheck.radio_CheckedChanged")
        End Try
    End Sub

***********************************************************************************
Below is the code I am in my arcpy script.  Sorry I have not figured out how to post the code seperately.
***********************************************************************************

#QAQCComparison.py

import arcpy
import time
import csv
import os

try:
    arcpy.env.workspace = "C:\Users\Documents\ArcGIS\Default1.gdb"
    arcpy.env.overwriteOutput = True

    #Remove PinParcelComparison Layer from ArcMap
    mxd = arcpy.mapping.MapDocument("CURRENT")
    #mxd = arcpy.mapping.MapDocument("C:\Program Files (x86)\ArcGIS\Desktop10.2\bin\ArcMap.exe")
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    for i in arcpy.mapping.ListLayers(mxd , "PinParcelComparison"):
        arcpy.mapping.RemoveLayer(df , i)

    #Build Comparison Layer
    target_features = "Database Connections\DATA.sde\Buildings\Addresses"
    join_features = "Database Connections\PSRCELS.sde\LAND_REC\ParcelRegion"

    out_feature_class = "C:\Users\Documents\ArcGIS\Default1.gdb\PinParcelBuild"

    arcpy.SpatialJoin_analysis(target_features, join_features, out_feature_class)

    #Remove PinParcelBuild Layer from ArcMap
    mxd = arcpy.mapping.MapDocument("CURRENT")
    df = arcpy.mapping.ListDataFrames(mxd)[0]

    for i in arcpy.mapping.ListLayers(mxd , "PinParcelBuild"):
        arcpy.mapping.RemoveLayer(df , i)
    #Create No match Pin - No match Parcel layer.
    in_feature_class =  "C:\Users\Documents\ArcGIS\Default1.gdb\PinParcelBuild"
    output_feature_class = "C:\Users\Documents\ArcGIS\Default1.gdb\PinParcelComparison"

    where_clause = '"PIN" <> "CO_PIN"'

    arcpy.Select_analysis(in_feature_class, output_feature_class, where_clause)
    #arcpy.RefreshActiveView()

    #Create .csv & .txt files (Excel & Notepad)
    #---------------------------------------------------------------------
    # a = append, appending to the end of the file if it exists
    # w = write, truncating the file first
    # r = open for reading (default)
    # x = open for exclusive creation, failing if the file already exists
    # b = binary mode
    # t = text mode (default)
    # + = open a disk file for updating (reading and writing)
    # U = universal newlines mode (deprecated)
    #---------------------------------------------------------------------
    #Create the search cursor
    cursor = arcpy.SearchCursor(output_feature_class)
    print "complete"
    # Iterate through the rows in the cursor
    #In the output feature classes a new ObjectID is created.
    #The Target_FID field contains the OBJECTID of the record from the target_features.
    field = "OBJECTID"
    field1 = "TARGET_FID"
    field2 = "PIN"
    field3 = "STATUS"
    count = 0
    # == Conditional Evaluation
    # = Assignment of variable
    for row in cursor:
        OBJECTID = str(row.getValue(field))
        TARGET_FID = str(row.getValue(field1))
        PIN = str(row.getValue(field2))
        STATUS = str(row.getValue(field3))
    print "Close"

except arcpy.ExecuteError:
    #Geoprocessing error.
    print "A geoprocessing error has occured"
    print arcpy.GetMessages(2)
except Exception as e:
    #Python error.
    print "An error has occured"
    print e
0 Kudos