2

I am converting an old VBA code I used to use in ArcMap 9.2, into VB.NET for ArcMap 10. It should extract x, y coordinates from OSGB and converts to decimal degrees while copying to clipboard. I converted it and does not work. Nothing happens when I click on button.

Protected Overloads Sub OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs, ByVal shift As Long, ByVal X As Long, ByVal Y As Long)

    Dim pMxDoc As IMxDocument
    Dim pPoint As IPoint
    Dim pClone As IClone
    Dim pGeometry As IGeometry
    Dim pSpatialRefFactory As ISpatialReferenceFactory
    Dim pSpatialRef As ISpatialReference
    Dim pGeographicCoordSys As IGeographicCoordinateSystem

    'Get the point where the user clicked
    pMxDoc = My.ArcMap.Application.Document
    If pMxDoc.CurrentLocation.IsEmpty Then Exit Sub
    'Clone the point because we don't want to alter
    'the actual document's current location point
    pClone = pMxDoc.CurrentLocation
    pPoint = pClone.Clone
    pGeometry = pPoint 'QI

    'Create a new geographic coordinate system to use in the conversion
    pSpatialRefFactory = New SpatialReferenceEnvironment
    pGeographicCoordSys = pSpatialRefFactory.CreateGeographicCoordinateSystem(4277)
    'esriSRGeoCS_OSGB1936
    pSpatialRef = pGeographicCoordSys 'QI
    pSpatialRef.SetFalseOriginAndUnits(-180, -90, 1000000)

    pGeometry.Project(pSpatialRef)
    MsgBox(pPoint.X & ", " & pPoint.Y, , "Decimal Degrees")

    Dim MyData As New DataObject

    MyData.SetText(pPoint.Y & ", " & pPoint.X)

    MyData.PutInClipboard()
    MsgBox("Data copied to clipboard successfuly")

End Sub

I get only 2 warnings not errors with message:

A reference was created to embedded interop assembly 'ESRI.ArcGIS.Carto' because of an indirect reference to that assembly from assembly 'ESRI.ArcGIS.ArcMap'. Consider changing the 'Embed Interop Types' property on either assembly. ArcMapAddin2

UPDATE:

I got the tool working by changing from button to tool.

Protected Overrides Sub OnMouseDown(ByVal arg As ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs)
    MyBase.OnMouseDown(arg)

The tool works and copies to clipboard. But when I press Ok in dialog box it closes ArcMap down. but why?

0

Works fine now. Here is the code for anyone interested. Useful if you need to copy OSGB1936 co-ordinates into Google Maps.

Seems the way I was inserting the text into the clipboard was crashing ArcMap. Also should be tool not button.

Protected Overrides Sub OnMouseDown(ByVal arg As ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs)
MyBase.OnMouseDown(arg)

Dim pMxDoc As IMxDocument
Dim pPoint As IPoint
Dim pClone As IClone
Dim pGeometry As IGeometry
Dim pSpatialRefFactory As ISpatialReferenceFactory
Dim pSpatialRef As ISpatialReference
Dim pGeographicCoordSys As IGeographicCoordinateSystem

Try

'Get the point where the user clicked
pMxDoc = My.ArcMap.Application.Document
If pMxDoc.CurrentLocation.IsEmpty Then Exit Sub
'Clone the point because we don't want to alter
'the actual document's current location point
pClone = pMxDoc.CurrentLocation
pPoint = pClone.Clone
pGeometry = pPoint 'QI

'Create a new geographic coordinate system to use in the conversion
pSpatialRefFactory = New SpatialReferenceEnvironment
pGeographicCoordSys = pSpatialRefFactory.CreateGeographicCoordinateSystem(4277)
'esriSRGeoCS_OSGB1936
pSpatialRef = pGeographicCoordSys 'QI
pSpatialRef.SetFalseOriginAndUnits(-180, -90, 1000000)

pGeometry.Project(pSpatialRef)
MsgBox(pPoint.X & ", " & pPoint.Y, , "Decimal Degrees")

My.Computer.Clipboard.SetText(pPoint.Y & ", " & pPoint.X)

MsgBox("Data copied to clipboard successfully")

Catch ex As Exception
        MsgBox(ex.Message)
End Try

End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.