About the Calculate area geoprocessing function tool Sample
[C#]
CalculateAreaFunction.cs
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 { //Local members private string m_ToolName = "CalculateArea"; //Function Name 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 { // 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 = "CalculateArea"; 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 "AreaCalculation"; } } // 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 ("CalculateArea"): 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 ("CalculateArea"): 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 } }
[Visual Basic .NET]
CalculateAreaFunction.vb
Imports Microsoft.VisualBasic Imports System Imports System.Collections.Generic Imports System.Text Imports System.Runtime.InteropServices Imports ESRI.ArcGIS.Geodatabase Imports ESRI.ArcGIS.Geometry Imports ESRI.ArcGIS.Geoprocessing Imports ESRI.ArcGIS.esriSystem Imports ESRI.ArcGIS.DataSourcesFile Imports ESRI.ArcGIS.DataSourcesGDB Imports ESRI.ArcGIS.ADF.CATIDs Namespace GPCalculateArea Public Class CalculateAreaFunction : Implements IGPFunction2 ' Local members Private m_ToolName As String = "CalculateArea" 'Function Name Private m_MetaDataFile As String = "CalculateArea_area.xml" 'Metadata file Private m_Parameters As IArray ' Array of Parameters Private m_GPUtilities As New GPUtilities ' GPUtilities object #Region "IGPFunction2 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 ReadOnly Property Name() As String Implements IGPFunction2.Name Get Return m_ToolName End Get End Property ' Set the function tool Display Name as seen in ArcToolbox. Public ReadOnly Property DisplayName() As String Implements IGPFunction2.DisplayName Get Return "Calculate Area" End Get End Property ' 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 ReadOnly Property ParameterInfo() As IArray Implements IGPFunction2.ParameterInfo Get 'Array to the hold the parameters Dim pParameters As IArray = New ArrayClass() 'Input DataType is GPFeatureLayerType Dim inputParameter As IGPParameterEdit3 = New GPParameterClass() inputParameter.DataType = New GPFeatureLayerTypeClass() ' Default Value object is DEFeatureClass inputParameter.Value = New GPFeatureLayerClass() ' Set Input Parameter properties inputParameter.Direction = esriGPParameterDirection.esriGPParameterDirectionInput inputParameter.DisplayName = "Input Features" inputParameter.Name = "input_features" inputParameter.ParameterType = esriGPParameterType.esriGPParameterTypeRequired pParameters.Add(inputParameter) ' Area field parameter inputParameter = New GPParameterClass() inputParameter.DataType = New GPStringTypeClass() ' Value object is GPString Dim gpStringValue As IGPString = New GPStringClass() gpStringValue.Value = "Area" inputParameter.Value = CType(gpStringValue, IGPValue) ' Set field name parameter properties inputParameter.Direction = esriGPParameterDirection.esriGPParameterDirectionInput inputParameter.DisplayName = "Area Field Name" inputParameter.Name = "field_name" inputParameter.ParameterType = esriGPParameterType.esriGPParameterTypeRequired pParameters.Add(inputParameter) ' Output parameter (Derived) and data type is DEFeatureClass Dim outputParameter As IGPParameterEdit3 = New GPParameterClass() outputParameter.DataType = New GPFeatureLayerTypeClass() ' Value object is DEFeatureClass outputParameter.Value = New DEFeatureClass() 'Create a new feature schema object Dim featureSchema As IGPFeatureSchema featureSchema = New GPFeatureSchema Dim schema As IGPSchema schema = CType(featureSchema, IGPSchema) 'Clone the dependency schema.CloneDependency = True ' Set output parameter properties outputParameter.Direction = esriGPParameterDirection.esriGPParameterDirectionOutput outputParameter.DisplayName = "Output FeatureClass" outputParameter.Name = "out_featureclass" outputParameter.ParameterType = esriGPParameterType.esriGPParameterTypeDerived outputParameter.Schema = schema outputParameter.AddDependency("input_features") pParameters.Add(outputParameter) Return pParameters End Get End Property ' 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 Function Validate(ByVal paramvalues As IArray, ByVal updateValues As Boolean, ByVal envMgr As IGPEnvironmentManager) As IGPMessages Implements IGPFunction2.Validate If m_Parameters Is Nothing Then m_Parameters = ParameterInfo() End If ' Call UpdateParameters only if updatevalues is true If updateValues = True Then UpdateParameters(paramvalues, envMgr) End If ' Call InternalValidate (Basic Validation). Are all the required parameters supplied? ' Are the Values to the parameters the correct data type? Dim validateMsgs As IGPMessages validateMsgs = m_GPUtilities.InternalValidate(m_Parameters, paramvalues, updateValues, True, envMgr) ' Call UpdateMessages() UpdateMessages(paramvalues, envMgr, validateMsgs) ' Return the messages Return validateMsgs End Function ' This method will update the output parameter value with the additional area field. Public Sub UpdateParameters(ByVal paramvalues As ESRI.ArcGIS.esriSystem.IArray, ByVal pEnvMgr As ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager) Implements ESRI.ArcGIS.Geoprocessing.IGPFunction2.UpdateParameters m_Parameters = paramvalues ' Retrieve the input parameter value Dim parameterValue As IGPValue parameterValue = m_GPUtilities.UnpackGPValue(m_Parameters.Element(0)) ' Get the derived output feature class schema and empty the additional fields. ' This will ensure you don't get duplicate entries Dim derivedFeatures As IGPParameter3 derivedFeatures = CType(paramvalues.Element(2), IGPParameter3) Dim schema As IGPFeatureSchema schema = CType(derivedFeatures.Schema, IGPFeatureSchema) schema.AdditionalFields = Nothing ' If we have an input value, create a new field based on the field name the user entered If parameterValue.IsEmpty() = False Then Dim fieldNameParameter As IGPParameter3 fieldNameParameter = CType(paramvalues.Element(1), IGPParameter3) Dim fieldName As String fieldName = fieldNameParameter.Value.GetAsText() ' Check if the user's entered value already exists Dim areaField As IField areaField = m_GPUtilities.FindField(parameterValue, fieldName) If areaField Is Nothing Then Dim fieldsEdit As IFieldsEdit fieldsEdit = New Fields Dim fieldEdit As IFieldEdit fieldEdit = New Field fieldEdit.Name_2 = fieldName fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble fieldsEdit.AddField(fieldEdit) ' Add an additional field for the area values to the derived output Dim pFields As IFields pFields = fieldsEdit schema.AdditionalFields = pFields End If End If End Sub ' Called after returning from the internal validation routine. You can examine the messages created from internal validation and change them if desired. Public Sub UpdateMessages(ByVal paramvalues As ESRI.ArcGIS.esriSystem.IArray, ByVal pEnvMgr As ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager, ByVal Messages As ESRI.ArcGIS.Geodatabase.IGPMessages) Implements ESRI.ArcGIS.Geoprocessing.IGPFunction2.UpdateMessages ' Check for error messages Dim msg As IGPMessage msg = CType(Messages, IGPMessage) If msg.IsError() Then Return End If ' Get the first input parameter Dim parameter As IGPParameter parameter = CType(paramvalues.Element(0), IGPParameter) ' UnPackGPValue. This ensures you get the value either from ' the DataElement or from GPVaraible (ModelBuilder) Dim parameterValue As 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 Dim inputFeatureClass As IFeatureClass = Nothing Dim qf As IQueryFilter = Nothing m_GPUtilities.DecodeFeatureLayer(parameterValue, inputFeatureClass, qf) Dim fieldParameter As IGPParameter3 fieldParameter = CType(paramvalues.Element(1), IGPParameter3) Dim fieldName As String fieldName = fieldParameter.Value.GetAsText() ' Check if the field already exists and provide a warning Dim indexA As Integer indexA = inputFeatureClass.FindField(fieldName) If indexA > 0 Then Messages.ReplaceWarning(1, "Field already exists. It will be overwritten.") End If Return End Sub ' Execute: Execute the function given the array of the parameters Public Sub Execute(ByVal paramvalues As IArray, ByVal trackcancel As ITrackCancel, ByVal envMgr As IGPEnvironmentManager, ByVal message As IGPMessages) Implements IGPFunction2.Execute ' Get the first Input Parameter Dim parameter As IGPParameter = CType(paramvalues.Element(0), IGPParameter) ' UnPackGPValue. This ensures you get the value either form the dataelement or GpVariable (ModelBuilder) Dim parameterValue As IGPValue = m_GPUtilities.UnpackGPValue(parameter) ' Open the Input Dataset - use DecodeFeatureLayer as the input might be ' a layer file or a feature layer from ArcMap Dim inputFeatureClass As IFeatureClass = Nothing Dim qf As IQueryFilter = Nothing m_GPUtilities.DecodeFeatureLayer(parameterValue, inputFeatureClass, qf) If inputFeatureClass Is Nothing Then message.AddError(2, "Could not open input dataset.") Return End If ' Add the field if it does not exist. Dim indexA As Integer parameter = CType(paramvalues.Element(1), IGPParameter) Dim sField As String = parameter.Value.GetAsText() indexA = inputFeatureClass.FindField(sField) If indexA < 0 Then Dim fieldEdit As IFieldEdit = New FieldClass() fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble fieldEdit.Name_2 = sField message.AddMessage(sField) inputFeatureClass.AddField(fieldEdit) End If Dim featcount As Integer featcount = inputFeatureClass.FeatureCount(Nothing) ' Set the properties of the Step Progresson Dim pStepPro As IStepProgressor pStepPro = CType(trackcancel, IStepProgressor) pStepPro.MinRange = 0 pStepPro.MaxRange = featcount pStepPro.StepValue = 1 pStepPro.Message = "Calculate Area" pStepPro.Position = 0 pStepPro.Show() ' Create an Update Cursor indexA = inputFeatureClass.FindField(sField) Dim updateCursor As IFeatureCursor = inputFeatureClass.Update(Nothing, False) Dim updateFeature As IFeature = updateCursor.NextFeature() Dim geometry As IGeometry Dim area As IArea Dim dArea As Double Do While Not updateFeature Is Nothing geometry = updateFeature.Shape area = CType(geometry, IArea) dArea = area.Area updateFeature.Value(indexA) = dArea updateCursor.UpdateFeature(updateFeature) updateFeature.Store() updateFeature = updateCursor.NextFeature() pStepPro.Step() Loop pStepPro.Hide() ' Release the update cursor to remove the lock on the input data. System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor) End Sub ' 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 ReadOnly Property FullName() As IName Implements IGPFunction2.FullName Get ' Add CalculateArea.FullName getter implementation Dim functionFactory As IGPFunctionFactory = New CalculateAreaFunctionFactory() 'INSTANT VB NOTE: The local variable name was renamed since Visual Basic will not uniquely identify class members when local variables have the same name: Return CType(functionFactory.GetFunctionName(m_ToolName), IName) End Get End Property ' This is used to set a custom renderer for the output of the Function Tool. Public Function GetRenderer(ByVal pParam As IGPParameter) As Object Implements IGPFunction2.GetRenderer Return Nothing End Function ' 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 ReadOnly Property HelpContext() As Integer Implements IGPFunction2.HelpContext Get Return 0 End Get End Property ' This is the path to a .chm file which is used to describe and explain the function and its operation. Public ReadOnly Property HelpFile() As String Implements IGPFunction2.HelpFile Get Return "" End Get End Property ' This is used to return whether the function tool is licensed to execute. Public Function IsLicensed() As Boolean Implements IGPFunction2.IsLicensed Return True End Function ' 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 ReadOnly Property MetadataFile() As String Implements IGPFunction2.MetadataFile Get Return m_MetaDataFile End Get End Property ' This is the class id used to override the default dialog for a tool. ' By default, the Toolbox will create a dialog based upon the parameters returned ' by the ParameterInfo property. Public ReadOnly Property DialogCLSID() As UID Implements IGPFunction2.DialogCLSID Get Return Nothing End Get End Property #End Region #Region "IGPFunction Members" Public Function GetRenderer1(ByVal pParam As ESRI.ArcGIS.Geoprocessing.IGPParameter) As Object Implements ESRI.ArcGIS.Geoprocessing.IGPFunction.GetRenderer Return Nothing End Function Public ReadOnly Property ParameterInfo1() As ESRI.ArcGIS.esriSystem.IArray Implements ESRI.ArcGIS.Geoprocessing.IGPFunction.ParameterInfo Get Return ParameterInfo() End Get End Property Public ReadOnly Property DialogCLSID1() As ESRI.ArcGIS.esriSystem.UID Implements ESRI.ArcGIS.Geoprocessing.IGPFunction.DialogCLSID Get Return DialogCLSID End Get End Property Public ReadOnly Property DisplayName1() As String Implements ESRI.ArcGIS.Geoprocessing.IGPFunction.DisplayName Get Return DisplayName End Get End Property Public Sub Execute1(ByVal paramvalues As ESRI.ArcGIS.esriSystem.IArray, ByVal trackcancel As ESRI.ArcGIS.esriSystem.ITrackCancel, ByVal envMgr As ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager, ByVal message As ESRI.ArcGIS.Geodatabase.IGPMessages) Implements ESRI.ArcGIS.Geoprocessing.IGPFunction.Execute Call Execute(paramvalues, trackcancel, envMgr, message) End Sub Public ReadOnly Property FullName1() As ESRI.ArcGIS.esriSystem.IName Implements ESRI.ArcGIS.Geoprocessing.IGPFunction.FullName Get FullName1 = FullName End Get End Property Public ReadOnly Property HelpContext1() As Integer Implements ESRI.ArcGIS.Geoprocessing.IGPFunction.HelpContext Get Return HelpContext End Get End Property Public ReadOnly Property HelpFile1() As String Implements ESRI.ArcGIS.Geoprocessing.IGPFunction.HelpFile Get Return HelpFile End Get End Property Public Function IsLicensed1() As Boolean Implements ESRI.ArcGIS.Geoprocessing.IGPFunction.IsLicensed Return IsLicensed() End Function Public ReadOnly Property MetadataFile1() As String Implements ESRI.ArcGIS.Geoprocessing.IGPFunction.MetadataFile Get Return MetadataFile End Get End Property Public ReadOnly Property Name1() As String Implements ESRI.ArcGIS.Geoprocessing.IGPFunction.Name Get Return Name End Get End Property Public Function Validate1(ByVal paramvalues As ESRI.ArcGIS.esriSystem.IArray, ByVal updateValues As Boolean, ByVal envMgr As ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager) As ESRI.ArcGIS.Geodatabase.IGPMessages Implements ESRI.ArcGIS.Geoprocessing.IGPFunction.Validate Return Validate(paramvalues, updateValues, envMgr) End Function #End Region End Class '//////////////////////////// ' Function Factory Class '////////////////////////// <Guid("2554BFC7-94F9-4d28-B3FE-14D17599B35A"), ComVisible(True)> _ Public Class CalculateAreaFunctionFactory : Implements IGPFunctionFactory Private m_GPFunction As IGPFunction ' Register the Function Factory with the ESRI Geoprocessor Function Factory Component Category. #Region "Component Category Registration" <ComRegisterFunction()> _ Private Shared Sub Reg(ByVal regKey As String) GPFunctionFactories.Register(regKey) End Sub <ComUnregisterFunction()> _ Private Shared Sub Unreg(ByVal regKey As String) GPFunctionFactories.Unregister(regKey) End Sub #End Region ' Utility Function added to create the function names. Private Function CreateGPFunctionNames(ByVal index As Long) As IGPFunctionName Dim functionName As IGPFunctionName = New GPFunctionNameClass() 'INSTANT VB NOTE: The local variable name was renamed since Visual Basic will not uniquely identify class members when local variables have the same name: Dim name_Renamed As IGPName Select Case index Case (0) name_Renamed = CType(functionName, IGPName) name_Renamed.Category = "AreaCalculation" name_Renamed.Description = "Calculate Area for FeatureClass" name_Renamed.DisplayName = "Calculate Area" name_Renamed.Name = "CalculateArea" name_Renamed.Factory = Me End Select Return functionName End Function ' 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 ReadOnly Property Name() As String Implements IGPFunctionFactory.Name Get Return "AreaCalculation" End Get End Property ' This is the alias name of the factory. Public ReadOnly Property [Alias]() As String Implements IGPFunctionFactory.Alias Get Return "area" End Get End Property ' This is the class id of the factory. Public ReadOnly Property CLSID() As UID Implements IGPFunctionFactory.CLSID Get Dim id As UID = New UIDClass() id.Value = Me.GetType().GUID.ToString("B") Return id End Get End Property ' This method will create and return a function object based upon the input name. Public Function GetFunction(ByVal Name As String) As IGPFunction Implements IGPFunctionFactory.GetFunction Select Case Name Case ("CalculateArea") m_GPFunction = New CalculateAreaFunction() End Select Return m_GPFunction End Function ' This method will create and return a function name object based upon the input name. Public Function GetFunctionName(ByVal Name As String) As IGPName Implements IGPFunctionFactory.GetFunctionName Dim gpName As IGPName = New GPFunctionNameClass() Select Case Name Case ("CalculateArea") Return CType(CreateGPFunctionNames(0), IGPName) End Select Return gpName End Function ' This method will create and return an enumeration of function names that the factory supports. Public Function GetFunctionNames() As IEnumGPName Implements IGPFunctionFactory.GetFunctionNames ' Add CalculateFunctionFactory.GetFunctionNames implementation Dim nameArray As IArray = New EnumGPNameClass() nameArray.Add(CreateGPFunctionNames(0)) Return CType(nameArray, IEnumGPName) End Function ' 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 Function GetFunctionEnvironments() As IEnumGPEnvironment Implements IGPFunctionFactory.GetFunctionEnvironments Return Nothing End Function #End Region End Class End Namespace