Skip to content

Commit

Permalink
Stickerprint fixes: layout is now more parametrics. generate_model_de…
Browse files Browse the repository at this point in the history
…mos now avoids a rendering error.

Remaining geometry issues may be related to model definitions; will pursue in a followup.
  • Loading branch information
briantrice committed Apr 29, 2024
1 parent adeaa5e commit e983e4e
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 55 deletions.
75 changes: 36 additions & 39 deletions SlideRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
6. Models
7. Commands
"""

# ----------------------1. Setup----------------------------

from copy import copy
import inspect
import math
import re
Expand Down Expand Up @@ -871,14 +871,13 @@ class BleedDir(Enum):
DOWN = 'down'


def extend(image: Image, geom: Geometry, y: int, direction: BleedDir, amplitude: int):
def extend(image: Image, total_w: int, y: int, direction: BleedDir, amplitude: int):
"""
Used to create bleed for sticker cutouts
y: pixel row to duplicate
amplitude: number of pixels to extend
"""
w = geom.total_w
for x in range(0, w):
for x in range(0, total_w):
bleed_color = image.getpixel((x, y))
for yi in range(y - amplitude, y) if direction == BleedDir.UP else range(y, y + amplitude):
image.putpixel((x, yi), bleed_color)
Expand Down Expand Up @@ -1821,7 +1820,7 @@ def render_sliderule_mode(model: Model, sliderule_img=None, borders: bool = Fals
return sliderule_img


def render_stickerprint_mode(model, sliderule_img):
def render_stickerprint_mode(m: Model, sliderule_img: Image.Image):
# Code Names
# (fs) | UL,UM,UR [ ML,MM,MR ] LL,LM,LR |
# (bs) | UL,UM,UR [ ML,MM,MR ] LL,LM,LR |
Expand All @@ -1832,56 +1831,55 @@ def render_stickerprint_mode(model, sliderule_img):
o_y2 = 50 # y dir margins
o_a = 50 # overhang amount
ext = 20 # extension amount
geom = model.geometry
scale_w = 6500
geom_s = Geometry(
(scale_w, 1600),
Geometry.NO_MARGINS,
(scale_w, Geometry.SH),
Geometry.DEFAULT_TICK_WH,
640
)
style = model.style
scale_h = geom_s.SH
total_w = scale_w + 2 * o_x2
total_h = 5075
stickerprint_img = image_for_rendering(model, w=total_w, h=total_h)
r = Renderer.make(stickerprint_img, geom_s, style)
geom = m.geometry
x_off = (geom.side_w - geom.SL - 900) // 2
scale_w = geom.side_w - x_off * 2
scale_h = geom.SH
geom_s = copy(geom)
geom_s.side_w = scale_w
# geom_s.SL = scale_w
geom_s.oX = 0
geom_s.oY = 0
style = m.style
# fsUM,MM,LM:
y = 0
y += o_y2 + o_a
x_off = 750
x_left = geom.oX + x_off
slide_h = geom_s.slide_h
stator_h = geom_s.stator_h
slide_h = geom.slide_h
stator_h = geom.stator_h
total_w = scale_w + 2 * o_x2
sticker_row_h = max(slide_h, stator_h)
total_h = (geom.side_h + 2 * o_a) * 2 + o_a * 5 + sticker_row_h * 2 + 145
stickerprint_img = image_for_rendering(m, w=total_w, h=total_h)
r = Renderer.make(stickerprint_img, geom_s, style)
transcribe(sliderule_img, stickerprint_img, x_left, geom.oY, scale_w, stator_h, o_x2, y)
extend(stickerprint_img, geom_s, y + stator_h - 1, BleedDir.DOWN, ext)
extend(stickerprint_img, scale_w, y + stator_h - 1, BleedDir.DOWN, ext)
r.draw_corners(o_x2, y - o_a, o_x2 + scale_w, y + stator_h)
y += stator_h + o_a
transcribe(sliderule_img, stickerprint_img, x_left, geom.oY + stator_h + 1, scale_w, slide_h, o_x2, y)
extend(stickerprint_img, geom_s, y + 1, BleedDir.UP, ext)
extend(stickerprint_img, geom_s, y + slide_h - 1, BleedDir.DOWN, ext)
extend(stickerprint_img, scale_w, y + 1, BleedDir.UP, ext)
extend(stickerprint_img, scale_w, y + slide_h - 1, BleedDir.DOWN, ext)
r.draw_corners(o_x2, y, o_x2 + scale_w, y + slide_h)
y += slide_h + o_a
transcribe(sliderule_img, stickerprint_img, x_left, geom.oY + geom.side_h - stator_h, scale_w, stator_h, o_x2, y)
extend(stickerprint_img, geom_s, y + 1, BleedDir.UP, ext)
extend(stickerprint_img, geom_s, y + stator_h - 1, BleedDir.DOWN, ext)
extend(stickerprint_img, scale_w, y + 1, BleedDir.UP, ext)
extend(stickerprint_img, scale_w, y + stator_h - 1, BleedDir.DOWN, ext)
r.draw_corners(o_x2, y, o_x2 + scale_w, y + stator_h + o_a)
# bsUM,MM,LM:
y += stator_h + o_a + o_a + o_a
y_start = geom.oY + geom.side_h + geom.oY
transcribe(sliderule_img, stickerprint_img, x_left, y_start, scale_w, stator_h, o_x2, y)
extend(stickerprint_img, geom_s, y + stator_h - 1, BleedDir.DOWN, ext)
extend(stickerprint_img, scale_w, y + stator_h - 1, BleedDir.DOWN, ext)
r.draw_corners(o_x2, y - o_a, o_x2 + scale_w, y + stator_h)
y += stator_h + o_a
transcribe(sliderule_img, stickerprint_img, x_left, y_start + stator_h + 1 - 3, scale_w, slide_h, o_x2, y)
extend(stickerprint_img, geom_s, y + 1, BleedDir.UP, ext)
extend(stickerprint_img, geom_s, y + slide_h - 1, BleedDir.DOWN, ext)
extend(stickerprint_img, scale_w, y + 1, BleedDir.UP, ext)
extend(stickerprint_img, scale_w, y + slide_h - 1, BleedDir.DOWN, ext)
r.draw_corners(o_x2, y, o_x2 + scale_w, y + slide_h)
y += slide_h + o_a
transcribe(sliderule_img, stickerprint_img, x_left, y_start + geom_s.side_h - stator_h, scale_w, stator_h, o_x2, y)
extend(stickerprint_img, geom_s, y + 1, BleedDir.UP, ext)
extend(stickerprint_img, geom_s, y + stator_h - 1, BleedDir.DOWN, ext)
transcribe(sliderule_img, stickerprint_img, x_left, y_start + geom.side_h - stator_h, scale_w, stator_h, o_x2, y)
extend(stickerprint_img, scale_w, y + 1, BleedDir.UP, ext)
extend(stickerprint_img, scale_w, y + stator_h - 1, BleedDir.DOWN, ext)
y_bottom = y + stator_h + o_a
r.draw_corners(o_x2, y, o_x2 + scale_w, y_bottom)
y_b = y_bottom + 20
Expand All @@ -1895,23 +1893,22 @@ def render_stickerprint_mode(model, sliderule_img):
x_off + o_a, stator_h + o_a]
]
box_x_mirror = 6.5 * o_a + box_w + 2 * x_off
for box in boxes:
(x0, y0, dx, dy) = box
for (x0, y0, dx, dy) in boxes:
r.draw_box(x0, y0, dx, dy, Color.CUT)
r.draw_box(x0, y0 + slide_h + o_a, dx, dy, Color.CUT)

x0 = round(2 * box_x_mirror - x0 - dx)

r.draw_box(x0, y0, dx, dy, Color.CUT)
r.draw_box(x0, y0 + slide_h + o_a, dx, dy, Color.CUT)
r.draw_box(x0, y0 + sticker_row_h + o_a, dx, dy, Color.CUT)
points = [
[2 * o_a + 120, y_b + o_a + scale_h],
[6 * o_a + box_w + x_off + 2 * scale_h, y_b + scale_h],
[6 * o_a + box_w + x_off + scale_h, y_b + 2 * scale_h],

[2 * o_a + 120, y_b + slide_h + o_a + scale_h],
[6 * o_a + box_w + x_off + scale_h, y_b + 640 + o_a + o_a + 2 * scale_h],
[6 * o_a + box_w + x_off + 2 * scale_h, y_b + 640 + o_a + o_a + scale_h]
[6 * o_a + box_w + x_off + scale_h, y_b + slide_h + o_a + o_a + 2 * scale_h],
[6 * o_a + box_w + x_off + 2 * scale_h, y_b + slide_h + o_a + o_a + scale_h]
]
hole_r = 34 # (2.5mm diameter screw holes) = math.ceil(0.25 * Geometry.PixelsPerCM / 2)
for (p_x, p_y) in points:
Expand Down
Binary file modified examples/Aristo868.StickerCut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/Aristo965.StickerCut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/Demo.StickerCut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/FaberCastell283.StickerCut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/FaberCastell283N.StickerCut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/Graphoplex621.StickerCut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/Hemmi153.StickerCut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/MannheimOriginal.StickerCut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/MannheimWithRuler.StickerCut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/PickettN515T.StickerCut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/Ruler.StickerCut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/UltraLog.StickerCut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 23 additions & 16 deletions generate_model_demos.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,45 @@

from SlideRule import (
Models, Model, keys_of,
render_diagnostic_mode, render_sliderule_mode, render_stickerprint_mode
render_diagnostic_mode, render_sliderule_mode, render_stickerprint_mode, Mode
)


def main():
args_parser = argparse.ArgumentParser()
args_parser.add_argument('--mode',
choices=[m.value for m in Mode],
default=None,
help='Which mode to render (all by default)')
args_parser.add_argument('--model',
choices=keys_of(Models),
default=None,
help='Which sliderule model (all by default)')
cli_args = args_parser.parse_args()
base_dir = os.path.relpath('examples/')
os.makedirs(base_dir, exist_ok=True)
modes = [next(m for m in Mode if m.value == cli_args.mode)] if cli_args.mode else Mode
for model_name in ([cli_args.model] if cli_args.model else keys_of(Models)):
print(f'Building example outputs for: {model_name}')
model = getattr(Models, model_name) or Model.from_toml_file(os.path.join(base_dir, f'Model-{model_name}.toml'))

diagnostic_img = render_diagnostic_mode(model)
diagnostic_filename = os.path.join(base_dir, f'{model_name}.Diagnostic.png')
diagnostic_img.save(diagnostic_filename)
print(f' Diagnostic output for: {model_name} at: {diagnostic_filename}')

sliderule_img = render_sliderule_mode(model, borders=True, cutoffs=True)
sliderule_filename = os.path.join(base_dir, f'{model_name}.SlideRuleScales.png')
sliderule_img.save(sliderule_filename, 'PNG')
print(f' SlideRuleScales output for: {model_name} at: {sliderule_filename}')

sliderule_stickers_img = render_sliderule_mode(model)
stickers_img = render_stickerprint_mode(model, sliderule_stickers_img)
stickers_filename = os.path.join(base_dir, f'{model_name}.StickerCut.png')
stickers_img.save(stickers_filename)
print(f' StickerCut output for: {model_name} at: {stickers_filename}')
if Mode.DIAGNOSTIC in modes:
diagnostic_img = render_diagnostic_mode(model)
diagnostic_filename = os.path.join(base_dir, f'{model_name}.Diagnostic.png')
diagnostic_img.save(diagnostic_filename)
print(f' Diagnostic output for: {model_name} at: {diagnostic_filename}')
elif Mode.RENDER in modes:
sliderule_img = render_sliderule_mode(model, borders=True, cutoffs=True)
sliderule_filename = os.path.join(base_dir, f'{model_name}.SlideRuleScales.png')
sliderule_img.save(sliderule_filename, 'PNG')
print(f' SlideRuleScales output for: {model_name} at: {sliderule_filename}')
elif Mode.STICKERPRINT in modes:
sliderule_img = render_sliderule_mode(model, cutoffs=True)
sliderule_stickers_img = render_sliderule_mode(model, sliderule_img)
stickers_img = render_stickerprint_mode(model, sliderule_stickers_img)
stickers_filename = os.path.join(base_dir, f'{model_name}.StickerCut.png')
stickers_img.save(stickers_filename)
print(f' StickerCut output for: {model_name} at: {stickers_filename}')


if __name__ == '__main__':
Expand Down

0 comments on commit e983e4e

Please sign in to comment.