From 671c0ce29ccd4f15faf47eca308c8a973bac3436 Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Mon, 13 Jan 2014 16:44:46 -0500 Subject: [PATCH] Add mode to create Digikey BOM --- bomgen/Bomgen/conf.inc.php | 13 ++ bomgen/Bomgen/digikey.inc.php | 50 ++++++++ bomgen/Makefile | 2 +- bomgen/bomgen.php | 226 +++++++++++++++++++--------------- 4 files changed, 190 insertions(+), 101 deletions(-) create mode 100644 bomgen/Bomgen/conf.inc.php create mode 100644 bomgen/Bomgen/digikey.inc.php diff --git a/bomgen/Bomgen/conf.inc.php b/bomgen/Bomgen/conf.inc.php new file mode 100644 index 0000000..a1bf549 --- /dev/null +++ b/bomgen/Bomgen/conf.inc.php @@ -0,0 +1,13 @@ + diff --git a/bomgen/Bomgen/digikey.inc.php b/bomgen/Bomgen/digikey.inc.php new file mode 100644 index 0000000..0e7cc13 --- /dev/null +++ b/bomgen/Bomgen/digikey.inc.php @@ -0,0 +1,50 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +/* Exportation BOM digikey. */ +function export_bom_digikey(&$data, $num, $col_num_to_id, $col_id_to_num, $filename) +{ + global $debug; + + $handle = fopen($filename, "w"); + + foreach ($data as $key => $row) { + $num = count($row); + + for ($c = 0; $c < $num; $c++) { + if (isset($col_num_to_id[$c])) { + if ($col_num_to_id[$c] == QTY_COL_NAME) { + $qty = $row[$c]; + } else if ($col_num_to_id[$c] == "Part Number") { + $pn = $row[$c]; + + if ($pn == "") { + $pn = "MISSING"; + } + } else if ($col_num_to_id[$c] == DESIGNATOR_COL_NAME) { + $refdes = $row[$c]; + } else if ($col_num_to_id[$c] == "Manufacturer") { + $manuf = $row[$c]; + } + + } + } + + if ($pn != "MISSING") { + $line = $qty . CSV_DK_DELIM . $manuf . CSV_DK_DELIM . $pn . CSV_DK_DELIM . $refdes . "\r\n"; + fwrite($handle, $line); + } + } + + fclose($handle); +} + +?> diff --git a/bomgen/Makefile b/bomgen/Makefile index e196e5c..ca8c9e3 100644 --- a/bomgen/Makefile +++ b/bomgen/Makefile @@ -8,7 +8,7 @@ endif SCRIPT = bomgen.php INC_DIR = Bomgen -INC_FILES = functions.inc.php refdes.inc.php +INC_FILES = functions.inc.php refdes.inc.php digikey.inc.php conf.inc.php SYSCONFDIR=/etc diff --git a/bomgen/bomgen.php b/bomgen/bomgen.php index c240dea..4883b72 100755 --- a/bomgen/bomgen.php +++ b/bomgen/bomgen.php @@ -15,21 +15,17 @@ require_once 'Spreadsheet/Excel/Writer.php'; require_once 'Bomgen/functions.inc.php'; require_once 'Bomgen/refdes.inc.php'; +require_once 'Bomgen/digikey.inc.php'; +require_once 'Bomgen/conf.inc.php'; $debug = 0; +$digikey_bom = false; + define("DATA_ROW_START", 3); define("PAPER_US_LETTER", 1); -define("QTY_COL_NAME", "Count"); -define("COMPANY_PN_COL_NAME", "P/N_LSI"); -define("DESIGNATOR_COL_NAME", "RefDes"); - - -define("ASSEMBLY_COL_NAME", "Asm"); -define("DO_NOT_POPULATE_KEYWORD", "DNP"); - /* width = 0 -> Ne pas afficher la colonne. */ $csv_infos = array(QTY_COL_NAME => array("nom" => "Qty", @@ -67,11 +63,12 @@ $csv_infos = function usage() { echo "Usage: bomgen.php [OPTIONS] [INPUT FILE]\n"; - echo "Generate an Excel BOM from an Altium CSV-exported BOM.\n"; - echo "Output file has same name as input file, but with a .xls extension.\n"; + echo "Generate an Excel BOM from a PCAD CSV-exported BOM.\n"; + echo "Output file has same name as input file, but with a .xls or .txt extension.\n"; echo "\n"; echo "Options:\n"; echo " -c Add cost information to the BOM\n"; + echo " -k Create digiley BOM submission file (tab-separated fields)\n"; echo " -h Display this help and exit\n"; } @@ -93,6 +90,8 @@ $col_id_to_num = array(); */ $col_num_to_id = array(); +$entete = array(); + /* * value: clé * c: Numéro de colonne dans le fichier CSV. @@ -167,7 +166,7 @@ $add_cost = false; $comp_total = 0; $titre = "BOM"; -$short_opts = "hc"; +$short_opts = "hck"; $cg = new Console_Getopt(); @@ -191,6 +190,9 @@ if (sizeof($opts) > 0) { case 'c': $add_cost = true; break; + case 'k': + $digikey_bom = true; + break; case 'h': usage(); exit(1); @@ -213,17 +215,7 @@ if (sizeof($args) == 0) { exit(1); } -$dest = strip_ext($src) . ".xls"; - -/* Création d'un workbook. */ -$workbook = new Spreadsheet_Excel_Writer($dest); - -/* Création du worksheet */ -$worksheet =& $workbook->addWorksheet($titre); - -if (PEAR::isError($worksheet)) { - die($worksheet->getMessage()); -} +$dest = strip_ext($src); /* Importation du BOM CSV. */ $data = array(); @@ -270,7 +262,8 @@ if (($handle = fopen($src, "r")) !== FALSE) { if ($csv_infos[$id]['width'] != 0) { $nom = $csv_infos[$id]['nom']; - xls_write_col_header(DATA_ROW_START, $k, $nom); + $entete[$k] = $nom; + $k++; } } else { @@ -321,110 +314,143 @@ array_multisort($sort_type, SORT_ASC, SORT_STRING, $sort_volt, SORT_ASC, SORT_NUMERIC, $data); -$row_num = DATA_ROW_START + 1; +if ($digikey_bom) { + /* Exportation BOM digikey. */ + $dest .= ".txt"; -/* Insertion des données dans le tableau. */ -foreach ($data as $key => $row) { - $comp_total++; /* Compte le nombre de composants total. */ - $num = count($row); + export_bom_digikey($data, $num, $col_num_to_id, $col_id_to_num, $dest); +} else { + /* Exportation BOM Excel */ + + $dest .= ".xls"; + + /* Création d'un workbook. */ + $workbook = new Spreadsheet_Excel_Writer($dest); - /* Index de la prochaine colonne disponible pour affichage. */ + /* Création du worksheet */ + $worksheet =& $workbook->addWorksheet($titre); + + if (PEAR::isError($worksheet)) { + die($worksheet->getMessage()); + } + + $row_num = DATA_ROW_START; + + /* Écriture des colonnes d'entête. */ $k = 0; + foreach ($entete as $key => $value) { + xls_write_col_header($row_num, $k, $value); + $k++; + } - for ($c = 0; $c < $num; $c++) { - if (isset($csv_infos[$col_num_to_id[$c]])) { - $width = $csv_infos[$col_num_to_id[$c]]['width']; + $row_num++; - if (($add_cost == true) && ($col_num_to_id[$c] == "UnitCost")) { - /* Lecture du prix dans Syteline. */ - $price = get_price($row[$col_id_to_num[COMPANY_PN_COL_NAME]]); - if ($price == 0) { - /* Si pas de prix Syteline, on utilise le prix dans Altium. */ - $price = $row[$c]; - } + /* Insertion des données dans le tableau. */ + foreach ($data as $key => $row) { + $comp_total++; /* Compte le nombre de composants total. */ + $num = count($row); - if ($price == 0) { - $price = ""; - } + /* Index de la prochaine colonne disponible pour affichage. */ + $k = 0; - xls_write_price($row_num, $k, $price); - $k++; - } else if (($add_cost == true) && ($col_num_to_id[$c] == "TotalCost")) { - /* Formule prix total = qté * unit_cost */ - xls_write_price($row_num, $k, "=" . col_to_letter($col_id_to_xls_num["Quantity"]) . - ($row_num + 1) . "*" . col_to_letter($col_id_to_xls_num["UnitCost"]) . - ($row_num + 1) . ")"); - $k++; - } else if ($col_num_to_id[$c] == "MPN") { - $mpn = $row[$c]; - - if (empty($mpn)) { - /* Lecture du "MPN" dans Syteline si celui dans Altium est absent. */ - $mpn = get_mpn($row[$col_id_to_num[COMPANY_PN_COL_NAME]]); - } + for ($c = 0; $c < $num; $c++) { + if (isset($csv_infos[$col_num_to_id[$c]])) { + $width = $csv_infos[$col_num_to_id[$c]]['width']; - xls_write_cell($row_num, $k, $mpn, XLS_FORMAT_TEXT); + if (($add_cost == true) && ($col_num_to_id[$c] == "UnitCost")) { + /* Lecture du prix dans Syteline. */ + $price = get_price($row[$col_id_to_num[COMPANY_PN_COL_NAME]]); + if ($price == 0) { + /* Si pas de prix Syteline, on utilise le prix dans Altium. */ + $price = $row[$c]; + } - $k++; - } else if ($col_num_to_id[$c] == "Manufacturer") { - $manufacturer = $row[$c]; + if ($price == 0) { + $price = ""; + } + + xls_write_price($row_num, $k, $price); - //if (empty($manufacturer)) { + $k++; + } else if (($add_cost == true) && ($col_num_to_id[$c] == "TotalCost")) { + /* Formule prix total = qté * unit_cost */ + xls_write_price($row_num, $k, "=" . col_to_letter($col_id_to_xls_num["Quantity"]) . + ($row_num + 1) . "*" . col_to_letter($col_id_to_xls_num["UnitCost"]) . + ($row_num + 1) . ")"); + + $k++; + } else if ($col_num_to_id[$c] == "MPN") { + $mpn = $row[$c]; + + if (empty($mpn)) { + /* Lecture du "MPN" dans Syteline si celui dans Altium est absent. */ + $mpn = get_mpn($row[$col_id_to_num[COMPANY_PN_COL_NAME]]); + } + + xls_write_cell($row_num, $k, $mpn, XLS_FORMAT_TEXT); + + $k++; + } else if ($col_num_to_id[$c] == "Manufacturer") { + $manufacturer = $row[$c]; + + //if (empty($manufacturer)) { /* Lecture du "Manufacturer" dans Syteline si celui dans Altium est absent. */ //$manufacturer = get_manufacturer($row[$col_id_to_num[COMPANY_PN_COL_NAME]]); - //} - - xls_write_cell($row_num, $k, $manufacturer, XLS_FORMAT_TEXT); + //} - $k++; - } else { - if ($width) { - $value = $row[$c]; + xls_write_cell($row_num, $k, $manufacturer, XLS_FORMAT_TEXT); - /* Ligne de données. */ - xls_write_cell($row_num, $k, $value, XLS_FORMAT_TEXT); $k++; + } else { + if ($width) { + $value = $row[$c]; + + /* Ligne de données. */ + xls_write_cell($row_num, $k, $value, XLS_FORMAT_TEXT); + + $k++; + } } } } - } - $row_num++; -} + $row_num++; + } -if ($add_cost == true) { - /* Ajout cellule prix total. */ - xls_write_price($row_num, $col_id_to_xls_num["TotalCost"], - "=SUM(" . col_to_letter($col_id_to_xls_num["TotalCost"]) . - (DATA_ROW_START + 2) . ":" . col_to_letter($col_id_to_xls_num["TotalCost"]) . - $row_num . ")"); -} + if ($add_cost == true) { + /* Ajout cellule prix total. */ + xls_write_price($row_num, $col_id_to_xls_num["TotalCost"], + "=SUM(" . col_to_letter($col_id_to_xls_num["TotalCost"]) . + (DATA_ROW_START + 2) . ":" . col_to_letter($col_id_to_xls_num["TotalCost"]) . + $row_num . ")"); + } -/* TODO... */ -if (empty($project_name)) { - $project_name = "PROJECT NAME"; -} -if (empty($revision)) { - $revision = "REV"; -} + /* TODO... */ + if (empty($project_name)) { + $project_name = "PROJECT NAME"; + } + if (empty($revision)) { + $revision = "REV"; + } -/* Écriture des informations globales relatives au BOM. */ -xls_write_cell_gen(0, 0, "BOM " . $project_name, - XLS_FORMAT_TEXT, false, 18, FALSE, FALSE); + /* Écriture des informations globales relatives au BOM. */ + xls_write_cell_gen(0, 0, "BOM " . $project_name, + XLS_FORMAT_TEXT, false, 18, FALSE, FALSE); -xls_write_cell_gen(1, 0, "Révision (schéma): " . $revision, - XLS_FORMAT_TEXT, false, 16, FALSE, FALSE); + xls_write_cell_gen(1, 0, "Révision (schéma): " . $revision, + XLS_FORMAT_TEXT, false, 16, FALSE, FALSE); -xls_write_cell_gen(1, 2, "Composants: " . $comp_total, - XLS_FORMAT_TEXT, false, 16, FALSE, FALSE); + xls_write_cell_gen(1, 2, "Composants: " . $comp_total, + XLS_FORMAT_TEXT, false, 16, FALSE, FALSE); -xls_configure($workbook, $worksheet); + xls_configure($workbook, $worksheet); -$workbook->close(); + $workbook->close(); -if (!file_exists($dest)) { - echo "Cannot create destination file: " . $dest . "\n"; - exit(1); + if (!file_exists($dest)) { + echo "Cannot create destination file: " . $dest . "\n"; + exit(1); + } } ?> -- 2.20.1