Giter VIP home page Giter VIP logo

ghostscad's Introduction

GhostSCAD

GhostSCAD is a piece of software that makes it easy to create CAD models using Go and compile them to the OpenSCAD language. It allows you to use the full power of a real programming language and use the rendering capabilities of OpenSCAD.

It requires GO 1.17.

Screenshot

See the blog post explaining the motivations here and a real-life example here.

Example

GhostSCAD aims to minimize the boilerplate but, since it uses a general purpose programming language, there is still some. First, you need to create a Go module for your project and add GhostSCAD to it:

mkdir design; cd design
go mod init example.com/design
go get github.com/ljanyst/ghostscad

Here's an example rendering a sphere of radius 10 and producing the appropriate OpenSCAD model in the main.scad file:

package main

import (
	. "github.com/ljanyst/ghostscad/primitive"
	"github.com/ljanyst/ghostscad/sys"
)

func main() {
	sys.RenderOne(Sphere(10))
}

You can generate such a minimal program by running:

go run github.com/ljanyst/ghostscad/util/stub_generator -file-name blah.go

Here's a slightly more elaborate example:

package main

import (
	"github.com/ljanyst/ghostscad/lib/shapes"
	"github.com/ljanyst/ghostscad/sys"

	. "github.com/ljanyst/ghostscad/primitive"
)

func main() {
	sector := shapes.NewSector(20, 45, 135).SetFn(72).Build()
	arc := shapes.NewArc(25, 45, 290).SetWidth(2).SetFn(72).Build()
	sys.RenderMultiple([]sys.Shape{
		{"sector", sector, sys.None},
		{"arc", arc, sys.None},
		{"sector-and-arc", NewList(sector, arc), sys.Default},
	})
}

It imports the complex shape library provided with GhostSCAD and renders multiple shapes. The parameter to sys.RenderMultiple is a slice of Shape object. The default shape is the one being rendered to sector-and-arc.scad unless the program is instructed otherwise via commandline parameters. You can list the available shapes:

]==> go build
]==> ./sector-and-arc -list-shapes
sector
arc
sector-and-arc (default)

You can render the one you like:

]==> ./sector-and-arc -shape arc
]==> ls arc.scad
arc.scad

You can also generate the STL file directly (if it were a 3D shape) by supplying the -stl parameter. Use the familiar -help to see all the available parameters.

There's a whole bunch of other examples in this code repository. The author leaves it as an exercise to the reader to find them. :)

Rendering Automation

One of the cool features of OpenSCAD is automatic re-rendering triggered by changes to the underlying SCAD files. Since GhostSCAD leverages the Go programming language, it also makes sense to use it in a fully-fledged code editor. Here's an Elisp function to compile and run the current go buffer in Emacs:

(defun go-run-this-file ()
  "go run"
  (interactive)
  (save-buffer)
  (shell-command (format "go run %s" (buffer-file-name))))

You can then bind it to a key combination in your go-mode hook like this:

(local-set-key (kbd "C-c C-r") 'go-run-this-file)

After you set this up, hitting "CTRL-C CTRL-R" compbination runs the code in the current go buffer and overwrites the main.scad file with the most recent version of your model. If you have OpenSCAD rendering this file, it will redraw your model automagically.

Happy hacking!

ghostscad's People

Contributors

ljanyst avatar

Watchers

Rohan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.