This interface is a starting point for much of the other objects in ArcMap. For example, this interface provides access to the current active view, the currently selected map, all of the maps, and the style gallery. This interface also has many properties reflected in the running application including: the text font, the text size, and the search tolerance. Almost every ArcMap customization uses IMxDocument one way or another.
Obtain a reference to this interface via IApplication::Document.
To edit the contents of a map document saved to a file (*.mxd) outside of an ArcMap session or without instantiating a new Application (ArcMap process), see IMapDocument. IMapDocument provides the ability to edit and save the contents of a map document.
[Visual Basic 6.0]
In ArcMap's VBA environment, the MxDocument object is exposed globally through ThisDocument (which supports IDocument). From this object you can obtain an IMxDocument reference. You can also obtain a reference to the same object through the Application object (which supports IApplication). The following VBA code shows both techniques. Dim pMxDocument As IMxDocument
Set pMxDocument = Application.Document
' or
Set pMxDocument = ThisDocument
Showing posts with label ArcObject. Show all posts
Showing posts with label ArcObject. Show all posts
Monday, June 9, 2008
Thursday, June 5, 2008
IScreenDisplay Example with Description
Looking at ScreenDisplay
Both the PageLayout and the Map have an associated ScreenDisplay
object that is used for all graphic rendering. ScreenDisplay maintains a
DisplayTransformation. You can access the properties of DisplayTransformation
such as VisibleBounds and use the methods to convert coordinates
and distances between map and device units. This is illustrated
by the diagram in Figure 4.
Figure 5
Private Sub Hello()
Dim mxDoc As IMxDocument
Set mxDoc = Application.Document
Dim activeView As IActiveView
Set activeView = mxDoc.activeView
‘ set up a text symbol to draw with
Dim sym As ITextSymbol
Set sym = New TextSymbol
‘ change the font size to 18
Dim fnt As IFontDisp
Set fnt = sym.Font
fnt.Size = 18
sym.Font = fnt
‘ draw into the active view’s display
With activeView.ScreenDisplay
.StartDrawing .hDC, esriNoScreenCache
.SetSymbol sym
Dim bnds As IArea
Set bnds =
.DisplayTransformation.VisibleBounds
.DrawText bnds.Centroid, “Hello”
.FinishDrawing
End With
End Sub
Events such as adding a new layer to the map will cause the ScreenDisplay
to trigger a redraw. The drawing process occurs in three phases.
The drawing order is the geography, the selection, and the annotation.
A drawing cache is an off-screen bitmap of the rendered map that is
stamped onto the screen whenever the window is painted. ScreenDisplay
manages several drawing caches. When you initiate drawing to a
ScreenDisplay you must specify the destination cache. If the constant
esriNoScreenCache is specifi ed, the graphics will draw directly on the
ScreenDisplay’s window and will not be cached. If another window
obscures the display, the graphics will be lost. The following script
draws the text “Hello” in the center of the ScreenDisplay using the esri-
NoScreenCache constant.
In the example in Figure 5, note that the drawing calls SetSymbol and
DrawText are bracketed within the StartDrawing and FinishDrawing
methods.
Working with Layers
Maps maintain a collection of layers. A layer is any object that implements
the ILayer interface. Examples of layer objects include Feature-
Layer, GroupLayer, GraphicsLayer, AnnotationLayer, CadLayer, Tin-
Layer, and RasterLayer. The Layer(index) and LayerCount properties
of the IMap interface can be used to navigate the layer collection. The
function illustrated in Figure 6 fi nds and returns the layer with the specifi
ed name.
Figure 6
Function FindLayer(map As IMap, name As String)
As ILayer
Dim i As Integer
For i = 0 To map.LayerCount - 1
If map.Layer(i).name = name Then
Set FindLayer = map.Layer(i)
Exit Function
End If
Next
End Function
Figure 7
Sub AddLayer()
‘ use a workspaceFactory to open the workspace
Dim wksFact As IWorkspaceFactory
Set wksFact = New Shapefi leWorkspaceFactory
Dim wks As IFeatureWorkspace
Set wks = wksFact.OpenFromFile(“c:\Data\shp”, 0)
‘ open the featureClass
Dim fc As IFeatureClass
Set fc = wks.OpenFeatureClass(“BigCypress”)
‘ create a featureLayer
Dim lyr As IFeatureLayer
Set lyr = New FeatureLayer
Set lyr.FeatureClass = fc
‘ name the layer with the featureClass name
Dim ds As IDataset
Set ds = fc
lyr.Name = ds.Name
‘ add the layer to the map
Dim mxDoc As IMxDocument
Set mxDoc = Application.Document
Dim map As IMap
Set map = mxDoc.FocusMap
map.AddLayer lyr
End Sub
A FeatureLayer manages a connection to a vector data source such as
a coverage, shapefi le, or Spatial Database Engine (SDE) layer through
a FeatureClass object. The script in Figure 7 obtains a FeatureClass
object for a shapefi le by opening the Workspace that contains the shapefi
le using Shapefi leWorkspaceFactory. It then creates a FeatureLayer,
attaches the FeatureClass to the layer, and adds it to the FocusMap.
Both the PageLayout and the Map have an associated ScreenDisplay
object that is used for all graphic rendering. ScreenDisplay maintains a
DisplayTransformation. You can access the properties of DisplayTransformation
such as VisibleBounds and use the methods to convert coordinates
and distances between map and device units. This is illustrated
by the diagram in Figure 4.
Figure 5
Private Sub Hello()
Dim mxDoc As IMxDocument
Set mxDoc = Application.Document
Dim activeView As IActiveView
Set activeView = mxDoc.activeView
‘ set up a text symbol to draw with
Dim sym As ITextSymbol
Set sym = New TextSymbol
‘ change the font size to 18
Dim fnt As IFontDisp
Set fnt = sym.Font
fnt.Size = 18
sym.Font = fnt
‘ draw into the active view’s display
With activeView.ScreenDisplay
.StartDrawing .hDC, esriNoScreenCache
.SetSymbol sym
Dim bnds As IArea
Set bnds =
.DisplayTransformation.VisibleBounds
.DrawText bnds.Centroid, “Hello”
.FinishDrawing
End With
End Sub
Events such as adding a new layer to the map will cause the ScreenDisplay
to trigger a redraw. The drawing process occurs in three phases.
The drawing order is the geography, the selection, and the annotation.
A drawing cache is an off-screen bitmap of the rendered map that is
stamped onto the screen whenever the window is painted. ScreenDisplay
manages several drawing caches. When you initiate drawing to a
ScreenDisplay you must specify the destination cache. If the constant
esriNoScreenCache is specifi ed, the graphics will draw directly on the
ScreenDisplay’s window and will not be cached. If another window
obscures the display, the graphics will be lost. The following script
draws the text “Hello” in the center of the ScreenDisplay using the esri-
NoScreenCache constant.
In the example in Figure 5, note that the drawing calls SetSymbol and
DrawText are bracketed within the StartDrawing and FinishDrawing
methods.
Working with Layers
Maps maintain a collection of layers. A layer is any object that implements
the ILayer interface. Examples of layer objects include Feature-
Layer, GroupLayer, GraphicsLayer, AnnotationLayer, CadLayer, Tin-
Layer, and RasterLayer. The Layer(index) and LayerCount properties
of the IMap interface can be used to navigate the layer collection. The
function illustrated in Figure 6 fi nds and returns the layer with the specifi
ed name.
Figure 6
Function FindLayer(map As IMap, name As String)
As ILayer
Dim i As Integer
For i = 0 To map.LayerCount - 1
If map.Layer(i).name = name Then
Set FindLayer = map.Layer(i)
Exit Function
End If
Next
End Function
Figure 7
Sub AddLayer()
‘ use a workspaceFactory to open the workspace
Dim wksFact As IWorkspaceFactory
Set wksFact = New Shapefi leWorkspaceFactory
Dim wks As IFeatureWorkspace
Set wks = wksFact.OpenFromFile(“c:\Data\shp”, 0)
‘ open the featureClass
Dim fc As IFeatureClass
Set fc = wks.OpenFeatureClass(“BigCypress”)
‘ create a featureLayer
Dim lyr As IFeatureLayer
Set lyr = New FeatureLayer
Set lyr.FeatureClass = fc
‘ name the layer with the featureClass name
Dim ds As IDataset
Set ds = fc
lyr.Name = ds.Name
‘ add the layer to the map
Dim mxDoc As IMxDocument
Set mxDoc = Application.Document
Dim map As IMap
Set map = mxDoc.FocusMap
map.AddLayer lyr
End Sub
A FeatureLayer manages a connection to a vector data source such as
a coverage, shapefi le, or Spatial Database Engine (SDE) layer through
a FeatureClass object. The script in Figure 7 obtains a FeatureClass
object for a shapefi le by opening the Workspace that contains the shapefi
le using Shapefi leWorkspaceFactory. It then creates a FeatureLayer,
attaches the FeatureClass to the layer, and adds it to the FocusMap.
Tuesday, June 3, 2008
IQueryFilter Example with Description
IQueryFilter filters data based on an attribute query. A string defining a where clause is required. An optional list of columns may be included to specify the column values to be retrieved. If no columns are specified, all values will be returned.
When To Use
When you need to filter data based on attribute values or the relationships between attributes.
Let's do it by one example
Dim pQueryFilter As IQueryFilter
Set pQueryFilter = New QueryFilter
Setting the SubFields and WhereClause: pQueryFilter.SubFields = "STATE_NAME,POPULATION"
pQueryFilter.WhereClause = "STATE_NAME = 'California'"
Using a query filter to search a feature class:Dim pFeatureCursor As IFeatureCursor
Set pFeatureCursor = pFeatureClass.Search(pQueryFilter, False)
When To Use
When you need to filter data based on attribute values or the relationships between attributes.
Let's do it by one example
Dim pQueryFilter As IQueryFilter
Set pQueryFilter = New QueryFilter
Setting the SubFields and WhereClause: pQueryFilter.SubFields = "STATE_NAME,POPULATION"
pQueryFilter.WhereClause = "STATE_NAME = 'California'"
Using a query filter to search a feature class:Dim pFeatureCursor As IFeatureCursor
Set pFeatureCursor = pFeatureClass.Search(pQueryFilter, False)
Sunday, June 1, 2008
ArcObject Defintion

Are You new in ArcObject or You are hearing first time this world, then read this it will help you a lot about understanding Arcobject.
ArcObjects is the collection of COM components that provides
the underpinning for two new ArcInfo 8 applications—ArcMap and
ArcCatalog. This set of components includes more than 1,200 objects
that may be used to customize, extend, or construct GIS applications.
Initially, an object model of this size can seem a little overwhelming.
Taking a close look at the object model for ArcMap will familiarize you
with some of the fundamental components in the larger object model.
The Basic Structure
Figure 1 shows a conceptual object model for ArcMap. The top-level
object, MxApplication, represents ArcMap itself. MxApplication manages
a single document, MxDocument. ArcMap uses a Single Document
Interface (SDI) in contrast to ArcView GIS, which uses a Multiple
Document Interface (MDI). MxDocument manages a collection of Map
objects and a PageLayout object. When ArcMap is in geographic view,
the ActiveView is a Map. When it’s in layout view, the ActiveView is
the PageLayout. Regardless of the type of view, there is always a single
FocusMap.
If ArcMap displays a document with three data frames in layout view,
there will be three Maps attached to the MxDocument, the ActiveView
property will return the PageLayout, and the FocusMap will return the
Map corresponding to the data frame that is highlighted. The VBA
script listed in Figure 2 illustrates this part of the object model. The
script changes the ActiveView’s extent and zooms in by 75 percent.
This script is fairly straightforward. The real work is getting the current
extent, which is an IEnvelope interface, from the ActiveView, shrinking
it, and making the modifi ed envelope the new extent. This script operates
on the ActiveView through the IActiveView interface. The ActiveView
can be either the PageLayout or a Map depending on what the end user is
looking at. This script will function correctly in either case.
Working with Interfaces
In some cases you will want to work specifi cally with either the Map or
the PageLayout through a different interface. In this situation, a COMobject
model differs greatly from a traditional object-oriented model
like Avenue or MapObjects. An interface is a collection of related methods
and properties. COM objects expose multiple interfaces. For example,
the Map exposes not only the IActiveView interface but the IMap
interface and several others
SDEWorkspaceFactory Example with Description
If you want to make a connection with any geodatabase or you want to establish sde connection then follow this code. It will help you a lot
This example opens a Geodatabase featureclass using a property set.
Dim pPropset As IPropertySet
Set pPropset = New PropertySet
Dim pFact As IWorkspaceFactory
Dim pWorkspace As IWorkspace
With pPropset
.SetProperty "Server", "testserver"
.SetProperty "Instance", "sdemss"
.SetProperty "Database", "sde"
' Ignored with ArcSDE for Oracle
.SetProperty "user", "sde"
.SetProperty "password", "go"
.SetProperty "version", "sde.DEFAULT"
End With
Set pFact = New SdeWorkspaceFactory
Set pWorkspace = pFact.Open(pPropset, Me.hWnd)
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace = pWorkspace
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeatureWorkspace.OpenFeatureClass("parcels")
This example opens a Geodatabase featureclass using a property set.
Dim pPropset As IPropertySet
Set pPropset = New PropertySet
Dim pFact As IWorkspaceFactory
Dim pWorkspace As IWorkspace
With pPropset
.SetProperty "Server", "testserver"
.SetProperty "Instance", "sdemss"
.SetProperty "Database", "sde"
' Ignored with ArcSDE for Oracle
.SetProperty "user", "sde"
.SetProperty "password", "go"
.SetProperty "version", "sde.DEFAULT"
End With
Set pFact = New SdeWorkspaceFactory
Set pWorkspace = pFact.Open(pPropset, Me.hWnd)
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace = pWorkspace
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeatureWorkspace.OpenFeatureClass("parcels")
ISpatialFilter Example with Description
Here I am Explaining the use of ISpatialFilter Interface
Add a custom UIToolControl onto any toolbar and make sure the names of the control match the code. This sample assumes the control is called UIToolControl1.
Paste the code into VBA.
Select the tool and then click on a feature.
Private Sub UIToolControl1_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
Dim pMxDoc As IMxDocument
Dim pActiveView As IActiveView
Dim pPoint As IPoint
Dim pFeature As IFeature
Set pMxDoc = Application.Document
Set pActiveView = pMxDoc.FocusMap
'Create a search point
Set pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)
'Pass the point to the FindFeature function along with the Map and search tolerance
Set pFeature = FindFeature(pMxDoc.SearchTolerance, pPoint, pMxDoc.FocusMap)
'Message box the feature ID and feature class alias name
If Not pFeature Is Nothing Then MsgBox pFeature.OID & " " & pFeature.Class.AliasName
End Sub
Private Function FindFeature(SearchTol As Double, pPoint As IPoint, pMap As IMap) As IFeature
Dim pEnvelope As IEnvelope
Dim pSpatialFilter As ISpatialFilter
Dim pEnumLayer As IEnumLayer
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureClass As IFeatureClass
Dim pFeatureCursor As IFeatureCursor
Dim pFeature As IFeature
Dim pUID As New UID
Dim ShapeFieldName As String
If pMap.LayerCount = 0 Then Exit Function
'Expand the points envelope to give better search results
Set pEnvelope = pPoint.Envelope
pEnvelope.Expand SearchTol, SearchTol, False
'Create a new spatial filter and use the new envelope as the geometry
Set pSpatialFilter = New SpatialFilter
Set pSpatialFilter.Geometry = pEnvelope
pSpatialFilter.SpatialRel = esriSpatialRelIntersects
'Search each selectable feature layer for a feature
'Return the first feature found
pUID = "{40A9E885-5533-11D0-98BE-00805F7CED21}" 'IFeatureLayer
Set pEnumLayer = pMap.Layers(pUID, False)
pEnumLayer.Reset
Set pFeatureLayer = pEnumLayer.Next
Do While Not pFeatureLayer Is Nothing
'Only search the selectable layers
If pFeatureLayer.Selectable Then
ShapeFieldName = pFeatureLayer.FeatureClass.ShapeFieldName
Set pSpatialFilter.OutputSpatialReference(ShapeFieldName) = pMap.SpatialReference
pSpatialFilter.GeometryField = pFeatureLayer.FeatureClass.ShapeFieldName
Set pFeatureClass = pFeatureLayer.FeatureClass
Set pFeatureCursor = pFeatureClass.Search(pSpatialFilter, False) 'Do the search
Set pFeature = pFeatureCursor.NextFeature 'Get the first feature
If Not pFeature Is Nothing Then
Set FindFeature = pFeature 'Exit if feature is valid
Exit Do
End If
End If
Set pFeatureLayer = pEnumLayer.Next
Loop
End Function
Add a custom UIToolControl onto any toolbar and make sure the names of the control match the code. This sample assumes the control is called UIToolControl1.
Paste the code into VBA.
Select the tool and then click on a feature.
Private Sub UIToolControl1_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
Dim pMxDoc As IMxDocument
Dim pActiveView As IActiveView
Dim pPoint As IPoint
Dim pFeature As IFeature
Set pMxDoc = Application.Document
Set pActiveView = pMxDoc.FocusMap
'Create a search point
Set pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)
'Pass the point to the FindFeature function along with the Map and search tolerance
Set pFeature = FindFeature(pMxDoc.SearchTolerance, pPoint, pMxDoc.FocusMap)
'Message box the feature ID and feature class alias name
If Not pFeature Is Nothing Then MsgBox pFeature.OID & " " & pFeature.Class.AliasName
End Sub
Private Function FindFeature(SearchTol As Double, pPoint As IPoint, pMap As IMap) As IFeature
Dim pEnvelope As IEnvelope
Dim pSpatialFilter As ISpatialFilter
Dim pEnumLayer As IEnumLayer
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureClass As IFeatureClass
Dim pFeatureCursor As IFeatureCursor
Dim pFeature As IFeature
Dim pUID As New UID
Dim ShapeFieldName As String
If pMap.LayerCount = 0 Then Exit Function
'Expand the points envelope to give better search results
Set pEnvelope = pPoint.Envelope
pEnvelope.Expand SearchTol, SearchTol, False
'Create a new spatial filter and use the new envelope as the geometry
Set pSpatialFilter = New SpatialFilter
Set pSpatialFilter.Geometry = pEnvelope
pSpatialFilter.SpatialRel = esriSpatialRelIntersects
'Search each selectable feature layer for a feature
'Return the first feature found
pUID = "{40A9E885-5533-11D0-98BE-00805F7CED21}" 'IFeatureLayer
Set pEnumLayer = pMap.Layers(pUID, False)
pEnumLayer.Reset
Set pFeatureLayer = pEnumLayer.Next
Do While Not pFeatureLayer Is Nothing
'Only search the selectable layers
If pFeatureLayer.Selectable Then
ShapeFieldName = pFeatureLayer.FeatureClass.ShapeFieldName
Set pSpatialFilter.OutputSpatialReference(ShapeFieldName) = pMap.SpatialReference
pSpatialFilter.GeometryField = pFeatureLayer.FeatureClass.ShapeFieldName
Set pFeatureClass = pFeatureLayer.FeatureClass
Set pFeatureCursor = pFeatureClass.Search(pSpatialFilter, False) 'Do the search
Set pFeature = pFeatureCursor.NextFeature 'Get the first feature
If Not pFeature Is Nothing Then
Set FindFeature = pFeature 'Exit if feature is valid
Exit Do
End If
End If
Set pFeatureLayer = pEnumLayer.Next
Loop
End Function
Subscribe to:
Posts (Atom)