1

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?

  • IApplication application = (IApplication) ArcMap.Application; the ArcMap application is type IMXApplication so it needs to be cast to an IApplication object. This is only for Esri Addin type projects. – Michael Stimson Mar 23 '15 at 1:01
  • Yes, if I use ADDINS projects, the code you wrote, works nicely, but as I told in my question I have developing an "Extended ArcObject" application, and it does not work at all. – Oscar Calderón Landaeta Mar 24 '15 at 19:05
  • I am about to leave the project, I have understood that there is no way to do it through "Extending ArcObjects" because I have been trying many ways to do it and nothing works; well I solved the problem, really easily, using ADD-INS control... so now I have facing another problem I will share to you in another question........ – Oscar Calderón Landaeta Mar 24 '15 at 19:08
1

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.

  • Thanks for your answer, the problem is that in the code line after where the error is C# marks "application" and tells me: "Local variable 'application' might not be initialized before accesing"; I have try to initialize the variable like: – Oscar Calderón Landaeta Mar 18 '15 at 19:03
  • IApplication application = new ESRI.ArcGIS.Framework.IApplication;//does not work IApplication application = new AppRefClass();//doesn't either – Oscar Calderón Landaeta Mar 18 '15 at 19:21
  • 1
    I have not working with addins, in addins I have no problem with this; I am building an app "Extending ArcObjects", so it has a different treatment. In addins is simple, you only have to write the code like: IApplication application = ArcMap.Application; – Oscar Calderón Landaeta Mar 18 '15 at 19:22
  • Finally I have understood that there is no way to do it through "Extending ArcObjects" because I have been trying many ways to do it and nothing works; well I solved the problem, really easily, using ADD-INS control... so now I have facing another problem I will share to you in another question........ – Oscar Calderón Landaeta Mar 24 '15 at 19:00
  • SeaJunk that is old school, doing it that way is being depreciated in favor of addins. Can you extend that to show the new & improved addin way and show how to tell the difference? – Michael Stimson Mar 24 '15 at 21:40
0

You can get the map in "Extending ArcObjects" using below code:

 Type typ = Type.GetTypeFromCLSID(typeof(AppRefClass).GUID);
 System.Object obj = Activator.CreateInstance(typ);
 IApplication app = obj as IApplication;
 IMap map = (app.Document as IMxDocument).FocusMap;

Check this http://edndoc.esri.com/arcobjects/9.2/ComponentHelp/esriFramework/AppRef.htm

If the extending ArcObject type is command you can get the application object from "OnCreate" method(defaultly created) of BaseCommand where application is from the hook object.

    /// <summary>
    /// Occurs when this command is created
    /// </summary>
    /// <param name="hook">Instance of the application</param>
    public override void OnCreate(object hook)
    {
        if (hook == null)
            return;

        m_application = hook as IApplication;

        //Disable if it is not ArcMap
        if (hook is IMxApplication)
            base.m_enabled = true;
        else
            base.m_enabled = false;

        // TODO:  Add other initialization code
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.