980 likes | 1.31k Views
基于 COM 接口编程基础( I). 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?. Introducing COM.
E N D
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?
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+
垃圾车 RaceCar 燃料 IDrive Fuel IDrive 加速 Accelerate 刹车 Brake IGarbage 倾倒 IRace 圈速度 拾起 停靠站 COM classes have interfaces • 对象拥有一个或多个接口 • 接口是定义了一组方法和属性的逻辑关系 • 与对象的通信是通过接口来进行的
GarbageTruck Fuel IDrive Accelerate Brake IGarbage Dump PickUp Working with ArcObjects COM classes • Instantiate COM classes with an interface • Dim <variable> As <some interface> • Interfaces group properties and methods Dim pGarbage As IDrive Set pGarbage = New GarbageTruck pGarbage.Fuel = "Full" pGarbage.Accelerate
IRadio ITape ICD More on interfaces … • 一个组合音响的例子 • 一个对象可以播放收音机、磁带、CD • 必须使用适当的接口 • 如果选择了播放磁带的接口,则不能收听收音机 Dim pBBox As ITape Set pBBox = New BoomBox pBBox.FM = True
多态 • 许多不同的类可以支持相同的接口 • 拥有相同的所有的方法和属性 • 可以有不同的执行方式和拥有不同的属性值 IDrive 燃料 加速 刹车
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 …
RaceCar Fuel IDrive Accelerate Brake IRace LapTime PitStop 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 IDrive Dim pCar As IDrive Set pCar = New RaceCar pCar.Accelerate pCar.Fuel = "Full" pCar.PitStop
Getting other interfaces • QueryInterface (QI) • Access other methods and properties 'Create a new RaceCar with the IDrive interface Dim pCar As IDrive Set pCar = New RaceCar pCar.Accelerate 'Switch interfaces Dim pRace As IRace Set pRace = pCar pRace.PitStop pCar.Accelerate '**pCar and pRace point to the same object** RaceCar Fuel IDrive Q I Accelerate Brake IRace LapTime PitStop Dim pArea As IArea Dim pPt As IPoint Set pArea = pPolygon ' QI for IArea on pPolygon Set pPt = pArea.Center
Testing an object reference • Is an object Nothing? If pLayerIs Nothing Then MsgBox "You must select a layer." Exit Sub End If • What TypeOf object is it? If TypeOfpLayerIsIFeatureLayer Then MsgBox "You selected a Feature Layer. " Else MsgBox "This layer is not a Feature Layer." End If
COM class code Interface • Interface module • 定义方法和属性 • Class module • 实现方法和属性 • Client module • 实例化类 • 使用方法和属性 Server Client
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
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
Exercise 6 overview • Design an interface • Create a COM class • Implement an interface • Write client code that uses your COM class • Use QueryInterface
Lesson overview • ArcObject 的对象模型图 • 阅读一个对象的模型图 • 类的类型 • 类的相互关系 • 接口、属性、方法的图标 • 根据OMD图表编写程序
ArcObject object model diagrams • OMDs help you write code • Show interfaces, methods, and properties for each class • Show relationships between classes • 在几个图表中拥有超过 1,500 个类 • 超过 1,600 个接口
在哪里可以找到ArcGIS OMD图表 • Start > Programs > ArcGIS > ArcObjects Developer Help • 简单的、详细的类图 • PDF files • 电子书籍光盘中 • 软件安装的目录中
Relationship symbols • 继承 • 组成 • 用来创建 • 对应关系 1:N • 联合 鸟 巢 CoClass Abstract * 羽毛 小鸡 _____ * CoClass Class 2 蛋 Class 翅膀 Class
ArcMap objects • 类和他们相应的对象 Application MxDocument * Map * Layer FeatureLayer
抽象类(没有阴影) • 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
实例化类 (Class) • Noncreatable class • Cannot create with the New keyword • Obtain instances from other objects • OMD Symbol: 3D Rectangle with no shade Dim pNewRow As IRow Set pNewRow = pTable.CreateRow 生 成
可创建的类 (CoClass) • 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
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
Property Set (write) Property Get (read) Property and method symbols • Property • 哑铃形状的图标 • Method
Setting properties • 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 • 如果引用对象发生了变化,对象的属性将同步受到影像 pLayer.Name = "Port Moresby" 'No Set keyword Set pLayer.FeatureClass = pMoresbyData 'Must use Set!
Getting properties • Return a value • Name: String • Return an object reference • Document: IDocument • StatusBar: IStatusBar 返回一个值 Dim strName As String Dim pDoc As IDocument Dim pBar As IStatusBar strName = Application.Name MsgBox strName 返回一个引用 Set pDoc = Application.Document Set pBar = Application.StatusBar pBar.Message(0) pDoc.Title
Finding interfaces • 棒棒糖类型的图标 ( ) 继承接口是有效的 该接口被本类使用. 所有的属性和放都列表在类图中. 这些接口也是有效的。但是详细的 属性和方法必须在其它地方浏览。 (e.g., Object Browser).
Wormholes • 概念上,这是一个对象模型 • 事实上, 被分别在几张图表里存放 • 虫洞表现了图表与图表间的连接关系 虫洞 Element on ArcMap OMD FeatureLayer on Map Layer OMD
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 ( ) MxDocument * Map Dim pMxDoc As IMxDocument Set pMxDoc = ThisDocument Dim pMap As IMap Set pMap = pMxDoc.FocusMap Dim pLayer As ILayer Set pLayer = pMap.Layer(1) 'Is pLayer a FeatureLayer? IfTypeOf pLayer Is IFeatureLayer Then MsgBox "Yes!, it’s a feature layer" End If * Layer FeatureLayer Others A
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
Lesson overview • 访问 maps and layers • 遍历 maps 和 layers • Collections • Enumerations • 创建一个新的图层 • 使用图层对象的属性 • 设置一个图层的数据源
Object model overview Application ArcMap DataSet MxDocument * Map Geodatabase * Table Layer Map Layer 0 .. 1 FeatureDataset * FeatureClass FeatureLayer
Accessing maps • Access maps from MxDocument • Get the active map • Get all maps (IMaps) • A collection of Maps Dim pMxDoc As IMxDocument Set pMxDoc=ThisDocument Dim pMap As IMap Set pMap = pMxDoc.FocusMap Dim pAllMaps As IMaps Set pAllMaps = pMxDoc.Maps 一个地图文档可以包含有多个数据框,每个数据框都可以拥有不同的图层和表现。FocusMap是指向当前活动的数据框
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 ILayer Set pLayer = pMxDoc.SelectedLayer Dim pLayer As ILayer Set pMap = pMxDoc.FocusMap Set pLayer = pMap.Layer(3) Dim pAllLayers As IEnumLayer Set pAllLayers = pMap.Layers
遍历Maps集合 • Collections are ordered • Reference items by position (index) • First item is at position 0 0 1 ' Syntax Example For <index = start>To<end> ' process each item … Next <index> ' Map collection example … Dim intIndex As Integer Dim pMaps As IMaps Set pMaps = pMxDoc.Maps ForintIndex = 0TopMaps.Count - 1 MsgBox pMaps.Item(intIndex).Name NextintIndex 2
遍历一个Map对象中的图层对象 • IMap’s Layers property returns IEnumLayers • Like a collection with fewer methods and properties • Next returns ILayer • Reset moves to top of Enum Dim pLayer As ILayer Dim pLayers As IEnumLayer Set pLayers = pMap.Layers IEnumLayer Top Set pLayer = pLayers.Next Set pLayer = pLayers.Next pLayers.Reset Set pLayer = pLayers.Next Set pLayer = pLayers.Next Nothing
遍历一个Map对象中的图层对象 • Do While or Do Until • Loop based on a Condition (Boolean) ' Syntax Example Do Until/While<a condition is true> 'Run this code Loop ' Layer enum example Dim pLayer As ILayer Dim pMapLayers As IEnumLayer Set pMapLayers = pMap.Layers Set pLayer = pMapLayers.Next Do UntilpLayerIs Nothing MsgBox pLayer.Name Set pLayer = pMapLayers.Next Loop Nothing !
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 IMap Dim X As Integer For X = 0 To pMaps.Count - 1 If pMaps.Item(X).Name = "Cities" Then Set pCityMap = pMaps.Item(X) Exit For End If Next X MsgBox "All Done", vbInformation
Loop review • Loop a specified number of times • For Next • Loop based on condition • Do While • Do Until • 小心无限循环 'Here is an Endless Loop Do While Not MsgBox("Add a Record?") = vbYes 'Code here to add a record to a table MsgBox "Record Added" Loop
Adding a new layer to a map • Layer is an abstract class: Not creatable • Creatable subclasses: TinLayer, FeatureLayer, RasterLayer, etc. 'Make a New FeatureLayer Dim pFLayer As ILayer Set 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 没有设置数据源,所以图标为失去数据连接的状态。
使用图层对象的属性 • 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
Setting a FeatureLayer’s data source • FeatureClass property (IFeatureLayer) • 指定显示的数据源 • 以传引用的方式 (must use the Set keyword) • 更多的数据访问例子在以后的课程中 'Make a new FeatureLayer Dim pFLayer As IFeatureLayer Set pFLayer = New FeatureLayer 'Get another layer’s FeatureClass DimpFClassAsIFeatureClass SetpFClass = pSomeOtherLayer.FeatureClass 'Set the new layer’s FeatureClass property Set pFLayer.FeatureClass = pFClass
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
Lesson overview • Data creation objects • Workspace • FeatureDataset • FeatureClass • Working with fields and field collections • Creating Tables and FeatureClasses • Adding rows • Editing table values Workspace FeatureDataset FeatureClasses
Data creation objects * Workspace Dataset WorkspaceFactory Field Fields * 1 .. ShapefileWorkspaceFactory Row Table AccessWorkspaceFactory FeatureClass ArcInfoWorkspaceFactory Others Which ones can be created new?
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 IWorkspaceFactory Set pWFactory = New ArcInfoWorkspaceFactory Dim pWorkspace As IWorkspace Set pWorkspace = pWFactory.OpenFromFile("D:\Covers", 0)