Current section
Files
Jump to
Current section
Files
src/gleamscad.erl
-module(gleamscad).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleamscad.gleam").
-export([union/1, difference/1, intersection/1, translate/4, translate_single/4, rotate/4, rotate_single/4, scale/4, scale_single/4, mirror/4, hull/1, minkowski/1, circle/2, square/3, polygon/1, cube/2, cube3/4, sphere/2, cylinder/4, cylinder2/5, custom_code/1, options/1, linear_extrude/6, to_openscad/1]).
-export_type([radius_or_diameter/0, centered_or_not/0, settings/0, open_s_c_a_d_expr/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type radius_or_diameter() :: radius | diameter.
-type centered_or_not() :: centered | not_centered.
-type settings() :: {minimum_angle, float()} |
{minimum_size, float()} |
{number_of_segments, integer()} |
{animation_step, float()}.
-opaque open_s_c_a_d_expr() :: {union, list(open_s_c_a_d_expr())} |
{difference, list(open_s_c_a_d_expr())} |
{intersection, list(open_s_c_a_d_expr())} |
{translate, list(open_s_c_a_d_expr()), {float(), float(), float()}} |
{translate_single, open_s_c_a_d_expr(), {float(), float(), float()}} |
{rotate, list(open_s_c_a_d_expr()), {float(), float(), float()}} |
{rotate_single, open_s_c_a_d_expr(), {float(), float(), float()}} |
{scale, list(open_s_c_a_d_expr()), {float(), float(), float()}} |
{scale_single, open_s_c_a_d_expr(), {float(), float(), float()}} |
{mirror, list(open_s_c_a_d_expr()), {float(), float(), float()}} |
{hull, list(open_s_c_a_d_expr())} |
{minkowski, list(open_s_c_a_d_expr())} |
{circle, float(), radius_or_diameter()} |
{square, float(), float(), centered_or_not()} |
{polygon, list({float(), float()})} |
{cube, {float(), float(), float()}, centered_or_not()} |
{sphere, float(), radius_or_diameter()} |
{cylinder, float(), float(), radius_or_diameter(), centered_or_not()} |
{cylinder2,
float(),
float(),
float(),
radius_or_diameter(),
centered_or_not()} |
{linear_extrude,
list(open_s_c_a_d_expr()),
float(),
centered_or_not(),
float(),
float(),
float()} |
{custom_code, binary()} |
{options, list(settings())}.
-file("src/gleamscad.gleam", 95).
?DOC(" Create a union of shapes\n").
-spec union(list(open_s_c_a_d_expr())) -> open_s_c_a_d_expr().
union(Expressions) ->
{union, Expressions}.
-file("src/gleamscad.gleam", 100).
?DOC(" Create a difference of shapes\n").
-spec difference(list(open_s_c_a_d_expr())) -> open_s_c_a_d_expr().
difference(Expressions) ->
{difference, Expressions}.
-file("src/gleamscad.gleam", 105).
?DOC(" Creates an intersection of shapes\n").
-spec intersection(list(open_s_c_a_d_expr())) -> open_s_c_a_d_expr().
intersection(Expressions) ->
{intersection, Expressions}.
-file("src/gleamscad.gleam", 110).
?DOC(" Translate (move) a shape\n").
-spec translate(list(open_s_c_a_d_expr()), float(), float(), float()) -> open_s_c_a_d_expr().
translate(Expressions, X, Y, Z) ->
{translate, Expressions, {X, Y, Z}}.
-file("src/gleamscad.gleam", 119).
-spec translate_single(open_s_c_a_d_expr(), float(), float(), float()) -> open_s_c_a_d_expr().
translate_single(Expression, X, Y, Z) ->
{translate_single, Expression, {X, Y, Z}}.
-file("src/gleamscad.gleam", 129).
?DOC(" Rotate a shape\n").
-spec rotate(list(open_s_c_a_d_expr()), float(), float(), float()) -> open_s_c_a_d_expr().
rotate(Expressions, X, Y, Z) ->
{rotate, Expressions, {X, Y, Z}}.
-file("src/gleamscad.gleam", 138).
-spec rotate_single(open_s_c_a_d_expr(), float(), float(), float()) -> open_s_c_a_d_expr().
rotate_single(Expression, X, Y, Z) ->
{rotate_single, Expression, {X, Y, Z}}.
-file("src/gleamscad.gleam", 148).
?DOC(" Scales a shape\n").
-spec scale(list(open_s_c_a_d_expr()), float(), float(), float()) -> open_s_c_a_d_expr().
scale(Expressions, X, Y, Z) ->
{scale, Expressions, {X, Y, Z}}.
-file("src/gleamscad.gleam", 157).
-spec scale_single(open_s_c_a_d_expr(), float(), float(), float()) -> open_s_c_a_d_expr().
scale_single(Expression, X, Y, Z) ->
{scale_single, Expression, {X, Y, Z}}.
-file("src/gleamscad.gleam", 167).
?DOC(" Mirrors a shape\n").
-spec mirror(list(open_s_c_a_d_expr()), float(), float(), float()) -> open_s_c_a_d_expr().
mirror(Expressions, X, Y, Z) ->
{mirror, Expressions, {X, Y, Z}}.
-file("src/gleamscad.gleam", 177).
?DOC(" Displays the convex hull of child nodes\n").
-spec hull(list(open_s_c_a_d_expr())) -> open_s_c_a_d_expr().
hull(Expressions) ->
{hull, Expressions}.
-file("src/gleamscad.gleam", 182).
?DOC(" Displays the minkowski sum of child nodes\n").
-spec minkowski(list(open_s_c_a_d_expr())) -> open_s_c_a_d_expr().
minkowski(Expressions) ->
{minkowski, Expressions}.
-file("src/gleamscad.gleam", 187).
?DOC(" Draws a 2d circle\n").
-spec circle(float(), radius_or_diameter()) -> open_s_c_a_d_expr().
circle(Value, Rod) ->
{circle, Value, Rod}.
-file("src/gleamscad.gleam", 192).
?DOC(" Draws a 2d square\n").
-spec square(float(), float(), centered_or_not()) -> open_s_c_a_d_expr().
square(Width, Height, Center) ->
{square, Width, Height, Center}.
-file("src/gleamscad.gleam", 201).
?DOC(" Draws a 2d polygon\n").
-spec polygon(list({float(), float()})) -> open_s_c_a_d_expr().
polygon(Polygons) ->
{polygon, Polygons}.
-file("src/gleamscad.gleam", 206).
?DOC(" Creates a 3d cube with all sides of equal length\n").
-spec cube(float(), centered_or_not()) -> open_s_c_a_d_expr().
cube(Size, Center) ->
{cube, {Size, Size, Size}, Center}.
-file("src/gleamscad.gleam", 211).
?DOC(" Creates a 3d cube\n").
-spec cube3(float(), float(), float(), centered_or_not()) -> open_s_c_a_d_expr().
cube3(Width, Depth, Height, Center) ->
{cube, {Width, Depth, Height}, Center}.
-file("src/gleamscad.gleam", 221).
?DOC(" Creates a 3d sphere\n").
-spec sphere(float(), radius_or_diameter()) -> open_s_c_a_d_expr().
sphere(Value, Rod) ->
{sphere, Value, Rod}.
-file("src/gleamscad.gleam", 226).
?DOC(" Creates a 3d cylinder\n").
-spec cylinder(float(), float(), radius_or_diameter(), centered_or_not()) -> open_s_c_a_d_expr().
cylinder(Height, Value, Rod, Center) ->
{cylinder, Height, Value, Rod, Center}.
-file("src/gleamscad.gleam", 236).
?DOC(" Creates a 3d cylinder. Enables you to pass different diameters/radii for top and bottom\n").
-spec cylinder2(
float(),
float(),
float(),
radius_or_diameter(),
centered_or_not()
) -> open_s_c_a_d_expr().
cylinder2(Height, Value1, Value2, Rod, Center) ->
{cylinder2, Height, Value1, Value2, Rod, Center}.
-file("src/gleamscad.gleam", 247).
?DOC(" Allows for adding custom code\n").
-spec custom_code(binary()) -> open_s_c_a_d_expr().
custom_code(Code) ->
{custom_code, Code}.
-file("src/gleamscad.gleam", 252).
?DOC(" Setting one or several OpenSCAD special variables\n").
-spec options(list(settings())) -> open_s_c_a_d_expr().
options(Values) ->
{options, Values}.
-file("src/gleamscad.gleam", 257).
?DOC(" Turns a 2D shape into a 3D shape\n").
-spec linear_extrude(
list(open_s_c_a_d_expr()),
float(),
centered_or_not(),
float(),
float(),
float()
) -> open_s_c_a_d_expr().
linear_extrude(Expressions, Height, Center, Convexity, Twist, Slices) ->
{linear_extrude, Expressions, Height, Center, Convexity, Twist, Slices}.
-file("src/gleamscad.gleam", 306).
-spec option_to_string(settings()) -> binary().
option_to_string(Option) ->
case Option of
{minimum_angle, Value} ->
_pipe = fmglee:new(<<"$fa=%f;"/utf8>>),
_pipe@1 = fmglee:f(_pipe, Value),
fmglee:build(_pipe@1);
{minimum_size, Value@1} ->
_pipe@2 = fmglee:new(<<"$fs=%f;"/utf8>>),
_pipe@3 = fmglee:f(_pipe@2, Value@1),
fmglee:build(_pipe@3);
{number_of_segments, Value@2} ->
_pipe@4 = fmglee:new(<<"$fn=%d;"/utf8>>),
_pipe@5 = fmglee:d(_pipe@4, Value@2),
fmglee:build(_pipe@5);
{animation_step, Value@3} ->
_pipe@6 = fmglee:new(<<"$t=%f;"/utf8>>),
_pipe@7 = fmglee:f(_pipe@6, Value@3),
fmglee:build(_pipe@7)
end.
-file("src/gleamscad.gleam", 297).
-spec centered_or_not_to_string(centered_or_not()) -> binary().
centered_or_not_to_string(Value) ->
_pipe = case Value of
centered ->
true;
not_centered ->
false
end,
_pipe@1 = gleam@bool:to_string(_pipe),
string:lowercase(_pipe@1).
-file("src/gleamscad.gleam", 283).
-spec build_single_command(
open_s_c_a_d_expr(),
binary(),
{float(), float(), float()}
) -> binary().
build_single_command(Expression, Command, Vector) ->
_pipe = fmglee:new(<<"%s([%f, %f, %f])%s"/utf8>>),
_pipe@1 = fmglee:s(_pipe, Command),
_pipe@2 = fmglee:f(_pipe@1, erlang:element(1, Vector)),
_pipe@3 = fmglee:f(_pipe@2, erlang:element(2, Vector)),
_pipe@4 = fmglee:f(_pipe@3, erlang:element(3, Vector)),
_pipe@6 = fmglee:s(
_pipe@4,
begin
_pipe@5 = Expression,
to_openscad(_pipe@5)
end
),
fmglee:build(_pipe@6).
-file("src/gleamscad.gleam", 328).
?DOC(" Exports the data to a string, containing OpenSCAD code\n").
-spec to_openscad(open_s_c_a_d_expr()) -> binary().
to_openscad(Expr) ->
case Expr of
{union, Expressions} ->
_pipe = fmglee:new(<<"union(){\n%s\n}"/utf8>>),
_pipe@3 = fmglee:s(
_pipe,
begin
_pipe@1 = Expressions,
_pipe@2 = gleam@list:map(_pipe@1, fun to_openscad/1),
gleam@string:join(_pipe@2, <<"\n"/utf8>>)
end
),
fmglee:build(_pipe@3);
{difference, Expressions@1} ->
_pipe@4 = fmglee:new(<<"difference(){\n%s\n}"/utf8>>),
_pipe@7 = fmglee:s(
_pipe@4,
begin
_pipe@5 = Expressions@1,
_pipe@6 = gleam@list:map(_pipe@5, fun to_openscad/1),
gleam@string:join(_pipe@6, <<"\n"/utf8>>)
end
),
fmglee:build(_pipe@7);
{intersection, Expressions@2} ->
_pipe@8 = fmglee:new(<<"intersection(){\n%s\n}"/utf8>>),
_pipe@11 = fmglee:s(
_pipe@8,
begin
_pipe@9 = Expressions@2,
_pipe@10 = gleam@list:map(_pipe@9, fun to_openscad/1),
gleam@string:join(_pipe@10, <<"\n"/utf8>>)
end
),
fmglee:build(_pipe@11);
{translate, Expressions@3, Vector} ->
build_command(Expressions@3, <<"translate"/utf8>>, Vector);
{translate_single, Expression, Vector@1} ->
build_single_command(Expression, <<"translate"/utf8>>, Vector@1);
{rotate, Expressions@4, Angle} ->
build_command(Expressions@4, <<"rotate"/utf8>>, Angle);
{rotate_single, Expression@1, Angle@1} ->
build_single_command(Expression@1, <<"rotate"/utf8>>, Angle@1);
{scale, Expressions@5, Vector@2} ->
build_command(Expressions@5, <<"scale"/utf8>>, Vector@2);
{scale_single, Expression@2, Vector@3} ->
build_single_command(Expression@2, <<"scale"/utf8>>, Vector@3);
{mirror, Expressions@6, Vector@4} ->
build_command(Expressions@6, <<"mirror"/utf8>>, Vector@4);
{circle, Value, Rod} ->
case Rod of
radius ->
_pipe@12 = fmglee:new(<<"circle(r=%f);"/utf8>>),
_pipe@13 = fmglee:f(_pipe@12, Value),
fmglee:build(_pipe@13);
diameter ->
_pipe@14 = fmglee:new(<<"circle(d=%f);"/utf8>>),
_pipe@15 = fmglee:f(_pipe@14, Value),
fmglee:build(_pipe@15)
end;
{hull, Expressions@7} ->
_pipe@16 = fmglee:new(<<"hull(){\n%s\n}"/utf8>>),
_pipe@19 = fmglee:s(
_pipe@16,
begin
_pipe@17 = Expressions@7,
_pipe@18 = gleam@list:map(_pipe@17, fun to_openscad/1),
gleam@string:join(_pipe@18, <<"\n"/utf8>>)
end
),
fmglee:build(_pipe@19);
{minkowski, Expressions@8} ->
_pipe@20 = fmglee:new(<<"minkowski(){\n%s\n}"/utf8>>),
_pipe@23 = fmglee:s(
_pipe@20,
begin
_pipe@21 = Expressions@8,
_pipe@22 = gleam@list:map(_pipe@21, fun to_openscad/1),
gleam@string:join(_pipe@22, <<"\n"/utf8>>)
end
),
fmglee:build(_pipe@23);
{square, Width, Height, Center} ->
_pipe@24 = fmglee:new(
<<"square(width=%f,height=%f,center=%s);"/utf8>>
),
_pipe@25 = fmglee:f(_pipe@24, Width),
_pipe@26 = fmglee:f(_pipe@25, Height),
_pipe@27 = fmglee:s(_pipe@26, centered_or_not_to_string(Center)),
fmglee:build(_pipe@27);
{polygon, Polygons} ->
_pipe@28 = fmglee:new(<<"polygon(points=[%s]);"/utf8>>),
_pipe@34 = fmglee:s(
_pipe@28,
begin
_pipe@29 = Polygons,
_pipe@33 = gleam@list:map(
_pipe@29,
fun(Point) -> _pipe@30 = fmglee:new(<<"[%f,%f]"/utf8>>),
_pipe@31 = fmglee:f(
_pipe@30,
erlang:element(1, Point)
),
_pipe@32 = fmglee:f(
_pipe@31,
erlang:element(2, Point)
),
fmglee:build(_pipe@32) end
),
gleam@string:join(_pipe@33, <<","/utf8>>)
end
),
fmglee:build(_pipe@34);
{cube, Size, Center@1} ->
{W, D, H} = Size,
_pipe@35 = fmglee:new(
<<"cube(size=[%f, %f, %f], center=%s);"/utf8>>
),
_pipe@36 = fmglee:f(_pipe@35, W),
_pipe@37 = fmglee:f(_pipe@36, D),
_pipe@38 = fmglee:f(_pipe@37, H),
_pipe@39 = fmglee:s(_pipe@38, centered_or_not_to_string(Center@1)),
fmglee:build(_pipe@39);
{sphere, Value@1, Rod@1} ->
case Rod@1 of
radius ->
_pipe@40 = fmglee:new(<<"sphere(r=%f);"/utf8>>),
_pipe@41 = fmglee:f(_pipe@40, Value@1),
fmglee:build(_pipe@41);
diameter ->
_pipe@42 = fmglee:new(<<"sphere(d=%f);"/utf8>>),
_pipe@43 = fmglee:f(_pipe@42, Value@1),
fmglee:build(_pipe@43)
end;
{cylinder, Height@1, Value@2, Rod@2, Center@2} ->
case Rod@2 of
radius ->
_pipe@44 = fmglee:new(
<<"cylinder(h=%f,r=%f,center=%s);"/utf8>>
),
_pipe@45 = fmglee:f(_pipe@44, Height@1),
_pipe@46 = fmglee:f(_pipe@45, Value@2),
_pipe@47 = fmglee:s(
_pipe@46,
centered_or_not_to_string(Center@2)
),
fmglee:build(_pipe@47);
diameter ->
_pipe@48 = fmglee:new(
<<"cylinder(h=%f,d=%f,center=%s);"/utf8>>
),
_pipe@49 = fmglee:f(_pipe@48, Height@1),
_pipe@50 = fmglee:f(_pipe@49, Value@2),
_pipe@51 = fmglee:s(
_pipe@50,
centered_or_not_to_string(Center@2)
),
fmglee:build(_pipe@51)
end;
{cylinder2, Height@2, Value1, Value2, Rod@3, Center@3} ->
case Rod@3 of
radius ->
_pipe@52 = fmglee:new(
<<"cylinder(h=%f,r1=%f,r2=%f,center=%s);"/utf8>>
),
_pipe@53 = fmglee:f(_pipe@52, Height@2),
_pipe@54 = fmglee:f(_pipe@53, Value1),
_pipe@55 = fmglee:f(_pipe@54, Value2),
_pipe@56 = fmglee:s(
_pipe@55,
centered_or_not_to_string(Center@3)
),
fmglee:build(_pipe@56);
diameter ->
_pipe@57 = fmglee:new(
<<"cylinder(h=%f,d1=%f,d2=%f,center=%s);"/utf8>>
),
_pipe@58 = fmglee:f(_pipe@57, Height@2),
_pipe@59 = fmglee:f(_pipe@58, Value1),
_pipe@60 = fmglee:f(_pipe@59, Value2),
_pipe@61 = fmglee:s(
_pipe@60,
centered_or_not_to_string(Center@3)
),
fmglee:build(_pipe@61)
end;
{linear_extrude,
Expressions@9,
Height@3,
Center@4,
Convexity,
Twist,
Slices} ->
_pipe@62 = fmglee:new(
<<"linear_extrude(height=%f,center=%s,convexity=%f,twist=%f,slices=%f){\n%s\n}"/utf8>>
),
_pipe@63 = fmglee:f(_pipe@62, Height@3),
_pipe@64 = fmglee:s(_pipe@63, centered_or_not_to_string(Center@4)),
_pipe@65 = fmglee:f(_pipe@64, Convexity),
_pipe@66 = fmglee:f(_pipe@65, Twist),
_pipe@67 = fmglee:f(_pipe@66, Slices),
_pipe@70 = fmglee:s(
_pipe@67,
begin
_pipe@68 = Expressions@9,
_pipe@69 = gleam@list:map(_pipe@68, fun to_openscad/1),
gleam@string:join(_pipe@69, <<"\n"/utf8>>)
end
),
fmglee:build(_pipe@70);
{custom_code, Code} ->
Code;
{options, Options} ->
_pipe@71 = Options,
_pipe@72 = gleam@list:map(
_pipe@71,
fun(X) -> option_to_string(X) end
),
gleam@string:join(_pipe@72, <<"\n"/utf8>>)
end.
-file("src/gleamscad.gleam", 269).
?DOC(" Helper function, for creating some of the strings\n").
-spec build_command(
list(open_s_c_a_d_expr()),
binary(),
{float(), float(), float()}
) -> binary().
build_command(Expressions, Command, Vector) ->
_pipe = fmglee:new(<<"%s([%f, %f, %f]){\n%s\n}"/utf8>>),
_pipe@1 = fmglee:s(_pipe, Command),
_pipe@2 = fmglee:f(_pipe@1, erlang:element(1, Vector)),
_pipe@3 = fmglee:f(_pipe@2, erlang:element(2, Vector)),
_pipe@4 = fmglee:f(_pipe@3, erlang:element(3, Vector)),
_pipe@7 = fmglee:s(
_pipe@4,
begin
_pipe@5 = Expressions,
_pipe@6 = gleam@list:map(_pipe@5, fun to_openscad/1),
gleam@string:join(_pipe@6, <<"\n"/utf8>>)
end
),
fmglee:build(_pipe@7).