Select to view content in your preferred language

How to run a python script in ArcGIS Pro SDK?

06-14-2023 05:35 PM
New Contributor III

I am able to get the Geoprocessing tools using C# in ArcGIS Pro SDK to work, but it takes a longer time to figure out the syntax. I decided to write the script in python and use it in my tool. For some reason, it doesn't seem to work. I included my python script below. Thanks for your help! Here is C# code in the CreateModelSoils.class file: 



 public void GenModelSoils_py(FeatureLayer aggLayer, FeatureLayer allSoils, string AggField, string outGDB)
            string installPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string toolboxPath = Path.Combine(installPath, "");
            var args = Geoprocessing.MakeValueArray(aggLayer, allSoils, AggField, outGDB);
            Geoprocessing.ExecuteToolAsync(toolboxPath, args);




 I call this method in my ViewModel: 



CreateModelSoils ms = new CreateModelSoils();
ms.GenModelSoils_py(AggregateLayer, AllSoils, SelectedField, outGDB);



 Here is the python script I would like to run: 



#Import libraries
import arcpy
import os

Def GenIntersectSort(aggLayer,allSoils,aggField,outGDB):
	#Setting the environment
	arcpy.env.workspace = outGDB
	#Declare variables
	aggLayer = os.path.join(outGDB, "\\" + AggLayer_Name)
	allSoils = os.path.join(outGDB, "\\All_Soils")
	Intersect_Sort = os.path.join(outGDB, "\\Intersect_Sort")
    Model_Soils = os.path.join(outGDB, "\\Model_Soils")

	#Intersect the aggregate layer and All_Soils layers
	in_features = [aggLayer,allSoils]
	Intersect = arcpy.Intersect_analysis(in_features,"memory\\Intersect","ALL","","INPUT")

	#Sort the Intersect_Sort fields
	sort_fields = [[AggField,"ASCENDING"],["MUKEY",""ASCENDING"]]
	Intersect_Sort = arcpy.Sort_management(Intersect,Intersect_Sort,sort_fields)
    #Create the Model_Soils layer with ID
    Model_Soils = arcpy.FeatureClassToFeatureLClass_conversion(Intersect_Sort,outGDB,'Model_Soils')
    //Clean-Up the Model_Soils fields
    keep_fields = "Element_ID"
    Delete_fields = arcpy.DeleteFields_management(Model_Soils,keep_fields,"KEEP_FIELDS")
	return Model_Soils



2 Replies
Esri Regular Contributor


There is ArcGIS Pro sdk community sample  CallScriptFromNet . It could help you.

0 Kudos
by Esri Regular Contributor
Esri Regular Contributor

i will try this tomorrow, but it looks like line 22 has a syntax error.  Maybe you fixed that already.

0 Kudos