#!/usr/bin/env python3 import math import hazwaz import svgwrite # 1 mm in px mm = 3.7795276 LINE_STYLE = { 'stroke': svgwrite.utils.rgb(0, 0, 0), 'stroke_width': 0.5, 'fill': 'none' } LINE_STYLE_LIGHT = { 'stroke': svgwrite.utils.rgb(127, 127, 127), 'stroke_width': 0.5, 'fill': 'none' } TEXT_STYLE = { 'stroke': "none", 'stroke_width': 0.5, 'fill': svgwrite.utils.rgb(0, 0, 0), "font_family": "Free Mono", "font_weight": "bold", "font_size": 12, } class Cylindrical(hazwaz.Command): """ Draw the pattern for cylindrical thimbles """ def draw_pattern(self, dest): grp = dest.g() r_min = 6 r_max = 14 h = 20 y = 20 for r in range(r_min, r_max): size = dest.g() c_x = (r + 20) c_y = (y + r) size.add(dest.circle( center=(c_x * mm, c_y * mm), r=r * mm, ** LINE_STYLE, )) size.add(dest.circle( center=(c_x * mm, c_y * mm), r=(r - 2) * mm, ** LINE_STYLE_LIGHT, )) for i in range(12): size.add(dest.line( start=( (c_x + (r - 3) * math.cos(math.pi / 6 * i)) * mm, (c_y + (r - 3) * math.sin(math.pi / 6 * i)) * mm, ), end=( (c_x + (r - 1) * math.cos(math.pi / 6 * i)) * mm, (c_y + (r - 1) * math.sin(math.pi / 6 * i)) * mm, ), ** LINE_STYLE_LIGHT, )) size.add(dest.text( text=str(r * 2), insert=(c_x * mm, c_y * mm + 5), text_anchor="middle", ** TEXT_STYLE, )) d = r * math.pi * 2 / 12 size.add(dest.rect( insert=((r_max * 2 + 20 + 10) * mm, y * mm), size=((d * 15) * mm, h * mm), ** LINE_STYLE, )) for i in range(1, 15): size.add(dest.line( start=( (r_max * 2 + 20 + 10 + d * i) * mm, (y + 1) * mm, ), end=( (r_max * 2 + 20 + 10 + d * i) * mm, (y + 3) * mm, ), ** LINE_STYLE_LIGHT, )) size.add(dest.line( start=( (r_max * 2 + 20 + 10 + d * i - 1) * mm, (y + 2) * mm, ), end=( (r_max * 2 + 20 + 10 + d * i + 1) * mm, (y + 2) * mm, ), ** LINE_STYLE_LIGHT, )) for i in range(3): size.add(dest.line( start=( (r_max * 2 + 20 + 10 + d * 15 - 2) * mm, (y + 5 + 5 * i - 1) * mm, ), end=( (r_max * 2 + 20 + 10 + d * 15 - 2) * mm, (y + 5 + 5 * i + 1) * mm, ), ** LINE_STYLE_LIGHT, )) size.add(dest.line( start=( (r_max * 2 + 20 + 10 + d * 15 - 3) * mm, (y + 5 + 5 * i) * mm, ), end=( (r_max * 2 + 20 + 10 + d * 15 - 1) * mm, (y + 5 + 5 * i) * mm, ), ** LINE_STYLE_LIGHT, )) size.add(dest.text( text="size: " + str(r * 2), insert=((r_max * 2 + 20 + 10 + 2) * mm, (y + 6) * mm + 8), text_anchor="start", ** TEXT_STYLE, )) y += (max(r * 2, h) + 10) grp.add(size) dest.add(grp) def main(self): dest = svgwrite.Drawing( filename="cylindrical_thimbles.svg", size=("210mm", "297mm"), profile="full", ) self.draw_pattern(dest) dest.save() class Flat(hazwaz.Command): """ Draw the pattern for flat thimbles """ def draw_pattern(self, dest): grp = dest.g() sa = 2 r_min = 7 r_max = 13 h = 20 y = 20 for i, r in enumerate(range(r_min, r_max)): size = dest.g() # half the circumference plus 2 mm sewing allowance w = r * math.pi + 4 h = 35 top_x = 20 top_y = (35 + 10) * i + 20 thimble = dest.path( ( "M", top_x * mm, top_y * mm, "c", 0, h / 2 * mm, 0, h * mm, w / 2 * mm, h * mm, "c", w / 2 * mm, 0, w / 2 * mm, - h / 2 * mm, w / 2 * mm, - h * mm, "L", top_x * mm, top_y * mm, ), ** LINE_STYLE, ) size.add(thimble) size.add(dest.text( text="size: " + str(r * 2), insert=((top_x + w / 2) * mm, (top_y + 1) * mm + 8), text_anchor="middle", ** TEXT_STYLE, )) grp.add(size) dest.add(grp) def main(self): dest = svgwrite.Drawing( filename="flat_thimbles.svg", size=("210mm", "297mm"), profile="full", ) self.draw_pattern(dest) dest.save() class Thimble(hazwaz.MainCommand): """ Draw the pattern for leather thimbles """ commands = ( Cylindrical(), Flat(), ) if __name__ == "__main__": Thimble().run()