读取识别显示DXF格式文件源码文档格式.docx
- 文档编号:16694631
- 上传时间:2022-11-25
- 格式:DOCX
- 页数:12
- 大小:20.73KB
读取识别显示DXF格式文件源码文档格式.docx
《读取识别显示DXF格式文件源码文档格式.docx》由会员分享,可在线阅读,更多相关《读取识别显示DXF格式文件源码文档格式.docx(12页珍藏版)》请在冰豆网上搜索。
11
39.199********322
21
736.4554281665768
31
ENDSEC
EOF
0-introductionofextendedsymbolnames,followingthe"
0"
SECTION,ENDSEC-begin/endofsection.SectionscanincludeHeader,Entities,Objects.Intheabovecode,youseeonlyEntitiessectionwheretheentitiesare.
LINE-beginsLINEentitydescription.Lines:
10
39.19953392043317
meanX1doublevalue.Thevalueafter20isY1,after30-Z1(0in2Ddrawings).11,21and31codesareconsequentlyforX2,Y2,Z2.Sohereweseethelinewithcoordinates(39.19..,36,45..-39,19..,736,45..)-thisisverticalline.
SoouraimistoreadthisASCIIformat.Weneedtoloadthefiletostreamandtotakelines-evenlines(0,2,4..)areCODE,oddlines(1,3,5...)areVALUEandrepeatthisprocedurestepbysteptilltheendoffile"
EOF"
.
Shrink
//takeapairoflinesinDXFfile,repeatthistill"
:
publicvoidNext()
{
FCode=Convert.ToInt32(FStream.ReadLine());
//code
FValue=FStream.ReadLine();
//value
}
//forcode=0wecreateentities.EntitiesherearenotonlythosewhichvisibleinAutoCAD.
//EntitiescanbealsonamesofSectionsandmanyotherinternalDXFobjects.
//ThismethodiscalledforallFCode==0
publicDXFEntityCreateEntity()
DXFEntityE;
switch(FValue)
{
case"
ENDSEC"
returnnull;
//herewedonotcreateentity
ENDBLK"
ENDTAB"
LINE"
//for"
valuewecreateDXFLineobject
E=newDXFLine();
break;
SECTION"
//"
willbeobjecttostoreotherobjectslikeLine
E=newDXFSection();
BLOCK"
//createblockobject
E=newDXFBlock();
INSERT"
//insertisreferencetoblock.
E=newDXFInsert();
TABLE"
E=newDXFTable();
CIRCLE"
E=newDXFCircle();
LAYER"
E=newDXFLayer();
TEXT"
E=newDXFText();
MTEXT"
E=newDXFMText();
ARC"
E=newDXFArc();
ELLIPSE"
E=newDXFEllipse();
default:
//therearemanyotherobjectsarepossible.ForthemwecreateemptyEntity
E=newDXFEntity();
}
//eachEntitywillneedreferencetotheBaseobjectConverter,whichstoresallEntities.
E.Converter=this;
returnE;
//returnEntityandafteritisaddedtothearrayofEntities
}
Themethodtoreadpropertiesofentitiesisessentiallysimilartotheonedescribedabovebutithasoneimportantpoint:
differententitieshavebothidenticalanddifferentproperties.Forinstance,manyEntitieshave"
basepoint"
-inDXF,whichisdescribedbycodes:
10(x),20(y),30(z):
0.0
ThesecodesarethesameforLINE,CIRCLE,ELLIPSE,TEXTandformanyothers.SowecanmakeObject-Orientedstructuretoreadandtostorethepropertiesinordertoavoiddouble-coding.WewillstoreLayerandBasePointinentity"
DXFVisibleEntity"
whichwillbeancestorforallvisibleentities.Lookatthecodetoreadtheseproperties:
//baseclassforallvisibleentities
publicclassDXFVisibleEntity:
DXFEntity
//Basepoint(x,y,z)forallentities
publicDXFImport.SFPointPoint1=newSFPoint();
//virtualfunctionReadProperty()isoverriddeninalldescendantsofEntitytoread
//specificproperties
publicoverridevoidReadProperty()
//forthedifferentcodeswereadvalues
switch(Converter.FCode)
//readLayer
case8:
layer=Converter.LayerByName(Converter.FValue);
//readCoordinates
case10:
//X
Point1.X=Convert.ToSingle(Converter.FValue,Converter.N);
case20:
//Y
Point1.Y=Convert.ToSingle(Converter.FValue,Converter.N);
//readColor
case62:
FColor=CADImage.IntToColor(Convert.ToInt32(Converter.FValue,Converter.N));
WeusethesameapproachtoreadthesecondcoordinateinLINE,radiusinCircleandsoon.
DXFFileandDXFImport.NETStructure
ThisschemeshowsmainpartsofDXFfileandthewaytheyareconnectedwiththeC#sourcecodeintheproject.ThedashlinesstandforassociationsbetweenDXFfileobjectsandobjects,programmedinC#.
CADImageisaclassforloadingfromDXFfileanddrawingtoGraphics.ItstorestheDXFEntitiesinfield:
publicDXFSectionFEntities;
Inthescheme,itisDXFSection.
DXFEntityisbaseclassforallEntitiesclasses.ClassesDXFBlocksandDXFSectionarenotvisible.ClassDXFVisibleEntityistheancestorforallvisibleEntities.
Bytheway,theschemeaboveismadeinDXFformatinABViewersoftware.
CADTricks
IfyouarenotfamiliarwithAutoCAD,pleasepayattentiontothestructureofDXFentities.Thereisaspecialentity"
Block"
whichmayhavemany"
inserts"
intheCADdrawing.Blockisjustsetofentities(includingnestedblocks)whichcanbeinsertedmanytimes.
Note
Blockchangesmanypropertiesofelementwhenshowingit.
Soifyouwanttoknowthecolorofentity,itisnotenoughtoread
"
Entity.Color"
-itisnecessarytoseealltheInsertsandBlocks,
inwhichthisentitycanbeincluded.TogetthecorrectcolorinDXFImport
.NETprojectwemadethefollowingfunction:
EntColor(DXFEntityE,DXFInsertIns).
PleaseuseEntColor()functiongetthecorrectColortypeevenifyoudonothaveBlocksinthefile.Payattentionthatthemostcommoncoloris"
ByLayer"
andinordertoreadthecorrectcolor,weneedtoreadthecolorfromLayerentity.Thisfunctionalityisalsoprovidedinthisfunction.
BelowistheEntColorfunction.Ithasmanytricks,forinstancecheckingthelayer.name=="
-inDXFlayer"
isspecialandelementswithcolor"
getthecolorfromBlockiftheyareon"
layer.
//UsethisfunctoknowthecolorofEntity,DXFInsertisInsertentityornull.
publicstaticColorEntColor(DXFEntityE,DXFInsertIns)
DXFInsertvIns=Ins;
DXFEntityEnt=E;
ColorResult=DXFConst.clNone;
if(EntisDXFVisibleEntity)Result=E.FColor;
/*if(EntisPolyline)
Result=((Polyline)Ent).Pen.Pen.Color;
*/
if(E.layer==null)returnResult;
/*algorithmisratherdifficulthere.Thisistheway,howAutoCADworkswiththecolors,
ifyoutrytocreateentitiesinAutoCAD,youwillseehowtheyuseColors*/
if((Result==clByLayer)||(Result==clByBlock))
if((vIns==null)||((Result==clByLayer)&
&
(Ent.layer.name!
="
)))
if(Result==clByLayer)
if(Ent.layer.color!
=clNone)
Result=Ent.layer.color;
elseResult=Color.Black;
else
while(vIns!
=null)
Result=vIns.color;
if((Result!
=clByBlock)&
!
((Result==clByLayer)&
(vIns.layer.name=="
Result=vIns.layer.color;
if((vIns.owner==null)&
(Result==clByLayer))
vIns=vIns.owner;
Result=clNone;
returnResult;
HowtoUsetheSoftware
ThemaincodeisinDXFImport.cs.YoucanjustusethisfileinyourprojectorseetoitasanexampleofreadingandvisualizationofDXFfiles.
SamplecodetouseDXFImport.csisForm1.cs.
HowtoViewEntities
InForm1.cs,weusetheForm1_Paintevent:
privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse)
//FCADImage-baseclasstoreferencetoDXF
if(FCADImage==null)
return;
FCADImage.Draw(e.Graphics);
//CADImage.Draw()acceptsGraphicstodrawto
WecanuseFCADImage.Draw()fordrawingtoanyGraphics-forinstance,toprinter.FCADImage.Draw()functionshoulduseaspecialalgorithm,wheneachentityisdrawnwiththeuseofblock/insert/layerparameters.
publicvoidDraw(Graphicse)
if(FMain==null)
FGraphics=e;
//Iteratebeginstoovertheentitiestodrawallofthem
FEntities.Iterate(newCADEntityProc(DrawEntity),FParams);
PayattentiontoFEntities.Iterate()func,itallowsaccessingallentitiesincludingtheirbeinginsidetheblocks.Thisishowitworks:
ThereisaclassDXFGroupwhichisanancestorofEntityandwhichcanstorearrayofEntities.Forinstance,BlockisancestorofDXFGroupandcanstoremanyentitieslikeLINE.Forbetterunification,wecanuseONEBASEGROUPENTITYobjectforallthedrawing,thisEntitywillhavearrayofallentities,eachofthemcanalsobetheGroup-theclassic"
Tree"
IteratemethodfinallyshouldcallDraw()foreachEntityinordertodrawittotheGraphics:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 读取 识别 显示 DXF 格式文件 源码
![提示](https://static.bdocx.com/images/bang_tan.gif)