2016-12-14 229 views
-2

我在c#中編寫代碼以從dxf文件中讀取點座標。我基本上是逐行閱讀DXF文件,並檢查條件如if(i == "AcdbLine")將點的座標寫入文件。從dxf文件讀取座標c#

AcDbLine 
10 
0.0 
20 
0.0 
30 
0.0 
11 
700.0 
21 
0.0 
31 
0.0 
    0 
LINE 

這將座標寫爲(0,0,0)(700,0,0)。

我現在的問題是在區塊內發現它時忽略AcDbLine。我想編寫一個邏輯,當它落在AcDbBlockBeginAcDbBlockEnd的塊內時,它將忽略任何AcDbline及其相應的點。

AcDbBlockBegin 
    2 
*U1 
70 
    1 
10 
0.0 
20 
0.0 
30 
0.0 
    3 
*U1 
    1 

    0 
LINE 
    5 
3F0 
330 
3E9 
100 
AcDbEntity 
    8 
0 
100 
AcDbLine 
10 
-47.22702216883923 
20 
-0.0131059296418084 
30 
0.0 
11 
-19.82207380431916 
21 
-0.0131059296418084 
31 
0.0 
    0 
LINE 
    5 
3F1 
330 
3E9 
100 
AcDbEntity 
    8 
0 
100 
AcDbLine 
10 
22.19765948514734 
20 
0.0131059296418101 
30 
0.0 
11 
47.22702216883923 
21 
0.0131059296418101 
31 
0.0 
    0 
ENDBLK 
    5 
3EB 
330 
3E9 
100 
AcDbEntity 
    8 
0 
100 
AcDbBlockEnd 

對不起,感謝您的幫助!

回答

0

我一直在寫文本解析器超過40年。如果我做不到,沒有人能做到。嘗試下面的代碼

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     enum State 
     { 
      FIND_BLOCK, 
      GET_ACDB_LINE, 
      ACD_BLOCK 
     } 
     const string FILENAME = @"c:\temp\test.txt"; 
     static void Main(string[] args) 
     { 
      StreamReader reader = new StreamReader(FILENAME); 
      string inputLine = ""; 
      State state = State.FIND_BLOCK; 
      int lineRow = 0; 
      Line newLine = new Line(); 
      while ((inputLine = reader.ReadLine()) != null) 
      { 
       inputLine = inputLine.Trim(); 
       if (inputLine.Length > 0) 
       { 
        switch (state) 
        { 
         case State.FIND_BLOCK : 
          switch (inputLine) 
          { 
           case "AcDbBlockBegin" : 
            state = State.ACD_BLOCK; 
            break; 
           case "AcDbLine": 
            state = State.GET_ACDB_LINE; 
            lineRow = 0; 
            newLine = new Line(); 
            Line.lines.Add(newLine); 
            break; 
          } 
          break; 

         case State.ACD_BLOCK : 
          if (inputLine == "AcDbBlockEnd") 
          { 
           state = State.FIND_BLOCK; 
          } 
          break; 

         case State.GET_ACDB_LINE : 
          if (inputLine == "LINE") 
          { 
           state = State.FIND_BLOCK; 
          } 
          else 
          { 
           switch (++lineRow) 
           { 
            case 2: 
             newLine.xLoc = double.Parse(inputLine); 
             break; 
            case 4: 
             newLine.yLoc = double.Parse(inputLine); 
             break; 
            case 6: 
             newLine.zLoc = double.Parse(inputLine); 
             break; 
            case 8: 
             newLine.xVal = double.Parse(inputLine); 
             break; 
            case 10: 
             newLine.yVal = double.Parse(inputLine); 
             break; 
            case 12: 
             newLine.zVal = double.Parse(inputLine); 
             break; 
           } 
          } 
          break; 
        } 
       } 
      } 

     } 
     public class Line 
     { 
      public static List<Line> lines = new List<Line>(); 
      public double xLoc { get; set; } 
      public double xVal { get; set; } 
      public double yLoc { get; set; } 
      public double yVal { get; set; } 
      public double zLoc { get; set; } 
      public double zVal { get; set; } 
     } 
    } 
}