cad

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2026 License: BSD-2-Clause Imports: 2 Imported by: 0

README

GOCAD GoDoc

A simple library for writing OpenSCAD code in Go.

Supported features

Shapes

  • sphere
  • cube
  • cylinder
  • rounded cube
  • box (hollow)
  • extruded polygon

Transformations

  • translate
  • rotate
  • mirror
  • scale

Boolean operations

  • union
  • difference
  • intersection
  • hull
  • minkowski

Install

run go get github.com/richi0/gocad

Usage

Same as SCAD but with type safety, the ability to chain operations and an easy way to create your own complex shapes.

The following code creates the 3D model below.

package main

import (
	"fmt"
	cad "github.com/richi0/gocad"
)

func main() {
    s := cad.NewSphere(11).Difference(
        cad.NewSphere(3).Translate(0, 0, 8).Scale(1.5, 1.5, 1.5),
        cad.NewSphere(3).Translate(0, 0, -11),
        cad.NewSphere(3).Translate(11, 0, 0),
        cad.NewSphere(3).Translate(-11, 0, 0),
        cad.NewSphere(3).Translate(0, 11, 0),
        cad.NewSphere(3).Translate(0, -11, 0),
    )
    c := cad.NewCube(20, 20, 20).Center().Difference(cad.NewSphere(12), cad.NewCube(20, 20, 20))
    d := cad.NewDocument(s, c)
    fmt.Println(d.Render())
}

example image

Build your own shapes

Creating custom shapes is simple. For example, the box shape that is included in this library. Render is the only method you need to define. The NewBox function is just for convenience.

type Box struct {
	x      float64
	y      float64
	z      float64
	radius float64
	wall   float64
	Shape
}

func (b *Box) Render() string {
	base := NewCube(b.x, b.y, b.z)
	radius := 0.01
	if b.radius != 0 {
		radius = b.radius
	}

	diff := NewRoundedCube(b.x-2*b.wall, b.y-2*b.wall, b.z+radius, radius).Translate(b.wall, b.wall, b.wall)
	res := base.Difference(diff)
	return res.Render()
}

func NewBox(
	x float64,
	y float64,
	z float64,
	radius float64,
	wall float64) *Box {
	b := &Box{x, y, z, radius, wall, CommonMutations{}}
	b.parent = b
	return b
}

Then use it like this.

package main

import (
	"fmt"
	cad "github.com/richi0/gocad"
)

func main() {
	b := cad.NewBox(120, 60, 20, 5, 5)
	d := cad.NewDocument(b)
	fmt.Println(d.Render())
}

example image

Extra example

Created shapes can be reused easily. See the following Go code, SCAD code and the 3D-model

package main

import (
	"fmt"

	cad "github.com/richi0/gocad"
)

func main() {
	x := cad.NewSphere(3).Hull(
		cad.NewCube(10, 10, 10).Center().Translate(20, 0, 0),
		cad.NewSphere(3).Translate(40, 0, 0)).Translate(-20, 0, 0)
	y := x.Rotate(0, 0, 90)
	z := x.Rotate(0, 90, 0)
	d := cad.NewDocument(x.Union(y, z))
	d.Fn = 100
	fmt.Println(d.Render())
}
$fn = 100;
union() {
        translate([-20.000000,0.000000,0.000000]) {
                hull() {
                        sphere(3.000000);
                        translate([20.000000,0.000000,0.000000]) {
                                translate([-5.000000,-5.000000,-5.000000]) {
                                        cube([10.000000, 10.000000, 10.000000]);
                                }
                        }
                        translate([40.000000,0.000000,0.000000]) {
                                sphere(3.000000);
                        }
                }
        }
        rotate([0.000000,0.000000,90.000000]) {
                translate([-20.000000,0.000000,0.000000]) {
                        hull() {
                                sphere(3.000000);
                                translate([20.000000,0.000000,0.000000]) {
                                        translate([-5.000000,-5.000000,-5.000000]) {
                                                cube([10.000000, 10.000000, 10.000000]);
                                        }
                                }
                                translate([40.000000,0.000000,0.000000]) {
                                        sphere(3.000000);
                                }
                        }
                }
        }
        rotate([0.000000,90.000000,0.000000]) {
                translate([-20.000000,0.000000,0.000000]) {
                        hull() {
                                sphere(3.000000);
                                translate([20.000000,0.000000,0.000000]) {
                                        translate([-5.000000,-5.000000,-5.000000]) {
                                                cube([10.000000, 10.000000, 10.000000]);
                                        }
                                }
                                translate([40.000000,0.000000,0.000000]) {
                                        sphere(3.000000);
                                }
                        }
                }
        }
}

example image

Extruded polygon

ExtrudedPolygon takes a list of 2-D points and extrudes them to a given height, producing a linear_extrude()/polygon() solid. Use NewPoint2D to define each vertex and NewExtrudedPolygon to assemble the shape.

package main

import (
	"fmt"

	cad "github.com/richi0/gocad"
)

func main() {
	p := cad.NewExtrudedPolygon(
		10,
		cad.NewPoint2D(0, 0),
		cad.NewPoint2D(20, 0),
		cad.NewPoint2D(10, 20),
	)
	d := cad.NewDocument(p)
	fmt.Println(d.Render())
}

This produces the following OpenSCAD code:

$fn = 50;
linear_extrude(10.000000) {polygon(points=[[0.000000,0.000000],[20.000000,0.000000],[10.000000,20.000000]]);};

Like every other shape, ExtrudedPolygon supports chained transformations and boolean operations:

p := cad.NewExtrudedPolygon(
    10,
    cad.NewPoint2D(0, 0),
    cad.NewPoint2D(20, 0),
    cad.NewPoint2D(10, 20),
).Translate(5, 5, 0).Rotate(0, 0, 45)

Documentation

Overview

Package cad provides a Go API for generating OpenSCAD code. It models CAD primitives (cubes, cylinders, spheres, etc.) and boolean/transform operations as Go types, and renders them to OpenSCAD syntax via a Document.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Box

type Box struct {
	CommonMutations
	// contains filtered or unexported fields
}

Box is a hollow rectangular box with optional rounded corners and a configurable wall thickness.

func NewBox

func NewBox(
	x float64,
	y float64,
	z float64,
	radius float64,
	wall float64) *Box

NewBox creates a Box with outer dimensions x, y, z, corner radius, and wall thickness.

func (*Box) Center

func (b *Box) Center() Object

Center returns the Box translated so its geometric centre is at the origin.

type CommonMutations

type CommonMutations struct {
	// contains filtered or unexported fields
}

CommonMutations is an embeddable struct that implements the transformation and boolean methods of the Object interface, delegating to its parent Object.

func (*CommonMutations) Difference

func (c *CommonMutations) Difference(o ...Object) Object

Difference subtracts the provided objects from this object.

func (*CommonMutations) Hull

func (c *CommonMutations) Hull(o ...Object) Object

Hull returns the convex hull of this object and the provided objects.

func (*CommonMutations) Intersection

func (c *CommonMutations) Intersection(o ...Object) Object

Intersection returns the overlapping volume of this object and the provided objects.

func (*CommonMutations) Minkowski

func (c *CommonMutations) Minkowski(o ...Object) Object

Minkowski returns the Minkowski sum of this object and the provided objects.

func (*CommonMutations) Mirror

func (c *CommonMutations) Mirror(x float64, y float64, z float64) Object

Mirror reflects the object across the plane defined by the normal vector (x, y, z) and returns the resulting Object.

func (*CommonMutations) Rotate

func (c *CommonMutations) Rotate(x float64, y float64, z float64) Object

Rotate rotates the object by (x, y, z) degrees around the respective axes and returns the resulting Object.

func (*CommonMutations) Scale

func (c *CommonMutations) Scale(x float64, y float64, z float64) Object

Scale scales the object by factors (x, y, z) and returns the resulting Object.

func (*CommonMutations) Translate

func (c *CommonMutations) Translate(x float64, y float64, z float64) Object

Translate moves the object by (x, y, z) and returns the resulting Object.

func (*CommonMutations) Union

func (c *CommonMutations) Union(o ...Object) Object

Union combines this object with the provided objects into a merged solid.

type Container

type Container struct {
	CommonMutations
	// contains filtered or unexported fields
}

Container is an internal base type that wraps a set of Objects inside a named OpenSCAD boolean operation block.

type Cube

type Cube struct {
	CommonMutations
	// contains filtered or unexported fields
}

Cube is a rectangular box primitive.

func NewCube

func NewCube(x float64, y float64, z float64) *Cube

NewCube creates a Cube with dimensions x, y, z.

func (*Cube) Center

func (c *Cube) Center() Object

Center returns the Cube translated so that its geometric centre is at the origin.

type Cylinder

type Cylinder struct {
	CommonMutations
	// contains filtered or unexported fields
}

Cylinder is a cylindrical primitive.

func NewCylinder

func NewCylinder(h float64, r float64) *Cylinder

NewCylinder creates a Cylinder with height h and radius r.

func (*Cylinder) Center

func (c *Cylinder) Center() Object

Center returns the Cylinder translated so its midpoint along the Z axis is at the origin.

type Difference

type Difference struct {
	Container
}

Difference subtracts subsequent objects from the first object.

func NewDifference

func NewDifference(o ...Object) *Difference

NewDifference creates a Difference from the provided objects.

type Document

type Document struct {
	Fn int
	// contains filtered or unexported fields
}

Document holds a collection of Objects and global render settings, and produces a complete, formatted OpenSCAD source string.

func NewDocument

func NewDocument(o ...Object) *Document

NewDocument creates a Document containing the provided objects with a default $fn of 50.

func (*Document) Render

func (d *Document) Render() string

Render returns the full OpenSCAD source string for all objects in the document, with proper indentation.

type ExtrudedPolygon

type ExtrudedPolygon struct {
	CommonMutations
	// contains filtered or unexported fields
}

ExtrudedPolygon is a 2-D polygon linearly extruded to height h.

func NewExtrudedPolygon

func NewExtrudedPolygon(h float64, points ...Point2D) *ExtrudedPolygon

NewExtrudedPolygon creates an ExtrudedPolygon with the given height and vertex points.

type Hull

type Hull struct {
	Container
}

Hull produces the convex hull enclosing all child objects.

func NewHull

func NewHull(o ...Object) *Hull

NewHull creates a Hull from the provided objects.

type Intersection

type Intersection struct {
	Container
}

Intersection produces the overlapping volume of all child objects.

func NewIntersection

func NewIntersection(o ...Object) *Intersection

NewIntersection creates an Intersection from the provided objects.

type Minkowski

type Minkowski struct {
	Container
}

Minkowski computes the Minkowski sum of its child objects.

func NewMinkowski

func NewMinkowski(o ...Object) *Minkowski

NewMinkowski creates a Minkowski from the provided objects.

type Mirror

type Mirror struct {
	Transform
}

Mirror represents an OpenSCAD mirror() operation.

func NewMirror

func NewMirror(x float64, y float64, z float64, o ...Object) *Mirror

NewMirror creates a Mirror across the plane with normal (x, y, z).

type Object

type Object interface {
	Translate(x float64, y float64, z float64) Object
	Mirror(x float64, y float64, z float64) Object
	Rotate(x float64, y float64, z float64) Object
	Scale(x float64, y float64, z float64) Object
	Union(o ...Object) Object
	Difference(o ...Object) Object
	Hull(o ...Object) Object
	Intersection(o ...Object) Object
	Minkowski(o ...Object) Object
	// contains filtered or unexported methods
}

Object is the core interface implemented by all CAD shapes and containers. It provides rendering to OpenSCAD syntax and fluent transformation methods.

type Point2D

type Point2D struct {
	// contains filtered or unexported fields
}

Point2D represents a 2-D point used to define polygon vertices.

func NewPoint2D

func NewPoint2D(x float64, y float64) Point2D

NewPoint2D creates a Point2D at (x, y).

type Rotate

type Rotate struct {
	Transform
}

Rotate represents an OpenSCAD rotate() operation.

func NewRotate

func NewRotate(x float64, y float64, z float64, o ...Object) *Rotate

NewRotate creates a Rotate by (x, y, z) degrees.

type RoundedCube

type RoundedCube struct {
	CommonMutations
	// contains filtered or unexported fields
}

RoundedCube is a box primitive with rounded corners defined by a sphere radius.

func NewRoundedCube

func NewRoundedCube(
	x float64,
	y float64,
	z float64,
	radius float64,
) *RoundedCube

NewRoundedCube creates a RoundedCube with dimensions x, y, z and corner radius.

func (*RoundedCube) Center

func (r *RoundedCube) Center() Object

Center returns the RoundedCube translated so its geometric centre is at the origin.

type Scale

type Scale struct {
	Transform
}

Scale represents an OpenSCAD scale() operation.

func NewScale

func NewScale(x float64, y float64, z float64, o ...Object) *Scale

NewScale creates a Scale by factors (x, y, z).

type Sphere

type Sphere struct {
	CommonMutations
	// contains filtered or unexported fields
}

Sphere is a spherical primitive.

func NewSphere

func NewSphere(r float64) *Sphere

NewSphere creates a Sphere with radius r.

type Transform

type Transform struct {
	CommonMutations
	// contains filtered or unexported fields
}

Transform is an internal base type for OpenSCAD spatial transformation operations (translate, mirror, rotate, scale).

type Translate

type Translate struct {
	Transform
}

Translate represents an OpenSCAD translate() operation.

func NewTranslate

func NewTranslate(x float64, y float64, z float64, o ...Object) *Translate

NewTranslate creates a Translate that moves objects by (x, y, z).

type Union

type Union struct {
	Container
}

Union combines its child objects into a single merged solid.

func NewUnion

func NewUnion(o ...Object) *Union

NewUnion creates a Union from the provided objects.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL