Select to view content in your preferred language

Programmatically  Adding layer to the Arcmap Using C#

8506
10
09-26-2010 01:49 AM
by Anonymous User
Not applicable
Original User: Shrey

Hi All,


     After the user Login to the system I am Invoking the Arcmap by a function. While doing so I am also calling the function to Add the shapefile required in my application .For this I am using the function as mentioned below :



   public void StartArcMap()
        {
            if (m_pDoc == null)
            {
                // Cursor.Current = Cursors.WaitCursor;

                //Start arcmap
                CheckLicence();
                m_pDoc = new MxDocumentClass();

                //Get a reference to the application
                m_application = m_pDoc.Parent;



                m_appHWnd = m_application.hWnd;
                m_application.Caption = "Registeration System V 3.0";
                m_pMxdocument = m_application.Document as IMxDocument;
               
             
                //Show arcmap
                m_application.Visible = true;
               
                //Calling to Add The Shape File
                AddShapefile();

                //Hide All toolbars of Arcmap
              //  m_application.Document.CommandBars.HideAllToolbars();



             
                //Get the commandbars collection
                ICommandBars pCmdBars;
                pCmdBars = m_application.Document.CommandBars;
                //Create New ToolBar
                ICommandBar pNewBar;
                pNewBar = pCmdBars.Create("Adminstrator", ESRI.ArcGIS.SystemUI.esriCmdBarType.esriCmdBarTypeToolbar);

                UIDClass clsSearch = new UIDClass();
                clsSearch.Value = "Tools.clsSearch";
                int intclsSearch = pNewBar.Count;
                object objclsSearch = intclsSearch;
                pNewBar.Add(clsSearch,ref objclsSearch);  

 
                 
             



             
            }
        }//Start the Arcmap Application


  public void AddShapefile()
        {

         
            try
            {


                //IWorkspace pworkspace;
                IWorkspaceFactory pWorkspaceFactory;
                IFeatureWorkspace pFeatureWorkSpace;
                IFeatureClass pFeatureClass;
                IGeoFeatureLayer pGeoFeatureLayer;

                pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
                //pFeatureWorkSpace = (IFeatureWorkspace)pWorkspaceFactory;
                pFeatureWorkSpace = pWorkspaceFactory.OpenFromFile(@"C:\Program Files\ArcGIS\DeveloperKit\SamplesNET\data\AirportsAndGolf", 0) as IFeatureWorkspace;

                pFeatureClass = pFeatureWorkSpace.OpenFeatureClass("STATE");
                pGeoFeatureLayer = new FeatureLayerClass();
                pGeoFeatureLayer.Name = pFeatureClass.AliasName;
                pGeoFeatureLayer.FeatureClass = pFeatureClass;

                m_pMxdocument.AddLayer((ILayer)pGeoFeatureLayer);

                // Ading the Road LAyer
                pFeatureWorkSpace = pWorkspaceFactory.OpenFromFile(@"C:\Program Files\ArcGIS\DeveloperKit\SamplesNET\data\AirportsAndGolf", 0) as IFeatureWorkspace;

                pFeatureClass = pFeatureWorkSpace.OpenFeatureClass("AIRPORT");
                pGeoFeatureLayer = new FeatureLayerClass();
                pGeoFeatureLayer.Name = pFeatureClass.AliasName;
                pGeoFeatureLayer.FeatureClass = pFeatureClass;

                m_pMxdocument.AddLayer((ILayer)pGeoFeatureLayer);
            }
            catch (Exception ex)
            {
                MessageBox.Show(""+ex);

            }
 
        }//End OF Adding the Shapefile


But while doing so the I cannot open the shapefiles and system shows the following error (Attachment one) and if I try some other shape files it Arcmap will shut down and the Error 2 will be show (Attachment)


Please require your help desperately. Thank You
0 Kudos
10 Replies
KevinNetherton
Emerging Contributor
Was just trying to do this with Java.  I found the problem for me was adding a layer directly to the map document.  I know the api indicates that this should be possible!

http://resources.arcgis.com/en/help/arcobjects-java/api/arcobjects/index.html

In the end I found I had to get the IMap object from the document and add the layer to that object.  In a nutshell this is what worked for me in Java, I expect the code should be similar for C#/.Net. 


...
 public void addShapeFile2() {
  // THIS ONE WORKS!
  try{
   String mapDocPath = "C:\\eclipse_config\\3.7.2\\gba\\ca.bc.gov.AE\\data\\out.mxd";
   MapDocument mapDoc = new MapDocument();
   mapDoc.esri_new(mapDocPath);
   
   
   ShapefileWorkspaceFactory wsf = new ShapefileWorkspaceFactory();
   String shapeFileWrkSpcStr = "C:\\eclipse_config\\3.7.2\\gba\\ca.bc.gov.gbaui\\data\\set1";
   String shapeFileStr = "locality.shp";
   Workspace work = new Workspace(wsf.openFromFile(shapeFileWrkSpcStr,0));
   IFeatureClass featureClass = work.openFeatureClass(shapeFileStr);
   FeatureLayer fl = new FeatureLayer();
   fl.setFeatureClassByRef(featureClass);
   fl.setName("Locality");
   ILayer iFl = (ILayer)fl;
   // getting the map object from the mapdocument object:
   IMap imap = mapDoc.getMap(0);
   // add the layer to the imap object!
   imap.addLayer(iFl);
   mapDoc.save(true, true);
   mapDoc.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
...



Hope this helps someone!
0 Kudos