[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

gEDA-user: I wrote a BOM "compiler"



Thats very nice output Karel.  I recently wrote a similar program in
Perl.  Its pretty short so I have included it below.

I used a similar design, I have a CSV file of parts for each "kit" I
want to build:

refdes,value,comment,footprint,supplier,part number,currency,Qty 1,Qty
100,,Qty
1,Qty 100
<snip>
R33,4M7,0.125W 5%,bf_0805,Rapier,,US,0.05,0.02,,0.02,0.02
R32,15M,0.125W 5%,bf_0805,Digikey,P15MACT-ND,US,0.02,0.02,,0.02,0.02
R31,4M7,0.125W 5%,bf_0805,Rapier,,US,0.05,0.02,,0.02,0.02
R30,15M,0.125W 5%,bf_0805,Digikey,P15MACT-ND,AUD,0.02,0.02,,0.02,0.02
<snip>

Note the fields above for different currency, lets me add up costs in my
local currency.  I also supply the script with an input text file like
this:

12,4fx.csv
44,fxomod.csv
44,fxsmod.csv

that says I want 12 4fx boards, 44 fxsmod and 44 fxomod boards. The
script then spits out a shopping list for each supplier, e.g. this is
the one for Digikey:

<snip>
66      399-1805-1-ND
130     490-1014-1-ND
12      122-1437-ND
<snip>

I then cut/paste onto the Digikey or Farnell web sites to place the
order.  Perl is really cool for processing text files - this program
only too me about an hour to write.

Here is the Perl script, the Supplier="nil" mode is used to generate
lists of parts that I haven't sourced yet.

Cheers,

David

#!/usr/bin/perl -w
# David Rowe 2 August 2006
#
# Generate purchase orders based on kits of parts in CSV files

use strict;
use Carp;

sub parse_kit($$$);
sub output_po($);

my $numArgs = $#ARGV + 1;
if ($numArgs != 3) {
    print "usage: ./genpo.pl inputFile Supplier OutputFile\n";
    exit;
}
my $inputFileName = shift;
my $supplier = shift;
my $poFileName = shift;
my %QtyByPartNum = ();
my %ValueByPartNum = ();
my %FootprintByPartNum = ();
my %QtyByValue = ();

open IN, $inputFileName or die "Cant open file $_\n";
while (<IN>) {
    # each line specifies Qty,"kitname.csv" on each line

    if(/(.*),(.*)/) {
        print "Qty: $1 kit: $2\n";
        parse_kit($1, $2, $supplier);
    }

}

close IN;

if ($supplier eq "nil") {
    output_po_nil($poFileName);
}
else {
    output_po($poFileName);
}

sub parse_kit($$$) {
    my ($qty, $kitFileName, $supplier) = @_;

    open KIT, $kitFileName or die "Cant open kit file $_\n";

    # dump first two lines

    <KIT>; # exchange rate
    <KIT>; # title row

    while (<KIT>) {
        # each line specifies Qty,"kitname.csv" on each line

        my ($refdes, $value, $comment, $footprint, $supp, $partnum) =
split(/,/);

        # normal operation

        if(defined $supp && ($supplier eq $supp)) {
            print "refdes: $refdes\n";
            if (defined $QtyByPartNum{$partnum}) {
                $QtyByPartNum{$partnum} += $qty;
            }
            else {
                $QtyByPartNum{$partnum} = $qty;
                $ValueByPartNum{$partnum} = $value;
                $FootprintByPartNum{$partnum} = $footprint;
            }
        }

        # searching for parts without a supplier

        if ($refdes =~ /\w/) {
            if (!length($supp) && ($supplier eq "nil")) {
                print "refdes: $refdes supp: $supp\n";
                $value = $value . " " . $comment . " " . $footprint;
                if (defined $QtyByValue{$value}) {
                    $QtyByValue{$value} += $qty;
                }
                else {
                    $QtyByValue{$value} = $qty;
                }
            }
        }
    }
    close KIT;
}

sub output_po($) {
    my $poFileName = shift;
    my $partNum;
    my $items = 0;

    open PO, ">$poFileName" or die "Can't open output PO file $_\n";

    foreach $partNum (keys %QtyByPartNum) {
        print "$QtyByPartNum{$partNum} $partNum ";
        print "$ValueByPartNum{$partNum} $FootprintByPartNum
{$partNum}\n";
        print PO "$QtyByPartNum{$partNum}\t$partNum\n";
        $items++;
    }
    print "$items total\n";

    close PO;
}

sub output_po_nil($) {
    my $poFileName = shift;
    my $value;
    my $items = 0;

    open PO, ">$poFileName" or die "Can't open output PO file $_\n";

    foreach $value (keys %QtyByValue) {
        print "$QtyByValue{$value} $value\n";
        print PO "$QtyByValue{$value}\t$value\n";
        $items++;
    }
    print "$items total\n";

    close PO;
}





_______________________________________________
geda-user mailing list
geda-user@xxxxxxxxxxxxxx
http://www.seul.org/cgi-bin/mailman/listinfo/geda-user