Skip to content

Instantly share code, notes, and snippets.

@AlexDaniel
Created June 28, 2015 06:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save AlexDaniel/76569d745033eac7e3b3 to your computer and use it in GitHub Desktop.
ugly code that causes the problem
#!/usr/bin/env perl6
my $FILENAME = 'pins.ASC';
my $WIDTH = 500;
my $HEIGHT = 500;
my @parts;
my ($xMin, $yMin, $xMax, $yMax);
sub MAIN($filename, Str $layer where /^[T|B]$/ = 'T') {
@parts = gather {
($xMin, $yMin, $xMax, $yMax) = process $filename;
};
say <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
width="500"
height="500"
id="svg2"
version="1.1">;
printPart $_ for grep { .part<layer> eq $layer }, @parts;
say </svg>;
}
class Part {
has Match $.part;
has Match @.pins;
}
sub process($filename) {
my $fh = open $filename, :r;
my $part;
my ($xMin, $yMin, $xMax, $yMax);
for $fh.lines {
if /:r ^ Part \s+ $<name>=[\w+] \s+ '(' $<layer>=[T | B] ')' $/ {
$part = Part.new(part => $/);
take $part;
next;
}
next unless $part;
if /:r ^
\s* $<pin>=[\d+]
\s+ $<name>=[\w+]
\s+ $<x>=[\d*'.'\d+]
\s+ $<y>=[\d*'.'\d+]
\s+ $<layer>=[\d]
\s+ $<net>=[\w+]
\s+ $<nail>=[\d+]
$/ {
$xMin min= +$<x>;
$yMin min= +$<y>;
$xMax max= +$<x>;
$yMax max= +$<y>;
$part.pins.push: $/;
$*ERR.print: '.';
}
}
return $xMin, $yMin, $xMax, $yMax;
}
sub getColor($) is cached {
use Digest::SHA;
return '#' ~ [~] (sha256 "hello".encode: 'ascii').list[^3.fmt: '%02x'
}
sub printPart($part) {
for $part.pins {
say <circle cx="{(.<x> - $xMin)/($xMax - $xMin) * $WIDTH}" cy="{(.<y> - $yMin)/($yMax - $yMin) * $HEIGHT}" r="1" fill="{getColor(.<net>)}" />
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment