Sunday, 5 March 2017

arcgis 10.0 - How to select and add a featureclass through GXDialogue


I am new to arcobject i have learned how to add a feature class by specifying its name in code through gxdialogue.



i want to change it so i could directly select the the featureclass instead mentioning its name in coding (i.e. want to remove hardcoding).


can you please suggest what changes i need to do in code.


below is the code:


  IGxDialog gxd = new GxDialogClass();
gxd.AllowMultiSelect = false;
gxd.ButtonCaption = "Add featureclass";
gxd.Title = "Add FeatureClasses";
gxd.RememberLocation = true;
IGxObjectFilter gxObjFilter = new GxFilterPersonalGeodatabases();




gxd.ObjectFilter = gxObjFilter;
IEnumGxObject gxEnumObj;
gxd.DoModalOpen(ArcMap.Application.hWnd, out gxEnumObj);
IGxObject gxObj = gxEnumObj.Next();
//getting the address of PGDB
string WSAddress = gxObj.FullName;



IMxDocument mxdoc = ArcMap.Application.Document as IMxDocument;

IWorkspaceFactory wsf = new AccessWorkspaceFactoryClass();

IWorkspace ws = wsf.OpenFromFile(WSAddress, arcMap.Application.hWnd);



IFeatureWorkspace fws = ws as IFeatureWorkspace;
IFeatureClass fc = fws.OpenFeatureClass("FetureClassName");


IFeatureLayer fl = new FeatureLayerClass();
fl.Name = "Feature";
fl.FeatureClass = fc;

mxdoc.AddLayer(fl);
mxdoc.ActiveView.Refresh();
mxdoc.UpdateContents();

Answer



I would do something like this for feature classes:



private IGxDialog getDialog(string title, IGxObjectFilter filter)
{
IGxDialog pGxDialog = new GxDialog();
IGxObjectFilter pGxObjFilter = filter;
pGxDialog.AllowMultiSelect = true;
pGxDialog.ObjectFilter = pGxObjFilter;
pGxDialog.Title = title;
pGxDialog.ButtonCaption = "OK";

return pGxDialog;

}

private void getFeatureClass(string title)
{
IEnumGxObject pEnumGxObj = null;
IGxDialog dialog = getDialog(title, new GxFilterFeatureClasses());

if (dialog.DoModalOpen(0, out pEnumGxObj))
{
IGxObject pGxObj = pEnumGxObj.Next();


if(pGxObj != null)
{
IGxDataset gxDataset = pGxObj as IGxDataset;
IName name = gxDataset.Dataset.FullName;

//open your feature class here or later
IFeatureClass fc = name.Open() as IFeatureClass;
}
}

}

The biggest thing is to turn the IGxObject into an IGxDataset. You can get an idea of all the different objects a IGxObject can represent and when to use them here.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...