File size: 4,498 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
Imports Interfaces = DWSIM.Interfaces
Imports DWSIM.Interfaces
Imports DWSIM.Interfaces.Enums.GraphicObjects

Namespace GraphicObjects

    Public Class TextGraphic

        Inherits GraphicObject

#Region "Constructors"

        Public Sub New()

            Me.ObjectType = Interfaces.Enums.GraphicObjects.ObjectType.GO_Text
            Me.Height = 20
            Me.Width = 50

        End Sub

        Public Sub New(ByVal graphicPosition As SKPoint, ByVal text As String)
            Me.New()
            Me.SetPosition(graphicPosition)
            Me.Text = text
        End Sub

        Public Sub New(ByVal posX As Integer, ByVal posY As Integer, ByVal text As String)
            Me.New(New SKPoint(posX, posY), text)
        End Sub

#End Region

        Public Property Text As String = "TEXT (double-click to edit)"

        Public Property Size As Double = 14.0#

        Public Property Color As SKColor = SKColors.Black

        Public Overrides Sub Draw(ByVal g As Object)

            Dim canvas As SKCanvas = DirectCast(g, SKCanvas)

            Dim tpaint As New SKPaint()

            If DrawMode = 0 Then
                With tpaint
                    .TextSize = Size
                    .IsAntialias = GlobalSettings.Settings.DrawingAntiAlias
                    .Color = If(GlobalSettings.Settings.DarkMode, SKColors.LightSteelBlue, Color)
                    .IsStroke = False
                    .Typeface = GetFont()
                End With
            Else
                With tpaint
                    .TextSize = Size
                    .IsAntialias = GlobalSettings.Settings.DrawingAntiAlias
                    .Color = SKColors.Black
                    .IsStroke = False
                    .Typeface = GetFont()
                End With
            End If

            Dim newtext = ReplaceVars(Text)

            Dim lines = newtext.Split(vbLf)

            Dim newy As Integer = Y

            Try
                Height = 0
                Width = 0
                For Each l As String In lines
                    Dim trect As New SKRect(0, 0, 2, 2)
                    tpaint.GetTextPath(l, 0, 0).GetBounds(trect)
                    newy += trect.Height + 2
                    Height += trect.Height + 2
                    Width = Math.Max(Width, trect.Width)
                    canvas.DrawText(l, X, newy, tpaint)
                Next
            Catch ex As Exception
                Dim trect As New SKRect(0, 0, 2, 2)
                tpaint.GetTextPath(Text.Replace("\n", vbCrLf), 0, 0).GetBounds(trect)
                Height = trect.Height
                Width = trect.Width
                canvas.DrawText(Text, X, Y + MeasureString(newtext, tpaint).Height, tpaint)
            End Try

        End Sub

        Private Function ReplaceVars(oldtext As String) As String

            Dim newtext As String = oldtext
            Dim i As Integer = 0

            If Flowsheet IsNot Nothing Then
                If Text.Contains("{") And Text.Contains("}") Then
                    For i = 1 To Flowsheet.WatchItems.Count
                        Dim objID = Flowsheet.WatchItems(i - 1).ObjID
                        Dim propID = Flowsheet.WatchItems(i - 1).PropID
                        If Flowsheet.SimulationObjects.ContainsKey(objID) Then
                            Dim units = Flowsheet.SimulationObjects(objID).GetPropertyUnit(propID)
                            Dim name = Flowsheet.GetTranslatedString(propID)
                            newtext = newtext.Replace("{" + i.ToString() + ":N}", name)
                            newtext = newtext.Replace("{" + i.ToString() + ":U}", units)
                            Dim value = Flowsheet.SimulationObjects(objID).GetPropertyValue(propID).ToString()
                            If Double.TryParse(value, New Double) Then
                                Dim dval = Double.Parse(value).ToString(Flowsheet.FlowsheetOptions.NumberFormat)
                                newtext = newtext.Replace("{" + i.ToString() + ":V}", dval)
                            Else
                                newtext = newtext.Replace("{" + i.ToString() + ":V}", value)
                            End If
                        End If
                    Next
                End If
            End If

            Return newtext

        End Function

    End Class

End Namespace