Halcon은 concat_obj 함수를 통해 여러 XLD 객체를 결합할 수 있으며, write_contour_xld_dxf를 사용해 이를 DXF 형식으로 저장할 수 있습니다. 하지만 기본적으로 생성된 DXF 파일은 모든 도형이 동일한 레이어(레이어 0)에 위치하고, 선의 색상도 고정된 값(색상 코드 7, 흰색)으로 지정되어 자동화된 설계 작업에는 한계가 있습니다.
예를 들어, 아래 Halcon 코드는 다양한 크기의 결함 영역을 추출하여 개별적으로 DXF로 출력합니다.
read_image (Image, 'sample.png')
threshold_sub_pix (Image, Border, 30)
select_shape_xld (Border, Region_Skin, 'area', 'and', 30000, 99999)
select_shape_xld (Border, Region_Hole, 'area', 'and', 2000, 9999)
select_shape_xld (Border, Region_Inner, 'area', 'and', 200, 999)
select_shape_xld (Border, Region_Jagged, 'area', 'and', 10, 150)
write_contour_xld_dxf (Region_Skin, 'output/defect_skin.dxf')
write_contour_xld_dxf (Region_Hole, 'output/defect_hole.dxf')
write_contour_xld_dxf (Region_Inner, 'output/defect_inner.dxf')
write_contour_xld_dxf (Region_Jagged, 'output/defect_jagged.dxf')
이후 이들을 하나의 파일로 합치더라도, 모든 도형은 여전히 단일 레이어에 흰색으로 표시됩니다. 이를 해결하기 위해선 DXF 파일 구조를 이해하고, 텍스트 기반으로 수정하는 접근이 필요합니다.
DXF 파일 구조 분석
DXF는 텍스트 기반 포맷으로, ASCII 형태로 구성되어 있어 메모장 등으로 열어 확인할 수 있습니다. 주요 정보는 그룹 코드(Group Code)로 구분되며, 다음은 레이어 정의 예시입니다.
LAYER
70
1
0
LAYER
2
0
70
0
62
7
6
CONTINUOUS
- 2: 레이어 이름 (여기서는
0) - 62: 레이어 색상 인덱스 (7 = 흰색)
즉, 특정 레이어에 속한 도형을 다른 레이어로 옮기려면 이 값을 변경하면 됩니다.
다중 DXF 파일을 레이어별로 병합하는 C# 솔루션
아래 C# 코드는 여러 DXF 파일을 읽어들여 각각을 지정된 레이어와 색상으로 재정의한 후, 하나의 통합 파일로 저장합니다. 가변 인수(params)를 사용해 유연하게 확장 가능하며, 레이어 및 색상 매핑은 외부에서 제어할 수 있습니다.
using System;
using System.Collections.Generic;
using System.IO;
class DxfMerger
{
public static void MergeDxfFiles(string outputPath, params (string filePath, int layer, int color)[] inputs)
{
var combinedContent = string.Empty;
foreach (var (filePath, layer, color) in inputs)
{
string dxfPath = filePath + ".dxf";
if (!File.Exists(dxfPath)) continue;
string content = File.ReadAllText(dxfPath);
content = ReplaceLineEndings(content, "#");
// 레이어 및 색상 업데이트
content = UpdateLayerAndColor(content, layer, color);
content = ReplaceLineEndings(content, "\r\n");
if (string.IsNullOrEmpty(combinedContent))
{
combinedContent = content;
}
else
{
combinedContent = combinedContent.Replace("EOF", "\r\n" + content.Trim());
}
}
if (!string.IsNullOrEmpty(combinedContent))
{
using (var writer = new StreamWriter(outputPath + ".dxf"))
{
writer.Write(combinedContent);
}
}
}
private static string ReplaceLineEndings(string text, string replacement)
{
return text.Replace("\r\n", replacement).Replace("\n", replacement);
}
private static string UpdateLayerAndColor(string content, int layer, int color)
{
// 도형의 레이어 번호 변경 (8#0# → 8#[layer]#)
content = content.Replace($"8#0#", $"8#{layer}#");
// 레이어 정의 섹션 업데이트
string oldLayerDef = $"0#LAYER# 2#0# 70#0# 62#7#";
string newLayerDef = $"0#LAYER# 2#{layer}# 70#0# 62#{color}#";
content = content.Replace(oldLayerDef, newLayerDef);
return content;
}
}
사용 예제
// 호출 예시
MergeDxfFiles(
"result/merged_output",
("source/skin", 5, 3), // 피부 결함 - 레이어 5, 색상 노란색
("source/hole", 2, 4), // 구멍 결함 - 레이어 2, 색상 청록색
("source/inner", 3, 7), // 내부 결함 - 레이어 3, 색상 흰색
("source/jagged", 4, 1) // 테두리 결함 - 레이어 4, 색상 빨간색
);
결과 DXF 파일은 각 결함 유형이 지정된 레이어와 색상으로 구분되어 AutoCAD 등에서 시각적으로 명확하게 식별할 수 있습니다.