File size: 8,976 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
Imports System.Windows.Forms
Imports DWSIM.ExtensionMethods
Imports DWSIM.Interfaces
Imports DWSIM.SharedClassesCSharp.FilePicker

Public Class Window2

    Inherits WeifenLuo.WinFormsUI.Docking.DockContent

    Public SelectedObject As ISimulationObject

    Private Sub Window_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Viewer.EnsureCoreWebView2Async()

        SetupInspectorWindow()

    End Sub

    Public Sub Populate()

        Dim f As New Loading

        With f

            .Label1.Text = "Loading reports..."
            .ProgressBar1.Value = 0

        End With

        Me.Enabled = False

        f.Show()

        Dim sitems = Host.Items.Where(Function(x) x.Name.Contains(SelectedObject.GraphicObject.Tag))

        Dim tvc As New List(Of TreeNode)

        Dim ct As New Threading.CancellationTokenSource

        AddHandler f.btnCancel.Click, Sub()
                                          ct.Cancel()
                                      End Sub

        Dim allitems As New List(Of InspectorItem)

        Task.Factory.StartNew(Sub()
                                  Me.UIThread(Sub()
                                                  allitems = GetItems(Host.Items)
                                              End Sub)
                                  Dim i As Integer = 1
                                  For Each item In sitems.Where(Function(x) x.ParentID = -1)
                                      Dim timetaken = item.TimeTaken.TotalMilliseconds.ToString("N0") + " ms"
                                      If timetaken = "0 ms" Then timetaken = (item.TimeTaken.TotalMilliseconds * 1000).ToString("N0") + " µs"
                                      If timetaken = "0 µs" Then timetaken = (item.TimeTaken.TotalMilliseconds * 1000000).ToString("N0") + " ns"
                                      Dim titem = New TreeNode() With {.Text = item.Name + " (" + timetaken + ")", .Tag = item.ID}
                                      tvc.Add(titem)
                                      Me.UIThread(Sub()
                                                      f.Label1.Text = String.Format("Loading reports... ({0}/{1})", i, allitems.Count)
                                                      f.ProgressBar1.Value = CDbl(i / allitems.Count) * 100
                                                      i += 1
                                                  End Sub)
                                      Dim nesteditems = GetItems(item)
                                      For Each item2 In nesteditems
                                          Dim parent = GetAllTreeItems(tvc).Where(Function(x) DirectCast(x, TreeNode).Tag = item2.ParentID).FirstOrDefault
                                          Dim timetaken2 = item2.TimeTaken.TotalMilliseconds.ToString("N0") + " ms"
                                          If timetaken2 = "0 ms" Then timetaken2 = (item2.TimeTaken.TotalMilliseconds * 1000).ToString("N0") + " µs"
                                          If timetaken2 = "0 µs" Then timetaken2 = (item2.TimeTaken.TotalMilliseconds * 1000000).ToString("N0") + " ns"
                                          Dim titem2 = New TreeNode() With {.Text = item2.Name + " (" + timetaken2 + ")", .Tag = item2.ID}
                                          If parent Is Nothing Then
                                              tvc.Add(titem2)
                                          Else
                                              DirectCast(parent, TreeNode).Nodes.Add(titem2)
                                          End If
                                          Me.UIThread(Sub()
                                                          f.Label1.Text = String.Format("Loading reports... ({0}/{1})", i, allitems.Count)
                                                          f.ProgressBar1.Value = CDbl(i / allitems.Count) * 100
                                                          i += 1
                                                      End Sub)
                                          If ct.IsCancellationRequested Then Throw New TaskCanceledException()
                                      Next
                                  Next
                              End Sub, ct.Token).ContinueWith(Sub()
                                                                  Me.UIThread(Sub()
                                                                                  itemSelector.Nodes.AddRange(tvc.ToArray)
                                                                                  Me.Enabled = True
                                                                                  f.Close()
                                                                                  itemSelector.Select()
                                                                                  itemSelector.SelectedNode = itemSelector.Nodes(0)
                                                                                  'itemSelector.ExpandAll()
                                                                              End Sub)
                                                              End Sub)


    End Sub

    Public Sub SetupInspectorWindow()

        'Events

        Dim avsol As List(Of String) = Host.Items.Select(Of String)(Function(x) x.SolutionID).Distinct().ToList



        AddHandler itemSelector.AfterSelect,
            Sub(sender, e)

                If itemSelector.SelectedNode IsNot Nothing Then
                    Dim nesteditems = GetItems(Host.Items.ToList)
                    Dim sitem = nesteditems.Where(Function(x) x.ID = DirectCast(itemSelector.SelectedNode, TreeNode).Tag.ToString).FirstOrDefault
                    If Not sitem Is Nothing Then
                        Viewer.NavigateToString(sitem.GetHTML())
                    Else
                        MessageBox.Show("Selected report not found.")
                    End If
                End If

            End Sub

    End Sub

    Public Shared Function GetItems(ByVal list As List(Of InspectorItem)) As List(Of InspectorItem)
        Dim myItems As List(Of InspectorItem) = New List(Of InspectorItem)()
        For Each i As InspectorItem In list
            GetInspectorItems(i, myItems)
        Next

        Return myItems
    End Function

    Public Shared Function GetItems(ByVal iitem As InspectorItem) As List(Of InspectorItem)
        Dim myItems As List(Of InspectorItem) = New List(Of InspectorItem)()
        For Each i As InspectorItem In iitem.Items
            GetInspectorItems(i, myItems)
        Next

        Return myItems
    End Function

    Private Shared Sub GetInspectorItems(ByVal item As InspectorItem, ByVal items As List(Of InspectorItem))
        items.Add(item)
        For Each i As InspectorItem In item.Items
            GetInspectorItems(i, items)
        Next
    End Sub

    Public Shared Function GetAllTreeItems(ByVal iitem As List(Of TreeNode)) As List(Of TreeNode)
        Dim myItems As List(Of TreeNode) = New List(Of TreeNode)()
        For Each i As TreeNode In iitem
            GetTreeItems(i, myItems)
        Next
        Return myItems
    End Function

    Private Shared Sub GetTreeItems(ByVal item As TreeNode, ByVal items As List(Of TreeNode))
        items.Add(item)
        For Each i As TreeNode In item.Nodes
            GetTreeItems(i, items)
        Next
    End Sub

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim filePickerForm As IFilePicker = FilePickerService.GetInstance().GetFilePicker()

        Dim handler As IVirtualFile = filePickerForm.ShowSaveDialog(
            New List(Of FilePickerAllowedType) From {New FilePickerAllowedType("HTML File", "*.html")})

        If handler IsNot Nothing Then
            Try
                Dim html As String
                html = Await Viewer.ExecuteScriptAsync("document.documentElement.outerHTML;")
                html = System.Text.RegularExpressions.Regex.Unescape(html)
                html = html.Remove(0, 1)
                html = html.Remove(html.Length - 1, 1)
                Using stream As New IO.MemoryStream()
                    Using writer As New IO.StreamWriter(stream) With {.AutoFlush = True}
                        writer.Write(html)
                        handler.Write(stream)
                    End Using
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message,
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error)
            End Try
        End If

    End Sub

    Private Sub Window2_Shown(sender As Object, e As EventArgs) Handles Me.Shown

        Text = String.Format(Text, SelectedObject.GraphicObject.Tag)
        TabText = String.Format(TabText, SelectedObject.GraphicObject.Tag)

        Populate()

    End Sub

End Class