Category Archives: ArcGIS VB .NET

Event Handler

Friend Sub AddHandlerEx()

Dim application2 As ESRI.ArcGIS.Framework.IApplication = CType(My.ThisApplication, ESRI.ArcGIS.Framework.IApplication)
Dim mxDocument As ESRI.ArcGIS.ArcMapUI.IMxDocument = application2.Document
Dim activeView As ESRI.ArcGIS.Carto.IActiveView = mxDocument.ActiveView
Dim map As ESRI.ArcGIS.Carto.IMap = activeView.FocusMap
‘Step 2: Create an instance of the delegate using the AddressOf operator and add it to
‘the AfterDraw event.
m_ActiveViewEventsSelectionChanged = New ESRI.ArcGIS.Carto.IActiveViewEvents_SelectionChangedEventHandler(AddressOf OnActiveViewEventsSelectionChanged)
‘Step 3: Dynamically associate an event handler to the delegate object.
AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).SelectionChanged, m_ActiveViewEventsSelectionChanged

End Sub

Public Sub RemoveHandlerEx()
Dim application2 As ESRI.ArcGIS.Framework.IApplication = CType(My.ThisApplication, ESRI.ArcGIS.Framework.IApplication)
Dim mxDocument As ESRI.ArcGIS.ArcMapUI.IMxDocument = application2.Document
Dim activeView As ESRI.ArcGIS.Carto.IActiveView = mxDocument.ActiveView
Dim map As ESRI.ArcGIS.Carto.IMap = activeView.FocusMap
‘Step 4: Dynamically remove an event handler.
RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).SelectionChanged, m_ActiveViewEventsSelectionChanged
End Sub

Advertisements

Wiring events in arcGIS VB.NET 10 and VBA

In VB.NET:
http://www.esri.com/news/arcuser/1005/files/eventdriven.pdf

 

 

 

http://edndoc.esri.com/arcobjects/9.2/NET/ViewCodePages/e9d3cad8-0770-466a-a62d-6cd9d059c464Form1.vb.htm

 
Public Class Form1

‘ ***** SECTION (Start): Declare Event Handlers

‘ Notes:
‘ Variables are prefixed with an ‘m_’ denoting that they are member variables.
‘ This means they are global in scope for this class.

‘ The different IActiveViewEvents_XXXXXXXXX are ‘hidden types’ and do not show
‘ up using intellisense in VB.NET of Visaul Studio 2005. You can see them
‘ however using the Object Browser.
Private m_ActiveViewEventsAfterDraw As ESRI.ArcGIS.Carto.IActiveViewEvents_AfterDrawEventHandler
Private m_ActiveViewEventsAfterItemDraw As ESRI.ArcGIS.Carto.IActiveViewEvents_AfterItemDrawEventHandler
Private m_ActiveViewEventsContentsChanged As ESRI.ArcGIS.Carto.IActiveViewEvents_ContentsChangedEventHandler
Private m_ActiveViewEventsContentsCleared As ESRI.ArcGIS.Carto.IActiveViewEvents_ContentsClearedEventHandler
Private m_ActiveViewEventsFocusMapChanged As ESRI.ArcGIS.Carto.IActiveViewEvents_FocusMapChangedEventHandler
Private m_ActiveViewEventsItemAdded As ESRI.ArcGIS.Carto.IActiveViewEvents_ItemAddedEventHandler
Private m_ActiveViewEventsItemDeleted As ESRI.ArcGIS.Carto.IActiveViewEvents_ItemDeletedEventHandler
Private m_ActiveViewEventsItemReordered As ESRI.ArcGIS.Carto.IActiveViewEvents_ItemReorderedEventHandler
Private m_ActiveViewEventsSelectionChanged As ESRI.ArcGIS.Carto.IActiveViewEvents_SelectionChangedEventHandler
Private m_ActiveViewEventsSpatialReferenceChanged As ESRI.ArcGIS.Carto.IActiveViewEvents_SpatialReferenceChangedEventHandler
Private m_ActiveViewEventsViewRefreshed As ESRI.ArcGIS.Carto.IActiveViewEvents_ViewRefreshedEventHandler
‘ ***** SECTION (End): Declare Event Handlers

Private Sub btnStep1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStep1.Click

‘ map is set by referencing the public shared member variable (m_map) in the
‘ CommandWiringEvents class. This is one way to pass variables between classes in an application.
Dim map As ESRI.ArcGIS.Carto.IMap = CommandWiringEvents.m_map

TextBox3.Clear()

‘ ***** SECTION (Start): Set up the event handlers for all of the IActiveViewEvents

‘ Notes:
‘ You set the member IActiveViewEvents_XXXXXXXXX variables to the EventHandler
‘ for the specific events you want to capture. VB.NET uses the AddressOf
‘ operator to emulate ‘C++’ pointer like functionality.

‘ Create an instance of the delegate, add it to AfterDraw event
m_ActiveViewEventsAfterDraw = New ESRI.ArcGIS.Carto.IActiveViewEvents_AfterDrawEventHandler(AddressOf OnActiveViewEventsAfterDraw)
AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).AfterDraw, m_ActiveViewEventsAfterDraw

‘ Create an instance of the delegate, add it to AfterItemDraw event
m_ActiveViewEventsAfterItemDraw = New ESRI.ArcGIS.Carto.IActiveViewEvents_AfterItemDrawEventHandler(AddressOf OnActiveViewEventsItemDraw)
AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).AfterItemDraw, m_ActiveViewEventsAfterItemDraw

‘ Create an instance of the delegate, add it to ContentsChanged event
m_ActiveViewEventsContentsChanged = New ESRI.ArcGIS.Carto.IActiveViewEvents_ContentsChangedEventHandler(AddressOf OnActiveViewEventsContentsChanged)
AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ContentsChanged, m_ActiveViewEventsContentsChanged

‘ Create an instance of the delegate, add it to ContentsCleared event
m_ActiveViewEventsContentsCleared = New ESRI.ArcGIS.Carto.IActiveViewEvents_ContentsClearedEventHandler(AddressOf OnActiveViewEventsContentsCleared)
AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ContentsCleared, m_ActiveViewEventsContentsCleared

‘ Create an instance of the delegate, add it to FocusMapChanged event
m_ActiveViewEventsFocusMapChanged = New ESRI.ArcGIS.Carto.IActiveViewEvents_FocusMapChangedEventHandler(AddressOf OnActiveViewEventsFocusMapChanged)
AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).FocusMapChanged, m_ActiveViewEventsFocusMapChanged

‘ Create an instance of the delegate, add it to ItemAdded event
m_ActiveViewEventsItemAdded = New ESRI.ArcGIS.Carto.IActiveViewEvents_ItemAddedEventHandler(AddressOf OnActiveViewEventsItemAdded)
AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemAdded, m_ActiveViewEventsItemAdded

‘ Create an instance of the delegate, add it to ItemDeleted event
m_ActiveViewEventsItemDeleted = New ESRI.ArcGIS.Carto.IActiveViewEvents_ItemDeletedEventHandler(AddressOf OnActiveViewEventsItemDeleted)
AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemDeleted, m_ActiveViewEventsItemDeleted

‘ Create an instance of the delegate, add it to ItemReordered event
m_ActiveViewEventsItemReordered = New ESRI.ArcGIS.Carto.IActiveViewEvents_ItemReorderedEventHandler(AddressOf OnActiveViewEventsItemReordered)
AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemReordered, m_ActiveViewEventsItemReordered

‘ Create an instance of the delegate, add it to SelectionChanged event
m_ActiveViewEventsSelectionChanged = New ESRI.ArcGIS.Carto.IActiveViewEvents_SelectionChangedEventHandler(AddressOf OnActiveViewEventsSelectionChanged)
AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).SelectionChanged, m_ActiveViewEventsSelectionChanged

‘ Create an instance of the delegate, add it to SpatialReferenceChanged event
m_ActiveViewEventsSpatialReferenceChanged = New ESRI.ArcGIS.Carto.IActiveViewEvents_SpatialReferenceChangedEventHandler(AddressOf OnActiveViewEventsSpatialReferenceChanged)
AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).SpatialReferenceChanged, m_ActiveViewEventsSpatialReferenceChanged

‘ Create an instance of the delegate, add it to ViewRefreshed event
m_ActiveViewEventsViewRefreshed = New ESRI.ArcGIS.Carto.IActiveViewEvents_ViewRefreshedEventHandler(AddressOf OnActiveViewEventsViewRefreshed)
AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ViewRefreshed, m_ActiveViewEventsViewRefreshed
‘ ***** SECTION (End): Set up the event handlers for all of the IActiveViewEvents

End Sub

Private Sub btnStep3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStep3.Click

‘ map is set by referencing the public shared member variable (m_map) in the
‘ CommandWiringEvents class. This is one way to pass variables between classes in an application.
Dim map As ESRI.ArcGIS.Carto.IMap = CommandWiringEvents.m_map

‘ ***** SECTION (Start): Remove Event Handlers
RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).AfterDraw, m_ActiveViewEventsAfterDraw

RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).AfterItemDraw, m_ActiveViewEventsAfterItemDraw

RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ContentsChanged, m_ActiveViewEventsContentsChanged

RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ContentsCleared, m_ActiveViewEventsContentsCleared

RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).FocusMapChanged, m_ActiveViewEventsFocusMapChanged

RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemAdded, m_ActiveViewEventsItemAdded

RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemDeleted, m_ActiveViewEventsItemDeleted

RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemReordered, m_ActiveViewEventsItemReordered

RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).SelectionChanged, m_ActiveViewEventsSelectionChanged

RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).SpatialReferenceChanged, m_ActiveViewEventsSpatialReferenceChanged

RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ViewRefreshed, m_ActiveViewEventsViewRefreshed
‘ ***** SECTION (End): Remove Event Handlers

End Sub

‘ ***** SECTION (Start): Custom Functions that you write to add additionaly functionality for the events
‘ Event handler
Private Sub OnActiveViewEventsAfterDraw(ByVal Display As ESRI.ArcGIS.Display.IDisplay, ByVal phase As ESRI.ArcGIS.Carto.esriViewDrawPhase)
‘ Add your code here
‘ System.Windows.Forms.MessageBox.Show(“AfterDraw”)
TextBox3.Text = TextBox3.Text + vbCrLf + “AfterDraw”
End Sub

‘ Event handler
Private Sub OnActiveViewEventsItemDraw(ByVal Index As Short, ByVal Display As ESRI.ArcGIS.Display.IDisplay, ByVal phase As ESRI.ArcGIS.esriSystem.esriDrawPhase)
‘ Add your code here
‘ System.Windows.Forms.MessageBox.Show(“ItemDraw”)
TextBox3.Text = TextBox3.Text + vbCrLf + “ItemDraw”
End Sub

‘ Event handler
Private Sub OnActiveViewEventsContentsChanged()
‘ Add your code here
‘ System.Windows.Forms.MessageBox.Show(“ContentsChanged”)
TextBox3.Text = TextBox3.Text + vbCrLf + “ContentsChanged”
End Sub

‘ Event handler
Private Sub OnActiveViewEventsContentsCleared()
‘ Add your code here
‘ System.Windows.Forms.MessageBox.Show(“ContentsCleared”)
TextBox3.Text = TextBox3.Text + vbCrLf + “ContentsCleared”
End Sub

‘ Event handler
Private Sub OnActiveViewEventsFocusMapChanged()
‘ Add your code here
‘ System.Windows.Forms.MessageBox.Show(“FocusMapChanged”)
TextBox3.Text = TextBox3.Text + vbCrLf + “FocusMapChanged”
End Sub

‘ Event handler
Private Sub OnActiveViewEventsItemAdded(ByVal Item As Object)
‘ Add your code here
‘ System.Windows.Forms.MessageBox.Show(“ItemAdded”)
TextBox3.Text = TextBox3.Text + vbCrLf + “ItemAdded”
End Sub

‘ Event handler
Private Sub OnActiveViewEventsItemDeleted(ByVal Item As Object)
‘ Add your code here
‘ System.Windows.Forms.MessageBox.Show(“ItemDeleted”)
TextBox3.Text = TextBox3.Text + vbCrLf + “ItemDeleted”
End Sub

‘ Event handler
Private Sub OnActiveViewEventsItemReordered(ByVal Item As Object, ByVal toIndex As Integer)
‘ Add your code here
‘ System.Windows.Forms.MessageBox.Show(“ItemReordered”)
TextBox3.Text = TextBox3.Text + vbCrLf + “ItemReordered”
End Sub

‘ Event handler
Private Sub OnActiveViewEventsSelectionChanged()
‘ Add your code here
‘ System.Windows.Forms.MessageBox.Show(“SelectionChanged”)
TextBox3.Text = TextBox3.Text + vbCrLf + “SelectionChanged”
End Sub

‘ Event handler
Private Sub OnActiveViewEventsSpatialReferenceChanged()
‘ Add your code here
‘ System.Windows.Forms.MessageBox.Show(“SpatialReferenceChanged”)
TextBox3.Text = TextBox3.Text + vbCrLf + “SpatialReferenceChanged”
End Sub

‘ Event handler
Private Sub OnActiveViewEventsViewRefreshed(ByVal view As ESRI.ArcGIS.Carto.IActiveView, ByVal phase As ESRI.ArcGIS.Carto.esriViewDrawPhase, ByVal data As Object, ByVal envelope As ESRI.ArcGIS.Geometry.IEnvelope)
‘ Add your code here
‘ System.Windows.Forms.MessageBox.Show(“ViewRefreshed”)
TextBox3.Text = TextBox3.Text + vbCrLf + “ViewRefreshed”
End Sub
‘ ***** SECTION (End): Custom Functions that you write to add additionaly functionality for the events

End Class

Understanding VB.NET Code

1. Option Explicit: Used at file level to force explicit declaration of all variables in that file.

When Option Explicit appears in a file, you must explicitly declare all variables using the Dim, Private, Public, or ReDim statements. If you attempt to use an undeclared variable name, an error occurs at compile time.

If you don’t use the Option Explicit statement, all undeclared variables are of Object type.

VBA: Option Explicit

VB.NET: Option Explicit On

Reference: http://msdn.microsoft.com/en-us/library/y9341s4f(v=vs.71).aspx

2. Declare Statement: Used at module level to declare references to external procedures in a dynamic-link library (DLL).

[Public | Private] Declare Function name Lib “libname” [Alias “aliasname”] [([arglist])] [As type]

Reference: http://msdn.microsoft.com/en-us/library/aa243324(v=vs.60).aspx

VBA: ByVal field As Any

VB.Net: ByVal field As Object

Reference: http://social.msdn.microsoft.com/Forums/en/vbinterop/thread/2c87df7c-48ad-4ab8-a6b8-e0e277eb74ec

3. Radio Buttons (or Option Buttons)

VBA: RadioButton.Value

VB.NET: RadioButton.Checked

When grouped under a GroupBox, users will only be able to select one radio button.

4. Common Dialogue

FileOpenDialog.Filter

VBA:  .. +  “Microstation files (*.dgn)|*.DGN|”

VB.NET:   .. + “Microstation files (*.dgn)|*.DGN”

Note that vb.net does not allow a vertical bar at the end of the very last one of the Filter Options

5. Left and Right Function

In VB.NET, add:

Imports VBStrings = Microsoft.VisualBasic.Strings

and use

VBStrings.Left

 

Dim

Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pFeatureLayer As IFeatureLayer
Set pFeatureLayer = pMxDoc.FocusMap.Layer(modCommon.GetLayerIndex(“RidgeLine”))
Dim pFeatureCursor As IFeatureCursor
Set pFeatureCursor = pFeatureLayer.Search(Nothing, False)
Dim pFeature As IFeature
Set pFeature = pFeatureCursor.NextFeature

Zaware

It is necessary to declare a Zaware for a 3d point before the Z value can be defined.

Dim pPoint1 As IPoint
Set pPoint1 = New Point
pPoint1.PutCoords 1, 1
Dim pZAware As IZAware
Set pZAware = pPoint1
pZAware.ZAware = True
pPoint1.z = 11.1

Adding a field to the attribute table in ArcGIS

Dim pMyField3 As IFieldEdit
Set pMyField3 = New Field

With pMyField3
.Name = “Topo”
.Type = esriFieldTypeDouble
End With

Dim pTable3 As ITable
Set pTable3 = pFeatureLayer
pTable3.AddField pMyField3

ArcGIS Intersections

Dim pTopo As ITopologicalOperator
Set pTopo = pF1.Shape ‘ store the line 1 shape in the topo op
If Not pTopo.IsSimple Then pTopo.Simplify
Dim pGeom As IGeometry
Set pGeom = pTopo.Intersect(pF2.Shape, esriGeometry0Dimension) ‘ get intersection with line 2
If pGeom.IsEmpty Then
MsgBox “The intersection of the 2 geometries is empty or consists of points”
Exit Sub
Else
Dim ppcl As IPointCollection
Set ppcl = pGeom
MsgBox ppcl.Point(0).X & “, ” & ppcl.Point(0).Y
End If

esriGeometry0Dimension are points (IPointCollection)
esriGeometry1Dimension are lines (IPolyline)
For more information:
http://edndoc.esri.com/arcobjects/9.1/componenthelp/esriGeometry/esriGeometryDimension.htm

Getting a user’s selected feature in ArcGIS VBA/VB.NET

Dim pFL2 As IFeatureLayer
Set pFL2 = pMap.Layer(0)

Set pF2 As IFeature

Dim pfs As IFeatureSelection
Set pfs = pFL2
Dim id As Long
id = pfs.SelectionSet.IDs.Next
Set pF2 = pFL2.FeatureClass.GetFeature(id) ‘ this is line 2

Then you can do stuff on the Ifeature pF2 and pf2.Shape

Start Indices of VBA Collection and ArcGIS IPointCollection

Note that VBA Collection starts with an annoying 1, while IpointCollection starts with 0. phew.

The Complete guide to calling Excel Objects in VB .NET

This guide explains how to use Excel Objects in VB .NET

1. Import Microsoft.Office.Interop

2. Call Excel inside a Try-Catch Statement

Dim XApp As Excel.Application
Dim XBooks As Excel.Workbooks
Dim XBook As Excel.Workbook

Dim XSheet As Excel.Worksheet
Dim XRange As Excel.Range
XApp = CreateObject("Excel.Application")

XApp.Visible = False 'Run Excel in the background

XBooks = XApp.Workbooks
XBook = XBooks.Open(templateXLSX)

XSheet = XApp.Application.Sheets("Raw Data")

3. Manipulate Excel Object

XRange = XApp.Range("A1")

XRange.Value2 = "NEW VALUE"

4. Save the Excel Files

XBook.Close(False) ‘The boolean argument indicates whether the file is saved before closing

5. Clean all Excel objects

Cleaning objects are required, or else the Excel object will not go away in Task Manager

     While (System.Runtime.InteropServices.Marshal.ReleaseComObject(XBook_New) <> 0)
            End While
            XBook = Nothing

            While (System.Runtime.InteropServices.Marshal.ReleaseComObject(XBooks) <> 0)
            End While
            XBooks = Nothing
            XApp.Quit()

            While (System.Runtime.InteropServices.Marshal.ReleaseComObject(XApp) <> 0)
            End While
            XApp = Nothing

For each object, do ReleaseComObject and then set its value to Nothing

If the spreadsheets are updated via a Loop, check carefully that every object is cleaned before it is called again.

For example:

XRange = XApp.Range(“A1″)

XRange.Value2=”NEW VALUE 1”

System.Runtime.InteropServices.Marshal.ReleaseComObject(XRange)

XRange = XApp.Range(“A2”) ‘XRange is cleaned before it is called.

 

Also, call Garbage Collector twice as follows:

       GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()
        GC.WaitForPendingFinalizers()

6. For all language environments to run your program without MUI installed, add this to your code:

   Dim oldCI As System.Globalization.CultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture
   System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US")

at the beginning after setting Xapp, and at the end

    System.Threading.Thread.CurrentThread.CurrentCulture = oldCI

That’s all!