当前位置:文档之家› C# 读取DXF文件 (2)

C# 读取DXF文件 (2)

u s i n g S y s t e m;usingusing ponentModel;using System.Data;using System.Drawing;using System.Text;usingusing System.IO;using System.Collections;namespace DXF{public partial class Form1 : Form{private FileStream fs;private StreamReader sr;private ArrayList LayerList = new ArrayList();private ArrayList LineList = new ArrayList();private ArrayList ArcList=new ArrayList();private ArrayList EllipseList = new ArrayList();private ArrayList LwopolylineList = new ArrayList();private ArrayList SplineList=new ArrayList();private string[] str = new string[2];private int count;private double leftx;private double lefty;private double rightx;private double righty;public Form1(){InitializeComponent();}private string[] ReadPair(){string code = sr.ReadLine().Trim();string codedata = sr.ReadLine().Trim();count += 2;string[] result = new string[2] { code, codedata };return result;}private void Read(){while (sr.Peek() != -1){str = ReadPair();if (str[1] == "SECTION"){str = ReadPair();switch (str[1]){case"HEADER": ReadHeader();break;case"TABLES": ReadTable();break;case"ENTITIES": ReadEntities();break;}}}sr.Close();fs.Close();btDraw.Enabled = true;label1.Text = count.ToString();count = 0;}private void ReadTable(){while (str[1] != "ENDSEC"){while (str[0] != "2" || str[1] != "LAYER") {str = ReadPair();}while (str[0] != "0" || str[1] != "LAYER") {str = ReadPair();}while (str[0] == "0" && str[1] == "LAYER"){ReadLAYER();}while (str[1] != "ENDSEC"){str = ReadPair();}}}private void ReadLAYER(){LAYER newlayer = new LAYER();while (str[1] != "ENDTAB"){str = ReadPair();switch (str[0]){case"2": = str[1];break;case"62": newlayer.colornum = str[1];break;case"6": newlayer.lstyle = str[1];break;case"370": newlayer.lwidth = str[1];break;}if (str[0] == "0" && str[1] == "LAYER") {LayerList.Add(newlayer);return;}}LayerList.Add(newlayer);}private void ReadEntities(){while (str[1] != "ENDSEC"){switch (str[1]){case"LINE": ReadLine();break;case"ARC": ReadArc();break;case"CIRCLE": ReadArc();break;case"ELLIPSE": ReadEllipse();break;case"LWPOLYLINE": ReadLwpolyline();break;case"SPLINE": ReadSpline();break;default: str = ReadPair();break;}}}private void ReadArc(){ARC newarc = new ARC();while (str[1] != "ENDSEC"){str = ReadPair();switch (str[0]){case"8": newarc.LName = str[1];break;case"10": newarc.CenterX = Double.Parse(str[1]);break;case"20": newarc.CenterY = Double.Parse(str[1]);break;case"40": newarc.Radiu = Double.Parse(str[1]);break;case"50": newarc.SAngle = Double.Parse(str[1]);break;case"51": newarc.EAngle = Double.Parse(str[1]);break;case"370": newarc.lwidth = str[1];break;case"0": ArcList.Add(newarc);return;}}}private void ReadLine(){LINE newline = new LINE();while (str[1] != "ENDSEC"){str = ReadPair();switch (str[0]){case"8": newline.LName = str[1];break;case"10": newline.StartX = Double.Parse(str[1]);break;case"20": newline.StartY = Double.Parse(str[1]);break;case"11": newline.EndX = Double.Parse(str[1]);break;case"21": newline.EndY = Double.Parse(str[1]);break;case"62": newline.colornum = str[1];break;case"370": newline.lwidth = str[1];break;case"0": LineList.Add(newline);return;}}}private void ReadEllipse(){ELLIPSE newellipse = new ELLIPSE();while (str[1] != "ENDSEC"){str = ReadPair();switch (str[0]){case"8": newellipse.LName = str[1];break;case"10": newellipse.CenterX = Double.Parse(str[1]);break;case"20": newellipse.CenterY = Double.Parse(str[1]);break;case"11": newellipse.DeltaX = Double.Parse(str[1]);break;case"21": newellipse.DeltaY = Double.Parse(str[1]);break;case"40": newellipse.Radio = Double.Parse(str[1]);break;case"41": newellipse.PSAngle = Double.Parse(str[1]);break;case"42": newellipse.PEAngle = Double.Parse(str[1]);break;case"370": newellipse.lwidth = str[1];break;case"0": EllipseList.Add(newellipse);return;}}}private void ReadLwpolyline(){LWPOLYLINE newlw = new LWPOLYLINE();while (str[1] != "ENDSEC"){str = ReadPair();switch (str[0]){case"8": newlw.LName = str[1];break;case"370": newlw.lwidth = str[1];break;case"62": newlw.colornum = str[1];break;case"90": newlw.PointCount = Int32.Parse(str[1]);break;case"70": newlw.Flag = Int32.Parse(str[1]);break;case"10": newlw.pointx = new double[newlw.PointCount];newlw.pointy = new double[newlw.PointCount];//if (newlw.Flag == 1)newlw.converxity = new double[newlw.PointCount];//else//newlw.converxity = new double[newlw.PointCount - 1];newlw.pointx[0] = Double.Parse(str[1]);str=ReadPair();newlw.pointy[0] = Double.Parse(str[1]);for (int i = 1; i < newlw.PointCount; i++){string temp = sr.ReadLine().Trim();if (temp == "42"){newlw.converxity[i - 1] = Double.Parse(sr.ReadLine().Trim()); i--;}else if (temp == "20"){string r = sr.ReadLine().Trim();newlw.pointy[i] = Double.Parse(r);}else{string r = sr.ReadLine().Trim();newlw.pointx[i] = Double.Parse(r);i--;}}string s = sr.ReadLine().Trim();if (s == "42")newlw.converxity[newlw.PointCount - 1] = Double.Parse(sr.ReadLine().Trim());else if (s == "0"){sr.ReadLine();LwopolylineList.Add(newlw);return;}else sr.ReadLine();break;case"0": LwopolylineList.Add(newlw);return;}}}public void ReadSpline(){SPLINE newspline = new SPLINE();while (str[1] != "ENDSEC"){str = ReadPair();switch (str[0]){case"8": newspline.LName = str[1];break;case"370": newspline.lwidth = str[1];break;case"62": newspline.colornum = str[1];break;case"70": newspline.Flag = Int32.Parse(str[1]);break;case"74": newspline.Count = Int32.Parse(str[1]);newspline.throughpx = new double[Int32.Parse(str[1])]; newspline.throughpy = new double[Int32.Parse(str[1])];break;case"12": newspline.SVertorX = Double.Parse(str[1]);break;case"22": newspline.SVertorY = Double.Parse(str[1]);break;case"13": newspline.EVertorX = Double.Parse(str[1]);break;case"23": newspline.EVertorY = Double.Parse(str[1]);break;case"11": newspline.throughpx[0] = Double.Parse(str[1]);str = ReadPair();newspline.throughpy[0] = Double.Parse(str[1]);str = ReadPair();for(int{str=ReadPair();if(str[0]=="11"){newspline.throughpx[i]=Double.Parse(str[1]); i--;}else if(str[0]=="21"){newspline.throughpy[i]=Double.Parse(str[1]); i--;}}if(newspline.Flag==11){for(int i=0;i<3;i++){str=ReadPair();}}break;case"0": SplineList.Add(newspline);return;}}}public void ReadHeader(){while (str[1] != "ENDSEC"){str = ReadPair();switch (str[1]){case"$EXTMIN": str = ReadPair();leftx = Double.Parse(str[1]);str = ReadPair();lefty = Double.Parse(str[1]);break;case"$EXTMAX": str = ReadPair();rightx = Double.Parse(str[1]);str = ReadPair();righty = Double.Parse(str[1]);break;}}}//打开DXF文件private void btOpen_Click(object sender, EventArgs e){btDraw.Enabled = false;LayerList.Clear();LineList.Clear();ArcList.Clear();EllipseList.Clear();LwopolylineList.Clear();SplineList.Clear();this.openFileDialog1.ShowDialog();if (this""){string path = this.openFileDialog1.FileName;fs = new FileStream(path, FileMode.Open, FileAccess.Read); sr = new StreamReader(fs);Read();}}private void btDraw_Click(object sender, EventArgs e){double width = Math.Ceiling(rightx) - Math.Ceiling(leftx) + 40;double height = Math.Ceiling(righty) - Math.Ceiling(lefty) + 40;Bitmap bmp = new Bitmap((int)width, (int)height);Graphics gx = Graphics.FromImage(bmp);double pw = width / (double)460;double ph = height / (double)384;for (int i = 0; i < LineList.Count; i++){LINE temp = (LINE)LineList[i];temp.Move(leftx, lefty, righty - lefty + 39);temp.Draw(gx);}for (int i = 0; i < ArcList.Count; i++){ARC temp = (ARC)ArcList[i];temp.Move(leftx, lefty, righty - lefty + 39);temp.Draw(gx);}for (int i = 0; i < EllipseList.Count; i++){ELLIPSE temp = (ELLIPSE)EllipseList[i];temp.Move(leftx + 1, lefty + 1);temp.Draw(gx, height - 1, pw, ph);}for (int i = 0; i < LwopolylineList.Count; i++){LWPOLYLINE temp = (LWPOLYLINE)LwopolylineList[i];temp.Draw(gx, leftx, lefty, righty - lefty + 39);}for (int i = 0; i < SplineList.Count; i++){SPLINE temp = (SPLINE)SplineList[i];temp.Draw(gx, leftx - 10, lefty, righty - lefty + 39);}this.pictureBox1.Image = bmp; }}}。

相关主题