Convert GxDialog to Workspace

540
2
07-06-2017 01:23 PM
AbelPerez
Occasional Contributor

ArcGIS 10.1 with VB2010

I am using the GxDialog to get the users option to save a feature class. The target can be a shapefile, geodatabase, or Dataset inside a geodatabase.

I can’t wrap my head around how I am supposed to parse the Workspace, FeatureDataset, and FeatureClass name to use in creating the feature class.

 

So far I have

 

            'setup the save dialog

            Dim pGxDialog As IGxDialog = New GxDialog

            Dim pGxObjFilterSh As IGxObjectFilter = New GxFilterShapefiles          'shapefiles

            Dim pGxObjFilterPg As IGxObjectFilter = New GxFilterPGDBFeatureClasses  'personal geodb (Access db)

            'Dim pGxObjFilterFg As IGxObjectFilter = New GxFilterFGDBFeatureClasses  'file geodb (ESRI db)

            Dim pGxObjectFilterCollection As IGxObjectFilterCollection = CType(pGxDialog, IGxObjectFilterCollection)

            pGxObjectFilterCollection.AddFilter(pGxObjFilterSh, True)

            pGxObjectFilterCollection.AddFilter(pGxObjFilterPg, False) 'this one actually contains both file and personal geodb feature classes

            'pGxObjectFilterCollection.AddFilter(pGxObjFilterFg, False)

 

            'get the users selection (it can be a shapefile or a geodb). Catalog paths consist of two parts: the workspace and the base name.

            If pGxDialog.DoModalSave(MyApp.hWnd) Then

                Dim catalogPath As String = My.Computer.FileSystem.CombinePath(pGxDialog.FinalLocation.FullName, pGxDialog.Name)

                Dim workspaceCategory As String = pGxDialog.FinalLocation.Category

                Debug.Print(String.Format("catalogPath={0}, workspaceCategory={1}", catalogPath, workspaceCategory))

                Debug.Print("ClassID={0}", pGxDialog.FinalLocation.ClassID)

 

                'I can use the workspaceCategory to get the target type. But how do I split out either the entire string path or

                'the pGxDialog to create a Workspace object. How do I set up the objects to allow a Feature Dataset?

          Select Case workspaceCategory

              Case "Personal Geodatabase", "Personal Geodatabase Feature Dataset"

                  'personal geodb (Access database C:\myData\mypGDB.mdb)

                  wkspFactory = New AccessWorkspaceFactory

                  pCLSID.Value = "esriGeodatabase.Feature"

              Case "File Geodatabase", "File Geodatabase Feature Dataset"

                  'file geodb (File database C:\myData\myfGDB.gdb)

                  wkspFactory = New FileGDBWorkspaceFactory

                  pCLSID.Value = "esriGeodatabase.Feature"

              Case "Folder"

                  'shapefile (c:\mydata\)

                  wkspFactory = New ShapefileWorkspaceFactory

                  pCLSID.Value = "esriCore.Feature"

              Case Else

                  Throw New Exception("Unsupported workspace category: " & workspaceCategory)

          End Select

            End If

0 Kudos
2 Replies
KenBuja
MVP Honored Contributor

This is one way I did it, using several different functions and using the Geoprocessor to create the data set.

First, I use this line to call a function to open the GXDialog. The second argument is a specific type of feature class, either ESRI.ArcGIS.Catalog.GxFilterPolygonFeatureClasses, ESRI.ArcGIS.Catalog.GxFilterPolylineFeatureClasses or ESRI.ArcGIS.Catalog.GxFilterPointFeatureClasses. Then I get the directory path and name of the dataset.

SaveArray = BrowsetoCreateFeatureClass("Save overlapping polygons", New ESRI.ArcGIS.Catalog.GxFilterPolygonFeatureClasses)

If SaveArray Is Nothing Then Exit Sub

FCDirectoryLine = SaveArray(0)
FCNameLine = SaveArray(1)
‍‍‍‍‍‍
    Friend Function BrowsetoCreateFeatureClass(ByVal Title As String, ByVal pFilter As ESRI.ArcGIS.Catalog.IGxObjectFilter) As String()

        Dim pGxDialog As ESRI.ArcGIS.CatalogUI.IGxDialog = New ESRI.ArcGIS.CatalogUI.GxDialog

        Try
            With pGxDialog
                .Title = Title
                .Name = ""
                .ObjectFilter = pFilter
                If Not .DoModalSave(My.ArcMap.Application.hWnd) Then Return Nothing

                Return {.FinalLocation.FullName, .Name}

            End With

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace.ToString, "Browse to Create Featureclass")
            Return Nothing
        End Try

    End Function‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Then I use another function to create the feature class from that information (getting the spatial reference from another dataset, but you can set this on your own)

pPolygonFClass = CreateFeatureClass(FCDirectory, FCName, pGDataset.SpatialReference, "Polygon")
    Friend Function CreateFeatureClass(ByVal FCLocation As String, ByVal FCName As String, ByVal pSR As ESRI.ArcGIS.Geometry.ISpatialReference3, ByVal GeometryType As String, Optional ByVal AddLayer As Boolean = False) As ESRI.ArcGIS.Geodatabase.IFeatureClass

        Dim CreateFClass As New ESRI.ArcGIS.DataManagementTools.CreateFeatureclass
        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
            Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
                releaser.ManageLifetime(CreateFClass)

                CreateFClass.out_path = FCLocation
                CreateFClass.out_name = FCName
                CreateFClass.spatial_reference = pSR
                CreateFClass.geometry_type = GeometryType

                Result = RunTool(CreateFClass, Nothing, AddLayer)
                If Result Is Nothing Then Return Nothing

                Return ReturnObjectfromResult(Result, "Feature Class")

            End Using
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace.ToString, "Create Feature Class")
            Return Nothing
        End Try

    End Function

    Private Sub ReturnMessages(ByVal pResult As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Title As String)

        If pResult Is Nothing Then Exit Sub

        Dim ErrorMessage As String

        If pResult.MessageCount > 0 Then
            For Count As Integer = 0 To pResult.MessageCount - 1
                ErrorMessage += pResult.GetMessage(Count)
            Next
        End If

        System.Windows.Forms.MessageBox.Show(ErrorMessage, Title)

    End Sub

    Friend Function ReturnObjectfromResult(ByVal result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Type As String) As Object

        Dim GPVal As ESRI.ArcGIS.Geodatabase.IGPValue
        Dim InMemFC As String
        Dim GPUtil As ESRI.ArcGIS.Geoprocessing.IGPUtilities3 = New ESRI.ArcGIS.Geoprocessing.GPUtilities

        Try
            GPVal = result.GetOutput(0)
            InMemFC = GPVal.GetAsText()

            Select Case Type
                Case "Feature Class"
                    Return GPUtil.OpenFeatureClassFromString(InMemFC)
                Case "Table"
                    Return GPUtil.OpenTableFromString(InMemFC)
                Case "Feature Layer"
                    Return GPUtil.OpenFeatureLayerFromString(InMemFC)
                Case Else
                    Return Nothing
            End Select

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString, "ReturnObjectfromResult error")
            Return Nothing
        End Try

    End Function

    Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel2, Optional ByVal AddOutput As Boolean = False) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
            System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor
            Dim GP As New ESRI.ArcGIS.Geoprocessor.Geoprocessor

            GP.AddOutputsToMap = AddOutput
            GP.OverwriteOutput = True

            Result = CType(GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2)
            If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then ReturnMessages(Result, "Geoprocessing Error")
            GP.ClearMessages()
        Catch ex As Exception
            ReturnMessages(Result, "Fail")
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Run Geoprocessor")
        End Try

        Return Result

    End Function

If I need to determine what kind of Workspace that the dataset is saved in, I use this

pDataset = pPolygonFClass //ESRI.ArcGIS.Geodatabase.IDataset
var type as String = GetCategory(pDataset.Workspace)
    Public Function GetCategory(ByVal pWorkspace As ESRI.ArcGIS.Geodatabase.IWorkspace) As String

        Dim sClassID As String
        sClassID = pWorkspace.WorkspaceFactory.GetClassID.Value

        Select Case sClassID
            Case "{DD48C96A-D92A-11D1-AA81-00C04FA33A15}" ' pGDB
                GetCategory = "Personal Geodatabase"

            Case "{71FE75F0-EA0C-4406-873E-B7D53748AE7E}" ' fGDB
                GetCategory = "File Geodatabase"          '

            Case "{D9B4FA40-D6D9-11D1-AA81-00C04FA33A15}" ' GDB
                GetCategory = "SDE Database"

            Case "{A06ADB96-D95C-11D1-AA81-00C04FA33A15}" ' Shape
                GetCategory = "Shapefile Workspace"

            Case "{34DAE34F-DBE2-409C-8F85-DDBB46138011}" ' SDC
                GetCategory = "SDC Workspace"

            Case "{1D887452-D9F2-11D1-AA81-00C04FA33A15}" ' Coverage
                GetCategory = "ArcInfo Coverage Workspace"

            Case "{7F2BC55C-B902-43D0-A566-AA47EA9FDA2C}" ' InMemory
                GetCategory = "InMemory Workspace"

            Case "{59158055-3171-11D2-AA94-00C04FA37849}" 'OLEDB Workspace
                GetCategory = "OLEDB Workspace"

            Case "{30F6F271-852B-4EE8-BD2D-099F51D6B238}" 'Excel Workspace
                GetCategory = "Excel Workspace"

            Case Else
                GetCategory = "Unknown Workspace Category"
        End Select
    End Function
0 Kudos
AbelPerez
Occasional Contributor

Great info. Let me study the code and see how I can use it in my process.

0 Kudos