App-HPGL2Cadsoft
view release on metacpan or search on metacpan
lib/App/HPGL2Cadsoft.pm view on Meta::CPAN
$self->_calculate_bbox();
# Report bounding box dimensions, maybe the user wants to change the scaling factor
say "Object bounding box stretches from (x,y) to (x,y) in millimeter:";
say " ("
. sprintf( '%.3f', $self->_bbox->min_x() ) . " "
. sprintf( '%.3f', $self->_bbox->min_y() ) . ") ("
. sprintf( '%.3f', $self->_bbox->max_x() ) . " "
. sprintf( '%.3f', $self->_bbox->max_y() ) . ")";
say
"Total dimensions in x and y directions with scaling factor " . $self->scaling_factor() . " in mm are:";
say " ("
. sprintf( '%.3f', $self->_bbox->max_x() - $self->_bbox->min_x() ) . " "
. sprintf( '%.3f', $self->_bbox->max_y() - $self->_bbox->min_y() ) . ")";
$self->_write_script();
say "Done!";
}
sub _calculate_bbox {
my $self = shift();
# Init min and max values to the first point in the dataset
my $l = $self->_hpgl_lines()->[0]->[0];
# Create bounding box with min and max values to be equal to the first point in the dataset
$self->_bbox(
Grid::Coord->new( $l->min_y(), $l->min_x() => $l->max_y(), $l->max_x() )
);
foreach my $line (@{$self->_hpgl_lines()}) {
my $start = $line->[0];
my $stop = $line->[1];
if ( !$self->_bbox->contains($start) ) {
$self->_grow_bbox( $start );
}
if ( !$self->_bbox->contains($stop) ) {
$self->_grow_bbox( $stop );
}
}
}
sub _grow_bbox {
my $self = shift();
my $point = shift();
my $bbox = \$self->_bbox;
$$bbox->min_y( $point->min_y() ) if ( $point->min_y() < $$bbox->min_y() );
$$bbox->min_x( $point->min_x() ) if ( $point->min_x() < $$bbox->min_x() );
$$bbox->max_y( $point->max_y() ) if ( $point->max_y() > $$bbox->max_y() );
$$bbox->max_x( $point->max_x() ) if ( $point->max_x() > $$bbox->max_x() );
# Safety check
warn "Bbox not updated as expected" if ( !$$bbox->contains($point) );
}
sub _write_script {
my $self = shift();
my $fh = IO::File->new( "> " . $self->output_file );
if ( defined $fh ) {
# Generate the script header
print $fh "# Generated by HPGL2Cadsoft on " . localtime(time) . "\n";
print $fh "grid mm\n";
print $fh "grid 1\n";
print $fh "set wire_bend 2\n";
print $fh "change width 0\n";
# Write the wires
map {
print $fh "wire ("
. sprintf( '%.3f', $_->[0]->min_x() ) . " "
. sprintf( '%.3f', $_->[0]->min_y() ) . ") ("
. sprintf( '%.3f', $_->[1]->min_x() ) . " "
. sprintf( '%.3f', $_->[1]->min_y() ) . ")\n";
} @{$self->_hpgl_lines()};
} else {
die "Could not open output '". $self->output_file . " for writing";
}
$fh->close();
}
# Speed up the Moose object construction
__PACKAGE__->meta->make_immutable;
no Moose;
1;
# ABSTRACT: module to convert a HPGL file into a Cadsoft Eagle script
__END__
=pod
=encoding UTF-8
=head1 NAME
App::HPGL2Cadsoft - module to convert a HPGL file into a Cadsoft Eagle script
=head1 VERSION
version 0.01
=head1 SYNOPSIS
my $object = App::HPGL2Cadsoft->new(input_file => 'logo.hpgl', output_file => 'logo.scr', scaling_factor => 342);
=head1 DESCRIPTION
( run in 0.783 second using v1.01-cache-2.11-cpan-a49fcb8fa48 )