ThinkGeo.com    |     Documentation    |     Premium Support

Create my own LineStyle

Is it possible to create my own LineStyle ?


I need to draw arrows for example. I tried to define my own LineStyle class and overloaded the Draw method but it did not work.


Thanks



Le Sommer,


First welcome to the community, hope you enjoy learning and sharing here.


Here I created a new LineStyle which draw a green circle at the end of a line. You can use the similar way to customize and draw an arrow. 


Public Class MyLineStyle 
    Inherits LineStyle 
    Protected Overloads Overrides Sub DrawCore(ByVal features As IEnumerable(Of Feature), ByVal canvas As GeoCanvas, ByVal labelsInThisLayer As Collection(Of SimpleCandidate), ByVal labelsInAllLayers As Collection(Of SimpleCandidate)) 
        For Each feature As Feature In features 
            Dim line As LineShape = DirectCast(feature.GetShape(), LineShape) 
            Dim endPoint As New PointShape(line.Vertices(1)) 
            
            canvas.DrawLine(feature, New GeoPen(GeoColor.StandardColors.Black), DrawingLevel.LevelOne) 
            ' It should have no offsets (the 7th and 8th arguments) when setting the small green circle but as a bug, here we set offset (-2.5, -2.5) for it to make it at the center of the line edge. We will fix it in the future release. 
            canvas.DrawEllipse(endPoint, 5F, 5F, New GeoPen(GeoColor.StandardColors.Green), New GeoSolidBrush(GeoColor.StandardColors.Green), DrawingLevel.LevelFour, -2.5F, -2.5F, PenBrushDrawingOrder.BrushFirst) 
        Next 
    End Sub 
End Class 

Here is what it looks like.


Thanks,


Ben



I tried this sample on my InMemoryFeatureLayer like this :


Dim lineLayer As New InMemoryFeatureLayer()
lineLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = New MyLineStyle
lineLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20


But nothing is drawn, the DrawCore is not called.  Thanks for your help !



Le Sommer,


There are 3 pens within LineStyle (OuterPen, InnerPen and CenterPen), the system will check them before drawing and if none of them is set, the lineStyle is considered as a blank style and will not be drew.
 
Here is the code it works

       lineLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = New MyLineStyle(New GeoPen(GeoColor.StandardColors.Red))
        lineLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20
 

And here attached is the complete demo

Thanks,


Ben



428-MyLineStyle.zip (15.8 KB)

It's OK now


Thanks



my pleasure:)

How do you draw an arrow in stead of a circle at the end of the line?

Here my method to draw arrows :



Protected Overloads Overrides Sub DrawCore(ByVal features As IEnumerable(Of Feature), ByVal canvas As GeoCanvas, ByVal labelsInThisLayer As Collection(Of SimpleCandidate), ByVal labelsInAllLayers As Collection(Of SimpleCandidate))
        For Each feature As Feature In features
            Dim line As LineShape = DirectCast(feature.GetShape(), LineShape)
            
            Dim X1 As Double = line.Vertices(0).X
            Dim Y1 As Double = line.Vertices(0).Y
            Dim X2 As Double = line.Vertices(1).X
            Dim Y2 As Double = line.Vertices(1).Y

            Dim angle As Double = Math.Atan((Y2 - Y1) / (X2 - X1))
            Dim openingAngle As Double = Math.PI / 8
            Dim radius As Double = _Map.CurrentScale / 40000000
           
            canvas.DrawLine(feature, OuterPen, DrawingLevel.LevelOne)
           
            Dim oLineShape As New LineShape
            oLineShape.Vertices.Add(New Vertex(X2 + radius * Math.Cos(Math.PI + angle - openingAngle), Y2 + radius * Math.Sin(Math.PI + angle - openingAngle)))
            oLineShape.Vertices.Add(New Vertex(X2, Y2))
            oLineShape.Vertices.Add(New Vertex(X2 + radius * Math.Cos(Math.PI + angle + openingAngle), Y2 + radius * Math.Sin(Math.PI + angle + openingAngle)))
            canvas.DrawLine(oLineShape, OuterPen, DrawingLevel.LevelOne)
        Next
    End Sub


Thanks

Le Sommer, Thanks for sharing! 
  
 Ben