C# 读取DXF文件 2.docx
《C# 读取DXF文件 2.docx》由会员分享,可在线阅读,更多相关《C# 读取DXF文件 2.docx(16页珍藏版)》请在冰豆网上搜索。
C#读取DXF文件2
usingSystem;
using
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
using
usingSystem.IO;
usingSystem.Collections;
namespaceDXF
{
publicpartialclassForm1:
Form
{
privateFileStreamfs;
privateStreamReadersr;
privateArrayListLayerList=newArrayList();
privateArrayListLineList=newArrayList();
privateArrayListArcList=newArrayList();
privateArrayListEllipseList=newArrayList();
privateArrayListLwopolylineList=newArrayList();
privateArrayListSplineList=newArrayList();
privatestring[]str=newstring[2];
privateintcount;
privatedoubleleftx;
privatedoublelefty;
privatedoublerightx;
privatedoublerighty;
publicForm1()
{
InitializeComponent();
}
privatestring[]ReadPair()
{
stringcode=sr.ReadLine().Trim();
stringcodedata=sr.ReadLine().Trim();
count+=2;
string[]result=newstring[2]{code,codedata};
returnresult;
}
privatevoidRead()
{
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;
}
privatevoidReadTable()
{
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();
}
}
}
privatevoidReadLAYER()
{
LAYERnewlayer=newLAYER();
while(str[1]!
="ENDTAB")
{
str=ReadPair();
switch(str[0])
{
case"2":
newlayer.name=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);
}
privatevoidReadEntities()
{
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;
}
}
}
privatevoidReadArc()
{
ARCnewarc=newARC();
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;
}
}
}
privatevoidReadLine()
{
LINEnewline=newLINE();
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;
}
}
}
privatevoidReadEllipse()
{
ELLIPSEnewellipse=newELLIPSE();
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;
}
}
}
privatevoidReadLwpolyline()
{
LWPOLYLINEnewlw=newLWPOLYLINE();
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=newdouble[newlw.PointCount];
newlw.pointy=newdouble[newlw.PointCount];
//if(newlw.Flag==1)
newlw.converxity=newdouble[newlw.PointCount];
//else
//newlw.converxity=newdouble[newlw.PointCount-1];
newlw.pointx[0]=Double.Parse(str[1]);
str=ReadPair();
newlw.pointy[0]=Double.Parse(str[1]);
for(inti=1;i{
stringtemp=sr.ReadLine().Trim();
if(temp=="42")
{
newlw.converxity[i-1]=Double.Parse(sr.ReadLine().Trim());
i--;
}
elseif(temp=="20")
{
stringr=sr.ReadLine().Trim();
newlw.pointy[i]=Double.Parse(r);
}
else
{
stringr=sr.ReadLine().Trim();
newlw.pointx[i]=Double.Parse(r);
i--;
}
}
strings=sr.ReadLine().Trim();
if(s=="42")
newlw.converxity[newlw.PointCount-1]=Double.Parse(sr.ReadLine().Trim());
elseif(s=="0")
{
sr.ReadLine();
LwopolylineList.Add(newlw);
return;
}
elsesr.ReadLine();
break;
case"0":
LwopolylineList.Add(newlw);
return;
}
}
}
publicvoidReadSpline()
{
SPLINEnewspline=newSPLINE();
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=newdouble[Int32.Parse(str[1])];
newspline.throughpy=newdouble[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--;
}
elseif(str[0]=="21")
{
newspline.throughpy[i]=Double.Parse(str[1]);
i--;
}
}
if(newspline.Flag==11)
{
for(inti=0;i<3;i++)
{
str=ReadPair();
}
}
break;
case"0":
SplineList.Add(newspline);
return;
}
}
}
publicvoidReadHeader()
{
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文件
privatevoidbtOpen_Click(objectsender,EventArgse)
{
btDraw.Enabled=false;
LayerList.Clear();
LineList.Clear();
ArcList.Clear();
EllipseList.Clear();
LwopolylineList.Clear();
SplineList.Clear();
this.openFileDialog1.ShowDialog();
if(this"")
{
stringpath=this.openFileDialog1.FileName;
fs=newFileStream(path,FileMode.Open,FileAccess.Read);
sr=newStreamReader(fs);
Read();
}
}
privatevoidbtDraw_Click(objectsender,EventArgse)
{
doublewidth=Math.Ceiling(rightx)-Math.Ceiling(leftx)+40;
doubleheight=Math.Ceiling(righty)-Math.Ceiling(lefty)+40;
Bitmapbmp=newBitmap((int)width,(int)height);
Graphicsgx=Graphics.FromImage(bmp);
doublepw=width/(double)460;
doubleph=height/(double)384;
for(inti=0;i{
LINEtemp=(LINE)LineList[i];
temp.Move(leftx,lefty,righty-lefty+39);
temp.Draw(gx);
}
for(inti=0;i{
ARCtemp=(ARC)ArcList[i];
temp.Move(leftx,lefty,righty-lefty+39);
temp.Draw(gx);
}
for(inti=0;i{
ELLIPSEtemp=(ELLIPSE)EllipseList[i];
temp.Move(leftx+1,lefty+1);
temp.Draw(gx,height-1,pw,ph);
}
for(inti=0;i{
LWPOLYLINEtemp=(LWPOLYLINE)LwopolylineList[i];
temp.Draw(gx,leftx,lefty,righty-lefty+39);
}
for(inti=0;i{
SPLINEtemp=(SPLINE)SplineList[i];
temp.Draw(gx,leftx-10,lefty,righty-lefty+39);
}
this.pictureBox1.Image=bmp;
}
}
}