Skip to content

Commit 8d37e12

Browse files
committed
added ImplicitCAD support (SolidCode#134)
1 parent fa1cbfc commit 8d37e12

File tree

6 files changed

+116
-8
lines changed

6 files changed

+116
-8
lines changed

solid/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
from pathlib import Path
33
import os
44
import platform
5+
import sys
56

67
class Config:
78
def __init__(self):
8-
self.builtins_file = Path(__file__).absolute().parent / "core/builtins.openscad"
9+
self.use_implicit_builtins = "--implicit" in sys.argv
10+
11+
builtins_suffix = ".openscad" if not self.use_implicit_builtins else ".implicit"
12+
self.builtins_file = Path(__file__).absolute().parent / ("core/builtins" + builtins_suffix)
913

1014
self.enable_pickle_cache = True
1115
self.pickle_cache_dir = self.get_pickle_cache_dir()

solid/core/builtins.implicit

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module sphere(r=default, d=default, $fn=default);
2+
module cube(size=default, center=default, r=default);
3+
module square(size=default, center=default, r=default);
4+
module cylinder(r=default, h=default, r1=default, r2=default,
5+
$fn=default, center=default);
6+
module circle(r=default, $fn=default);
7+
module polygon(points, paths=default, r=default);
8+
9+
module union(r=default);
10+
module difference(r=default);
11+
module intersection(r=default);
12+
13+
module translate(v);
14+
module scale(v);
15+
module rotate(a=default, v=default);
16+
17+
module linear_extrude(height=default,
18+
center=default,
19+
twist=default,
20+
scale=default,
21+
translate=default,
22+
r=default);
23+
24+
module pack(size, sep=default);
25+
module shell(w);
26+
27+
module rotate_extrude(angle=default,
28+
r=default,
29+
translate=default,
30+
rotate=default);
31+
32+
module unit(unit);
33+

solid/core/scad_render.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ def _write_to_file(out_string, filename=None, outdir=''):
4848
outfile = filename
4949

5050
if not outfile:
51+
suffix = ".scad" if not config.use_implicit_builtins else ".escad"
52+
5153
#try to get the filename of the calling module
5254
import __main__
5355
if hasattr(__main__, "__file__"):
5456
#not called from a terminal
5557
calling_file = Path(__main__.__file__).absolute()
56-
outfile = calling_file.with_suffix(".scad")
58+
outfile = calling_file.with_suffix(suffix)
5759
else:
58-
outfile = "expsolid_out.scad"
60+
outfile = "expsolid_out" + suffix
5961

6062
outpath = Path(outdir)
6163
if not outpath.exists():
@@ -74,9 +76,9 @@ def get_include_string():
7476
continue
7577

7678
if v[1]:
77-
strings.append(f"use <{k}>")
79+
strings.append(f"use <{k}>;")
7880
else:
79-
strings.append(f"include <{k}>")
81+
strings.append(f"include <{k}>;")
8082

8183
s = "\n".join(strings)
8284
s += "\n\n" if s else ''

solid/examples/14-implicitCAD.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# ======================================================
2+
# = add relative path to the solid package to sys.path =
3+
# ======================================================
4+
import sys
5+
from pathlib import Path
6+
solidPath = Path(__file__).absolute().parent.parent.parent.as_posix()
7+
sys.path.append(solidPath)
8+
#==================================================
9+
10+
# run extopenscad with -r 2 to get proper results:
11+
# extopenscad -r 2 examples/14-implicitCAD.scad
12+
13+
# this is the same example as 06-functions.py but with nice smooth implicitCAD
14+
# roundings....
15+
16+
# this is how you activate the "implicit mode" of ExpSolid
17+
# I couldn't figure out a nicer way to set a parameter which can be accessed
18+
# during the import routine of exp_solid
19+
#
20+
# alternatively you can call the whole script with the --implicit parameter:
21+
# python3 examples/14-implicitCAD.py --implicit
22+
23+
import sys
24+
sys.argv.append("--implicit")
25+
26+
from solid import *
27+
28+
def wheel():
29+
return cylinder(r=35, h=15, center=True).rotate(0, 90, 0)
30+
31+
def axle():
32+
a = cylinder(r=10, h=130, center=True).\
33+
rotate(0, 90, 0)
34+
35+
w1 = wheel().left(67)
36+
w2 = wheel().right(67)
37+
38+
return w1 + w2 + a
39+
40+
def torso():
41+
bottom = cube(100, 250, 50, center=True, r=10)
42+
top = cube(80, 100, 60, center=True, r=10)
43+
44+
window_cube = cube(200, 55 ,50, center=True, r=10).down(10)
45+
top = difference(r=10) (
46+
top,
47+
(union(r=10) (window_cube, window_cube.rotate(0, 0, 90)))
48+
)
49+
50+
return union(r=10)(bottom, top.up(50))
51+
52+
def car():
53+
t = torso()
54+
55+
front_axle = axle().down(20).back(80)
56+
57+
rear_axle = front_axle.forward(160)
58+
59+
return union(r=3)(t, front_axle, rear_axle)
60+
61+
car().save_as_scad()
62+

solid/extensions/access_syntax.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from . import convenience
33
from ..core.utils import escape_openscad_identifier
44
from ..core.object_base import ObjectBase
5+
from ..config import config
56

67
__nothing__ = None
78

@@ -15,9 +16,11 @@
1516
"""
1617

1718
#builtin transformations
18-
_cascading_builtins = ("union difference intersection intersection_for translate " +\
19-
"scale rotate mirror resize color offset hull render " +\
20-
"linear_extrude rotate_extrude projection surface").split(" ")
19+
_cascading_builtins = ("union difference intersection translate " +\
20+
"scale rotate linear_extrude rotate_extrude").split(" ")
21+
if not config.use_implicit_builtins:
22+
_cascading_builtins += ("intersection_for mirror resize color offset hull render " +\
23+
"projection surface").split(" ")
2124

2225
def add_builtin_to_object_base(name):
2326
#get the builtin

solid/extensions/bosl2/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from ... import config
2+
if config.use_implicit_builtins:
3+
raise Exception("ExpSolid: unfortunately ImplicitCAD can't handle bosl2...")
4+
15
#load the libs from std
26
from . import std
37
from . import constants

0 commit comments

Comments
 (0)