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 stuPolyLineArray = new List(); //聚合结构集合 //static List stuDataArray = new List(); //字体结构集合 static List stuLineArray = new List(); //线状结构集合 static List stuArcArray = new List(); //圆弧或拱形结构集合 static List stuCircleArray = new List(); //圆形结构集合 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 table1 = new List(); List table2 = new List(); 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"); } } }