首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从dxf文件c#读取坐标

从dxf文件c#读取坐标
EN

Stack Overflow用户
提问于 2016-12-14 03:00:02
回答 1查看 1.8K关注 0票数 0

我正在用c#编写代码,从dxf文件中读取点坐标。我基本上是逐行读取dxf文件,并检查像if( i == "AcdbLine")这样的条件,将点的坐标写入文件中。

代码语言:javascript
复制
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块内时,它会忽略任何AcDbBlockBegin及其相应的点。

代码语言:javascript
复制
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

对不起,代码太长了,谢谢您的帮助!

EN

回答 1

Stack Overflow用户

发布于 2016-12-14 03:51:37

我已经写了40多年的文本解析器。如果我做不到,没人能做到。尝试下面的代码

代码语言:javascript
复制
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; }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41134127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档