summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Kremer <->2022-09-29 20:12:35 +0200
committerThomas Kremer <->2022-09-29 20:12:35 +0200
commit7b518529903ed4976b97868d8d7061737e0f5b73 (patch)
tree248ae8490757238b4745df4d6ad62c54b5b9d86a
parent93a592f37d9f5f98ed71588df3143db69d6855dc (diff)
polylines with only 1 point only warn, translate to bbox is now the default (option ignore_bbox disables it)
-rwxr-xr-xdxf2camm.pl32
1 files changed, 26 insertions, 6 deletions
diff --git a/dxf2camm.pl b/dxf2camm.pl
index bbd406d..8659dde 100755
--- a/dxf2camm.pl
+++ b/dxf2camm.pl
@@ -147,7 +147,11 @@ sub dxf_extract_polylines {
warn("ignoring entity: $e->{name}"),next unless $e->{name} eq "LWPOLYLINE";
my ($x,$y) = @{$e->{attrs}}{qw(x y)};
die "invalid number of coordinates in lwpolyline"
- unless ref $x eq "ARRAY" && @$x == @$y && @$x >= 1;
+ unless ref $x eq "ARRAY" && @$x == @$y;
+ if (@$x <= 1) {
+ warn "ignoring lwpolyline with ".(@$x? "only one" : "no")." point.\n";
+ next;
+ }
my $closed = $e->{attrs}{int} & 1;
my @points = map [0+$$x[$_],0+$$y[$_]], 0..$#$x;
push @res, [($closed?"closed":"open"),\@points];
@@ -813,6 +817,7 @@ sub usage {
offset => "Set knive offset to this value (mm).",
offsetless_start => "Start each polyline without knife offset.",
bbox => "Add a bounding box with this much spacing.",
+ ignore_bbox => "Don't translate origin according to bounding box",
align_knife => "Begin with a small cut at [0,0]->[0,2] to align the knife.",
overlap => "add this much (mm) of the start of a loop to its end to make it overlap.",
raw => "Don't emit header/footer commands.",
@@ -830,7 +835,7 @@ sub usage {
help => "Show this help screen.",
);
-@opts = qw(output|o=s offset|off=f offsetless_start! bbox=f align_knife! overlap=f raw! relative! epsilon=f shortline=f smallangle=f coarsify=f combine! combine_cycles|cycles! combine_reverse|reverse! translate=s scale=f sort=s help|h|?);
+@opts = qw(output|o=s offset|off=f offsetless_start! bbox=f ignore_bbox! align_knife! overlap=f raw! relative! epsilon=f shortline=f smallangle=f coarsify=f combine! combine_cycles|cycles! combine_reverse|reverse! translate=s scale=f sort=s help|h|?);
GetOptions(\%opts,@opts) or usage(2);
@@ -864,16 +869,31 @@ $paths = combine_polylines_fuzzy($paths,$opts{combine_cycles},$opts{combine_reve
# Note: This assumes, no point is referentially used twice.
for (@$paths) { # a path
for my $p (@{$$_[1]}) { # a point
- if (defined $opts{translate}) {
- $$p[$_] += $opts{translate}[$_] for 0,1;
- }
for (@$p) { # a coordinate
- $_ = $opts{scale}*$_*CAMM::units_per_mm;
+ $_ *= $opts{scale}*CAMM::units_per_mm;
#$_ = lround($opts{scale}*$_*CAMM::units_per_mm);
}
}
}
+# We shift everything to [offset,offset] based on bounding box now, since cutting negative coordinates isn't possible anyway.
+# TODO: make option to not translate according to bbox.
+
+my $input_bbox = $opts{ignore_bbox} ? [0,0,0,0] : bbox_union(compute_bboxes($paths));
+
+# keep the translate setting from the bbox option.
+$opts{translate} //= [0,0];
+$opts{translate}[$_] *= CAMM::units_per_mm for 0,1;
+$opts{translate}[$_] += ($opts{offset}//0)-$input_bbox->[$_] for 0,1;
+
+if (defined $opts{translate}) {
+ for (@$paths) { # a path
+ for my $p (@{$$_[1]}) { # a point
+ $$p[$_] += $opts{translate}[$_] for 0,1;
+ }
+ }
+}
+
$paths = coarsify_polylines($paths,$opts{coarsify}*CAMM::units_per_mm)
if $opts{coarsify};