Skip to content

added ScadInterface (#181, #180, #178, #165, #61) #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 17 additions & 0 deletions solid/examples/scad_interface_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from solid import *

scad = ScadInterface()

scad.set_global_var("$fn", 6)

scad.register_customizer_var("cyl_pos", "[1, 2, 3]")

scad.additional_header_code("//some non sense")

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)
44 changes: 44 additions & 0 deletions solid/scad_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 = '\n' #make sure we don't get attached to some other code
#without a line break

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'

def additional_header_code(self, code):
self.header += code + "\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