How to Read Layer and Linetype in Autocad Vb.net

  • Download source - 39.21 KB
  • Download demo - 23.31 KB

Problem and Solution

Software development in industry, edifice and many other fields requires working with CAD drawings. The well-nigh popular CAD formats are AutoCAD DWG and AutoCAD DXF, the latter being "simplified" dwg - a special format to be used past developers. The trouble is that DXF and DWG formats are actually complicated. They accept dozens of objects with hundreds of interaction tricks and thousands of properties. Official DXF Reference from Autodesk has 256 pages though it fails to draw many important facts. Hence, the evolution for CAD drawings is frequently required but is not easy to implement. This commodity is to tell you how to write the DXF reader in C#, what problems can arise and of course you can observe case in C# source lawmaking, which is costless for employ under MPL license.

dxfimportnet.png

DXF Construction

DXF is an open up ASCII format from Autodesk and you can hands find documentation on information technology in the web. Here are some words about it. Below is a very unproblematic example, to show the main parts:

          0  SECTION   2 ENTITIES   0 LINE  10 39.19953392043317  xx 36.4554281665769  xxx 0.0  11 39.19953392043322  21 736.4554281665768  31 0.0   0 ENDSEC   0 EOF

0 - introduction of extended symbol names, following the "0"

Section, ENDSEC - begin / terminate of section. Sections can include Header, Entities, Objects. In the above code, you lot see just Entities section where the entities are.

LINE - begins LINE entity description. Lines:

10   39.19953392043317        

hateful X1 double value. The value afterward 20 is Y1, later on 30 - Z1 (0 in 2nd drawings). 11, 21 and 31 codes are consequently for X2, Y2, Z2. Then here we run across the line with coordinates (39.19.., 36, 45.. - 39,19.., 736,45..) - this is vertical line.

Then our aim is to read this ASCII format. We need to load the file to stream and to have lines - even lines (0, 2, 4..) are Code, odd lines (1, 3, v...) are VALUE and repeat this procedure step by step till the end of file "EOF".

            public          void          Next() {   FCode = Convert.ToInt32(FStream.ReadLine());    FValue = FStream.ReadLine();  }             public          DXFEntity CreateEntity() {   DXFEntity E;          switch          (FValue)   {          case          "          ENDSEC":          render          null;             case          "          ENDBLK":          return          nothing;          case          "          ENDTAB":          render          aught;          case          "          LINE":           East =          new          DXFLine();          pause;          example          "          SECTION":        E =          new          DXFSection();          pause;          case          "          BLOCK":        Due east =          new          DXFBlock();          suspension;          example          "          INSERT":        E =          new          DXFInsert();          intermission;          case          "          TABLE":       Due east =          new          DXFTable();          break;          case          "          Circumvolve":       E =          new          DXFCircle();          break;          case          "          LAYER":       E =          new          DXFLayer();          break;          case          "          TEXT":       Due east =          new          DXFText();          break;          case          "          MTEXT":       Due east =          new          DXFMText();          pause;          example          "          ARC":       Due east =          new          DXFArc();          break;          case          "          ELLIPSE":       E =          new          DXFEllipse();          interruption;          default:        E =          new          DXFEntity();          break;   }      E.Converter =          this;          render          E;  }        

The method to read properties of entities is essentially similar to the ane described to a higher place but information technology has one important point: different entities have both identical and different properties. For case, many Entities take "base bespeak" - in DXF, which is described by codes: x (x), 20 (y), thirty(z):

LINE  x 39.19953392043317  xx 36.4554281665769  thirty 0.0        

These codes are the aforementioned for LINE, Circumvolve, ELLIPSE, TEXT and for many others. So nosotros tin make Object-Oriented construction to read and to store the backdrop in order to avoid double-coding. We will shop Layer and Base Point in entity "DXFVisibleEntity" which will be ancestor for all visible entities. Look at the lawmaking to read these properties:

            public          form          DXFVisibleEntity : DXFEntity {           public          DXFImport.SFPoint Point1 =          new          SFPoint();                public          override          void          ReadProperty()   {             switch          (Converter.FCode)     {                   case          eight:         layer = Converter.LayerByName(Converter.FValue);          break;                   instance          10:          Point1.X = Convert.ToSingle(Converter.FValue, Converter.N);          suspension;          case          20:          Point1.Y = Convert.ToSingle(Converter.FValue, Converter.N);          break;                   case          62:         FColor = CADImage.IntToColor(Convert.ToInt32(Converter.FValue, Converter.N));          break;     }   } }        

We use the same approach to read the 2d coordinate in LINE, radius in Circumvolve and so on.

DXF File and DXF Import .Internet Construction

dxfimportnetstructure.png

This scheme shows main parts of DXF file and the way they are connected with the C# source lawmaking in the project. The dash lines stand for associations betwixt DXF file objects and objects, programmed in C#.

CADImage is a class for loading from DXF file and drawing to Graphics. Information technology stores the DXF Entities in field:

          public          DXFSection FEntities;        

In the scheme, it is DXFSection.

DXFEntity is base of operations course for all Entities classes. Classes DXFBlocks and DXFSection are not visible. Class DXFVisibleEntity is the antecedent for all visible Entities.

Past the manner, the scheme above is made in DXF format:) in ABViewer software.

CAD Tricks

If you are not familiar with AutoCAD, please pay attention to the structure of DXF entities. There is a special entity "Block" which may accept many "inserts" in the CAD drawing. Block is merely set of entities (including nested blocks) which can be inserted many times.

Annotation

Block changes many backdrop of element when showing information technology.  And then if you want to know the color of entity, it is not enough to read   "Entity.Colour"  - it is necessary to see all the Inserts and Blocks,  in which this entity tin be included. To get the right color in DXF Import  .NET project nosotros fabricated the post-obit function: EntColor(DXFEntity E, DXFInsert Ins).

Delight use EntColor() function go the correct Color blazon fifty-fifty if you do non have Blocks in the file. Pay attention that the most common color is "ByLayer" and in social club to read the correct color, we need to read the color from Layer entity. This functionality is also provided in this role.

Below is the EntColor function. Information technology has many tricks, for instance checking the layer.proper noun == "0" - in DXF layer "0" is special and elements with colour "ByLayer" get the color from Cake if they are on "0" layer.

            public          static          Color EntColor(DXFEntity Eastward, DXFInsert Ins) {   DXFInsert vIns = Ins;   DXFEntity Ent = E;   Colour Upshot = DXFConst.clNone;          if(Ent          is          DXFVisibleEntity) Result = E.FColor;             if(E.layer ==          naught)          return          Issue;            if((Result ==  clByLayer)||(Consequence == clByBlock))   {          if((vIns ==          goose egg)||((Consequence == clByLayer)&&(Ent.layer.proper noun !=          "          0")))     {          if(Effect == clByLayer)       {          if(Ent.layer.color != clNone)           Issue = Ent.layer.color;          else          Outcome = Color.Blackness;       }     }          else          {          while(vIns !=          zippo)       {         Result = vIns.color;          if((Result !=  clByBlock) && !((Outcome ==  clByLayer) &&           (vIns.layer.name ==          "          0")))         {          if(Result ==  clByLayer)             Effect = vIns.layer.color;          break;         }          if((vIns.owner ==          null)&&(Outcome == clByLayer))           Result = vIns.layer.color;         vIns = vIns.possessor;       }     }   }          if((Result == clByLayer)||(Result == clByBlock))     Result = clNone;          return          Event; }

How to Use the Software

The principal code is in DXFImport.cs. You can only use this file in your project or meet to it every bit an instance of reading and visualization of DXF files.

Sample code to apply DXFImport.cs is Form1.cs.

How to View Entities

In Form1.cs, we use the Form1_Paint event:

          private          void          Form1_Paint(object          sender, Arrangement.Windows.Forms.PaintEventArgs e) {            if          (FCADImage ==          null)          return;   FCADImage.Draw(e.Graphics);  }

We can use FCADImage.Depict() for cartoon to any Graphics - for instance, to printer. FCADImage.Draw() function should utilize a special algorithm, when each entity is drawn with the use of block/insert/layer parameters.

          public          void          Describe(Graphics east) {          if          (FMain ==          aught)          return;   FGraphics = eastward;      FEntities.Iterate(new          CADEntityProc(DrawEntity), FParams); }

Pay attention to FEntities.Iterate() func, it allows accessing all entities including their being within the blocks. This is how information technology works: There is a class DXFGroup which is an ancestor of Entity and which can shop array of Entities. For instance, Block is antecedent of DXFGroup and tin can store many entities like LINE. For amend unification, we tin use ONE BASE GROUP ENTITY object for all the drawing, this Entity will have array of all entities, each of them can besides exist the Grouping - the archetype "Tree".

Iterate method finally should call Describe() for each Entity in order to describe it to the Graphics:

          protected          static          void          DrawEntity(DXFEntity Ent)     {         Ent.Depict(FGraphics);     }        

Depict() method is overridden in descendants of Entity to draw particular entities. Let us see in particular how it is implemented in DXFLine:

            public          override          void          Draw(System.Drawing.Graphics G)   {          SFPoint P1, P2;          Colour RealColor = DXFConst.EntColor(this, Converter.FParams.Insert);          P1 = Converter.GetPoint(Point1);          P2 = Converter.GetPoint(Point2);          if          (FVisible)       G.DrawLine(new          Pen(RealColor,          one),  P1.Ten, P1.Y, P2.10, P2.Y);   }        
  1. We get the Real Color via DXFConst.EntColor(this, Converter.FParams.Insert); equally I described earlier.
  2. Points are converted from Global Coordinates to screen coordinates in part GetPoint(). GetPoint not only converts global-to-screen but too uses Block offsets and cake scale inside. Thus it facilitates the evolution work, eliminating the demand to "encounter" what cake is being drawn at the moment - Cake changes "FParams.matrix" to draw itself. And all entities coordinates use this "FParams.matrix".
  3. Entity is fatigued to the given Graphics 1000:

Then y'all can describe to printer, to raster image or to other Graphics.

DXF Import .NET Reference

          public          class          DXFConst        

Stores constants and base functions.

          public          class          DXFMatrix        

Class to work with coordinates.

          public          struct          FRect        

Description of 3D space where the CAD drawing is situated in global coordinates.

          public          struct          CADIterate

Stores all needed parameters for entities when processing "Iterate" function.

          public          course          CADImage        

Class to draw CAD drawing.

          public          grade          DXFEntity        

Base class for all DXF Entities.

          public          class          DXFGroup : DXFEntity

Base of operations form for all grouping entities.

          public          class          DXFTable : DXFGroup        

Class to read from DXF "Tabular array" section - here it reads only Layers.

          public          class          DXFVisibleEntity : DXFEntity

Base course for all visible entities (invisible - are "DXFTable", "DXFLayer", etc.)

          public          class          DXFCustomVertex: DXFVisibleEntity        

Special class for 3D point in DXF.

          public          class          DXFText: DXFCustomVertex

Stores and Draws "Text" DXF entity.

The following classes are for particular DXF entities:

          public          class          DXFLine : DXFVisibleEntity          public          class          DXFArc: DXFCircle          public          class          DXFEllipse: DXFArc          public          form          DXFLayer: DXFEntity
          public          class          DXFBlock : DXFGroup

Form to work with DXF Block.

          public          class          DXFInsert : DXFVisibleEntity        

Class to work with "Insert" in DXF, for AutoCAD users this is "Block reference". Blocks are not visible, Inserts are visible.

Conclusion

The purpose of the article is to requite some communication how to write DXF readers. DXF file structure is non and then hard equally its logical presentation. The article looks through the base of operations DXF format problems and shows how to observe solution for them. The instance source is written in C# and may be helpful for all who demand to have access to DXF files.

Other Projects on The Code Project

On The Code Project, you can already find some other DXF reader:

  • http://www.codeproject.com/KB/cs/dxfreader.aspx

The principal advantages of our project as compared to the project to a higher place are:

  1. Block / Inserts
  2. Layers
  3. Text

which are not presented in that DXF reader.

History

This is the first open source version of DXF Import .NET.

greenwoodinceed.blogspot.com

Source: https://www.codeproject.com/Articles/156522/DXF-Import-NET-Read-and-View-AutoCAD-Format-Files

0 Response to "How to Read Layer and Linetype in Autocad Vb.net"

إرسال تعليق

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel