Created
October 2, 2019 14:51
-
-
Save BrunoCaimar/cba37168cee417c58da8d9319bc2d7dc to your computer and use it in GitHub Desktop.
AO - Exemplos Adicionar novo campo, apagar campo, renomear e calcular campo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ESRI.ArcGIS.DataManagementTools; | |
using ESRI.ArcGIS.esriSystem; | |
using ESRI.ArcGIS.Geodatabase; | |
using ESRI.ArcGIS.Geoprocessor; | |
using Neoenergia.ModuloProjetos.Util.Test; | |
using System; | |
using System.Runtime.InteropServices; | |
namespace GeoprocessorExemplo | |
{ | |
internal class Program | |
{ | |
private static int Main(string[] args) | |
{ | |
try | |
{ | |
Console.WriteLine("Iniciando licenca"); | |
IniciaLicenca(); | |
Console.WriteLine("Abrindo WS"); | |
var workspace = Testing.AbrirWorkspace("SDE.DEFAULT"); | |
var nomeTabela = "NEOSDE.ATeste"; | |
var novoCampo = "NovoCampo"; | |
var campoAntigo = "Campo2"; | |
Console.WriteLine("Adicionando campo"); | |
AdicionarCampo(workspace, nomeTabela, novoCampo); | |
Console.WriteLine("Calcular campo"); | |
TransferirValorCampo(nomeTabela, campoAntigo, novoCampo); | |
Console.WriteLine("Apagando campo GP"); | |
ApagarCampo(nomeTabela, campoAntigo); | |
Console.WriteLine("Renomeando campo GP"); | |
RenomearCampo(nomeTabela, novoCampo, campoAntigo); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($">>>>>>>> Erro: {ex}"); | |
Console.Error.WriteLine($">>>>>>>> Erro: {ex}"); | |
} | |
Console.WriteLine("--------------------------------"); | |
Console.WriteLine("Digite uma tecla para finalizar!"); | |
Console.ReadKey(); | |
return 0; | |
} | |
private static void AdicionarCampo(IWorkspace workspace, string nomeTabela, string nomeCampo) | |
{ | |
var table2AddField = ((IFeatureWorkspace)workspace).OpenTable(nomeTabela); | |
var schemaLock = (ISchemaLock)table2AddField; | |
try | |
{ | |
schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); | |
var newField = new FieldClass(); | |
var field = (IFieldEdit2)newField; | |
field.Name_2 = nomeCampo; | |
field.Type_2 = esriFieldType.esriFieldTypeString; | |
field.Length_2 = 50; | |
field.DefaultValue_2 = "Teste"; | |
table2AddField.AddField(newField); | |
} | |
finally | |
{ | |
schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); | |
} | |
} | |
private static void ApagarCampo(string nomeTabela, string nomeCampo) | |
{ | |
var dropField = new DeleteField(nomeTabela, nomeCampo); | |
ExecutarGP(dropField); | |
} | |
private static esriJobStatus ExecutarGP(IGPProcess process2Execute) | |
{ | |
Console.WriteLine($"Executando GP... {process2Execute}"); | |
var gp = new Geoprocessor(); | |
gp.SetEnvironmentValue("workspace", | |
@"C:\Users\bcaimar\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\[email protected]"); | |
var retorno = esriJobStatus.esriJobFailed; | |
EventHandler<ToolExecutedEventArgs> executedEvent = (s, e) => | |
{ | |
retorno = e.GPResult.Status; | |
Console.WriteLine($"\nTEE> {e.GPResult.Status} - {e.GPResult.ResultID}"); | |
}; | |
gp.ToolExecuted += executedEvent; | |
try | |
{ | |
gp.Execute(process2Execute, null); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Erro: {ex.Message} - {ex.StackTrace}"); | |
if (ex is COMException) | |
{ | |
Console.WriteLine($"Erro (COM): {(ex as COMException).ErrorCode}"); | |
} | |
throw; | |
} | |
finally | |
{ | |
if (gp != null) | |
{ | |
Console.WriteLine($"**********************************"); | |
for (int i = 0; i < gp.MessageCount; i++) | |
{ | |
var msg = gp.GetMessage(i); | |
var rc = gp.GetReturnCode(i); | |
Console.WriteLine($"msg: {rc}-{msg}"); | |
} | |
Console.WriteLine($"**********************************"); | |
gp.ToolExecuted -= executedEvent; | |
} | |
} | |
return retorno; | |
} | |
private static void IniciaLicenca() | |
{ | |
Testing.InicializarLicenca(); | |
} | |
private static void RenomearCampo(string nomeTabela, string nomeCampo, string novoNomeCampo) | |
{ | |
var alterField = new AlterField(nomeTabela, nomeCampo) | |
{ | |
new_field_name = novoNomeCampo | |
}; | |
var ret = ExecutarGP(alterField); | |
} | |
private static void TransferirValorCampo(string nomeTabela, string campoAtual, string novoCampo) | |
{ | |
var expressaoCalculo = $"!{campoAtual}!"; | |
var gpCalculate = new CalculateField(nomeTabela, novoCampo, expressaoCalculo) | |
{ | |
expression_type = "PYTHON" | |
}; | |
ExecutarGP(gpCalculate); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment