103 online 3 members  + Join RP Base  +
FreeScript

dxf

by yofu  ·  version 2026.11.14  ·  updated 3 days ago

No reviews yet

Downloads
0
Reviews
0
Size
453 KB
Code check
Clean
Licence
MIT
Released
Nov 2026

dxf for golang

dxf
dxf
DXF (Drawing Exchange Format by Autodesk, Inc.) library for Go.

  • Writes drawings in the structure of AutoCAD 2000 (AC1015), as ASCII or
binary DXF.
  • Reads ASCII and binary DXF files of AutoCAD R12 and later. Data this
package does not interpret is kept, and handles are kept, when a drawing
read is written again.
  • Entities: LINE, XLINE, RAY, CIRCLE, ARC, ELLIPSE, POINT, TEXT, MTEXT,
SHAPE, 3DFACE, SOLID, TRACE, LWPOLYLINE, POLYLINE (2D, 3D, meshes), MESH,
SPLINE, HELIX, INSERT, ATTDEF, ATTRIB, HATCH, MPOLYGON, DIMENSION
(also ARC_DIMENSION and LARGE_RADIAL_DIMENSION), LEADER, MULTILEADER,
MLINE, ACAD_TABLE (block and cell texts; the table data is kept), TOLERANCE,
VIEWPORT, IMAGE, WIPEOUT, LIGHT, underlays (PDFUNDERLAY, DWFUNDERLAY,
DGNUNDERLAY) and ACIS entities (3DSOLID, BODY, REGION and surfaces: the
ACIS data as SAT text, the geometry is not interpreted). Other entity
types are kept as read (entity.Unknown), as are sections this package
does not interpret (e.g. ACDSDATA with the ACIS data of AC1027 and later,
THUMBNAILIMAGE).
  • Blocks with attributes, groups, layouts and viewports, extended data,
text in code pages (e.g. Shift_JIS for Japanese), and editing (query,
remove, move, rotate, scale, copy, explode).
  • Files: R12 export, DWG through the ODA File Converter, streaming read,
recovery of damaged files, audit, import between drawings, external
references, proxy graphics of custom entities.
  • Validated with ezdxf (see
tools/ezdxfcheck).
Installation
$ go get github.com/yofu/dxf

Quick start
d := dxf.NewDrawing()
d.AddLayer("WALL", color.Red, dxf.DefaultLineType, true) // and make it current
d.Line(0, 0, 0, 1000, 0, 0)
d.Circle(500, 500, 0, 250)
d.Text("Plan", 0, -100, 0, 50)
d.SetExt() // $EXTMIN, $EXTMAX
if err := d.SaveAs("plan.dxf"); err != nil {
	log.Fatal(err)
}

d2, err := dxf.FromFile("plan.dxf")
if err != nil {
	log.Fatal(err)
}
for _, e := range d2.EntitiesOnLayer("WALL") {
	fmt.Printf("%T\n", e)
}

The examples of this README are in example_test.go.

The torus above is drawn by example/torus.
DrawingEntity properties
Color, linetype and lineweight are BYLAYER by default. New entities created
by Drawing methods are on the current layer (ChangeLayer) and get the
current properties of the header ($CECOLOR, $CELTYPE, $CELWEIGHT).

l, _ := d.Line(0, 0, 0, 100, 0, 0)
l.SetColor(color.Red)      // 62 (SetColorByBlock / SetColorByLayer)
lt, _ := d.LineType("HIDDEN")
l.SetLineType(lt)          // 6 (nil: BYLAYER)
l.SetLineWeight(35)        // 370: 0.35 mm
l.Thickness = 10           // 39
l.SetVisible(false)        // 60
l.SetTrueColor(255, 128, 0) // 420 (AC1018 or later); 62 is set to the nearest color number
l.SetTransparency(0.5)      // 440 (AC1018 or later); also layers: layer.SetTransparency
l.SetXData([][2]string{{"1001", "MYAPP"}, {"1000", "data"}}) // APPID is registered when writing

// linetypes: dashes (> 0), spaces (< 0) and dots (0)
d.AddLineType("DASHED2", "Dashed __ __ __", 0.5, -0.25)
// complex linetypes with text strings and shapes
gas, _ := d.AddComplexLineType("GAS_LINE", "Gas line ----GAS----",
	&table.LineTypeElement{Length: 0.5},
	table.TextElement(-0.2, "GAS", d.CurrentStyle, 0.1, 0, -0.1, -0.05), // scale, rotation (rad), offset
	&table.LineTypeElement{Length: -0.25})
shx := d.AddShapeFile("ltypeshp.shx")
d.AddComplexLineType("TRACKS", "Tracks -|-|-|-",
	table.ShapeElement(0.15, 132, shx, 0.25, 0, 0, 0), &table.LineTypeElement{Length: 0.15})
l.SetLineType(gas)

Blocks and attributes
d.BeginBlock("TITLE", 0, 0, 0) // entities below are added to the block
d.LwPolyline(true, []float64{0, 0}, []float64{180, 0}, []float64{180, 40}, []float64{0, 40})
d.AttributeDefinition("NAME", "Drawing name", "untitled", 10, 15, 0, 5) // tag, prompt, default
d.EndBlock()

ins, _ := d.Insert("TITLE", 230, 10, 0) // attributes are created from the definitions
ins.SetAttribute("NAME", "Floor plan")
ins.Scale = []float64{2, 2, 2}
d.SyncAttributes(ins) // after changing the insertion point, scale or rotation

Other entities
d.XLine([]float64{0, 0, 0}, []float64{1, 1, 0}) // also Ray
d.Trace([][]float64{{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0}})
d.AddShapeFile("ltypeshp.shx")
d.Shape("TRACK1", []float64{5, 5, 0}, 2)
d.Tolerance(`{\Fgdt;j}%%v0.1`, []float64{0, 10, 0}) // feature control frame
d.Helix([]float64{0, 0, 0}, 2, 1, 3, true)            // radius, pitch, turns, ccw
d.Mesh(vertices, [][]int{{0, 1, 2}, {0, 2, 3}})
d.MPolygon(outer, hole) // polygon with holes, solid fill
d.Light("L1", entity.LIGHT_SPOT, []float64{0, 0, 10})
solid, _ := d.Body("3DSOLID", satLines) // ACIS data as SAT text
fmt.Println(solid.SAT)

// multilines: MLINESTYLE "Standard" (added when needed) or styles of your own
d.AddMLineStyle("WALL", entity.MLineStyleElement{Offset: 0.1, Color: 1, LineType: "BYLAYER"},
	entity.MLineStyleElement{Offset: -0.1, Color: 5, LineType: "BYLAYER"})
d.MLine("WALL", [][]float64{{0, 0}, {4, 0}, {4, 4}}, false, 1, entity.MLINE_ZERO) // closed, scale, justification
// multileader with MTEXT content at (10, 10); leaders from their arrow heads
// (the text size is estimated from the number of characters)
d.MultiLeaderText(`Note\Psecond line`, []float64{10, 10, 0}, 2.5, [][]float64{{0, 0, 0}, {5, 8, 0}})
// tables: cell texts and conversion to a block reference
content := table.Content()  // [][]string
d.TableToBlockReference(table)

Text, dimensions, leaders and hatches
```go
mt, _ := d.MText("first line\nsecond line", 0, 0, 0, 2.5, 0) // "\n" -> \P
mt.Attachment = entity.MTEXT_MIDDLE_CENTER
fmt.Println(mt.PlainText()) // without MTEXT formatting codes
mt.SetBackgroundColor(color.Yellow, 1.5) // background fill (AC1018 or later)

…