From d51b6872289f46b32c6a0ae3d5a612391ef664bd Mon Sep 17 00:00:00 2001 From: jeff <1105041+jeff-dh@users.noreply.github.com> Date: Wed, 2 Jun 2021 01:14:50 +0200 Subject: [PATCH] added ScadInterface (#181, #180, #178, #165, #61) --- solid/__init__.py | 3 +- solid/examples/scad_interface_example.py | 15 +++++++++ solid/scad_interface.py | 40 ++++++++++++++++++++++++ solid/solidpython.py | 11 ++++--- 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 solid/examples/scad_interface_example.py create mode 100644 solid/scad_interface.py diff --git a/solid/__init__.py b/solid/__init__.py index d7c9cb1f..24fd1086 100644 --- a/solid/__init__.py +++ b/solid/__init__.py @@ -6,6 +6,7 @@ from .solidpython import OpenSCADObject, IncludedOpenSCADObject from .objects import * from .patch_euclid import run_euclid_patch +from .scad_interface import ScadInterface # Type hints -from .objects import P2, P3, P4, Vec3 , Vec4, Vec34, P3s, P23, Points, Indexes, ScadSize, OpenSCADObjectPlus \ No newline at end of file +from .objects import P2, P3, P4, Vec3 , Vec4, Vec34, P3s, P23, Points, Indexes, ScadSize, OpenSCADObjectPlus diff --git a/solid/examples/scad_interface_example.py b/solid/examples/scad_interface_example.py new file mode 100644 index 00000000..923b8fc2 --- /dev/null +++ b/solid/examples/scad_interface_example.py @@ -0,0 +1,15 @@ +from solid import * + +scad = ScadInterface() + +scad.set_global_var("$fn", 6) + +scad.register_customizer_var("cyl_pos", "[1, 2, 3]") + +cube_pos = scad.get("cyl_pos") + +c = translate(cube_pos) ( + cylinder(r=scad.inline("$t * 3"), h=scad.inline("$t * 10")) + ) + +scad_render_to_file(c, scad_interface=scad) diff --git a/solid/scad_interface.py b/solid/scad_interface.py new file mode 100644 index 00000000..49d01e7a --- /dev/null +++ b/solid/scad_interface.py @@ -0,0 +1,40 @@ +from .solidpython import OpenSCADObject + +class OpenSCADConstant(OpenSCADObject): + def __init__(self, code): + super().__init__("not valid openscad code !?!?!", {}) + self.code = code + + def __repr__(self): + return self._render() + + def _render(self, render_holes=42): + return self.code + +class ScadInterface: + def __init__(self): + self.header = '' + + def register_customizer_var(self, name, value, options=''): + self.header += f'{name} = {value}; //{options}\n' + + def set_global_var(self, name, value): + self.header += f'{name} = {value};\n' + + def get_header_str(self): + return self.header + + def register_font(self, filename): + self.header += f'use <{filename}>\n' + + @staticmethod + def get(name): + return ScadInterface.inline(name) + + @staticmethod + def inline(code): + return scad_inline(code) + +def scad_inline(code): + return OpenSCADConstant(code) + diff --git a/solid/solidpython.py b/solid/solidpython.py index 4fc935f2..6725fbff 100755 --- a/solid/solidpython.py +++ b/solid/solidpython.py @@ -395,7 +395,6 @@ def _get_include_path(self, include_file_path): # No loadable SCAD file was found in sys.path. Raise an error raise ValueError(f"Unable to find included SCAD file: {include_file_path} in sys.path") - # ========================================= # = Rendering Python code to OpenSCAD code= # ========================================= @@ -412,7 +411,7 @@ def _find_include_strings(obj: Union[IncludedOpenSCADObject, OpenSCADObject]) -> include_strings.update(_find_include_strings(param)) return include_strings -def scad_render(scad_object: OpenSCADObject, file_header: str = '') -> str: +def scad_render(scad_object: OpenSCADObject, file_header: str = '', scad_interface=None) -> str: # Make this object the root of the tree root = scad_object @@ -424,6 +423,9 @@ def scad_render(scad_object: OpenSCADObject, file_header: str = '') -> str: includes = ''.join(include_strings) + "\n" scad_body = root._render() + if scad_interface != None: + file_header += scad_interface.get_header_str() + if file_header and not file_header.endswith('\n'): file_header += '\n' @@ -507,14 +509,15 @@ def scad_render_to_file(scad_object: OpenSCADObject, filepath: PathStr=None, out_dir: PathStr=None, file_header: str='', - include_orig_code: bool=True) -> str: + include_orig_code: bool=True, + scad_interface = None) -> str: header = file_header if include_orig_code: version = _get_version() date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") header = f"// Generated by SolidPython {version} on {date}\n" + file_header - rendered_string = scad_render(scad_object, header) + rendered_string = scad_render(scad_object, header, scad_interface=scad_interface) return _write_code_to_file(rendered_string, filepath, out_dir, include_orig_code) def _write_code_to_file(rendered_string: str,