image

This question was posed on the GIS Stack Exchange, and the replies include a number of interesting approaches. Below is another one.

Why not just use .NET?

I have several years worth of DLLs built using VB.NET and ArcObjects. Recently I’ve been using more and more open-source libraries, the majority of which can be manipulated using Python.

A major advantage of using a interpreted language (rather than one that needs to compile) like Python is that you can easily change paths and variables in a script.

You can deliver a folder of .NET ArcObjects DLLs, a sample Python script and an end user can automate their workflow themselves. This negates the requirement to build a UI on top of your code as a (Python familiar) user can script it themselves.

DLLs still have their place as ArcObjects includes many functions not available in the ESRI Python libraries. It is also easier to write and test ArcObjects code in Visual Studio with all the advantages of error checking and auto-completion. Continue reading »

An often required function is to get a specific layer from a current map document. The code below is for VB.NET and requires a map document and uses the alias name of the feature class. The first function returns all IGeoFeature layers in a map – these are layers based on vector geographic data. The second loops through these layers and compares the layer names.

Continue reading »

The following VBA function hides all on the geodatabase tables added by ESRI into an Access database. Useful if you spatially enable your users database, and they wonder what all those GDB_ tables are for..

GDB Tables

To unhide just change the Trues to False in the code snippet below, or add a parameter to the sub.
Continue reading »

It can be frustrating when a code sample that seems to do exactly what you want is written in C#, and your application is in VB.Net. http://www.kamalpatel.net/ConvertCSharp2VB.aspx solves this problem by converting the C# code directly into its VB.Net equivalent – all online and for free for code up to 6.15KB in size, and for larger amounts of code a tool can be downloaded for free.

It does the majority of the conversion well but I ran into a few minor problems where line breaks were not taken into account.

This tool will be even more useful when the ESRI samples, the majority of which are only available in VB6, are updated to .NET…

Locked!

Writing ArcObjects code and using an Access personal geodatabase will result sooner or later in an error along the lines of “Cannot aquire a schema lock because of an existing lock.” Spending hours going through code, making sure objects are disposed of and that all connections closed often makes very little difference. It becomes apparent some of the problems lie deeper than customised code when the same errors occur when using ArcMap or ArcCatalog without any modifications.

I have been using Unlocker, a freeware program, to solve these issues when developing with ArcObjects (or just using ArcMap..). Once installed a simple right click on an ldb or mdb file can remove any file locks, and your code is free to run again. It works with any Windows file or folder so is a useful program to have around even without geodatabases.

More reviews and an alternative download site can be found here.

Unlocker

A recent request by an ArcMap was user was how to add the current coordinates of the dataframe to the map layout in ArcMap. I messed around with grids and labelling for a while before resorting to a VBA script.

Coordinate Text

The code below should copied and pasted into the VBA Editor in ArcMap, and run when the current view is a page layout. The number of decimal places and distances of the text labels from the grid can be changed easily by modifying the relevant variable.

    1 Public Sub AddCornerText()

    2 

    3 Dim pMxDoc As IMxDocument

    4 Dim pActiveView As IActiveView

    5 Dim pLayoutExtent As IEnvelope

    6 Dim strText As String

    7 Dim pMapFrame As IFrameElement

    8 Dim pGraphic As IElement

    9 Dim dblOffset As Double

   10 Dim lngRound As Long

   11 

   12 ‘this is the offset used so the text does not overlap the map

   13 ‘modify according to needs

   14 dblOffset = 0.2

   15 

   16 ‘this is the number of decimal places used for each coordinate value

   17 lngRound = 0

   18 

   19 Set pMxDoc = ThisDocument

   20 Set pActiveView = pMxDoc.FocusMap

   21 

   22 ‘find the map data frame in the layout

   23 

   24 Set pMapFrame = pMxDoc.ActiveView.GraphicsContainer.FindFrame(pMxDoc.ActiveView.FocusMap)

   25 Set pGraphic = pMapFrame

   26 Set pLayoutExtent = pGraphic.Geometry.Envelope

   27 

   28 ‘create the bottom left point and text

   29 strText = Round(pActiveView.Extent.XMin, lngRound) & _

   30 “:” & Round(pActiveView.Extent.YMin, lngRound)

   31 AddPoint pLayoutExtent.XMin – dblOffset, pLayoutExtent.YMin – dblOffset, strText

   32 

   33 ‘create the top left point and text

   34 strText = Round(pActiveView.Extent.XMin, lngRound) & _

   35 “:” & Round(pActiveView.Extent.YMax, lngRound)

   36 AddPoint pLayoutExtent.XMin – dblOffset, pLayoutExtent.YMax + dblOffset, strText

   37 

   38 ‘create the bottom right point and text

   39 strText = Round(pActiveView.Extent.XMax, lngRound) & _

   40 “:” & Round(pActiveView.Extent.YMin, lngRound)

   41 AddPoint pLayoutExtent.XMax + dblOffset, pLayoutExtent.YMin – dblOffset, strText

   42 

   43 ‘create the top right point and text

   44 strText = Round(pActiveView.Extent.XMax, lngRound) & _

   45 “:” & Round(pActiveView.Extent.YMax, lngRound)

   46 AddPoint pLayoutExtent.XMax + dblOffset, pLayoutExtent.YMax + dblOffset, strText

   47 

   48 

   49 pMxDoc.ActiveView.Refresh

   50 

   51 End Sub

   52 

   53 Private Sub AddPoint(dblX As Double, dblY As Double, strText As String)

   54 

   55 Dim pMxDoc As IMxDocument

   56 Dim pPoint As IPoint

   57 Dim pTextElement As ITextElement

   58 Dim pPageLayout As IPageLayout

   59 Dim pElement As IElement

   60 Dim pGContainer As IGraphicsContainer

   61 

   62 Set pMxDoc = ThisDocument

   63 

   64 ‘create the location for the text

   65 

   66 Set pPoint = New Point

   67 pPoint.X = dblX

   68 pPoint.Y = dblY

   69 

   70 ‘create the text element

   71 

   72 Set pTextElement = New TextElement

   73 pTextElement.Text = strText

   74 

   75 ‘add the element to the layout

   76 

   77 Set pPageLayout = pMxDoc.PageLayout

   78 Set pElement = pTextElement

   79 pElement.Geometry = pPoint

   80 Set pGContainer = pPageLayout

   81 pGContainer.AddElement pElement, 0

   82 

   83 End Sub

My start up program when working with ArcObjects is nearly always set to ArcMap.exe As my ArcMap installation had recently moved from an F: drive to a C: drive I had to update this for over a dozen projects. There had to be an easier way (!) so I spent some time trying to find out how to accomplish this with a Visual Studio macro. This can easily be altered to set the start up program for a set of projects in a solution, or to another .exe.

    1 Imports System

    2 Imports EnvDTE

    3 Imports VSLangProj

    4 Imports VSLangProj2

    5 Imports VSLangProj80

    6 

    7 Public Module ArcGISMacros

    8 

    9     Sub SetStartProgram()

   10 

   11         Dim proj As Project

   12         Dim config As Configuration

   13         Dim configProps As Properties

   14         Dim prop As [Property]

   15 

   16         For Each proj In DTE.Solution.Projects

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

   18                 config = proj.ConfigurationManager.ActiveConfiguration

   19                 configProps = config.Properties

   20                 prop = configProps.Item(“StartProgram”)

   21                 If prop.Value.ToString() Like “*ArcMap*” Then

   22                     prop.Value = “C:\Program Files\ArcGIS\Bin\ArcMap.exe”

   23                 End If

   24             End If

   25         Next

   26     End Sub

   27 End Module