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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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
'Copyright (C) 2002 Microsoft Corporation
'All rights reserved.
'
'THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
'EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
'MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
'
'Date: June 2002
'Author: Duncan Mackenzie
'
'Requires the release version of .NET Framework

Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports DWSIM.Interfaces
Imports System.Xml.Linq
Imports DWSIM.Interfaces.Enums
Imports DWSIM.Interfaces.Enums.GraphicObjects

Namespace GraphicObjects

    <System.Serializable()> Public MustInherit Class GraphicObject

        Implements IGraphicObject

        Friend m_Rotation As Single

        Public Function IsRunningOnMono() As Boolean
            Return Not Type.GetType("Mono.Runtime") Is Nothing
        End Function

        Public Function GetBoundsRectangle() As Rectangle
            Return New Rectangle(X, Y, Width, Height)
        End Function

        Public Shared Function ReturnInstance(typename As String) As IGraphicObject
            Dim t As Type = Type.GetType(typename, False)
            If Not t Is Nothing Then Return Activator.CreateInstance(t) Else Return Nothing
        End Function

        Public Overridable Property Calculated() As Boolean Implements IGraphicObject.Calculated
            Get
                Select Case Status
                    Case Status.Calculated
                        Return True
                    Case Status.Calculating
                        Return False
                    Case Status.ErrorCalculating
                        Return False
                    Case Status.Idle
                        Return True
                    Case Status.Inactive
                        Return False
                    Case Status.NotCalculated
                        Return False
                    Case Else
                        Return False
                End Select
            End Get
            Set(ByVal Value As Boolean)
                If Value = False Then
                    Status = Status.ErrorCalculating
                Else
                    Status = Status.Calculated
                End If
            End Set
        End Property

        Public Overridable Overloads Function HitTest(ByVal pt As System.Drawing.Point) As Boolean
            Dim gp As New Drawing2D.GraphicsPath()
            Dim myMatrix As New Drawing2D.Matrix()
            gp.AddRectangle(New Rectangle(X, Y, Width, Height))
            If Me.Rotation <> 0 Then
                myMatrix.RotateAt(Me.Rotation, New PointF(Me.X + Me.Width / 2, Me.Y + Me.Height / 2), _
                    Drawing.Drawing2D.MatrixOrder.Append)
            End If
            gp.Transform(myMatrix)
            Return gp.IsVisible(pt)
        End Function

        Public Overridable Overloads Function HitTest(ByVal rect As Rectangle) As Boolean
            'is this object contained within the supplied rectangle
            Dim gp As New Drawing2D.GraphicsPath()
            Dim myMatrix As New Drawing2D.Matrix()
            gp.AddRectangle(New Rectangle(X, Y, Width, Height))
            If Me.Rotation <> 0 Then
                myMatrix.RotateAt(Me.Rotation, New PointF(Me.X + Me.Width / 2, Me.Y + Me.Height / 2), _
                    Drawing.Drawing2D.MatrixOrder.Append)
            End If
            gp.Transform(myMatrix)
            Dim gpRect As Rectangle = Rectangle.Round(gp.GetBounds)
            Return rect.Contains(gpRect)
        End Function

#Region "Constructors"
        Protected Sub New()
        End Sub

        Protected Sub New(ByVal graphicPosition As Point)
            Me.New()
            Me.SetPosition(graphicPosition)
        End Sub

        Protected Sub New(ByVal posX As Integer, ByVal posY As Integer)
            Me.New(New Point(posX, posY))
        End Sub

        Protected Sub New(ByVal graphicPosition As Point, ByVal graphicSize As Size)
            Me.New(graphicPosition)
            Me.SetSize(graphicSize)
            Me.AutoSize = False
        End Sub

        Protected Sub New(ByVal posX As Integer, ByVal posY As Integer, ByVal graphicSize As Size)
            Me.New(New Point(posX, posY), graphicSize)
        End Sub

        Protected Sub New(ByVal posX As Integer, ByVal posY As Integer, _
                ByVal width As Integer, ByVal height As Integer)
            Me.New(New Point(posX, posY), New Size(width, height))
        End Sub

        Protected Sub New(ByVal graphicPosition As Point, ByVal Rotation As Single)
            Me.New()
            Me.SetPosition(graphicPosition)
            Me.Rotation = Rotation
        End Sub

        Protected Sub New(ByVal posX As Integer, ByVal posY As Integer, ByVal Rotation As Single)
            Me.New(New Point(posX, posY), Rotation)
        End Sub

        Protected Sub New(ByVal graphicPosition As Point, ByVal graphicSize As Size, ByVal Rotation As Single)
            Me.New(graphicPosition, Rotation)
            Me.SetSize(graphicSize)
            Me.AutoSize = False
        End Sub

        Protected Sub New(ByVal posX As Integer, ByVal posY As Integer, _
            ByVal graphicSize As Size, ByVal Rotation As Single)
            Me.New(New Point(posX, posY), graphicSize, Rotation)
        End Sub

        Protected Sub New(ByVal posX As Integer, ByVal posY As Integer, ByVal width As Integer, _
                               ByVal height As Integer, ByVal Rotation As Single)
            Me.New(New Point(posX, posY), New Size(width, height), Rotation)
        End Sub

#End Region

        Public Overridable Function GetPosition() As Point
            Dim myPosition As New Point(X, Y)
            Return myPosition
        End Function

        Public Overridable Sub SetPosition(ByVal Value As Point)
            X = Value.X
            Y = Value.Y
        End Sub

        Public Overridable Sub SetSize(ByVal Value As Size)
            Width = Value.Width
            Height = Value.Height
        End Sub

        Public Overridable Function GetSize() As Size
            Dim mySize As New Size(Width, Height)
            Return mySize
        End Function

        Public Overridable Property Rotation() As Integer Implements IGraphicObject.Rotation
            Get
                Return m_Rotation
                If IsRunningOnMono() Then Return 0
            End Get
            Set(ByVal Value As Integer)
                If System.Math.Abs(Value) <= 360 Then
                    m_Rotation = Value
                    If IsRunningOnMono() Then m_Rotation = 0
                Else
                    Throw New ArgumentOutOfRangeException("Rotation", "Rotation must be between -360.0 and 360.0")
                End If
            End Set
        End Property

        Public Overridable Sub Draw(ByVal g As Graphics)

            PositionConnectors()

            RotateConnectors()

        End Sub

        Public Sub DrawRoundRect(ByVal g As Graphics, ByVal p As Pen, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal radius As Single, ByVal myBrush As Brush)

            If width / 2 < radius Then
                radius = width / 2 - 2
            ElseIf height / 2 < radius Then
                radius = height / 2 - 2
            End If

            Dim gp As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath

            gp.AddLine(x + radius, y, x + width - radius, y)
            gp.AddArc(x + width - radius, y, radius, radius, 270, 90)
            gp.AddLine(x + width, y + radius, x + width, y + height - radius)
            gp.AddArc(x + width - radius, y + height - radius, radius, radius, 0, 90)
            gp.AddLine(x + width - radius, y + height, x + radius, y + height)
            gp.AddArc(x, y + height - radius, radius, radius, 90, 90)
            gp.AddLine(x, y + height - radius, x, y + radius)
            gp.AddArc(x, y, radius, radius, 180, 90)

            gp.CloseFigure()

            g.DrawPath(p, gp)
            g.FillPath(myBrush, gp)

            gp.Dispose()

        End Sub

        Public Overridable Sub PositionConnectors()

            'To be implemented in derived classes.

        End Sub

        Public Sub RotateConnectors()

            Dim center As Point = New Point(X + Me.Width / 2, Y + Me.Height / 2)
            Dim pt As Point
            Dim raio, angulo As Double
            Dim con As ConnectionPoint
            For Each con In Me.InputConnectors
                pt = con.Position
                raio = ((pt.X - center.X) ^ 2 + (pt.Y - center.Y) ^ 2) ^ 0.5
                angulo = Math.Atan2(pt.Y - center.Y, pt.X - center.X)
                pt.X = center.X + raio * Math.Cos(angulo + Me.Rotation / 360 * 2 * Math.PI)
                pt.Y = center.Y + raio * Math.Sin(angulo + Me.Rotation / 360 * 2 * Math.PI)
                con.Position = pt
            Next
            For Each con In Me.OutputConnectors
                pt = con.Position
                raio = ((pt.X - center.X) ^ 2 + (pt.Y - center.Y) ^ 2) ^ 0.5
                angulo = Math.Atan2(pt.Y - center.Y, pt.X - center.X)
                pt.X = center.X + raio * Math.Cos(angulo + Me.Rotation / 360 * 2 * Math.PI)
                pt.Y = center.Y + raio * Math.Sin(angulo + Me.Rotation / 360 * 2 * Math.PI)
                con.Position = pt
            Next
            With Me.EnergyConnector
                pt = .Position
                raio = ((pt.X - center.X) ^ 2 + (pt.Y - center.Y) ^ 2) ^ 0.5
                angulo = Math.Atan2(pt.Y - center.Y, pt.X - center.X)
                pt.X = center.X + raio * Math.Cos(angulo + Me.Rotation / 360 * 2 * Math.PI)
                pt.Y = center.Y + raio * Math.Sin(angulo + Me.Rotation / 360 * 2 * Math.PI)
                .Position = pt
            End With

        End Sub

        Public Overridable Sub CreateConnectors(ByVal InCount As Integer, ByVal OutCount As Integer)

            'Creates all the connection points.

            For I As Integer = 1 To InCount

                Dim Con As New ConnectionPoint
                Con.Type = ConType.ConIn
                InputConnectors.Add(Con)

            Next

            For I As Integer = 1 To OutCount

                Dim Con As New ConnectionPoint
                Con.Type = ConType.ConOut
                OutputConnectors.Add(Con)

            Next

        End Sub

        Public Overridable Function LoadData(data As System.Collections.Generic.List(Of System.Xml.Linq.XElement)) As Boolean

            XMLSerializer.XMLSerializer.Deserialize(Me, data)

            'DWSIM Mobile compatibility

            If ObjectType = Enums.GraphicObjects.ObjectType.CompressorExpander Then ObjectType = Enums.GraphicObjects.ObjectType.Compressor
            If ObjectType = Enums.GraphicObjects.ObjectType.HeaterCooler Then ObjectType = Enums.GraphicObjects.ObjectType.Heater

            'Other Properties

            Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.InvariantCulture
            Dim xel As XElement = (From xel2 As XElement In data Select xel2 Where xel2.Name = "AdditionalInfo").SingleOrDefault

            If Not xel Is Nothing Then
                Dim arr As New ArrayList
                For Each xel2 As XElement In xel.Elements
                    arr.Add(XMLSerializer.XMLSerializer.StringToArray2(xel2.Value, ci, Type.GetType("System.Double")))
                Next
                Me.AdditionalInfo = arr.ToArray(Type.GetType("System.Object"))
            End If
            Return True

        End Function

        Public Overridable Function SaveData() As System.Collections.Generic.List(Of System.Xml.Linq.XElement)

            Dim elements As List(Of XElement) = XMLSerializer.XMLSerializer.Serialize(Me)
            Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.InvariantCulture

            With elements

                If Not Me.AdditionalInfo Is Nothing Then
                    .Add(New XElement("AdditionalInfo", New XElement("Info", XMLSerializer.XMLSerializer.ArrayToString2(Me.AdditionalInfo(0), ci)),
                                                        New XElement("Info", XMLSerializer.XMLSerializer.ArrayToString2(Me.AdditionalInfo(1), ci)),
                                                        New XElement("Info", XMLSerializer.XMLSerializer.ArrayToString2(Me.AdditionalInfo(2), ci)),
                                                        New XElement("Info", XMLSerializer.XMLSerializer.ArrayToString2(Me.AdditionalInfo(3), ci))))
                End If

                .Add(New XElement("InputConnectors"))

                For Each cp As ConnectionPoint In InputConnectors
                    If cp.IsAttached And Not cp.AttachedConnector Is Nothing Then
                        elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", cp.IsAttached.ToString(ci)),
                                                                                        New XAttribute("ConnType", cp.Type.ToString),
                                                                                        New XAttribute("AttachedFromObjID", cp.AttachedConnector.AttachedFrom.Name.ToString),
                                                                                        New XAttribute("AttachedFromConnIndex", cp.AttachedConnector.AttachedFromConnectorIndex),
                                                                                        New XAttribute("AttachedFromEnergyConn", cp.AttachedConnector.AttachedFromEnergy.ToString)))
                    Else
                        elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", cp.IsAttached.ToString(ci))))
                    End If
                Next

                .Add(New XElement("OutputConnectors"))

                For Each cp As ConnectionPoint In OutputConnectors
                    If cp.IsAttached And Not cp.AttachedConnector Is Nothing Then
                        elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", cp.IsAttached.ToString(ci)), _
                                                                                        New XAttribute("ConnType", cp.Type.ToString),
                                                                                        New XAttribute("AttachedToObjID", cp.AttachedConnector.AttachedTo.Name.ToString), _
                                                                                        New XAttribute("AttachedToConnIndex", cp.AttachedConnector.AttachedToConnectorIndex), _
                                                                                        New XAttribute("AttachedToEnergyConn", cp.AttachedConnector.AttachedToEnergy.ToString)))
                    Else
                        elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", cp.IsAttached.ToString(ci))))
                    End If
                Next

                .Add(New XElement("EnergyConnector"))

                If EnergyConnector.IsAttached Then
                    elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", EnergyConnector.IsAttached.ToString(ci)),
                                                                                    New XAttribute("AttachedToObjID", EnergyConnector.AttachedConnector.AttachedTo.Name.ToString), _
                                                                                        New XAttribute("AttachedToConnIndex", EnergyConnector.AttachedConnector.AttachedToConnectorIndex), _
                                                                                        New XAttribute("AttachedToEnergyConn", EnergyConnector.AttachedConnector.AttachedToEnergy.ToString)), _
                                                                                        New XAttribute("AttachedFromObjID", EnergyConnector.AttachedConnector.AttachedFrom.Name.ToString), _
                                                                                        New XAttribute("AttachedFromConnIndex", EnergyConnector.AttachedConnector.AttachedFromConnectorIndex), _
                                                                                        New XAttribute("AttachedFromEnergyConn", EnergyConnector.AttachedConnector.AttachedFromEnergy.ToString))
                Else
                    elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", EnergyConnector.IsAttached.ToString(ci))))
                End If

                .Add(New XElement("SpecialConnectors"))

                For Each cp As ConnectionPoint In SpecialConnectors
                    If cp.IsAttached Then
                        elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", cp.IsAttached.ToString(ci)),
                                                                                        New XAttribute("AttachedToObjID", cp.AttachedConnector.AttachedTo.Name.ToString), _
                                                                                        New XAttribute("AttachedToConnIndex", cp.AttachedConnector.AttachedToConnectorIndex), _
                                                                                        New XAttribute("AttachedToEnergyConn", cp.AttachedConnector.AttachedToEnergy.ToString)), _
                                                                                        New XAttribute("AttachedFromObjID", cp.AttachedConnector.AttachedFrom.Name.ToString), _
                                                                                        New XAttribute("AttachedFromConnIndex", cp.AttachedConnector.AttachedFromConnectorIndex), _
                                                                                        New XAttribute("AttachedFromEnergyConn", cp.AttachedConnector.AttachedFromEnergy.ToString))
                    Else
                        elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", cp.IsAttached.ToString(ci))))
                    End If
                Next

            End With

            Return elements

        End Function

        Public Property Active As Boolean = True Implements IGraphicObject.Active

        Public Property AdditionalInfo As Object = Nothing Implements IGraphicObject.AdditionalInfo

        Public Property Description As String = "" Implements IGraphicObject.Description

        Public Property FlippedH As Boolean = False Implements IGraphicObject.FlippedH

        Public Property FlippedV As Boolean = False Implements IGraphicObject.FlippedV

        Public Property IsEnergyStream As Boolean = False Implements IGraphicObject.IsEnergyStream

        Public Property ObjectType As ObjectType = Enums.GraphicObjects.ObjectType.Nenhum Implements IGraphicObject.ObjectType

        Public Property Shape As Integer = 0 Implements IGraphicObject.Shape

        Public Property ShapeOverride As ShapeIcon = ShapeIcon.DefaultShape Implements IGraphicObject.ShapeOverride

        Public Property Status As Status = Enums.GraphicObjects.Status.NotCalculated Implements IGraphicObject.Status

        Public Property AutoSize As Boolean = False Implements IGraphicObject.AutoSize

        Public Property Height As Integer = 20 Implements IGraphicObject.Height

        Public Property IsConnector As Boolean = False Implements IGraphicObject.IsConnector

        Public Property Name As String = "" Implements IGraphicObject.Name

        Public Property Tag As String = "" Implements IGraphicObject.Tag

        Public Property Width As Integer = 20 Implements IGraphicObject.Width

        Public Property X As Integer = 0 Implements IGraphicObject.X

        Public Property Y As Integer = 0 Implements IGraphicObject.Y

        Public Property EnergyConnector As IConnectionPoint = New ConnectionPoint() Implements IGraphicObject.EnergyConnector

        Public Property InputConnectors As New List(Of IConnectionPoint) Implements IGraphicObject.InputConnectors

        Public Property OutputConnectors As New List(Of IConnectionPoint) Implements IGraphicObject.OutputConnectors

        Public Property SpecialConnectors As New List(Of IConnectionPoint) Implements IGraphicObject.SpecialConnectors

        Public Property Position As IPoint = New Point() Implements IGraphicObject.Position

        Public Function Clone() As IGraphicObject Implements IGraphicObject.Clone
            Dim instance As GraphicObject = Activator.CreateInstance(Me.GetType)
            instance.LoadData(Me.SaveData)
            Return instance
        End Function

        Public Sub Draw1(surface As Object) Implements IGraphicObject.Draw
            Draw(DirectCast(surface, System.Drawing.Graphics))
        End Sub

        Public Property Selected As Boolean = False Implements IGraphicObject.Selected

        <Xml.Serialization.XmlIgnore> Public Property Owner As ISimulationObject Implements IGraphicObject.Owner

        <Xml.Serialization.XmlIgnore> <NonSerialized> Private _editorform As System.Windows.Forms.Form

        Public Property Editor As Object Implements IGraphicObject.Editor
            Get
                Return _editorform
            End Get
            Set(value As Object)
                _editorform = value
            End Set
        End Property

        Public Function HitTest1(zoomedSelection As Object) As Boolean Implements IGraphicObject.HitTest

            Return HitTest(zoomedSelection)

        End Function

        Public Property DrawOverride As Action(Of Object) Implements IGraphicObject.DrawOverride

        Public Property Extensions As New Dictionary(Of String, IGraphicObjectExtension) Implements IGraphicObject.Extensions

    End Class

End Namespace