Skip to content

Commit

Permalink
Merge pull request #123 from Cabalist/pep8_examples
Browse files Browse the repository at this point in the history
Pep8 examples/
  • Loading branch information
etjones authored Oct 14, 2019
2 parents 822326d + 17dec25 commit 431aded
Show file tree
Hide file tree
Showing 16 changed files with 227 additions and 281 deletions.
42 changes: 20 additions & 22 deletions solid/examples/animation_example.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import os
#! /usr/bin/env python3
import sys
from math import cos, sin
from typing import Optional

from solid import *
from solid.utils import *
from math import sin, cos, radians, degrees
from solid import scad_render_animated_file
from solid.objects import square, translate
from solid.solidpython import OpenSCADObject

def my_animate(_time=0):

def my_animate(_time: Optional[float] = 0) -> OpenSCADObject:
# _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 None

Expand All @@ -27,18 +28,15 @@ 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
file_out = 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
back_and_forth=True, # If true, runs the complete motion
# forward and then in reverse,
# to avoid discontinuity
file_out = 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
back_and_forth=True, # If true, runs the complete motion
# forward and then in reverse,
# to avoid discontinuity
out_dir=out_dir,
include_orig_code=True ) # Append SolidPython code
# to the end of the generated
# OpenSCAD code.
include_orig_code=True) # Append SolidPython code
# to the end of the generated
# OpenSCAD code.
print(f"{__file__}: SCAD file written to: \n{file_out}")



12 changes: 6 additions & 6 deletions solid/examples/append_solidpython_code.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
#! /usr/bin/env python3
import sys

from solid import *
from solid.utils import *
from solid import scad_render_to_file
from solid.objects import cylinder
from solid.utils import up

SEGMENTS = 48

Expand All @@ -14,6 +13,7 @@ def show_appended_python_code():

return a


if __name__ == '__main__':
out_dir = sys.argv[1] if len(sys.argv) > 1 else None
a = show_appended_python_code()
Expand All @@ -23,5 +23,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.
# = ------------------------------------------------------------ =
file_out = scad_render_to_file(a, out_dir=out_dir, include_orig_code=True)
file_out = scad_render_to_file(a, out_dir=out_dir, include_orig_code=True)
print(f"{__file__}: SCAD file written to: \n{file_out}")
33 changes: 16 additions & 17 deletions solid/examples/basic_geometry.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import os
#! /usr/bin/env python3
import sys

from solid import *
from solid.utils import *
from solid import scad_render_to_file
from solid.objects import cube, cylinder, difference, translate, union
from solid.utils import right

SEGMENTS = 48

Expand All @@ -17,18 +15,18 @@ 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([-10, 0, 0])(
difference()(
cylinder(r=5, h=15, center=True),
cylinder(r=4, h=16, center=True)
)
)
left_piece = union()(
translate((-15, 0, 0))(
cube([10, 5, 3], center=True)
),
translate((-10, 0, 0))(
difference()(
cylinder(r=5, h=15, center=True),
cylinder(r=4, h=16, center=True)
)

)
)

# Right piece uses a more Pythonic grammar. + (plus) is equivalent to union(),
# - (minus) is equivalent to difference() and * (star) is equivalent to intersection
# solid.utils also defines up(), down(), left(), right(), forward(), and back()
Expand All @@ -39,6 +37,7 @@ def basic_geometry():

return union()(left_piece, right_piece)


if __name__ == '__main__':
out_dir = sys.argv[1] if len(sys.argv) > 1 else None

Expand Down
20 changes: 11 additions & 9 deletions solid/examples/basic_scad_include.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#! /usr/bin/env python3
import sys
from pathlib import Path

from solid import *
from solid import scad_render_to_file
from solid.objects import import_scad, use


# Import OpenSCAD code and call it from Python code.
# The path given to use() or import_scad() must be absolute or findable in sys.path


def demo_import_scad():
scad_path = Path(__file__).parent / 'scad_to_include.scad'
scad_mod = import_scad(scad_path)
a = scad_mod.steps(5)
return a
return scad_mod.steps(5)

# The `use()` function mimics the bahavior of OpenSCAD's use()`

# The `use()` function mimics the behavior of OpenSCAD's use()`
def demo_scad_use():
# scad_to_include.scad includes a module called steps()
scad_path = Path(__file__).parent / 'scad_to_include.scad'
# `This adds the SCAD module `steps()` to the global namespace
use(scad_path)
use(scad_path)

return steps(5)

a = steps(5)
return a

if __name__ == '__main__':
out_dir = Path(sys.argv[1]) if len(sys.argv) > 1 else None
Expand Down
64 changes: 33 additions & 31 deletions solid/examples/bom_scad.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#! /usr/bin/env python3

# Basic shape with several repeated parts, demonstrating the use of
# solid.utils.bill_of_materials()
Expand All @@ -18,11 +17,12 @@
#
# -ETJ 08 Mar 2011

import os
import sys

from solid import *
from solid.utils import *
from solid import scad_render_to_file
from solid.objects import cube, cylinder, difference, translate, union
from solid.utils import EPSILON
from solid.utils import bill_of_materials, bom_part, set_bom_headers

head_rad = 2.65
head_height = 2.8
Expand All @@ -36,6 +36,7 @@

set_bom_headers("link", "leftover")


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

Expand All @@ -44,10 +45,10 @@ def head():
def m3_16(a=3):
bolt_height = 16
m = union()(
head(),
translate([0, 0, -bolt_height])(
cylinder(r=m3_rad, h=bolt_height)
)
head(),
translate((0, 0, -bolt_height))(
cylinder(r=m3_rad, h=bolt_height)
)
)
return m

Expand All @@ -56,10 +57,10 @@ def m3_16(a=3):
def m3_12():
bolt_height = 12
m = union()(
head(),
translate([0, 0, -bolt_height])(
cylinder(r=m3_rad, h=bolt_height)
)
head(),
translate((0, 0, -bolt_height))(
cylinder(r=m3_rad, h=bolt_height)
)
)
return m

Expand All @@ -69,40 +70,41 @@ def m3_nut():
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)
)
hx,
translate((0, 0, -EPSILON))(
cylinder(r=m3_rad, h=nut_height + 2 * EPSILON)
)
)
return n


@bom_part()
def doohickey():
hole_cyl = translate([0, 0, -EPSILON])(
cylinder(r=m3_rad, h=doohickey_h + 2 * EPSILON)
hole_cyl = translate((0, 0, -EPSILON))(
cylinder(r=m3_rad, h=doohickey_h + 2 * EPSILON)
)
d = difference()(
cube([30, 10, doohickey_h], center=True),
translate([-10, 0, 0])(hole_cyl),
hole_cyl,
translate([10, 0, 0])(hole_cyl)
cube([30, 10, doohickey_h], center=True),
translate((-10, 0, 0))(hole_cyl),
hole_cyl,
translate((10, 0, 0))(hole_cyl)
)
return d


def assembly():
return union()(
doohickey(),
translate([-10, 0, doohickey_h / 2])(m3_12()),
translate([ 0, 0, doohickey_h / 2])(m3_16()),
translate([ 10, 0, doohickey_h / 2])(m3_12()),
# Nuts
translate([-10, 0, -nut_height - doohickey_h / 2])(m3_nut()),
translate([ 0, 0, -nut_height - doohickey_h / 2])(m3_nut()),
translate([ 10, 0, -nut_height - doohickey_h / 2])(m3_nut()),
doohickey(),
translate((-10, 0, doohickey_h / 2))(m3_12()),
translate((0, 0, doohickey_h / 2))(m3_16()),
translate((10, 0, doohickey_h / 2))(m3_12()),
# Nuts
translate((-10, 0, -nut_height - doohickey_h / 2))(m3_nut()),
translate((0, 0, -nut_height - doohickey_h / 2))(m3_nut()),
translate((10, 0, -nut_height - doohickey_h / 2))(m3_nut()),
)


if __name__ == '__main__':
out_dir = sys.argv[1] if len(sys.argv) > 1 else None

Expand Down
17 changes: 6 additions & 11 deletions solid/examples/hole_example.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import os
#! /usr/bin/env python3
import sys

# Assumes SolidPython is in site-packages or elsewhwere in sys.path
from solid import *
from solid.utils import *
from solid import scad_render_to_file
from solid.objects import cube, cylinder, hole, part, rotate
from solid.utils import FORWARD_VEC, right, up

SEGMENTS = 120

Expand Down Expand Up @@ -78,9 +75,8 @@ def multipart_hole():

# The section of the bolt inside not_part disappears. The section
# of the bolt inside is_part is still there.
a = not_part + bolt + right(45)(is_part + bolt)
return not_part + bolt + right(45)(is_part + bolt)

return a

if __name__ == '__main__':
out_dir = sys.argv[1] if len(sys.argv) > 1 else None
Expand All @@ -95,6 +91,5 @@ def multipart_hole():
b = up(40)(multipart_hole())
a += b

file_out = scad_render_to_file(a, out_dir=out_dir, file_header=f'$fn = {SEGMENTS};',
include_orig_code=True)
file_out = scad_render_to_file(a, out_dir=out_dir, file_header=f'$fn = {SEGMENTS};', include_orig_code=True)
print(f"{__file__}: SCAD file written to: \n{file_out}")
Loading

0 comments on commit 431aded

Please sign in to comment.