Skip to content

Commit

Permalink
added ImplicitCAD support (SolidCode#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-dh committed Jun 2, 2021
1 parent fa1cbfc commit 8d37e12
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 8 deletions.
6 changes: 5 additions & 1 deletion solid/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
from pathlib import Path
import os
import platform
import sys

class Config:
def __init__(self):
self.builtins_file = Path(__file__).absolute().parent / "core/builtins.openscad"
self.use_implicit_builtins = "--implicit" in sys.argv

builtins_suffix = ".openscad" if not self.use_implicit_builtins else ".implicit"
self.builtins_file = Path(__file__).absolute().parent / ("core/builtins" + builtins_suffix)

self.enable_pickle_cache = True
self.pickle_cache_dir = self.get_pickle_cache_dir()
Expand Down
33 changes: 33 additions & 0 deletions solid/core/builtins.implicit
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module sphere(r=default, d=default, $fn=default);
module cube(size=default, center=default, r=default);
module square(size=default, center=default, r=default);
module cylinder(r=default, h=default, r1=default, r2=default,
$fn=default, center=default);
module circle(r=default, $fn=default);
module polygon(points, paths=default, r=default);

module union(r=default);
module difference(r=default);
module intersection(r=default);

module translate(v);
module scale(v);
module rotate(a=default, v=default);

module linear_extrude(height=default,
center=default,
twist=default,
scale=default,
translate=default,
r=default);

module pack(size, sep=default);
module shell(w);

module rotate_extrude(angle=default,
r=default,
translate=default,
rotate=default);

module unit(unit);

10 changes: 6 additions & 4 deletions solid/core/scad_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ def _write_to_file(out_string, filename=None, outdir=''):
outfile = filename

if not outfile:
suffix = ".scad" if not config.use_implicit_builtins else ".escad"

#try to get the filename of the calling module
import __main__
if hasattr(__main__, "__file__"):
#not called from a terminal
calling_file = Path(__main__.__file__).absolute()
outfile = calling_file.with_suffix(".scad")
outfile = calling_file.with_suffix(suffix)
else:
outfile = "expsolid_out.scad"
outfile = "expsolid_out" + suffix

outpath = Path(outdir)
if not outpath.exists():
Expand All @@ -74,9 +76,9 @@ def get_include_string():
continue

if v[1]:
strings.append(f"use <{k}>")
strings.append(f"use <{k}>;")
else:
strings.append(f"include <{k}>")
strings.append(f"include <{k}>;")

s = "\n".join(strings)
s += "\n\n" if s else ''
Expand Down
62 changes: 62 additions & 0 deletions solid/examples/14-implicitCAD.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# ======================================================
# = add relative path to the solid package to sys.path =
# ======================================================
import sys
from pathlib import Path
solidPath = Path(__file__).absolute().parent.parent.parent.as_posix()
sys.path.append(solidPath)
#==================================================

# run extopenscad with -r 2 to get proper results:
# extopenscad -r 2 examples/14-implicitCAD.scad

# this is the same example as 06-functions.py but with nice smooth implicitCAD
# roundings....

# this is how you activate the "implicit mode" of ExpSolid
# I couldn't figure out a nicer way to set a parameter which can be accessed
# during the import routine of exp_solid
#
# alternatively you can call the whole script with the --implicit parameter:
# python3 examples/14-implicitCAD.py --implicit

import sys
sys.argv.append("--implicit")

from solid import *

def wheel():
return cylinder(r=35, h=15, center=True).rotate(0, 90, 0)

def axle():
a = cylinder(r=10, h=130, center=True).\
rotate(0, 90, 0)

w1 = wheel().left(67)
w2 = wheel().right(67)

return w1 + w2 + a

def torso():
bottom = cube(100, 250, 50, center=True, r=10)
top = cube(80, 100, 60, center=True, r=10)

window_cube = cube(200, 55 ,50, center=True, r=10).down(10)
top = difference(r=10) (
top,
(union(r=10) (window_cube, window_cube.rotate(0, 0, 90)))
)

return union(r=10)(bottom, top.up(50))

def car():
t = torso()

front_axle = axle().down(20).back(80)

rear_axle = front_axle.forward(160)

return union(r=3)(t, front_axle, rear_axle)

car().save_as_scad()

9 changes: 6 additions & 3 deletions solid/extensions/access_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from . import convenience
from ..core.utils import escape_openscad_identifier
from ..core.object_base import ObjectBase
from ..config import config

__nothing__ = None

Expand All @@ -15,9 +16,11 @@
"""

#builtin transformations
_cascading_builtins = ("union difference intersection intersection_for translate " +\
"scale rotate mirror resize color offset hull render " +\
"linear_extrude rotate_extrude projection surface").split(" ")
_cascading_builtins = ("union difference intersection translate " +\
"scale rotate linear_extrude rotate_extrude").split(" ")
if not config.use_implicit_builtins:
_cascading_builtins += ("intersection_for mirror resize color offset hull render " +\
"projection surface").split(" ")

def add_builtin_to_object_base(name):
#get the builtin
Expand Down
4 changes: 4 additions & 0 deletions solid/extensions/bosl2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from ... import config
if config.use_implicit_builtins:
raise Exception("ExpSolid: unfortunately ImplicitCAD can't handle bosl2...")

#load the libs from std
from . import std
from . import constants
Expand Down

0 comments on commit 8d37e12

Please sign in to comment.