migrating from vba to .net desktop add-insmaps.geog.pdx.edu/bagis/downloads/gia_2011_v2.pdfmigrating...

30
Lesley Bross Portland State University [email protected] March 29, 2011 Migrating from VBA to .NET Desktop Add-ins

Upload: others

Post on 15-Mar-2020

23 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Lesley Bross Portland State University

[email protected] March 29, 2011

Migrating from VBA to .NET Desktop Add-ins

Page 2: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Project background

• CSAR engaged in a multi-year agreement with the USDA-NRCS, National Weather and Climate Council (NWCC) to develop a spatial decision support system (SDSS)

• The new SDSS is intended to replace the USGS GIS Weasel tool used by NWCC to improve water forecast accuracy

The project was funded by USDA-NRCS National Water and Climate Center (CESU Agreement #: 68-7482-10-514)

Page 3: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

More background

• GIS Weasel’s last release was in 2003

• The GIS Weasel is used to:

1) Prepare watershed GIS layers for Areas Of Interest (AOI)

2) Delineate Hydrological Response Units (HRU)

3) Generate HRU physical parameters that are used in hydrological models to forecast water supply

• Basin Analysis GIS (BAGIS) phase completed 2009-2010 (See Dr. Duh’s session tomorrow at 1:30p)

• HRU Delineation phase began summer 2010

Page 4: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Our conundrum

1) VBA is going away

But we want to be able to use all that VBA code we just finished writing for the AOI definition phase …

2) Project requirements

a) Users are accustomed to using ArcMap in their workflow and wish to continue doing so

b) Solution is easy to share (e-mail an .mxt)

c) Installation cannot modify the Windows registry (security)

d) Simple development framework

Page 5: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

What about .NET add-ins ?

1) Install as plug-in to ArcMap; No Windows installation required

2) Single file - simple to distribute

a) E-mail attachment

b) Well-known folder (network location)

Page 6: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

What about .NET add-ins ?

3) .NET add-in Development framework is robust and relatively easy-to-use

a) Wizards assist with adding components

b) No need to write COM interfaces

c) Benefits of .NET development environment (Intellisense, debugging, unit testing)

d) Access to entire library of VB .NET form controls (DataGridView, Tooltip, etc.)

Page 7: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Development environment

• ArcMap 10.0 desktop client

• Microsoft IDE (Integrated Development Environment)

a) Visual Basic 2008 Express (free) http://www.microsoft.com/express/downloads/

b) Visual Studio 2010 Professional (free for 90 days with registration)

• .NET framework: installed by default with Microsoft IDE

• ArcObjects 10 SDK

a) Available on ArcMap Install CD

b) May also want to install VBA support

Page 8: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

IDE Express vs. Pro - ArcGIS Templates

• Packaging: Packaging for COM (.dll) deployment; Missing from Express but not needed for add-ins

• ArcGIS Snippet Finder doesn’t work as documented in Express

• Screenshots in documentation aren’t quite the same in Express

• Debugger configured manually in Express (ArcGIS templates can cover this)

Page 9: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

C# or VB .NET ?

• Familiarity with Visual Basic from using VBA made VB .NET an easier transition

• VB .NET provided a migration path for our existing VBA scripts

• Industry trends towards C# - ESRI Dev Summit

Page 10: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Converting VBA code to use in .NET

1) Export forms and code modules from inside the VBA editor. Forms cannot be converted but their associated code can be exported.

Page 11: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Converting – step 2

2) Dust off an old copy of Visual Basic 6 and create a new project. Add your exported VBA files to this project. “Washing” the code through VB6 allows you to run the VB upgrade wizard.

Page 12: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Converting – step 3

3) Open the VB 6 project in Visual Basic 2008. The .NET version of VB will recognize the VB 6 project and volunteer to upgrade it to Visual Basic 2008.

Page 13: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Converting – step 4

4) Create a new Visual Basic 2008 ArcMap Add-In project per ESRI guidelines

Page 14: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Converting – step 5

5) Recreate the forms from the VBA project. Note: using the same field names results in less work when migrating the code. The add-in framework supports both ESRI DockableWindow and Windows Form objects for the UI.

Page 15: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Converting – step 6

6) Copy and paste the code from the converted project to the new project. Clean up warnings and review comments added by the conversion routine

Page 16: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Language differences

• There are lots! (see references at end)

• Application: Application -> My.ArcMap.Application

• Document: ThisDocument -> My.ArcMap.Document

• Exception handling: On Error GoTo / On Error Resume Next -> Try Catch Finally and throwing exceptions

Page 17: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Our solution

1) Leave BAGIS (phase 1) tools in VBA with their toolbar

2) Develop HRU delineation tools on a separate toolbar with VB .NET

3) Convert re-usable functions from VBA to VB .NET using aforementioned process

4) Forms were not converted

Page 18: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Introducing HDGIS

Page 19: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Unit Testing

Page 20: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

What is unit testing?

1) Testing individual pieces/modules of code (functions or subs)

2) Provide known inputs and check output for expected results; Replaces user interface

3) NUnit: open source testing framework for .NET; Integrates with VB Express/Visual Studio. It’s free!

Page 21: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Using NUnit

1) Download/install .exe from the NUnit website

2) Create a project separate from ArcObjects project; No templates

3) Add nunit.framework to project assemblies

4) Set NUnit as debug application; You can step through code while running Nunit tests

5) NUnit debug configuration will vary depending on IDE

Page 22: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

NUnit organization

1) Testing subs are in separate module from code they are testing

2) Testing subs are stored in test folder

3) Test data may also live in test project

4) Test project separate from Add-In project

5) Code modules are copied from test project to Add-In project when are validated

6) No testing subs/data in Add-In project; No forms in test project

Page 23: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

TextFixtureSetUp

• Runs before tests are conducted; Initializes ArcMap

Page 24: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

A unit test

Page 25: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

TextFixtureTearDown

• Runs when tests are complete; Shutdown ArcMap

Page 26: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Successful test

Page 27: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

Failed tests

Page 28: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

References

• Add-ins for ArcGIS Desktop 10 http://blogs.esri.com/Dev/blogs/arcgisdesktop/archive/2010/05/05/Add_2D00_ins-for-ArcGIS-Desktop-10.aspx

• ArcGIS Visual Studio IDE Integration Framework for extending ArcObjects: http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//00010000021w000000

• GEOG 490/590: GIS Programming: http://web.pdx.edu/~jduh/courses/geog490w11/index.htm

• Migrating VBA/VB6 ArcObjects Applications to .NET: http://proceedings.esri.com/library/userconf/devsummit08/papers/migrating_vba-vb6_arcobjects_applications_to_net.pdf

Page 29: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

References

• Nunit: http://www.nunit.org/index.php?p=home

• The GIS Weasel!! http://wwwbrr.cr.usgs.gov/projects/SW_MoWS/software/weasel_s/weasel.shtml

• Walkthrough: Building custom UI elements using add-ins: http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Walkthrough_Building_custom_UI_elements_using_add_ins/0001000001ms000000/

Page 30: Migrating from VBA to .NET Desktop Add-insmaps.geog.pdx.edu/BAGIS/downloads/GIA_2011_v2.pdfMigrating from VBA to .NET Desktop Add-ins . Project background •CSAR engaged in a multi-year

For more information

• Slides available on slideshare.net: http://tinyurl.com/5to9yxo

• E-mail: [email protected]