|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Nielsiano\DmplBuilder package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Niels Stampe <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Nielsiano\DmplBuilder; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class DmplBuilder |
|
16
|
|
|
* |
|
17
|
|
|
* @package Nielsiano\DmplBuilder |
|
18
|
|
|
*/ |
|
19
|
|
|
class DmplBuilder implements PlotBuilder |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Generated DM/PL command instructions. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $instructions = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var bool |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $cutOff = false; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var bool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $flipAxes = false; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $measuringUnit = 'M'; |
|
43
|
|
|
|
|
44
|
|
|
const KISS_CUT = 50; |
|
45
|
|
|
const FLEXCUT_PEN = 6; |
|
46
|
|
|
const REGULAR_PEN = 0; |
|
47
|
|
|
const CUT_THROUGH = 100; |
|
48
|
|
|
const MEASURING_UNITS = [1, 2, 3, 4, 5, 'M']; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Adds a new plot of x and y to machine instructions. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function plot(int $x, int $y): PlotBuilder |
|
54
|
|
|
{ |
|
55
|
|
|
array_map([$this, 'pushCommand'], $this->flipAxes ? [$y, $x] : [$x, $y]); |
|
56
|
|
|
|
|
57
|
3 |
|
return $this; |
|
58
|
|
|
} |
|
59
|
3 |
|
|
|
60
|
|
|
/** |
|
61
|
3 |
|
* Changes the pen of the plotter. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function changePen(int $pen): PlotBuilder |
|
64
|
|
|
{ |
|
65
|
|
|
if (! in_array($pen, range(0, 6))) { |
|
66
|
|
|
throw new \InvalidArgumentException(sprintf('[%d] is not a valid Pen.', $pen)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->pushCommand(sprintf('P%d;', $pen)); |
|
70
|
5 |
|
} |
|
71
|
|
|
|
|
72
|
5 |
|
/** |
|
73
|
1 |
|
* Compiles a string in DMPL format with machine instructions. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function compile(): string |
|
76
|
4 |
|
{ |
|
77
|
|
|
$init = sprintf(';: EC%s,U H L0,A100,100,R,', $this->measuringUnit); |
|
78
|
|
|
|
|
79
|
|
|
$this->pushCommand($this->cutOff ? ';:c,e' : 'e'); |
|
80
|
|
|
|
|
81
|
|
|
return $init . implode(',', $this->instructions); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
15 |
|
/** |
|
85
|
|
|
* Alias for `compile()` (for backwards compatibility). |
|
86
|
15 |
|
*/ |
|
87
|
|
|
public function compileDmpl(): string |
|
88
|
15 |
|
{ |
|
89
|
|
|
return $this->compile(); |
|
90
|
15 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Pushes a command to the instructions. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function pushCommand(string $command): PlotBuilder |
|
96
|
|
|
{ |
|
97
|
|
|
$this->instructions[] = $command; |
|
98
|
|
|
|
|
99
|
15 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
15 |
|
|
|
102
|
|
|
/** |
|
103
|
15 |
|
* Lifts the pen up. |
|
104
|
|
|
*/ |
|
105
|
|
|
public function penUp(): PlotBuilder |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->pushCommand('U'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
2 |
|
* Pushes the pen down on paper. |
|
112
|
|
|
*/ |
|
113
|
2 |
|
public function penDown(): PlotBuilder |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->pushCommand('D'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Changes the plotter pen to use flexcut. |
|
120
|
|
|
*/ |
|
121
|
2 |
|
public function flexCut(): PlotBuilder |
|
122
|
|
|
{ |
|
123
|
2 |
|
return $this->changePen(self::FLEXCUT_PEN); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Change to the regular plotter pen. |
|
128
|
|
|
*/ |
|
129
|
|
|
public function regularCut(): PlotBuilder |
|
130
|
|
|
{ |
|
131
|
1 |
|
return $this->changePen(self::REGULAR_PEN); |
|
132
|
|
|
} |
|
133
|
1 |
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Changes the pen pressure in gram. |
|
136
|
|
|
*/ |
|
137
|
|
|
public function pressure(int $gramPressure): PlotBuilder |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->pushCommand(sprintf('BP%d;', $gramPressure)); |
|
140
|
|
|
} |
|
141
|
2 |
|
|
|
142
|
|
|
/** |
|
143
|
2 |
|
* Specifies measuring unit. |
|
144
|
|
|
* 1 selects 0.001 inch |
|
145
|
|
|
* 5 selects 0.005 inch |
|
146
|
|
|
* M selects 0.1 mm |
|
147
|
|
|
*/ |
|
148
|
|
|
public function setMeasuringUnit($unit): PlotBuilder |
|
149
|
|
|
{ |
|
150
|
|
|
if (! in_array($unit, self::MEASURING_UNITS)) { |
|
151
|
|
|
throw new \InvalidArgumentException(sprintf('[%s] is not a valid measuring unit.', $unit)); |
|
152
|
1 |
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
$this->measuringUnit = $unit; |
|
155
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Changes the plotter velocity. |
|
161
|
|
|
*/ |
|
162
|
|
|
public function velocity(int $velocity): PlotBuilder |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->pushCommand(sprintf('V%d;', $velocity)); |
|
165
|
|
|
} |
|
166
|
2 |
|
|
|
167
|
|
|
/** |
|
168
|
2 |
|
* Flips the x, y coordinates. |
|
169
|
1 |
|
*/ |
|
170
|
|
|
public function flipAxes(): PlotBuilder |
|
171
|
|
|
{ |
|
172
|
1 |
|
$this->flipAxes = true; |
|
173
|
|
|
|
|
174
|
1 |
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Cuts off paper when a operation finishes. |
|
179
|
|
|
*/ |
|
180
|
|
|
public function cutOff(): PlotBuilder |
|
181
|
|
|
{ |
|
182
|
|
|
$this->cutOff = true; |
|
183
|
1 |
|
|
|
184
|
|
|
return $this; |
|
185
|
1 |
|
} |
|
186
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|