Presentation is loading. Please wait.

Presentation is loading. Please wait.

ArcObject. Chapter 9 3 Kiểu thực thể - Tập thực thể - Thực thể.

Similar presentations


Presentation on theme: "ArcObject. Chapter 9 3 Kiểu thực thể - Tập thực thể - Thực thể."— Presentation transcript:

1 ArcObject

2 Chapter 9

3 3 Kiểu thực thể - Tập thực thể - Thực thể.

4 Exercise 9a: Đặt vấn đề As a programmer for a wildife conservation (bảo tồn) project, you work with biologists (sinh học) who observe elephants. Currently, the scientist are notebooks to keep track of their elephants. They also record each elephant’s trumpeting sound in a wav file. Your job is to create an Arcmap environment for their work. Sine elephant object don’t exist in VBA or ArcGIS, you will program a new elephant class. The biologists want to store and retrieve elephant names and ages. They also want to be able to hear an elephant’s trumpet sound. You can use that information to make a UML diagram of the elephant class.

5 Exercise 9a: Đặt vấn đề In this exercise, you will do the service-side programming to make an elephant class based on the diagram. Your elephant class will consist of two variables and a procedure that plays a sound file. In the next exercise, you will do the client-side programming and write code to create elephant objects out of the class and use their properties and methods.

6 Exercise 9: 1.Start Arcmap and open ex09a.mxd in the C:\ArcObjects\Chapter09 foder. The map is empty because the elephant class you are about to create won’t be linked to any geographic location. In later chapers, you will learn how to create geopraphic features line points, lines, and polygons, and assign their geopraphic coordinates and attribute values 2. Click the tools menu, point the macros, and click Visual Basic Editor

7 Exercise 9: 3.In the project window, right-click Project(ex09a.mxd), point to insert, and click class module Class 1 opens and looks just like any other code module. This one will store your elephant class and the code for its properties and methods. 4.In the properties window, for the name property, replace class 1 with elephant. Press Enter. The elephant class is create. Now you will add its properties.

8 Exercise 9: 5. In the elephant class module, add the following two lines of code to declare age and name variables. Option Explicit Public Age As Integer Public Name As String Notice that you do not write code to set these varibles. That’s because they will be set by client-side programmers(including you) who make elephant objects out of your elephant class. For exemple, after creating a new elephant, a client-side programmer might write. myElephant.Name = “Benny “ or myElephant.Age = 10

9 Exercise 9: Now that the properties are done, you will code the trumper method. 6. With the elephant code module active, click the insert menu and click procedure. 7. In the add procedure dialog box, type trumpet as the name. Make sure the Type is set to Sub and the Scope to Public. If the trumper method were going to return a value, like a decibel level, you would make it a function. Since it just plays a sound file, you will make it a subroutine.

10 Exercise 9: 9. Inside the Trumpet subroutine, add the following code. (If you have installed the data at the different location, you will need to modify the path accordingly.) sndPlaySound “C:\ArcObjects\Data\elephant.wav”, SND_ASYNC

11 Exercise 9: This code is a bit different from anything you’ve seen before. The sndPlaySound function does not come from either VBA or ArcObjects, but is a Microsoft Windows Application Programming Interface (API) function. API functions carry out operating system operations like playing sound files, finding out who is logged on to a computer, or retrieving the path to the Temp folder. To call an API function in VBA, you first have to declare it in another code module. (You did this in chapter 6 when you declared the Kilogram ToPound function has already been declared for you in the standard module called PlaySounds.

12 Exercise 9: The sndPlaySound function has two arguments: the first is a path to a.wav sound file, and the second is a constant, which can be either SND_SYNC or SND_ASYNC. * The SND_SYNC option pauses the code until the sound fie has finished playing. * The SND_ASYNC option lets more code run while the sound file plays. Functions return a value, but you don’t always have to use it. The sndSoundPlay function returns True if the sound plays and False if it doesn’t. To keep the Elephant code simple, you aren’t going to use the returned value. To learn more about the sndPlaySound function andits options, go to msdn.microsoft.com and the search for sndPlaySound. Your elephant class is now complete. Granted, it’s a pretty simple class, but it relies on the same coding principles you would use to create more robust classes. You are now ready to make lephant objects.

13 Exercise 9: 10. If you want to save your word, click the File menu in ArcMap and click Save As. Navigate to C:\ArcObjects\Chapter09. Rename thefile my_ex09a.mxd and click Save. If you are continuing with the next exercise,leave ArcMap open. Otherwise close it.

14 Chapter 9: Creating objects Up to now, you have created objects with the aid of a user interface. For example, in chapter 3, you created a new form object from the Insert menu, and you then created CommandButton objects by dragging them from the Visual Basic Editor Toolbox onto the form. Now you will create objects with code, by declaring and setting variables. Variables represent not only basic data types like numbers, dates, and strings, but can also represent objects.

15 Chapter 9: Creating objects Variables that represent basic data types are called intrinsic variables. You declare and set them with code like the following: Dim X As Integer X = 365 Then you use them Msgbox X

16 Chapter 9: Creating objects Variables that represent objects are called object variables. These variables are also declared and set, but the code is a little different. When you declare an object variable, you use the class name as a data type. For an elephant object, you would declare the variables as Elephant. Dim E As Elephant The line of code to set an object variables begins with the Set keyword. If you are creating a new object, you also use the New keyword between the equals sign and the class name. So to create a new elephant object, you would write the following line of code: Set E = New Elephant

17 Chapter 9: Creating objects Now that you have an object variable called E, which refers to a new elephant, you can use the variable to set the elephant’s properties and run its methods. E.Name = “Mark” E.Trumpet

18 Chapter 9: Creating objects Then, if you want, you can create some more elephants. Dim E1 As Elephant Dim E2 As Elephant Set E1 = New Elephant Set E2 = New Elephant After creating them, you can set their properties so that each one is unique. E1.name = “jerry” E1.age = 24 E2.name = “ron” E2.age = “ron”

19 Exercise 9b: Đặt vấn đề In the previous exercise, you made an elephant class and programmed its properties and methods. In this exercise, you will make new objects out of this class and you their properties and methods.

20 Exercise 9b: 1. Start arcmap and open ex09b.mxd in the c:/arcobjects/chapter09 folder. On the standard toolbar you see the addelephant button next to and data button. 2. Right-click the add elephant button and click view source. You see the addelephant button’s empty click event. You will write code there to create elephant objects from the elephant class.

21 Exercise 9b: 3. In the this document code module, in the addelephant click event,add the following line of code to declare an object variable. Dim theElephant as elephant 4. Add the following line of code to create a new elephant object. Set theElephant = new elephant After the dim and set lines run, a new object is in your computer’s memory and you have a variable (theElephant) to refer to it. 5. Type theElephant. (including the dot). Your elephant class is recognized by VBA as a full-fledged class. As with any orther object variable, after you type the dot you see the drop-down list of its properties and methods.

22 Exercise 9b: Next,you will write code to set this new elephant’s properties. 6. Finish the line of code you started above by using an input box to set the elephant’s name property. theElephant.name =inputbox(“enter name:”) 7. Then set the elephant’s age property theElephant.age = inputbox(“inter age”) When these two lines run,input boxes appear to your biologist-users

23 Exercise 9b: Next,you will write a line of code that displays the values back to the user. 8. Add a line of code to display the new elephant’s name and age. In the message box, the elephant’s name will be on the top line. vbCrLf (carriage return line feed) will put the elephant’s age on the second line. Msgbox “name: “& theElephant.name & vbCrLf &_ “age: “& theElephant.age,_ Vbinformation,”add elephant” 9. At the end of the addEleplant procedure, just before the End Sud, add the following line of code to run the Truimpet method. TheElephant.Trumpet

24 Exercise 9b: Now you will test the AddEleplant button. 10. Close Visual Basic Editor. 11. On the Arcmap Standard toolbar, click AddElephant. 12. In the first input box,type jack for the name. Click Ok 13. in the second input box type 35 for the age. Click Ok. You see the new elephant’s data display. 14. Click Ok on the Add Elephant message box. After clicking ok, you hear the elephant trumpet. If you don’t hear the trumpet, check your computer’s volume, speakers, and sound card. 15. If you want to save your word, click the File menu in ArcMap and click Save As. Navigate to C:\ArcObjects\Chapter09. Rename the file my_ ex09b.mxb and click save. If you are continuing with the next chapter, leave ArcMap open. Otherwise close it.


Download ppt "ArcObject. Chapter 9 3 Kiểu thực thể - Tập thực thể - Thực thể."

Similar presentations


Ads by Google