summaryrefslogtreecommitdiffstats
path: root/share/extensions/foldablebox.py
blob: 06e3e545b18224048893607931b52940382d0195 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#! /usr/bin/env python
# coding=utf-8
#
# Copyright (C) 2009 Aurelio A. Heckert <aurium (a) gmail dot com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

__version__ = "0.2"

import inkex

class FoldableBox(inkex.EffectExtension):
    """Foldable Box generation."""
    def add_arguments(self, pars):
        pars.add_argument("--width", type=float, default=10.0, help="The Box Width")
        pars.add_argument("--height", type=float, default=15.0, help="The Box Height")
        pars.add_argument("--depth", type=float, default=3.0, help="The Box Depth (z dimention)")
        pars.add_argument("--unit", default="cm", help="The unit of the box dimensions")
        pars.add_argument("--proportion", type=float, default=0.6, help="Inner tab proportion")
        pars.add_argument("--guide", type=inkex.Boolean, default=False, help="Add guide lines")

    def guide(self, value, orient):
        """Create a guideline conditionally"""
        if self.options.guide:
            self.svg.namedview.new_guide(value, orient)

    def effect(self):
        doc_w = self.svg.unittouu(self.document.getroot().get('width'))
        doc_h = self.svg.unittouu(self.document.getroot().get('height'))

        box_w = self.svg.unittouu(str(self.options.width) + self.options.unit)
        box_h = self.svg.unittouu(str(self.options.height) + self.options.unit)
        box_d = self.svg.unittouu(str(self.options.depth) + self.options.unit)
        tab_h = box_d * self.options.proportion

        box_id = self.svg.get_unique_id('box')
        group = self.svg.get_current_layer().add(inkex.Group(id=box_id))

        line_style = {'stroke': '#000000', 'fill': 'none',
                      'stroke-width': str(self.svg.unittouu('1px'))}

        self.guide(doc_h, True)

        # Inner Close Tab
        line = group.add(inkex.PathElement(id=box_id + '-inner-close-tab'))
        line.path = [
            ['M', [box_w - (tab_h * 0.7), 0]],
            ['C', [box_w - (tab_h * 0.25), 0, box_w, tab_h * 0.3, box_w, tab_h * 0.9]],
            ['L', [box_w, tab_h]],
            ['L', [0, tab_h]],
            ['L', [0, tab_h * 0.9]],
            ['C', [0, tab_h * 0.3, tab_h * 0.25, 0, tab_h * 0.7, 0]],
            ['Z', []]
        ]
        line.style = line_style

        lower_pos = box_d + tab_h
        left_pos = 0

        self.guide(doc_h - tab_h, True)

        # Upper Close Tab
        line = group.add(inkex.PathElement(id=box_id + '-upper-close-tab'))
        line.path = [
            ['M', [left_pos, tab_h]],
            ['L', [left_pos + box_w, tab_h]],
            ['L', [left_pos + box_w, lower_pos]],
            ['L', [left_pos + 0, lower_pos]],
            ['Z', []]
        ]
        line.style = line_style

        left_pos += box_w

        # Upper Right Tab
        side_tab_h = lower_pos - (box_w / 2)
        if side_tab_h < tab_h:
            side_tab_h = tab_h

        line = group.add(inkex.PathElement(id=box_id + '-upper-right-tab'))
        line.path = [
            ['M', [left_pos, side_tab_h]],
            ['L', [left_pos + (box_d * 0.8), side_tab_h]],
            ['L', [left_pos + box_d, ((lower_pos * 3) - side_tab_h) / 3]],
            ['L', [left_pos + box_d, lower_pos]],
            ['L', [left_pos + 0, lower_pos]],
            ['Z', []]
        ]
        line.style = line_style

        left_pos += box_w + box_d

        # Upper Left Tab
        line = group.add(inkex.PathElement(id=box_id + '-upper-left-tab'))
        line.path = [
            ['M', [left_pos + box_d, side_tab_h]],
            ['L', [left_pos + (box_d * 0.2), side_tab_h]],
            ['L', [left_pos, ((lower_pos * 3) - side_tab_h) / 3]],
            ['L', [left_pos, lower_pos]],
            ['L', [left_pos + box_d, lower_pos]],
            ['Z', []]
        ]
        line.style = line_style

        left_pos = 0

        self.guide(doc_h - tab_h - box_d, True)

        # Right Tab
        line = group.add(inkex.PathElement(id=box_id + '-left-tab'))
        line.path = [
            ['M', [left_pos, lower_pos]],
            ['L', [left_pos - (box_d / 2), lower_pos + (box_d / 4)]],
            ['L', [left_pos - (box_d / 2), lower_pos + box_h - (box_d / 4)]],
            ['L', [left_pos, lower_pos + box_h]],
            ['Z', []]
        ]
        line.style = line_style

        # Front
        line = group.add(inkex.PathElement(id=box_id + '-front'))
        line.path = [
            ['M', [left_pos, lower_pos]],
            ['L', [left_pos + box_w, lower_pos]],
            ['L', [left_pos + box_w, lower_pos + box_h]],
            ['L', [left_pos, lower_pos + box_h]],
            ['Z', []]
        ]
        line.style = line_style

        left_pos += box_w

        # Right
        line = group.add(inkex.PathElement(id=box_id + '-right'))
        line.path = [
            ['M', [left_pos, lower_pos]],
            ['L', [left_pos + box_d, lower_pos]],
            ['L', [left_pos + box_d, lower_pos + box_h]],
            ['L', [left_pos, lower_pos + box_h]],
            ['Z', []]
        ]
        line.style = line_style

        left_pos += box_d

        # Back
        line = group.add(inkex.PathElement(id=box_id + '-back'))
        line.path = [
            ['M', [left_pos, lower_pos]],
            ['L', [left_pos + box_w, lower_pos]],
            ['L', [left_pos + box_w, lower_pos + box_h]],
            ['L', [left_pos, lower_pos + box_h]],
            ['Z', []]
        ]
        line.style = line_style

        left_pos += box_w

        # Left
        line = group.add(inkex.PathElement(id=box_id + '-line'))
        line.path = [
            ['M', [left_pos, lower_pos]],
            ['L', [left_pos + box_d, lower_pos]],
            ['L', [left_pos + box_d, lower_pos + box_h]],
            ['L', [left_pos, lower_pos + box_h]],
            ['Z', []]
        ]
        line.style = line_style

        lower_pos += box_h
        left_pos = 0
        b_tab = lower_pos + box_d
        if b_tab > box_w / 2.5:
            b_tab = box_w / 2.5

        # Bottom Front Tab
        line = group.add(inkex.PathElement(id=box_id + '-bottom-front-tab'))
        line.path = [
            ['M', [left_pos, lower_pos]],
            ['L', [left_pos, lower_pos + (box_d / 2)]],
            ['L', [left_pos + box_w, lower_pos + (box_d / 2)]],
            ['L', [left_pos + box_w, lower_pos]],
            ['Z', []]
        ]
        line.style = line_style

        left_pos += box_w

        # Bottom Right Tab
        line = group.add(inkex.PathElement(id=box_id + '-bottom-right-tab'))
        line.path = [
            ['M', [left_pos, lower_pos]],
            ['L', [left_pos, lower_pos + b_tab]],
            ['L', [left_pos + box_d, lower_pos + b_tab]],
            ['L', [left_pos + box_d, lower_pos]],
            ['Z', []]
        ]
        line.style = line_style

        left_pos += box_d

        # Bottom Back Tab
        line = group.add(inkex.PathElement(id=box_id + '-bottom-back-tab'))
        line.path = [
            ['M', [left_pos, lower_pos]],
            ['L', [left_pos, lower_pos + (box_d / 2)]],
            ['L', [left_pos + box_w, lower_pos + (box_d / 2)]],
            ['L', [left_pos + box_w, lower_pos]],
            ['Z', []]
        ]
        line.style = line_style

        left_pos += box_w

        # Bottom Left Tab
        line = group.add(inkex.PathElement(id=box_id + '-bottom-left-tab'))
        line.path = [
            ['M', [left_pos, lower_pos]],
            ['L', [left_pos, lower_pos + b_tab]],
            ['L', [left_pos + box_d, lower_pos + b_tab]],
            ['L', [left_pos + box_d, lower_pos]],
            ['Z', []]
        ]
        line.style = line_style

        left_pos += box_d
        lower_pos += b_tab

        group.transform = inkex.Transform(translate=((doc_w - left_pos) / 2, (doc_h - lower_pos) / 2))


if __name__ == '__main__':  # pragma: no cover
    FoldableBox().run()