migrating your vba applications to desktop add-ins · 2012. 7. 26. · technical workshops | esri...
TRANSCRIPT
Technical Workshops |
Esri International User Conference San Diego, California
Migrating your VBA applications to Desktop Add-ins
John Hauck, Chris Fox
July 26, 2012
• What are add-ins? • Introduction to .NET and Visual Studio • Converting VBA Code
Agenda
What are Add-ins?
• New way to extend ArcGIS Desktop Applications - Collection of tools on a toolbar - Perform action in response to application event
• Easy to build, install, and share - No Component Object Model (COM) registration - Authored in .NET, Java, or Python (new at 10.1)
ArcGIS Desktop Add-Ins
• Buttons and Tools • Toolbars • Menus • Construction Tools • Tool Palettes • Combo Boxes • Multi-Items • Context Menus • Dockable Windows • Extensions
Supported Add-In Types
Add-in Wizard
Add-in Contents
Resources
Add-in file (.esriaddin) AAddddddddd-iiiiinnnn fffffffiiiiiiillllllleeeee
( i ddi )
Assemblies/ JARs XML
Metadata
• No registration required - Installed by copying to a well-known folder - Uninstalled by deleting
• Single file
- Upload to arcgis.com- Attach to e-mail - Place on network file share
Easy to Install and Share
• Add-ins built for previous version will load automatically in newer versions
- No need to rebuild or reinstall
• Add-ins are not backwards compatible
Compatibility with Newer Versions
Add-in Installation
Introduction to .NET and Visual Studio
• Development framework to build applications for the Windows Platform
• Key Features - Common Language Runtime - Interoperability - Base Class Library - Windows Forms
What is .NET?
• Coded solutions to common programming problems - Interacting with databases - Parsing XML - File reading and writing - String manipulation - Working with collections
Base Class Library
• Tools and Controls to build GUIs - Toolbars and Menus - Text Box, Combo Box, List View, Button - Layout and Data Binding
• Events driven
Windows Forms
• When building your add-in you need to target a specific version of .NET
• 3.5 is recommended for 10 and 10.1 - .NET 3.5 is required for ArcGIS Desktop
• 4.0 is supported at 10.1
Supported .NET Versions
• VB.NET - Syntax closest to VBA and VB6
• C# - Syntax similar to C++ & Java
• No difference in performance • Same access to .NET BCL and ArcObjects API
Selecting a Language
Where to write your code?
• Integrated Development Environment (IDE) - Source code editor - Debugger
• Visual Studio
- Visual Studio 2008 (10) and 2010 (10* and 10.1) - Free express version is supported
- *2010 VS Express not supported at 10.0
• One IDE for all .NET projects - Templates
• Code Editor - Syntax highlighting, code completion
• Debugger • Windows Forms Designer
- Drag/Drop controls
Visual Studio Features
• Project Templates and Wizards - Template for each Desktop Application - Add-in Wizard helps you declare components
• ArcGIS Snippet Finder - Code samples for common workflows
Esri’s Visual Studio integration
• Ensure the following are installed: - ArcGIS Desktop - Visual Studio - ArcObjects SDK for the Microsoft .NET Framework
- Order is important, VS before the SDK
Before you get started…
• Open Visual Studio and go to File > New Project
Creating a New Add-In Project
Define Information About Add-In
Define Add-In Components
Creating your Add-in
Converting VBA Code
• The business logic of your application is already written - No need to change the logic
• The ArcObjects interfaces and classes you work with are
the same in VBA and .NET
The hard part is already done
• VB.NET syntax is very similar - A lot of code will just work
• Some errors will be automatically fixed - SET keyword not used to instantiate variable
• Other errors will be highlighted for you
Copy/Paste VBA Code
• Add-In Wizard creates static classes depending on your type of customization.
- Entry point to Application and Document
• Replace references to ThisDocument and Application
- ex. My.ArcMap.Document, My.ArcMap.Application
Entry Points for Customization
• Unreferenced interfaces and classes are highlighted
• Right-Click Project > Add Reference > .NET tab - Find correct library from SDK help
• Use Imports to resolve name w/o being fully qualified - IMxDocument vs. ESRI.ArcGIS.ArcMapUI.IMxDocument
Add Namespace References
• Other common errors - Migrate Win32 API calls to .NET Framework libraries
- File I/O: CopyFile vs. System.IO.File.Copy - http://msdn.microsoft.com/en-us/library/aa302340.aspx
- Not all types are the same, key differences:
Resolve Remaining Errors
VBA VB.NET Variant/Object Object Currency Decimal Integer Short Long Integer
• Build > Build Solution - Need to resolve all errors before build will complete
• Creates .esriaddin file and installs it - Add-in file created in bin folder - This is the only file that needs to be distributed
Building Your Add-in
Add-in file (.esriaddin) AAddddddddd-iiiiinnnn fffffffiiiiiiillllllleeeee
( i ddi )
• Add Breakpoints to the code • Debug > Start Debugging
- Will launch the Desktop Application
• Interact with the add-in in the Application
Debugging
Building and Debugging
Steps to evaluate UC sessions
• My UC Homepage > “Evaluate Sessions”
• Choose session from planner OR
• Search for session
www.esri.com/ucsurveysessions
• Thank you for attending
• Have fun at UC2012
• Open for Questions
• Please fill out the evaluation:
www.esri.com/ucsessionsurveys
First Offering ID: 1033
Second Offering ID: 1034