summaryrefslogtreecommitdiffstats
path: root/share/extensions/hpgl_encoder.py
blob: f6a30f3ae8b1e0e37b07c612a3fb8779ba6b3eaf (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# coding=utf-8
#
# Copyright (C) 2008 Aaron Spike, aaron@ekips.org
#
# 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.
#
"""
Base class for HGPL Encoding
"""

import re
import math

import inkex
from inkex.transforms import Transform, DirectedLineSegment, Vector2d
from inkex.bezier import cspsubdiv


class NoPathError(ValueError):
    """Raise that paths not selected"""


# Find the pen number in the layer number
FIND_PEN = re.compile(r"\s*pen\s*(\d+)\s*", re.IGNORECASE)
# Find the pen speed in the layer number
FIND_SPEED = re.compile(r"\s*speed\s*(\d+)\s*", re.IGNORECASE)
# Find pen force in the layer name
FIND_FORCE = re.compile(r"\s*force\s*(\d+)\s*", re.IGNORECASE)


class hpglEncoder(object):
    """HPGL Encoder, used by others"""

    def __init__(self, effect):
        """options:
        "resolutionX":float
        "resolutionY":float
        "pen":int
        "force:int
        "speed:int
        "orientation":string // "0", "90", "-90", "180"
        "mirrorX":bool
        "mirrorY":bool
        "center":bool
        "flat":float
        "overcut":float
        "toolOffset":float
        "precut":bool
        "autoAlign":bool
        """
        self.options = effect.options
        self.doc = effect.svg
        self.docWidth = effect.svg.viewbox_width
        self.docHeight = effect.svg.viewbox_height
        self.hpgl = ""
        self.divergenceX = "False"
        self.divergenceY = "False"
        self.sizeX = "False"
        self.sizeY = "False"
        self.dryRun = True
        self.lastPoint = [0, 0, 0]
        self.lastPen = -1
        self.lastSpeed = -1
        self.lastForce = -1
        self.offsetX = 0
        self.offsetY = 0
        # dots per inch to dots per user unit:

        self.scaleX = self.options.resolutionX / effect.svg.viewport_to_unit("1.0in")
        self.scaleY = self.options.resolutionY / effect.svg.viewport_to_unit("1.0in")
        scaleXY = (self.scaleX + self.scaleY) / 2

        # mm to dots (plotter coordinate system):
        self.overcut = (
            effect.svg.viewport_to_unit(str(self.options.overcut) + "mm") * scaleXY
        )
        self.toolOffset = (
            effect.svg.viewport_to_unit(str(self.options.toolOffset) + "mm") * scaleXY
        )

        # scale flatness to resolution:
        self.flat = self.options.flat / (
            1016 / ((self.options.resolutionX + self.options.resolutionY) / 2)
        )
        if self.toolOffset > 0.0:
            self.toolOffsetFlat = (
                self.flat / self.toolOffset * 4.5
            )  # scale flatness to offset
        else:
            self.toolOffsetFlat = 0.0
        self.mirrorX = -1.0 if self.options.mirrorX else 1.0
        self.mirrorY = 1.0 if self.options.mirrorY else -1.0
        # process viewBox attribute to correct page scaling
        self.viewBoxTransformX = 1
        self.viewBoxTransformY = 1
        viewBox = effect.svg.get_viewbox()
        if viewBox and viewBox[2] and viewBox[3]:
            self.viewBoxTransformX = self.docWidth / effect.svg.viewport_to_unit(
                effect.svg.add_unit(viewBox[2])
            )
            self.viewBoxTransformY = self.docHeight / effect.svg.viewport_to_unit(
                effect.svg.add_unit(viewBox[3])
            )

    def getHpgl(self):
        """Return the HPGL instructions"""
        # dryRun to find edges
        transform = Transform(
            [
                [self.mirrorX * self.scaleX * self.viewBoxTransformX, 0.0, 0.0],
                [0.0, self.mirrorY * self.scaleY * self.viewBoxTransformY, 0.0],
            ]
        )
        transform.add_rotate(int(self.options.orientation))

        self.vData = [
            ["", "False", 0],
            ["", "False", 0],
            ["", "False", 0],
            ["", "False", 0],
        ]
        self.process_group(self.doc, transform)
        if (
            self.divergenceX == "False"
            or self.divergenceY == "False"
            or self.sizeX == "False"
            or self.sizeY == "False"
        ):
            raise NoPathError("No paths found")
        # live run
        self.dryRun = False
        # move drawing according to various modifiers
        if self.options.autoAlign:
            if self.options.center:
                self.offsetX -= (self.sizeX - self.divergenceX) / 2
                self.offsetY -= (self.sizeY - self.divergenceY) / 2
        else:
            self.divergenceX = 0.0
            self.divergenceY = 0.0
            if self.options.center:
                if self.options.orientation == "0":
                    self.offsetX -= (self.docWidth * self.scaleX) / 2
                    self.offsetY += (self.docHeight * self.scaleY) / 2
                if self.options.orientation == "90":
                    self.offsetY += (self.docWidth * self.scaleX) / 2
                    self.offsetX += (self.docHeight * self.scaleY) / 2
                if self.options.orientation == "180":
                    self.offsetX += (self.docWidth * self.scaleX) / 2
                    self.offsetY -= (self.docHeight * self.scaleY) / 2
                if self.options.orientation == "270":
                    self.offsetY -= (self.docWidth * self.scaleX) / 2
                    self.offsetX -= (self.docHeight * self.scaleY) / 2
            else:
                if self.options.orientation == "0":
                    self.offsetY += self.docHeight * self.scaleY
                if self.options.orientation == "90":
                    self.offsetY += self.docWidth * self.scaleX
                    self.offsetX += self.docHeight * self.scaleY
                if self.options.orientation == "180":
                    self.offsetX += self.docWidth * self.scaleX
        if not self.options.center and self.toolOffset > 0.0:
            self.offsetX += self.toolOffset
            self.offsetY += self.toolOffset

        # initialize transformation matrix and cache
        transform = Transform(
            [
                [
                    self.mirrorX * self.scaleX * self.viewBoxTransformX,
                    0.0,
                    -float(self.divergenceX) + self.offsetX,
                ],
                [
                    0.0,
                    self.mirrorY * self.scaleY * self.viewBoxTransformY,
                    -float(self.divergenceY) + self.offsetY,
                ],
            ]
        )
        transform.add_rotate(int(self.options.orientation))
        self.vData = [
            ["", "False", 0],
            ["", "False", 0],
            ["", "False", 0],
            ["", "False", 0],
        ]
        # add move to zero point and precut
        if self.toolOffset > 0.0 and self.options.precut:
            if self.options.center:
                # position precut outside of drawing plus one time the tooloffset
                if self.offsetX >= 0.0:
                    precutX = self.offsetX + self.toolOffset
                else:
                    precutX = self.offsetX - self.toolOffset
                if self.offsetY >= 0.0:
                    precutY = self.offsetY + self.toolOffset
                else:
                    precutY = self.offsetY - self.toolOffset
                self.processOffset(
                    "PU",
                    Vector2d(precutX, precutY),
                    self.options.pen,
                    self.options.speed,
                    self.options.force,
                )
                self.processOffset(
                    "PD",
                    Vector2d(precutX, precutY + self.toolOffset * 8),
                    self.options.pen,
                    self.options.speed,
                    self.options.force,
                )
            else:
                self.processOffset(
                    "PU",
                    Vector2d(0, 0),
                    self.options.pen,
                    self.options.speed,
                    self.options.force,
                )
                self.processOffset(
                    "PD",
                    Vector2d(0, self.toolOffset * 8),
                    self.options.pen,
                    self.options.speed,
                    self.options.force,
                )
        # start conversion
        self.process_group(self.doc, transform)
        # shift an empty node in in order to process last node in cache
        if self.toolOffset > 0.0 and not self.dryRun:
            self.processOffset("PU", Vector2d(0, 0), 0, 0, 0)
        return self.hpgl

    def process_group(self, group, transform):
        """flatten layers and groups to avoid recursion"""
        for child in group:
            if not isinstance(child, inkex.ShapeElement):
                continue
            if child.is_visible():
                if isinstance(child, inkex.Group):
                    self.process_group(child, transform)
                elif isinstance(child, inkex.PathElement):
                    self.process_path(child, transform)
                else:
                    # This only works for shape elements (not text yet!)
                    new_elem = child.replace_with(child.to_path_element())
                    # Element is given composed transform b/c it's not added back to doc
                    new_elem.transform = child.composed_transform()
                    self.process_path(new_elem, transform)

    def get_pen_number(self, node):
        """Get pen number for node label (usually group)"""
        for parent in [node] + list(node.ancestors()):
            match = FIND_PEN.search(parent.label or "")
            if match:
                return int(match.group(1))
        return int(self.options.pen)

    def get_pen_speed(self, node):
        """Get pen speed for node label (usually group)"""
        for parent in [node] + list(node.ancestors()):
            match = FIND_SPEED.search(parent.label or "")
            if match:
                return int(match.group(1))
        return int(self.options.speed)

    def get_pen_force(self, node):
        """Get pen force for node label (usually group)"""
        for parent in [node] + list(node.ancestors()):
            match = FIND_FORCE.search(parent.label or "")
            if match:
                return int(match.group(1))
        return int(self.options.force)

    def process_path(self, node, transform):
        """Process the given element into a plotter path"""
        pen = self.get_pen_number(node)
        speed = self.get_pen_speed(node)
        force = self.get_pen_force(node)

        path = (
            node.path.to_absolute()
            .transform(node.composed_transform())
            .transform(transform)
            .to_superpath()
        )
        if path:
            cspsubdiv(path, self.flat)
            # path to HPGL commands
            oldPosX = 0.0
            oldPosY = 0.0
            for singlePath in path:
                cmd = "PU"
                for singlePathPoint in singlePath:
                    posX, posY = singlePathPoint[1]
                    # check if point is repeating, if so, ignore
                    if int(round(posX)) != int(round(oldPosX)) or int(
                        round(posY)
                    ) != int(round(oldPosY)):
                        self.processOffset(cmd, Vector2d(posX, posY), pen, speed, force)
                        cmd = "PD"
                        oldPosX = posX
                        oldPosY = posY
                # perform overcut
                if self.overcut > 0.0 and not self.dryRun:
                    # check if last and first points are the same, otherwise the path
                    # is not closed and no overcut can be performed
                    if int(round(oldPosX)) == int(round(singlePath[0][1][0])) and int(
                        round(oldPosY)
                    ) == int(round(singlePath[0][1][1])):
                        overcutLength = 0
                        for singlePathPoint in singlePath:
                            posX, posY = singlePathPoint[1]
                            # check if point is repeating, if so, ignore
                            if int(round(posX)) != int(round(oldPosX)) or int(
                                round(posY)
                            ) != int(round(oldPosY)):
                                overcutLength += (
                                    Vector2d(posX, posY) - (oldPosX, oldPosY)
                                ).length
                                if overcutLength >= self.overcut:
                                    newEndPoint = self.changeLength(
                                        Vector2d(oldPosX, oldPosY),
                                        Vector2d(posX, posY),
                                        -(overcutLength - self.overcut),
                                    )
                                    self.processOffset(
                                        cmd, newEndPoint, pen, speed, force
                                    )
                                    break
                                self.processOffset(
                                    cmd, Vector2d(posX, posY), pen, speed, force
                                )
                                oldPosX = posX
                                oldPosY = posY

    def changeLength(self, p1, p2, offset):
        """change length of line"""
        if p1.x == p2.x and p1.y == p2.y:  # abort if points are the same
            return p1
        return Vector2d(DirectedLineSegment(p2, p1).point_at_length(-offset))

    def processOffset(self, cmd, point, pen, speed, force):
        """Calculate offset correction"""
        if self.toolOffset == 0.0 or self.dryRun:
            self.storePoint(cmd, point, pen, speed, force)
        else:
            # insert data into cache
            self.vData.pop(0)
            self.vData.insert(3, [cmd, point, pen, speed, force])
            # decide if enough data is available
            if self.vData[2][1] != "False":
                if self.vData[1][1] == "False":
                    self.storePoint(
                        self.vData[2][0],
                        self.vData[2][1],
                        self.vData[2][2],
                        self.vData[2][3],
                        self.vData[2][4],
                    )
                else:
                    # perform tool offset correction (It's a *tad* complicated, if you want
                    #                     to understand it draw the data as lines on paper)
                    if self.vData[2][0] == "PD":
                        # If the 3rd entry in the cache is a pen down command,
                        #             make the line longer by the tool offset
                        pointThree = self.changeLength(
                            self.vData[1][1], self.vData[2][1], self.toolOffset
                        )
                        self.storePoint(
                            "PD",
                            pointThree,
                            self.vData[2][2],
                            self.vData[2][3],
                            self.vData[2][4],
                        )
                    elif self.vData[0][1] != "False":
                        # Elif the 1st entry in the cache is filled with data and the 3rd entry
                        #   is a pen up command shift the 3rd entry by the current tool offset
                        #   position according to the 2nd command
                        pointThree = self.changeLength(
                            self.vData[0][1], self.vData[1][1], self.toolOffset
                        )
                        pointThree = self.vData[2][1] - (self.vData[1][1] - pointThree)
                        self.storePoint(
                            "PU",
                            pointThree,
                            self.vData[2][2],
                            self.vData[2][3],
                            self.vData[2][4],
                        )
                    else:
                        # Else just write the 3rd entry
                        pointThree = self.vData[2][1]
                        self.storePoint(
                            "PU",
                            pointThree,
                            self.vData[2][2],
                            self.vData[2][3],
                            self.vData[2][4],
                        )
                    if self.vData[3][0] == "PD":
                        # If the 4th entry in the cache is a pen down command guide tool to next
                        #           line with a circle between the prolonged 3rd and 4th entry
                        originalSegment = DirectedLineSegment(
                            self.vData[2][1], self.vData[3][1]
                        )
                        if originalSegment.length >= self.toolOffset:
                            pointFour = self.changeLength(
                                originalSegment.end,
                                originalSegment.start,
                                -self.toolOffset,
                            )
                        else:
                            pointFour = self.changeLength(
                                originalSegment.start,
                                originalSegment.end,
                                self.toolOffset - originalSegment.length,
                            )
                        # get angle start and angle vector
                        angleStart = DirectedLineSegment(
                            self.vData[2][1], pointThree
                        ).angle
                        angleVector = (
                            DirectedLineSegment(self.vData[2][1], pointFour).angle
                            - angleStart
                        )
                        # switch direction when arc is bigger than 180°
                        if angleVector > math.pi:
                            angleVector -= math.pi * 2
                        elif angleVector < -math.pi:
                            angleVector += math.pi * 2
                        # draw arc
                        if angleVector >= 0:
                            angle = angleStart + self.toolOffsetFlat
                            while angle < angleStart + angleVector:
                                self.storePoint(
                                    "PD",
                                    self.vData[2][1]
                                    + self.toolOffset
                                    * Vector2d(math.cos(angle), math.sin(angle)),
                                    self.vData[2][2],
                                    self.vData[2][3],
                                    self.vData[2][4],
                                )
                                angle += self.toolOffsetFlat
                        else:
                            angle = angleStart - self.toolOffsetFlat
                            while angle > angleStart + angleVector:
                                self.storePoint(
                                    "PD",
                                    self.vData[2][1]
                                    + self.toolOffset
                                    * Vector2d(math.cos(angle), math.sin(angle)),
                                    self.vData[2][2],
                                    self.vData[2][3],
                                    self.vData[2][4],
                                )
                                angle -= self.toolOffsetFlat
                        self.storePoint(
                            "PD",
                            pointFour,
                            self.vData[3][2],
                            self.vData[2][3],
                            self.vData[2][4],
                        )

    def storePoint(self, command, point, pen, speed, force):
        x = int(round(point.x))
        y = int(round(point.y))
        # skip when no change in movement
        if (
            self.lastPoint[0] == command
            and self.lastPoint[1] == x
            and self.lastPoint[2] == y
        ):
            return
        if self.dryRun:
            # find edges
            if self.divergenceX == "False" or x < self.divergenceX:
                self.divergenceX = x
            if self.divergenceY == "False" or y < self.divergenceY:
                self.divergenceY = y
            if self.sizeX == "False" or x > self.sizeX:
                self.sizeX = x
            if self.sizeY == "False" or y > self.sizeY:
                self.sizeY = y
        else:
            # store point
            if not self.options.center:
                # only positive values are allowed (usually)
                if x < 0:
                    x = 0
                if y < 0:
                    y = 0
            # select correct pen
            if self.lastPen != pen:
                self.hpgl += ";PU;SP%d" % pen
            if self.lastSpeed != speed:
                if speed > 0:
                    self.hpgl += ";VS%d" % speed
            if self.lastForce != force:
                if force > 0:
                    self.hpgl += ";FS%d" % force
            # do not repeat command
            if command == "PD" and self.lastPoint[0] == "PD" and self.lastPen == pen:
                self.hpgl += ",%d,%d" % (x, y)
            else:
                self.hpgl += ";%s%d,%d" % (command, x, y)
            self.lastPen = pen
            self.lastSpeed = speed
            self.lastForce = force
        self.lastPoint = [command, x, y]