File size: 7,378 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
Imports DWSIM.ExtensionMethods
Imports DWSIM.Interfaces
Imports DWSIM.SharedClasses
Imports Eto.Forms
Imports DWSIM.UI.Shared.Common
Imports DWSIM.UnitOperations.SpecialOps

Public Class GraphicObjectControlPanelModeEditors

    Private Shared Function CreateInputForm() As Dialog

        Dim tb As New TextBox With {.Width = 100}
        Dim f = CreateDialog(tb, "", 200, 60)
        Return f

    End Function

    Public Shared Sub SetInputDelegate(gobj As IGraphicObject, myObj As ISimulationObject)

        gobj.ControlPanelModeEditorDisplayDelegate =
            Sub()
                Dim f = CreateInputForm()
                Dim tb = DirectCast(f.Content, TextBox)
                Dim SelectedObject = myObj?.GetFlowsheet.SimulationObjects.Values.Where(Function(x2) x2.Name = myObj.SelectedObjectID).FirstOrDefault
                If Not SelectedObject Is Nothing Then
                    Dim currentvalue = SystemsOfUnits.Converter.ConvertFromSI(myObj.SelectedPropertyUnits, SelectedObject.GetPropertyValue(myObj.SelectedProperty))
                    tb.Text = currentvalue.ToString(myObj?.GetFlowsheet.FlowsheetOptions.NumberFormat)
                    f.Title = SelectedObject.GraphicObject.Tag + "/" + myObj?.GetFlowsheet.GetTranslatedString(myObj.SelectedProperty)
                    AddHandler tb.KeyDown,
                    Sub(s, e)
                        If e.Key = Keys.Enter Then
                            Try
                                SelectedObject.SetPropertyValue(myObj.SelectedProperty, tb.Text.ToDoubleFromCurrent().ConvertToSI(myObj.SelectedPropertyUnits))
                                f.Close()
                            Catch ex As Exception
                                MessageBox.Show("Error", ex.Message, MessageBoxButtons.OK, MessageBoxType.Error)
                            End Try
                        End If
                    End Sub
                    f.Location = Mouse.Position
                    f.ShowModal()
                End If
            End Sub


    End Sub

    Private Shared Function CreatePIDForm(PIDobj As ISimulationObject) As Dialog

        Dim PID As PIDController = PIDobj

        Dim fs = PID.GetFlowsheet()
        Dim nf = fs.FlowsheetOptions.NumberFormat
        Dim units = fs.FlowsheetOptions.SelectedUnitSystem

        Dim panel = GetDefaultContainer()
        Dim form = CreateDialog(panel, PID.GraphicObject.Tag, 200, 200)

        Dim btn1, btn2 As Button
        Dim tb1, tb2, tb3 As TextBox
        Dim che As New CheckBox

        Dim isActive = PID.Active
        Dim isAuto = Not PID.ManualOverride

        btn1 = panel.CreateAndAddButtonRow(If(isActive, "ON", "OFF"), Nothing,
                                    Sub(btn, e)
                                        If PID Is Nothing Then Exit Sub
                                        isActive = Not isActive
                                        PID.Active = isActive
                                        If isActive Then
                                            btn1.BackgroundColor = Eto.Drawing.Colors.Green
                                            btn1.Text = "ON"
                                            btn2.Enabled = True
                                            tb1.Enabled = True
                                            tb2.Enabled = True
                                            tb3.Enabled = True
                                        Else
                                            btn1.BackgroundColor = Eto.Drawing.Colors.Red
                                            btn1.Text = "OFF"
                                            btn2.Enabled = False
                                            tb1.Enabled = False
                                            tb2.Enabled = False
                                            tb3.Enabled = False
                                        End If
                                    End Sub)

        btn1.BackgroundColor = If(PID.Active, Eto.Drawing.Colors.Green, Eto.Drawing.Colors.Red)
        btn1.TextColor = Eto.Drawing.Colors.White

        btn2 = panel.CreateAndAddButtonRow(If(isAuto, "AUTO", "MANUAL"), Nothing,
                                    Sub(btn, e)
                                        If PID Is Nothing Then Exit Sub
                                        isAuto = Not isAuto
                                        PID.ManualOverride = Not isAuto
                                        tb3.ReadOnly = isAuto
                                        If isAuto Then
                                            btn2.BackgroundColor = Eto.Drawing.Colors.Green
                                            btn2.Text = "AUTO"
                                        Else
                                            btn2.BackgroundColor = Eto.Drawing.Colors.Blue
                                            btn2.Text = "MANUAL"
                                        End If
                                    End Sub)

        btn2.BackgroundColor = If(isAuto, Eto.Drawing.Colors.Green, Eto.Drawing.Colors.Blue)
        btn2.TextColor = Eto.Drawing.Colors.White

        tb1 = panel.CreateAndAddTextBoxRow(nf, "SP", PID.SPValue,
                                               Sub(tb, e)
                                               End Sub)

        tb2 = panel.CreateAndAddTextBoxRow(nf, "PV", PID.PVValue,
                                               Sub(tb, e)
                                               End Sub)

        tb3 = panel.CreateAndAddTextBoxRow(nf, "MV", PID.MVValue,
                                               Sub(tb, e)
                                               End Sub)

        tb2.ReadOnly = True
        tb3.ReadOnly = isAuto

        AddHandler tb1.KeyDown,
            Sub(obj, e)
                If e.Key = Keys.Enter Then
                    Try
                        PID.AdjustValue = tb1.Text.ToDoubleFromCurrent
                        PID.SPValue = PID.AdjustValue
                        form.Close()
                    Catch ex As Exception
                        MessageBox.Show("Error", ex.Message, MessageBoxButtons.OK, MessageBoxType.Error)
                    End Try
                End If
            End Sub

        AddHandler tb3.KeyDown,
            Sub(obj, e)
                If e.Key = Keys.Enter And Not tb3.ReadOnly Then
                    Try
                        PID.MVValue = tb3.Text.ToDoubleFromCurrent
                        form.Close()
                    Catch ex As Exception
                        MessageBox.Show("Error", ex.Message, MessageBoxButtons.OK, MessageBoxType.Error)
                    End Try
                End If
            End Sub

        If isActive Then
            btn2.Enabled = True
            tb1.Enabled = True
            tb2.Enabled = True
            tb3.Enabled = True
        Else
            btn2.Enabled = False
            tb1.Enabled = False
            tb2.Enabled = False
            tb3.Enabled = False
        End If

        Return form

    End Function

    Public Shared Sub SetPIDDelegate(gobj As IGraphicObject, myObj As ISimulationObject)

        gobj.ControlPanelModeEditorDisplayDelegate =
            Sub()
                Dim f = CreatePIDForm(myObj)
                f.Location = Mouse.Position
                f.ShowModal()
            End Sub

    End Sub

End Class