-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.vb
More file actions
282 lines (234 loc) · 11 KB
/
Form1.vb
File metadata and controls
282 lines (234 loc) · 11 KB
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
Imports System.IO
Imports System.Math
Imports iTextSharp.text.pdf
Public Class Form1
Private pdfPath As String = ""
' === CHKALLTC EVENT ===
Private Sub chkAlltc_CheckedChanged(sender As Object, e As EventArgs) Handles chkAlltc.CheckedChanged
If chkAlltc.Checked Then
For i = 2 To 9
Dim tcControl As TextBox = TryCast(Me.Controls("txtTC" & i), TextBox)
If tcControl IsNot Nothing Then
tcControl.Text = txtTC1.Text
End If
Next
End If
End Sub
' === CHKALLEW EVENT ===
Private Sub chkAllew_CheckedChanged(sender As Object, e As EventArgs) Handles chkAllew.CheckedChanged
If chkAllew.Checked Then
For i = 2 To 9
Dim varControl As TextBox = TryCast(Me.Controls("txtVAR" & i), TextBox)
If varControl IsNot Nothing Then
varControl.Text = txtVAR1.Text
End If
Next
End If
End Sub
' === CHKTOTAL EVENT ===
Private Sub chkTotal_CheckedChanged(sender As Object, e As EventArgs) Handles chkTotal.CheckedChanged
CalculateTotalDistance()
End Sub
' Calculate total distance from txtDIS1 to txtDIS9, update txtDISTOTAL if checkbox checked
Public Sub CalculateTotalDistance()
Dim totalDist As Double = 0
For i = 1 To 9
Dim disControl As TextBox = TryCast(Me.Controls("txtDIS" & i), TextBox)
If disControl IsNot Nothing Then
totalDist += Val(disControl.Text)
End If
Next
If chkTotal.Checked Then
txtDISTOTAL.Text = totalDist.ToString("F2")
End If
End Sub
' === LOAD PDF BUTTON ===
Private Sub btnUp_Click(sender As Object, e As EventArgs) Handles btnUp.Click
Dim openPDF As New OpenFileDialog()
openPDF.Filter = "PDF files (*.pdf)|*.pdf"
If openPDF.ShowDialog() = DialogResult.OK Then
pdfPath = openPDF.FileName
MessageBox.Show("Selected PDF: " & pdfPath)
End If
End Sub
' === LOAD TXT BUTTON ===
Private Sub btnWB_Click(sender As Object, e As EventArgs) Handles btnWB.Click
Dim openTxt As New OpenFileDialog()
openTxt.Filter = "Text files (*.txt)|*.txt"
If openTxt.ShowDialog() = DialogResult.OK Then
Dim filePath As String = openTxt.FileName
Try
Dim lines() As String = File.ReadAllLines(filePath)
' Assuming lines 2 to 6 contain A1 to A5 values like "A1=123"
For i As Integer = 1 To 5
If lines.Length > i Then
Dim lineParts() As String = lines(i).Split("="c)
If lineParts.Length = 2 Then
Dim value = lineParts(1).Trim()
Dim txtBox As TextBox = TryCast(Me.Controls("txtA" & i.ToString()), TextBox)
If txtBox IsNot Nothing Then
txtBox.Text = value
End If
End If
End If
Next
MessageBox.Show("Values loaded into A1-5 successfully.")
Catch ex As Exception
MessageBox.Show("Error reading file: " & ex.Message)
End Try
End If
End Sub
' === FILL PDF BUTTON ===
Private Sub btnInput_Click(sender As Object, e As EventArgs) Handles btnInput.Click
ProcessPDF()
End Sub
' Main PDF processing and form filling method
Public Sub ProcessPDF()
If pdfPath = "" Then
MessageBox.Show("Please select a PDF first.")
Return
End If
Try
Dim reader As New PdfReader(pdfPath)
Dim outputPDF As String = Path.Combine(Path.GetDirectoryName(pdfPath), "Filled-" & Path.GetFileName(pdfPath))
Dim stamper As New PdfStamper(reader, New FileStream(outputPDF, FileMode.Create))
Dim fields As AcroFields = stamper.AcroFields
Dim totalETE As Double = 0
Dim totalDIS As Double = 0
Dim remDist As Double = 0
' Process calculation fields for legs 1 to 9
For i = 1 To 9
Dim DIS As Double = Val(GetText("txtDIS" & i))
Dim TAS As Double = Val(GetText("txtTAS" & i))
Dim WS As Double = Val(GetText("txtVEL" & i))
Dim WNDDIR As Double = Val(GetText("txtDIR" & i))
Dim TC As Double = Val(GetText("txtTC" & i))
Dim VAR As Double = Val(GetText("txtVAR" & i))
Dim ALT As String = GetText("txtALT" & i)
Dim TEMP As Double = Val(GetText("txtTMP" & i))
If DIS > 0 Then
fields.SetField("DIS" & i, DIS.ToString() & " nm")
totalDIS += DIS
End If
If TAS > 0 Then fields.SetField("TAS" & i, Math.Round(TAS).ToString() & " kt")
If WS > 0 Then fields.SetField("VEL" & i, Math.Round(WS).ToString() & " kt")
If WNDDIR <> 0 Then fields.SetField("DIR" & i, Math.Round(WNDDIR).ToString())
If TC <> 0 Then fields.SetField("TC" & i, Math.Round(TC).ToString())
If VAR <> 0 Then fields.SetField("VAR" & i, Math.Round(VAR).ToString())
If ALT <> "" Then fields.SetField("ALT" & i, ALT)
If TEMP <> 0 Then fields.SetField("TEM" & i, Math.Round(TEMP).ToString())
' Perform wind correction calculations only if needed values are valid
If TAS > 0 And DIS > 0 Then
Dim angleDiff As Double = WNDDIR - TC
Dim ratio As Double = WS / TAS
Dim sinValue As Double = Sin(angleDiff * PI / 180)
Dim WCA As Double = 0
If Abs(ratio * sinValue) <= 1 Then
WCA = Asin(ratio * sinValue) * 180 / PI
End If
WCA = Math.Round(WCA)
fields.SetField("COR" & i, WCA.ToString())
Dim GS As Double = Math.Sqrt(TAS ^ 2 + WS ^ 2 - 2 * TAS * WS * Cos(angleDiff * PI / 180))
GS = Math.Round(GS)
fields.SetField("GS" & i, GS.ToString() & " kt")
Dim ETE As Double = (DIS / GS) * 60
ETE = Math.Round(ETE * 2) / 2
totalETE += ETE
fields.SetField("ETE" & i, ETE.ToString() & " m")
Dim TH As Double = TC + WCA
fields.SetField("TH" & i, Math.Round(TH).ToString())
Dim MH As Double = TH + VAR
fields.SetField("MH" & i, Math.Round(MH).ToString())
remDist -= DIS
fields.SetField("REM" & i, remDist.ToString() & " nm")
End If
Next
' Direct textbox-to-PDF fields mapping
Dim fieldCounts As New Dictionary(Of String, Integer) From {
{"txtFLD", 5}, {"txtELV", 5}, {"txtRWY", 5}, {"txtRD", 5},
{"txtLOCT", 3}, {"txtTAF", 3}, {"txtLOCM", 2}, {"txtMETAR", 2},
{"txtLOCW", 3}, {"txtWALT", 4},
{"txtWND1A", 3}, {"txtWND2A", 3}, {"txtWND3A", 3}, {"txtWND4A", 3},
{"txtLOCP", 4}, {"txtPSN", 4}, {"txtW", 5}, {"txtA", 5}
}
For Each prefix In fieldCounts.Keys
Dim maxCount As Integer = fieldCounts(prefix)
For i = 1 To maxCount
Dim value As String = GetText(prefix & i)
If value <> "" Then
Dim pdfField As String = Replace(prefix & i, "txt", "")
fields.SetField(pdfField, value)
End If
Next
Next
' Single weight field
Dim wtValue As String = GetText("txtWT")
If wtValue <> "" Then
fields.SetField("WT", wtValue)
End If
' Set totals fields
fields.SetField("ETETOTAL", Math.Round(totalETE * 2) / 2 & " m")
If chkTotal.Checked Then
fields.SetField("DISTOTAL", totalDIS.ToString() & " nm")
End If
' Calculate W*A, set M1-5, and sum WT and MT
Dim totalW As Double = 0
Dim totalM As Double = 0
For i As Integer = 1 To 5
Dim wVal As Double = Val(GetText("txtW" & i))
Dim aVal As Double = Val(GetText("txtA" & i))
Dim product As Double = wVal * aVal
' Update M textboxes and PDF fields
Dim mTextBox As TextBox = TryCast(Me.Controls("txtM" & i), TextBox)
If mTextBox IsNot Nothing Then mTextBox.Text = product.ToString("F2")
fields.SetField("M" & i, product.ToString("F2"))
totalW += wVal
totalM += product
Next
' Set WT and MT totals in PDF and textboxes
fields.SetField("WT", totalW.ToString("F2"))
Dim wtTextBox As TextBox = TryCast(Me.Controls("txtWT"), TextBox)
If wtTextBox IsNot Nothing Then wtTextBox.Text = totalW.ToString("F2")
fields.SetField("MT", totalM.ToString("F2"))
Dim mtTextBox As TextBox = TryCast(Me.Controls("txtMT"), TextBox)
If mtTextBox IsNot Nothing Then mtTextBox.Text = totalM.ToString("F2")
' Calculate CG and set it
Dim cgValue As Double = 0
If totalW <> 0 Then
cgValue = totalM / totalW
End If
' Debug line: confirm calculation
Console.WriteLine("Calculated CG: " & cgValue.ToString("F2"))
' Set value into the PDF field named "CG"
fields.SetField("CG", cgValue.ToString("F2"))
' Set value into your form TextBox named "txtCG"
If txtCG IsNot Nothing Then
txtCG.Text = cgValue.ToString("F2")
End If
stamper.Close()
reader.Close()
MessageBox.Show("PDF completed and saved as:" & vbCrLf & outputPDF)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
' Helper method to get TextBox text by name safely
Private Function GetText(name As String) As String
Dim ctrl As Control = Me.Controls(name)
If ctrl IsNot Nothing AndAlso TypeOf ctrl Is TextBox Then
Return CType(ctrl, TextBox).Text
Else
Return ""
End If
End Function
' === EXIT BUTTON ===
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Dim result As DialogResult = MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
Application.Exit()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
'test
End Class