Skip to content

Commit 2200143

Browse files
committed
- Added scale_x/y and offset_x/y
- Removed invert_x/y - Fixed issue where gwrite was modifying the pipeline's content
1 parent 35d7f4b commit 2200143

2 files changed

Lines changed: 29 additions & 24 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ You can use the following options inside a profile. You only need to provide the
7474

7575

7676
### Output Control
77+
These parameters define the transformation between *vpype*'s and the target's coordinate systems. *vpype* coordinate is based on CSS pixels (1/96th of an inch), has origin at the top-left corner with positive X value extending right and positive Y values extending downward.
7778
- `unit`: Defines the [vpype unit](https://vpype.readthedocs.io/en/stable/fundamentals.html#units) which should be used in the output format. Defaults to `mm`.
78-
- `invert_x`: Inverts/Mirrors the output relative to the middle point of the x axis when set to `true`. Defaults to `false`.
79-
- `invert_y`: Inverts/Mirrors the output relative to the middle point of the y axis when set to `true`. Defaults to `false`. This option can be helpful if your output does not have the x=0, y=0 coordinates at the top left (the default) but instead at the bottom left.
79+
- `scale_x`: Apply a scaling factor on the X axis. Use `-1` to invert the direction.
80+
- `scale_y`: Apply a scaling factor on the Y axis. Use `-1` to invert the direction.
81+
- `offset_x`: Apply an offset to the X axis. This offset is expressed in the unit defined by `unit`.
82+
- `offset_y`: Apply an offset to the Y axis. This offset is expressed in the unit defined by `unit`.
8083

8184
### Output Format
8285
All of the options below default to an empty text which means no output is generated. However, if `segment_first` or `segment_last` is omitted the code from `segment` is used. If there is only one segment. `segment_first` takes priority over `segment_last`.

vpype_gcode/gwrite.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import click
1+
import copy
2+
import typing
23
from pathlib import Path
4+
5+
import click
36
import vpype as vp
4-
import typing
5-
from vpype.layers import LayerType
67

78
# Load the default config
89
vp.CONFIG_MANAGER.load_config_file(str(Path(__file__).parent / "bundled_configs.toml"))
@@ -16,9 +17,7 @@ def invert_axis(document: vp.Document, invert_x: bool, invert_y: bool):
1617
the center of the bounds.
1718
"""
1819

19-
layer_ids = vp.multiple_to_layer_ids(LayerType.ALL, document)
20-
bounds = document.bounds(layer_ids)
21-
20+
bounds = document.bounds()
2221
if not bounds:
2322
raise ValueError("no geometry available, cannot compute origin")
2423

@@ -27,11 +26,9 @@ def invert_axis(document: vp.Document, invert_x: bool, invert_y: bool):
2726
0.5 * (bounds[1] + bounds[3]),
2827
)
2928

30-
for vid in layer_ids:
31-
lc = document[vid]
32-
lc.translate(-origin[0], -origin[1])
33-
lc.scale(-1 if invert_x else 1, -1 if invert_y else 1)
34-
lc.translate(origin[0], origin[1])
29+
document.translate(-origin[0], -origin[1])
30+
document.scale(-1 if invert_x else 1, -1 if invert_y else 1)
31+
document.translate(origin[0], origin[1])
3532

3633
return document
3734

@@ -88,13 +85,19 @@ def gwrite(document: vp.Document, output: typing.TextIO, profile: str):
8885
segment_last = config.get("segment_last", None)
8986
unit = config.get("unit", "mm")
9087

91-
invert_x = config.get("invert_x", False)
92-
invert_y = config.get("invert_y", False)
88+
offset_x = config.get("offset_x", 0.0)
89+
offset_y = config.get("offset_y", 0.0)
90+
scale_x = config.get("scale_x", 1.0)
91+
scale_y = config.get("scale_y", 1.0)
9392

94-
scale = 1 / vp.convert_length(unit)
93+
# transform the document according to the desired parameters
94+
orig_document = document
95+
document = copy.deepcopy(document) # do NOT affect the pipeline's document
96+
unit_scale = vp.convert_length(unit)
97+
document.scale(scale_x / unit_scale, scale_y / unit_scale)
98+
document.translate(offset_x, offset_y)
9599

96-
if invert_x or invert_y:
97-
document = invert_axis(document, invert_x, invert_y)
100+
# process file
98101
filename = output.name
99102
if document_start is not None:
100103
output.write(document_start.format(filename=filename))
@@ -107,12 +110,11 @@ def gwrite(document: vp.Document, output: typing.TextIO, profile: str):
107110
if layer_start is not None:
108111
output.write(layer_start.format(index=layer_index))
109112
lastlines_index = len(layer) - 1
110-
for lines_index, lines in enumerate(layer):
111-
lines_scaled = lines * scale
113+
for lines_index, line in enumerate(layer):
112114
if line_start is not None:
113115
output.write(line_start.format(index=lines_index))
114-
segment_last_index = len(lines_scaled) - 1
115-
for segment_index, seg in enumerate(lines_scaled):
116+
segment_last_index = len(line) - 1
117+
for segment_index, seg in enumerate(line):
116118
x = seg.real
117119
y = seg.imag
118120
dx = x - last_x
@@ -160,7 +162,7 @@ def gwrite(document: vp.Document, output: typing.TextIO, profile: str):
160162
output.flush()
161163
output.close()
162164

163-
return document
165+
return orig_document
164166

165167

166-
gwrite.help_group = "Gcode"
168+
gwrite.help_group = "Output"

0 commit comments

Comments
 (0)