Skip to content

Commit

Permalink
-- Changed classes 'openscad_object' and 'imported_openscad_object' t…
Browse files Browse the repository at this point in the history
…o 'OpenSCADObject' and 'ImportedOpenSCADObject' to bring them closer to Pep8.

-- Removed space between open-paren & first element, e.g. cube( 5) => cube(5), to agree better with Pep8
  • Loading branch information
Evan Jones committed May 23, 2015
1 parent 991029a commit 151235f
Show file tree
Hide file tree
Showing 22 changed files with 653 additions and 655 deletions.
46 changes: 23 additions & 23 deletions README.md
Expand Up @@ -36,7 +36,7 @@ This Python code:
cube(10),
sphere(15)
)
print(scad_render( d))
print(scad_render(d))


Generates this OpenSCAD code:
Expand All @@ -49,18 +49,18 @@ Generates this OpenSCAD code:
That doesn't seem like such a savings, but the following SolidPython code is a
lot shorter (and I think a lot clearer) than the SCAD code it compiles to:

d = cube( 5) + right(5)( sphere(5)) - cylinder( r=2, h=6)
d = cube(5) + right(5)(sphere(5)) - cylinder(r=2, h=6)

Generates this OpenSCAD code:

difference(){
union(){
cube(5);
translate( [5, 0,0]){
sphere( 5);
sphere(5);
}
}
cylinder( r=2, h=6);
cylinder(r=2, h=6);
}

# Advantages<a id="advantages"></a>
Expand All @@ -79,9 +79,9 @@ impossible in pure OpenSCAD. Among these are:
(You may need to use `sudo pip install solidpython`, depending on your environment.)

* **OR:** Download SolidPython ( Click [here](https://github.com/SolidCode/SolidPython/archive/master.zip) to download directly, or use git to pull it all down)
* **OR:** Download SolidPython (Click [here](https://github.com/SolidCode/SolidPython/archive/master.zip) to download directly, or use git to pull it all down)

( Note that SolidPython also depends on the [PyEuclid](http://pypi.python.org/pypi/euclid) Vector math library, installable via `sudo pip install euclid`)
(Note that SolidPython also depends on the [PyEuclid](http://pypi.python.org/pypi/euclid) Vector math library, installable via `sudo pip install euclid`)

* Unzip the file, probably in ~/Downloads/SolidPython-master
* In a terminal, cd to location of file:
Expand Down Expand Up @@ -115,8 +115,8 @@ impossible in pure OpenSCAD. Among these are:
sphere(15)
)

* Call ```scad_render( py_scad_obj)``` to generate SCAD code. This returns a string of valid OpenSCAD code.
* *or*: call ```scad_render_to_file( py_scad_obj, filepath)``` to
* Call ```scad_render(py_scad_obj)``` to generate SCAD code. This returns a string of valid OpenSCAD code.
* *or*: call ```scad_render_to_file(py_scad_obj, filepath)``` to
store that code in a file.
* If 'filepath' is open in the OpenSCAD IDE and Design =>
'Automatic Reload and Compile' is checked (in the OpenSCAD IDE), calling
Expand All @@ -130,7 +130,7 @@ The best way to learn how SolidPython works is to look at the included example c
If you've installed SolidPython, the following line of Python will print(the location of )
the examples directory:

import os, solid; print(os.path.dirname( solid.__file__) + '/examples')
import os, solid; print(os.path.dirname(solid.__file__) + '/examples')
Or browse the example code on Github [here](https://github.com/SolidCode/SolidPython/tree/master/solid/examples)

Expand All @@ -142,24 +142,24 @@ Following Elmo Mäntynen's suggestion, SCAD objects override
the basic operators + (union), - (difference), and * (intersection).
So

c = cylinder( r=10, h=5) + cylinder( r=2, h=30)
c = cylinder(r=10, h=5) + cylinder(r=2, h=30)
is the same as:

c = union()(
cylinder( r=10, h=5),
cylinder( r=2, h=30)
cylinder(r=10, h=5),
cylinder(r=2, h=30)
)

Likewise:

c = cylinder( r=10, h=5)
c -= cylinder( r=2, h=30)
c = cylinder(r=10, h=5)
c -= cylinder(r=2, h=30)

is the same as:

c = difference()(
cylinder( r=10, h=5),
cylinder( r=2, h=30)
cylinder(r=10, h=5),
cylinder(r=2, h=30)
)

### First-class Negative Space (Holes)<a id="first-class-negative-space-holes"></a>
Expand Down Expand Up @@ -215,18 +215,18 @@ My apologies.
### Arcs<a id="arcs"></a>
I've found this useful for fillets and rounds.

arc( rad=10, start_degrees=90, end_degrees=210)
arc(rad=10, start_degrees=90, end_degrees=210)

draws an arc of radius 10 counterclockwise from 90 to 210 degrees.

arc_inverted( rad=10, start_degrees=0, end_degrees=90)
arc_inverted(rad=10, start_degrees=0, end_degrees=90)

draws the portion of a 10x10 square NOT in a 90 degree circle of radius 10.
This is the shape you need to add to make fillets or remove to make rounds.

### Offsets<a id="offsets"></a>
To offset a set of points in one direction or another ( inside or outside a closed
figure, for example) use `solid.utils.offset_points( point_arr, offset, inside=True)`
To offset a set of points in one direction or another (inside or outside a closed
figure, for example) use `solid.utils.offset_points(point_arr, offset, inside=True)`

Note that, for a non-convex figure, inside and outside may be non-intuitive. The
simple solution is to manually check that your offset is going in the direction you
Expand All @@ -235,16 +235,16 @@ intend, and change the boolean value of `inside` if you're not happy.
See the code for futher explanation. Improvements on the inside/outside algorithm would be welcome.

### Extrude Along Path<a id="extrude_along_path"></a>
`solid.utils.extrude_along_path( shape_pts, path_pts, scale_factors=None)`
`solid.utils.extrude_along_path(shape_pts, path_pts, scale_factors=None)`

See [`solid/examples/path_extrude_example.py`](https://github.com/SolidCode/SolidPython/blob/master/solid/examples/path_extrude_example.py) for use.


### Basic color library<a id="basic-color-library"></a>
You can change an object's color by using the OpenSCAD ```color([rgba_array])``` function:

transparent_blue = color( [0,0,1, 0.5])( cube(10)) # Specify with RGB[A]
red_obj = color( Red)( cube( 10)) # Or use predefined colors
transparent_blue = color( [0,0,1, 0.5])(cube(10)) # Specify with RGB[A]
red_obj = color(Red)(cube(10)) # Or use predefined colors

These colors are pre-defined in solid.utils:

Expand Down
2 changes: 1 addition & 1 deletion TODO_SolidPython.txt
Expand Up @@ -69,7 +69,7 @@ Completed:
-- Improving syntax so it's as close as possible to OpenSCAD's -ETJ 15 Feb 2011
-- By default, let polygon assume that connectivity between specified points (the paths argument) is
in the order specified in the points array. Without this, there's always
boilerplate: polygon( points=arr, paths= range(len(arr)))
boilerplate: polygon(points=arr, paths= range(len(arr)))
-- Add 'segments' arguments to circle(), sphere() and cylinder()? This breaks
with OpenSCAD, but is simpler than circle(10).add_param('$fn', 30)
-- Unit testing.
Expand Down
8 changes: 4 additions & 4 deletions solid/examples/animation_example.py
Expand Up @@ -6,17 +6,17 @@
from solid import *
from solid.utils import *

def my_animate( _time=0):
def my_animate(_time=0):
# _time will range from 0 to 1, not including 1
rads = _time * 2 * 3.1416
rad = 15
c = translate( [rad*cos(rads), rad*sin(rads)])( square( 10))
c = translate( [rad*cos(rads), rad*sin(rads)])(square(10))

return c

if __name__ == '__main__':
out_dir = sys.argv[1] if len(sys.argv) > 1 else os.curdir
file_out = os.path.join( out_dir, 'animation_example.scad')
file_out = os.path.join(out_dir, 'animation_example.scad')

print("%(__file__)s: SCAD file written to: \n%(file_out)s"%vars())

Expand All @@ -28,7 +28,7 @@ def my_animate( _time=0):
# at the bottom of the OpenSCAD window
# - FPS & Steps are flexible. For a start, set both to 20
# play around from there
scad_render_animated_file( my_animate, # A function that takes a float argument
scad_render_animated_file(my_animate, # A function that takes a float argument
# called '_time' in [0,1)
# and returns an OpenSCAD object
steps=20, # Number of steps to create one complete motion
Expand Down
6 changes: 3 additions & 3 deletions solid/examples/append_solidpython_code.py
Expand Up @@ -8,13 +8,13 @@
SEGMENTS = 48

def show_appended_python_code():
a = cylinder( r=10, h=10, center=True) + up(5)( cylinder(r1=10, r2=0, h=10))
a = cylinder(r=10, h=10, center=True) + up(5)(cylinder(r1=10, r2=0, h=10))

return a

if __name__ == '__main__':
out_dir = sys.argv[1] if len(sys.argv) > 1 else os.curdir
file_out = os.path.join( out_dir, 'append_solidpython_code.scad')
file_out = os.path.join(out_dir, 'append_solidpython_code.scad')

a = show_appended_python_code()

Expand All @@ -24,5 +24,5 @@ def show_appended_python_code():
# = bottom of the generated OpenSCAD code, so the final document
# = contains the easy-to-read python code as well as the SCAD.
# = ------------------------------------------------------------ =
scad_render_to_file( a, file_out, include_orig_code=True)
scad_render_to_file(a, file_out, include_orig_code=True)

20 changes: 10 additions & 10 deletions solid/examples/basic_geometry.py
Expand Up @@ -16,13 +16,13 @@ def basic_geometry():
# left_piece uses standard OpenSCAD grammar (note the commas between
# block elements; OpenSCAD doesn't require this)
left_piece = union()(
translate( [-15, 0, 0])(
cube( [10, 5, 3], center=True)
translate([-15, 0, 0])(
cube([10, 5, 3], center=True)
),
translate( [-10, 0, 0])(
translate([-10, 0, 0])(
difference()(
cylinder( r=5, h=15, center=True),
cylinder( r=4, h=16, center=True)
cylinder(r=5, h=15, center=True),
cylinder(r=4, h=16, center=True)
)
)
)
Expand All @@ -31,15 +31,15 @@ def basic_geometry():
# - (minus) is equivalent to difference() and * (star) is equivalent to intersection
# solid.utils also defines up(), down(), left(), right(), forward(), and back()
# for common transforms.
right_piece = right( 15)( cube([10, 5, 3], center=True))
cyl = cylinder( r=5, h=15, center=True) - cylinder( r=4, h=16, center=True)
right_piece += right(10)( cyl)
right_piece = right(15)(cube([10, 5, 3], center=True))
cyl = cylinder(r=5, h=15, center=True) - cylinder(r=4, h=16, center=True)
right_piece += right(10)(cyl)

return union()(left_piece, right_piece)

if __name__ == '__main__':
out_dir = sys.argv[1] if len(sys.argv) > 1 else os.curdir
file_out = os.path.join( out_dir, 'basic_geometry.scad')
file_out = os.path.join(out_dir, 'basic_geometry.scad')

a = basic_geometry()

Expand All @@ -49,4 +49,4 @@ def basic_geometry():
# the detail of arcs by changing the SEGMENTS variable. This can
# be expensive when making lots of small curves, but is otherwise
# useful.
scad_render_to_file( a, file_out, file_header='$fn = %s;'%SEGMENTS)
scad_render_to_file(a, file_out, file_header='$fn = %s;'%SEGMENTS)
6 changes: 3 additions & 3 deletions solid/examples/basic_scad_include.py
Expand Up @@ -9,17 +9,17 @@
def demo_scad_include():
# scad_to_include.scad includes a module called steps()
scad_path = os.path.join(os.path.dirname(__file__), "scad_to_include.scad")
use( scad_path) # could also use 'include', but that has side-effects;
use(scad_path) # could also use 'include', but that has side-effects;
# 'use' just imports without executing any of the imported code
return steps(5)


if __name__ == '__main__':
out_dir = sys.argv[1] if len(sys.argv) > 1 else os.curdir
file_out = os.path.join( out_dir, 'scad_include_example.scad')
file_out = os.path.join(out_dir, 'scad_include_example.scad')

a = demo_scad_include()

print("%(__file__)s: SCAD file written to: \n%(file_out)s"%vars())

scad_render_to_file( a, file_out)
scad_render_to_file(a, file_out)
20 changes: 10 additions & 10 deletions solid/examples/bom_scad.py
Expand Up @@ -34,16 +34,16 @@
doohickey_h = 5

def head():
return cylinder( h=head_height, r =head_rad)
return cylinder(h=head_height, r =head_rad)


@bom_part("M3x16 Bolt", 0.12, currency="€")
def m3_16( a=3):
def m3_16(a=3):
bolt_height = 16
m = union()(
head(),
translate( [0,0, -bolt_height])(
cylinder( r=m3_rad, h=bolt_height)
cylinder(r=m3_rad, h=bolt_height)
)
)
return m
Expand All @@ -55,20 +55,20 @@ def m3_12():
m = union()(
head(),
translate( [ 0, 0, -bolt_height])(
cylinder( r=m3_rad, h=bolt_height)
cylinder(r=m3_rad, h=bolt_height)
)
)
return m


@bom_part("M3 Nut", 0.04, currency="R$")
def m3_nut():
hx = cylinder( r=nut_rad, h=nut_height)
hx = cylinder(r=nut_rad, h=nut_height)
hx.add_param('$fn', 6) # make the nut hexagonal
n = difference()(
hx,
translate([0,0,-EPSILON])(
cylinder( r=m3_rad, h=nut_height+2*EPSILON )
cylinder(r=m3_rad, h=nut_height+2*EPSILON )
)
)
return n
Expand All @@ -81,9 +81,9 @@ def doohickey():
)
d = difference()(
cube([30, 10, doohickey_h], center=True),
translate([-10, 0,0])( hole_cyl),
translate([-10, 0,0])(hole_cyl),
hole_cyl,
translate([10,0,0])( hole_cyl)
translate([10,0,0])(hole_cyl)
)
return d

Expand All @@ -102,7 +102,7 @@ def assemble():

if __name__ == '__main__':
out_dir = sys.argv[1] if len(sys.argv) > 1 else os.curdir
file_out = os.path.join( out_dir, 'BOM_example.scad')
file_out = os.path.join(out_dir, 'BOM_example.scad')

a = assemble()

Expand All @@ -111,4 +111,4 @@ def assemble():
print("%(__file__)s: SCAD file written to: \n%(file_out)s"%vars())
print(bom)

scad_render_to_file( a, file_out)
scad_render_to_file(a, file_out)

0 comments on commit 151235f

Please sign in to comment.