290 likes | 486 Views
Lecture 3 Introducing ArcObjects. ArcObjects ArcObjects is the development platform for the ArcGIS family of applications such as ArcMap, ArcCatalog, and ArcScene. ArcObjects is built using Microsoft’s Component Object Model (COM) technology. Therefore –
E N D
Lecture 3 Introducing ArcObjects ArcObjects ArcObjects is the development platform for the ArcGIS family of applications such as ArcMap, ArcCatalog, and ArcScene. ArcObjects is built using Microsoft’s Component Object Model (COM) technology. Therefore – VBA is embedded within ArcCatalog and ArcMap, and can be used to customize the ArcGIS Desktop applications. Jun Liang, Geography @ UNC
ArcObjects (Cont.) • A set of components of the ArcGIS platform which can be accessed through programming environment (VBA, C++, Python, etc.) • Have characteristics which can be queried or set • Have the ability to perform operations • Have the ability to respond to changes in the application framework in which they are deployed • Object Model Diagrams (OMD’s)in PDF format (ALLOMDS.pdf) Jun Liang, Geography @ UNC
Reading the object model diagrams Jun Liang, Geography @ UNC
Reading the object model diagrams (Cont.) Types of Classes An abstract class cannot be used to create new objects but is a specification for instances of subclasses (through type inheritance.) A CoClass can directly create objects by declaring a new object. A Class cannot directly create objects, but objects of this class can be created as a property of another class or instantiated by objects from another class. Jun Liang, Geography @ UNC
Associations Relationships 1. Associations Associations represent relationships between classes. They have defined multiplicities at both end. Jun Liang, Geography @ UNC
Multiplicity 2. Multiplicity A Multiplicity is a constraint on the number of objects that can be associated with another object. This is the notation for multiplicities: 1 – One and only one. 0..1 – Zero or one M..N – From M to N (positive integers) * Or 0..* - From zero to any positive integer 1..* - From one to any positive integer Jun Liang, Geography @ UNC
Inheritance Type inheritance defines specialized classes that share properties and methods with the superclass and have additional properties and methods. This diagram shows that a primary line (creatable class) and secondary line (creatable class) are types of a line (abstract class). Jun Liang, Geography @ UNC
Pole Transformer Instantiation Instantiation specifies that one object from one class has a method with which it creates an object from another class. A pole object might have a method to create a transformer object. OverviewWindowFactory OverviewWindow Jun Liang, Geography @ UNC
Pole Crossarm Composition Composition is a stronger form of aggregation in which objects from the “whole” class control the lifetime of objects from the “part” class. 1..* A pole contains one or many crossarms. In This design, a crossarm cannot be recycled before the pole is removed. The pole object controls the lifetime of the crossarm object. Jun Liang, Geography @ UNC
TOCView MxDocument Composition (Cont.) A MxDocument object contains none or many TOCView object: * Jun Liang, Geography @ UNC
Interface key Property Get: Read Property Put: Write Property Get/Put: Read/Write Property Put by Reference: Write Jun Liang, Geography @ UNC
Making Decisions Coding for making decisions Making a case for branching Select Case theTrafficSignalValue Case Red Stop the car Case Green Continue with caution Case Yellow Stop the car if you can not make if before red Case Else Treat the intersection as if it has a stop sign End Select Jun Liang, Geography @ UNC
Coding for making decisions (Cont.) If Then Statement If Today = aWorkDay Then Turn left to work in the city ElseIf Weather = Bad Then Go ahead for the library Else Turn right to drive in the countryside End IF Jun Liang, Geography @ UNC
Using subroutines and functions Calling a subroutine (example - ex6a) Public Sub Message() MsgBox “Geography is terrific” End Sub Public Sub GetMessages() Call Message End Sub • One procedure can call many others. • A procedure may call a procedure that calls another procedure Jun Liang, Geography @ UNC
Using subroutines and functions (Cont.) Passing values to a subroutines PublicSub PrintMap (aPageSize As String) .. End When you call the PrintMap subroutine, you must enter a value for its argument: Call PrintMap (“Letter”) Example in ArcGIS: Call CreateNewChart(userTitle) You can also make several calls to a single subroutine (example - ex6c) Jun Liang, Geography @ UNC
Using subroutines and functions (Cont.) Returning values with functions (ex6d) Question: What is the difference between subroutines and functions? strValue = InputBox (“Enter a Parcel Value”) Example: Public Function KilometersToMiles (km As Double) As Double KilometersToMiles = km * 0.621371 End Function Dim x As Double x = KilometersToMiles(10) Jun Liang, Geography @ UNC
Looping your code Coding a For loop (ex7a) For x=1 to 10 ‘Code here to print map MsgBox “Printing map” & x Next For x=1 to 10000 Step 20 … Next For x=1 to 10 cboFavoriteNumber.AddItem x end Jun Liang, Geography @ UNC
Looping your code (Cont.) Coding a Do loop (ex7b) Do While Expression -Code here runs as long as the expression is true Loop Do Until Expression -Code here runs as long as the expression is false Loop Do While Msgbox (“Print the map?”, vbYesNo) = vbYes ‘Code here to print map MsgBox “Printing Map” Loop Jun Liang, Geography @ UNC
Fixing bugs Before VBA code runs, it is automatically translated into machine language. This translation is called compiling. (1)Syntax Error: When there is a problem in the translation, you get a compile error and the code does not run. Examples? (2) Instruction Error: Run time errors occur after the code has successfully complied and is being run. Examples? Jun Liang, Geography @ UNC
Fixing bugs (Cont.) Using the debug tools • Step Into button – when will you need this? • Toggle Breakpoint – you can also add a breakpoint by clicking in the code window’s margin. Either way, a red circle marks the spot and the line of code turn red. Please try exercise 8 for debugging experiences. Jun Liang, Geography @ UNC
Making your own objects How did you make objects in Avenue? Examples: aPointList = List.Make aFileName = FileName.Make(“$HOME/data/test.data”) aCircle = Circle.Make(aPoint, aRadius) In Visual Basic: Dim d1 As Dog Set d1 = New Dog d1.Name = “Sparky” d1.Bark Jun Liang, Geography @ UNC
Making your own objects (Cont.) How to create classes? In VBA, you create a new class by making a new class module. You can store the module in any of the three projects: the current map document, the normal template, or a base template. • Class created in the current map document – client side programmers will only be able to create objects from it. • If you want the class to b e available to any map document, you should store the class module in the normal project. Jun Liang, Geography @ UNC
Making your own objects (Cont.) To create a class, you need (exercise 9ab) • Create properties of this class • Create methods of this class Example: Public Value As Currency Public Zoning As String Public Function CalculateTax() …… End Function Jun Liang, Geography @ UNC
Programming with Interfaces Programming Interface – different from a user interface. An interface is a logical grouping of properties and methods for a class. It might be based on the level of generality or on some other similarity of use or purpose. Elephant IElephant TuskLength: Integer Trumpet Age: Integer Name: String Sleep IAnimal Jun Liang, Geography @ UNC
Programming with Interfaces (Cont.) • Interfaces are part of the Component Object Model (COM) • COM is a set of programming standards developed by Microsoft. • COM allows code written in one language (like visual basic) to work with code written in another language, like C++. • ArcObjects interfaces and classes are created with C++ • As you work with classes that have interfaces, you have to find out which interfaces a property or method is on before you use it. Jun Liang, Geography @ UNC
Programming with Interfaces (Cont.) Map IMap Layer: ILayer MapScale: Double Name: String AddLayer: ILayer MoveLayer: ILayer IActiveView Extent: IEnvelope ShowSelection: Boolean Dim pMap As IMap Set pMap = New Map pMap.Name = “Ryan’s Map” Jun Liang, Geography @ UNC
Programming with Interfaces (Cont.) Using IApplication and IDocument When you start ArcMap and open a map document, a couple of objects are already in use: • Application object : Application • MxDocument object : ThisDocument These two are the only predefined object variables there are. • All ArcObjects classes have interfaces. • All ArcObjects variables are pointer variables that point to interfaces. See page 152 for IApplication Please finish Exercise 10a on class. Jun Liang, Geography @ UNC
Programming with Interfaces (Cont.) Using multiple interfaces • ArcObjects classes can have several interfaces • You can work with multiple interfaces pointing to multiple classes • You may also want to work with different interfaces from the same variable. Check examples on Page 159 Learn how you can define different interfaces which point to the same object. Please finish exercise 10b Jun Liang, Geography @ UNC