• ArcObject SDK for .net 调用arctoolbox工具(包括自定义)


    DATE: 2018-04-17

    开发环境:

    ArcGIS 10.5,Visual Studio 2015 64位

    在进行ArcObject(下文简称AO)开发前,应当先安装ArcGIS相关软件,并安装.net开发包,下文所述步骤,是在你已安装好环境,并有ArcGIS 许可的前题下,文中代码,才能运行。

    废话不说,直接聊过程、上图、上代码,_^^_

    开始新建工程:

    1、本文以WinForm表单工程为例,.net framework 要在4以上,见下图:

    2、添加许可(此步骤重要

    添加许可时,要根据当前机器所安装的许可,选择正确的许可类型。

    在第一步生成的工程名称上右键,在弹出的菜单上点击 Add ArcGIS Licence Checking……,在弹出的对话框中选择正确的许可类型(注:如果各位亲自定义的toolbox工具中用到了ArcGIS扩展功能,此时一定要把许可勾选,否则在运行和调试时,会出现赶也走赶不走的bug),见下图:

    该步骤完成后,VS会自动在我们新建的表单工程的Program.cs文件的main方法添加一些许可初始化代码,并生成一个LicenseInitializer.cs文件,在该文件中,进行了许可的绑定。

    3、调用 arcgis 自带的toolbox工具和自定义 toolbox 工具

    在完成了许可初始化后,我们就可以进行调用toolbox工具代码的编写了,在本示例中,我主要编写了两个方法,一个是对 arcgis 自带工具的调用和自定义工具的调用,不说废话,show you code:

    3.1、调用 arcgis 自带工具

    调用自带工具只展示一种方式,其中要注意的是,向 parameters 添加参数的顺序一定要与工具输入参数顺序一致,否则会调用不成功:

    public static void callSystemToolbox()
            {
                DirectoryInfo rootDir = Directory.GetParent(Environment.CurrentDirectory);
                string root = rootDir.Parent.Parent.FullName;//路径最后不带
    
                //using ESRI.ArcGIS.Geoprocessing
                //GeoGeoprocessing 是一个COM Interop程序集, COM Interop看上去像是介乎于COM和.Net之间的一条纽带,一座桥梁
                IGeoProcessor2 gp = new GeoProcessorClass();
                gp.OverwriteOutput = true;
    
                IVariantArray parameters = new VarArrayClass();
                parameters.Add(root + "\NetCallArcObject105Toolbox\resource\data\dawen.shp");
                parameters.Add(root + "\NetCallArcObject105Toolbox\resource\result\Copy4");
    
                object sev1 = null;
                try
                {
                    gp.Execute("CopyFeatures", parameters, null);
                    Console.WriteLine(gp.GetMessages(ref sev1));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(gp.GetMessages(ref sev1));
                }
            }   

     3.2、调用自定义toolbox工具

    下面代码展示了 2 种调用自定义 toolbox 工具的方式,写在一个方法内,各位亲在阅读时,要注意 2 种方式引用的命名空间不一样,show you code:

     private void callCustomerToolbox()
            {
                DirectoryInfo rootDir = Directory.GetParent(Environment.CurrentDirectory);
                string root = rootDir.Parent.Parent.FullName;
    
                //第 1 种调用方式:
                //using ESRI.ArcGIS.Geoprocessing
                //GeoGeoprocessing 是一个COM Interop程序集, COM Interop看上去像是介乎于COM和.Net之间的一条纽带,一座桥梁
                IGeoProcessor2 gpc = new GeoProcessorClass();
                gpc.OverwriteOutput = true;
                gpc.AddToolbox(root + "\NetCallArcObject105Toolbox\resource\customertoolbox\ZCustomer.tbx");
    IVariantArray parameters1
    = new VarArrayClass(); parameters1.Add(root + "\NetCallArcObject105Toolbox\resource\data\rain_2016.flt"); parameters1.Add("0 0.240362 1;0.240362 0.677384 2;0.677384 1.136257 3;1.136257 1.638832 4;1.638832 2.163258 5;2.163258 2.731386 6;2.731386 3.430621 7;3.430621 4.304665 8;4.304665 5.593879 9"); parameters1.Add(root + "\NetCallArcObject105Toolbox\resource\result\rain_2016.json"); object sev1 = null; IGeoProcessorResult result = new GeoProcessorResultClass(); try {
    //第一个参数是各位亲自定义的toolbox工具的名称 result
    = gpc.Execute("ProduceJsonFromFltWithNoProject", parameters1, null); Console.WriteLine(gpc.GetMessages(ref sev1)); } catch (Exception ex) { Console.WriteLine(gpc.GetMessages(ref sev1)); } //第 2 种调用方式: //using ESRI.ArcGIS.Geoprocessor //Geoprocessor 是一个托管程序集 Geoprocessor GP = new Geoprocessor(); GP.OverwriteOutput = true; GP.AddToolbox(root + "\NetCallArcObject105Toolbox\resource\customertoolbox\ZCustomer.tbx"); IVariantArray parameters2 = new VarArrayClass(); parameters2.Add(root + "\NetCallArcObject105Toolbox\resource\data\rain_2016.flt"); parameters2.Add(root + "\NetCallArcObject105Toolbox\resource\result\rain_2016.tif"); object sev2 = null; try { GP.Execute("ModelTest", parameters2, null); Console.WriteLine(GP.GetMessages(ref sev2)); } catch (Exception ex) { Console.WriteLine(GP.GetMessages(ref sev2)); } }

    代码打完,收工

    尾注:

     代码调用的自定义 toolbox 模型,各位新可以根据自已所熟悉的工具新建一个简单的模型,替换掉我代码中toolbox的路径,即可,切记切记!

     祝各位亲玩的愉快~

  • 相关阅读:
    user.table.column, table.column 或列说明无效
    spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务
    easyui-treegrid移除树节点出错
    jquery easyui easyui-treegrid 使用异步加载数据
    Java动态调用webService,axis2动态调用webService
    让IE支持Css3属性(圆角、阴影、渐变)
    float浮动之后高度自适应失效解决方案
    td中使用overflow:hidden; 无效解决方案
    jquery插件select2事件不起作用(select2-3.5.4)
    wsdl自动生成Java代码,根据wsdl生成Java代码
  • 原文地址:https://www.cnblogs.com/gregcn/p/8865875.html
Copyright © 2020-2023  润新知