ExportExcel 정리, height정렬 attRef 분리

This commit is contained in:
2025-07-29 14:06:12 +09:00
parent 8bd5d9580c
commit 66dd64306c
7 changed files with 1613 additions and 1861 deletions

317
Models/JsonDataProcessor.cs Normal file
View File

@@ -0,0 +1,317 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace DwgExtractorManual.Models
{
/// <summary>
/// JSON <20><><EFBFBD><EFBFBD> ó<><C3B3> <20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD> Ŭ<><C5AC><EFBFBD><EFBFBD>
/// </summary>
internal class JsonDataProcessor
{
/// <summary>
/// JSON <20><><EFBFBD>Ͽ<EFBFBD><CFBF><EFBFBD> PDF <20>м<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>о<EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>͸<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ
/// </summary>
public bool UpdateMappingDataFromJson(Dictionary<string, Dictionary<string, (string, string, string, string)>> mappingData, string jsonFilePath)
{
try
{
Debug.WriteLine($"[DEBUG] JSON <20><><EFBFBD>Ͽ<EFBFBD><CFBF><EFBFBD> PDF <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ <20><><EFBFBD><EFBFBD>: {jsonFilePath}");
if (!File.Exists(jsonFilePath))
{
Debug.WriteLine($"? JSON <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ʽ<EFBFBD><CABD>ϴ<EFBFBD>: {jsonFilePath}");
return false;
}
// JSON <20><><EFBFBD><EFBFBD> <20>б<EFBFBD> <20><> <20><><EFBFBD><EFBFBD>
string jsonContent = File.ReadAllText(jsonFilePath, System.Text.Encoding.UTF8);
jsonContent = CleanJsonContent(jsonContent);
JObject jsonData;
try
{
jsonData = JObject.Parse(jsonContent);
}
catch (Newtonsoft.Json.JsonReaderException jsonEx)
{
Debug.WriteLine($"? JSON <20>Ľ<EFBFBD> <20><><EFBFBD><EFBFBD>: {jsonEx.Message}");
throw new System.Exception($"PDF <20>м<EFBFBD> JSON <20><><EFBFBD><EFBFBD> <20>Ľ<EFBFBD> <20><><EFBFBD><EFBFBD>: {jsonEx.Message}\n<><6E><EFBFBD><EFBFBD>: {jsonFilePath}");
}
var results = jsonData["results"] as JArray;
if (results == null)
{
Debug.WriteLine("? JSON<4F><4E><EFBFBD><EFBFBD> 'results' <20><EFBFBD><E8BFAD> ã<><C3A3> <20><> <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>.");
return false;
}
int updatedCount = 0;
int totalEntries = 0;
foreach (JObject result in results)
{
var fileInfo = result["file_info"];
var pdfAnalysis = result["pdf_analysis"];
if (fileInfo == null || pdfAnalysis == null) continue;
string fileName = fileInfo["name"]?.ToString();
if (string.IsNullOrEmpty(fileName)) continue;
string fileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName);
if (!mappingData.ContainsKey(fileNameWithoutExt))
{
Debug.WriteLine($"?? <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>Ϳ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>: {fileNameWithoutExt}");
continue;
}
foreach (var property in pdfAnalysis.Cast<JProperty>())
{
string aiLabel = property.Name;
var valueObj = property.Value as JObject;
if (valueObj == null) continue;
string pdfValue = valueObj["value"]?.ToString();
if (string.IsNullOrEmpty(pdfValue)) continue;
totalEntries++;
var fileData = mappingData[fileNameWithoutExt];
var matchingEntry = fileData.FirstOrDefault(kvp =>
string.Equals(kvp.Value.Item1.Trim(), aiLabel.Trim(), StringComparison.OrdinalIgnoreCase));
if (!string.IsNullOrEmpty(matchingEntry.Key))
{
var existingValue = matchingEntry.Value;
fileData[matchingEntry.Key] = (existingValue.Item1, existingValue.Item2, existingValue.Item3, pdfValue);
updatedCount++;
}
}
}
Debug.WriteLine($"[DEBUG] PDF <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ <20>Ϸ<EFBFBD>: {updatedCount}/{totalEntries} <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ<EFBFBD><C6AE>");
return true;
}
catch (System.Exception ex)
{
Debug.WriteLine($"? JSON<4F><4E><EFBFBD><EFBFBD> PDF <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ <20><> <20><><EFBFBD><EFBFBD>: {ex.Message}");
return false;
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD> <20><><EFBFBD>ųʸ<C5B3><CAB8><EFBFBD> JSON <20><><EFBFBD>Ϸ<EFBFBD> <20><><EFBFBD><EFBFBD>
/// </summary>
public void SaveMappingDictionary(Dictionary<string, Dictionary<string, (string, string, string, string)>> mappingData, string filePath)
{
try
{
Debug.WriteLine($"[DEBUG] <20><><EFBFBD><EFBFBD> <20><><EFBFBD>ųʸ<C5B3> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>: {filePath}");
var serializableData = new Dictionary<string, Dictionary<string, object>>();
foreach (var fileEntry in mappingData)
{
var fileData = new Dictionary<string, object>();
foreach (var mapEntry in fileEntry.Value)
{
fileData[mapEntry.Key] = new
{
AILabel = mapEntry.Value.Item1,
DwgTag = mapEntry.Value.Item2,
DwgValue = mapEntry.Value.Item3,
PdfValue = mapEntry.Value.Item4
};
}
serializableData[fileEntry.Key] = fileData;
}
string jsonContent = JsonConvert.SerializeObject(serializableData, Formatting.Indented);
File.WriteAllText(filePath, jsonContent, System.Text.Encoding.UTF8);
Debug.WriteLine($"? <20><><EFBFBD><EFBFBD> <20><><EFBFBD>ųʸ<C5B3> <20><><EFBFBD><EFBFBD> <20>Ϸ<EFBFBD>: {Path.GetFileName(filePath)}");
Debug.WriteLine($"?? <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><>: {mappingData.Count}");
}
catch (System.Exception ex)
{
Debug.WriteLine($"? <20><><EFBFBD><EFBFBD> <20><><EFBFBD>ųʸ<C5B3> <20><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD>: {ex.Message}");
throw;
}
}
/// <summary>
/// JSON <20><><EFBFBD>Ͽ<EFBFBD><CFBF><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD>ųʸ<C5B3><CAB8><EFBFBD> <20>ε<EFBFBD>
/// </summary>
public Dictionary<string, Dictionary<string, (string, string, string, string)>> LoadMappingDictionary(string filePath)
{
var result = new Dictionary<string, Dictionary<string, (string, string, string, string)>>();
try
{
Debug.WriteLine($"[DEBUG] <20><><EFBFBD><EFBFBD> <20><><EFBFBD>ųʸ<C5B3> <20>ε<EFBFBD> <20><><EFBFBD><EFBFBD>: {filePath}");
if (!File.Exists(filePath))
{
Debug.WriteLine($"?? <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ʽ<EFBFBD><CABD>ϴ<EFBFBD>: {filePath}");
return result;
}
string jsonContent = File.ReadAllText(filePath, System.Text.Encoding.UTF8);
jsonContent = CleanJsonContent(jsonContent);
Dictionary<string, Dictionary<string, JObject>> deserializedData;
try
{
deserializedData = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, JObject>>>(jsonContent);
}
catch (Newtonsoft.Json.JsonReaderException jsonEx)
{
Debug.WriteLine($"? JSON <20>Ľ<EFBFBD> <20><><EFBFBD><EFBFBD>: {jsonEx.Message}");
throw new System.Exception($"<22><><EFBFBD><EFBFBD> JSON <20><><EFBFBD><EFBFBD> <20>Ľ<EFBFBD> <20><><EFBFBD><EFBFBD>: {jsonEx.Message}\n<><6E><EFBFBD><EFBFBD>: {filePath}");
}
if (deserializedData != null)
{
foreach (var fileEntry in deserializedData)
{
var fileData = new Dictionary<string, (string, string, string, string)>();
foreach (var mapEntry in fileEntry.Value)
{
var valueObj = mapEntry.Value;
string aiLabel = valueObj["AILabel"]?.ToString() ?? "";
string dwgTag = valueObj["DwgTag"]?.ToString() ?? "";
string dwgValue = valueObj["DwgValue"]?.ToString() ?? "";
string pdfValue = valueObj["PdfValue"]?.ToString() ?? "";
fileData[mapEntry.Key] = (aiLabel, dwgTag, dwgValue, pdfValue);
}
result[fileEntry.Key] = fileData;
}
}
Debug.WriteLine($"? <20><><EFBFBD><EFBFBD> <20><><EFBFBD>ųʸ<C5B3> <20>ε<EFBFBD> <20>Ϸ<EFBFBD>");
Debug.WriteLine($"?? <20>ε<EFBFBD><CEB5><EFBFBD> <20><><EFBFBD><EFBFBD> <20><>: {result.Count}");
return result;
}
catch (System.Exception ex)
{
Debug.WriteLine($"? <20><><EFBFBD><EFBFBD> <20><><EFBFBD>ųʸ<C5B3> <20>ε<EFBFBD> <20><> <20><><EFBFBD><EFBFBD>: {ex.Message}");
throw;
}
}
/// <summary>
/// JSON <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>Ͽ<EFBFBD> <20>Ľ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>·<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>.
/// </summary>
private string CleanJsonContent(string jsonContent)
{
if (string.IsNullOrEmpty(jsonContent))
return jsonContent;
try
{
var lines = jsonContent.Split('\n');
var cleanedLines = new List<string>();
bool inMultiLineComment = false;
foreach (string line in lines)
{
string processedLine = line;
// <20><>Ƽ<EFBFBD><C6BC><EFBFBD><EFBFBD> <20>ּ<EFBFBD> ó<><C3B3>
if (inMultiLineComment)
{
int endIndex = processedLine.IndexOf("*/");
if (endIndex >= 0)
{
processedLine = processedLine.Substring(endIndex + 2);
inMultiLineComment = false;
}
else
{
continue;
}
}
int multiLineStart = processedLine.IndexOf("/*");
if (multiLineStart >= 0)
{
int multiLineEnd = processedLine.IndexOf("*/", multiLineStart + 2);
if (multiLineEnd >= 0)
{
processedLine = processedLine.Substring(0, multiLineStart) +
processedLine.Substring(multiLineEnd + 2);
}
else
{
processedLine = processedLine.Substring(0, multiLineStart);
inMultiLineComment = true;
}
}
// <20>̱۶<CCB1><DBB6><EFBFBD> <20>ּ<EFBFBD> <20><><EFBFBD><EFBFBD>
bool inString = false;
bool escaped = false;
int commentIndex = -1;
for (int i = 0; i < processedLine.Length - 1; i++)
{
char current = processedLine[i];
char next = processedLine[i + 1];
if (escaped)
{
escaped = false;
continue;
}
if (current == '\\')
{
escaped = true;
continue;
}
if (current == '"')
{
inString = !inString;
continue;
}
if (!inString && current == '/' && next == '/')
{
commentIndex = i;
break;
}
}
if (commentIndex >= 0)
{
processedLine = processedLine.Substring(0, commentIndex);
}
if (!string.IsNullOrWhiteSpace(processedLine))
{
cleanedLines.Add(processedLine);
}
}
return string.Join("\n", cleanedLines);
}
catch (System.Exception ex)
{
Debug.WriteLine($"? JSON <20><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD>: {ex.Message}");
return jsonContent;
}
}
}
}