You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcObjects Visual Basic for Application (VBA) forum

Zoom to Layer   Hayden Lewis Feb 10, 2009
Re: Zoom to Layer   Asif Rahman Feb 10, 2009
Re: Zoom to Layer   Hayden Lewis Feb 10, 2009
Re: Zoom to Layer   Casey Ogden Feb 10, 2009
Re: Zoom to Layer   Hayden Lewis Feb 24, 2009
Re: Zoom to Layer   Ken Carrier Feb 26, 2009
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Zoom to Layer 
Author Hayden Lewis 
Date Feb 10, 2009 
Message I have an issues with the suggested methods of zooming to a layer. I want to zoom to a layer after a definition query has been run on it.
Using this code... "pActiveView.Extent = pLH.Layer(0).AreaOfInterest" zooms the activeview to the extent of the layer but not the data I defined in the query. Right clicking on the layer and selecting "Zoom to Layer" works but how can I achive that effect in VBA? 
 
Dim pMxDoc As IMxDocument
Dim pActiveView As IActiveView
Set pMxDoc = ThisDocument
Dim pMaps As IMaps
Set pMaps = pMxDoc.Maps
Dim pLH As IMap
Set pLH = pMaps.Item(0)
Dim pFLayer As ILayer
Set pFLayer = pLH.Layer(0)
Set pActiveView = pLH
Dim pDefine As IFeatureLayerDefinition
Set pDefine = pFLayer
If pLH.Layer(0) Is Nothing Then Exit Sub
  pActiveView.Extent = pLH.Layer(0).AreaOfInterest
  Dim pCommandItem As ICommandItem
  Set pCommandItem = Application.Document.CommandBars.Find(ArcId.PanZoom_ZoomOutFixed)
  
  pCommandItem.Execute
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Zoom to Layer 
Author Asif Rahman 
Date Feb 10, 2009 
Message Hope this help you. 
 
Public Sub ZoomToLayer()
  Dim pMxDoc As IMxDocument
  Dim pMap As IMap
  Dim pActiveView As IActiveView
  
  Set pMxDoc = Application.Document
  Set pMap = pMxDoc.FocusMap
  Set pActiveView = pMap
  
  If pMap.Layer(0) Is Nothing Then Exit Sub
  pActiveView.Extent = pMap.Layer(0).AreaOfInterest
  pActiveView.Refresh
End Sub
 
  Asif Rahman
afr.sdml@gmail.com 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Zoom to Layer 
Author Hayden Lewis 
Date Feb 10, 2009 
Message Thanks for your reply, This code does that same as what I had before... Zooms to the extents of the Layer rather than zooming to layers data. I have run a definition query on layer (0) and I want it to only zoom to the extent of those defined features not the extent of the layer but the extent of the defined data. Right clicking on the layer and selecting zoom to layer works correctly after running the definition query. Can this be captured with a command item? I have not been able find a way. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Zoom to Layer 
Author Casey Ogden 
Date Feb 10, 2009 
Message Hayden,

See this thread:

http://forums.esri.com/Thread.asp?c=93&f=992&t=57322

I had to come up with a select layer then zoom to def. query -- if you're interested in mine its below: 
 
Sub Zoom_Click()

    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    
    Dim pDTab As IDisplayTable
    Dim pContentsView As IContentsView
    Dim pLayer As ILayer
    Set pContentsView = pMxDoc.CurrentContentsView

    If Not TypeOf pContentsView.SelectedItem Is ILayer Then Exit Sub
    Set pDTab = pContentsView.SelectedItem
    
    Dim pFCur As IFeatureCursor
    Set pFCur = pDTab.SearchDisplayTable(Nothing, False)
    
    Dim pEnv As IEnvelope
    Dim pFeat As IFeature
    Set pFeat = pFCur.NextFeature
    Do While Not pFeat Is Nothing
        If pEnv Is Nothing Then
            Set pEnv = New Envelope
            Set pEnv = pFeat.Shape.Envelope
        Else
            pEnv.Union pFeat.Shape.Envelope
        End If
        Set pFeat = pFCur.NextFeature
    Loop
    If Not pEnv Is Nothing Then
        pMxDoc.ActivatedView.Extent = pEnv
        pMxDoc.ActivatedView.Refresh
    End If

End Sub
 
  Casey Ogden
DZSP | 21
Hagatna, Guam  
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Zoom to Layer 
Author Hayden Lewis 
Date Feb 24, 2009 
Message Thanks for your response, it was what I was after but i could not get it to do what I expected. It would make the Layout view "Blank". I actually figured out a another round about way to do this was to do a selection query after the defined query where it selects all the features. Then run a arcID zoom to selected then a ArcID clear selected features. 
 
Dim pMap As IMap
  Dim pActiveViews As IActiveView
  Dim pFeatureLayer As IFeatureLayer
  Dim pFeatureSelection As IFeatureSelection
  Dim pQueryFilter As IQueryFilter
  
  Set pMxDoc = Application.Document
  Set pMap = pMxDoc.FocusMap
  Set pActiveViews = pMap
  
  in the map
  If Not TypeOf pMap.Layer(0) Is IFeatureLayer Then Exit Sub
  Set pFeatureLayer = pMap.Layer(0)
  Set pFeatureSelection = pFeatureLayer 'QI
  
    Set pQueryFilter = New QueryFilter
  pQueryFilter.WhereClause = "IPM_NUMBER > '0'"
  
  
  pActiveViews.PartialRefresh esriViewGeoSelection, Nothing, Nothing
    pFeatureSelection.SelectFeatures pQueryFilter, esriSelectionResultNew, False
  
  pActiveViews.PartialRefresh esriViewGeoSelection, Nothing, Nothing
   
  Dim pCommandItem As ICommandItem
  Set pCommandItem = Application.Document.CommandBars.Find(ArcId.Query_ZoomToSelected)
  pCommandItem.Execute
  
  Dim pCommandItems As ICommandItem
  Set pCommandItems = Application.Document.CommandBars.Find(ArcId.Query_ClearSelection)
  
  pCommandItems.Execute
  
pMxDoc.ActiveView.Refresh
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Zoom to Layer 
Author Ken Carrier 
Date Feb 26, 2009 
Message Here is one way;



Sub ZoomToExtent()
Dim mxDoc As IMxDocument
Set mxDoc = ThisDocument
Dim featureLayer As IFeatureLayer
Set featureLayer = mxDoc.FocusMap.Layer(1)
Dim geoDataset As IGeoDataset
Set geoDataset = featureLayer
Dim activeView As IActiveView
Set activeView = mxDoc.FocusMap
Dim geoFeatureLayer As IGeoFeatureLayer
Set geoFeatureLayer = featureLayer
Dim enumGeometryBind As IEnumGeometryBind
Set enumGeometryBind = New EnumFeatureGeometry
enumGeometryBind.BindGeometrySource Nothing, geoFeatureLayer.DisplayFeatureClass
Dim geoFactory As IGeometryFactory
Set geoFactory = New GeometryEnvironment
Dim geometry As IGeometry
Set geometry = geoFactory.CreateGeometryFromEnumerator(enumGeometryBind)
activeView.Extent = geometry.envelope
activeView.Refresh
End Sub

Here is another way that looks similiar to yours;


Sub ZoomToDef()
Dim pDoc As IMxDocument
Dim pMap As IMap
Dim pActiveView As IActiveView
Dim pLayer As IGeoFeatureLayer
Dim pFeatLayerDef As IFeatureLayerDefinition
Dim sExp As String
Dim pFeatCursor As IFeatureCursor
Dim pEnv As IEnvelope
Dim pFeature As IFeature

Set pDoc = Application.Document
Set pMap = pDoc.FocusMap
Set pActiveView = pMap

Set pLayer = pMap.Layer(0)

' Set the definition expression for the layer
Set pFeatLayerDef = pLayer
' Change this expression string to match your data
sExp = "AREA < 1.4"
pFeatLayerDef.DefinitionExpression = sExp

' SearchDisplayFeatures searches only those features that
' meet the definition expression; passing Nothing for the
' query filter will return all features in the definition
Set pFeatCursor = pLayer.SearchDisplayFeatures(Nothing, False)
Set pEnv = New Envelope

' Loop through all the features and union their extents
Set pFeature = pFeatCursor.NextFeature
pEnv.Union pFeature.Extent
Do Until pFeature Is Nothing
Set pFeature = pFeatCursor.NextFeature
If Not pFeature Is Nothing Then
pEnv.Union pFeature.Extent
End If
Loop

' Zoom to new extent
pActiveView.Extent = pEnv
pActiveView.Refresh

End Sub

Hope this helps, I have used your method in the past but noticed that when using the select method and zoom to selected, the selection can slow down the process, either one of these methods should prove to be more efficient, at least they were for me. Good luck!
 
  Ken Carrier
GIS Specialist
Montgomery County Ohio Water Services