Skip to content

Commit

Permalink
- Improved HPGL export (minimised number of PR/PA command)
Browse files Browse the repository at this point in the history
- Implemented #310
  • Loading branch information
abey79 committed Oct 15, 2021
1 parent c2c3c77 commit 6f68002
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
New features and improvements:
* Added `lswap` command to swap the content of two layers (#300)
* Added `lreverse` command to reverse the order of paths within a layer (#300)
* Improved HPGL export (#253, #310, #316)

* Relative coordinates are now used by default to reduce file size. If absolute coordinates are needed, they a new `--absolute` option for the `write` command.
* A homing command (as defined by the `final_pu_params` configuration parameter) is no longer emitted between layers.

Bug fixes:
* ...
Expand Down
34 changes: 27 additions & 7 deletions vpype/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ def write_hpgl(
center: bool,
device: Optional[str],
velocity: Optional[int],
absolute: bool = False,
quiet: bool = False,
) -> None:
"""Create a HPGL file from the :class:`Document` instance.
Expand All @@ -516,6 +517,7 @@ def write_hpgl(
device: name of the device to use (the corresponding config must exists). If not
provided, a default device must be configured, which will be used.
velocity: if provided, a VS command will be generated with the corresponding value
absolute: if True, only use absolute coordinates
quiet: if True, do not print the plotter/paper info strings
"""

Expand Down Expand Up @@ -612,21 +614,39 @@ def complex_to_str(p: complex) -> str:
if paper_config.set_ps is not None:
output.write(f"PS{int(paper_config.set_ps)};")

first_layer = True
last_point: Optional[complex] = None
for layer_id in sorted(document.layers.keys()):
pen_id = 1 + (layer_id - 1) % plotter_config.pen_count

# As per #310, we emit a single PU; between layers
if not first_layer:
output.write(f"PU;")
else:
first_layer = False
output.write(f"SP{pen_id};")

for line in document.layers[layer_id]:
if len(line) < 2:
continue
output.write(f"PA;PU{complex_to_str(line[0])};")
output.write(f"PR;PD")
output.write(",".join(complex_to_str(p) for p in np.diff(line)))
output.write(";")

output.write(
f"PA;PU{paper_config.final_pu_params if paper_config.final_pu_params else ''};"
)
if absolute:
output.write(
f"PU{complex_to_str(line[0])};PD"
+ ",".join(complex_to_str(p) for p in line[1:])
+ ";"
)
else:
if last_point is None:
output.write(f"PU{complex_to_str(line[0])};PR;")
else:
output.write(f"PU{complex_to_str(line[0] - last_point)};")

output.write("PD" + ",".join(complex_to_str(p) for p in np.diff(line)) + ";")
last_point = line[-1]

if not absolute:
output.write("PA;")
output.write(f"PU{paper_config.final_pu_params if paper_config.final_pu_params else ''};")
output.write("SP0;IN;\n")
output.flush()
8 changes: 8 additions & 0 deletions vpype_cli/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
The `--landscape` and `--center` options are accepted and honored in HPGL.
By default, relative coordinates are used whenever possible in the HPGL output in order to
reduce file size. If `--absolute` is used, absolute coordinate are used instead.
Optionally, the HPGL-only `--velocity` can be provided, in which case a `VS` command will be
emitted with the provided value.
Expand Down Expand Up @@ -149,6 +152,9 @@
help="[SVG only] Color mode for paths (default: layer).",
)
@click.option("-d", "--device", type=str, help="[HPGL only] Type of the plotter device.")
@click.option(
"-a", "--absolute", is_flag=True, help="[HPGL only] Use absolute coordinates exclusively."
)
@click.option(
"-vs",
"--velocity",
Expand All @@ -175,6 +181,7 @@ def write(
pen_up: bool,
color_mode: str,
device: Optional[str],
absolute: bool,
velocity: Optional[int],
quiet: bool,
):
Expand Down Expand Up @@ -231,6 +238,7 @@ def write(
center=center,
device=device,
page_size=page_size,
absolute=absolute,
velocity=velocity,
quiet=quiet,
)
Expand Down

0 comments on commit 6f68002

Please sign in to comment.