File size: 21,539 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
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
Imports DWSIM.Interfaces
Imports DWSIM.SharedClasses
Imports DWSIM.Interfaces.Enums
Imports DWSIM.Interfaces.Enums.GraphicObjects
Imports AODL.Document

Public Class ReportCreator

    Dim DT As New DataTable
    Protected frm As IFlowsheet
    Dim su As SystemsOfUnits.Units
    Dim nf As String

    Public Sub New(flowsheet As IFlowsheet)
        frm = flowsheet
    End Sub

    Private Sub FillDataTable()

        su = CType(frm.FlowsheetOptions.SelectedUnitSystem, SystemsOfUnits.Units)
        nf = frm.FlowsheetOptions.NumberFormat

        If Not DT.Columns.Contains(("Nome")) Then DT.Columns.Add(("Nome"), GetType(System.String))
        If Not DT.Columns.Contains(("Tipo")) Then DT.Columns.Add(("Tipo"), GetType(System.String))
        If Not DT.Columns.Contains(("Propriedade")) Then DT.Columns.Add(("Propriedade"), GetType(System.String))
        If Not DT.Columns.Contains(("Valor")) Then DT.Columns.Add(("Valor"), GetType(System.String))
        If Not DT.Columns.Contains(("Unidade")) Then DT.Columns.Add(("Unidade"), GetType(System.String))
        DT.Rows.Clear()

        Dim properties() As String
        Dim description As String
        Dim objtype As ObjectType
        Dim propidx, r1, r2, r3, r4, r5, r6 As Integer
        r1 = 5
        r2 = 12
        r3 = 30
        r4 = 48
        r5 = 66
        r6 = 84

        For Each baseobj As ISimulationObject In frm.SimulationObjects.Values
            properties = baseobj.GetProperties(Interfaces.Enums.PropertyType.ALL)
            objtype = baseobj.GraphicObject.ObjectType
            description = frm.GetTranslatedString(baseobj.GraphicObject.Description)
            If objtype = ObjectType.MaterialStream Then
                Dim value As Object
                For propidx = 0 To r1 - 1
                    value = baseobj.GetPropertyValue(properties(propidx), su)
                    If Double.TryParse(value.ToString, New Double) Then
                        DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), Format(Double.Parse(value.ToString), nf), baseobj.GetPropertyUnit(properties(propidx), su)})
                    Else
                        DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), value.ToString, baseobj.GetPropertyUnit(properties(propidx), su)})
                    End If
                Next
                For propidx = r1 To r2 - 1
                    value = baseobj.GetPropertyValue(properties(propidx), su)
                    If Double.TryParse(value.ToString, New Double) Then
                        DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), Format(Double.Parse(value.ToString), nf), baseobj.GetPropertyUnit(properties(propidx), su)})
                    Else
                        DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), value.ToString, baseobj.GetPropertyUnit(properties(propidx), su)})
                    End If
                Next
                DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString("FraomolarnaMistura"), "", ""})
                For Each subst As ICompound In DirectCast(baseobj, IMaterialStream).Phases(0).Compounds.Values
                    DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, subst.Name, Format(subst.MoleFraction.GetValueOrDefault, nf), ""})
                Next
                If DirectCast(baseobj, IMaterialStream).Phases(2).Properties.massflow.HasValue Then
                    For propidx = r2 To r3 - 1
                        value = baseobj.GetPropertyValue(properties(propidx), su)
                        If Double.TryParse(value.ToString, New Double) Then
                            DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), Format(Double.Parse(value.ToString), nf), baseobj.GetPropertyUnit(properties(propidx), su)})
                        Else
                            DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), value.ToString, baseobj.GetPropertyUnit(properties(propidx), su)})
                        End If
                    Next
                    DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString("FraomolarnaPhaseVapor"), "", ""})
                    For Each subst As ICompound In DirectCast(baseobj, IMaterialStream).Phases(2).Compounds.Values
                        DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, subst.Name, Format(subst.MoleFraction.GetValueOrDefault, nf), ""})
                    Next
                End If
                If DirectCast(baseobj, IMaterialStream).Phases(1).Properties.massflow.HasValue Then
                    For propidx = r3 To r4 - 1
                        value = baseobj.GetPropertyValue(properties(propidx), su)
                        If Double.TryParse(value.ToString, New Double) Then
                            DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), Format(Double.Parse(value.ToString), nf), baseobj.GetPropertyUnit(properties(propidx), su)})
                        Else
                            DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), value.ToString, baseobj.GetPropertyUnit(properties(propidx), su)})
                        End If
                    Next
                    DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString("FraomolarnaPhaseLquid"), "", ""})
                    For Each subst As ICompound In DirectCast(baseobj, IMaterialStream).Phases(1).Compounds.Values
                        DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, subst.Name, Format(subst.MoleFraction.GetValueOrDefault, nf), ""})
                    Next
                End If
                If DirectCast(baseobj, IMaterialStream).Phases(3).Properties.massflow.HasValue Then
                    For propidx = r4 To r5 - 1
                        value = baseobj.GetPropertyValue(properties(propidx), su)
                        If Double.TryParse(value.ToString, New Double) Then
                            DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), Format(Double.Parse(value.ToString), nf), baseobj.GetPropertyUnit(properties(propidx), su)})
                        Else
                            DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), value.ToString, baseobj.GetPropertyUnit(properties(propidx), su)})
                        End If
                    Next
                    DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString("FraomolarnaPhaseLquid"), "", ""})
                    For Each subst As ICompound In DirectCast(baseobj, IMaterialStream).Phases(3).Compounds.Values
                        DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, subst.Name, Format(subst.MoleFraction.GetValueOrDefault, nf), ""})
                    Next
                End If
                If DirectCast(baseobj, IMaterialStream).Phases(4).Properties.massflow.HasValue Then
                    For propidx = r5 To r6 - 1
                        value = baseobj.GetPropertyValue(properties(propidx), su)
                        If Double.TryParse(value.ToString, New Double) Then
                            DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), Format(Double.Parse(value.ToString), nf), baseobj.GetPropertyUnit(properties(propidx), su)})
                        Else
                            DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), value.ToString, baseobj.GetPropertyUnit(properties(propidx), su)})
                        End If
                    Next
                    DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString("FraomolarnaPhaseLquid"), "", ""})
                    For Each subst As ICompound In DirectCast(baseobj, IMaterialStream).Phases(4).Compounds.Values
                        DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, subst.Name, Format(subst.MoleFraction.GetValueOrDefault, nf), ""})
                    Next
                End If
                If DirectCast(baseobj, IMaterialStream).Phases(6).Properties.massflow.HasValue Then
                    For propidx = r6 To 101
                        value = baseobj.GetPropertyValue(properties(propidx), su)
                        If Double.TryParse(value.ToString, New Double) Then
                            DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), Format(Double.Parse(value.ToString), nf), baseobj.GetPropertyUnit(properties(propidx), su)})
                        Else
                            DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), value.ToString, baseobj.GetPropertyUnit(properties(propidx), su)})
                        End If
                    Next
                    DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString("FraomolarnaPhaseLquid"), "", ""})
                    For Each subst As ICompound In DirectCast(baseobj, IMaterialStream).Phases(6).Compounds.Values
                        DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, subst.Name, Format(subst.MoleFraction.GetValueOrDefault, nf), ""})
                    Next
                End If
                If DirectCast(baseobj, IMaterialStream).Phases(7).Properties.massflow.HasValue Then
                    For propidx = 131 To 145
                        value = baseobj.GetPropertyValue(properties(propidx), su)
                        If Double.TryParse(value.ToString, New Double) Then
                            DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), Format(Double.Parse(value.ToString), nf), baseobj.GetPropertyUnit(properties(propidx), su)})
                        Else
                            DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(properties(propidx)), value.ToString, baseobj.GetPropertyUnit(properties(propidx), su)})
                        End If
                    Next
                    DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString("FraomolardaPhase"), "", ""})
                    For Each subst As ICompound In DirectCast(baseobj, IMaterialStream).Phases(7).Compounds.Values
                        DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, subst.Name, Format(subst.MoleFraction.GetValueOrDefault, nf), ""})
                    Next
                End If
            Else
                For Each prop As String In properties
                    DT.Rows.Add(New String() {baseobj.GraphicObject.Tag, description, frm.GetTranslatedString(prop), Format(baseobj.GetPropertyValue(prop, su), nf), baseobj.GetPropertyUnit(prop, su)})
                Next
            End If
        Next

    End Sub

    Public Sub CreateAndSaveODTFile(filename As String)

        FillDataTable()

        Dim fname = AODL.Document.Styles.FontFamilies.Arial
        Dim fsize = "10pt"

        'Create a new text document
        Dim document As New TextDocuments.TextDocument()
        document.[New]()

        'Create a table for a text document using the TableBuilder
        Dim table As Content.Tables.Table = Content.Tables.TableBuilder.CreateTextDocumentTable(document, "table1", "table1", 3, 1, 16.99, False, False)

        Dim paragraph As Content.Text.Paragraph = Content.Text.ParagraphBuilder.CreateParagraphWithCustomStyle(document, "p0")
        paragraph.ParagraphStyle.TextProperties.Bold = "bold"
        paragraph.ParagraphStyle.TextProperties.FontName = fname
        paragraph.ParagraphStyle.TextProperties.FontSize = fsize
        paragraph.TextContent.Add(New Content.Text.SimpleText(document, "DWSIM Simulation Results Report"))
        table.Rows(0).Cells(0).Content.Add(paragraph)

        paragraph = Content.Text.ParagraphBuilder.CreateParagraphWithCustomStyle(document, "p1")
        paragraph.ParagraphStyle.TextProperties.FontName = fname
        paragraph.ParagraphStyle.TextProperties.FontSize = fsize
        paragraph.TextContent.Add(New Content.Text.SimpleText(document, "Simulation File: " & frm.FilePath))
        table.Rows(1).Cells(0).Content.Add(paragraph)

        paragraph = Content.Text.ParagraphBuilder.CreateParagraphWithCustomStyle(document, "p2")
        paragraph.ParagraphStyle.TextProperties.FontName = fname
        paragraph.ParagraphStyle.TextProperties.FontSize = fsize
        paragraph.TextContent.Add(New Content.Text.SimpleText(document, "Date created: " & Date.Now.ToString))
        table.Rows(2).Cells(0).Content.Add(paragraph)

        'Add table to the document
        document.Content.Add(table)

        Try

            With document

                Dim i As Integer = 0
                Dim j As Integer = 1

                Dim prevmat, actualmat As String

                table = Content.Tables.TableBuilder.CreateTextDocumentTable(document, "table2", "table2", DT.Rows.Count + frm.SimulationObjects.Count * 3, 3, 16.99, False, False)

                Do
                    actualmat = Me.DT.Rows(i).Item(0).ToString
                    prevmat = Me.DT.Rows(i).Item(0).ToString

                    'Create a standard paragraph
                    paragraph = Content.Text.ParagraphBuilder.CreateParagraphWithCustomStyle(document, "p" + i.ToString() + j.ToString())
                    paragraph.ParagraphStyle.TextProperties.FontName = fname
                    paragraph.ParagraphStyle.TextProperties.FontSize = fsize
                    paragraph.ParagraphStyle.TextProperties.Bold = "bold"

                    'Add some simple text
                    paragraph.TextContent.Add(New Content.Text.SimpleText(document, frm.GetTranslatedString("Objeto") & ": " & prevmat & " (" & Me.DT.Rows(i).Item(1).ToString & ")"))

                    'Insert paragraph into the first cell
                    table.Rows(j).Cells(0).Content.Add(paragraph)

                    j = j + 1
                    Do

                        paragraph = Content.Text.ParagraphBuilder.CreateParagraphWithCustomStyle(document, "p1" + i.ToString() + j.ToString())
                        paragraph.ParagraphStyle.TextProperties.FontName = fname
                        paragraph.ParagraphStyle.TextProperties.FontSize = fsize
                        paragraph.TextContent.Add(New Content.Text.SimpleText(document, Me.DT.Rows(i).Item(2).ToString))
                        table.Rows(j + 1).Cells(0).Content.Add(paragraph)

                        paragraph = Content.Text.ParagraphBuilder.CreateParagraphWithCustomStyle(document, "p2" + i.ToString() + j.ToString())
                        paragraph.ParagraphStyle.ParagraphProperties.Alignment = "right"
                        paragraph.ParagraphStyle.TextProperties.FontName = fname
                        paragraph.ParagraphStyle.TextProperties.FontSize = fsize
                        paragraph.TextContent.Add(New Content.Text.SimpleText(document, Me.DT.Rows(i).Item(3).ToString))
                        table.Rows(j + 1).Cells(1).Content.Add(paragraph)

                        paragraph = Content.Text.ParagraphBuilder.CreateParagraphWithCustomStyle(document, "p3" + i.ToString() + j.ToString())
                        paragraph.ParagraphStyle.ParagraphProperties.MarginLeft = "0.2cm"
                        paragraph.ParagraphStyle.TextProperties.FontName = fname
                        paragraph.ParagraphStyle.TextProperties.FontSize = fsize
                        paragraph.TextContent.Add(New Content.Text.SimpleText(document, Me.DT.Rows(i).Item(4).ToString))
                        table.Rows(j + 1).Cells(2).Content.Add(paragraph)

                        i = i + 1
                        j = j + 1

                        If i < DT.Rows.Count Then actualmat = Me.DT.Rows(i).Item(0).ToString

                    Loop Until actualmat <> prevmat Or i >= DT.Rows.Count

                    j = j + 2

                Loop Until i >= DT.Rows.Count

            End With

            'Add table to the document
            document.Content.Add(table)

            'Save the document
            Using writer = New AODL.IO.OnDiskPackageWriter()
                document.Save(filename, New Export.OpenDocument.OpenDocumentTextExporter(writer))
            End Using

            frm.ShowMessage("Report saved sucessfully.", IFlowsheet.MessageType.Information)

        Catch ex As Exception

            frm.ShowMessage("Error saving report file: " + ex.ToString, IFlowsheet.MessageType.GeneralError)

        Finally

        End Try

    End Sub

    Public Sub CreateAndSaveODSFile(filename As String)

        FillDataTable()

        Dim sheetdoc As New SpreadsheetDocuments.SpreadsheetDocument()
        sheetdoc.[New]()
        Dim mysheet As New Content.Tables.Table(sheetdoc, "DWSIM_Report", "report")
        mysheet.Rows.Add(New Content.Tables.Row(mysheet))

        Try

            With mysheet

                Dim i As Integer = 0
                Dim j As Integer = 1

                Dim prevmat, actualmat As String

                Do

                    actualmat = Me.DT.Rows(i).Item(0).ToString
                    prevmat = Me.DT.Rows(i).Item(0).ToString
                    Dim cell = mysheet.CreateCell()
                    cell.OfficeValueType = "string"
                    Dim paragraph = Content.Text.ParagraphBuilder.CreateSpreadsheetParagraph(sheetdoc)
                    paragraph.TextContent.Add(New Content.Text.SimpleText(sheetdoc, frm.GetTranslatedString("Objeto") & ": " & prevmat & " (" & Me.DT.Rows(i).Item(1).ToString & ")"))
                    cell.Content.Add(paragraph)
                    mysheet.Rows.Add(New Content.Tables.Row(mysheet))
                    mysheet.Rows.Add(New Content.Tables.Row(mysheet))
                    mysheet.InsertCellAt(j, 0, cell)

                    j = j + 1

                    Do
                        mysheet.Rows.Add(New Content.Tables.Row(mysheet))
                        Dim cell0 = mysheet.CreateCell()
                        cell0.OfficeValueType = "string"
                        Dim paragraph0 = Content.Text.ParagraphBuilder.CreateSpreadsheetParagraph(sheetdoc)
                        paragraph0.TextContent.Add(New Content.Text.SimpleText(sheetdoc, Me.DT.Rows(i).Item(2).ToString))
                        cell0.Content.Add(paragraph0)
                        mysheet.InsertCellAt(j + 1, 0, cell0)
                        Dim cell1 = mysheet.CreateCell()
                        cell1.OfficeValueType = "string"
                        Dim paragraph1 = Content.Text.ParagraphBuilder.CreateSpreadsheetParagraph(sheetdoc)
                        paragraph1.TextContent.Add(New Content.Text.SimpleText(sheetdoc, Me.DT.Rows(i).Item(3).ToString))
                        cell1.Content.Add(paragraph1)
                        mysheet.InsertCellAt(j + 1, 1, cell1)
                        Dim cell2 = mysheet.CreateCell()
                        cell2.OfficeValueType = "string"
                        Dim paragraph2 = Content.Text.ParagraphBuilder.CreateSpreadsheetParagraph(sheetdoc)
                        paragraph2.TextContent.Add(New Content.Text.SimpleText(sheetdoc, Me.DT.Rows(i).Item(4).ToString))
                        cell2.Content.Add(paragraph2)
                        mysheet.InsertCellAt(j + 1, 2, cell2)
                        i = i + 1
                        j = j + 1
                        If i < DT.Rows.Count Then actualmat = Me.DT.Rows(i).Item(0).ToString
                    Loop Until actualmat <> prevmat Or i >= DT.Rows.Count

                    mysheet.Rows.Add(New Content.Tables.Row(mysheet))
                    mysheet.Rows.Add(New Content.Tables.Row(mysheet))

                    j = j + 2

                Loop Until i >= DT.Rows.Count

            End With

            sheetdoc.TableCollection.Add(mysheet)

            Using writer As New AODL.IO.OnDiskPackageWriter
                sheetdoc.Save(filename, New Export.OpenDocument.OpenDocumentTextExporter(writer))
            End Using

            frm.ShowMessage("Report saved sucessfully.", IFlowsheet.MessageType.Information)

        Catch ex As Exception

            frm.ShowMessage("Error saving report file: " + ex.ToString, IFlowsheet.MessageType.GeneralError)

        Finally


        End Try

    End Sub

End Class