Exemplo n.º 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            IGxDialog gxd = new GxDialog();

            gxd.AllowMultiSelect = false;
            gxd.ButtonCaption    = "Add";
            gxd.Title            = "Add a shapefile";
            gxd.RememberLocation = true;

            IGxObjectFilter           filter1 = new GxFilterFileGeodatabases();
            IGxObjectFilter           filter2 = new GxFilterShapefiles();
            IGxObjectFilterCollection filters = gxd as IGxObjectFilterCollection;

            filters.AddFilter(filter1, true);
            filters.AddFilter(filter2, false);

            IEnumGxObject enumObj;

            if (gxd.DoModalOpen(ArcMap.Application.hWnd, out enumObj) == false) // show dialog
            {
                return;                                                         // return if clicking on cancel
            }
            IGxObject gxObj    = enumObj.Next();
            int       len1     = gxObj.FullName.Length;
            int       len2     = gxObj.Name.Length;
            string    shpPath  = gxObj.FullName.Substring(0, len1 - len2);
            string    shpPath2 = gxObj.FullName.Substring(0);

            IMxDocument mxdoc = ArcMap.Application.Document as IMxDocument;

            IWorkspaceFactory wsf          = new ShapefileWorkspaceFactory();
            IWorkspace        ws           = wsf.OpenFromFile(shpPath, 0);
            IFeatureWorkspace fws          = ws as IFeatureWorkspace;
            IFeatureClass     featureClass = fws.OpenFeatureClass(gxObj.BaseName);
            IFeatureLayer     featureLayer = new FeatureLayer();

            featureLayer.FeatureClass = featureClass;
            ILayer layer = (ILayer)featureLayer;

            layer.Name = gxObj.BaseName;
            mxdoc.AddLayer(layer);
            mxdoc.ActiveView.Refresh();
            mxdoc.UpdateContents();

            textBox2.Text = shpPath2;
            tracts        = shpPath2;
        }
Exemplo n.º 2
0
        private void addFeature()
        {
            // 'make a dialog box object that will show shapefiles only
            IGxDialog       pGxDia = new GxDialog();
            IGxObjectFilter pGxObFil;

            //'the type of filter dictates what type of files can be chosen
            //'in this case it is shapefiles
            pGxObFil            = new GxFilterShapefiles();
            pGxDia.ObjectFilter = pGxObFil;


            //' make a gxEnum object to hold the files that the user selects from the dialog box
            IEnumGxObject gxEnum;

            if (!pGxDia.DoModalOpen(0, out gxEnum))
            {
                return;
            }

            IGxObject gxObj = gxEnum.Next();

            //        'Identify the workspace to access the file from
            //'in this case it will be a shapefile workspace factory since that is the type of file that will be chosen
            IWorkspaceFactory wksFact = new ShapefileWorkspaceFactory();
            IWorkspace        wks     = wksFact.OpenFromFile(gxObj.Parent.FullName, 0);

            //'create a new feature class object from the selected workspace
            //'set it to the name of the shapefile chosen in the dialog
            IFeatureWorkspace featWrk = (IFeatureWorkspace)wks;
            IFeatureClass     fClass  = featWrk.OpenFeatureClass(gxObj.Name);

            //'make a feature layer from teh featureclass object
            IFeatureLayer lyr = new FeatureLayer();

            lyr.FeatureClass = fClass;
            lyr.Name         = gxObj.Name;


            MapWorkspace.currentMap.Model.AddLayer(lyr);
            // imap.AddLayer(lyr);
        }
        public string GetOutputFileName(string aFileType, string anInitialDirectory = @"C:\")
        {
            // This would be done better with a custom type but this will do for the momment.
            IGxDialog myDialog = new GxDialogClass();
            myDialog.set_StartingLocation(anInitialDirectory);
            IGxObjectFilter myFilter;


            switch (aFileType)
            {
                case "Geodatabase FC":
                    myFilter = new GxFilterFGDBFeatureClasses();
                    break;
                case "Geodatabase Table":
                    myFilter = new GxFilterFGDBTables();
                    break;
                case "Shapefile":
                    myFilter = new GxFilterShapefiles();
                    break;
                case "DBASE file":
                    myFilter = new GxFilterdBASEFiles();
                    break;
                case "Text file":
                    myFilter = new GxFilterTextFiles();
                    break;
                default:
                    myFilter = new GxFilterDatasets();
                    break;
            }

            myDialog.ObjectFilter = myFilter;
            myDialog.Title = "Save Output As...";
            myDialog.ButtonCaption = "OK";

            string strOutFile = "None";
            if (myDialog.DoModalSave(thisApplication.hWnd))
            {
                strOutFile = myDialog.FinalLocation.FullName + @"\" + myDialog.Name;
                
            }
            myDialog = null;
            return strOutFile; // "None" if user pressed exit
            
        }