How can I execute the custom Toolbox model that contains a script file?

710
1
04-02-2020 05:15 AM
tanerkoka
Occasional Contributor II

Hi,

 I want to execute  the Toolbox ("Toolbox.tbx") model ("ModelScript") that contains a script ("TableToExcel.py") file but gives exception. I want to copy table values to excel from model .I run correct from arcmap open ModelScript model and run , but programaticly not .I'm using ArcObject sdk for .net . How Can I solve this problem?

Here is the arcobjects code below:
IGeoProcessorResult result;
IGeoProcessor2 gp = new GeoProcessorClass();

gp.AddToolbox(@"C:\Users\.....\Desktop\Bolge\TEST.gdb\Toolbox.tbx");  //Toolbox

gp.OverwriteOutput = true;

IVariantArray parameters = new VarArrayClass();
parameters.Add(@"C:\Users\.....\Desktop\Bolge\TEST.gdb\table");  //Parameter

object sev = null;
try {

result = gp.Execute("ModelScript", parameters, null);   //Model   }

catch (Exception ex){MessageBox.Show(gp.GetMessages(ref sev)); }

Here is the "TableToExcel.py" code below:

# Name: TableToExcel_2.py

import arcpy

# Set environment settings
arcpy.env.workspace = "C:\Users\.....\Desktop\Bolge"

# Set local variables
in_table = "TEST.gdb/table"
out_xls = "addresses.xls"

# Execute TableToExcel
arcpy.TableToExcel_conversion(in_table, out_xls)

Here is prictures of model in ArcMap :

toolbox

model

Here is te error from ArcObject .net :

Error HRESULT E_FAIL has been returned from a call to a COM component.

System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.
at ESRI.ArcGIS.Geoprocessing.GeoProcessorClass.IGeoProcessor2_Execute(String Name, IVariantArray ipValues, ITrackCancel pTrackCancel)

Thanks

Tags (3)
0 Kudos
1 Reply
DuncanHornby
MVP Notable Contributor

Unless I am missing something important you don't need to connect to the toolbox and call the model that is running the script. As you have provided us with what the script is doing I see all that it is doing is calling the standard TableToExcel_conversion tool.

You can call geo-processing tools directly within ArcObjects. Below is some example code using another tool, but you can see how it is called and if you follow that pattern it will work.

Notice that the tool is called as Select_analysis, this is the select tool and analysis is the toolbox alias.

' Initialise Geoprocessor
Dim pGP As IGeoProcessor
pGP = New GeoProcessor
With pGP
 .AddOutputsToMap = False
 .LogHistory = False
 .OverwriteOutput = True
End With

' Load parameters
Dim pVariantArray As IVariantArray
pVariantArray = New VarArray
With pVariantArray
 .Add(m_pFeatureLayer.FeatureClass)
 .Add(outFC)
 .Add(sSQL)
End With

' Execute tool
Dim pGeoProcessorResult As IGeoProcessorResult
pGeoProcessorResult = pGP.Execute("Select_analysis", pVariantArray, Nothing)
If pGeoProcessorResult.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then
 ' Error! do something
Else
 ' ran without issue to something else
End If‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos