0

I will selected one feature in ArcMap that feature zoom in viewer window in ArcMap i tried in ArcMap VBA but no result

Public Sub zoomtool()

Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMap As IMap
Set pMap = pDoc.FocusMap

-----------------------------------------------Here how to call viewer window

enter image description here

Set act = pMap

Dim pLayer As IFeatureLayer
Dim pFSel As IFeatureSelection
For i = 0 To pMap.LayerCount - 1

Set pLayer = pMap.Layer(i)
Set pFSel = pLayer

'Get the selected features
Dim pSelSet As ISelectionSet
Set pSelSet = pFSel.SelectionSet

Dim pEnumGeom As IEnumGeometry
Dim pEnumGeomBind As IEnumGeometryBind

Set pEnumGeom = New EnumFeatureGeometry
Set pEnumGeomBind = pEnumGeom
pEnumGeomBind.BindGeometrySource Nothing, pSelSet

Dim pGeomFactory As IGeometryFactory
Set pGeomFactory = New GeometryEnvironment

Dim pGeom As IGeometry
Set pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom)

pDoc.ActiveView.Extent = pGeom.Envelope
pDoc.ActiveView.Refresh
Next i

End Sub 
1

The code you have provided zooms the main view to the selected features, but also continues even trying to zoom layers with no selection.. first test for selection using ISelectionSet.Count before trying to zoom; After zooming to the selected feature you need to exit the loop so you don't go trying to zoom to every layer - use Exit For.

Public Sub zoomtool()

    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pDoc.FocusMap
    Set act = pMap

    Dim pLayer As IFeatureLayer
    Dim pFSel As IFeatureSelection
    For i = 0 To pMap.LayerCount - 1

        Set pLayer = pMap.Layer(i)
        Set pFSel = pLayer

        'Get the selected features
        Dim pSelSet As ISelectionSet
        Set pSelSet = pFSel.SelectionSet
        ' check there is anything selected for this layer
        If pSelSet.Count > 0 then

            Dim pEnumGeom As IEnumGeometry
            Dim pEnumGeomBind As IEnumGeometryBind

            Set pEnumGeom = New EnumFeatureGeometry
            Set pEnumGeomBind = pEnumGeom
            pEnumGeomBind.BindGeometrySource Nothing, pSelSet

            Dim pGeomFactory As IGeometryFactory
            Set pGeomFactory = New GeometryEnvironment

            Dim pGeom As IGeometry
            Set pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom)

            pDoc.ActiveView.Extent = pGeom.Envelope
            pDoc.ActiveView.Refresh
            Exit For ' escape out now that the zoom is refreshed
        End If
    Next i

End Sub 

However you were talking about a viewer window, these are a little more complex. You can access the windows of the application using IApplicationWindows.DataWindows (IApplication implements IApplicationWindows), this returns all sorts of windows: Overview, magnifier, table views... Use the TypeOf() to decide if you've found the window you're after (TypeOf(window) = IMapInsetWindow), after you've got that far you need to set the VisibleBounds of the MapInset:

Dim pLayer As IFeatureLayer
Dim pFSel As IFeatureSelection
Dim pAppWind As IApplicationWindows
Dim pDataWind As IDataWindow
Dim pDataWindows As ISet
pAppWind = pApplication
pDataWindows = pAppWind.DataWindows
pDataWindows.Reset()
pDataWind = pDataWindows.Next()

' Loop through windows until you find the right one(s)
Do Until pDataWind Is Nothing
    If TypeOf (pDataWind) Is IMapInsetWindow Then
        '' this is the one! it's a Map Inset Window
        Dim pInsetWindow As IMapInsetWindow = pDataWind

        For i = 0 To pMap.LayerCount - 1
            pLayer = pMap.Layer(i)
            pFSel = pLayer
            Dim pSelSet As ISelectionSet
            pSelSet = pFSel.SelectionSet
            If pSelSet.Count > 0 Then
                Dim pEnumGeom As IEnumGeometry
                Dim pEnumGeomBind As IEnumGeometryBind

                pEnumGeom = New EnumFeatureGeometry
                pEnumGeomBind = pEnumGeom
                pEnumGeomBind.BindGeometrySource(Nothing, pSelSet)

                Dim pGeomFactory As IGeometryFactory
                pGeomFactory = New GeometryEnvironment

                Dim pGeom As IGeometry
                pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom)
                Dim pEnv As IEnvelope
                pEnv = pGeom.Envelope
                pEnv.SpatialReference = pMap.SpatialReference
                pEnv.Expand(1.1, 1.1, True) ' expand by 10%

                pInsetWindow.MapInset.VisibleBounds = pEnv

                pInsetWindow.Refresh()

                Exit For ' escape out now that the zoom is refreshed
            End If
        Next
    End If
    pDataWind = pDataWindows.Next()
Loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.