Stability | experimental |
---|---|
Maintainer | mwm@mired.org |
Safe Haskell | Safe-Inferred |
The Graphics.OpenSCAD module provides abstract data types for creating OpenSCAD model definitions calls, along with a function to render it as a string, and some utilities. The primary goal is that the output should always be valid OpenSCAD. If you manage to generate OpenSCAD source that causes OpenSCAD to complain, please open an issue.
Standard usage is to have a main
function that looks like:
main = draw $ Solid
and then set your IDE's compile command to use runhaskell
or
equivalent to run your code and send the output to a .scad file. Open
that file in OpenSCAD, and set it to automatically reload if the file
changes. Recompiling your program will cause the model to be loaded
and displayed by OpenSCAD.
The type constructors are generally not exported, with functions being
exported in their stead. This allows extra checking to be done on
those that need it. It also provides consistency, as otherwise you'd
have to remember whether box
is a constructor or a convenience
function, etc.
Because of this, the constructors are not documented, the exported
functions are. The documentation is generally just the corresponding
OpenSCAD function name, along with the names of the arguments from the
OpenSCAD documentation. If no OpenSCAD function name is given, then
it's the same as the OpenSCAD
function. You should check
the OpenSCAD documentation for usage information.
- data Solid
- data Shape
- data Facet
- type Vector = (Float, Float, Float)
- type Point = (Float, Float)
- render :: Solid -> String
- draw :: Solid -> IO ()
- sphere :: Float -> Facet -> Solid
- box :: Float -> Float -> Float -> Solid
- cube :: Float -> Solid
- cylinder :: Float -> Float -> Facet -> Solid
- obCylinder :: Float -> Float -> Float -> Facet -> Solid
- rectangle3d :: Float -> Float -> Solid
- square3d :: Float -> Solid
- circle3d :: Float -> Facet -> Solid
- import3d :: FilePath -> Solid
- linearExtrude :: Float -> Float -> Point -> Int -> Int -> Facet -> Shape -> Solid
- rotateExtrude :: Int -> Facet -> Shape -> Solid
- rectangle :: Float -> Float -> Shape
- square :: Float -> Shape
- circle :: Float -> Facet -> Shape
- import2d :: FilePath -> Shape
- projection :: Bool -> Solid -> Shape
- union :: [Solid] -> Solid
- intersection :: [Solid] -> Solid
- difference :: Solid -> Solid -> Solid
- minkowski :: [Solid] -> Solid
- hull :: [Solid] -> Solid
- scale :: Vector -> Solid -> Solid
- resize :: Vector -> Solid -> Solid
- rotate :: Vector -> Solid -> Solid
- translate :: Vector -> Solid -> Solid
- mirror :: Vector -> Solid -> Solid
- multMatrix :: Transform -> Solid -> Solid
- color :: Colour Float -> Solid -> Solid
- transparent :: AlphaColour Float -> Solid -> Solid
- up :: Float -> Solid -> Solid
- projection3d :: Bool -> Solid -> Solid
- scale2d :: Point -> Shape -> Shape
- resize2d :: Point -> Shape -> Shape
- rotate2d :: Point -> Shape -> Shape
- translate2d :: Point -> Shape -> Shape
- mirror2d :: Point -> Shape -> Shape
- diam :: Float -> Float
- var :: Facet -> [Solid] -> Solid
- fn :: Int -> Facet
- fs :: Float -> Facet
- fa :: Float -> Facet
- def :: Facet
Basic data types
A Solid
is a solid object in OpenSCAD. Since we don't have
optional or named objects, some constructors appear twice to allow
two different variants to be used. And of course, they all have all
their arguments.
A Shape
is a two-dimensional object. They are a separate type
so that Haskell can type check that we aren't using a 2d operation
on a 3d shape, or vice versa. Unfortunately, this means the
dynamically typed functions that accept either - and possibly
generate either - need to have two versions of those functions. I
believe the 2d creation functions are more common for 2d objects,
but the 3d transformation functions are more common. Hence the 2d
creation functions (which have the names of 2d objects like circle,
square, etc.) that create Solid
s have 3d
appended, but the 3d
version of transformations that have both 2d and 3d versions have
3d
appended.
Type aliases to save typing
type Vector = (Float, Float, Float)Source
Vector
is used where OpenSCAD expects an OpenSCAD vector
of length 3.
type Point = (Float, Float)Source
Point
is used where OpenSCAD expects an OpenSCAD vector
of length 3.
Rendering functions
Constructors
Solid
s
A convenience function for creating a cube as a box
with all
sides the same length.
cylinder :: Float -> Float -> Facet -> SolidSource
Create a cylinder with cylinder radius height 'Facet'
.
obCylinder :: Float -> Float -> Float -> Facet -> SolidSource
Create an oblique cylinder with cylinder /radius1 height radius2
Facet
/
rectangle3d :: Float -> Float -> SolidSource
Create a rectangular Solid
with rectangle x-size y-size
.
import3d :: FilePath -> SolidSource
__UNTESTED__ import3d
is import filename
, where filename
is an stl file. It's 3d because import is a key word.
:: Float | height |
-> Float | twist |
-> Point | scale |
-> Int | slices |
-> Int | convexity |
-> Facet | |
-> Shape | to extrude |
-> Solid |
Extrude a Shape
along a line with linear_extrude
.
rotateExtrude :: Int -> Facet -> Shape -> SolidSource
Rotate a Shape
around the origin with rotate_extrude
convexity 'Facet' 'Shape'
Shape
s
import2d :: FilePath -> ShapeSource
__UNTESTED__ import2d
is import filename
, where filename
is an image or other 2d object.
Combinations of Solid
s
intersection :: [Solid] -> SolidSource
Create the intersection of a list of Solid
s.
Transformations
Solid
s
rotate :: Vector -> Solid -> SolidSource
Rotate a Solid
by different amounts around each of the three axis.
multMatrix :: Transform -> Solid -> SolidSource
Transform a Solid
with a Transform
matrix.
transparent :: AlphaColour Float -> Solid -> SolidSource
Render a Solid
in a transparent color. This uses the
AphaColour
color model.
projection3d :: Bool -> Solid -> SolidSource
Shape
s
translate2d :: Point -> Shape -> ShapeSource
translate2d
is translate
for Shape
s.