500 likes | 664 Views
ESRI EUROPEAN USER CONFERENCE. Customizing ArcInfo 8 with VBA. Overview. Customization options VBA in ArcInfo 8 Introducing CO M Object model Example tasks in VBA. U Customization options. Customization options. AML ODE VBA scripts Using COM objects
E N D
ESRI EUROPEAN USER CONFERENCE Customizing ArcInfo 8 with VBA
Overview • Customization options • VBA in ArcInfo 8 • Introducing COM • Object model • Example tasks in VBA
Customization options • AML • ODE • VBA scripts • Using COM objects • Extending the application • Creating COM objects • Use any COM compliant language Customization options
Comparing customizations • VBA • Customizations are document based • Less code • Not compiled • Integrated debugging • Third party language • Create COM objects that plug into ArcInfo • More code • Compiled Customization options
The development environment • VBA Editor • Visual Basic programming VBA in ArcInfo 8
Storing customizations • Customization context • ArcMap has 3 levels of storage • ArcCatalog only uses a Normal Template VBA in ArcInfo 8
This Document Base Template Normal Template The VBA project explorer VBA in ArcInfo 8
Running macros • Code can be added to Modules or Documents as Pocedures • A macro is a VBA Procedure • Test run a macro from the VBA Editor or the Macros dialog (Tools/Macros/Macros) VBA in ArcInfo 8
Add a macro to a toolbar • Drag a macro onto a toolbar or menu • The macro becomes a button on the toolbar • Edit the control’s properties (image, etc.) • Define UIControls and use the Properties and Events for storing macros VBA in ArcInfo 8
Document Events • Perform some actions when certain things occur in the document • Write code in Project.ThisDocument VBA in ArcInfo 8
ArcMap (MxDocument) ActiveViewChanged BeforeCloseDocument CloseDocument MapsChanged NewDocument OnContextMenu OpenDocument ArcCatalog (GxDocument) ActiveViewChanged CloseDocument NewDocument OnContextMenu OpenDocument Document Events VBA in ArcInfo 8
User Forms • VB Forms created in the VBA Editor • Can be called from your macros • Used for display or input of data VBA in ArcInfo 8
Creating Templates • Create a new document • Make your customizations • Save As template • New documents can now easily load your customizations VBA in ArcInfo 8
Introducing COM • Component Object Model • Binary way for objects to communicate • Technology behind Object Linking and Embedding (OLE) • Active X • Any technology built on COM Introducing COM
Extensibility • Extensibility • Create objects that other applications can use (e.g. ArcMap) • Use any COM compliant language Introducing COM
Integration • Object Linking and Embedding • Can integrate ESRI objects into third party applications • Can integrate third party objects into ESRI applications • VBA Introducing COM
Working with objects • Objects • Methods • Properties Introducing COM
VB object example • pDog is a reference to the entire object • Dim declares the variable • Use Set when setting a variable to an object • Use New to create a new object Dim pDog As Dog Set pDog = New Dog pDog.Bark DOG Bark Name Color Growl CallPet Introducing COM
COM object interfaces • Define methods and properties • Logical group • An object can have one or more interfaces • The only way to communicate with an object is through its interfaces Introducing COM
DOG IDog Color Bark IPet Name Growl CallPet COM objects • Objects • Interfaces • Methods • Properties Introducing COM
DOG IDog Color Bark IPet Name Growl CallPet VB COM object example • pDog is a reference to the IDog interface of the DOG object Dim pDog As IDog Set pDog = New Dog pDog.Bark Introducing COM
DOG IDog Color Bark IPet Name Growl CallPet Getting a different interface • QueryInterface - QI • pPet holds a new interface to the existing dog object • pDog still holds the IDog interface Dim pDog As IDog Set pDog = New Dog pDog.Bark Dim pPet As IPet Set pPet = pDog pPet.CallPet Introducing COM
DOG IDog Color Bark CAT IPet Name Growl Color ICat CallPet Meow Purr IPet Name CallPet Polymorphism • Multiple objects can support the same interface • The object implements the methods and properties Introducing COM
DOG IDog Color Bark CAT IPet Name Growl Color ICat CallPet Meow Purr IPet Name CallPet Multiple objects - VB Example Dim pDog As IDog Dim pCat As ICat Set pDog = New Dog Set pCat = New Cat Dim pPet As IPet Set pPet = pDog pPet.CallPet Set pPet = pCat pPet.CallPet Introducing COM
Object diagrams • Define the types of objects • CoClass • Class • Abstract Class • Help define which interfaces are on an object • Show relationships between objects • Which objects can be created • How objects are created Object model
IMap Map IActiveView CoClass • Instantiable and Creatable • Can create with the New keyword • Shaded and 3D Dim pMap As IMap Set pMap = New Map Object model
IFeature Feature IFeatureBuffer Class • Instantiable but not creatable • Can’t be created with the New Keyword • Created from other objects • No shade and 3D Object model
ILayer Layer Abstract class • Not instantiable and not creatable • Holds interfaces • Shaded and 2D Object model
Class Relationships • “Is a type of” • “Has a” • “Composed of” • “Creates a” Object model
ILayer Layer IFeatureLayer FeatureLayer IGeoDataset “Is a type of” relationship • Interfaces on the parent are available on the child Object model
IActiveViewEvents Legend ILegend IClone LegendFormat ILegendFormat “Has A” relationship • “Has A” objects are not destroyed with the parent Object model
ILayer Layer IMap Map * IActiveView “Composed of” relationship • “Composed of” objects are destroyed with the parent Object model
FeatureClass IGeoDataset Feature IFeature IFeatureClass IFeatureBuffer “Creates A” Relationship Object model
Class diagram key Object model
Example tasks • Change the message in the Status Bar • Change the extent to a selected layer • Executing an existing command Example tasks in VBA
Change the message on the status bar • Use the IApplication interface of the Application object • Top level object • Contains a StatusBar object • Application variable is preset to IApplication Application.Statusbar.Message(0) = "ArcMap is great" Example tasks in VBA
Change the extent to a selected layer • Get the document object • Get the selected layer in the document • Set the extent of the map to the extent of the layer Example tasks in VBA
IApplication IDocument IMap Application Document Map IMxApplication IMxDocument IActiveView Document • One document per application • ThisDocument variable • A document • Contains one or more maps * Example tasks in VBA
IMap Map IActiveView Maps and layers • A map can have 0 or more layers • Types of layers • Feature, Graphics, Annotation, Image, Group * ILayer Layer IFeatureLayer FeatureLayer IGeoDataset Example tasks in VBA
IMxDocument • IMxDocument • Operations specific to ArcMap documents Dim pMxDoc As IMxDocument Set pMxDoc = ThisDocument Example tasks in VBA
IMxDocument properties • IMxDocument.ActiveView returns IActiveView • Of map in Geographic view • Of PageLayout in page view • IMxDocument.SelectedLayer returns the selected layer in the TOC Example tasks in VBA
Get to the extent • IActiveView.Extent sets / gets the extent of the map • IGeoDataset.Extent gets the extent of the layer Dim pMxDoc As IMxDocument Set pMxDoc = ThisDocument Dim pGeoDataset As IGeoDataset Set pGeoDataset = pMxDoc.SelectedLayer pMxDoc.ActiveView.Extent = pGeoDataset.Extent pMxDoc.ActiveView.Refresh Example tasks in VBA
Executing an existing command • Get a commanditem from the commandbars collection • Use ArcID • Execute the commanditem Example tasks in VBA
ICommandItem CommandBars • Collection of Menus and toolbars • Contain Commands, Macros, UI Controls • CommandBars.Find(UID) returns ICommandItem • UID (Unique Identifier) for all COM objects ICommandItem CommandItem * ICommandBar CommandBar Example tasks in VBA
Executing a command • ArcID module returns the UID for all ESRI CommandItems • CommandItems are organized in ArcID by Category_Name Dim pCommandItem As ICommandItem Set pCommandItem = CommandBars.Find(ArcID.File_AddData) pCommandItem.Execute Example tasks in VBA