using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Excel = Microsoft.Office.Interop.Excel;
namespace DwgExtractorManual.Models
{
///
/// Excel ½ÃÆ®¿¡ µ¥ÀÌÅ͸¦ ¾²´Â ÀÛ¾÷À» ´ã´çÇϴ Ŭ·¡½º
///
internal class ExcelDataWriter
{
private readonly ExcelManager excelManager;
public ExcelDataWriter(ExcelManager excelManager)
{
this.excelManager = excelManager ?? throw new ArgumentNullException(nameof(excelManager));
}
///
/// Title Block µ¥ÀÌÅ͸¦ Excel ½ÃÆ®¿¡ ±â·Ï
///
public void WriteTitleBlockData(List titleBlockRows)
{
if (excelManager.TitleBlockSheet == null || titleBlockRows == null || titleBlockRows.Count == 0)
return;
int currentRow = 2; // Çì´õ ´ÙÀ½ ÇàºÎÅÍ ½ÃÀÛ
foreach (var row in titleBlockRows)
{
excelManager.TitleBlockSheet.Cells[currentRow, 1] = row.Type;
excelManager.TitleBlockSheet.Cells[currentRow, 2] = row.Name;
excelManager.TitleBlockSheet.Cells[currentRow, 3] = row.Tag;
excelManager.TitleBlockSheet.Cells[currentRow, 4] = row.Prompt;
excelManager.TitleBlockSheet.Cells[currentRow, 5] = row.Value;
excelManager.TitleBlockSheet.Cells[currentRow, 6] = row.Path;
excelManager.TitleBlockSheet.Cells[currentRow, 7] = row.FileName;
currentRow++;
}
}
///
/// Text Entity µ¥ÀÌÅ͸¦ Excel ½ÃÆ®¿¡ ±â·Ï
///
public void WriteTextEntityData(List textEntityRows)
{
if (excelManager.TextEntitiesSheet == null || textEntityRows == null || textEntityRows.Count == 0)
return;
int currentRow = 2; // Çì´õ ´ÙÀ½ ÇàºÎÅÍ ½ÃÀÛ
foreach (var row in textEntityRows)
{
excelManager.TextEntitiesSheet.Cells[currentRow, 1] = row.Type;
excelManager.TextEntitiesSheet.Cells[currentRow, 2] = row.Layer;
excelManager.TextEntitiesSheet.Cells[currentRow, 3] = row.Text;
excelManager.TextEntitiesSheet.Cells[currentRow, 4] = row.Path;
excelManager.TextEntitiesSheet.Cells[currentRow, 5] = row.FileName;
currentRow++;
}
}
///
/// ¸ÅÇÎ µ¥ÀÌÅ͸¦ Excel ½ÃÆ®¿¡ ±â·Ï
///
public void WriteMappingDataToExcel(Dictionary> mappingData)
{
try
{
if (excelManager.MappingSheet == null || mappingData == null)
return;
int currentRow = 2; // Çì´õ ´ÙÀ½ ÇàºÎÅÍ ½ÃÀÛ
Debug.WriteLine($"[DEBUG] Writing mapping data to Excel. Total files: {mappingData.Count}");
foreach (var fileEntry in mappingData)
{
string fileName = fileEntry.Key;
var fileMappingData = fileEntry.Value;
Debug.WriteLine($"[DEBUG] Processing file: {fileName}, entries: {fileMappingData.Count}");
foreach (var mapEntry in fileMappingData)
{
string mapKey = mapEntry.Key;
(string aiLabel, string dwgTag, string attValue, string pdfValue) = mapEntry.Value;
if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(mapKey))
{
continue;
}
try
{
// ¹èÄ¡ ¾÷µ¥ÀÌÆ®¸¦ À§ÇÑ ¹è¿ »ç¿ë
object[,] rowData = new object[1, 6];
rowData[0, 0] = fileName;
rowData[0, 1] = mapKey;
rowData[0, 2] = aiLabel ?? "";
rowData[0, 3] = dwgTag ?? "";
rowData[0, 4] = attValue ?? "";
rowData[0, 5] = pdfValue ?? "";
Excel.Range range = excelManager.MappingSheet.Range[
excelManager.MappingSheet.Cells[currentRow, 1],
excelManager.MappingSheet.Cells[currentRow, 6]];
range.Value = rowData;
}
catch (System.Exception ex)
{
Debug.WriteLine($"? Error writing row {currentRow}: {ex.Message}");
}
currentRow++;
}
}
Debug.WriteLine($"[DEBUG] Mapping data written to Excel. Total rows: {currentRow - 2}");
}
catch (System.Exception ex)
{
Debug.WriteLine($"? ¸ÅÇÎ µ¥ÀÌÅÍ Excel ±â·Ï Áß ¿À·ù: {ex.Message}");
throw;
}
}
///
/// Excel ¸ÅÇÎ ½ÃÆ®¿¡¼ FileName°ú AILabelÀÌ ¸ÅĪµÇ´Â ÇàÀ» ã¾Æ Pdf_value¸¦ ¾÷µ¥ÀÌÆ®
///
public bool UpdateExcelRow(string fileName, string aiLabel, string pdfValue)
{
try
{
if (excelManager.MappingSheet == null)
return false;
Excel.Range usedRange = excelManager.MappingSheet.UsedRange;
if (usedRange == null) return false;
int lastRow = usedRange.Rows.Count;
for (int row = 2; row <= lastRow; row++)
{
var cellFileName = excelManager.MappingSheet.Cells[row, 1]?.Value?.ToString() ?? "";
var cellAiLabel = excelManager.MappingSheet.Cells[row, 3]?.Value?.ToString() ?? "";
if (string.Equals(cellFileName.Trim(), fileName.Trim(), StringComparison.OrdinalIgnoreCase) &&
string.Equals(cellAiLabel.Trim(), aiLabel.Trim(), StringComparison.OrdinalIgnoreCase))
{
excelManager.MappingSheet.Cells[row, 6] = pdfValue;
return true;
}
}
return false;
}
catch (System.Exception ex)
{
Debug.WriteLine($"? Excel Çà ¾÷µ¥ÀÌÆ® Áß ¿À·ù: {ex.Message}");
return false;
}
}
///
/// DWG Àü¿ë ¸ÅÇÎ ¿öÅ©ºÏÀ» »ý¼ºÇϰí ÀúÀå (PDF Ä÷³ Á¦¿Ü)
///
public void SaveDwgOnlyMappingWorkbook(Dictionary> mappingData, string resultFolderPath)
{
try
{
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
string savePath = System.IO.Path.Combine(resultFolderPath, $"{timestamp}_DwgOnly_Mapping.xlsx");
Debug.WriteLine($"[DEBUG] DWG Àü¿ë ¸ÅÇÎ ¿öÅ©ºÏ »ý¼º ½ÃÀÛ: {savePath}");
var dwgOnlyWorkbook = excelManager.CreateNewWorkbook();
var dwgOnlyWorksheet = (Excel.Worksheet)dwgOnlyWorkbook.Worksheets[1];
dwgOnlyWorksheet.Name = "DWG Mapping Data";
// Çì´õ »ý¼º (PDF Value Ä÷³ Á¦¿Ü)
dwgOnlyWorksheet.Cells[1, 1] = "ÆÄÀϸí";
dwgOnlyWorksheet.Cells[1, 2] = "Map Key";
dwgOnlyWorksheet.Cells[1, 3] = "AI Label";
dwgOnlyWorksheet.Cells[1, 4] = "DWG Tag";
dwgOnlyWorksheet.Cells[1, 5] = "DWG Value";
// Çì´õ ½ºÅ¸ÀÏ Àû¿ë
var headerRange = dwgOnlyWorksheet.Range["A1:E1"];
headerRange.Font.Bold = true;
headerRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
headerRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
// µ¥ÀÌÅÍ ÀÔ·Â
int totalRows = mappingData.Sum(f => f.Value.Count);
if (totalRows > 0)
{
object[,] data = new object[totalRows, 5];
int row = 0;
foreach (var fileEntry in mappingData)
{
string fileName = fileEntry.Key;
foreach (var mapEntry in fileEntry.Value)
{
string mapKey = mapEntry.Key;
var (aiLabel, dwgTag, dwgValue, pdfValue) = mapEntry.Value;
data[row, 0] = fileName;
data[row, 1] = mapKey;
data[row, 2] = aiLabel;
data[row, 3] = dwgTag;
data[row, 4] = dwgValue;
row++;
}
}
Excel.Range dataRange = dwgOnlyWorksheet.Range[
dwgOnlyWorksheet.Cells[2, 1],
dwgOnlyWorksheet.Cells[totalRows + 1, 5]];
dataRange.Value = data;
}
dwgOnlyWorksheet.Columns.AutoFit();
excelManager.SaveWorkbookAs(dwgOnlyWorkbook, savePath);
Debug.WriteLine($"? DWG Àü¿ë ¸ÅÇÎ ¿öÅ©ºÏ ÀúÀå ¿Ï·á: {System.IO.Path.GetFileName(savePath)}");
dwgOnlyWorkbook.Close(false);
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
catch (System.Exception ex)
{
Debug.WriteLine($"? DWG Àü¿ë ¸ÅÇÎ ¿öÅ©ºÏ ÀúÀå Áß ¿À·ù: {ex.Message}");
throw;
}
}
///
/// Height Á¤·ÄµÈ Excel ÆÄÀÏ »ý¼º
///
public void WriteHeightSortedData(List textEntities, Excel.Worksheet worksheet, string fileName)
{
// Çì´õ ¼³Á¤
worksheet.Cells[1, 1] = "Height";
worksheet.Cells[1, 2] = "Type";
worksheet.Cells[1, 3] = "Layer";
worksheet.Cells[1, 4] = "Tag";
worksheet.Cells[1, 5] = "FileName";
worksheet.Cells[1, 6] = "Text";
// Çì´õ ½ºÅ¸ÀÏ Àû¿ë
var headerRange = worksheet.Range["A1:F1"];
headerRange.Font.Bold = true;
headerRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightBlue);
// µ¥ÀÌÅÍ´Â ExtractTextEntitiesWithHeight¿¡¼ ÀÌ¹Ì Á¤·ÄµÇ¾úÀ¸¹Ç·Î ´Ù½Ã Á¤·ÄÇÏÁö ¾Ê½À´Ï´Ù.
// µ¥ÀÌÅÍ ÀÔ·Â
int row = 2;
foreach (var entity in textEntities) // sortedEntities¸¦ textEntities·Î ¼öÁ¤
{
worksheet.Cells[row, 1] = entity.Height;
worksheet.Cells[row, 2] = entity.Type;
worksheet.Cells[row, 3] = entity.Layer;
worksheet.Cells[row, 4] = entity.Tag;
worksheet.Cells[row, 5] = fileName;
worksheet.Cells[row, 6] = entity.Text;
row++;
}
worksheet.Columns.AutoFit();
}
}
}