Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update io.py to add no_basic_shapes option #247

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 39 additions & 10 deletions vpype/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ def write_svg(
layer_label_format: str = "%d",
show_pen_up: bool = False,
color_mode: str = "none",
single_path: bool = False
) -> None:
"""Create a SVG from a :py:class:`Document` instance.

Expand Down Expand Up @@ -364,6 +365,9 @@ def write_svg(
show_pen_up: add paths for the pen-up trajectories
color_mode: "none" (no formatting), "layer" (one color per layer), "path" (one color
per path)
single_path: if true, we use svg:path elements to write monolithic lines. This is useful
to speed up importing SVG file into InkScape and maybe other vector graphic software.
If false, we use svg:line, svg:polyline and svg:polygon standard elements
"""

# compute bounds
Expand Down Expand Up @@ -429,10 +433,14 @@ def write_svg(

for layer in corrected_doc.layers.values():
for line in layer.pen_up_trajectories():
group.add(
dwg.line((line[0].real, line[0].imag), (line[-1].real, line[-1].imag))
)

if single_path:
group.add(
vmario89 marked this conversation as resolved.
Show resolved Hide resolved
dwg.path(d='M{:1.3f},{:1.3f} {:1.3f},{:1.3f}'.format(line[0].real, line[0].imag, line[1].real, line[1].imag))
)
else:
group.add(
dwg.line((line[0].real, line[0].imag), (line[-1].real, line[-1].imag))
)
dwg.add(group)

for layer_id in sorted(corrected_doc.layers.keys()):
Expand All @@ -448,21 +456,42 @@ def write_svg(
group.attribs["style"] = "display:inline"
group.attribs["id"] = f"layer{layer_id}"

monolithic_path = []
for line in layer:
if len(line) <= 1:
continue

if len(line) == 2:
path = dwg.line((line[0].real, line[0].imag), (line[1].real, line[1].imag))
elif line[0] == line[-1]:
path = dwg.polygon((c.real, c.imag) for c in line[:-1])
if single_path:
if len(line) == 2:
monolithic_path.append('M{:1.3f},{:1.3f} {:1.3f},{:1.3f}'.format(line[0].real, line[0].imag, line[1].real, line[1].imag))
elif line[0] == line[-1]:
d = 'M{:1.3f},{:1.3f}'.format(line[0].real, line[0].imag)
for c in line[:-1]:
d += ' {:1.3f},{:1.3f}'.format(c.real, c.imag)
d += ' Z'
monolithic_path.append(d)
else:
d = 'M{:1.3f},{:1.3f}'.format(line[0].real, line[0].imag)
for c in line:
d += ' {:1.3f},{:1.3f}'.format(c.real, c.imag)
monolithic_path.append(d)

else:
path = dwg.polyline((c.real, c.imag) for c in line)
if len(line) == 2:
path = dwg.line((line[0].real, line[0].imag), (line[1].real, line[1].imag))
elif line[0] == line[-1]:
path = dwg.polygon((c.real, c.imag) for c in line[:-1])
else:
path = dwg.polyline((c.real, c.imag) for c in line)

if color_mode == "path":
path.attribs["stroke"] = _COLORS[color_idx % len(_COLORS)]
color_idx += 1
group.add(path)
if not single_path:
group.add(path)

if single_path:
group.add(dwg.path(' '.join(monolithic_path)))

dwg.add(group)

Expand Down
11 changes: 11 additions & 0 deletions vpype_cli/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@
is_flag=True,
help="[HPGL only] Do not display the plotter configuration or paper loading information.",
)
@click.option(
"-s",
"--single-path",
is_flag=True,
help=(
"[SVG only] Write monolithic (combined) svg:path elements instead "
"svg:line, svg:polyline, svg:polygon elements"
),
)
@click.pass_obj # to obtain the command string
@vp.global_processor
def write(
Expand All @@ -177,6 +186,7 @@ def write(
device: Optional[str],
velocity: Optional[int],
quiet: bool,
single_path: bool,
):
"""Write command."""

Expand Down Expand Up @@ -205,6 +215,7 @@ def write(
layer_label_format=layer_label,
show_pen_up=pen_up,
color_mode=color_mode,
single_path=single_path,
)
elif file_format == "hpgl":
if not page_size:
Expand Down