PolygonDrawingTool with GoPolygonStyle.Line

Hello,

is there an example of a simple PolygonDrawingTool with Line
style? I try to modify the class of DrawDemo4 without success. I get an line polygon but always with 2 points more at the start and end point. If i click 3 times for example, i get an polygon with 5 points. The points get visible if i try to move the start point or the last point. Dead

Can anybody help me with this?

Thanks a lot,
Andreas

<Serializable()> Public Class PolygonDrawingTool
        Inherits GoTool
        Public Sub New(ByVal view As GoView)
            MyBase.new(view)
        End Sub
 
        Public Overrides Sub Start()
            MainForm.App.SetStatusMessage("Drawing mode -- click to add points to a new Polygon, Enter or Escape to stop")
            Me.View.Cursor = Cursors.Cross
        End Sub
 
        Public Overrides Sub [Stop]()
            If Not (myPolygon Is Nothing) Then
                myPolygon.Remove()
                myPolygon = Nothing
            End If
            Me.View.Cursor = Me.View.DefaultCursor
            MainForm.App.SetStatusMessage("Stopped drawing mode")
        End Sub
 
        Public Overrides Sub DoMouseDown()
            If Me.LastInput.DoubleClick Then
                Return
            End If
            If (myPolygon Is Nothing) Then
                ' create a new Polygon starting at the first mouse down point
                myPolygon = New GoPolygon()
                myPolygon.Style = GoPolygonStyle.Line
 
                myPolygon.AddPoint(Me.FirstInput.DocPoint)
                Me.View.Layers.Default.Add(myPolygon)
            End If
 
            myPolygon.AddPoint(Me.LastInput.DocPoint)
        End Sub
 
        Public Overrides Sub DoMouseMove()
            If Not (myPolygon Is Nothing) Then
                Dim numpts As Integer = myPolygon.PointsCount
               
                If (numpts >= 1) Then
                    Dim Start As PointF = myPolygon.GetPoint(numpts - 1)
                    Dim [end] As PointF = Me.LastInput.DocPoint
                    myPolygon.SetPoint(numpts - 1, [end])
                End If
            End If
        End Sub
 
        Public Overrides Sub DoMouseUp()
            ' don't call the base functionality, which assumes mouse up stops the tool
            If Me.LastInput.DoubleClick Then
                FinishPolygon()
            End If
        End Sub
 
        Private Sub FinishPolygon()
            If (Not (myPolygon Is Nothing) AndAlso myPolygon.PointsCount > 2) Then
                StartTransaction()
                ' make the final segment go back to the first point
                Me.LastInput.DocPoint = myPolygon.GetPoint(0)
                DoMouseMove()
                ' now move the Polygon from the view to the document
                myPolygon.Remove()
                Me.View.Document.Add(myPolygon)
                ' and make it selected
                Me.View.Selection.Select(myPolygon)
                myPolygon = Nothing
                Me.TransactionResult = "New Polygon"
                StopTransaction()
                StopTool()
            End If
        End Sub
 
        Private myPolygon As GoPolygon = Nothing
    End Class

Haven’t forgotten you… busy here today. Will look at this soon.

OK, I see what you’ve done to get rid of the extra Bezier points. Looks like a reasonable modification to me, I don’t see the extra points. Guess I’ll have to go try it.

        Public Overrides Sub DoMouseMove()
            If Not (myPolygon Is Nothing) Then
                Dim numpts As Integer = myPolygon.PointsCount
               
                If (numpts >= 2) Then
                    ' Dim Start As PointF = myPolygon.GetPoint(numpts - 1)
                    Dim [end] As PointF = Me.LastInput.DocPoint
                    myPolygon.SetPoint(numpts - 1, [end])
                End If
            End If
        End Sub
that doesn't actually help the extra points, but it is a correctness thing.
ok... note the algorithm always has the "next point" in the polygon waiting for the mouse down.
so... you click 3 times and hit Enter... you get 4 points.
and... it you double click to end... you get an extra one there.
In FinishPolygon, the code uses that last point to close the shape:
// make the final segment go back to the first point
this.LastInput.DocPoint = myPolygon.GetPoint(0);
DoMouseMove();
but that really isn’t necessary, so you can change this to:
        //this.LastInput.DocPoint = myPolygon.GetPoint(0);
//DoMouseMove();
myPolygon.RemovePoint(myPolygon.PointsCount - 1);
(I’ve switched to C# here)

so… now you’re good in the case of ending with Enter.

and… if you do this:

public override void DoMouseUp() {
// don’t call the base functionality, which assumes mouse up stops the tool
if (this.LastInput.DoubleClick) {
myPolygon.RemovePoint(myPolygon.PointsCount - 1);
FinishPolygon();
}
}
you get rid of the extra point on the double click.



Hello Jake,

thanks a lot, this works like I want!
Now I understand this code better. :)

Andreas