Skip to content

Commit 85db3d0

Browse files
Added support for 'import_', 'import_stl', and 'import_dxf', which all resolve to the OpenSCAD statement 'import'
1 parent 9190617 commit 85db3d0

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

solid/solidpython.py

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
{'name': 'union', 'args': [], 'kwargs': []} ,
2929
{'name': 'intersection', 'args': [], 'kwargs': []} ,
3030
{'name': 'difference', 'args': [], 'kwargs': []} ,
31-
{'name': 'hole', 'args': [], 'kwargs': []} ,
32-
{'name': 'part', 'args': [], 'kwargs': []} ,
31+
{'name': 'hole', 'args': [], 'kwargs': []} ,
32+
{'name': 'part', 'args': [], 'kwargs': []} ,
3333

3434
# Transforms
3535
{'name': 'translate', 'args': [], 'kwargs': ['v']} ,
@@ -43,14 +43,27 @@
4343
{'name': 'render', 'args': [], 'kwargs': ['convexity']},
4444

4545
# 2D to 3D transitions
46+
# FIXME: linear_extrude now has a scale parameter as of OpenSCAD 13.06 -ETJ 20 Jan 2014
47+
# Also: Check release notes for any other changes to the language, here:
48+
# https://github.com/openscad/openscad/blob/master/RELEASE_NOTES
49+
# Add Colors as here: http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations
4650
{'name': 'linear_extrude', 'args': [], 'kwargs': ['height', 'center', 'convexity', 'twist','slices']} ,
4751
{'name': 'rotate_extrude', 'args': [], 'kwargs': ['convexity', 'segments']} ,
48-
{'name': 'dxf_linear_extrude', 'args': ['file'], 'kwargs': ['layer', 'height', 'center', 'convexity', 'twist', 'slices']} ,
52+
{'name': 'dxf_linear_extrude', 'args': ['file'],'kwargs': ['layer', 'height', 'center', 'convexity', 'twist', 'slices']} ,
4953
{'name': 'projection', 'args': [], 'kwargs': ['cut']} ,
5054
{'name': 'surface', 'args': ['file'], 'kwargs': ['center','convexity']} ,
5155

56+
#Child/ren
57+
{'name': 'child', 'args': [], 'kwargs': ['index', 'vector', 'range']} ,
58+
{'name': 'children', 'args': [], 'kwargs': ['index', 'vector', 'range']} ,
59+
60+
# FIXME: support 'resize' as well -ETJ 20 Jan 2014
61+
5262
# Import/export
53-
{'name': 'import_stl', 'args': ['filename'], 'kwargs': ['convexity']} ,
63+
{'name': 'import_stl', 'args': ['file'], 'kwargs': ['layer', 'origin']} ,
64+
{'name': 'import_dxf', 'args': ['file'], 'kwargs': ['layer', 'origin']},
65+
{'name': 'import_', 'args': ['file'], 'kwargs': ['layer', 'origin']},
66+
5467

5568
# Modifiers; These are implemented by calling e.g.
5669
# obj.set_modifier( '*') or
@@ -87,6 +100,19 @@ def __init__( self):
87100
def __init__( self):
88101
openscad_object.__init__(self, 'part', {})
89102
self.set_part_root( True)
103+
''',
104+
# Import, import_dxf, and import_stl all resolve to the same OpenSCAD keyword, 'import'
105+
'import_':'''class import_( openscad_object):
106+
def __init__( self, file, origin=(0,0), layer=None):
107+
openscad_object.__init__(self, 'import', {'file':file, 'origin':origin, 'layer':layer})
108+
''',
109+
'import_dxf':'''class import_dxf( openscad_object):
110+
def __init__( self, file, origin=(0,0), layer=None):
111+
openscad_object.__init__(self, 'import', {'file':file, 'origin':origin, 'layer':layer})
112+
''',
113+
'import_stl':'''class import_stl( openscad_object):
114+
def __init__( self, file, origin=(0,0), layer=None):
115+
openscad_object.__init__(self, 'import', {'file':file, 'origin':origin, 'layer':layer})
90116
'''
91117

92118
}
@@ -259,18 +285,26 @@ def scad_render_animated_file( func_to_animate, steps=20, back_and_forth=True, f
259285
def scad_render_to_file( scad_object, filepath=None, file_header='', include_orig_code=True):
260286
rendered_string = scad_render( scad_object, file_header)
261287

262-
calling_file = os.path.abspath( calling_module().__file__)
263-
264-
if include_orig_code:
265-
rendered_string += sp_code_in_scad_comment( calling_file)
266-
267-
# This write is destructive, and ought to do some checks that the write
268-
# was successful.
269-
# If filepath isn't supplied, place a .scad file with the same name
270-
# as the calling module next to it
271-
if not filepath:
272-
filepath = os.path.splitext( calling_file)[0] + '.scad'
273-
288+
try:
289+
calling_file = os.path.abspath( calling_module().__file__)
290+
291+
if include_orig_code:
292+
rendered_string += sp_code_in_scad_comment( calling_file)
293+
294+
# This write is destructive, and ought to do some checks that the write
295+
# was successful.
296+
# If filepath isn't supplied, place a .scad file with the same name
297+
# as the calling module next to it
298+
if not filepath:
299+
filepath = os.path.splitext( calling_file)[0] + '.scad'
300+
except AttributeError, e:
301+
# If no calling_file was found, this is being called from the terminal.
302+
# We can't read original code from a file, so don't try,
303+
# and can't read filename from the calling file either, so just save to
304+
# solid.scad.
305+
if not filepath:
306+
filepath = os.path.abspath('.') + "/solid.scad"
307+
274308
f = open( filepath,"w")
275309
f.write( rendered_string)
276310
f.close()

solid/test/test_solidpython.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
{'name': 'render', 'kwargs': {'convexity': None}, 'expected': '\n\nrender();', 'args': {}, },
2929
{'name': 'projection', 'kwargs': {'cut': None}, 'expected': '\n\nprojection();', 'args': {}, },
3030
{'name': 'surface', 'kwargs': {'center': False, 'convexity': None}, 'expected': '\n\nsurface(center = false, file = "/Path/to/dummy.dxf");', 'args': {'file': "'/Path/to/dummy.dxf'"}, },
31-
{'name': 'import_stl', 'kwargs': {'convexity': None}, 'expected': '\n\nimport_stl(filename = "/Path/to/dummy.dxf");', 'args': {'filename': "'/Path/to/dummy.dxf'"}, },
31+
{'name': 'import_stl', 'kwargs': {'layer': None, 'origin': (0,0)}, 'expected': '\n\nimport(origin = [0, 0], file = "/Path/to/dummy.stl");', 'args': {'file': "'/Path/to/dummy.stl'"}, },
32+
{'name': 'import_dxf', 'kwargs': {'layer': None, 'origin': (0,0)}, 'expected': '\n\nimport(origin = [0, 0], file = "/Path/to/dummy.dxf");', 'args': {'file': "'/Path/to/dummy.dxf'"}, },
33+
{'name': 'import_', 'kwargs': {'layer': None, 'origin': (0,0)}, 'expected': '\n\nimport(origin = [0, 0], file = "/Path/to/dummy.dxf");', 'args': {'file': "'/Path/to/dummy.dxf'"}, },
3234
{'name': 'linear_extrude', 'kwargs': {'twist': None, 'slices': None, 'center': False, 'convexity': None, 'height': 1}, 'expected': '\n\nlinear_extrude(center = false, height = 1);', 'args': {}, },
3335
{'name': 'rotate_extrude', 'kwargs': {'convexity': None}, 'expected': '\n\nrotate_extrude();', 'args': {}, },
34-
{'name': 'dxf_linear_extrude', 'kwargs': {'layer': None, 'center': False, 'slices': None, 'height': 1, 'twist': None, 'convexity': None}, 'expected': '\n\ndxf_linear_extrude(center = false, height = 1, file = "/Path/to/dummy.dxf");', 'args': {'file': "'/Path/to/dummy.dxf'"}, },
3536
{'name': 'intersection_for', 'kwargs': {}, 'expected': '\n\nintersection_for(n = [0, 1, 2]);', 'args': {'n': [0, 1, 2]}, },
3637
]
3738

0 commit comments

Comments
 (0)