A math-inspired CAD program in haskell. CSG, bevels, and shells; 2D & 3D; gcode generation...
ImplicitCAD is a programmatic CAD program, implemented in haskell. Unlike traditional CAD programs, programmatic CAD programs use text descriptions of objects, as in programming. Concepts like variables, control structures and abstraction are used, just as in programming. This provides a number of advantages:
The traditional example of programmatic CAD is OpenSCAD.
Generally, objects in programmatic CAD are built with Constructive Solid Geometry or CSG. Unions, intersections and differences of simpler shapes slowly build the object. ImplicitCAD supports all this and much more! For example, it provides rounded unions so that one can have smooth interfaces between objects.
It also directly provides some GCode generation, and has a parser for OpenSCAD to make it easier for people to transition/use.
ImplicitCAD is very much a work in progress. The author considers it ready for beta testers and greatly appreciates bug reports.
Let's begin with OpenSCAD examples, since they're likely a more comfortable format than Haskell for most readers :)
ImplicitCAD supports a modified version of the OpenSCAD language, used by the popular programmatic CAD tool of the same name.
Generally, normal OpenSCAD code should work. For example, save the following as
example1.scad(or grab it out of the Examples/ directory shipped with ImplicitCAD).
// example1.scad -- The union of a square and a circle. union() { square([80,80]); translate ([80,80]) circle(30); }
Running
extopenscad example1.scadwill produce
example1.svg, which will look like:
You can read more about standard openscad functionality in the OpenSCAD User Manual.
However, there are additional ImplicitCAD specific features. For example a rounded union:
//example2.escad -- A rounded union of a square and a circle. union(r=14) { square([80,80]); translate ([80,80]) circle(30); }
(For code like this that is not backwards compatible with OpenSCAD, it is recommended that you save it as a .escad file -- Extended OpenSCAD.)
Like openscad, ImplicitCAD supports extruding objects.
// example3.escad -- the extruded product of the union of five circles. linear_extrude (height = 40, center=true){ union ( r = 8) { circle (10); translate ([22,0]) circle (10); translate ([0,22]) circle (10); translate ([-22,0]) circle (10); translate ([0,-22]) circle (10); } }
And we allow you to twist them as you extrude.
// example4.escad -- the twisted extruded product of the union of five circles. linear_extrude (height = 40, center=true, twist=90){ union ( r = 8) { circle (10); translate ([22,0]) circle (10); translate ([0,22]) circle (10); translate ([-22,0]) circle (10); translate ([0,-22]) circle (10); } }
In fact, we've extended this to allow you to twist at non-constant rates and even reverse directions. You just make
twista function! (We're following the openscad convention of using degrees...)
// example5.escad -- the variably twisted extruded product of the union of 5 circles. linear_extrude (height = 40, center=true, twist(h) = 35*cos(h*2*pi/60)) { union ( r = 8) { circle (10); translate ([22,0]) circle (10); translate ([0,22]) circle (10); translate ([-22,0]) circle (10); translate ([0,-22]) circle (10); } }
We also allow you to do rounded extrusions. See, we heard you like rounding, so we set this up so you can rounded extrude your rounded union...
// example6.escad -- A rounded extrusion of the rounded union of 5 circles. linear_extrude (height = 40, center=true, r=5){ union ( r = 8) { circle (10); translate ([22,0]) circle (10); translate ([0,22]) circle (10); translate ([-22,0]) circle (10); translate ([0,-22]) circle (10); } }
This is fully compatible with twisting, of course!
// example7.escad -- A twisted rounded extrusion of the rounded union of 5 circles. linear_extrude (height = 40, center=true, twist=90, r=5){ union ( r = 8) { circle (10); translate ([22,0]) circle (10); translate ([0,22]) circle (10); translate ([-22,0]) circle (10); translate ([0,-22]) circle (10); } }
ImplicitCAD also provides full programmatic functionality, like variable assignment in loops. For example, the trivial program:
// Example8.escad -- variable assignment in loops. a = 5; for (c = [1, 2, 3]) { echo(c); a = a*c; echo(a); }
Has the output:
1.0 5.0 2.0 10.0 3.0 30.0 Nothing to render
As a functional programmer, I couldn't resist adding some other niceties to the language. For example, function currying:
// Example9.escad -- function currying. f = max(4); echo(f(5)); echo(max(4,5));
And some higher order functions, like my friend map:
// Example10.escad -- map!. echo(map(cos, [0, pi/2, pi]));
Everything you saw above can be done with the Haskell API. For example, a simple 2D example, the same as our first ExtOpenSCAD one:
-- Example 11 - the union of a square and a circle. import Graphics.Implicitout = union [ squareR 0 True (80, 80) , translate (40, 40) (circle 30) ]
main = writeSVG 2 "test.svg" out
A rounded union:
-- Example 12 - the rounded union of a square and a circle. import Graphics.Implicitout = unionR 14 [ squareR 0 True (80, 80) , translate (80, 80) (circle 30) ]
main = writeSVG 2 "test.svg" out
A simple 3D example:
-- Example 13 - the union of a cube and a sphere. import Graphics.Implicitout = union [ cubeR 0 False (20, 20, 20) , translate (20, 20, 20) (sphere 15) ]
main = writeSTL 1 "test.stl" out
You can do a whole lot more!
apt-get install ghc cabal-install zlib1g-dev
pacman -S ghc cabal-install
yum install ghc cabal-install
brew install ghc cabal-install
cabal update && cabal install implicit
cabal update
git clone https://github.com/colah/ImplicitCAD.git
cd ImplicitCAD/
cabal install --only-dependencies
cabal configure && cabal install
echo "circle(30);" > test.escad
extopenscad test.escad
~/.cabal/bin/extopenscad test.escad-- see bellow.
ghci
import Graphics.Implicit
writeSVG 1 "test.svg" (circle 30)
bash: extopenscad: command not found(or similar for your shell)
~/.cabal/bin/is not in your
$PATHvariable. Try using
~/.cabal/bin/extopenscadas your command instead.
module is not loaded: 'Graphics.Implicit' (./Graphics/Implicit.hs)
~/.cabal/lib/. If that works, you should be able to use ghc anywhere with the
-Ldiror
-lliboptions. Alternatively, some people have permanently fixed this by doing the cabal install as root.
Documentation can be generated from the source code of ImplicitCAD by Haddock by running
cabal haddock.
Releases of ImplicitCAD are uploaded to HackageDB which, in addition to making them avaialable through
cabal install, puts the generated documentation on the Internet. So you can read the documentation for the most recent release of ImplicitCAD, 0.3.0.1, on HackageDB.
In Implicit CAD, we consider objects as functions of `outwardness'. The boundary is 0, negative is the interior and positive the exterior. The magnitude is how far out or in. A description of the mathematical ideas underpinning ImplicitCAD are in a blog post on colah's blog.
ImplicitCAD is very much a work in progress.
What works (January 26th, 2020 -- regressions are possible if not probable):
What still needs to be done:
And a wishlist of things to be done as we go: - More optimisation. - Less bugs. - More coverage of the openscad language. - Pull the hacklab laser cutter support, and place it in HSlice.