Select to view content in your preferred language

How to write a 'zoom to next feature' addin?

2319
3
02-01-2012 12:45 AM
CatherineGladstone
New Contributor
I am trying to write an addin for Arc 10 that will zoom to the next feature in a layer each time I click a button. I have found a VBA sample that does this (http://arcscripts.esri.com/details.asp?dbid=13938) and modified it to work in an addin.  However, this approach has failed as the cursor it uses to loop through the features does not seem to be retained with each button click now that the tool is outside of the ArcMap application - so each time i click the button i get zoomed to the first feature in the table.

Is it the case that if i use a cursor in an add-in it will not be retained when I next click the same button?  If so, what can I do instead of a cursor to maintain my position in the feature table when I next click the button to zoom to next feature?
0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor
Please post your code so we can see how you've declared and instantiated the cursor.
0 Kudos
CatherineGladstone
New Contributor
Hi, here is the code. As I said, this runs fine but does not retain the cursor or the 'layer no' i specify when it is run again.

#Region "Zoom through features in nominated Layer"

    '''<summary>Zooms to the selected layer in the TOC associated with the active view.</summary>
    '''
    '''<param name="mxDocument">An IMxDocument interface</param>
    ''' 
    '''<remarks></remarks>
    Public Sub ZoomThruLayer(ByVal mxDocument As IMxDocument)

        If mxDocument Is Nothing Then
            Return
        End If

        ' Get the map
        Dim activeView As IActiveView = mxDocument.ActiveView


        ' ask the user for the layer's number in the Table Of Contents
        Dim LayerNo As String
        If LayerNo = "" Then
            LayerNo = InputBox("Enter the layer number in the TOC for the layer to process." _
               & vbNewLine & "Remember to start counting at 0.")
        End If


        ' Select the required layer to cycle thru features of (post-proc putput layer)
        Dim pFeatureLayer As IFeatureLayer
        pFeatureLayer = mxDocument.FocusMap.Layer(CInt(LayerNo))


        ' is this the first time through?
        Dim m_pFeatCur As IFeatureCursor
        Dim testVal As String
        If m_pFeatCur Is Nothing Then
            m_pFeatCur = pFeatureLayer.Search(Nothing, False)

        Else
            'Use messages to find cause of error - not working though!
            MessageBox.Show("Cursor WAS found!")

        End If


        ' get the next feature
        Dim pFeat As IFeature
        pFeat = m_pFeatCur.NextFeature
        If pFeat Is Nothing Then
            m_pFeatCur = Nothing
            Exit Sub
        End If

        ' select the next feature
        Dim pFeatureSelection As IFeatureSelection
        pFeatureSelection = pFeatureLayer
        With pFeatureSelection
            .Clear()
            .Add(pFeat)
        End With

        ' Zoom to the feature
        Dim pTopoOp As ITopologicalOperator
        pTopoOp = pFeat.ShapeCopy
        activeView.Extent = pTopoOp.Buffer(25).Envelope

        ' set map scale
        '-------------------------------------------------------------
        'Uncomment the line below and change the scale to whatever you want
        'if you want to see the selected features at a set scale each time.

        ' mxDocument.FocusMap.MapScale = 1000
        '-------------------------------------------------------------

        ' Refresh the map
        activeView.Refresh()

    End Sub
#End Region
0 Kudos
KenBuja
MVP Esteemed Contributor
Your problem is here

' is this the first time through?
        Dim m_pFeatCur As IFeatureCursor
        Dim testVal As String
        If m_pFeatCur Is Nothing Then
                 m_pFeatCur = pFeatureLayer.Search(Nothing, False)
Else
            'Use messages to find cause of error - not working though!
                 MessageBox.Show("Cursor WAS found!")
End If


Every time you click the button you're declaring the cursor, so it will be evaluated as Nothing. You have to declare m_pFeatCur outside of this sub. Take a look at the original script again and look at where it was declared.
0 Kudos