Problem converting add-in to stand alone console application.

715
2
07-05-2012 01:16 PM
colintalbert
New Contributor
Hello all,
    I have an ArcCatalog add-in that I would like to convert to a console application but am stuck.  The add-in does some stuff to the selected dataset in the table of contents when a user clicks the button.  I would like the console application to do the exact same stuff to a dataset specified by a full path which will be passed into the application as a comand line parameter.

In my add-in I get the selected IGxObject easily with the following code:
        Dim pGxObject As IGxObject
        pGxObject = m_app.SelectedObject

        Dim pGxDataset As IGxDataset
        Try
            pGxDataset = pGxObject
        Catch ex As Exception
        End Try

In my console version of the application I don't know to create an m_app because I don't have the hook.
        m_app = CType(Hook, ESRI.ArcGIS.CatalogUI.IGxApplication)


Any tricks for creating an IGxDataset or IGxObject with only a path?
The only thing I can think of is using approt to open a new or existing ArcCatalog instance and generating it from there but I'd rather not do this if possible.
Thanks,
Colin
0 Kudos
2 Replies
WeifengHe
Esri Contributor
In a console application, you don't have the hook.  You need to have a workspace, and connect to the dataset use the workspace.  You probably won't have an GxDataset, instead you will use IDataset interface.

If I see more code of yours, I can tell what type of workspace or dataset to use.
0 Kudos
colintalbert
New Contributor
Thanks your suggestion is helpful.

I found this snipit that I think does what you describe:  http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Get_FeatureClass_From_Sh...

My problem is that I will not know the type of the feature class only the path to it.  It could be a shapefile, raster, table, personal gdb feature class, etc.
What I was doing in my add in was determing the type by looking at th pGXDataset.Dataset.Type and then handling each accordingly.



'Get what type of Object was selected (i.e.Coverage, shapefile, database, etc.)
        Dim intType As Integer
        intType = pGxDataset.Dataset.Type

        'Used for string manipulation

        strLocation = pGxDataset.DatasetName.WorkspaceName.PathName
        strName = pGxDataset.DatasetName.Name
        strCat = pGxDataset.DatasetName.Category
        lblLocation.Text = strLocation & "\" & strName

        'do a a check on the type of dataset.

        strCategory = pGxDataset.Dataset.Category

        'Handle shapefiles, personal geodatabase feature class, SDE feature class
        If intType = 5 Then
            'Specific to Personal GDB Feature Class
            If strCategory = "Personal Geodatabase Feature Class" Then
                pWorkspaceFactory = New AccessWorkspaceFactory
                pWorkspace = pWorkspaceFactory.OpenFromFile(strLocation, 0)
                pFeatureWorkspace = pWorkspace
                pFeatureClass = pFeatureWorkspace.OpenFeatureClass(strName)
                'Specific to Shapefile
            ElseIf strCategory = "File Geodatabase Feature Class" Then
                pWorkspaceFactory = New FileGDBWorkspaceFactory
                pWorkspace = pWorkspaceFactory.OpenFromFile(strLocation, 0)
                pFeatureWorkspace = pWorkspace
                pFeatureClass = pFeatureWorkspace.OpenFeatureClass(strName)
            ElseIf strCategory = "Shapefile Feature Class" Then
                pWorkspaceFactory = New ShapefileWorkspaceFactory
                pWorkspace = pWorkspaceFactory.OpenFromFile(strLocation, 0)
                pFeatureWorkspace = pWorkspace
                pFeatureClass = pFeatureWorkspace.OpenFeatureClass(strName)
                'Specific to SDE feature class
            ElseIf strCategory = "SDE Feature Class" Then
                pWorkspaceFactory = New SdeWorkspaceFactory
                pWorkspace = pWorkspaceFactory.OpenFromFile(strLocation, 0)
                pFeatureWorkspace = pWorkspace
                pFeatureClass = pFeatureWorkspace.OpenFeatureClass(strName)
                'Specific to Labels and Tic Feature Classes....will exit
            ElseIf strCategory = "Tic Feature Class" Or strCategory = "Label Feature Class" Then
                MsgBox("Data format not supported for Statistics")
                Exit Sub
                'Specific to Other Feature Classes
            ElseIf strCat = "Polygon Feature Class" Or strCat = "Arc Feature Class" Or strCat = "Point Feature Class" Or strCat = "Region Feature Class" Or strCat = "Route Feature Class" Or strCat = "Preliminary Region Feature Class" Then
                pDataset = pGxDataset.Dataset
                pFeatureClass = pGxDataset.Dataset
                pCoverageFeatureClass = pFeatureClass
                'String manipualtion used to get the name of the coverage and type.
                intLen = Len(pDataset.FullName.NameString)
                intCovName = InStr(pDataset.FullName.NameString, "Coverage =")
                intFeatureClassName = InStr(pDataset.FullName.NameString, "FeatureClass =")
                strCovName = Strings.Right(pDataset.FullName.NameString, intLen - (intCovName + 10))

                intColon = InStr(strCovName, ";")
                strCovName = Strings.Left(strCovName, intColon - 1)

                'String manipulation to get the suffix for the extension
                strFeatureClassName = Strings.Right(pDataset.FullName.NameString, intLen - (intFeatureClassName + 14))
                intFeatureColon = InStr(strFeatureClassName, ".")
                intOther = Len(strFeatureClassName)
                intEndColon = InStr(strFeatureClassName, ";")
                strFeatureClassName = Mid(strFeatureClassName, intFeatureColon + 1, (intOther - intFeatureColon) - 1)

                'Case to handle the extension to the feture class table
                Select Case strCat
                    Case "Node Feature Class"
                        strExtension = ".nat"
                    Case "Polygon Feature Class"
                        strExtension = ".pat"
                    Case "Arc Feature Class"
                        strExtension = ".aat"
                    Case "Point Feature Class"
                        strExtension = ".pat"
                    Case "Tic Feature Class"
                        strExtension = ".tic"
                    Case "Route Feature Class"
                        strExtension = ".rat" & strFeatureClassName
                    Case "Region Feature Class"
                        strExtension = ".pat" & strFeatureClassName
                    Case "Preliminary Region Feature Class"
                        strExtension = ".pat" & strFeatureClassName
                    Case "Section Feature Class"
                        strExtension = ".sec" & strFeatureClassName
                    Case "Annotation Feature Class"
                        strExtension = ".tat" & strFeatureClassName
                End Select
            Else 'unhandled type
                MsgBox("This tool is not set up to handle this data type:  " & strCat)

            End If
            pDataset = pGxDataset.Dataset
            pTable = pDataset
            pFields = getFields(pTable)

            'Handle Coverages and Feature DataSets
        ElseIf intType = 4 Then
            'Specific to Feature DataSets
            If strCat = "Personal Geodatabase Feature Dataset" Or strCategory = "SDE Feature Dataset" Then
                MsgBox("Statistics Sample is not supported for Feature DataSets." & vbCrLf & "Please select a Feature Class, such as polygon, rather than coverage")
                Exit Sub
            Else
                'Specific to Coverages
                MsgBox("Please select a Feature Class, such as polygon, rather than coverage")
                Exit Sub
            End If

            'Handle Databases and Tables
        ElseIf intType = 10 Then
            'Further test to determine if this is a table or .dbf
            If strCat = "Info Table" Then
                pWorkspaceFactory = New ArcInfoWorkspaceFactory
            ElseIf strCat = "File Geodatabase Table" Then
                pWorkspaceFactory = New FileGDBWorkspaceFactory
            ElseIf strCat = "Personal Geodatabase Table" Then
                pWorkspaceFactory = New AccessWorkspaceFactory
            Else
                pWorkspaceFactory = New ShapefileWorkspaceFactory
            End If

            pWorkspace = pWorkspaceFactory.OpenFromFile(strLocation, 0)
            pFeatureWorkspace = pWorkspace
            pTable = pFeatureWorkspace.OpenTable(strName)
            pFields = getFields(pTable)

            'Handle Rasters
        ElseIf intType = 12 Then
            pWorkspaceFactory = New RasterWorkspaceFactory
            pWorkspace = pWorkspaceFactory.OpenFromFile(strLocation, 0)
            pRasterWorkspace = pWorkspace
            pRasterDataSet = pRasterWorkspace.OpenRasterDataset(strName)
            pRasterBandCollection = pRasterDataSet

            'Count the number of Raster Bands
            intRasterBandCount = pRasterBandCollection.Count
            pRasterBand = pRasterBandCollection.Item(0)
            pTable = pRasterBand.AttributeTable
            pFields = getFields(pTable)

            'Handle the other types of data
        Else
        End If
0 Kudos