Select to view content in your preferred language

Define Projection using VB.NET in ArcCatalog

748
1
01-18-2011 05:12 AM
RuchiraWelikala
Regular Contributor
Hi all,
I'm trying to automate a process that I constantly do manually in ArcCatalog.  I export shapefiles from CAD and define their projection to NAD83 Zone 17 using ArcCatalog. 
I want to press a command button in ArcCatalog (in the toolbar) and have the selected shapefile be defined to NAD 83 Zone 17.

Here's the code I have so far, but I can't seem to find away to cast the IGxObject to a feature layer to define it's projection..would I even have to cast it?

   
Private Sub ProjectShapefile()
        Try
            Dim pGApp As IGxApplication = m_application
            Dim pGxObj As IGxObject = pGApp.SelectedObject
            Dim pGxDataset As IGxDataset
            If (TypeOf (pGxObj) Is GxShapefileDataset) = False Then
                'If pGxObj.Category <> "Shapefile" Then

                MsgBox("Make a valid Selection")
                Exit Sub

                'End If
            End If

            Dim pSpatRefFact As ISpatialReferenceFactory
            Dim pPCS As IProjectedCoordinateSystem
            Dim pSpatialRef As ISpatialReference
            pSpatRefFact = New SpatialReferenceEnvironment
            pPCS = pSpatRefFact.CreateProjectedCoordinateSystem(esriSRProjCSType.esriSRProjCS_NAD1983UTM_17N)

            Dim gxShape As GxShapefileDataset = New GxShapefileDataset
            gxShape = pGxObj
            pGxDataset = pGxObj

            Dim pGeoDataset As IGeoDataset = pGxDataset

            Dim pGeoDatasetEdit As IGeoDatasetSchemaEdit = gxShape

            If pGeoDatasetEdit.CanAlterSpatialReference = True Then
                ' Alter the target layer's spatial reference
                pGeoDatasetEdit.AlterSpatialReference(pPCS)
            Else
                Exit Sub
            End If

            ' Get the spatial reference information and export it to a prj file 

            pSpatialRef = pGeoDataset.SpatialReference

            pGApp.Refresh(pGxObj.Parent.FullName)

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

    End Sub
0 Kudos
1 Reply
RuchiraWelikala
Regular Contributor
I got it to work using the following code:

    Private Sub ProjShapefile()
        Dim pGxApp As IGxApplication = m_application
        Dim pGxObj As IGxObject = pGxApp.SelectedObject

        If (TypeOf (pGxObj) Is GxShapefileDataset) = False Then
            MsgBox("Make a valid Selection")
            Exit Sub
        End If

        Dim pGxDataset As IGxDataset = pGxObj
        Dim pDataset As IDataset = pGxDataset.Dataset

        Dim pGeoDataset As IGeoDataset = pDataset

        Dim pSpatRefFact As ISpatialReferenceFactory
        Dim pPCS As IProjectedCoordinateSystem
        Dim pSpatialRef As ISpatialReference
        pSpatRefFact = New SpatialReferenceEnvironment
        pPCS = pSpatRefFact.CreateProjectedCoordinateSystem(esriSRProjCSType.esriSRProjCS_NAD1983UTM_17N)

        Dim pGeoDatasetEdit As IGeoDatasetSchemaEdit = pGeoDataset

        If pGeoDatasetEdit.CanAlterSpatialReference = True Then
            pGeoDatasetEdit.AlterSpatialReference(pPCS)

        Else
            MsgBox("error")
            Exit Sub

        End If

        pGxApp.Refresh(pGxObj.Parent.FullName)

    End Sub
0 Kudos