Select to view content in your preferred language

Zoom to selected feature in Page layout

1364
7
Jump to solution
04-14-2013 01:55 PM
HushamHassan
Regular Contributor
Hi,
I have text box in standalone application windows form application(ImapDocument)  for parcel No when user click on Search the listview populated with the Parcel Info.
it work fine, I am using  ImapDocument.
I need to zoom to this parcel  in page layout and export to PDF or jpg when user click on Create Map/Print Button.  I can't figure out how to do that.

I am using ArcgisDesktop 10.1


here is my code so far.

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click         License = New esriLicenseObj         License.Initialize_ArcInfo()         Try                        'strPath = "C:\Users\owner\Desktop\gis Dev\parcel.mxd" ' MXD path             Dim pMapdoc as ImapDocument = New MapDocument                         pMapDoc.Open("C:\Users\owner\Desktop\gis Dev\parcel.mxd")              'pActiveView = pMapDocument.PageLayout             'pActiveView.Activate(hWnd)             Dim pFeatureLayer As IFeatureLayer = CType(pMapDoc.Layer(0, 0), IFeatureLayer)             Dim featureName As String = pFeatureLayer.Name             Dim pMap As IMap             ''Dim pLayer As ILayer             Dim pActiveView As IActiveView             pActiveView = pMapDoc.PageLayout             pActiveView.Activate(GetDesktopWindow()) '(hWnd)              'Dim pFeatureLayer As IFeatureLayer             Dim pFeatureSelection As IFeatureSelection             Dim pQueryFilter As IQueryFilter               pMap = pMapDoc.Map(0) '  this is the solution                pActiveView = pMap              pQueryFilter = New QueryFilter              ''First layer in the map             'If Not TypeOf pMap.Layer(0) Is IFeatureLayer Then Exit Sub             pFeatureLayer = pMap.Layer(0)             pFeatureSelection = pFeatureLayer 'QI              'Create the query filter             pQueryFilter = New QueryFilter             'pQueryFilter.SubFields = "PROP_ID"             pQueryFilter.WhereClause = "PROP_ID = '" & UCase(txtParcel.Text) & "'"              Dim pFClass As IFeatureClass = pFeatureLayer.FeatureClass              Dim pFCursor As IFeatureCursor = pFClass.Search(pQueryFilter, True)              'Invalidate only the selection cache             'Flag the original selection             pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)                 'Perform the selection             pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)             'Flag the new selection             pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)                      Dim pFeature As IFeature             pFeature = pFCursor.NextFeature              Dim nCols As Integer             nCols = pFeature.Fields.FieldCount             'define row             'Dim nRows As Integer             ''Me.Listsheet.ColumnHeaderCollection = nCols             'nRows = pFeature.Fields.FieldCount               For i = 2 To nCols - 1                 ' Me.lstResult.Items.Add(pFeature.Fields.Field(i).Name & "    " & pFeature.Value(i))                 Me.Listsheet.Items.Add(pFeature.Fields.Field(i).Name & "    " & pFeature.Value(i))                 'Me.tmpDT.DataSource(pFeature.Fields.Field(i).Name & "    " & pFeature.Value(i))                 'convertICursorToDataTable(tmpDT)             Next              ''Do not make any call to ArcObjects after ShutDownApplication()             ' Get the license back to the ArcInfo              ' License.ShutDown()             ''****************************************************************         Catch ex As Exception             MsgBox(ex.ToString)             Return         End Try         '******************************************************************************      End Sub

[ATTACH=CONFIG]23487[/ATTACH]
Thank you
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
You can clear the featureselection after zooming to the feature or, instead of using IFeatureSelection, use ISelectionSet to search for a particular parcel.

View solution in original post

0 Kudos
7 Replies
KenBuja
MVP Esteemed Contributor
Have you seen the Esri snippet "Create JPEG from ActiveView"?
0 Kudos
HushamHassan
Regular Contributor
Thank you  kenbuja
Yes I tried that before, it only export all the layout not the selected feature.  because I am using ImapDocument,

Regards
0 Kudos
HushamHassan
Regular Contributor
HI Buja
I am trying to some thing similar to your script zoom to select by polygon, but using Imapdocument so no need to lunch Arcmap.

Regards
0 Kudos
KenBuja
MVP Esteemed Contributor
What you'll need to do is the zoom to the extent of the feature you've selected. Try this after you've created the variable pFeature

pFeature = pFCursor.NextFeature
Dim pEnvelope as IEnvelope

If Not pFeature Is Nothing Then
    pEnvelope = pfeature.Extent
    ' back out a little from the extent of the feature
    ' comment out or remove next line if not wanted
    pEnvelope.Expand 0.5, 0.5, False
    pActiveView.Extent = pEnvelope
    pActiveView.Refresh
End If
0 Kudos
HushamHassan
Regular Contributor
Hi,  Your Code worked perfect,,  Thank you.
is there any way to remove the selection outline from the jpg (Blue outline)?

here is my code after Modification:

Public Class Form1
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    
    Property pMapDoc As IMapDocument

 Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Try
            'Dim strPath As String
            'Dim hWnd As Long
            'R21160
            'strPath = "C:\Users\owner\Desktop\gis Dev\FinalProject\parcel.mxd" ' MXD path
            'Dim pMapDoc As IMapDocument = New MapDocument
            pMapDoc = New MapDocument
            pMapDoc.Open("C:\Users\owner\Desktop\gis Dev\FinalProject\parcel.mxd")

            'pActiveView = pMapDocument.PageLayout
            'pActiveView.Activate(hWnd)
            Dim pFeatureLayer As IFeatureLayer = CType(pMapDoc.Layer(0, 0), IFeatureLayer)
            Dim featureName As String = pFeatureLayer.Name
            Dim pMap As IMap
            ''Dim pLayer As ILayer
            Dim pActiveView As IActiveView
            pActiveView = pMapDoc.PageLayout
            pActiveView.Activate(GetDesktopWindow()) '(hWnd)

            'Dim pFeatureLayer As IFeatureLayer
            Dim pFeatureSelection As IFeatureSelection
            Dim pQueryFilter As IQueryFilter

            pMap = pMapDoc.Map(0) '  this is the solution

            pActiveView = pMap
            pQueryFilter = New QueryFilter

            ''First layer in the map
            'If Not TypeOf pMap.Layer(0) Is IFeatureLayer Then Exit Sub
            pFeatureLayer = pMap.Layer(0)
            pFeatureSelection = pFeatureLayer 'QI

            'Create the query filter
            pQueryFilter = New QueryFilter
            'pQueryFilter.SubFields = "PROP_ID"
            pQueryFilter.WhereClause = "PROP_ID = '" & UCase(txtParcel.Text) & "'"
            Dim pFClass As IFeatureClass = pFeatureLayer.FeatureClass

            Dim pFCursor As IFeatureCursor = pFClass.Search(pQueryFilter, True)
            'Invalidate only the selection cache
            'Flag the original selection
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

            'Perform the selection
            pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)
            'Flag the new selection
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)
            Dim pFeature As IFeature
            ' pFeature = pFCursor.NextFeature
            pFeature = pFCursor.NextFeature
            Dim pEnvelope As IEnvelope

            If Not pFeature Is Nothing Then
                pEnvelope = pFeature.Extent
                ' back out a little from the extent of the feature
                ' comment out or remove next line if not wanted
                pEnvelope.Expand(0.5, 0.5, False)
                pActiveView.Extent = pEnvelope
                pActiveView.Refresh()
            End If
            Dim nCols As Integer
            nCols = pFeature.Fields.FieldCount

            For i = 2 To nCols - 1

                Me.Listsheet.Items.Add(pFeature.Fields.Field(i).Name & "    " & pFeature.Value(i))
              
            Next

            ' Get the license back to the ArcInfo 
            ' License.ShutDown()
            ''****************************************************************
          
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return
        End Try

       
    End Sub
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim pActiveview As IActiveView
        pActiveview = pMapDoc.PageLayout
        Dim screenResolution As System.Int32 = 96
        Dim outputResolution As System.Int32 = 300

        Dim export As IExport = New ExportJPEG
        export.Resolution = outputResolution
        export.ExportFileName = "C:\Users\owner\Desktop\gis Dev\FinalProject\parcel.jpg"

        Dim exportRect As tagRECT
        exportRect.left = 0
        exportRect.top = 0
        exportRect.right = 800 * (outputResolution \ screenResolution)
        exportRect.bottom = 800 * (outputResolution \ screenResolution)

        Dim envelope As IEnvelope = New EnvelopeClass()
        envelope.PutCoords(exportRect.left, exportRect.top, exportRect.right, exportRect.bottom)
        export.PixelBounds = envelope

        Dim hDC As Int32 = export.StartExporting()
        'Dim ActiveView As IActiveView = pMapDoc.ActiveView
        pActiveview.Output(hDC, CShort(export.Resolution), exportRect, Nothing, Nothing)

        export.FinishExporting()
        export.Cleanup()

    End Sub



Many Thanks
0 Kudos
KenBuja
MVP Esteemed Contributor
You can clear the featureselection after zooming to the feature or, instead of using IFeatureSelection, use ISelectionSet to search for a particular parcel.
0 Kudos
HushamHassan
Regular Contributor
Hi,
Yes  it work,  Thank you  I just cleared the selection after zooming,

pMap.ClearSelection()
0 Kudos