Upload
stephen-watson
View
323
Download
1
Embed Size (px)
Citation preview
Copyright © 2001, 2002 ESRI. All rights reserved. Introduction to Programming ArcObjects with VBA
基于 COM 接口编程基础( I )
6-2Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Lesson overview
COM: 组件对象模型 Component Object Model
使用 COM 类 Working with COM classes 接口 Interfaces
多态 Polymorphism
接口查询 QueryInterface
测试一个对象的引用 Testing an object reference Is it nothing?
What type of object is it?
6-3Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Introducing COM
COM is a standard for creating classes
Classes can be reused between applications Independent of programming language
All ArcObjects are COM classes
Technologies based on COM Object Linking and Embedding (OLE)
OLE DB
ActiveX is any technology built on COM
DCOM and COM+
6-4Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
COM classes have interfaces
对象拥有一个或多个接口
接口是定义了一组方法和属性的逻辑关系 与对象的通信是通过接口来进行的
RaceCar
AccelerateBrake
Fuel
停靠站圈速度IRace
IDrive
垃圾车
加速刹车
燃料
拾起倾倒IGarbage
IDrive
6-5Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Instantiate COM classes with an interface Dim <variable> As <some interface>
Interfaces group properties and methods
Dim pGarbage As IDriveSet pGarbage = New GarbageTruck
pGarbage.Fuel = "Full"pGarbage.Accelerate
Working with ArcObjects COM classes
GarbageTruck
AccelerateBrake
Fuel
PickUp
DumpIGarbage
IDrive
6-6Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
More on interfaces …
一个组合音响的例子 一个对象可以播放收音机、磁带、 CD
必须使用适当的接口 如果选择了播放磁带的接口,则不能收听收音机
IRadio
ITape
ICD
Dim pBBox As ITapeSet pBBox = New BoomBoxpBBox.FM = True
6-7Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
IDrive
燃料
加速
刹车
多态 许多不同的类可以支持相同的接口
拥有相同的所有的方法和属性 可以有不同的执行方式和拥有不同的属性值
6-8Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
ArcObjects 多态 许多 ArcGIS 的类都表现出多态的特性 General interfaces for all subtypes
ILayer: All layer types (raster, tin, feature, etc.)
IGxFile: All ArcCatalog file types (shapefile, map, table, etc.)
IActiveView: Map (data view) and PageLayout (layout view)
Several others …
6-9Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Using methods and properties
Dim the variable pointing to an interface
Instantiate the object (Set)
Call methods, set properties
Only use methods and properties for the declared interface
'Create a new RaceCar with IDriveDim pCar As IDriveSet pCar = New RaceCarpCar.AcceleratepCar.Fuel = "Full"
pCar.PitStop
RaceCar
AccelerateBrake
Fuel
PitStop
LapTimeIRace
IDrive
6-10
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Getting other interfaces
QueryInterface (QI)
Access other methods and properties
Q I
RaceCar
AccelerateBrake
Fuel
PitStop
LapTimeIRace
IDrive
'Create a new RaceCar with the IDrive interfaceDim pCar As IDriveSet pCar = New RaceCarpCar.Accelerate
'Switch interfacesDim pRace As IRaceSet pRace = pCarpRace.PitStoppCar.Accelerate'**pCar and pRace point to the same object**Dim pArea As IArea
Dim pPt As IPoint
Set pArea = pPolygon ' QI for IArea on pPolygon
Set pPt = pArea.Center
6-12
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Testing an object reference
Is an object Nothing?
If pLayer Is Nothing Then MsgBox "You must select a layer." Exit SubEnd If
If TypeOf pLayer Is IFeatureLayer Then MsgBox "You selected a Feature Layer. "Else MsgBox "This layer is not a Feature Layer."End If
What TypeOf object is it?
6-14
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
COM class code
Interface module 定义方法和属性
Class module 实现方法和属性
Client module 实例化类 使用方法和属性 Client
Server
InterfaceInterface
6-16
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Using library names
Many libraries may share interface or class names 可以明白地引用对象库
'Create a new point and line from the esriCore library
Dim pPoint As esriCore.IPoint
Dim pLine As esriCore.ILine
Set pPoint = New Point
Set pLine = New Line
6-17
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Using the ESRI Object Browser
Lists classes, interfaces, properties, and methods
C:\ArcGIS\arcexe83\ArcObjects Developer Kit\Utilities
所在位置: C:\ArcGIS\arcexe83\ArcObjects Developer Kit\Utilities
6-18
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Exercise 6 overview
Design an interface
Create a COM class Implement an interface
Write client code that uses your COM class
Use QueryInterface
Copyright © 2001, 2002 ESRI. All rights reserved. Introduction to Programming ArcObjects with VBA
理解对象模型图( OMD )(II)
6-20
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Lesson overview
ArcObject 的对象模型图 阅读一个对象的模型图
类的类型 类的相互关系
接口、属性、方法的图标
根据 OMD 图表编写程序
6-21
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
OMDs help you write code Show interfaces, methods, and properties for each class
Show relationships between classes
在几个图表中拥有超过 1,500 个类 超过 1,600 个接口
ArcObject object model diagrams
6-22
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
在哪里可以找到 ArcGIS OMD 图表 Start > Programs > ArcGIS > ArcObjects Developer Help
简单的、详细的类图 PDF files
电子书籍光盘中 软件安装的目录中
6-23
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Relationship symbols
继承 组成 用来创建 对应关系 1 : N
联合
*_____
蛋
鸟
翅膀
羽毛
2
Abstract
Class
Class
Class
小鸡CoClass
巢CoClass
*
6-24
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
类和他们相应的对象
ArcMap objects
MxDocument
Map
Layer
*
*
FeatureLayer
Application
6-25
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Not creatable or instantiable Can never have instances of an abstract class
Define general interfaces for subclasses Subclasses inherit interfaces
OMD symbol: 2D shaded rectangle
抽象类(没有阴影)
6-26
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
实例化类 (Class) Noncreatable class
Cannot create with the New keyword
Obtain instances from other objects
OMD Symbol: 3D Rectangle with no shade
Dim pNewRow As IRowSet pNewRow = pTable.CreateRow
生 成
6-27
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Creatable: Use the New keyword
Dim pMap As IMap
Set pMap = New Map
Instantiable: Obtain from other objects
Dim pMap As IMap
Set pMap = pMxDocument.FocusMap
OMD symbol: Shaded 3D rectangle
可创建的类 (CoClass)
6-28
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Where to begin? Getting into the OMD
特殊的全局变量 Application: IApplication interface of the Application object
ThisDocument: IDocument interface of the MxDocument object
阅读 ArcMap 或 ArcCatalog OMD 图表的入口
Application
ThisDocument
6-29
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Property and method symbols
Property 哑铃形状的图标
Method
Property Get(read)
Property Get(read)
Property Set(write)
Property Set(write)
6-30
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Property Put: Most ArcObjects properties Property holds a value or a copy of an object
Do not use Set keyword
Property Put by Reference: Some ArcObjects properties Property holds a reference to an object
Must use the Set keyword
如果引用对象发生了变化,对象的属性将同步受到影像
Setting properties
pLayer.Name = "Port Moresby" 'No Set keyword
Set pLayer.FeatureClass = pMoresbyData 'Must use Set!
6-31
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Return a value Name: String
Return an object reference Document: IDocument
StatusBar: IStatusBar
Getting properties
返回一个值Dim strName As String Dim pDoc As IDocumentDim pBar As IStatusBarstrName = Application.NameMsgBox strName
返回一个引用Set pDoc = Application.DocumentSet pBar = Application.StatusBar pBar.Message(0) pDoc.Title
6-32
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
棒棒糖类型的图标 ( )
Finding interfaces
这些接口也是有效的。但是详细的属性和方法必须在其它地方浏览。(e.g., Object Browser).
这些接口也是有效的。但是详细的属性和方法必须在其它地方浏览。(e.g., Object Browser).
该接口被本类使用 .所有的属性和放都列表在类图中 .
该接口被本类使用 .所有的属性和放都列表在类图中 .
继承接口是有效的继承接口是有效的
6-34
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
FeatureLayer on Map Layer OMD
FeatureLayer on Map Layer OMD
概念上 , 这是一个对象模型 事实上 , 被分别在几张图表里存放
虫洞表现了图表与图表间的连接关系
Wormholes
Element on ArcMap OMD
Element on ArcMap OMD
虫洞
6-35
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Example: MxDocument > Map > layer Get the FocusMap (active data frame) from MxDocument
MxDocument may have several Maps ( * )
Get a layer from the the Map Many types of layers ( )
Dim pMxDoc As IMxDocumentSet pMxDoc = ThisDocumentDim pMap As IMapSet pMap = pMxDoc.FocusMapDim pLayer As ILayerSet pLayer = pMap.Layer(1)
'Is pLayer a FeatureLayer?If TypeOf pLayer Is IFeatureLayer Then
MsgBox "Yes!, it’s a feature layer"End If
MxDocument
Map
FeatureLayer
Layer*
*
Others
A
6-36
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Exercise 7 overview
Interpret the ArcObject object model diagrams Write code to change the ArcMap caption
Work with the MxDocument and its Maps
Use the TypeOf keyword to test an object reference
Copyright © 2001, 2002 ESRI. All rights reserved. Introduction to Programming ArcObjects with VBA
Maps and layers(III)
6-38
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Lesson overview
访问 maps and layers
遍历 maps 和 layers Collections
Enumerations
创建一个新的图层 使用图层对象的属性
设置一个图层的数据源
6-39
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Object model overview
FeatureLayer
Map
MxDocument
Application
ArcMap
*
*Layer
FeatureDataset*
Geodatabase
FeatureClass
Table
DataSet
0 .. 1
Map Layer
6-40
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Accessing maps
Access maps from MxDocument
Get the active map
Get all maps (IMaps) A collection of Maps
Dim pMxDoc As IMxDocumentSet pMxDoc = ThisDocumentDim pMap As IMapSet pMap = pMxDoc.FocusMap
Dim pAllMaps As IMapsSet pAllMaps = pMxDoc.Maps
一个地图文档可以包含有多个数据框,每个数据框都可以拥有不同的图层和表现。 FocusMap 是指向当前活动的数据框
6-41
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Accessing layers Access layers from Map or MxDocument
Get the selected layer (IMxDocument)
Get a specific layer (IMap)
Get all layers (IMap) An enumeration of layers
Dim pLayer As ILayerSet pLayer = pMxDoc.SelectedLayer
Dim pAllLayers As IEnumLayerSet pAllLayers = pMap.Layers
Dim pLayer As ILayerSet pMap = pMxDoc.FocusMapSet pLayer = pMap.Layer(3)
6-42
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
遍历 Maps 集合 Collections are ordered
Reference items by position (index)
First item is at position 0
' Map collection example … Dim intIndex As Integer Dim pMaps As IMaps Set pMaps = pMxDoc.Maps
For intIndex = 0 To pMaps.Count - 1 MsgBox pMaps.Item(intIndex).Name Next intIndex
00
11
22
' Syntax Example For <index = start> To <end> ' process each item … Next <index>
' Syntax Example For <index = start> To <end> ' process each item … Next <index>
6-43
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
遍历一个 Map 对象中的图层对象 IMap’s Layers property returns IEnumLayers
Like a collection with fewer methods and properties
Next returns ILayer
Reset moves to top of Enum
Set pLayer = pLayers.Next
Nothing
pLayers.Reset
IEnumLayerTop
Set pLayer = pLayers.Next
Set pLayer = pLayers.Next
Set pLayer = pLayers.Next
Dim pLayer As ILayerDim pLayers As IEnumLayerSet pLayers = pMap.Layers
6-44
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
遍历一个 Map 对象中的图层对象 Do While or Do Until
Loop based on a Condition (Boolean)
' Layer enum example Dim pLayer As ILayer Dim pMapLayers As IEnumLayer Set pMapLayers = pMap.Layers
Set pLayer = pMapLayers.Next Do Until pLayer Is Nothing MsgBox pLayer.Name Set pLayer = pMapLayers.Next Loop
Nothing
' Syntax ExampleDo Until/While <a condition is true> 'Run this codeLoop
' Syntax ExampleDo Until/While <a condition is true> 'Run this codeLoop
!
6-45
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Managing flow in a loop
Exit a loop prematurely when a condition is true For Next loops: Exit For
Do While and Do Until loops: Exit Do
Dim pCityMap As IMapDim X As IntegerFor X = 0 To pMaps.Count - 1 If pMaps.Item(X).Name = "Cities" Then Set pCityMap = pMaps.Item(X) Exit For End IfNext X MsgBox "All Done", vbInformation
6-46
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Loop review
Loop a specified number of times For Next
Loop based on condition Do While
Do Until
小心无限循环'Here is an Endless LoopDo While Not MsgBox("Add a Record?") = vbYes 'Code here to add a record to a table MsgBox "Record Added"Loop
6-47
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Adding a new layer to a map
Layer is an abstract class: Not creatable Creatable subclasses: TinLayer, FeatureLayer, RasterLayer, etc.
'Make a New FeatureLayerDim pFLayer As ILayerSet pFLayer = New FeatureLayer
'Add a layer to MxDocument or Map Dim pMxDoc As IMxDocument Dim pMap As IMap Set pMxDoc = ThisDocument Set pMap = pMxDoc.FocusMap
pMap.AddLayer pFLayer
没有设置数据源,所以图标为失去数据连接的状态。
6-48
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
使用图层对象的属性 Ilayer 接口的属性
Name, Visible, ShowTips, MaximumScale, MinimumScale, etc.
IGeoDataset 接口属性 Extent, SpatialReference
'This code will work for ANY type of layer'Access the document’s selected layer Dim pLayer As ILayer Set pLayer = pMxDoc.SelectedLayer'Set basic layer properties pLayer.Name = "Streets" pLayer.Visible = True pLayer.ShowTips = False
6-49
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Setting a FeatureLayer’s data source
FeatureClass property (IFeatureLayer) 指定显示的数据源 以传引用的方式 (must use the Set keyword)
更多的数据访问例子在以后的课程中'Make a new FeatureLayerDim pFLayer As IFeatureLayerSet pFLayer = New FeatureLayer
'Get another layer’s FeatureClassDim pFClass As IFeatureClassSet pFClass = pSomeOtherLayer.FeatureClass
'Set the new layer’s FeatureClass propertySet pFLayer.FeatureClass = pFClass
6-50
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Exercise 8 overview
Loop Maps in a document
Layers in a map
Fields in a layer table
Add a layer to a map Set basic properties
Set the data source
Copyright © 2001, 2002 ESRI. All rights reserved. Introduction to Programming ArcObjects with VBA
Data access and creation(IIII)
6-52
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Lesson overview
Data creation objects Workspace
FeatureDataset
FeatureClass
Working with fields and field collections
Creating Tables and FeatureClasses
Adding rows
Editing table values
FeatureClassesFeatureClasses
WorkspaceWorkspaceFeatureDatasetFeatureDataset
6-53
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Data creation objects
Table
FieldsField1 ..
DatasetWorkspaceWorkspaceFactory
AccessWorkspaceFactory
Row
*
FeatureClassArcInfoWorkspaceFactory
ShapefileWorkspaceFactory
Others
Which ones can be created new?Which ones can be created new?
*
6-55
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Opening an existing Workspace
Use IWorkspaceFactory to return a Workspace object Generic interface for all sub-types of WorkspaceFactory
OpenFromFile: Access an existing folder on disk
Open: Connect to an existing database (e.g., ArcSDE)
Dim pWFactory As IWorkspaceFactorySet pWFactory = New ArcInfoWorkspaceFactory
Dim pWFactory As IWorkspaceFactorySet pWFactory = New ArcInfoWorkspaceFactory
Dim pWorkspace As IWorkspaceSet pWorkspace = pWFactory.OpenFromFile("D:\Covers", 0)
Dim pWorkspace As IWorkspaceSet pWorkspace = pWFactory.OpenFromFile("D:\Covers", 0)
6-57
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Getting a FeatureDataset
IFeatureWorkspace interface on Workspace OpenFeatureDataset method
Dim pFWorkspace As IFeatureWorkspaceSet pFWorkspace = pWorkspace 'QI for IFeatureWorkspace
Dim pCover As IFeatureDatasetSet pCover = pFWorkspace.OpenFeatureDataset("streets")
FeatureClassesFeatureClasses
WorkspaceWorkspaceFeatureDatasetFeatureDataset
6-58
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Getting FeatureClasses
Use IFeatureClassContainer to get existing FeatureClasses from a FeatureDataset
Dim pFCC As IFeatureClassContainerDim pStreetArcs As IFeatureClassSet pFCC = pCover 'QI for IFeatureClassContainerSet pStreetArcs = pFCC.ClassByName("arc")
Dim pFCC As IFeatureClassContainerDim pStreetArcs As IFeatureClassSet pFCC = pCover 'QI for IFeatureClassContainerSet pStreetArcs = pFCC.ClassByName("arc")
FeatureClassesFeatureClasses
6-59
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Exercise 9A overview Add a ShapeFile layer to a map
Private Sub AddShapeFile_Click()
Dim pWF As IWorkspaceFactory
Set pWF = New ShapefileWorkspaceFactory
Dim pFWS As IFeatureWorkspace
Set pFWS = pWF.OpenFromFile("C:\USA", 0)
Dim pFClass As IFeatureClass
Set pFClass = pFWS.OpenFeatureClass("STATES")
Dim pFLayer As IFeatureLayer
Set pFLayer = New FeatureLayer
Set pFLayer.FeatureClass = pFClass
Dim pDataset As IDataset
Set pDataset = pFClass
pFLayer.Name = pDataset.Name
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
pDoc.AddLayer pFLayer
End Sub
6-60
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Working with Name objects
A lightweight version of the object it represents Use Open on IName to return the object
Several creatable subtypes
6-61
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Creating a new Workspace
Use IWorkspaceFactory to get a WorkspaceName Create method
Use IName to get a Workspace Open method
6-63
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Creating a new Table or FeatureClass
Use IFeatureWorkspace interface on Workspace CreateTable and CreateFeatureClass methods
Requires a Fields collection
6-64
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Field and Fields classes
Tables and FeatureClasses have an associated Fields A Fields is a Collection
A Fields object has Field objects One or several (1..*)
Creatable
6-65
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
IField and IFieldEdit
Get field properties with IField Read-only
Set field properties with IFieldEdit Write-only
Dim pNameField As IFieldEdit
Set pNameField = New Field
With pNameField
.Name = "StreetName"
.Type = esriFieldTypeString
.Length = 16
End With
6-66
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Creating a Fields collection
Use the IFieldsEdit interface AddField method: Puts a field in the collection
Dim pFieldsEdit As IFieldsEdit
Set pFieldsEdit = New Fields
pFieldsEdit.AddField pOIDField
pFieldsEdit.AddField pNameField
pFieldsEdit.AddField pSalesField
'Field = Fields !
6-67
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Creating a Table or FeatureClass
Use IFeatureWorkspace interface on Workspace CreateTable and CreateFeatureClass methods
Dim pTable As ITable
Set pTable = pFeatureWorkspace.CreateTable _
("Store55", pFieldsEdit, Nothing, Nothing, "")
Table NameTable Name
Fields CollectionFields Collection ArcSDE Configuration Keyword
ArcSDE Configuration Keyword
如果是创建 FeatrueClass 则需要指定 FeatureType 和 ShapeFieldName 参数
如果是创建 FeatrueClass 则需要指定 FeatureType 和 ShapeFieldName 参数
6-69
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Work with fields in a table
IFields interface FieldCount 返回所有字段的数目 FindField 返回查找的字段的索引号 Field 返回指定索引号的字段
For intLoop = 1 To pFClass.Fields.FieldCount – 1
MsgBox pFClass.Fields.Field(intLoop).Name
Next intLoop
intFieldNum = pTable.Fields.FindField("Area")
If intFieldNum > -1 Then '-1 means the field was not found
Set pAreaField = pTable.Fields.Field(intFieldNum)
End If
6-70
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Adding rows and values to a table
调用 Itable 对象的 CreateRow 方法,返回一个 Row 对象调用 IFeatureClass 的 CreateFeature 方法则返回一个 Feature 对象
Value property 使用 Get 或 set 方法了进行赋值
指定字段的索引号
Dim pRow As IRow
Set pRow = pTable.CreateRow
pRow.Value(1) = "Jesse White" 'Name
pRow.Value(pTable.FindField("Age")) = 35 'Age
pRow.Store 保存提交新的行保存提交新的行
TableRow
FeatureClassFeature
Feature 有一个 Shape 属性来保几何对象Feature 有一个 Shape 属性来保几何对象
6-71
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Exercise 9B overview
Make a new ShapeFile
Create a new table Add records
Edit values
Copyright © 2001, 2002 ESRI. All rights reserved. Introduction to Programming ArcObjects with VBA
Geometry and geoprocessing(V)
Thad Tilton:Thad Tilton:
6-73
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Lesson overview
Geometry
Operators: Working with feature geometry
Drawing geometry
Geoprocessing
6-74
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
线段:在曲线的起点和终点之间线段集合成 paths/rings
Paths/rings 集合成 lines/polygons
可以在任何层次上编辑
Feature geometry
Segments
Bezier curve
Line Circular arc
3 Rings (closed paths)
2 Paths
1 Poly1 Line
Points Multipoints PolygonsPolylines
6-75
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Geometry objects
Point MultiPoint
Ring
PathSegment
Geometry
EnvelopeCurve
Polyline
Line BezierCurveCircularArc
Polycurve
**
*
Polygon
*
Geometrycollection
*
6-76
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Points and multipoints
点是零维的 通过 X 、 Y坐标来定义 可以拥有 Z 和 M 属性值
Multipoints 是 points 的集合
Point
Multipoint with
six points
Dim pPoint As IPoint
Set pPoint = New Point
pPoint.X = 300
pPoint.Y = 450
Dim pMultiPts As IPointCollection
Set pMultiPts = New MultiPoint
pMultiPts.AddPoint pPoint
6-77
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
由起点、终点和方程定义三个方面组成
子类 : Line, BezierCurve, CircularArc
线段用来创建其它的几个对象 Paths, polylines, rings, and polygons
Dim pSegment As ILine
Set pSegment = New Line
pSegment.FromPoint = pPointA
pSegment.ToPoint = pPointB
Segments
From
To
FromTo
6-78
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Paths and polygons
Polylines由一组闭合或不闭合的 Paths 组成
Polygons由一个或多个 rings 组成
Path with four segments
Polygon with seven rings
6-79
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Envelopes
定义了 feature 的空间范围最小外接矩形
所有的几何对象都有 Envelop 属性 Get or set with IGeometry :: Envelope
Dim pEnvelope As IEnvelope
Set pEnvelope = pLine.Envelope
6-80
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Zooming in to a Feature
Get the extent using a shape’s Envelope property On the IGeometry interface
Set the ActiveView Extent property with an Envelope
Feature Geometry
Envelope
1 Polygon Feature
pMxDoc.ActiveView.Extent = pFeature.Shape.EnvelopepMxDoc.ActiveView.Refresh
pMxDoc.ActiveView.Extent = pFeature.Shape.EnvelopepMxDoc.ActiveView.Refresh
6-81
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Displaying features
调用 IScreenDisplay 接口的几个 Draw 方法 StartDrawing: 为绘制准备显示环境 DrawPoint, DrawMultipoint, DrawPolyline, DrawPolygon
FinishDrawing: 把缓存中的内容绘制到屏幕上
Quick draw of geometry and symbol
Dim pDisplay As IScreenDisplay
Set pDisplay = pMxApplication.Display
pDisplay.StartDrawing pDisplay.HDC, esriNoScreenCache
pDisplay.SetSymbol pSym
pDisplay.DrawPolygon pPolygon
pDisplay.FinishDrawing
6-82
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Geometry spatial operator interfaces
Interfaces supported by subtypes of Geometry ITopologicalOperator (空间操作) IProximityOperator (测量) IRelationalOperator (检查)
用于 …完成例如 buffer, cut, clip等空间操作 测量 shapes之间的距离检查空间关系 通过空间标准来进行查询
6-83
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
ITopologicalOperator
提供方法去操作几何对象由 Point, Multipoint, Polyline, and Polygon提供
Dim pTopoOp As ITopologicalOperator
Dim pBuffPoly As IPolygon
Set pTopoOp = pFeature.Shape
Set pBuffPoly = pTopoOp.Buffer (intBufferDistance)
Buffer
IntersectUnion
Cut Clip
6-84
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
IRelationalOperator
提供检查空间关系的功能 (return Boolean) Equals: 几何对象在空间上是否相同 ?
Touches: 边界是否接触 ?
Contains: 是否是包容关系 ?
其它操作
Dim pRelationOp As IRelationalOperator
Dim booTouches As Boolean
Set pRelationOp = pPoly
booTouches = pRelationOp.Touches (pAnotherPoly)
6-85
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
IProximityOperator
提供检查两个空间对象的相邻关系功能 ReturnDistance: Returns the minimum distance between
features (double)
ReturnNearestPoint: Finds and returns the nearest point on the specified feature (point)
Dim pProxOp As IProximityOperator
Dim dblDistance As Double
Set pProxOp = pLine
dblDistance = pProxOp.ReturnDistance (pSomeOtherLine)
?
6-86
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Geoprocessing
IBasicGeoprocessor provides access to methods found in the Geoprocessing Wizard
Use for an entire Feature class
Most methods require an input table Clip
Dissolve
Intersect
Merge
Union
BasicGeoprocessor
IBasicGeoprocessor : IUnknown
SpatialReference: ISpatialReference
Clip (in InputTable: ITable, inuseSelectedInput: Boolean, in clipTable:ITable, in useSelectedClip: Boolean, inTolerance: Double, in OutputName:IFeatureClassName): IFeatureClass
Merge (in Tables: IArray, in fieldsTable:ITable, in OutputName:IFeatureClassName): IFeatureClass
Dissolve (in InputTable: ITable, inuseSelected: Boolean, in dissolveField:String, in summaryFields: String, inOutputName: IDatasetName): ITable
Intersect (in InputTable: ITable, inuseSelectedInput: boolean, inoverlayTable: ITable, inuseSelectedOverlay: Boolean, inTolerance: Double, in OutputName:IFeatureClassName): IFeatureClass
Union (in InputTable: ITable, inuseSelectedInput: Boolean, inoveralyTable: ITable, in overlayTable:ITable, in useSelectedOverlay: Boolean, inTolerance: Double, in OutputName:IFeatureClassName): IFeatureClass
IBasicGeoprocessor
6-87
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Area and length
Each feature is a type of Geometry
QueryInterface to Curve (Line features) or Polygon
Dim pArea As IArea
Set pArea = pPoly ‘QI
Msgbox pArea.Area
Dim pCurve As ICurve
Set pCurve = pLine ‘QI
Msgbox pCurve.Length
Polygon
IPolygon: IPolycurve
ExteriorRingCount: Long
QueryExteriorRings(ExteriorRings: IRing)
CloseFindExteriorRing(interiorRing: IRing): IRing
QueryInteriorRings(exteriorRings: IRing,interiorRings: IRing)
IPolygon
IAreaInteriorRingCount(exteriorRing: IRong): Long
SimplifyPreserveFromTo
CurveICurve: IGeometry
FromPoint: IPoint
ICurve
IsClosed: Boolean
Length: Double
ToPont: IPoint
6-88
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Exercise 10 overview
Create point features from coordinates
Store point features
Assign feature attributes
Create a polygon from a Point collection
Challenge: Calculate the area from the polygon you created
Copyright © 2001, 2002 ESRI. All rights reserved. Introduction to Programming ArcObjects with VBA
Working with subsets and selections(VI)
6-90
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Lesson overview
从 Table 或 Layer 对象中取得当前的选择集 SelectionSet
从 records 对象中取得一个 subset游标 Cursor
特征游标 FeatureCursor
查询过滤器和空间过滤器 QueryFilters and SpatialFilters
Three types of cursors: Search, Update, and Insert
遍历一个 cursor 的记录 Displaying a subset of layer features
6-91
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Object Model overview
TableCursor
FeatureClassFeatureCursor
QueryFilter
Row
Feature
= ‘in conjunction with’ = ‘in conjunction with’
SelectionSet
SpatialFilter
6-92
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
SelectionSet
Get the currently selected records (Rows or Features) IFeatureSelection :: SelectionSet (FeatureLayer)
ITableWindow :: SelectionSet (TableWindow)
Public Sub GetLayerSelection() Dim pMxDoc As IMxDocument Dim pFSel As IFeatureSelection Dim pSelSet As ISelectionSet Set pMxDoc = ThisDocument
Set pFSel = pMxDoc.SelectedLayer 'QueryInterface Set pSelSet = pFSel.SelectionSet MsgBox pSelSet.Count & " selected features"End Sub
6-94
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Creating a QueryFilter
Create a QueryFilter with the New keyword
Set the WhereClause property (string)
QueryFilterQueryFilter
WhereClauseWhereClause
Dim pQFilter As IQueryFilter
Set pQFilter = New QueryFilter
pQFilter.WhereClause = "LENGTH > 100000"
Dim pQFilter As IQueryFilter
Set pQFilter = New QueryFilter
pQFilter.WhereClause = "LENGTH > 100000"
6-95
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Returning a Search cursor
Apply to a Table or FeatureClass Search method
Returns a Cursor or FeatureCursor
Dim pFCursor As IFeatureCursor
Set pFCursor = pFClass.Search(pQFilter, True)
Dim pFCursor As IFeatureCursor
Set pFCursor = pFClass.Search(pQFilter, True)
'What do you think this code does?
Set pFCursor = pFClass.Search(Nothing, True)
'What do you think this code does?
Set pFCursor = pFClass.Search(Nothing, True)
FeatureClass
Search
QueryFilter
FeatureCursor
6-97
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
SpatialFilter
Type of QueryFilter
Select features based on a spatial relationship
Geometry Property• Point• Line• Polygon
Geometry Property• Point• Line• Polygon
SpatialRel Property• Inside• Contains• Intersects• Etc …
SpatialRel Property• Inside• Contains• Intersects• Etc …
Dim pSFilter As ISpatialFilterSet pSFilter = New SpatialFilterSet pSFilter.Geometry = pBufferPolygonpSFilter.SpatialRel = esriSpatialRelContainsSet pFCursor = pCityFClass.Search (pSFilter, True)
Dim pSFilter As ISpatialFilterSet pSFilter = New SpatialFilterSet pSFilter.Geometry = pBufferPolygonpSFilter.SpatialRel = esriSpatialRelContainsSet pFCursor = pCityFClass.Search (pSFilter, True)
6-99
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
三种类型的 cursors
由调用的方法来决定返回的游标 Search cursor
Search method
Use for read-only analysis of a record subset
Update cursor Update method
Use to update or delete records in the database
Insert cursor Insert method
Use to insert new records into the database
Dim myCursor As IFeatureCursor
Set myCursor = pFClass.Search(pQFilter, False)
Dim myCursor As IFeatureCursor
Set myCursor = pFClass.Search(pQFilter, False)
Dim myCursor As IFeatureCursor
Set myCursor = pFClass.Update(pQFilter, False)
Dim myCursor As IFeatureCursor
Set myCursor = pFClass.Update(pQFilter, False)
Dim myCursor As IFeatureCursor
Set myCursor = pFClass.Insert(True)
Dim myCursor As IFeatureCursor
Set myCursor = pFClass.Insert(True)
6-101
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
访问游标中的记录 当游标初始化完成,游标指针指向第一个位置的上面 使用 NextRow/NextFeature 方法来返回一个 Row/Feature
Set myRow = myCursor.NextRowSet myRow = myCursor.NextRowSet myRow = myCursor.NextRow
Set myRow = myCursor.NextRow
myCursor initializes heremyCursor initializes here
Dim myCursor As ICursorSet myCursor = pTable.Search(pQFilter, False)
Nothing is returned when all records have been accessedNothing is returned when all records have been accessed
CursorCursor
6-102
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Dim pFeature As IFeatureDim intLength As Long
Set pFeature = pFCursor.NextFeatureDo Until pFeature Is Nothing intLength = intLength + pFeature.Value(9) Set pFeature = pFCursor.NextFeatureLoopMsgBox pFClass.FeatureCount(pQFilter) & _
" roads longer than 100 km." & _ " The total length is " & intLength
例子 : 统计游标中的属性 Use a Do Until loop
Loop until no more features (Nothing)
Use IFeatureClass::FeatureCount to count features
6-103
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
回顾 : Features and geometry
Every feature has geometry (shape)
All geometry has an envelope A bounding rectangle
Use an Envelope to set the ArcMap extent
'Zoom to Qatar …pQFilter.WhereClause = "Name = ‘Qatar’"Set pFCursor = pFClass.Search (pQFilter, True)Set pFeature = pFCursor.NextFeatureIf Not pFeature Is Nothing Then Dim pEnv As IEnvelope Set pEnv = pFeature.Shape.Envelope pMxDoc.ActiveView.Extent = pEnvEnd If
'Zoom to Qatar …pQFilter.WhereClause = "Name = ‘Qatar’"Set pFCursor = pFClass.Search (pQFilter, True)Set pFeature = pFCursor.NextFeatureIf Not pFeature Is Nothing Then Dim pEnv As IEnvelope Set pEnv = pFeature.Shape.Envelope pMxDoc.ActiveView.Extent = pEnvEnd If
6-105
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
显示图层中的部分要素 使用 IFeatureLayerDefinition :: DefinitionExpression
创建一个临时的要素子集用于显示和分析
Public Sub MakeLayerDefinition() Dim pMxDoc As IMxDocument Dim pFDefine As IFeatureLayerDefinition Set pMxDoc = ThisDocument Set pFDefine = pMxDoc.SelectedLayer 'QueryInterface pFDefine.DefinitionExpression = "Pop > 3000000" pMxDoc.ActiveView.Refresh 'Refresh the display End Sub
6-106
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
Exercise 11 overview
显示图层的一个子集 IFeatureLayerDefinition :: DefinitionExpression
对一个要素子集进行统计 IQueryFilter :: WhereClause
FeatureCursors
根据属性或空间关系形成一个游标 SpatialFilter
SelectionSet
6-107
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
例子:遍历 GeoDatabase 数据集、要素类
Sub BrowsFeatureClassInFeatureDatasset()
' 定义一个 WorkSpaceFactory 类工厂
Dim pSdeWorkspaceFactory As IWorkspaceFactory
' 定义一个 WorkSpace 对象
Dim pSdeWorkspace As IWorkspace
' 定义一个 PropertySet 对象 , 用来设置 SDE连接信息 .
Dim pConnectionProperties As IPropertySet
' 创建一个新的 PropertySet 对象
Set pConnectionProperties = New PropertySet
With pConnectionProperties
.SetProperty "SERVER", "afei"
.SetProperty "INSTANCE", "5151/tcp"
.SetProperty "DATABASE", "sde"
.SetProperty "USER", "sde"
.SetProperty "PASSWORD", "sde"
.SetProperty "VERSION", "SDE.DEFAULT"
End With
' 定义一个 SDE 类型的 WorkSpace 类工厂
Set pSdeWorkspaceFactory = New SdeWorkspaceFactory
' 通过类工厂的 Create 方法返回一个 Sde 类型的 WorkSpace 对象 .
Set pSdeWorkspace = pSdeWorkspaceFactory.Open(pConnectionProperties, 0)
6-108
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
遍历要素集( FeatureDataset )If Not pSdeWorkspace Is Nothing Then
'---------------------------------------------------
' 定义一个数据集集合对象
Dim pDatasets As IEnumDataset
' 通过接口查询返回所有 FeatureDataset类型的数据集集合
Set pDatasets = pSdeWorkspace.Datasets(esriDTFeatureDataset)
' 定义一个数据集对象
Dim pDataset As IFeatureDataset
'取得第一个数据集
Set pDataset = pDatasets.Next
' 如果该数据集不是空的
If Not pDataset Is Nothing Then
' 定义一个 FeatureClassContainer 对象
Dim pFeatureCC As IFeatureClassContainer
' 通过接口查询返回FeatureClassContainer 对象 .
Set pFeatureCC = pDataset
' 定义一个特征类集合对象
Dim pEFC As IEnumFeatureClass
' 通过接口查询返回集合对象
Set pEFC = pFeatureCC.Classes
' 定义特征类对象
Dim pFC As IFeatureClass
'取得第一个特征类
Set pFC = pEFC.Next
6-109
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
遍历要素类( FeatureClass )
Do While Not pFC Is Nothing
' 显示特征类得别名
MsgBox pFC.AliasName
' 循环到下一个特征类
Set pFC = pEFC.Next
Loop
End If
'--------------------------------------------------
Else
MsgBox " 连接错误 "
End If
End Sub
6-110
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
遍历一个 FeatureClass 中的 Feature 对象 If Not InFeatureClass Is Nothing Then Dim i As Long For i = 0 To InFeatureClass.FeatureCount(Nothing) - 1 Dim Ft As IFeature Dim Geo As IGeometry Set Ft = InFeatureClass.GetFeature(i) Set Geo = Ft.ShapeCopy Set Geo = Nothing Set Ft = Nothing Next iEnd If
6-111
Introduction to Programming ArcObjects with VBACopyright © 2001, 2002 ESRI. All rights reserved.
上机操作练习课程内容