zoukankan      html  css  js  c++  java
  • 将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:666dxfsdefectsXLD_Skin";
     24             string path_H = @"I:666dxfsdefectsXLD_H";
     25             string path_I = @"I:666dxfsdefectsXLD_I";
     26             string path_J = @"I:666dxfsdefectsXLD_J";
     27             string path_combine = @"I:666dxfscombine----";
     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", "
    " + 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("
    ", "#");  //为增强文本可读性,将
    替换为#
    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("#", "
    ");  //将#恢复为
    
    113             
    114             return text;
    115         }
    116     }
    117 }

    观察“图层号——颜色代号”分别是(0, 7)、(2, 4)、(3, 7)、(5, 3),与代码中的设置相符。

  • 相关阅读:
    Mayan游戏 (codevs 1136)题解
    虫食算 (codevs 1064)题解
    靶形数独 (codevs 1174)题解
    黑白棋游戏 (codevs 2743)题解
    神经网络 (codevs 1088) 题解
    The Rotation Game (POJ 2286) 题解
    倒水问题 (codevs 1226) 题解
    银河英雄传说 (codevs 1540) 题解
    生日蛋糕 (codevs 1710) 题解
    第一章 1.11 高阶函数
  • 原文地址:https://www.cnblogs.com/xh6300/p/7429898.html
Copyright © 2011-2022 走看看