Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-dh committed Jun 1, 2021
1 parent fa7dc9f commit d51b687
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
3 changes: 2 additions & 1 deletion solid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
from .objects import P2, P3, P4, Vec3 , Vec4, Vec34, P3s, P23, Points, Indexes, ScadSize, OpenSCADObjectPlus
15 changes: 15 additions & 0 deletions solid/examples/scad_interface_example.py
Original file line number Diff line number Diff line change
@@ -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)
40 changes: 40 additions & 0 deletions solid/scad_interface.py
Original file line number Diff line number Diff line change
@@ -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)

11 changes: 7 additions & 4 deletions solid/solidpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=
# =========================================
Expand All @@ -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

Expand All @@ -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'

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d51b687

Please sign in to comment.