Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

Thanks in advance

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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
share|improve this answer

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.

share|improve this answer
    
When setting the IGxDialog filter to show only workspaces, you'll get an error when trying to cast the IGxObject as an IGxDataset. –  kenbuja May 7 '13 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. –  Conor May 7 '13 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 –  Seem May 8 '13 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.