• Go 80.1%
  • Lean 19.9%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-12-02 21:10:14 +09:00
.claude/docs move tests to private sketch-test repo (#321) 2026-11-30 16:27:54 +09:00
.github reuse cone arc radius bounds (#325) 2026-12-02 08:35:38 +09:00
_lookup add source lookup index (#324) 2026-12-02 08:31:43 +09:00
_proofs reuse cone arc radius bounds (#325) 2026-12-02 08:35:38 +09:00
docs add source lookup index (#324) 2026-12-02 08:31:43 +09:00
examples make root file names uniform snake_case 2026-11-05 20:49:17 +09:00
geom compact fragment boundary indices (#335) 2026-12-02 21:10:14 +09:00
internal/cmd keep fillet and chamfer legs in place (#237) 2026-10-25 22:57:24 +09:00
param use math.Hypot for the hypot builtin (#207) 2026-10-23 22:03:09 +09:00
sketchtest refactor: share boundary area calculation (#290) 2026-11-16 08:19:04 +09:00
.gitattributes add github actions ci pipeline (#15) 2026-07-03 11:22:46 +09:00
.gitignore move tests to private sketch-test repo (#321) 2026-11-30 16:27:54 +09:00
.golangci.yml bump go to 1.27.3 (#228) 2026-10-25 22:12:21 +09:00
.goreleaser.yaml add release workflow (#32) 2026-07-07 10:30:21 +09:00
AGENTS.md direct agents to lookup index (#326) 2026-12-02 08:53:40 +09:00
annotate.go make root file names uniform snake_case 2026-11-05 20:49:17 +09:00
attachment.go add revision-bound sketch attachments (#177) 2026-10-13 17:16:52 +09:00
chains.go publish open boundary chains beside profiles (#123) 2026-09-21 03:00:15 +09:00
CLAUDE.md direct agents to lookup index (#326) 2026-12-02 08:53:40 +09:00
compound.go offset chains of lines and arcs (#250) 2026-10-26 00:31:53 +09:00
conditioning.go reduce verify analysis cost (#275) 2026-11-06 13:18:36 +09:00
conditioning_banded.go trim rank elimination to the pivot band (#307) 2026-11-21 17:17:23 +09:00
cone_fit_crossing.go bound cone crossing queries with integer Bernstein (#306) 2026-11-21 16:59:07 +09:00
cone_line.go add Lean proofs for cone-side certification (#297) 2026-11-21 12:46:10 +09:00
cone_side.go speed up cone arc proofs with a float prefilter (#318) 2026-11-21 21:16:47 +09:00
cone_side_arc.go reuse cone arc radius bounds (#325) 2026-12-02 08:35:38 +09:00
cone_side_float.go speed up cone arc proofs with a float prefilter (#318) 2026-11-21 21:16:47 +09:00
constraint.go prove angle and distance enclosure zero sets (#293) 2026-11-21 08:12:22 +09:00
constraint_conic.go reorganize root package files by responsibility (#272) 2026-11-05 20:01:04 +09:00
constraint_dimension.go prove angle and distance enclosure zero sets (#293) 2026-11-21 08:12:22 +09:00
constraint_spline.go reorganize root package files by responsibility (#272) 2026-11-05 20:01:04 +09:00
constraint_tangent.go reorganize root package files by responsibility (#272) 2026-11-05 20:01:04 +09:00
diagnose.go make root file names uniform snake_case 2026-11-05 20:49:17 +09:00
doc.go give every sketch an origin point (#50) 2026-07-31 19:00:13 +09:00
dxf.go perf: preallocate export builders (#284) 2026-11-06 15:00:24 +09:00
enclose.go solve enclosure piece centres to 1e-6 (#313) 2026-11-21 19:18:08 +09:00
enclose_fold.go reuse Enclose solver plan and cheapen multiplies (#256) 2026-11-03 18:42:02 +09:00
enclose_system.go sum Krawczyk products with one rounding bound (#315) 2026-11-21 20:00:43 +09:00
export_capacity.go perf: preallocate export builders (#284) 2026-11-06 15:00:24 +09:00
frame.go refuse to export non-finite geometry (#88) 2026-08-04 21:49:40 +09:00
generate.go slim readme into splash page with docs/guide (#14) 2026-07-03 09:26:11 +09:00
go.mod update r3 dependency (#334) 2026-12-02 15:50:11 +09:00
go.sum update r3 dependency (#334) 2026-12-02 15:50:11 +09:00
interval.go inline the outward rounding steps (#302) 2026-11-21 15:15:23 +09:00
interval_trig.go sum small-angle sine series in two words (#314) 2026-11-21 19:35:37 +09:00
introspect.go guard the tangent-conics kind case against a typed nil (#86) 2026-08-04 19:03:23 +09:00
jacobian.go prove local Jacobian matches dense (#317) 2026-11-21 20:31:27 +09:00
json.go format JSON foreign-ref errors lazily (#311) 2026-11-21 17:18:23 +09:00
json_constraint.go reorganize root package files by responsibility (#272) 2026-11-05 20:01:04 +09:00
json_decode.go reorganize root package files by responsibility (#272) 2026-11-05 20:01:04 +09:00
json_world.go reject foreign handles at marshal (#53) 2026-07-31 21:43:30 +09:00
labels.go size every arrowhead off the drawn leader (#119) 2026-09-13 21:24:53 +09:00
LICENSE Initial commit 2026-06-09 08:36:03 +09:00
loops.go speed up Containments and arc target tests (#203) 2026-10-23 01:23:08 +09:00
names.go add constraint introspection and naming api (#13) 2026-07-03 08:19:45 +09:00
non_finite.go speed up the LM linear solve (#312) 2026-11-21 17:18:28 +09:00
nurbs.go fix CreateNURBS validation doc claims (#96) 2026-08-05 10:37:41 +09:00
parameters.go screen CheckConstraint and AddConstraint against a corrupt constraint candidate (#87) 2026-08-04 19:35:29 +09:00
plane.go extract units package into the units module (#38) 2026-07-12 22:15:31 +09:00
png.go draw point and entity names in svg (#114) 2026-09-13 16:37:32 +09:00
probe.go prove angle and distance enclosure zero sets (#293) 2026-11-21 08:12:22 +09:00
profile_junction_line_arc_quarter.go reorganize root package files by responsibility (#272) 2026-11-05 20:01:04 +09:00
profile_junction_line_circle.go reorganize root package files by responsibility (#272) 2026-11-05 20:01:04 +09:00
profile_junction_line_line.go prove exact line crossing in Lean (#316) 2026-11-21 20:20:29 +09:00
profile_union.go certify full gear trims and join profiles (#164) 2026-10-10 21:30:09 +09:00
profiles.go size profile outer boundaries once (#333) 2026-12-02 15:13:13 +09:00
profiles_bounded.go reorganize root package files by responsibility (#272) 2026-11-05 20:01:04 +09:00
README.md add certified enclosure of a solved configuration (#154) 2026-10-07 08:53:43 +09:00
reference.go reduce verification reference allocations (#278) 2026-11-06 13:53:52 +09:00
removal.go reduce residual plan allocations (#261) 2026-11-03 21:51:48 +09:00
revision.go hash revision without fmt or maps (#309) 2026-11-21 17:18:08 +09:00
scene_builder.go add bounded sketch scene construction (#255) 2026-11-02 00:11:14 +09:00
SECURITY.md add security policy and untrusted-input timeout example (#30) 2026-07-04 09:07:29 +09:00
sketch.go reduce large solve allocations (#260) 2026-11-03 21:39:45 +09:00
solver.go prove local Jacobian matches dense (#317) 2026-11-21 20:31:27 +09:00
solver_normal.go skip sparse jacobian transposition (#279) 2026-11-06 13:53:56 +09:00
solver_rank.go trim rank elimination to the pivot band (#307) 2026-11-21 17:17:23 +09:00
spline.go reuse fit evaluators while coordinates agree (#251) 2026-10-28 00:32:13 +09:00
svg.go write SVG elements without fmt (#310) 2026-11-21 17:18:12 +09:00
tools.go reorganize root package files by responsibility (#272) 2026-11-05 20:01:04 +09:00
tools_corner.go fix fillet leg-order stray arc (#288) 2026-11-14 15:29:00 +09:00
tools_curve.go reorganize root package files by responsibility (#272) 2026-11-05 20:01:04 +09:00
tools_offset.go reorganize root package files by responsibility (#272) 2026-11-05 20:01:04 +09:00
tools_replicate.go reorganize root package files by responsibility (#272) 2026-11-05 20:01:04 +09:00
verify.go reduce verify analysis cost (#275) 2026-11-06 13:18:36 +09:00
world.go detect a profile that has gone stale (#40) 2026-07-13 07:26:23 +09:00

sketch

DeepWiki

The sketch wordmark and tagline appear from left to right over a CAD grid, then clear and repeat

A standalone, fully programmable headless parametric 2D sketch engine for Go, in the spirit of the sketch environment in Autodesk Fusion.

You build geometry — points, lines, circles, arcs, ellipses, splines — in code, tie it together with geometric and dimensional constraints, and a numerical solver moves the geometry so that every constraint is satisfied at once. Because dimensions are ordinary editable values, sketches are fully parametric: change a dimension, re-solve, and the geometry updates.

Why this exists

This engine is built to be driven by an AI agent as a verification step before it acts on real CAD software. Constraint sketching is easy to get subtly wrong — an under- or over-constrained profile, a configuration that can flip, a dimension that doesn't resolve as intended.

Rather than discover that inside Fusion after committing to an operation, an agent can reproduce the sketch here first and check it programmatically: Does it fully constrain (DOF == 0)? Are any constraints redundant or conflicting? Does it admit more than one valid configuration?

Only once the sketch is proven sound does the agent carry the plan into the CAD package — and the SVG/PNG exporters let an agent or human eyeball the result along the way.

Features

  • Levenberg–Marquardt constraint solver with degrees-of-freedom and redundancy analysis.
  • A rich, Fusion-like constraint set — coincidence, tangency, parallel, perpendicular, symmetry, equality and more — plus dimensional constraints that are editable and fully parametric.
  • Sketch-modification tools — trim/extend/break, fillet/chamfer, mirror, rectangular/circular patterns, and offset — on committed geometry.
  • Verification diagnostics — redundant/conflicting constraint detection, free-DOF attribution, over-constraint rejection, and a multi-solution ambiguity probe.
  • Certified solve — Sketch.Enclose returns outward-rounded intervals that contain the exact solution of a point-and-line sketch, proves the solution is the only one in its box, and follows it across a range of a driving dimension, or refuses with a named cause.
  • Units of measure and expression-driven dimensions — typed units and a parameter/expression engine, so a single parameter can drive a whole sketch.
  • Profile (closed-region) detection with exact areas and hole nesting.
  • Export to SVG and PNG — optionally annotated with dimensions, constraint glyphs, degree-of-freedom coloring, conflict highlighting and a verification badge (see the Gallery) — plus DXF R12 (CAD interchange) and JSON (lossless save / load round-trip).
  • Pure Go, minimal dependencies. The production runtime depends only on the standard library plus github.com/lestrrat-go/option/v3 (functional options), github.com/lestrrat-3d/r3 (3D coordinate math) and github.com/lestrrat-3d/units (units of measure); the geom subpackage is standard-library-only and param adds only units, so both stay independently extractable.
import "github.com/lestrrat-3d/sketch"

The exporters render annotated SVG, so you can see a sketch's constraints and — the point of the engine — its verification state, not just its geometry. Every image below is generated from a compiled builder by internal/cmd/genimages and kept in sync by a test. Pass the With… options (WithDimensions, WithConstraints, WithDOFColoring, WithConflicts, WithStatusBadge, WithProfileFill, WithLabels) to SVG/PNG; all default off. WithLabels writes the optional names points and entities carry onto the drawing — a point's upright, an entity's italic — and searches for a spot for each one that is clear of the geometry and of the other names. Where position alone cannot say which vertex a name belongs to, the name is underlined and carries an arrow to its own point, so a sketch reasoned about as "A to B" says which point is which. The gallery also renders "windowed" — WithFrame draws a border and WithGrid lays a coordinate grid behind the sketch; a framed render always carries a small github.com/lestrrat-3d/sketch provenance footer.

The masthead GIF uses the same Sketch-generated geometry as banner.svg. Regenerate it with go run ./internal/cmd/genimages banner-gif docs/images/banner.gif. This command requires ffmpeg with SVG decoding.

Constraints and dimensions — geometric-constraint glyphs (H/V = horizontal / vertical, ∥ = parallel, ⊥ = perpendicular) and CAD-style dimensions with arrowheads and units:

A grounded rectangle with horizontal, vertical, parallel and perpendicular constraint glyphs on its sides and width and height dimension lines labelled in millimetres

Degrees of freedom — the headline verification cue. Geometry that is not fully constrained is blue and hollow; fully constrained geometry is black; the grounded anchor point is a green square, so the sketch's tie to the origin is visible. A status badge reports the DOF count and constraint state:

Under-constrained (a free corner) Fully constrained
A rectangle whose top edge and corners are blue and hollow because they still have free degrees of freedom, with the grounded origin corner shown as a green square, and a badge reading DOF 3, underconstrained The same rectangle fully constrained: every edge black and every corner a black dot except the grounded origin corner, which is a green square, with a badge reading DOF 0, fully constrained

Conflicting constraints — the same right triangle, dimensioned two ways. Its legs are 400 and 300, so its hypotenuse is 500. Dimension the hypotenuse 500 and everything agrees; dimension it 600 and the constraints can no longer all hold — the over-specified dimension is flagged in red and the badge reports the sketch is unsolvable:

Consistent (solves) Conflicting (unsolvable)
A 400 by 300 right triangle with its hypotenuse dimensioned 500 mm, every edge black because the sketch is fully constrained, with a badge reading DOF 0, fully constrained, solvable true The same 400 by 300 right triangle but with its hypotenuse dimensioned 600 mm and drawn in red because that length conflicts with the two legs, with a badge reading DOF 0, overconstrained, solvable false

Parametric — dimensions are editable values; change one and re-solve and everything driven by it follows:

width = 120 mm width = 200 mm
A plate with a centered circular hole dimensioned at width 120 millimetres The same plate re-solved at width 200 millimetres, larger, with the hole still centered and proportional

Modification tools — trim, fillet, chamfer, mirror, pattern and offset run on committed geometry. A fillet, for example, replaces a sharp corner with a tangent arc:

Before After (corner filleted)
A plain rectangle with sharp corners The same rectangle with one corner rounded off by a tangent fillet arc

Regular polygon — a hexagon whose equal-length side and equal-spoke constraints (the = glyphs) hold its regularity, then grounded to DOF 0: a fixed center pins position, a horizontal construction diagonal pins rotation, and a circumradius dimension pins size. Every edge is black (fully constrained) and the badge confirms it:

A regular hexagon rendered fully constrained: black edges, equal-length glyphs on the sides, equal construction spokes, a dashed horizontal diagonal carrying an H constraint glyph, and a badge reading DOF 0, fully constrained

Quick start

You author geometry directly on the sketch from points: s.CreatePoint(x, y) returns a solver-bound *sketch.Point, and the curve builders (s.CreateLine/CreateCircle/CreateArc/CreateEllipse/CreateSpline) take those points. Topology is expressed by sharing a point — the corner where two lines meet is literally one *Point. Constrain the geometry, solve, edit a dimension, and re-solve.

package examples_test

import (
  "context"
  "fmt"

  "github.com/lestrrat-3d/sketch"
)

// Example_sketch_quickstart builds an axis-aligned rectangle entirely from
// constraints, edits one dimension and re-solves, then exports the result. It
// is the smallest end-to-end taste of the engine: author geometry from points,
// constrain it, solve, edit, re-solve, export.
func Example_sketch_quickstart() {
  w := sketch.NewWorld()
  s, _ := w.CreateSketch(w.XY())

  // Four corners as rough initial guesses; the solver finds the exact spots.
  // Sharing a *Point between two lines is what makes a corner a corner.
  a := s.CreatePoint(0, 0)
  b := s.CreatePoint(18, 2)
  c := s.CreatePoint(17, 11)
  d := s.CreatePoint(1, 13)

  ab := s.CreateLine(a, b)
  bc := s.CreateLine(b, c)
  dc := s.CreateLine(d, c)
  ad := s.CreateLine(a, d)

  // Ground one corner at the origin so the sketch can't float away.
  a.MoveTo(0, 0)
  s.Fix(a)

  // Axis-align the four sides.
  s.AddConstraint(
    sketch.NewHorizontal(ab),
    sketch.NewHorizontal(dc),
    sketch.NewVertical(ad),
    sketch.NewVertical(bc),
  )

  // Driving dimensions: editable values that make the sketch parametric.
  width := sketch.NewDistance(a, b, 20)
  height := sketch.NewDistance(a, d, 12)
  s.AddConstraint(width, height)

  res, err := s.Solve(context.Background())
  if err != nil {
    fmt.Printf("failed to solve: %s\n", err)
    return
  }
  fmt.Printf("DOF=%d b=(%.0f,%.0f) c=(%.0f,%.0f) d=(%.0f,%.0f)\n",
    res.DOF, b.X(), b.Y(), c.X(), c.Y(), d.X(), d.Y())

  // Edit a dimension and re-solve: the rectangle becomes 35 x 12.
  width.Set(35)
  if _, err := s.Solve(context.Background()); err != nil {
    fmt.Printf("failed to re-solve: %s\n", err)
    return
  }
  fmt.Printf("after width.Set(35): b=(%.0f,%.0f) c=(%.0f,%.0f)\n",
    b.X(), b.Y(), c.X(), c.Y())

  // Export the solved sketch in several formats.
  svg, err := s.SVG()
  if err != nil {
    fmt.Printf("failed to render SVG: %s\n", err)
    return
  }
  dxf, err := s.DXF()
  if err != nil {
    fmt.Printf("failed to render DXF: %s\n", err)
    return
  }
  data, err := s.MarshalJSON()
  if err != nil {
    fmt.Printf("failed to marshal JSON: %s\n", err)
    return
  }
  fmt.Printf("exports non-empty: svg=%t dxf=%t json=%t\n", len(svg) > 0, len(dxf) > 0, len(data) > 0)

  // Output:
  // DOF=0 b=(20,0) c=(20,12) d=(0,12)
  // after width.Set(35): b=(35,0) c=(35,12)
  // exports non-empty: svg=true dxf=true json=true
}

source: examples/sketch_readme_example_test.go

The code blocks in this README are embedded from compiled, go test-verified examples. For more worked programs — a constraint-built hexagon, a parametric plate, a parametric fillet, an ambiguity probe — browse the examples package.

Documentation

Task-focused guides live in docs/guide:

  • Geometry — builders, grounding, construction geometry, compound shapes, the geom shaping toolkit, and removal.
  • Constraints — the geometric and dimensional constraint set, sign conventions, driven dimensions, and introspection/naming.
  • Units & parameters — typed units and the expression engine that lets one parameter drive a whole sketch.
  • Profiles — closed-region detection with exact areas and hole nesting.
  • Solving & goals — running the solver, reading its report, verification, interactive drag goals, and how the solver works.

The full API reference is on pkg.go.dev. Design notes and engine internals are in docs.

License

This project is source-available, and is licensed under the PolyForm Noncommercial License 1.0.0.

  • Noncommercial use is free. Individuals, hobby and personal projects, research, education, nonprofits, and government may use, modify, and redistribute it at no cost, subject to the license terms.
  • Commercial / business use requires a separate license. Any use by or for a business, or for commercial advantage, is not permitted under the noncommercial license. To obtain a commercial license, reach out on Bluesky at @lestrrat.bsky.social.

Contributions

This repository does not accept external pull requests.