1 / 98

基于 COM 接口编程基础( I)

基于 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.

zavad
Download Presentation

基于 COM 接口编程基础( I)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 基于COM接口编程基础(I)

  2. 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?

  3. 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+

  4. 垃圾车 RaceCar 燃料 IDrive Fuel IDrive 加速 Accelerate 刹车 Brake IGarbage 倾倒 IRace 圈速度 拾起 停靠站 COM classes have interfaces • 对象拥有一个或多个接口 • 接口是定义了一组方法和属性的逻辑关系 • 与对象的通信是通过接口来进行的

  5. 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

  6. IRadio ITape ICD More on interfaces … • 一个组合音响的例子 • 一个对象可以播放收音机、磁带、CD • 必须使用适当的接口 • 如果选择了播放磁带的接口,则不能收听收音机 Dim pBBox As ITape Set pBBox = New BoomBox pBBox.FM = True

  7. 多态 • 许多不同的类可以支持相同的接口 • 拥有相同的所有的方法和属性 • 可以有不同的执行方式和拥有不同的属性值 IDrive 燃料 加速 刹车

  8. 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 …

  9. 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

  10. 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

  11. 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

  12. COM class code Interface • Interface module • 定义方法和属性 • Class module • 实现方法和属性 • Client module • 实例化类 • 使用方法和属性 Server Client

  13. 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

  14. 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

  15. Exercise 6 overview • Design an interface • Create a COM class • Implement an interface • Write client code that uses your COM class • Use QueryInterface

  16. 理解对象模型图(OMD)(II)

  17. Lesson overview • ArcObject 的对象模型图 • 阅读一个对象的模型图 • 类的类型 • 类的相互关系 • 接口、属性、方法的图标 • 根据OMD图表编写程序

  18. 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 个接口

  19. 在哪里可以找到ArcGIS OMD图表 • Start > Programs > ArcGIS > ArcObjects Developer Help • 简单的、详细的类图 • PDF files • 电子书籍光盘中 • 软件安装的目录中

  20. Relationship symbols • 继承 • 组成 • 用来创建 • 对应关系 1:N • 联合 鸟 巢 CoClass Abstract * 羽毛 小鸡 _____ * CoClass Class 2 蛋 Class 翅膀 Class

  21. ArcMap objects • 类和他们相应的对象 Application MxDocument * Map * Layer FeatureLayer

  22. 抽象类(没有阴影) • 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

  23. 实例化类 (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 生 成

  24. 可创建的类 (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

  25. 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

  26. Property Set (write) Property Get (read) Property and method symbols • Property • 哑铃形状的图标 • Method

  27. 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!

  28. 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

  29. Finding interfaces • 棒棒糖类型的图标 ( ) 继承接口是有效的 该接口被本类使用. 所有的属性和放都列表在类图中. 这些接口也是有效的。但是详细的 属性和方法必须在其它地方浏览。 (e.g., Object Browser).

  30. Wormholes • 概念上,这是一个对象模型 • 事实上, 被分别在几张图表里存放 • 虫洞表现了图表与图表间的连接关系 虫洞 Element on ArcMap OMD FeatureLayer on Map Layer OMD

  31. 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

  32. 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

  33. Maps and layers(III)

  34. Lesson overview • 访问 maps and layers • 遍历 maps 和 layers • Collections • Enumerations • 创建一个新的图层 • 使用图层对象的属性 • 设置一个图层的数据源

  35. Object model overview Application ArcMap DataSet MxDocument * Map Geodatabase * Table Layer Map Layer 0 .. 1 FeatureDataset * FeatureClass FeatureLayer

  36. 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是指向当前活动的数据框

  37. 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

  38. 遍历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

  39. 遍历一个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

  40. 遍历一个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 !

  41. 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

  42. 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

  43. 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 没有设置数据源,所以图标为失去数据连接的状态。

  44. 使用图层对象的属性 • 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

  45. 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

  46. 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

  47. Data access and creation(IIII)

  48. 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

  49. Data creation objects * Workspace Dataset WorkspaceFactory Field Fields * 1 .. ShapefileWorkspaceFactory Row Table AccessWorkspaceFactory FeatureClass ArcInfoWorkspaceFactory Others Which ones can be created new?

  50. 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)

More Related