summaryrefslogtreecommitdiff
path: root/boxes/tuckbox.py
blob: 73d4eaf1f3fd89b55488324856bbf5af74c7b00b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3

import argparse

import svgwrite

try:
    from lesana.command import _get_first_docstring_line  # type: ignore
except ImportError:
    def _get_first_docstring_line(obj):
        return ""

try:
    import argcomplete  # type: ignore
except ImportError:
    argcomplete = False


# 1 mm in px
mm = 3.7795276

LINE_STYLE = {
    'stroke': svgwrite.utils.rgb(127, 127, 127),
    'stroke_width': 0.5,
    'fill': 'none'
    }


class Structure():
    def __init__(self, x, y, z, flap=20, tab=16):
        """
        """
        self.x = x * mm
        self.y = y * mm
        self.z = z * mm
        self.f = flap * mm
        self.t = tab * mm  # tab lenght

        self.full_width = self.t + self.x * 2 + self.z * 2
        self.full_height = self.f + self.z * 2 + self.y

    def add_to(self, drw, canvas_x=297 * mm, canvas_y=210 * mm):
        grp = drw.g()
        grp.add(drw.polygon(
            (
                (3 * mm, self.z - self.t),
                (self.z - 3 * mm, self.z - self.t),
                (self.z, self.z),
                (0, self.z),
            ),
            id='right_side_bottom_tab',
            ** LINE_STYLE,
            ))
        grp.add(drw.rect(
            (self.z, 0),
            (self.x, self.z),
            id='base',
            ** LINE_STYLE,
            ))
        grp.add(drw.polygon(
            (
                (self.z + self.x + 3 * mm, self.z - self.t),
                (self.z * 2 + self.x - 3 * mm, self.z - self.t),
                (self.z * 2 + self.x, self.z),
                (self.z + self.x, self.z),
            ),
            id='left_side_bottom_tab',
            ** LINE_STYLE,
            ))
        grp.add(drw.polygon(
            (
                (self.z * 2 + self.x + 3 * mm, self.z - self.t),
                (self.z * 2 + self.x * 2 - 3 * mm, self.z - self.t),
                (self.z * 2 + self.x * 2, self.z),
                (self.z * 2 + self.x, self.z),
            ),
            id='back_bottom_tab',
            ** LINE_STYLE,
            ))

        grp.add(drw.rect(
            (0, self.z),
            (self.z, self.y),
            id='right_side',
            ** LINE_STYLE,
            ))
        grp.add(drw.rect(
            (self.z, self.z),
            (self.x, self.y),
            id='front',
            ** LINE_STYLE,
            ))
        grp.add(drw.rect(
            (self.x + self.z, self.z),
            (self.z, self.y),
            id='left_side',
            ** LINE_STYLE,
            ))
        # TODO: remove the semicircle from the rect
        grp.add(drw.rect(
            (self.x + self.z * 2, self.z),
            (self.x, self.y),
            id='back',
            ** LINE_STYLE,
            ))
        grp.add(drw.ellipse(
            (self.x + self.z * 2 + self.x / 2, self.z + self.y),
            (8 * mm, 8 * mm),
            id='back_cut',
            ** LINE_STYLE,
            ))
        grp.add(drw.polygon(
            (
                (self.x * 2 + self.z * 2, self.z),
                (self.x * 2 + self.z * 2 + self.t, self.z + 3 * mm),
                (self.x * 2 + self.z * 2 + self.t, self.z + self.y - 3 * mm),
                (self.x * 2 + self.z * 2, self.z + self.y),
            ),
            id='side_tab',
            ** LINE_STYLE,
            ))

        grp.add(drw.polygon(
            (
                (0, self.z + self.y),
                (self.z, self.z + self.y),
                (self.z - 3 * mm, self.z + self.y + self.t),
                (3 * mm, self.z + self.y + self.t),
            ),
            id='right_side_top_tab',
            ** LINE_STYLE,
            ))
        grp.add(drw.rect(
            (self.z, self.z + self.y),
            (self.x, self.z),
            id='top',
            ** LINE_STYLE,
            ))
        grp.add(drw.polygon(
            (
                (self.z + self.x, self.z + self.y),
                (self.z * 2 + self.x, self.z + self.y),
                (self.z * 2 + self.x - 3 * mm, self.z + self.y + self.t),
                (self.z + self.x + 3 * mm, self.z + self.y + self.t),
            ),
            id='left_side_top_tab',
            ** LINE_STYLE,
            ))

        # TODO: make this an half-ellipse
        grp.add(drw.ellipse(
            (self.z + self.x / 2, self.z * 2 + self.y),
            (self.x / 2, self.f),
            id='top_tab',
            ** LINE_STYLE,
            ))
        grp.add(drw.rect(
            (self.z, self.z + self.y + self.z - self.f - 1),
            (self.x, self.f + 1),
            id='top_tab_excess',
            ** LINE_STYLE,
            ))

        grp.rotate(
            180,
            center=(canvas_x / 2, canvas_y / 2),
            )
        grp.translate(
            (canvas_x - self.full_width) / 2,
            (canvas_y - self.full_height) / 2
        )
        drw.add(grp)


class Command():
    """
    Generate an SVG with the basic shape of a tuckbox for gaming cards.
    """

    def get_parser(self):
        desc = _get_first_docstring_line(self)
        parser = argparse.ArgumentParser(description=desc)
        parser.add_argument(
            "-x", "--size-x",
            default=60,
            type=int,
            help="Internal width of the box.",
        )
        parser.add_argument(
            "-y", "--size-y",
            default=94,
            type=int,
            help="Internal height of the box.",
        )
        parser.add_argument(
            "-z", "--size-z",
            default=42,
            type=int,
            help="Internal thickness of the box.",
        )
        parser.add_argument(
            "-o", "--output",
            default="tuckbox",
            help="Base output file name."
        )
        return parser

    def main(self):
        self.parser = self.get_parser()
        if argcomplete:
            argcomplete.autocomplete(self.parser)
        self.args = self.parser.parse_args()

        size_spec = "{}×{}×{}".format(
            self.args.size_x,
            self.args.size_y,
            self.args.size_z,
        )
        dest = svgwrite.Drawing(
            filename="{}-{}.svg".format(self.args.output, size_spec),
            size=('297mm', '210mm'),
            profile='full',
            )
        struc = Structure(self.args.size_x, self.args.size_y, self.args.size_z)
        struc.add_to(dest)
        dest.save()


if __name__ == '__main__':
    Command().main()