I am creating a tool that opens an IGxDialog
which it's objects are filtered to show only workspaces, my question is how can I convert IGxObject
to IWorkSpace
or get the selected workspace without taking the selected object name and opening it using an IWorkSpaceFactory
3 Answers
This code works for geodatabases, but I haven't figured out how to get a workspace for a shapefile using this method
Private Sub GetWorkspace()
Dim pEnumGxObj As ESRI.ArcGIS.Catalog.IEnumGxObject
Dim pFCFilter As New ESRI.ArcGIS.Catalog.GxFilterWorkspaces
Dim pGxDatabase As ESRI.ArcGIS.Catalog.IGxDatabase2
Dim pGxDialog As New ESRI.ArcGIS.CatalogUI.GxDialog
Dim pGxObject As ESRI.ArcGIS.Catalog.IGxObject
Dim pWorkspace As ESRI.ArcGIS.Geodatabase.IWorkspace
pGxDialog.ObjectFilter = pFCFilter
If Not pGxDialog.DoModalOpen(My.ArcMap.Application.hWnd, pEnumGxObj) Then Exit Sub
pGxObject = pEnumGxObj.Next
If pGxObject Is Nothing Then Exit Sub
Try
If TypeOf pGxObject Is ESRI.ArcGIS.Catalog.IGxDatabase2 Then
pGxDatabase = pGxObject
pWorkspace = pGxDatabase.Workspace
System.Windows.Forms.MessageBox.Show(pWorkspace.PathName)
Else
System.Windows.Forms.MessageBox.Show("This is likely a shapefile workspace")
End If
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.ToString, "GetWorkspace")
End Try
End Sub
Try this (WILL ONLY WORK WHEN IGXOBJECT IS A SHAPEFILE):
ESRI.ArcGIS.Catalog.IGxDataset gxDataset; //Declare IgxDataset
gxDataset = (IGxDataset)gxObj; //cast IgxObject to IgxDataset
ESRI.ArcGIS.Geodatabase.IDataset gDataset;//Declare IDataset
gDataset = gxDataset.Dataset; //IGxDataset to IDataset
ESRI.ArcGIS.Geodatabase.IWorkspace gxWorkspace; //Declare workspace
gxWorkspace = gDataset.Workspace; //IDataset to IWorkspace
The order is IGxObject > IGxDataset > IDataset > IWorkspace.
-
When setting the IGxDialog filter to show only workspaces, you'll get an error when trying to cast the IGxObject as an IGxDataset.– kenbujaCommented May 7, 2013 at 21:04
-
Yes. However, the above code will properly provide the workspace when IGxObject is a shapefile. I have edited my answer accordingly. Perhaps a combination of our two implementations may best suit the original poster.– ConorCommented May 7, 2013 at 23:13
-
I tried this thing before, and it gives me an InvalidCastException when trying to cast the IGxObject (which I've selected a workspace) to IGxDataset– SeemCommented May 8, 2013 at 6:14
This one works for me. It is tested for file geodatabase, personal geodatabase.
//enumGxObject = output of IGxDialogue
IWorkspace GetWorkspace(IEnumGxObject enumGxObject)
{
IGxObject gxObject;
while ((gxObject = enumGxObject.Next()) != null)
{
Catalog.IGxDatabase2 gxDatabase;
if ((gxDatabase = gxObject as IGxDatabase2) != null)
return gxDatabase.Workspace;
if(IsShapeFileWorkspace(gxObject))
return GetShapeFileWorkspace(gxObject);
}
return null;
}
Edit: added code will check shape file workspace and will return shape file workspace.
bool IsShapeFileWorkspace(IGxObject gxObject)
{
if((gxObject as GxShapefileDataset) == null)
return false;
return true;
}
IWorkspace GetShapeFileWorkspace(IGxObject gxObject)
{
var gxFolder = gxObject as Catalog.IGxFolder;
var iName = FileSystemWorkspaceNames.Next();
return iName.Open() as IWorkspace;
}
I hope it will work for u too.