Graphics-Penplotter-GcodeXY
view release on metacpan or search on metacpan
lib/Graphics/Penplotter/GcodeXY/Split.pm view on Meta::CPAN
my ( $x1, $y1, $x2, $y2, $info );
$len = scalar @{ $self->{currentpage} };
$linecount = 1; # skip the gcode header entry
LINE:
while ( $linecount <= $len - 1 ) {
$self->_setprevious( $op, $xn, $yn );
$current = $self->{currentpage}[$linecount];
( $op, $xn, $yn ) = $f->_parse($current);
if ( $op == $PU ) {
next LINE if $location == $OUT;
$f->do_penup();
next LINE;
}
if ( $op == $PD ) {
next LINE if $location == $OUT;
$f->do_pendown();
next LINE;
}
# G00 or G01: clip against this sub-sheet boundary
( $x1, $y1, $x2, $y2, $info ) = $f->_LiangBarsky(
$corner{$i}{$j}{blx}, $corner{$i}{$j}{bly},
$corner{$i}{$j}{trx}, $corner{$i}{$j}{try},
$prevx, $prevy, $xn, $yn
);
if ( $info == 1 || $info == 3 ) {
# Start inside boundary
if ( $op == $G00 ) {
$f->do_penup() if $penstate == $PENDOWN;
$f->_addfastmove( $x2, $y2 );
}
else { # G01
$f->do_pendown() if $penstate == $PENUP;
$f->_addslowmove( $x2, $y2 );
}
$location = ( $info == 3 ) ? $OUT : $IN;
}
elsif ( $info == 2 ) {
# Entirely outside
$f->do_penup() if $location == $IN && $penstate == $PENDOWN;
$location = $OUT;
}
elsif ( $info == 4 ) {
# Start outside, end inside
if ( $op == $G00 ) {
$f->do_penup() if $penstate == $PENDOWN;
$f->_addfastmove( $x2, $y2 );
}
else { # G01
$f->do_penup() if $penstate == $PENDOWN;
$f->_addfastmove( $x1, $y1 );
$f->do_pendown();
$f->_addslowmove( $x2, $y2 );
}
$location = $IN;
}
elsif ( $info == 5 ) {
# Both endpoints outside but crossing through
if ( $op == $G01 ) {
$f->do_penup() if $penstate == $PENDOWN;
$f->_addfastmove( $x1, $y1 );
$f->do_pendown();
$f->_addslowmove( $x2, $y2 );
}
# G00 entirely outside: ignore
$location = $OUT;
}
}
continue { $linecount++ }
$f->_addtopage( $f->{trailer} );
$f->output();
} # foreach $j
} # foreach $i
return 1;
}
# ===========================================================================
# PEN STATE WRAPPERS
# (distinct from GcodeXY::penup/pendown -- these also track $penstate)
# ===========================================================================
sub do_penup ($self) {
$self->_addpath( 'u', -1, -1, -1, -1 );
$penstate = $PENUP;
return 1;
}
sub do_pendown ($self) {
$self->_addpath( 'd', -1, -1, -1, -1 );
$penstate = $PENDOWN;
return 1;
}
# ===========================================================================
# COORDINATE HELPERS
# ===========================================================================
# Generate one move (fast or slow), translating from big-sheet coordinates
# to sub-sheet coordinates, applying scale and whitespace margin.
sub _addmove ($self, $x, $y, $speed) {
my ( $newx, $newy );
if ( $mode eq 'p' ) {
$newx = ( $x - $xoffset ) * $scale + $xwhite;
$newy = ( $y - $yoffset ) * $scale + $ywhite;
}
else { # 'l' -- landscape: swap axes
$newx = ( $y - $yoffset ) * $scale + $xwhite;
$newy = ( $xoffset + $xlen - $x ) * $scale + $ywhite;
}
if ( $speed eq 'slow' ) {
$self->_addpath( 'l', $prevx, $prevy, $newx, $newy );
}
else {
( run in 0.476 second using v1.01-cache-2.11-cpan-5a3173703d6 )