My Windows XP recently decided to fall apart, so after a few days reinstalling everything I decided it would be a good time to switch from ArcGIS 9.1 to 9.2. The installation went smoothly enough, and after a few more hours I had Visual Studio 2005 up and running as well. With some trepidation I opened up my largest ArcGIS VB solution…1030 errors, let alone warnings! The solution had been developed for ArcGIS 9.1 so I had expected some issues..

After some investigation it became apparent the cause of most errors were:

1. None of the ESRI 9.1 DLLs that my projects referenced were present on my machine. These had all been updated to 9.2

2. The ESRI.ArcGIS.Utility library has been deprecated, and its existing functionality moved to the ESRI.ArcGIS.ADF library.

With regards to the first issue all the libraries still had the same name, but were different versions. Changing the “Specific Name” property of the reference allowed VS to find the library and removed the error. I must have several 100 of these references for several projects so I decided to try out the VS macros to automate this task. To edit and create macros manually go to Tools >> Macros > Macros IDE in Visual Studio (2005). All the subs in this post were created in the same module, and require the following references listed below. Some of these had to be added manually to the “MyMacros” project via the References in the Macros IDE Project Explorer. The VSLangProj80 and VSLangProj2 libraries are in C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PublicAssemblies\

    1 Option Strict On

    2 Option Explicit On

    3 

    4 Imports EnvDTE

    5 Imports VSLangProj

    6 Imports VSLangProj2

    7 Imports VSLangProj80

The following procedure loops through every project in the solution, and then through every reference in the project. If the reference starts with the text ESRI then the “Specific Version” property is set to false.

    2 

    3         ‘http://msdn2.microsoft.com/en-us/library/vslangproj80.reference3.specificversion(VS.80).aspx

    4 

    5         Dim projectItem As ProjectItem

    6         Dim myCodeProject As VSLangProj.VSProject

    7         Dim proj As Project

    8         Dim ref As Reference3

    9 

   10         For Each proj In DTE.Solution.Projects

   11             If  TypeOf  proj.Object Is VSLangProj.VSProject  Then ‘loop through projects

   12                 myCodeProject = CType(proj.Object, VSProject)

   13                 For Each ref In myCodeProject.References

   14                     If ref.Name Like “ESRI.*” Then

   15                         ref.SpecificVersion = False

   16                     End If

   17                 Next

   18             End If

   19         Next

   20 

   21     End Sub

This greatly reduced the number of errors, and was so satisfying I continued playing around with macros and the macro recorder available in VS. OK it may have taken longer than doing all this manually..but where is the fun in that..

The following macros work as follows:

UpdateReferences – runs all the sub macros, passing parameters where appropriate.

ReplaceReference – removes a reference tat is no longer needed, and automatically adds in the new reference. In this example I use it to replace all references (sic) to the deprecated ESRI.ArcGIS.Utility reference with the new ESRI.ArcGIS.ADF reference.

ReplaceNameSpaces – this opens the find / replace dialog with parameters already filled. I commented out the automated execution of this replacement as I sometimes received dialogs while replacing that cause the macro to crash. In this example I replaced all Import declarations with the new ESRI.ArcGIS.ADF reference.

   35     Sub UpdateReferences()

   36 

   37         ChangeSpecificVersions()

   38         ReplaceNameSpaces(“ESRI.ArcGIS.Utility”, “ESRI.ArcGIS.ADF”)

   39         ReplaceReference(“ESRI.ArcGIS.Utility”, “C:\Program Files\ArcGIS\DotNet\ESRI.ArcGIS.ADF.dll”)

   40 

   41     End Sub

   42     Sub ReplaceReference(ByVal strOrigRefName As String, ByVal strNewRefPath As String)

   43 

   44 

   45         Dim projectItem As ProjectItem

   46         Dim myCodeProject As VSLangProj.VSProject

   47         Dim proj As Project

   48         Dim ref As Reference3

   49 

   50         For Each proj In DTE.Solution.Projects

   51             If TypeOf proj.Object Is VSLangProj.VSProject Then ‘loop through projects

   52                 myCodeProject = CType(proj.Object, VSProject)

   53                 For Each ref In myCodeProject.References

   54                     If ref.Name = strOrigRefName Then

   55                         ref.Remove()

   56                         myCodeProject.References.Add(strNewRefPath)

   57                     End If

   58                 Next

   59             End If

   60         Next

   61 

   62     End Sub

   63 

   64     Sub ReplaceNameSpaces(ByVal strOldNameSpace As String, ByVal strNewNameSpace As String)

   65 

   66         DTE.ExecuteCommand(“Edit.Find”)

   67         DTE.ExecuteCommand(“Edit.SwitchtoReplaceInFiles”)

   68         DTE.Find.Target = vsFindTarget.vsFindTargetFiles

   69         DTE.Find.FindWhat = strOldNameSpace

   70         DTE.Find.ReplaceWith = strNewNameSpace

   71         DTE.Find.MatchCase = False

   72         DTE.Find.MatchWholeWord = True

   73         DTE.Find.MatchInHiddenText = False

   74         DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral

   75         DTE.Find.SearchPath = “Entire Solution”

   76         DTE.Find.SearchSubfolders = True

   77         DTE.Find.KeepModifiedDocumentsOpen = False

   78         DTE.Find.FilesOfType = “*.vb”

   79         DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1

   80         DTE.Find.Action = vsFindAction.vsFindActionReplaceAll

   81         ‘If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then

   82         ‘Throw New System.Exception(“vsFindResultNotFound”)

   83         ‘End If

   84         ‘DTE.Windows.Item(“{CF2DDC32-8CAD-11D2-9302-005345000000}”).Close()

   85 

   86     End Sub

 If only Visual Studio would open in the same state that it was closed..

Excluded DLLs in set up packages seem to re-include themselves, projects in solutions disappear, and today an ArcObects command seemed to have disappeared.

All had previously been running without problems in debugging mode, opening ArcMap and then stopping at various breakpoints. However today no breakpoints were hit. Not even on the OnCreate or New functions of the BaseCommand. The button in ArcMap had disappeared. Adding it manually from the .tlb gave the “No New Objects Added” message.

Some time later after much blood, sweat, (and possibly tears), I delved into the registry. I searched for the class ID in key values and names. The class ID can be found in the class code file of the tool:

Public ConstClassId As String = “480d624c-3d4c-4210-ae11-0c775dddc35d”

Then I took the rather risky step of deleting all these keys. If anyone does the same I’d recommend making a backup of the registry first – not that I did myself..
Anyway I think the class must have been registered twice, or there was an error in the registry, as the next time I started the project with debugging everything had started working again =)

A few sample ArcToolbox commands I’ve been using recently. These can be run from the command line available in ArcCatalog (in the menu select Window >> Command Line).

workspace C:\MyPath\MyGeoDatabase.mdb
Intersect (featureClass1”;’featureDataset1\featureClass2′ ”) my_output_featureClass ALL # INPUT

This automates the Intersect tool, found in ArcToolbox under “Analysis Tools >> Overlay >> Intersect” and is useful if this is a process that has to be run several times, and can be part of a batch process. The first line sets the workspace to a geodatabase containing the two input classes, and the second line runs the intersect, outputting all fields to my_output_featureClass.

workspace C:\MyPath\MyGeoDatabase.mdb
delete myFeatureClass

This command deletes a feature class in the geodatabase.

More samples can be found in the ArcGIS Desktop Help under the “Geoprocessing tool reference” section.

commandline

A common requirement in many user dialogs is to display a list of unique values from a table, in order to delete or select records. While it is often easier to use Microsoft’s data objects, I try to use ArcObjects as the relevant libraries will always be installed on a user’s machine. The function will normally part of a larger project and I don’t like mixing the two methods to retrieve values from tables. I usually reuse a custom class for dealing with different types of geodatabases so that if a client changes from Access to SQL Server code changes will be minimal. Keeping up with changes to ESRI’s data access objects and Microsoft’s in the same project could get nasty..

There is a VBA / VB6 sample on how to get unique values at the EDN Site - but no .NET equivalent, hence my code sample below. There are no major changes except now a standard System.Collections enumerator is used rather than the ESRI IEnumVariantSimple.

    Public Sub ListUniqueRecords()

 

        Dim pMyTable As ITable

        Dim pCurs As ICursor = Nothing

        Dim intFieldIdx As Integer

        Dim pDataStatistics As IDataStatistics

        Dim pEnumVar As IEnumerator

        Dim pWorkspaceFactory As IWorkspaceFactory

        Dim pWorkspace As IWorkspace

        Dim pFeatWorkSpace As IFeatureWorkspace

        Dim strMyField As String = “VAL”

 

        Try

            pWorkspaceFactory = New AccessWorkspaceFactory

            pWorkspace = pWorkspaceFactory.OpenFromFile(“C:\MyPath\MyGDB.mdb”, 0)

            pFeatWorkSpace = CType(pWorkspace, IFeatureWorkspace)

            pMyTable = pFeatWorkSpace.OpenTable(“MyTableName”)

            intFieldIdx = pMyTable.FindField(strMyField)

 

            pCurs = pMyTable.Search(Nothing, True)

            pDataStatistics = New DataStatistics

            pDataStatistics.Field = strMyField

            pDataStatistics.Cursor = pCurs

 

            pEnumVar = CType(pDataStatistics.UniqueValues, IEnumerator)

 

            Do Until pEnumVar.MoveNext = False

                Debug.Print(pEnumVar.Current.ToString)

            Loop

 

        Catch ex As Exception

            Trace.WriteLine(ex.ToString)

        Finally

            ‘clean up

            pCurs = Nothing

            pWorkspace = Nothing

        End Try

 

    End Sub