using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geoprocessor;
using KGIS.Framework.AE.GPHelper;
//using KGIS.Framework.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace GPHelper
{
public class CustomGPHelper
{
public static void DLTBBGDissolve_Tool(IVariantArray pParam)
{
object sev = null;
//1-定义GeoProcessor对象
IGeoProcessor gp = new GeoProcessorClass();
try
{
//2-设置参数
gp.OverwriteOutput = true;
string strPath = AppDomain.CurrentDomain.BaseDirectory;
if (!strPath.EndsWith(@"\"))
{
strPath += @"\";
}
string toolPath = System.IO.Path.Combine(strPath, @"Resource\ZLDatabaseTool" + ".tbx");
if (!System.IO.File.Exists(toolPath))
{
throw new Exception("未找到预变更工具!");
}
//3-设置工具箱所在的路径
gp.AddToolbox(toolPath);
//5-执行工具
IGeoProcessorResult result = gp.Execute("DLTBBGDissolve", pParam, null);
if (result != null && result.Status != esriJobStatus.esriJobSucceeded)
{
string ms = "";
if (gp.MessageCount > 0)
{
for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
{
ms += "$" + gp.GetMessage(Count) + "\n\n";
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (gp.MessageCount > 0)
gp.GetMessages(ref sev).ToString();
}
}
public static void SetGDPDJB_Tool(IVariantArray pParam)
{
object sev = null;
//1-定义GeoProcessor对象
IGeoProcessor gp = new GeoProcessorClass();
try
{
//2-设置参数
gp.OverwriteOutput = true;
string strPath = AppDomain.CurrentDomain.BaseDirectory;
if (!strPath.EndsWith(@"\"))
{
strPath += @"\";
}
string toolPath = System.IO.Path.Combine(strPath, @"Resource\ZLDatabaseTool" + ".tbx");
if (!System.IO.File.Exists(toolPath))
{
throw new Exception("未找到预变更工具!");
}
//3-设置工具箱所在的路径
gp.AddToolbox(toolPath);
//5-执行工具
IGeoProcessorResult result = gp.Execute("SetGDPDJBTool", pParam, null);
if (result != null && result.Status != esriJobStatus.esriJobSucceeded)
{
string ms = "";
if (gp.MessageCount > 0)
{
for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
{
ms += "$" + gp.GetMessage(Count) + "\n\n";
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (gp.MessageCount > 0)
gp.GetMessages(ref sev).ToString();
}
}
public static void DLTBBG_Tool(IVariantArray pParam)
{
object sev = null;
//1-定义GeoProcessor对象
IGeoProcessor gp = new GeoProcessorClass();
try
{
//2-设置参数
gp.OverwriteOutput = true;
string strPath = AppDomain.CurrentDomain.BaseDirectory;
if (!strPath.EndsWith(@"\"))
{
strPath += @"\";
}
string toolPath = System.IO.Path.Combine(strPath, @"Resource\ZLDatabaseTool" + ".tbx");
if (!System.IO.File.Exists(toolPath))
{
throw new Exception("未找到预变更工具!");
}
//3-设置工具箱所在的路径
gp.AddToolbox(toolPath);
//5-执行工具
IGeoProcessorResult result = gp.Execute("DLTBBGTool", pParam, null);
if (result != null && result.Status != esriJobStatus.esriJobSucceeded)
{
string ms = "";
if (gp.MessageCount > 0)
{
for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
{
ms += "$" + gp.GetMessage(Count) + "\n\n";
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (gp.MessageCount > 0)
gp.GetMessages(ref sev).ToString();
}
}
public static void RepairGeo(GPParamClass gPParam)
{
Geoprocessor gp = new Geoprocessor();
try
{
RepairGeometry repairgeo = new RepairGeometry();
repairgeo.in_features = gPParam.FirstFeatureLayer.FeatureClass;
//repairgeo.out_feature_class = outFC;
repairgeo.delete_null = "TRUE";
IGeoProcessorResult tGeoResult = (IGeoProcessorResult)gp.Execute(repairgeo, null);
if (tGeoResult == null || tGeoResult.Status != esriJobStatus.esriJobSucceeded)
{
//LogAPI.Debug("检查图形失败:");
throw new Exception(ReturnMessages(gp));
}
string msg = ReturnMessages(gp);
}
catch (Exception ex)
{
throw new Exception(ReturnMessages(gp));
}
}
public static void RepairGeo(IFeatureClass pFc)
{
Geoprocessor gp = new Geoprocessor();
try
{
RepairGeometry repairgeo = new RepairGeometry();
repairgeo.in_features = pFc;
//repairgeo.out_feature_class = outFC;
repairgeo.delete_null = "TRUE";
IGeoProcessorResult tGeoResult = (IGeoProcessorResult)gp.Execute(repairgeo, null);
if (tGeoResult == null || tGeoResult.Status != esriJobStatus.esriJobSucceeded)
{
//LogAPI.Debug("检查图形失败:");
throw new Exception(ReturnMessages(gp));
}
string msg = ReturnMessages(gp);
}
catch (Exception ex)
{
throw new Exception(ReturnMessages(gp));
}
}
public static void DLTBGXGC_Tool(IVariantArray pParam)
{
object sev = null;
//1-定义GeoProcessor对象
IGeoProcessor gp = new GeoProcessorClass();
try
{
//2-设置参数
gp.OverwriteOutput = true;
string strPath = AppDomain.CurrentDomain.BaseDirectory;
if (!strPath.EndsWith(@"\"))
{
strPath += @"\";
}
string toolPath = System.IO.Path.Combine(strPath, @"Resource\ZLDatabaseTool" + ".tbx");
if (!System.IO.File.Exists(toolPath))
{
throw new Exception("未找到预变更工具!");
}
//3-设置工具箱所在的路径
gp.AddToolbox(toolPath);
//5-执行工具
IGeoProcessorResult result = gp.Execute("DLTBGXGCTool", pParam, null);
if (result != null && result.Status != esriJobStatus.esriJobSucceeded)
{
string ms = "";
if (gp.MessageCount > 0)
{
for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
{
ms += "$" + gp.GetMessage(Count) + "\n\n";
}
}
}
gp.RemoveToolbox(toolPath);
Marshal.ReleaseComObject(gp);
}
catch (Exception ex)
{
throw ex;
}
finally
{
//if (gp.MessageCount > 0)
// gp.GetMessages(ref sev).ToString();
}
}
public static void TableToTable(IFeatureLayer pLayer, string pOutLocal, string pOutName)
{
try
{
//for (int i = 0; i < listFeatureClass.Count(); i++)
//{
ESRI.ArcGIS.Geoprocessor.Geoprocessor geoprocessor = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
geoprocessor.OverwriteOutput = true;
ESRI.ArcGIS.ConversionTools.TableToTable TbToTb = new ESRI.ArcGIS.ConversionTools.TableToTable();
TbToTb.in_rows = pLayer.FeatureClass;
TbToTb.out_path = pOutLocal;
TbToTb.out_name = pOutName;
IGeoProcessorResult tGeoResult = (IGeoProcessorResult)geoprocessor.Execute(TbToTb, null);
if (tGeoResult == null || tGeoResult.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)
{
string msg = ReturnMessages(geoprocessor);
throw new Exception(msg);
}
//}
}
catch (Exception ex)
{
//LogAPI.Debug("将一个或多个属性表导出excel 时失败,异常原因: " + ex + " ; ");
throw ex;
}
}
///
/// 获取GP任务执行信息
///
///
///
private static string ReturnMessages(Geoprocessor gp)
{
string ms = "";
if (gp.MessageCount > 0)
{
for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
{
ms += "$" + gp.GetMessage(Count) + "\n\n";
}
}
return ms;
}
}
}