將Halcon導出的多個dxf文件合並成一個分圖層的dxf文件


Halcon中可以通過concat_obj合並多個XLD,並且可以通過write_contour_xld_dxf再將XLD保存為.dxf文件。但是,保存后的.dxf文件用AutoCAD打開后發現,它們都是位於一個圖層上的(0號圖層),並且顏色都是白色(顏色代號7)。

 

如下所示:

 1 read_image (Image, '未標題-1.png')  2 
 3 threshold_sub_pix (Image, Border, 30)  4 
 5 select_shape_xld (Border, defectsXLD_Skin, 'area', 'and', 30000, 99999)  6 select_shape_xld (Border, defectsXLD_H, 'area', 'and', 2000, 9999)  7 select_shape_xld (Border, defectsXLD_I, 'area', 'and', 200, 999)  8 select_shape_xld (Border, defectsXLD_J, 'area', 'and', 10, 150)  9 
10 write_contour_xld_dxf (defectsXLD_Skin,'dxfs/defectsXLD_Skin.dxf') 11 write_contour_xld_dxf (defectsXLD_H,'dxfs/defectsXLD_H.dxf') 12 write_contour_xld_dxf (defectsXLD_I,'dxfs/defectsXLD_I.dxf') 13 write_contour_xld_dxf (defectsXLD_J,'dxfs/defectsXLD_J.dxf') 14 
15 concat_obj (defectsXLD_Skin, defectsXLD_H, XLDs) 16 concat_obj (XLDs, defectsXLD_I, XLDs) 17 concat_obj (XLDs, defectsXLD_J, XLDs) 18 write_contour_xld_dxf (XLDs,'dxfs/XLDs.dxf')

 

有時候,我們要求這些輪廓線條位於不同的圖層上,並且對線條的顏色也有要求

 

那么應該怎么做呢?

 

dxf是一種通用的繪圖交換文件格式,很多通用的交換文件都可以用記事本程序打開,例如dxf文件、xml文件等,我們用記事本打開后,就能看到它的數據結構,從而為修改它們提供了思路——即可以通過流的方式讀取、寫入,從而改變它們的內容。

 

下面是兩篇很好的參考資料:

https://baike.baidu.com/item/DXF/364192?fr=aladdin

http://m.blog.csdn.net/Chailiren/article/details/72861045

 

請先閱讀完上面兩篇資料再往下閱讀。

 

LAYER 70
1
  0 LAYER 2
0               //圖層名
 70
0
 62
7                //圖層顏色
  6 CONTINUOUS

 

上面第二篇資料較好地解決了這個問題,但是在通用性上還有完善的空間,其實方法可以封裝得更完善一些。即:

 

① 用於合並的文件個數可能是不定的,可以是2個、3個、4個、5個或者更多。(方法傳入參數個數可變)

② 每個文件所占據的圖層號可能是不定的,並且對應的線條顏色也可能是不定的。

 

我針對如上兩個問題進行了重新封裝。封裝后的方法的使用demo完整程序如下:

 1 using System;  2 using System.Collections.Generic;  3 using System.ComponentModel;  4 using System.Data;  5 using System.Diagnostics;  6 using System.Drawing;  7 using System.IO;  8 using System.Linq;  9 using System.Text;  10 using System.Windows.Forms;  11 
 12 namespace WindowsForm_dxf分層  13 {  14     public partial class Form1 : Form  15  {  16         public Form1()  17  {  18  InitializeComponent();  19  }  20 
 21         private void button1_Click(object sender, EventArgs e)  22  {  23             string path_Skin = @"I:\666\dxfs\defectsXLD_Skin";  24             string path_H = @"I:\666\dxfs\defectsXLD_H";  25             string path_I = @"I:\666\dxfs\defectsXLD_I";  26             string path_J = @"I:\666\dxfs\defectsXLD_J";  27             string path_combine = @"I:\666\dxfs\combine----";  28 
 29  CombineDxfs(path_combine, path_Skin, path_H,path_I);  30 
 31             MessageBox.Show("dxf混合成功 !");  32  }  33 
 34 
 35 
 36 
 37         public void CombineDxfs(string path_combine, params string[] path_parts)  38  {  39 
 40             int _count = path_parts.Length;  //獲得需合並的各個.dxf文件的個數
 41             List<string> dxfsTextList = new List<string>();  //存儲各個.dxf文件中的文本的List
 42             List<StreamReader> streamReaders = new List<StreamReader>();  ////存儲各個StreamReader實例的List
 43 
 44             for (int i = 0; i < _count; i++)  45  {  46                 //將各個.dxf文件中的文本依次添加到dxfsTextList的item中
 47                 StreamReader streamDxf = new StreamReader(path_parts[i] + ".dxf");  48  streamReaders.Add(streamDxf);  49  dxfsTextList.Add(streamDxf.ReadToEnd());  50 
 51                 //指定各個.dxf文件所在的圖層數和線條顏色代號
 52                 switch (i)  53  {  54                     case 0:  55                         dxfsTextList[i] = SetDxfLayer_Color(dxfsTextList[i], 5, 3);  56                         break;  57 
 58                     case 1:  59                         dxfsTextList[i] = SetDxfLayer_Color(dxfsTextList[i], 2, 4);  60                         break;  61 
 62                     case 2:  63                         dxfsTextList[i] = SetDxfLayer_Color(dxfsTextList[i], 3, 7);  64                         break;  65 
 66                     case 3:  67                         dxfsTextList[i] = SetDxfLayer_Color(dxfsTextList[i], 4, 4);  68                         break;  69 
 70                     case 4:  71                         dxfsTextList[i] = SetDxfLayer_Color(dxfsTextList[i], 5, 5);  72                         break;  73 
 74 
 75                     default:  76                         dxfsTextList[i] = SetDxfLayer_Color(dxfsTextList[i], 7, 7);  77                         break;  78 
 79  }  80 
 81                 
 82  }  83 
 84             //將所有.dxf文件的文本合並到第一個文件中
 85             for (int i = 0; i < _count -1 ; i++)  86  {  87                 dxfsTextList[0] = dxfsTextList[0].Replace("EOF", "\r\n" + dxfsTextList[i + 1]);  88  }  89 
 90             //將dxfsTextList[0]的文本寫入到路徑為path_combine的.dxf文件中。
 91             StreamWriter combineText = new StreamWriter(path_combine + ".dxf");  92             combineText.Write(dxfsTextList[0]);  93  combineText.Flush();  94  combineText.Close();  95 
 96             //依次關閉各個streamReader
 97             foreach (var streamDxf in streamReaders)  98  {  99  streamDxf.Close(); 100  } 101             
102 
103  } 104 
105 
106         //指定各個.dxf文件所在的圖層數和線條顏色代號
107         private  string SetDxfLayer_Color(string text,int Layer_Num, int Color_Num) 108  { 109             text = text.Replace("\r\n", "#");  //為增強文本可讀性,將\r\n替換為#
110             text = text.Replace("8#0#", "8#" + Layer_Num + "#");  //指定.dxf文件所在的圖層數
111             text = text.Replace("0#LAYER# 2#0# 70#0# 62#7#", "0#LAYER# 2#" + Layer_Num + "# 70#0# 62#" + Color_Num + "#");  //在.dxf文件開頭指定其所在的圖層數和線條顏色
112             text = text.Replace("#", "\r\n");  //將#恢復為\r\n
113             
114             return text; 115  } 116  } 117 }

 

觀察“圖層號——顏色代號”分別是(0, 7)、(2, 4)、(3, 7)、(5, 3),與代碼中的設置相符。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM