AnsweredAssumed Answered

How can I draw a freehand polygon?

Question asked by kenbuja Champion on May 10, 2011
Latest reply on May 11, 2011 by kenbuja
I'm creating an Add-in that lets a user select the type of feature to draw on the map. I've gotten the code for adding points, polygons, and rectangles, but I'd like to add in the capability of drawing polygons in freehand mode. Is there way of doing that? Here's my existing code for the drawing tool

Public Class DrawTool
    Inherits ESRI.ArcGIS.Desktop.AddIns.Tool

    Public Sub New()

    End Sub

    Protected Overrides Sub OnUpdate()

    End Sub

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

        Dim activeView As ESRI.ArcGIS.Carto.IActiveView = My.ArcMap.Document.ActiveView
        Dim rgbColor As ESRI.ArcGIS.Display.IRgbColor = New ESRI.ArcGIS.Display.RgbColorClass()
        Dim pGeometry As ESRI.ArcGIS.Geometry.IGeometry5 = GetFeatureFromMouse(activeView)

        rgbColor.Red = 255

        'Add the user's drawn graphics as persistent on the map.
        AddGraphicToMap(activeView.FocusMap, pGeometry, rgbColor, rgbColor)
        'Best practice: Redraw only the portion of the active view that contains graphics.
        activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)

    End Sub

    Public Sub AddGraphicToMap(ByVal map As ESRI.ArcGIS.Carto.IMap, ByVal geometry As ESRI.ArcGIS.Geometry.IGeometry, ByVal rgbColor As ESRI.ArcGIS.Display.IRgbColor, ByVal outlineRgbColor As ESRI.ArcGIS.Display.IRgbColor)

        Dim graphicsContainer As ESRI.ArcGIS.Carto.IGraphicsContainer = CType(map, ESRI.ArcGIS.Carto.IGraphicsContainer) ' Explicit Cast
        Dim element As ESRI.ArcGIS.Carto.IElement = Nothing


        If (geometry.GeometryType) = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint Then

            ' Marker symbols
            Dim simpleMarkerSymbol As ESRI.ArcGIS.Display.ISimpleMarkerSymbol = New ESRI.ArcGIS.Display.SimpleMarkerSymbolClass()
            simpleMarkerSymbol.Color = rgbColor
            simpleMarkerSymbol.Outline = True
            simpleMarkerSymbol.OutlineColor = outlineRgbColor
            simpleMarkerSymbol.Size = 8
            simpleMarkerSymbol.Style = ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSCross

            Dim markerElement As ESRI.ArcGIS.Carto.IMarkerElement = New ESRI.ArcGIS.Carto.MarkerElementClass()
            markerElement.Symbol = simpleMarkerSymbol
            element = CType(markerElement, ESRI.ArcGIS.Carto.IElement) ' Explicit Cast

        ElseIf (geometry.GeometryType) = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline Then

            '  Line elements
            Dim simpleLineSymbol As ESRI.ArcGIS.Display.ISimpleLineSymbol = New ESRI.ArcGIS.Display.SimpleLineSymbolClass()
            simpleLineSymbol.Color = rgbColor
            simpleLineSymbol.Style = ESRI.ArcGIS.Display.esriSimpleLineStyle.esriSLSSolid
            simpleLineSymbol.Width = 5

            Dim lineElement As ESRI.ArcGIS.Carto.ILineElement = New ESRI.ArcGIS.Carto.LineElementClass()
            lineElement.Symbol = simpleLineSymbol
            element = CType(lineElement, ESRI.ArcGIS.Carto.IElement) ' Explicit Cast

        ElseIf (geometry.GeometryType) = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon Then

            ' Polygon elements
            Dim simpleFillSymbol As ESRI.ArcGIS.Display.ISimpleFillSymbol = New ESRI.ArcGIS.Display.SimpleFillSymbolClass()
            simpleFillSymbol.Color = rgbColor
            simpleFillSymbol.Style = ESRI.ArcGIS.Display.esriSimpleFillStyle.esriSFSForwardDiagonal
            Dim fillShapeElement As ESRI.ArcGIS.Carto.IFillShapeElement = New ESRI.ArcGIS.Carto.PolygonElementClass()
            fillShapeElement.Symbol = simpleFillSymbol
            element = CType(fillShapeElement, ESRI.ArcGIS.Carto.IElement) ' Explicit Cast

        End If

        If Not (element Is Nothing) Then

            element.Geometry = geometry
            graphicsContainer.AddElement(element, 0)

        End If

    End Sub

    Public Function GetFeatureFromMouse(activeView As ESRI.ArcGIS.Carto.IActiveView) As ESRI.ArcGIS.Geometry.IGeometry5

        Dim screenDisplay As ESRI.ArcGIS.Display.IScreenDisplay = activeView.ScreenDisplay
        Dim pPointSymbol As New ESRI.ArcGIS.Display.MarkerFillSymbol
        Dim pRGBColor As New ESRI.ArcGIS.Display.RgbColor
        Dim pPolygonSymbol As New ESRI.ArcGIS.Display.SimpleFillSymbol
        Dim pOutline As New ESRI.ArcGIS.Display.SimpleLineSymbol
        Dim pRubberBand As ESRI.ArcGIS.Display.IRubberBand2
        Dim pGeometry As ESRI.ArcGIS.Geometry.IGeometry5

            Select Case Globals.DrawFeatureType
                Case "Point"
                    pRubberBand = New ESRI.ArcGIS.Display.RubberPoint
                 Case "Polygon"
                    pRubberBand = New ESRI.ArcGIS.Display.RubberPolygon
                Case "Rectangle"
                    pRubberBand = New ESRI.ArcGIS.Display.RubberEnvelope
                Case "Freehand"
                    System.Windows.Forms.MessageBox.Show("Freehand not coded yet")
                    Return Nothing
                Case Else
                    System.Windows.Forms.MessageBox.Show("Not coded yet")
                    Return Nothing
            End Select

            pGeometry = pRubberBand.TrackNew(screenDisplay, Nothing)
            Return pGeometry

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Get Feature")
            Return Nothing
        End Try

    End Function

End Class