123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Threading;
- namespace BaseLibRWFile
- {
- public class Dxf
- {
- public struct DataLineStruct //线状结构
- {
- //public int iLineType;
- public float fX1Pos;
- public float fY1Pos;
- public float fZ1Pos;
- public float fX2Pos;
- public float fY2Pos;
- public float fZ2Pos;
- };
- public struct DataPolyLineStruct //聚合结构
- {
- //public int iLineType;
- public int iNum;
- public float fX1Pos;
- public float fY1Pos;
- public float fX2Pos;
- public float fY2Pos;
- };
- public struct DataArcStruct //圆弧或拱形结构
- {
- public float fXPos;
- public float fYPos;
- public float fX1Pos;
- public float fY1Pos;
- public float fRPos;
- public float fptXPos;
- public float fptYPos;
- public float fAngle1Pos;
- public float fAngle2Pos;
- public float fIPos;
- public float fJPos;
- };
- public struct DataCircleStruct //圆形结构
- {
- public float fXPos;
- public float fYPos;
- public float fZPos;
- public float fRPos;
- };
- public struct DataTypeStruct //字体结构
- {
- //public int iCutNum;
- //public bool xIsMoveDown;
- //public int iLineType;
- //public float fXPos;
- //public float fYPos;
- //public float fIPos;
- //public float fJPos;
- };
- static List<DataPolyLineStruct> stuPolyLineArray = new List<DataPolyLineStruct>(); //聚合结构集合
- //static List<DataTypeStruct> stuDataArray = new List<DataTypeStruct>(); //字体结构集合
- static List<DataLineStruct> stuLineArray = new List<DataLineStruct>(); //线状结构集合
- static List<DataArcStruct> stuArcArray = new List<DataArcStruct>(); //圆弧或拱形结构集合
- static List<DataCircleStruct> stuCircleArray = new List<DataCircleStruct>(); //圆形结构集合
- private static FileInfo theSourceFile;
- private static double XMax, XMin;
- private static double YMax, YMin;
- private static double ZMax, ZMin;
- //static int xStrat, xEnd, yStart, yEnd;
- static bool xIsRead = false;
- //public static int Pointnum;
- public static string[][] ReadDXF(string filePath, string filename, out int Pointnum)
- {
- string[][] Allpoint = new string[1000][];
- //xStrat = xEnd = yStart = yEnd = 0;
- for (int i = 0; i < 1000; i++)
- {
- Allpoint[i] = new string[10];
- for (int j = 0; j < 10; j++)
- {
- Allpoint[i][j] = null;
- }
- }
- string line1, line2; //这些line1和line2用于获取a/m数据
- List<string> table1 = new List<string>();
- List<string> table2 = new List<string>();
- line1 = "0"; //初始化line1 and line2
- line2 = "0";
- Pointnum = 0;
- filename = filePath + "\\" + filename; //treeView1.SelectedNode.Text;//e.Node.Text;
- stuLineArray.Clear();
- stuCircleArray.Clear();
- stuArcArray.Clear();
- stuPolyLineArray.Clear();
- theSourceFile = new FileInfo(filename); //设定读取文件位置
- StreamReader reader; //StreamReader reader = new StreamReader(filename, Encoding.Default);
- reader = theSourceFile.OpenText(); //读取DXF文件
- //该部分对DXF文件中的绘图对象进行解析,判断线段类型
- do
- {
- table1.Add(line1);
- table2.Add(line2);
- if (line1 == "2" && line2 == "ENTITIES")
- {
- xIsRead = true;
- }
- if (line2 == "ENDSEC")
- {
- xIsRead = false;
- }
- if (xIsRead)
- {
- if (line1 == "0" && line2 == "LINE")
- {
- float x1 = 0, y1 = 0, x2 = 0, y2 = 0;
- LineModule(reader, ref x1, ref y1, ref x2, ref y2);
- Allpoint[Pointnum][0] = (Pointnum + 1).ToString();
- Allpoint[Pointnum][1] = "LINE";
- Allpoint[Pointnum][2] = x1.ToString();
- Allpoint[Pointnum][3] = y1.ToString();
- Allpoint[Pointnum][4] = x2.ToString();
- Allpoint[Pointnum][5] = y2.ToString();
- Pointnum++;
- }
- //else if (line1 == "0" && line2 == "LWPOLYLINE");
- //PolylineModule(reader);
- else if (line1 == "0" && line2 == "CIRCLE")
- {
- float x1 = 0, y1 = 0, radius = 0;
- CircleModule(reader, ref x1, ref y1, ref radius);
- Allpoint[Pointnum][0] = (Pointnum + 1).ToString();
- Allpoint[Pointnum][1] = "CIRCLE";
- Allpoint[Pointnum][2] = x1.ToString();
- Allpoint[Pointnum][3] = y1.ToString();
- Allpoint[Pointnum][4] = radius.ToString();
- Pointnum++;
- }
- else if (line1 == "0" && line2 == "ARC")
- {
- float x1 = 0, y1 = 0, x2 = 0, y2 = 0, radius = 0;
- ArcModule(reader, ref x1, ref y1, ref x2, ref y2, ref radius);
- Allpoint[Pointnum][0] = (Pointnum + 1).ToString();
- Allpoint[Pointnum][1] = "ARC";
- Allpoint[Pointnum][4] = x1.ToString();
- Allpoint[Pointnum][5] = y1.ToString();
- Allpoint[Pointnum][2] = x2.ToString();
- Allpoint[Pointnum][3] = y2.ToString();
- Allpoint[Pointnum][6] = radius.ToString();
- Pointnum++;
- }
- else if (line1 == "0" && line2 == "POLYLINE")
- PolylineModule(reader);
- //else if (line1 == "0" && line2 == "TEXT")
- // ;
- }
- GetLineCouple(reader, out line1, out line2); // 区分线段类型,将值赋给line1和line2
- }
- while (line2 != "EOF");
- reader.DiscardBufferedData(); //清除读取状态.
- theSourceFile = null;
- reader.Close();
- return Allpoint;
- //PaintBack();
- //Compare();
- //Creat();
- }
- private static void GetLineCouple(StreamReader theReader, out string line1, out string line2) //区分线段类型,将值赋给line1和line2
- {
- System.Globalization.CultureInfo ci = Thread.CurrentThread.CurrentCulture;//
- string decimalSeparator = ci.NumberFormat.CurrencyDecimalSeparator;
- line1 = line2 = "";
- if (theReader == null)
- return;
- line1 = theReader.ReadLine();
- if (line1 != null)
- {
- line1 = line1.Trim();
- line1 = line1.Replace('.', decimalSeparator[0]);
- }
- line2 = theReader.ReadLine();
- if (line2 != null)
- {
- line2 = line2.Trim();
- line2 = line2.Replace('.', decimalSeparator[0]);
- }
- }
- private static void LineModule(StreamReader reader, ref float x1, ref float y1, ref float x2, ref float y2) //解析DXF文件中的线段
- {
- string line1, line2;
- line1 = "0";
- line2 = "0";
- x1 = 0;
- y1 = 0;
- float z1 = 0;
- x2 = 0;
- y2 = 0;
- float z2 = 0;
- DataLineStruct stu1 = new DataLineStruct();
- do
- {
- GetLineCouple(reader, out line1, out line2);
- if (line1 == "10")
- {
- x1 = (float)Convert.ToDouble(line2);
- if (x1 > XMax)
- XMax = x1;
- if (x1 < XMin)
- XMin = x1;
- }
- if (line1 == "20")
- {
- y1 = (float)Convert.ToDouble(line2);
- if (y1 > YMax)
- YMax = y1;
- if (y1 < YMin)
- YMin = y1;
- }
- if (line1 == "30")
- {
- z1 = (float)Convert.ToDouble(line2);
- if (z1 > ZMax)
- ZMax = y1;
- if (z1 < ZMin)
- ZMin = y1;
- }
- if (line1 == "11")
- {
- x2 = (float)Convert.ToDouble(line2);
- if (x2 > XMax)
- XMax = x2;
- if (x2 < XMin)
- XMin = x2;
- }
- if (line1 == "21")
- {
- y2 = (float)Convert.ToDouble(line2);
- if (y2 > YMax)
- YMax = y2;
- if (y2 < YMin)
- YMin = y2;
- }
- if (line1 == "31")
- {
- z2 = (float)Convert.ToDouble(line2);
- if (z2 > ZMax)
- ZMax = y1;
- if (z2 < ZMin)
- ZMin = y1;
- }
- }
- while (line1 != "31");
- stu1.fX1Pos = x1;
- stu1.fX2Pos = x2;
- stu1.fZ1Pos = z1;
- stu1.fZ2Pos = z2;
- stu1.fY1Pos = y1;
- stu1.fY2Pos = y2;
- //DXFLibrary.Line line3 = new DXFLibrary.Line("Doors", 0, 0, 0, 10);
- //DXFLibrary.Text text = new DXFLibrary.Text("L" + stuLineArray.Count.ToString(), x1, y1, 40, "PartialHeightDoors");
- stuLineArray.Add(stu1);
- }
- private static void CircleModule(StreamReader reader, ref float x1, ref float y1, ref float radius) //解析DXF文件中的圆
- {
- string line1, line2;
- line1 = "0";
- line2 = "0";
- x1 = 0;
- y1 = 0;
- float z1 = 0;
- radius = 0;
- DataCircleStruct stu1;
- do
- {
- GetLineCouple(reader, out line1, out line2);
- if (line1 == "10")
- {
- x1 = (float)Convert.ToDouble(line2);
- }
- if (line1 == "20")
- {
- y1 = (float)Convert.ToDouble(line2);
- }
- if (line1 == "30")
- {
- z1 = (float)Convert.ToDouble(line2);
- }
- if (line1 == "40")
- {
- radius = (float)Convert.ToDouble(line2);
- if (x1 + radius > XMax)
- XMax = x1 + radius;
- if (x1 - radius < XMin)
- XMin = x1 - radius;
- if (y1 + radius > YMax)
- YMax = y1 + radius;
- if (y1 - radius < YMin)
- YMin = y1 - radius;
- }
- }
- while (line1 != "40");
- stu1.fXPos = x1;
- stu1.fYPos = y1;
- stu1.fZPos = z1;
- stu1.fRPos = radius;
- stuCircleArray.Add(stu1);
- }
- private static void ArcModule(StreamReader reader, ref float x1, ref float y1, ref float x2, ref float y2, ref float radius) //解析DXF文件中圆弧
- {
- string line1, line2;
- line1 = "0";
- line2 = "0";
- float ptx = 0;
- float pty = 0;
- x1 = 0;
- y1 = 0;
- x2 = 0;
- y2 = 0;
- radius = 0;
- float angle1 = 0;
- float angle2 = 0;
- float i = 0;
- float j = 0;
- DataArcStruct stu1;
- do
- {
- GetLineCouple(reader, out line1, out line2);
- if (line1 == "10")
- {
- ptx = (float)Convert.ToDouble(line2);
- if (ptx > XMax)
- XMax = ptx;
- if (ptx < XMin)
- XMin = ptx;
- }
- if (line1 == "20")
- {
- pty = (float)Convert.ToDouble(line2);
- if (pty > YMax)
- YMax = pty;
- if (pty < YMin)
- YMin = pty;
- }
- if (line1 == "40")
- {
- radius = (float)Convert.ToDouble(line2);
- if (ptx + radius > XMax)
- XMax = ptx + radius;
- if (ptx - radius < XMin)
- XMin = ptx - radius;
- if (pty + radius > YMax)
- YMax = pty + radius;
- if (pty - radius < YMin)
- YMin = pty - radius;
- }
- if (line1 == "50")
- angle1 = (float)Convert.ToDouble(line2);
- if (line1 == "51")
- angle2 = (float)Convert.ToDouble(line2);
- }
- while (line1 != "51");
- x1 = ptx + (float)Math.Cos(angle1 / 180.0 * Math.PI) * radius;
- y1 = pty + (float)Math.Sin(angle1 / 180.0 * Math.PI) * radius;
- x2 = ptx + (float)Math.Cos(angle2 / 180.0 * Math.PI) * radius;
- y2 = pty + (float)Math.Sin(angle2 / 180.0 * Math.PI) * radius;
- i = ptx - x1;
- j = pty - y1;
- stu1.fXPos = x1;
- stu1.fYPos = y1;
- stu1.fX1Pos = x2;
- stu1.fY1Pos = y2;
- stu1.fIPos = i;
- stu1.fJPos = j;
- stu1.fRPos = radius;
- stu1.fptXPos = ptx;
- stu1.fptYPos = pty;
- stu1.fAngle1Pos = angle1;
- stu1.fAngle2Pos = angle2;
- stuArcArray.Add(stu1);
- //这部分与绘图编辑器有关。从dxf文件中获取的数据
- // if ((Math.Abs(XMax) - Math.Abs(XMin)) > this.pictureBox1.Size.Width)
- // {
- // scaleX = (double)(this.pictureBox1.Size.Width) / (double)(Math.Abs(XMax) - Math.Abs(XMin));
- // }
- // else
- // scaleX = 1;
- //
- //
- // if ((Math.Abs(YMax) - Math.Abs(YMin)) > this.pictureBox1.Size.Height)
- // {
- // scaleY = (double)(this.pictureBox1.Size.Height) / (double)(Math.Abs(YMax) - Math.Abs(YMin));
- // }
- // else
- // scaleY = 1;
- //
- // mainScale = Math.Min(scaleX, scaleY);
- //
- //
- // int ix = drawingList.Add(new arc(new Point((int)x1, (int)-y1), radius, angle1, angle2, Color.White, Color.Red, 1));
- // objectIdentifier.Add(new DrawingObject(6, ix));
- }
- private static void PolylineModule(StreamReader reader) //解析DXF文件中折线
- {
- string line1, line2;
- line1 = "0";
- line2 = "0";
- float x1 = 0;
- float y1 = 0;
- float x2 = 0;
- float y2 = 0;
- int count = 0;
- int num = 1;
- DataPolyLineStruct stu1 = new DataPolyLineStruct();
- do
- {
- GetLineCouple(reader, out line1, out line2);
- if (line1 == "10")
- {
- if (count <= 1)
- {
- x1 = (float)Convert.ToDouble(line2);
- }
- else
- {
- x2 = (float)Convert.ToDouble(line2);
- }
- }
- if (line1 == "20")
- {
- if (count <= 1)
- {
- y1 = (float)Convert.ToDouble(line2);
- }
- else
- {
- y2 = (float)Convert.ToDouble(line2);
- }
- }
- if (line1 == "0" && line2 == "VERTEX")
- {
- if (count > 1)
- {
- stu1.fX1Pos = x1;
- stu1.fX2Pos = x2;
- stu1.fY1Pos = y1;
- stu1.fY2Pos = y2;
- stu1.iNum = num;
- stuPolyLineArray.Add(stu1);
- x1 = x2;
- y1 = y2;
- num++;
- }
- count += 1;
- }
- }
- while (line2 != "SEQEND");
- }
- }
- }
|