[GIS] Getting map using C# code in ArcMap

arcgis-10.2arcobjectsc

I have been programming in C# a small code for:1) create a polyline Featureclass in a GDB, 2)put that in the map view (ArcMap 10.2) and finally 3) edit that feature using a button or tool…
Well I have tried to program it using AddInns controls, but I faced the trouble at the time to edit the feature; I switch to use "Extending ArcObjects" now I can create the feature in my local GDB easily, but at the momento to put the Feature into the map I can not do it; the code is as follows:

GLB.nombre = txtNombre.Text;//definig the name from a text box in a regular form....
IWorkspaceFactory pWorkSpaceFactory = new FileGDBWorkspaceFactoryClass();
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)pWorkSpaceFactory.OpenFromFile(@"C:\INFO_2015\ANH_GDB.gdb", 0);
IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset("Temp");

IFeatureClass featureClass;

UID CLSID = new UIDClass();
UID CLSEXT = null;
CLSID.Value = "esriGeoDatabase.Feature";
IObjectClassDescription objectClassDescription = new FeatureClassDescriptionClass();

IGeometryDef pGeoDefL = new GeometryDefClass();
var pGeoDefEditL = (IGeometryDefEdit)pGeoDefL;
pGeoDefEditL.GeometryType_2 = esriGeometryType.esriGeometryPolyline;

//here I define the fields of the DB.......
IFields fields = new FieldsClass();
fields = objectClassDescription.RequiredFields;
IFieldsEdit fieldsEdit = (IFieldsEdit)fields; // Explicit Cast
fieldsEdit.FieldCount_2 = 3;
// ID Field
IField oIdFieldL = new FieldClass();
var oIdFieldEditL = (IFieldEdit)oIdFieldL; // Explicit Cast
oIdFieldEditL.Name_2 = "ObjectID";
oIdFieldEditL.Type_2 = esriFieldType.esriFieldTypeOID;
fieldsEdit.set_Field(0, oIdFieldL);
//Entity type
IField shpFieldL = new FieldClass();
var shpFieldEditL = (IFieldEdit)shpFieldL; // Explicit Cast
shpFieldEditL.Name_2 = "Shape";
shpFieldEditL.Type_2 = esriFieldType.esriFieldTypeGeometry;
shpFieldEditL.GeometryDef_2 = pGeoDefL;
fieldsEdit.set_Field(1, shpFieldL);
//line lenght
IField longFieldL = new FieldClass();
var longFieldEditL = (IFieldEdit)longFieldL;
longFieldEditL.Name_2 = "Longitud";
longFieldEditL.Type_2 = esriFieldType.esriFieldTypeDouble;
fieldsEdit.set_Field(2, longFieldL);
// add field to field collection
fields = fieldsEdit; // Explicit Cast

IFieldChecker fieldChecker = new FieldCheckerClass();
IEnumFieldError enumFieldError = null;
IFields validatedFields = null;
fieldChecker.ValidateWorkspace = (IWorkspace)featureWorkspace;
fieldChecker.Validate(fields, out enumFieldError, out validatedFields);

featureClass = featureDataset.CreateFeatureClass(GLB.nombre, validatedFields, CLSID, CLSEXT, esriFeatureType.esriFTSimple, "Shape", "");

IApplication application;//HERE IS THE ERROR!!!!!!!!
IMxDocument mxDocument = ((IMxDocument)(application.Document)); // Explicit Cast
IActiveView activeView = mxDocument.ActiveView;
IMap map = activeView.FocusMap;

IFeatureLayer featureLayer = new FeatureLayerClass();
featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass(GLB.nombre);
featureLayer.Name = GLB.nombre;
map.AddLayer(featureLayer);

mxDocument.UpdateContents();
mxDocument.ActiveView.Refresh();

I have been working on it more than three weeks without solution, could anybody give me a hand?

Best Answer

You need to set your application variable to reference the application. In old school ArcObjects this done through the hook provided by your control that implements ICommand or ITool etc.

   public override void OnCreate(object hook)
    {
      _application = hook as IApplication;
      //get the editor environment
      UID editorUid = new UID();
      editorUid.Value = "esriEditor.Editor";
      _editor = m_application.FindExtensionByCLSID(editorUid) as IEditor;
    }

In add-ins you can use the ArcMap keyword to get at the application. The example below is in the tools constructor method.

public CCTool()
{
  //get the editor environment
  UID editorUid = new UID();
  editorUid.Value = "esriEditor.Editor";
  _editor = ArcMap.Application.FindExtensionByCLSID(editorUid) as IEditor;
}

The above examples are for code within an ArcMap Application. If you have a stand-alone application that drives ArcMap then you can use the code referenced in the Esri help topic Automating ArcGIS for Desktop applications. This shows how to start ArcMap and get a reference to IApplication.

Related Question