All Resources
Code SnippetAutomation2026-03-04
Parse DXF Files in C#
Read and extract geometry entities (lines, arcs, circles) from AutoCAD DXF files using C#. Essential for nesting, laser cutting, and CNC manufacturing workflows.
csharp
// Using the netDxf library (NuGet: netDxf)
using netDxf;
using netDxf.Entities;
public List<EntityInfo> ParseDxfFile(string filePath)
{
var doc = DxfDocument.Load(filePath);
var results = new List<EntityInfo>();
foreach (var line in doc.Entities.Lines)
{
results.Add(new EntityInfo
{
Type = "Line",
Layer = line.Layer.Name,
StartX = line.StartPoint.X, StartY = line.StartPoint.Y,
EndX = line.EndPoint.X, EndY = line.EndPoint.Y
});
}
foreach (var arc in doc.Entities.Arcs)
{
results.Add(new EntityInfo
{
Type = "Arc",
Layer = arc.Layer.Name,
CenterX = arc.Center.X, CenterY = arc.Center.Y,
Radius = arc.Radius
});
}
foreach (var circle in doc.Entities.Circles)
{
results.Add(new EntityInfo
{
Type = "Circle",
Layer = circle.Layer.Name,
CenterX = circle.Center.X, CenterY = circle.Center.Y,
Radius = circle.Radius
});
}
return results;
}How It Works
DXF (Drawing Exchange Format) is the universal interchange format for 2D CAD data. This snippet uses the netDxf NuGet package to parse a DXF file into typed entity objects:
- Load the document — netDxf handles both ASCII and binary DXF formats
- Iterate entity collections — Lines, Arcs, Circles, LwPolylines, etc. are accessed separately
- Extract geometry data — coordinates, radii, layer names, and other properties
DXF Format Structure
A DXF file is organized into sections:
- HEADER: Drawing settings (units, limits, variables)
- TABLES: Layer definitions, line types, text styles
- BLOCKS: Reusable block definitions (like symbols or standard parts)
- ENTITIES: The actual geometry — lines, arcs, polylines, text, dimensions
Each entity is encoded as group code/value pairs. For example, a LINE has group code 10/20 for start point X/Y and 11/21 for end point X/Y.
Common Use Cases
- Nesting software: Read DXF profiles to arrange parts on sheet metal
- Laser/plasma cutting: Extract contours for toolpath generation
- Quality inspection: Compare DXF design geometry against measured dimensions
- File conversion: Transform DXF geometry into other formats (SVG, G-code, custom XML)
Tips
- Install via NuGet:
dotnet add package netDxf - Always check the Layer property — manufacturing DXFs often separate cut lines, bend lines, and text on different layers
- LwPolyline (lightweight polyline) is the most common entity in manufactured part DXFs — it represents a continuous chain of line/arc segments
- For simple parsing without a library, DXF is plain text — you can parse group codes directly with a state machine
DXFC#CADFile ParsingCNC