|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2025 Scott Bezek and the splitflap contributors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | + |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import argparse |
| 22 | +import logging |
| 23 | +import os |
| 24 | +import sys |
| 25 | +import time |
| 26 | +import openscad |
| 27 | + |
| 28 | +from multiprocessing.dummy import Pool |
| 29 | +from shutil import rmtree |
| 30 | + |
| 31 | +script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 32 | +source_parts_dir = os.path.dirname(script_dir) |
| 33 | +repo_root = os.path.dirname(source_parts_dir) |
| 34 | +sys.path.append(repo_root) |
| 35 | + |
| 36 | +from util import file_util |
| 37 | + |
| 38 | +if __name__ == "__main__": |
| 39 | + logging.basicConfig(level=logging.DEBUG) |
| 40 | + |
| 41 | + parser = argparse.ArgumentParser() |
| 42 | + parser.add_argument( |
| 43 | + "-f", |
| 44 | + "--font", |
| 45 | + type=str, |
| 46 | + help="Name of font preset to use - see flap_fonts.scad", |
| 47 | + ) |
| 48 | + parser.add_argument("-t", "--text", type=str, help="String of text to generate") |
| 49 | + parser.add_argument( |
| 50 | + "--vertical-keepout-clip", |
| 51 | + action="store_true", |
| 52 | + help="Clip letters that violate vertical keepout zones", |
| 53 | + ) |
| 54 | + parser.add_argument( |
| 55 | + "--single-stl", |
| 56 | + action="store_true", |
| 57 | + default=False, |
| 58 | + help="Generate a single STL file for each flap", |
| 59 | + ) |
| 60 | + parser.add_argument( |
| 61 | + "--single-letter-stl", |
| 62 | + action="store_true", |
| 63 | + default=False, |
| 64 | + help="Generate a single STL file for the front and back letters (only used if --single-stl is not set)", |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + "--parallelism", |
| 68 | + type=int, |
| 69 | + help="How many OpenSCAD exports to run in parallel. Defaults to CPU count if unspecified.", |
| 70 | + default=None, |
| 71 | + ) |
| 72 | + args = parser.parse_args() |
| 73 | + |
| 74 | + extra_variables = {} |
| 75 | + |
| 76 | + if args.font is not None: |
| 77 | + extra_variables["font_preset"] = args.font |
| 78 | + |
| 79 | + if args.text is not None: |
| 80 | + extra_variables["character_list"] = args.text |
| 81 | + |
| 82 | + if args.vertical_keepout_clip: |
| 83 | + extra_variables["vertical_keepout_mode"] = 2 |
| 84 | + |
| 85 | + output_directory = os.path.join(source_parts_dir, "build", "flap_3dp") |
| 86 | + rmtree(output_directory, ignore_errors=True) |
| 87 | + |
| 88 | + def render_flap(flap_number): |
| 89 | + flap_path = os.path.join(output_directory, f"flap_{flap_number:02}") |
| 90 | + file_util.mkdir_p(flap_path) |
| 91 | + openscad_variables = { |
| 92 | + "flap_number": flap_number, |
| 93 | + "generateFullFlap": args.single_stl, |
| 94 | + "generateText": False, |
| 95 | + } |
| 96 | + openscad_variables.update(extra_variables) |
| 97 | + |
| 98 | + openscad.run( |
| 99 | + os.path.join(source_parts_dir, "flap_3dp.scad"), |
| 100 | + os.path.join(flap_path, f"{flap_number:02}_flap.stl"), |
| 101 | + variables=openscad_variables, |
| 102 | + capture_output=True, |
| 103 | + ) |
| 104 | + |
| 105 | + if not args.single_stl: |
| 106 | + if args.single_letter_stl: |
| 107 | + openscad_variables["generateText"] = True |
| 108 | + openscad.run( |
| 109 | + os.path.join(source_parts_dir, "flap_3dp.scad"), |
| 110 | + os.path.join(flap_path, f"{flap_number:02}_letters.stl"), |
| 111 | + variables=openscad_variables, |
| 112 | + capture_output=True, |
| 113 | + ignore_empty_top_level_object=True, |
| 114 | + ) |
| 115 | + else: |
| 116 | + openscad_variables["generateText"] = True |
| 117 | + openscad_variables["generateFrontText"] = True |
| 118 | + openscad_variables["generateBackText"] = False |
| 119 | + openscad.run( |
| 120 | + os.path.join(source_parts_dir, "flap_3dp.scad"), |
| 121 | + os.path.join(flap_path, f"{flap_number:02}_letter_front.stl"), |
| 122 | + variables=openscad_variables, |
| 123 | + capture_output=True, |
| 124 | + ignore_empty_top_level_object=True, |
| 125 | + ) |
| 126 | + |
| 127 | + openscad_variables["generateText"] = True |
| 128 | + openscad_variables["generateFrontText"] = False |
| 129 | + openscad_variables["generateBackText"] = True |
| 130 | + openscad.run( |
| 131 | + os.path.join(source_parts_dir, "flap_3dp.scad"), |
| 132 | + os.path.join(flap_path, f"{flap_number:02}_letter_back.stl"), |
| 133 | + variables=openscad_variables, |
| 134 | + capture_output=True, |
| 135 | + ignore_empty_top_level_object=True, |
| 136 | + ) |
| 137 | + |
| 138 | + pool = Pool(args.parallelism) |
| 139 | + num_flaps = len(args.text) if args.text is not None else 52 |
| 140 | + for _ in pool.imap_unordered(render_flap, range(num_flaps)): |
| 141 | + # Consume results as they occur so any exception is rethrown |
| 142 | + pass |
| 143 | + pool.close() |
| 144 | + pool.join() |
0 commit comments