[C#] Adding a layer to shapefile from Add Data Dialog

3193
4
Jump to solution
04-02-2013 04:42 PM
ConorBarber1
New Contributor II
Hello,

I am trying to add a layer to a shapefile. My code is currently very similar to what is provided by ESRI:

private void AddShapeFile() {   // Create a new ShapefileWorkspaceFactory object and   // open a shapefile folder - the path works with standard 9.3 installation   IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();   IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)     workspaceFactory.OpenFromFile(     "C:\\Program Files\\ArcGIS\\DeveloperKit\\SamplesNET\\data\\Y2000HurricaneData", 0);   // Create a new FeatureLayer and assign a shapefile to it   IFeatureLayer featureLayer = new FeatureLayerClass();   featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass("2000_hrcn");   ILayer layer = (ILayer)featureLayer;   layer.Name = featureLayer.FeatureClass.AliasName;   // Add the Layer to the focus map   ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument =      (ESRI.ArcGIS.ArcMapUI.IMxDocument)(m_application.Document);   IMap map = mxDocument.FocusMap;   map.AddLayer(layer); }


However, I cannot get the "m_application" reference to work. The instructions provided say that this code "assumes that you already have m_application variable as a reference to the Application object in your .NET custom component". What does this mean? How do I make this reference?

Below is my code FWIW. It essentially does the same as above except passes a shapefile chosen from an Add Data dialog that I created from IgxObject.



        private void btnOpenShapefile_Click(object sender, EventArgs e)         {             ESRI.ArcGIS.Catalog.IGxObjectFilterCollection pGxFilter; //establish a collection of filters             ESRI.ArcGIS.Catalog.GxFilterShapefiles pfilter2; //create a filter              Boolean notanythingselected;             ESRI.ArcGIS.Catalog.IGxObject gxObj; //declare an instance of GxObjects             ESRI.ArcGIS.CatalogUI.IGxDialog pGxDia; //declare an instance of GxDialogs             pfilter2 = new ESRI.ArcGIS.Catalog.GxFilterShapefiles(); //create a filter instance               pGxDia = new GxDialogClass(); //new dialog box object             pGxDia.Title = "Choose a Shapefile";              pGxFilter = (ESRI.ArcGIS.Catalog.IGxObjectFilterCollection)pGxDia;               pGxFilter.AddFilter(pfilter2, true);             ESRI.ArcGIS.Catalog.IEnumGxObject gxEnum = null;                            notanythingselected = pGxDia.DoModalOpen(this.Handle.ToInt32(), out gxEnum);             if (notanythingselected == false)             {                 return;             }             else             {                 gxEnum.Reset();                 gxObj = gxEnum.Next();                  ESRI.ArcGIS.Geodatabase.IWorkspaceFactory wksFact;                 wksFact = new ShapefileWorkspaceFactory();                                       ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featWrk;                 featWrk = (IFeatureWorkspace)wksFact.OpenFromFile(gxObj.Parent.FullName, 0);                  ESRI.ArcGIS.Geodatabase.IFeatureClass fClass;                 fClass = featWrk.OpenFeatureClass(gxObj.Name);                  ESRI.ArcGIS.Carto.IFeatureLayer lyr;                 lyr = new FeatureLayer();                 lyr.FeatureClass = fClass;                 lyr.Name = gxObj.Name;                  ESRI.ArcGIS.ArcMapUI.IMxDocument mxDoc = (ESRI.ArcGIS.ArcMapUI.IMxDocument)(m_application.Document); //ERROR IS HERE AT M_application                 IMap map = mxDoc.FocusMap;                 map.AddLayer(lyr);                 


I hope this has a super simple answer. I'm pretty new at this 🙂
0 Kudos
1 Solution

Accepted Solutions
AlexanderGray
Occasional Contributor III
For an addin the code to access the application is already baked into the config.Designer.cs.  All you need to do is use the "ArcMap" object.  ArcMap.Application gives you a reference to the application object.  You can access the ArcMap object in your form.

View solution in original post

0 Kudos
4 Replies
AlexanderGray
Occasional Contributor III
ok, there is some info missing to be able to answer your question.  First of all, is this an ArcGIS desktop or engine application?  Secondly, are you developing an addin or extension code.  Thirdly, how is the code called from the application?  Did you create a command or menu?  Is it an event handler.  The application member variable is usually stubbed out for you when you create an extension, command, tool, etc.  Most ArcObjects UI components have an on create or equivalent method that passes in a hook or something that holds a reference to the application.  That is what they mean when they assume you already have a reference to the application.  In 99% of cases you can use that reference, in other cases you get it from the apprefclass.
0 Kudos
ConorBarber1
New Contributor II
Hello Alexander! Thank you for the reply! Your preliminary answer has provided insight. I want to truly understand exactly what is happening in my code, so I'm going to try and get a bit more detailed in explaining where I am not understanding.

First off, here are the answers to your questions:

First of all, is this an ArcGIS desktop or engine application?


It's an ArcGIS desktop application. It is a Windows form in written in C# that opens when you click a button in ArcGIS desktop.

Secondly, are you developing an addin or extension code?


I am developing an addin.

Thirdly, how is the code called from the application?  Did you create a command or menu?  Is it an event handler?


The code is called when you click an "Open" button on the aforementioned Windows form. The purpose of the code is to browse for a shapefile, and once a shapefile is chosen to load the shapefile into the .mxd. The code accesses ESRI's "Add Data" Dialog through the IGxDialog interface and then creates a feature class in the .mxd with which to store the shapefile before loading it.



What I drew from your post (correct me if I am wrong) is that I need to use some sort of method (like an OnCreate) to pass in a reference to the map application so that m_application points to the map application. If this is the case, why? Does ArcObjects come in with a "built in" way to reference the current map application? Also, if this is the case, do you have a small snippet of what code like that might look like?

Thanks again for your help!

Conor
0 Kudos
AlexanderGray
Occasional Contributor III
For an addin the code to access the application is already baked into the config.Designer.cs.  All you need to do is use the "ArcMap" object.  ArcMap.Application gives you a reference to the application object.  You can access the ArcMap object in your form.
0 Kudos
ConorBarber1
New Contributor II
That was what I needed. Thank you.
0 Kudos