... 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(); } } ...
If you look at the IObjectFactory help topic in the developer help you'll see there is a code sample as well as a link to additional information.
m_pDoc = new MxDocumentClass();You're starting ArcMap programatically and trying to use automation to manipulate it. ArcMap was never designed to do this so you may run into some problems depending on what you try to do. That being said, you can add layers to the map. However, your application is running in one process and ArcMap is running in another process. ArcObjects are single apartment threaded, meaning you can't create an instance of an ArcObjects class in one process and use it in another process. If you want to use ArcObjects classes in conjunction with the instance of ArcMap that you have started then you will need to create the instances of those ArcObjects in the process in which ArcMap is running. You can do this using the IObjectFactory interface.
Shrey,Not sure if this response will help you, but try the following -- change the line:"IGeoFeatureLayer pGeoFeatureLayer;" to "IFeatureLayer pGeoFeatureLayer;"and see what happens.Another thing that might help you debug these things is in the "catch" block to use the Exception's StackTrace. I like to code it as follows:" MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace); "That way you are shown specifically which line is causing the problem.
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.