arcengine自己做一个工具Tool放到工具箱中
// Copyright 2010 ESRI
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
//
// See the use restrictions at <your ArcGIS install location>/DeveloperKit10.0/userestrictions.txt.
// using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.ADF.CATIDs; namespace GPCalculateArea
{ public class CalculateAreaFunction : IGPFunction2
{
private string m_ToolName = "CalculateArea"; //Function Name
//Local members private string m_metadatafile = "CalculateArea_area.xml";
private IArray m_Parameters; // Array of Parameters
private IGPUtilities m_GPUtilities; // GPUtilities object public CalculateAreaFunction()
{
m_GPUtilities = new GPUtilitiesClass();
} #region IGPFunction Members // Set the name of the function tool.
// This name appears when executing the tool at the command line or in scripting.
// This name should be unique to each toolbox and must not contain spaces.
public string Name
{
get { return m_ToolName; }
} // Set the function tool Display Name as seen in ArcToolbox.
public string DisplayName
{
get { return "Calculate Area"; }
} // This is the location where the parameters to the Function Tool are defined.
// This property returns an IArray of parameter objects (IGPParameter).
// These objects define the characteristics of the input and output parameters.
public IArray ParameterInfo
{
get
{
//Array to the hold the parameters
IArray parameters = new ArrayClass(); //Input DataType is GPFeatureLayerType
IGPParameterEdit3 inputParameter = new GPParameterClass();
inputParameter.DataType = new GPFeatureLayerTypeClass(); // Default Value object is GPFeatureLayer
inputParameter.Value = new GPFeatureLayerClass(); // Set Input Parameter properties
inputParameter.Direction = esriGPParameterDirection.esriGPParameterDirectionInput;
inputParameter.DisplayName = "Input Features";
inputParameter.Name = "input_features";
inputParameter.ParameterType = esriGPParameterType.esriGPParameterTypeRequired;
parameters.Add(inputParameter); // Area field parameter
inputParameter = new GPParameterClass();
inputParameter.DataType = new GPStringTypeClass(); // Value object is GPString
IGPString gpStringValue = new GPStringClass();
gpStringValue.Value = "Area";
inputParameter.Value = (IGPValue)gpStringValue; // Set field name parameter properties
inputParameter.Direction = esriGPParameterDirection.esriGPParameterDirectionInput;
inputParameter.DisplayName = "Area Field Name";
inputParameter.Name = "field_name";
inputParameter.ParameterType = esriGPParameterType.esriGPParameterTypeRequired;
parameters.Add(inputParameter); // Output parameter (Derived) and data type is DEFeatureClass
IGPParameterEdit3 outputParameter = new GPParameterClass();
outputParameter.DataType = new GPFeatureLayerTypeClass(); // Value object is DEFeatureClass
outputParameter.Value = new DEFeatureClassClass(); // Set output parameter properties
outputParameter.Direction = esriGPParameterDirection.esriGPParameterDirectionOutput;
outputParameter.DisplayName = "Output FeatureClass";
outputParameter.Name = "out_featureclass";
outputParameter.ParameterType = esriGPParameterType.esriGPParameterTypeDerived; // Create a new schema object - schema means the structure or design of the feature class (field information, geometry information, extent)
IGPFeatureSchema outputSchema = new GPFeatureSchemaClass();
IGPSchema schema = (IGPSchema)outputSchema; // Clone the schema from the dependency.
//This means update the output with the same schema as the input feature class (the dependency).
schema.CloneDependency = true; // Set the schema on the output because this tool will add an additional field.
outputParameter.Schema = outputSchema as IGPSchema;
outputParameter.AddDependency("input_features");
parameters.Add(outputParameter); return parameters;
}
} // Validate:
// - Validate is an IGPFunction method, and we need to implement it in case there
// is legacy code that queries for the IGPFunction interface instead of the IGPFunction2
// interface.
// - This Validate code is boilerplate - copy and insert into any IGPFunction2 code..
// - This is the calling sequence that the gp framework now uses when it QI's for IGPFunction2..
public IGPMessages Validate(IArray paramvalues, bool updateValues, IGPEnvironmentManager envMgr)
{
if (m_Parameters == null)
m_Parameters = ParameterInfo; // Call UpdateParameters().
// Only Call if updatevalues is true.
if (updateValues == true)
{
UpdateParameters(paramvalues, envMgr);
} // Call InternalValidate (Basic Validation). Are all the required parameters supplied?
// Are the Values to the parameters the correct data type?
IGPMessages validateMsgs = m_GPUtilities.InternalValidate(m_Parameters, paramvalues, updateValues, true, envMgr); // Call UpdateMessages();
UpdateMessages(paramvalues, envMgr, validateMsgs); // Return the messages
return validateMsgs;
} // This method will update the output parameter value with the additional area field.
public void UpdateParameters(IArray paramvalues, IGPEnvironmentManager pEnvMgr)
{
m_Parameters = paramvalues; // Retrieve the input parameter value
IGPValue parameterValue = m_GPUtilities.UnpackGPValue(m_Parameters.get_Element(0)); // Get the derived output feature class schema and empty the additional fields. This will ensure you don't get duplicate entries.
IGPParameter3 derivedFeatures = (IGPParameter3)paramvalues.get_Element(2);
IGPFeatureSchema schema = (IGPFeatureSchema)derivedFeatures.Schema;
schema.AdditionalFields = null; // If we have an input value, create a new field based on the field name the user entered.
if (parameterValue.IsEmpty() == false)
{
IGPParameter3 fieldNameParameter = (IGPParameter3)paramvalues.get_Element(1);
string fieldName = fieldNameParameter.Value.GetAsText(); // Check if the user's input field already exists
IField areaField = m_GPUtilities.FindField(parameterValue, fieldName);
if (areaField == null)
{
IFieldsEdit fieldsEdit = new FieldsClass();
IFieldEdit fieldEdit = new FieldClass();
fieldEdit.Name_2 = fieldName;
fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
fieldsEdit.AddField(fieldEdit); // Add an additional field for the area values to the derived output.
IFields fields = fieldsEdit as IFields;
schema.AdditionalFields = fields;
} }
} // Called after returning from the update parameters routine.
// You can examine the messages created from internal validation and change them if desired.
public void UpdateMessages(IArray paramvalues, IGPEnvironmentManager pEnvMgr, IGPMessages Messages)
{
// Check for error messages
IGPMessage msg = (IGPMessage)Messages;
if (msg.IsError())
return; // Get the first Input Parameter
IGPParameter parameter = (IGPParameter)paramvalues.get_Element(0); // UnPackGPValue. This ensures you get the value either form the dataelement or GpVariable (ModelBuilder)
IGPValue parameterValue = m_GPUtilities.UnpackGPValue(parameter); // Open the Input Dataset - Use DecodeFeatureLayer as the input might be a layer file or a feature layer from ArcMap.
IFeatureClass inputFeatureClass;
IQueryFilter qf;
m_GPUtilities.DecodeFeatureLayer(parameterValue, out inputFeatureClass, out qf); IGPParameter3 fieldParameter = (IGPParameter3)paramvalues.get_Element(1);
string fieldName = fieldParameter.Value.GetAsText(); // Check if the field already exists and provide a warning.
int indexA = inputFeatureClass.FindField(fieldName);
if (indexA > 0)
{
Messages.ReplaceWarning(1, "Field already exists. It will be overwritten.");
} return;
} // Execute: Execute the function given the array of the parameters
public void Execute(IArray paramvalues, ITrackCancel trackcancel, IGPEnvironmentManager envMgr, IGPMessages message)
{ // Get the first Input Parameter
IGPParameter parameter = (IGPParameter)paramvalues.get_Element(0); // UnPackGPValue. This ensures you get the value either form the dataelement or GpVariable (ModelBuilder)
IGPValue parameterValue = m_GPUtilities.UnpackGPValue(parameter); // Open Input Dataset
IFeatureClass inputFeatureClass;
IQueryFilter qf;
m_GPUtilities.DecodeFeatureLayer(parameterValue, out inputFeatureClass, out qf); if (inputFeatureClass == null)
{
message.AddError(2, "Could not open input dataset.");
return;
} // Add the field if it does not exist.
int indexA; parameter = (IGPParameter)paramvalues.get_Element(1);
string field = parameter.Value.GetAsText(); indexA = inputFeatureClass.FindField(field);
if (indexA < 0)
{
IFieldEdit fieldEdit = new FieldClass();
fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
fieldEdit.Name_2 = field;
inputFeatureClass.AddField(fieldEdit);
} int featcount = inputFeatureClass.FeatureCount(null); //Set the properties of the Step Progressor
IStepProgressor pStepPro = (IStepProgressor)trackcancel;
pStepPro.MinRange = 0;
pStepPro.MaxRange = featcount;
pStepPro.StepValue = (1);
pStepPro.Message = "Calculating Area";
pStepPro.Position = 0;
pStepPro.Show(); // Create an Update Cursor
indexA = inputFeatureClass.FindField(field);
IFeatureCursor updateCursor = inputFeatureClass.Update(qf, false);
IFeature updateFeature = updateCursor.NextFeature();
IGeometry geometry;
IArea area;
double dArea; while (updateFeature != null)
{
geometry = updateFeature.Shape;
area = (IArea)geometry;
dArea = area.Area;
updateFeature.set_Value(indexA, dArea);
updateCursor.UpdateFeature(updateFeature);
updateFeature.Store();
updateFeature = updateCursor.NextFeature();
pStepPro.Step();
} pStepPro.Hide(); // Release the update cursor to remove the lock on the input data.
System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor);
} // This is the function name object for the Geoprocessing Function Tool.
// This name object is created and returned by the Function Factory.
// The Function Factory must first be created before implementing this property.
public IName FullName
{
get
{
// Add CalculateArea.FullName getter implementation
IGPFunctionFactory functionFactory = new CalculateAreaFunctionFactory();
return (IName)functionFactory.GetFunctionName(m_ToolName);
}
} // This is used to set a custom renderer for the output of the Function Tool.
public object GetRenderer(IGPParameter pParam)
{
return null;
} // This is the unique context identifier in a [MAP] file (.h).
// ESRI Knowledge Base article #27680 provides more information about creating a [MAP] file.
public int HelpContext
{
get { return 0; }
} // This is the path to a .chm file which is used to describe and explain the function and its operation.
public string HelpFile
{
get { return ""; }
} // This is used to return whether the function tool is licensed to execute.
public bool IsLicensed()
{
IAoInitialize aoi = new AoInitializeClass();
ILicenseInformation licInfo = (ILicenseInformation)aoi; string licName = licInfo.GetLicenseProductName(aoi.InitializedProduct()); if (licName == "ArcInfo")
{
return true;
}
else
return false;
} // This is the name of the (.xml) file containing the default metadata for this function tool.
// The metadata file is used to supply the parameter descriptions in the help panel in the dialog.
// If no (.chm) file is provided, the help is based on the metadata file.
// ESRI Knowledge Base article #27000 provides more information about creating a metadata file.
public string MetadataFile
{
get { return m_metadatafile; }
} // By default, the Toolbox will create a dialog based upon the parameters returned
// by the ParameterInfo property.
public UID DialogCLSID
{
// DO NOT USE. INTERNAL USE ONLY.
get { return null; }
} #endregion
} //////////////////////////////
// Function Factory Class
////////////////////////////
[
Guid("2554BFC7-94F9-4d28-B3FE-14D17599B35A"),
ComVisible(true)
]
public class CalculateAreaFunctionFactory : IGPFunctionFactory
{
private const string m_ToolName = "ylArea"; //Function Name
// Register the Function Factory with the ESRI Geoprocessor Function Factory Component Category.
#region "Component Category Registration"
[ComRegisterFunction()]
static void Reg(string regKey)
{ GPFunctionFactories.Register(regKey);
} [ComUnregisterFunction()]
static void Unreg(string regKey)
{
GPFunctionFactories.Unregister(regKey);
}
#endregion // Utility Function added to create the function names.
private IGPFunctionName CreateGPFunctionNames(long index)
{
IGPFunctionName functionName = new GPFunctionNameClass();
functionName.MinimumProduct = esriProductCode.esriProductCodeProfessional;
IGPName name; switch (index)
{
case (0):
name = (IGPName)functionName;
name.Category = "AreaCalculation";
name.Description = "Calculate Area for FeatureClass";
name.DisplayName = "Calculate Area";
name.Name = m_ToolName;
name.Factory = (IGPFunctionFactory)this;
break;
} return functionName;
} // Implementation of the Function Factory
#region IGPFunctionFactory Members // This is the name of the function factory.
// This is used when generating the Toolbox containing the function tools of the factory.
public string Name
{
get { return m_ToolName; }
} // This is the alias name of the factory.
public string Alias
{
get { return "area"; }
} // This is the class id of the factory.
public UID CLSID
{
get
{
UID id = new UIDClass();
id.Value = this.GetType().GUID.ToString("B");
return id;
}
} // This method will create and return a function object based upon the input name.
public IGPFunction GetFunction(string Name)
{
switch (Name)
{
case (m_ToolName):
IGPFunction gpFunction = new CalculateAreaFunction();
return gpFunction;
} return null;
} // This method will create and return a function name object based upon the input name.
public IGPName GetFunctionName(string Name)
{
IGPName gpName = new GPFunctionNameClass(); switch (Name)
{
case (m_ToolName):
return (IGPName)CreateGPFunctionNames(0); }
return null;
} // This method will create and return an enumeration of function names that the factory supports.
public IEnumGPName GetFunctionNames()
{
IArray nameArray = new EnumGPNameClass();
nameArray.Add(CreateGPFunctionNames(0));
return (IEnumGPName)nameArray;
} // This method will create and return an enumeration of GPEnvironment objects.
// If tools published by this function factory required new environment settings,
//then you would define the additional environment settings here.
// This would be similar to how parameters are defined.
public IEnumGPEnvironment GetFunctionEnvironments()
{
return null;
} #endregion
} }
arcengine自己做一个工具Tool放到工具箱中的更多相关文章
- java中使用反射做一个工具类,来为指定类中的成员变量进行赋值操作,使用与多个类对象的成员变量的赋值。
//------------------------------------------------我是代码的分割线 // 首选是一个工具类,在该工具类里面,定义了一个方法,public void s ...
- 为什么工具类App,都要做一个社区?
非著名程序员涩郎 非著名程序员,字耿左直右,号涩郎,爱搞机,爱编程,是爬行在移动互联网中的一名码匠!个人微信号:loonggg,微博:涩郎,专注于移动互联网的开发和研究,本号致力于分享IT技术和程序猿 ...
- 在VisualStudio 工具箱中隐藏用户控件
当我们创建一个用户控件后,VisualStudio会自动将其添加到工具箱中,本来这是一个比较贴心的设计.但是,有的时候,我们并不想将用户控件放到工具箱中. 例如:在WPF中,为了避免一个页面的控件过多 ...
- 用Socket做一个局域网聊天工具(转)
原文:http://www.cnblogs.com/technology/archive/2010/08/15/1799858.html 程序设计成为简单的服务端和客户端之间的通信, 但通过一些方法可 ...
- 想做一个整合开源安全代码扫描工具的代码安全分析平台 - Android方向调研
想做一个整合开源安全代码扫描工具的代码安全分析平台 - Android方向调研 http://blog.csdn.net/testing_is_believing/article/details/22 ...
- 【移动端debug-6】如何做一个App里的web调试小工具
原文链接:如何做一个App里的web调试小工具 我们知道现在hybrid app非常流行,在这样的app里,h5页面是应用非常广泛的.相对于以往在pc端开发的网页,放在app里的网页由于无法直接使用桌 ...
- 尝试做一个.NET简单、高效、避免OOM的Excel工具
Github : https://github.com/shps951023/MiniExcel 简介 我尝试做一个.NET简单.高效.避免OOM的Excel工具 目前主流框架大多将资料全载入到记忆体 ...
- 做一个能对标阿里云的前端APM工具(上)
APM 全称是 Application Performance Monitor,即性能监控 这篇文章有三个前提: 从产品形态上看这肯定不是一个能够媲美阿里产品的竞品,所以抱歉我碰瓷了.你可以把这里的阿 ...
- 做一个能对标阿里云的前端APM工具(下)
上篇请访问这里做一个能对标阿里云的前端APM工具(上) 样本多样性问题 上一小节中的实施方案是微观的,即单次性的.具体的.但是从宏观上看,我需要保证性能测试是公允的,符合大众预期的.为了达到这种效果, ...
随机推荐
- WPF 仿IPhone滑块开关 样式 - CheckBox
原文:WPF 仿IPhone滑块开关 样式 - CheckBox <Style x:Key="CheckRadioFocusVisual"> <Setter Pr ...
- 洛谷 P1486 [NOI2004]郁闷的出纳员
题目描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资 ...
- js中自己实现bind函数的方式
最近由于工作比较忙,好久都没时间静下心来研究一些东西了.今天在研究 call 和 apply 的区别的时候,看到 github 上面的一篇文章,看完以后,感觉启发很大. 文章链接为 https://g ...
- 使用Swagger生成简单接口文档
使用swagger通过简单的配置可以生成简单的接口文档: 依赖包: // Swagger2 compile 'io.springfox:springfox-swagger2:2.8.0' compil ...
- Luogu P1108 低价购买 DP
第一问求最长下降子序列,不提: 第二问:借鉴了最短路的方法??? 我们求出来了每个位置的最长下降子序列的长度,那么刻意这样这样转移 if f[i]==f[j]+1&&a[i]<a ...
- Oracle KEEP的用法
[摘录自] http://blog.itpub.net/12932950/viewspace-687036/ http://flyfx.iteye.com/blog/1994993 聚合函数MIN, ...
- 2.在centos7虚拟机搭建nginx网站
1.nginx配置目录 cd /etc/nginx/conf.d/ 添加 vi www.18cat.conf server{ listen 80; server_name www.18cat.com; ...
- (转)求有向图的强连通分量个数(kosaraju算法)
有向图的连通分量的求解思路 kosaraju算法 逛了很多博客,感觉都很难懂,终于找到一篇能看懂的,摘要记录一下 原博客https://www.cnblogs.com/nullzx/p/6437926 ...
- python 函数基础知识整理
一.函数的定义: 定义:def 关键词开头,空格之后接函数名称和圆括号(),最后还有一个":". def 是固定的,不能变,必须是连续的def三个字母,不能分开... 空格 为了将 ...
- PIE SDK元素的选择和取消选择
1功能简介 在数据的查看等时候会用到元素的选择, 目前PIE SDK支持元素的选择和去取消选择功能,下面对这两种功能如何使用进行介绍. 2功能实现说明 2.1元素的选择 2.1.1 实现思路及原理说明 ...