Friday, 24 May 2019

Avoiding fails from ArcObjects geoprocessing with .NET?



There are some nice features in ArcToolbox we can use, but for some reason, this is NOT working properly. It doesn't even throw me an error.


My software is running inside ArcMap, so no need to AoInitialize again, corret?


    public void Execute()
{
InitializeProduct();
try
{
Geoprocessor gp = new Geoprocessor();
gp.OverwriteOutput = true;


FeatureToPoint featureToPoint = new FeatureToPoint();

string outputPathName = CurrentWorkspace.PathName + "\\teste_centroide";

featureToPoint.in_features = InputFeatureClass;
featureToPoint.out_feature_class = outputPathName;
featureToPoint.point_location = "INSIDE";

IGeoProcessorResult result = (IGeoProcessorResult)gp.Execute(featureToPoint, null);


if (result == null)
{
for (int i = 0; i <= gp.MessageCount - 1; i++)
{
Console.WriteLine(gp.GetMessage(i));
}
}

IGPUtilities gpUtils = new GPUtilitiesClass();
this.OutputFeatureClass = gpUtils.OpenFeatureClassFromString(outputPathName);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\r\n");
}

This is a code example I'm having here. I generated the DataManagement tools assembly, but I could not find the file to sign it.


This code just gives me an error. is it because of the signing?


I've tried the other way too, using IVariantArray and calling from the tool name, without sucess. Is it just me or...?


Can anyone point me a "nicer" solution? I need to run several process that are already built in ArcToolbox that I really don't want to duplicate.




Answer



In the code below, the multi2single function works for me in 10.0. I couldn't test Feature2Point since I don't have an ArcInfo license, can you?.


public class Test
{
public static void TestGP(IApplication app)
{
IMxDocument mxDoc = (IMxDocument)app.Document;
//Feat2Point((IFeatureLayer)mxDoc.FocusMap.get_Layer(0), @"D:\Projects\AmberGIS\Forums\forumtest.gdb\f2p");
Multi2Single((IFeatureLayer)mxDoc.FocusMap.get_Layer(0), @"D:\Projects\AmberGIS\Forums\forumtest.gdb\m2s");
}


public static void Multi2Single(IFeatureLayer inLayer, string outPath)
{
MultipartToSinglepart m2s = new MultipartToSinglepart();
m2s.in_features = inLayer.FeatureClass;
m2s.out_feature_class = outPath;
Execute(m2s);
}

public static void Feat2Point(IFeatureLayer inLayer, string outPath)

{
FeatureToPoint f2p = new FeatureToPoint();
f2p.in_features = inLayer.FeatureClass;
f2p.out_feature_class = outPath;
Execute(f2p);
}

public static void Execute(IGPProcess proc)
{
Geoprocessor gp = new Geoprocessor();

gp.AddOutputsToMap = true;
gp.OverwriteOutput = true;
gp.RegisterGeoProcessorEvents((IGeoProcessorEvents)new GPEvents());
IGeoProcessorResult2 result = gp.Execute(proc, null) as IGeoProcessorResult2;
IGPMessages msgs = result.GetResultMessages();
for(int i=0;i Debug.Print("{0} {1}", msgs.GetMessage(i).Description, msgs.GetMessage(i).Type);
}
}
public class GPEvents : IGeoProcessorEvents3, IGeoProcessorEvents

{
#region IGeoProcessorEvents3 Members
public void OnProcessMessages(IGeoProcessorResult result, IGPMessages pMsgs)
{
Debug.Print("OnProcessMessages {0}", result.Status);
}
public void OnProgressMessage(IGeoProcessorResult result, string message)
{
Debug.Print("OnProgressMessages {0}", result.Status);
}

public void OnProgressPercentage(IGeoProcessorResult result, double percentage)
{
Debug.Print("OnProgressPercentage {0}", result.Status);
}
public void OnProgressShow(IGeoProcessorResult result, bool Show)
{
Debug.Print("OnProgressShow {0}", result.Status);
}
public void PostToolExecute(IGeoProcessorResult result)
{

Debug.Print("PostToolExecute {0}", result.Status);
}
public void PreToolExecute(IGeoProcessorResult result)
{
Debug.Print("PreToolExecute {0}",result.Status);
}
#endregion

#region IGeoProcessorEvents Members


void IGeoProcessorEvents.OnMessageAdded(IGPMessage message)
{
Debug.Print("OnMessageAdded {0} {1}", message.Description, message.Type);
throw new NotImplementedException();
}

void IGeoProcessorEvents.PostToolExecute(IGPTool Tool, ESRI.ArcGIS.esriSystem.IArray Values, int result, IGPMessages Messages)
{
Debug.Print("PostToolExecute2 {0}", Tool.Name);
}


void IGeoProcessorEvents.PreToolExecute(IGPTool Tool, ESRI.ArcGIS.esriSystem.IArray Values, int processID)
{
if (Tool.IsLicensed())
Debug.Print("PreToolExecute");
else
Debug.Print("tool is not licensed to run");
}

void IGeoProcessorEvents.ToolboxChange()

{
Debug.Print("ToolboxChange");
}

#endregion
}

I get this output in VS:


PreToolExecute
PostToolExecute2 MultipartToSinglepart

Executing: MultipartToSinglepart GPL0 D:\Projects\AmberGIS\Forums\forumtest.gdb\m2s esriGPMessageTypeProcessDefinition
Start Time: Thu Sep 02 11:32:44 2010 esriGPMessageTypeProcessStart
Succeeded at Thu Sep 02 11:32:51 2010 (Elapsed Time: 7.00 seconds) esriGPMessageTypeProcessStop

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...