Reading .DXF files [closed]

2020-01-29 06:30发布

问题:


Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 2 years ago.

Does anyone know of source code, ideally in C# or similar, for reading .DXF files (as used by AutoCAD etc)? If not code, then tables showing the various codes (elements / blocks / etc) and their meanings?

I am writing a reader myself, and have dead tree documentation detailing the format, but am trying to avoid writing e.g. a converter from each of the 255 ACI colours to RGB... Thanks!

回答1:

Cadlib from WoutWare have I been using for a couple of projects with good results.



回答2:

I have work a couple of years at developing my own DXf-Viewer in java (you could drop your own DXF file or an URL on the viewer) for 2D drawings.
The published information from AutoCAD is a good base but doesn't explain how it works.
Becoming member of the Open Design Alliance, will give you the possibility to convert several CAD formats to DXF. It may be a good idea if you are developing a commercial product.
There is a german book (http://www.crlf.de/Verlag/DXF-intern/DXF-intern.html) about DXF which really explain this format. It's expensive, but could save days of search.
The colors in the DXF Format are indexed, you must have a converter from ACI to RGB. Be careful with values 0 and 1 which having a special meaning.

Regards.



回答3:

Fortunately AutoCAD publish the DXF format information here: DXF Formats



回答4:

Update in case someone is still looking...
It's the same library, just including both links.

https://github.com/haplokuon/netDxf
https://www.nuget.org/packages/netDXF/



回答5:

You may want to try the Open Design Alliance. It's a while since I looked at it myself, but they had a load of C libraries for dealing with various AutoCAD files.



回答6:

Here is a link to a CodeProject dxf reader; it seems very limited (and not particularly well done) though.



回答7:

Here is another open source dxf reader, in Java. Buggy however!



回答8:

I've written some C# 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]
                    }
                }
            }
    
        }
    }