Customs Tools

856
4
09-08-2021 01:38 PM
shawnstanley
New Contributor III

hello

 

I have a custom extension created in VB.NET for ArcGIS.  In one of the forms I have two tools which do very similar things (select a point from the map area) and so the code is rather similar.  One of the tools works but the other throws a NullReferenceException.  It happens because the following line returns nothing:

pCommandItem = Application.Document.CommandBars.Find(pUID, False, False)

However, and this is what is confusing to me - apart of course from why the two tools with similar wording has one not work ... If I add the tool to the ToolBar for the whole extension then it works in this form.

This is not a good solution since I do not want the tool in the ToolBar - it only works in relation to what the form is doing.  But why should it be working if it is added to the ToolBar, but not if it, and the similar tool are added to the Form?

Any ideas?

cheers

shawn

Tags (3)
0 Kudos
4 Replies
DuncanHornby
MVP Notable Contributor

You need to explain what 'pUID' is referencing and show your code.

0 Kudos
shawnstanley
New Contributor III

pUID is referencing the ClassID of the tool itself. 

 

pUID.Value = "{" & SelectPoint1.ClassId & "}"

 

SelectPoint1 or SelectPoint2 are the two tools being referenced.

 

The full code of point of those two tools is here, it's SelectPoint1.  As noted above, the other tool is the same, it just writes to a different part of the form.

 

Imports System.Runtime.InteropServices
Imports System.Drawing
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.ArcMapUI
Imports System.Windows.Forms
Imports System.Windows.Forms.Application

Imports ESRI.ArcGIS.ArcScene
Imports ESRI.ArcGIS.Analyst3D

<ComClass(SelectPoint1.ClassId, SelectPoint1.InterfaceId, SelectPoint1.EventsId), _
ProgId("NAME.SelectPoint1")> _
Public NotInheritable Class SelectPoint1
Inherits BaseTool

#Region "COM Registration Function(s)"
<ComRegisterFunction(), ComVisibleAttribute(False)> _
Public Shared Sub RegisterFunction(ByVal registerType As Type)
' Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType)

'Add any COM registration code after the ArcGISCategoryRegistration() call

End Sub

<ComUnregisterFunction(), ComVisibleAttribute(False)> _
Public Shared Sub UnregisterFunction(ByVal registerType As Type)
' Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType)

'Add any COM unregistration code after the ArcGISCategoryUnregistration() call

End Sub

#Region "ArcGIS Component Category Registrar generated code"
''' <summary>
''' Required method for ArcGIS Component Category registration -
''' Do not modify the contents of this method with the code editor.
''' </summary>
Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
MxCommands.Register(regKey)

End Sub
''' <summary>
''' Required method for ArcGIS Component Category unregistration -
''' Do not modify the contents of this method with the code editor.
''' </summary>
Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
MxCommands.Unregister(regKey)

End Sub

#End Region
#End Region


#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "a38b65c3-255c-4578-ab06-2e00c59efa2d"
Public Const InterfaceId As String = "042a7209-0920-4531-95be-9bcbb0f4a64d"
Public Const EventsId As String = "c94af36c-0e76-43ad-96ad-28d77b008c17"
#End Region

Private m_application As IApplication

' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()

MyBase.m_category = Work tools"
MyBase.m_caption = "Select Point"
MyBase.m_message = "Select Point"
MyBase.m_toolTip = "Select Point1"
MyBase.m_name = "SelectPoint1"

Try
Dim bitmapResourceName As String = Me.GetType().Name + ".bmp"
MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)
MyBase.m_cursor = New System.Windows.Forms.Cursor(Me.GetType(), Me.GetType().Name + ".cur")
Catch ex As Exception
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
End Try

End Sub

Public Overrides Sub OnCreate(ByVal hook As Object)

If Not hook Is Nothing Then
m_application = CType(hook, IApplication)

'Disable if it is not ArcMap or ArcScene
If TypeOf hook Is IMxApplication Or TypeOf hook Is ESRI.ArcGIS.ArcScene.ISxApplication Then
MyBase.m_enabled = True
Else
MyBase.m_enabled = False
End If
End If

' TODO: Add other initialization code
End Sub

Public Overrides Sub OnClick()
'TODO: Add SelectPoint1.OnClick implementation
End Sub

Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)

Dim pPoint As ESRI.ArcGIS.Geometry.IPoint = Nothing

Dim pFeatures As Collection = Nothing
Dim pFeature As ESRI.ArcGIS.Geodatabase.IFeature = Nothing

If TypeOf m_pApp Is ESRI.ArcGIS.ArcMapUI.IMxApplication Then

' If the tool applies to an active view make sure the right map is showing
pPoint = m_pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y)

Dim pMap As ESRI.ArcGIS.Carto.IMap
pMap = m_pActiveView.HitTestMap(pPoint)

If pMap Is Nothing Then Exit Sub

If Not pMap Is m_pActiveView.FocusMap Then

m_pActiveView.FocusMap = pMap
m_pActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)

End If

NAME.GetClosestFeature((m_pActiveView.FocusMap), pPoint, pFeatures, pFeature)

ElseIf TypeOf m_pApp Is ESRI.ArcGIS.ArcScene.ISxApplication Then

Dim pSxDoc As ESRI.ArcGIS.ArcScene.ISxDocument
pSxDoc = m_pApp.Document
Dim pScene As IScene
pScene = pSxDoc.Scene
Dim pSG As ESRI.ArcGIS.Analyst3D.ISceneGraph
pSG = pScene.SceneGraph

Dim pOwner As stdole.IUnknown = Nothing
Dim pObject As stdole.IUnknown = Nothing

pSG.Locate(pSG.ActiveViewer, X, Y, ESRI.ArcGIS.Analyst3D.esriScenePickMode.esriScenePickGeography, True, pPoint, pOwner, pObject)

If (pPoint Is Nothing) Then

Beep()
Exit Sub

End If

NAME.GetClosestSceneFeature(pScene, pPoint, pFeatures, pFeature)

End If

Dim SelectDistanceForm As frmSelectDistance

If OpenForms.Item("frmSelectDistance") IsNot Nothing Then

'exists
SelectDistanceForm = OpenForms.Item("frmSelectDistance")

Else

SelectDistanceForm = Nothing
'not exists

End If

If (pFeature Is Nothing) Then

SelectDistanceForm.txtLatitude1.Text = ""
SelectDistanceForm.txtLongitude1.Text = ""
Exit Sub

End If

Dim lngField As Integer

lngField = pFeature.Fields.FindField(SelectDistanceForm.cbxLatitude.Text)
SelectDistanceForm.txtLatitude1.Text = CStr(pFeature.Value(lngField))
lngField = pFeature.Fields.FindField(SelectDistanceForm.cbxLongitude.Text)
SelectDistanceForm.txtLongitude1.Text = CStr(pFeature.Value(lngField))

End Sub

Public Overrides Sub OnMouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
'TODO: Add SelectPoint1.OnMouseMove implementation
End Sub

Public Overrides Sub OnMouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
'TODO: Add SelectPoint1.OnMouseUp implementation
End Sub
End Class

0 Kudos
shawnstanley
New Contributor III

I'm afraid I don't understand what's going on on those pages Duncan enough to understand how to apply it to my situation.

0 Kudos