VB .NET drawing a line between two points

2509
10
Jump to solution
03-07-2012 05:34 AM
Gregde_Bruin
New Contributor III
Hi,

I'm attempting to fix someone's code that draws a line betweent two points. I'm converting it from VBA to VB .NET.

This section appears to be where it's failing. Right after Debug 8.1 or 8.2

  'Draw line between points
Dim calcLength As Double

pSegmentCollection = New Path
pLine = New Line

'Draw a line between first point and second point (FromPoint and ToPoint)
pLine.PutCoords(ToPoint, FromPoint)
Debug.Print("8.1")
calcLength = pLine.Length
Debug.Print("8.2")
pSegmentCollection.AddSegment(pLine)
Debug.Print("8.3")

Any help is appreciated.

Thank you,
cdebruin
0 Kudos
1 Solution

Accepted Solutions
Gregde_Bruin
New Contributor III
I figured out what was wrong. I needed to declare my FromPoint and ToPoint variables at the Class level so they would persist long enough to pick up both points.

Thank you, everyone, for the help.

View solution in original post

0 Kudos
10 Replies
Gregde_Bruin
New Contributor III
Still need help.

This is the exception that I got when I changed it over from user handled:

System.Runtime.InteropServices.COMException {"The operation was attempted on an empty geometry."}
0 Kudos
KenBuja
MVP Esteemed Contributor
Can you post more of the code?
0 Kudos
NeilClemmons
Regular Contributor III
Still need help.

This is the exception that I got when I changed it over from user handled:

System.Runtime.InteropServices.COMException {"The operation was attempted on an empty geometry."}


The error is telling you the geometry is empty, which means it does not contain valid points.  In the case of a line, it most likely means the from and to points have the same coordinates (which does not define a valid line).  Check your points to make sure the coordinates are being set correctly.  Also, if FromPoint and ToPoint are accurately named then you're passing them in backwards to the PutCoords() method.
0 Kudos
Gregde_Bruin
New Contributor III
Can you post more of the code?


I ceratinly have the ability.




Imports System
Imports System.Collections.Generic
Imports System.Text
Imports ESRI.ArcGIS.ArcMapUI
Imports Microsoft.VisualBasic
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.Framework

Public Class ToolSanLineMake
Inherits ESRI.ArcGIS.Desktop.AddIns.Tool
Private m_ismousedown As Boolean = False
Private m_linefeedback As ESRI.ArcGIS.Display.INewLineFeedback
Private m_focusmap As iactiveview

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

'Dim sanlineCount As Integer
Dim sanMHcount As Integer
Dim pLine As ILine


Dim mxdoc As IMxDocument = My.ArcMap.Document
m_focusmap = CType(mxdoc.FocusMap, IActiveView)

'pMxApp = My.ArcMap.Application
'pMxDoc = My.ArcMap.Document
'pMap = pMxDoc.FocusMap
'pActiveView = pMap

LayerInitialize()
getLayerSanitaryManholes()
getLayerSanitaryLines()
getTableNCsewerContracts()
If layerErrorCheck() <> "" Then Exit Sub

makeOrEdit = "Enter"

If arg.Shift <> 0 Then
' User wants to edit an existing line rather than make a new one
'Call SanLineEdit_MouseDown(ByVal button, ByVal shift, ByVal x, ByVal y)
MsgBox("User wants to edit")
Exit Sub
End If

Dim pFeatureSelection As ESRI.ArcGIS.Carto.IFeatureSelection
Dim FromPoint As New Point

'Select two manholes and draw a line in sanline layer
'Get the first manhole feature, store the coordinates and type
'If the global variable is 0, get the new point (tests for the first or second selection)
If startSect = 0 Then
pFeatureSelection = GetSelectionGeometry(sanmhFeatLyr, x, y)
sanMHcount = pFeatureSelection.SelectionSet.Count
If sanMHcount = 0 Then
MsgBox("No manhole was selected.")
Exit Sub
ElseIf sanMHcount > 1 Then
MsgBox("You have selected " & sanMHcount & " manholes. Select the first manhole.")
Exit Sub
End If

startSect = 1

'Get the GID field from poles table and store it in GFKPOLEST
pFeatureSelection.SelectionSet.Search(Nothing, True, sanmhFeatCursor)
sanmhFeature = sanmhFeatCursor.NextFeature

'Test for null manhole ID field: save point or display message box
sanlineGfkMHfromVal = checkNullGUID2(sanmhFeature, sanmhGIDfld)
sanmhMHUPrimElevVal = checkNullNumber(sanmhFeature, sanmhMHrimElevFld)
'sanlineGfkMHfromVal = checkNullString(sanmhFeature, sanmhLegacy_IDfld)
'Debug.Print "sanmhMHUPrimElevVal = " & sanmhMHUPrimElevVal
FromPoint = sanmhFeature.Shape

Else 'Get the second manhole feature, store the coordinates and type

Dim plFeatureSelection As ESRI.ArcGIS.Carto.IFeatureSelection
Dim ToPoint As New Point

plFeatureselection = GetSelectionGeometry(sanmhFeatLyr, x, y)
sanMHcount = plFeatureselection.SelectionSet.Count
'If No Selection is made Messagebox pops up
If sanMHcount = 0 Then
MsgBox("No manhole was selected. Please try again.")
Exit Sub
ElseIf sanMHcount > 1 Then
'If selection is more than one manhole then again Messagebox pops up
MsgBox("You have selected " & sanMHcount & " manholes. Select only one.")
Exit Sub
End If

'Get the GID field from the manholes table
plFeatureSelection.SelectionSet.Search(Nothing, True, sanmhFeatCursor)
sanmhFeature = sanmhFeatCursor.NextFeature
ToPoint = sanmhFeature.Shape
sanlineGfkMHtoVal = checkNullGUID2(sanmhFeature, sanmhGIDfld)
sanmhMHrimElevVal = checkNullString(sanmhFeature, sanmhMHrimElevFld)
sanmhLegacy_IDVal = checkNullString(sanmhFeature, sanmhLegacy_IDfld)

'Check to make sure that the same manhole was not selected twice
If sanlineGfkMHfromVal = sanlineGfkMHtoVal Then
MsgBox("The second manhole selected has the same GID number as the first. Select both manholes again.")
startSect = 0
Exit Sub
End If

If (sanlineGfkMHfromVal = GUID000) Or (sanlineGfkMHtoVal = GUID000) Then
MsgBox("One or both of the selected manholes is missing its GID value. The line cannot be made until a GID is created.")
startSect = 0
Exit Sub
End If

startSect = 0

'Draw line between points
Dim calcLength As Double = 0

pSegmentCollection = New Path
pLine = New Line
Debug.Print("calcLength = " & calcLength)
Debug.Print("1")
'Draw a line between first point and second point (FromPoint and ToPoint)
pLine.PutCoords(ToPoint, FromPoint)
Debug.Print("2")
calcLength = pLine.Length ''***Crashes ArcMap here. Something is wrong with pLine.Length***
Debug.Print("calcLength = " & calcLength)
Debug.Print("pLine.Length = " & pLine.Length)
Debug.Print("3")
pSegmentCollection.AddSegment(pLine)
Debug.Print("4")

Dim FormSanLineEdit1 As New FormSanLineEdit
FormSanLineEdit1.ShowDialog()

m_focusmap.Refresh()

End If

End Sub

End Class
0 Kudos
KenBuja
MVP Esteemed Contributor
Are you using the debugging tools to put in breakpoints so that you can evaluate the values of ToPoint, FromPoint, and pLine? In looking through your code, I also notice that you don't declare many of your variables. This can come back to haunt you later.
0 Kudos
Gregde_Bruin
New Contributor III
Are you using the debugging tools to put in breakpoints so that you can evaluate the values of ToPoint, FromPoint, and pLine? In looking through your code, I also notice that you don't declare many of your variables. This can come back to haunt you later.


I've officially been computer programming for two weeks, and I have no idea how to display the values of Point or Line objects/data types.
I have been using breakpoints and debugging tools to step through the code, which is how I hit the error.
Also, It's not my code, I'm just trying some minimal fixes to get it working again. All the code I've written so far has been explicitly declared.
0 Kudos
Gregde_Bruin
New Contributor III
I figured out what was wrong. I needed to declare my FromPoint and ToPoint variables at the Class level so they would persist long enough to pick up both points.

Thank you, everyone, for the help.
0 Kudos
KenBuja
MVP Esteemed Contributor
I've officially been computer programming for two weeks, and I have no idea how to display the values of Point or Line objects/data types.
I have been using breakpoints and debugging tools to step through the code, which is how I hit the error.
Also, It's not my code, I'm just trying some minimal fixes to get it working again. All the code I've written so far has been explicitly declared.


You can look at the values in a couple of ways. First, when the code hits the breakpoint, if you put your cursor over a variable, it will show you information about it. If you open the Locals window (Debug|Windows|Locals), you'll see a listing of all the variables used in your code. You can set up Watches on specific variables.
0 Kudos
MichaelVolz
Esteemed Contributor
Greg:

Have you tried looking at python to accomplish this task?  It would probably result in a lot less code on your part.  In v10.1 you will be able to associate python code with buttons on a toolbar which should make this even easier.
0 Kudos