public void Execute(ESRI.ArcGIS.esriSystem.IArray paramvalues, ESRI.ArcGIS.esriSystem.ITrackCancel TrackCancel, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager envMgr, ESRI.ArcGIS.Geodatabase.IGPMessages message) { try { IGPUtilities3 gpUtilities3 = new GPUtilitiesClass(); if (TrackCancel == null) { TrackCancel = new CancelTrackerClass(); } IGPParameter inputSourceLayerParameter = paramvalues.get_Element(in_sourcelayerNumber) as IGPParameter; IGPValue inputSourceLayerGPValue = gpUtilities3.UnpackGPValue(inputSourceLayerParameter) as IGPValue; IGPParameter inputTargetLayerParameter = paramvalues.get_Element(in_targetlayerNumber) as IGPParameter; IGPValue inputTargetLayerGPValue = gpUtilities3.UnpackGPValue(inputTargetLayerParameter) as IGPValue; ILayer sourceLayer = gpUtilities3.DecodeLayer(inputSourceLayerGPValue); ILayer targetLayer = gpUtilities3.DecodeLayer(inputTargetLayerGPValue); ILayerExtensions sourceLayerExtensions = sourceLayer as ILayerExtensions; if (sourceLayerExtensions == null) { message.AddWarning(resourceManager.GetString("GPTools_GPCopyLayerExtension_source_noext_support")); return; } ILayerExtensions targetLayerExtensions = targetLayer as ILayerExtensions; if (targetLayerExtensions == null) { message.AddWarning(resourceManager.GetString("GPTools_GPCopyLayerExtension_target_noext_support")); return; } // test if the feature classes already exists, // if they do and the environments settings are such that an overwrite is not allowed we need to abort at this point IGeoProcessorSettings gpSettings = (IGeoProcessorSettings)envMgr; if (gpSettings.OverwriteOutput == true) { } else { if (gpUtilities3.Exists(inputTargetLayerGPValue) == true) { message.AddError(120003, String.Format(resourceManager.GetString("GPTools_GPCopyLayerExtension_targetlayeralreadyexists"), inputTargetLayerGPValue.GetAsText())); return; } } for (int targetExtensionIndex = 0; targetExtensionIndex < targetLayerExtensions.ExtensionCount; targetExtensionIndex++) { targetLayerExtensions.RemoveExtension(targetExtensionIndex); } for (int sourceExtensionIndex = 0; sourceExtensionIndex < sourceLayerExtensions.ExtensionCount; sourceExtensionIndex++) { targetLayerExtensions.AddExtension(sourceLayerExtensions.get_Extension(sourceExtensionIndex)); } } catch (Exception ex) { message.AddError(120004, ex.Message); } }
public static IFeatureClass CreateFeatureClass(IGPValue gpFeatureClass, IGPEnvironmentManager environment, IFields fields = null) { if (gpFeatureClass == null) { throw new ArgumentException("Argument can not be null", "gpFeatureClass"); } if (environment == null) { throw new ArgumentException("Argument can not be null", "environment"); } IGeoProcessorSettings gpSettings = (IGeoProcessorSettings)environment; if (gpUtilities.Exists(gpFeatureClass)) { if (gpSettings.OverwriteOutput == true) { gpUtilities.Delete(gpFeatureClass); } else { throw new Exception("Output feature class already exists: " + gpFeatureClass.GetAsText()); } } IDEFeatureClass deFeatureClass = (IDEFeatureClass)gpUtilities.DecodeDETable(gpFeatureClass); if (deFeatureClass == null) { throw new Exception("Data Element decode return null"); } IObjectClassDescription objectClassDescription = (IObjectClassDescription) new FeatureClassDescriptionClass(); UID clsid = objectClassDescription.InstanceCLSID; UID extclsid = objectClassDescription.ClassExtensionCLSID; IDataElement dataElement = (IDataElement)deFeatureClass; if (dataElement.CatalogPath == null) { throw new ArgumentException("Catalog path is null", "CatalogPath"); } IFeatureClassName featureClassName = (IFeatureClassName)gpUtilities.CreateFeatureClassName(dataElement.CatalogPath); string path = dataElement.GetPath(); string name = dataElement.Name; IDEGeoDataset geoDataElement = (IDEGeoDataset)deFeatureClass; ISpatialReference spatialReference = geoDataElement.SpatialReference; IDETable deTable = (IDETable)deFeatureClass; string shapeFieldName = deFeatureClass.ShapeFieldName; Dictionary <string, IField> fieldBuilder = new Dictionary <string, IField>(); foreach (var input_fields in new IFields[] { deTable.Fields, fields }) { if (input_fields == null) { continue; } for (int i = 0; i < input_fields.FieldCount; i++) { IField field = deTable.Fields.get_Field(i); if (fieldBuilder.ContainsKey(field.Name.ToLower())) { fieldBuilder[field.Name.ToLower()] = (IField)((IClone)field).Clone(); } else { fieldBuilder.Add(field.Name.ToLower(), (IField)((IClone)field).Clone()); } if (field.Type == esriFieldType.esriFieldTypeGeometry) { shapeFieldName = field.Name; break; } } } IFields output_fields = new FieldsClass(); IFieldsEdit fields_edit = (IFieldsEdit)output_fields; foreach (IField field in fieldBuilder.Values) { fields_edit.AddField(field); if (field.Type == esriFieldType.esriFieldTypeGeometry) { IGeometryDefEdit defEdit = (IGeometryDefEdit)field.GeometryDef; defEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; } } string configKeyword = ((IGPString)environment.FindEnvironment(CONFIG_KEYWORD_PROP_NAME).Value).Value; //if (String.IsNullOrWhiteSpace(configKeyword)) configKeyword = "DEFAULTS"; IFeatureClass ret = null; if (featureClassName.FeatureDatasetName != null) { IFeatureDataset featureDataset = (IFeatureDataset)((IName)featureClassName.FeatureDatasetName).Open(); try { ret = featureDataset.CreateFeatureClass(name, output_fields, clsid, extclsid, esriFeatureType.esriFTSimple, shapeFieldName, configKeyword); } finally { Marshal.ReleaseComObject(featureDataset); } } else { IWorkspace workspace = (IWorkspace)((IName)((IDatasetName)featureClassName).WorkspaceName).Open(); try { IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; ret = featureWorkspace.CreateFeatureClass(name, output_fields, clsid, extclsid, esriFeatureType.esriFTSimple, shapeFieldName, configKeyword); } finally { Marshal.ReleaseComObject(workspace); } } return(ret); }