-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
132 lines (121 loc) · 5.92 KB
/
Copy pathProgram.cs
File metadata and controls
132 lines (121 loc) · 5.92 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace xmlpointtxt
{
internal static class Program
{
[STAThread]
static int Main(string[] args)
{
// ปลดล็อก codepage TIS-620 / Windows-874 (ไม่ได้รวมมาใน .NET Core โดยตรง)
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
// โหมด headless test: xmlpointtxt.exe --test [--primary] <xml-path> [out-dir]
// --primary = export เฉพาะแปลงหลัก 1 ไฟล์ (คง / หรือ รวม กรณีงานสอบเขต)
if (args.Length >= 2 && args[0] == "--test")
{
bool primaryOnly = args.Contains("--primary");
var rest = args.Skip(1).Where(a => a != "--primary").ToArray();
if (rest.Length < 1)
{
Console.Error.WriteLine("usage: xmlpointtxt.exe --test [--primary] <xml-path> [out-dir]");
return 2;
}
return RunHeadlessTest(rest[0], rest.Length >= 2 ? rest[1] : null, primaryOnly);
}
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
Logger.Error("UnhandledException", e.ExceptionObject as Exception);
Application.ThreadException += (s, e) =>
Logger.Error("ThreadException", e.Exception);
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
return 0;
}
private static int RunHeadlessTest(string xmlPath, string? outDir, bool primaryOnly = false)
{
try
{
Logger.Info($"=== HEADLESS TEST: {xmlPath} (primaryOnly={primaryOnly}) ===");
if (!File.Exists(xmlPath))
{
Logger.Error($"file not found: {xmlPath}");
Console.Error.WriteLine($"file not found: {xmlPath}");
return 2;
}
var pathToLoad = xmlPath;
if (SecretLoader.LooksEncrypted(xmlPath))
{
Console.WriteLine($"[encrypted] key={(SecretLoader.IsAvailable ? "found" : "MISSING")}");
if (!SecretLoader.IsAvailable)
{
Console.Error.WriteLine("ไฟล์ถูกเข้ารหัสและไม่พบ secrets.key ข้างๆ exe");
return 3;
}
var tmp = SecretLoader.DecryptToTempFile(xmlPath);
if (tmp == null)
{
Console.Error.WriteLine("ถอดรหัสไม่สำเร็จ");
return 4;
}
pathToLoad = tmp;
Console.WriteLine($"[decrypted] {tmp}");
}
var analyzer = new XmlAnalyzer();
try { analyzer.Load(pathToLoad); }
finally
{
if (!string.Equals(pathToLoad, xmlPath, StringComparison.OrdinalIgnoreCase))
{
try { File.Delete(pathToLoad); } catch { /* ignore */ }
}
}
var allParcels = analyzer.GetParcels();
Console.WriteLine($"Loaded. parcels = {allParcels.Count}, surveyType = '{analyzer.SurveyTypeName}'");
outDir ??= Path.Combine(Path.GetDirectoryName(xmlPath)!, "test-out");
Directory.CreateDirectory(outDir);
List<ParcelItem> parcels;
if (primaryOnly)
{
var primary = analyzer.GetPrimaryParcel();
if (primary == null)
{
Console.Error.WriteLine("no primary parcel found");
return 5;
}
parcels = new List<ParcelItem> { primary };
Console.WriteLine($"[primary] picked land={primary.LandNo} type={primary.TypeName} seq={primary.Seq}");
}
else
{
parcels = allParcels;
}
int g1 = 0, g2 = 0;
foreach (var p in parcels)
{
var res = analyzer.GetParcelPoints(p.Seq);
if (res.Points.Count == 0) continue;
if (res.IsGrade1) g1++; else g2++;
// primary mode → ชื่อสั้น (ไม่ต้องใส่ type) เพราะมีแค่ไฟล์เดียว
// full mode → ใส่ type+seq กันชื่อชนกรณีหลายแปลง LandNo เดียวกัน
var outFile = Path.Combine(outDir, primaryOnly
? Form1.BuildOutputFileName(p.LandNo, analyzer.MapSheet)
: Form1.BuildOutputFileName(p.LandNo, analyzer.MapSheet, p.TypeName, p.Seq));
Form1.ExportTxt(outFile, res.Points, res.IsGrade1);
Console.WriteLine($" parcel {p.LandNo} ({p.TypeName}, seq {p.Seq}): {res.Points.Count} pts, grade={(res.IsGrade1 ? 1 : 2)} → {Path.GetFileName(outFile)}");
}
Console.WriteLine($"Done. grade1 parcels={g1}, grade2 parcels={g2}");
Console.WriteLine($"Output dir: {outDir}");
Console.WriteLine($"Log file: {Logger.LogPath}");
return 0;
}
catch (Exception ex)
{
Logger.Error("Headless test failed", ex);
Console.Error.WriteLine($"FAILED: {ex.Message}");
Console.Error.WriteLine($"See log: {Logger.LogPath}");
return 1;
}
}
}
}