0

I want to read a .dxf file. I am using c# library to read. It read the file but it can't write all layers.

I want to get all list of Layers from it and write it.

Sudhir Panda
  • 774
  • 1
  • 7
  • 27
  • Its unclear what "can't write all layers" means. Are you having problems finding the layers to write them out, or having problems writing them out? And what are you writing them out to, another dxf? – Joel Lucsy Apr 28 '16 at 15:05
  • ok @JoelLucsy m clearing my requirements. I've a dxf file of a building map and i want to read how many doors in it and each door properties like(width and height) and distance between doors etc. – Sudhir Panda Apr 29 '16 at 08:05
  • Related or duplicate: https://stackoverflow.com/questions/1000785/reading-dxf-files – StayOnTarget Mar 05 '19 at 18:45

3 Answers3

2

You do not really need a library for this. DXF is in ASCII format and you got one line of text per DXF code followed immediately by a value.

All entities are making reference to a Layer using the DXF code 8, so you can do a regular expression search with '^8[\r\n]*([_a-zA-Z0-9-]+)$' and get the name. This method will work with partial DXF and older R12 DXF files. That will also give you the layers which are really used, like if you did purge the drawing first.

You could also use the Layer Table header to read the layer names. It is a bit more difficult since you need to find that section, but on the same principal, you search for code 0 - LAYER, then the following code 2 will give you the layer name.

cyrille
  • 2,616
  • 1
  • 10
  • 18
  • FYI, DXF be either text or binary based. Typically people use the text based one because its readable, but its possible to output in binary. – Joel Lucsy Apr 28 '16 at 15:02
  • You're right DXF can be binary but in this case it is a DXB format. I know I was assuming text here since it was mentioned DXF. – cyrille Apr 28 '16 at 15:06
  • I have learned to never assume a file extension accurately identifies the contents of a file. YMMV. – Joel Lucsy Apr 28 '16 at 15:08
1

I look this library. You have in public DXFTable layers all layers. Read documentation and look structure of DXFImport.cs

Nejc Galof
  • 2,538
  • 3
  • 31
  • 70
  • I am using this library @neiiic . But it cant write all layers like (LWPOLYLINE) . and i want to group layers ex (for Door= [Block : {ARC,LWPOLYLINE,LINE,LINE,LINE,LWPOLYLINE,LINE }]) – Sudhir Panda Apr 28 '16 at 07:45
  • Just to clarify that layers are not the same as LWPOLYLINE. LWPOLYLINE is an entity type. You are trying to find all the specific entities on certain layers by the sounds of it. Have you considered Teigha for DWG from www.opendesign.com? – Andrew Truckle Apr 30 '16 at 15:05
0

I've written some code to read points/lines/arc's from all ASCII DXF versions available at the moment with the same code
You can add more objects bij just adding more subroutines with the correct name
(look inside the DXF).

What it does:

  1. You send the the whole file you've imported with a reader to this routine
  2. Then it searches for the ENTITIES block (this is where al the geometry is stored)
  3. Then it searches for the object(Point/Line/Arc)
    (R12 till R14 use POINT / LINE/ ARC)
    (R2000 till R2013 use AcDbPoint / AcDbLine / AcDbArc or AcDbCircle if it contains the angles)
  4. Then it checks for a layer name
  5. Then it checks if the variable codes are there
  6. Then you can do something with the data

        private void ReadDxfFile (string DxfFile)
    {
        string Layer = "";
    
        string[] D = DxfFile.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
    
        int iEntities = 0; for(int i = 0; i < D.Length; i++) { if (D[i] == "ENTITIES") {iEntities = i; break; } }
        for (int i = iEntities; i < D.Length; i++)
        {
            if (D[i] == "POINT" || D[i] == "AcDbPoint")
            {
                int iEntity = i; if (D[i].StartsWith("AcDb")) { for (iEntity = i; D[iEntity] != "AcDbEntity"; iEntity--) ; }
                Layer = ""; for (int iLayer = iEntity; iLayer < i + 10 && Layer == ""; iLayer++) { if (D[iLayer] == "  8") { Layer = D[iLayer + 1]; }; }
                for (int iWaarden = i; iWaarden < i + 8; iWaarden++)
                {
                    if (D[iWaarden] == " 10" && D[iWaarden + 2] == " 20")
                    {
                        //Here you can store the following data in a list for later use
                        //LayerName = Layer
                        //X = D[iWaarden + 1]
                        //Y = D[iWaarden + 3]
                        //Z = D[iWaarden + 5]
                    }
                }
            }
    
            if (D[i] == "LINE" || D[i] == "AcDbLine")
            {
                int iEntity = i; if (D[i].StartsWith("AcDb")) { for (iEntity = i; D[iEntity] != "AcDbEntity"; iEntity--) ; }
                Layer = ""; for (int iLayer = iEntity; iLayer < i + 10 && Layer == ""; iLayer++) { if (D[iLayer] == "  8") { Layer = D[iLayer + 1]; }; }
                for (int iWaarden = i; iWaarden < i + 10; iWaarden++)
                {
                    if (D[iWaarden] == " 10" && D[iWaarden + 2] == " 20")
                    {
                        //Here you can store the following data in a list for later use
                        //LayerName = Layer
                        //Xbegin = D[iWaarden + 1]
                        //Ybegin = D[iWaarden + 3]
                        //Zbegin = D[iWaarden + 5]
                        //Xend = D[iWaarden + 7]
                        //Yend = D[iWaarden + 9]
                        //Zend = D[iWaarden + 11]
                    }
                }
            }
    
            if (D[i] == "ARC" || D[i] == "AcDbArc" || D[i] == "AcDbCircle")
            {
                int iEntity = i; if (D[i].StartsWith("AcDb")) { for (iEntity = i; D[iEntity] != "AcDbEntity"; iEntity--) ; }
                Layer = ""; for (int iLayer = iEntity; iLayer < i + 10 && Layer == ""; iLayer++) { if (D[iLayer] == "  8") { Layer = D[iLayer + 1]; }; }
                for (int iWaarden = i; iWaarden < i + 10; iWaarden++)
                {
                    if (D[iWaarden] == " 10" && D[iWaarden + 2] == " 20" && D[iWaarden + 10] == " 51")
                    {
                        //Here you can store the following data in a list for later use
                        //LayerName = Layer
                        //Xmid = D[iWaarden + 1]
                        //Ymid = D[iWaarden + 3]
                        //Zmid = D[iWaarden + 5]
                        //Radius = D[iWaarden + 7]
                        //StartAngle = D[iWaarden + 9]
                        //StartAngle = D[iWaarden + 11]
                    }
                    if (D[iWaarden] == " 10" && D[iWaarden + 2] == " 20" && D[iWaarden + 12] == " 51")
                    {
                        //Here you can store the following data in a list for later use
                        //LayerName = Layer
                        //Xmid = D[iWaarden + 1]
                        //Ymid = D[iWaarden + 3]
                        //Zmid = D[iWaarden + 5]
                        //Radius = D[iWaarden + 7]
                        //StartAngle = D[iWaarden + 11]
                        //StartAngle = D[iWaarden + 13]
                    }
                }
            }
    
        }
    }
    
Sander
  • 37
  • 5