From d4413a6bbe5c2fafcbe362618c5c711b27ecbe00 Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Wed, 22 Jan 2014 10:26:57 -0500 Subject: [PATCH] Add command line option to select PCAD or altium BOM --- bomgen/Bomgen/altium.inc.php | 18 ++++ bomgen/Bomgen/conf.inc.php | 8 -- bomgen/Bomgen/mapping.inc.php | 51 +++++++++++ bomgen/Bomgen/pcad.inc.php | 18 ++++ bomgen/Makefile | 9 +- bomgen/bomgen.php | 163 ++++++++++++++++------------------ 6 files changed, 173 insertions(+), 94 deletions(-) create mode 100644 bomgen/Bomgen/altium.inc.php create mode 100644 bomgen/Bomgen/mapping.inc.php create mode 100644 bomgen/Bomgen/pcad.inc.php diff --git a/bomgen/Bomgen/altium.inc.php b/bomgen/Bomgen/altium.inc.php new file mode 100644 index 0000000..372a70d --- /dev/null +++ b/bomgen/Bomgen/altium.inc.php @@ -0,0 +1,18 @@ + + * + * 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. + */ + +/* Altium BOM fields definitions. */ + +define("QTY_COL_NAME", "Quantity"); +define("DESIGNATOR_COL_NAME", "Designator"); +define("FOOTPRINT_COL_NAME", "Footprint"); + +?> diff --git a/bomgen/Bomgen/conf.inc.php b/bomgen/Bomgen/conf.inc.php index 8e758ef..84d2cb9 100644 --- a/bomgen/Bomgen/conf.inc.php +++ b/bomgen/Bomgen/conf.inc.php @@ -3,12 +3,4 @@ /* Digikey export delimiter. */ define("CSV_DK_DELIM", "\t"); -define("QTY_COL_NAME", "Quantity"); -define("COMPANY_PN_COL_NAME", "P/N_LSI"); -define("DESIGNATOR_COL_NAME", "Designator"); -define("FOOTPRINT_COL_NAME", "Footprint"); - -define("ASSEMBLY_COL_NAME", "Asm"); -define("DO_NOT_POPULATE_KEYWORD", "DNP"); - ?> diff --git a/bomgen/Bomgen/mapping.inc.php b/bomgen/Bomgen/mapping.inc.php new file mode 100644 index 0000000..72de10b --- /dev/null +++ b/bomgen/Bomgen/mapping.inc.php @@ -0,0 +1,51 @@ + + * + * 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. + */ + +/* Common fields for all BOM types. */ +define("ASSEMBLY_COL_NAME", "Asm"); +define("DO_NOT_POPULATE_KEYWORD", "DNP"); +define("COMPANY_PN_COL_NAME", "P/N_LSI"); + +/* width = 0 -> Ne pas afficher la colonne. */ +$csv_infos = + array(QTY_COL_NAME => array("nom" => "Qty", + "width" => 5), + DESIGNATOR_COL_NAME => array("nom" => "Designator", + "width" => 30), + "Description" => array("nom" => "Description", + "width" => 45), + "Value" => array("nom" => "Value", + "width" => 15), + "Voltage" => array("nom" => "Voltage", + "width" => 10), + "Current" => array("nom" => "Current", + "width" => 10), + "Power" => array("nom" => "Power", + "width" => 10), + "Manufacturer" => array("nom" => "Manufacturer", + "width" => 30), + "Part Number" => array("nom" => "Manuf. P/N", + "width" => 30), + COMPANY_PN_COL_NAME => array("nom" => "LSI P/N", + "width" => 15), + FOOTPRINT_COL_NAME => array("nom" => "Footprint", + "width" => 20), + ASSEMBLY_COL_NAME => array("nom" => "Assembly", + "width" => 0), + "UnitCost" => array("nom" => "Unit Cost", + "width" => 7), + "TotalCost" => array("nom" => "Total Cost", /* Colonne ajoutée par ce script. */ + "width" => 8), + "Type" => array("nom" => "Type", /* Colonne ajoutée par ce script. */ + "width" => 0), + ); + +?> diff --git a/bomgen/Bomgen/pcad.inc.php b/bomgen/Bomgen/pcad.inc.php new file mode 100644 index 0000000..7936dd4 --- /dev/null +++ b/bomgen/Bomgen/pcad.inc.php @@ -0,0 +1,18 @@ + + * + * 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. + */ + +/* PCAD BOM fields definitions. */ + +define("QTY_COL_NAME", "Count"); +define("DESIGNATOR_COL_NAME", "RefDes"); +define("FOOTPRINT_COL_NAME", "PatternName"); + +?> diff --git a/bomgen/Makefile b/bomgen/Makefile index ca8c9e3..60e34bd 100644 --- a/bomgen/Makefile +++ b/bomgen/Makefile @@ -8,7 +8,14 @@ endif SCRIPT = bomgen.php INC_DIR = Bomgen -INC_FILES = functions.inc.php refdes.inc.php digikey.inc.php conf.inc.php +INC_FILES = \ + functions.inc.php \ + refdes.inc.php \ + conf.inc.php \ + digikey.inc.php \ + mapping.inc.php \ + pcad.inc.php \ + altium.inc.php SYSCONFDIR=/etc diff --git a/bomgen/bomgen.php b/bomgen/bomgen.php index de00211..d22fbf2 100755 --- a/bomgen/bomgen.php +++ b/bomgen/bomgen.php @@ -16,7 +16,6 @@ require_once 'Bomgen/conf.inc.php'; require_once 'Spreadsheet/Excel/Writer.php'; require_once 'Bomgen/functions.inc.php'; require_once 'Bomgen/refdes.inc.php'; -require_once 'Bomgen/digikey.inc.php'; $debug = 0; @@ -28,46 +27,12 @@ define("DATA_ROW_START", 3); define("PAPER_US_LETTER", 1); -/* width = 0 -> Ne pas afficher la colonne. */ -$csv_infos = - array(QTY_COL_NAME => array("nom" => "Qty", - "width" => 5), - DESIGNATOR_COL_NAME => array("nom" => "Designator", - "width" => 30), - "Description" => array("nom" => "Description", - "width" => 45), - "Value" => array("nom" => "Value", - "width" => 15), - "Voltage" => array("nom" => "Voltage", - "width" => 10), - "Current" => array("nom" => "Current", - "width" => 10), - "Power" => array("nom" => "Power", - "width" => 10), - "Manufacturer" => array("nom" => "Manufacturer", - "width" => 30), - "Part Number" => array("nom" => "Manuf. P/N", - "width" => 30), - COMPANY_PN_COL_NAME => array("nom" => "LSI P/N", - "width" => 15), - FOOTPRINT_COL_NAME => array("nom" => "Footprint", - "width" => 20), - ASSEMBLY_COL_NAME => array("nom" => "Assembly", - "width" => 0), - "UnitCost" => array("nom" => "Unit Cost", - "width" => 7), - "TotalCost" => array("nom" => "Total Cost", /* Colonne ajoutée par ce script. */ - "width" => 8), - "Type" => array("nom" => "Type", /* Colonne ajoutée par ce script. */ - "width" => 0), - ); - -$short_opts = "hci:kn:"; +$short_opts = "hci:kn:t:"; function usage() { echo "Usage: bomgen.php [OPTIONS] [INPUT FILE]\n"; - echo "Generate an Excel BOM from a PCAD CSV-exported BOM.\n"; + echo "Generate an Excel BOM from a PCAD or Altium 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"; @@ -76,6 +41,8 @@ function usage() echo " -k Create digikey BOM submission file (tab-separated fields)\n"; echo " -h Display this help and exit\n"; echo " -n Number of cards/kits for digikey BOM submission\n"; + echo " -t BOM input type:\n"; + echo " pcad or altium\n"; } /* @@ -207,6 +174,9 @@ if (sizeof($opts) > 0) { case 'h': usage(); exit(1); + case 't': + $bom_type = strtolower($o[1]); + break; } } } @@ -226,6 +196,22 @@ if (sizeof($args) == 0) { exit(1); } +switch ($bom_type) { +case 'pcad': + require_once 'Bomgen/pcad.inc.php'; + break; +case 'altium': + require_once 'Bomgen/altium.inc.php'; + break; +default: + echo "Invalid BOM type: " . $bom_type . "\n"; + exit(1); + break; +} + +require_once 'Bomgen/mapping.inc.php'; +require_once 'Bomgen/digikey.inc.php'; + $dest = strip_ext($src); /* Importation du BOM CSV. */ @@ -238,72 +224,79 @@ if ($add_cost == false) { $csv_infos['UnitCost']['width'] = 0; } -if (($handle = fopen($src, "r")) !== FALSE) { - while (($row = fgetcsv($handle, 1024, ',')) !== false) { - $num = count($row); +$handle = fopen($src, "r"); - if ($num < 2) { - /* Ligne vide. */ - continue; - } +if ($handle == FALSE) { + echo "Error opening file: " . $src . "\n"; + exit(1); +} - if ($row_num == 1) { - /* Entête. */ +/* Read and import BOM source file. */ +while (($row = fgetcsv($handle, 1024, ',')) !== false) { + $num = count($row); - if ($add_cost == true) { - /* Ajout colonne temporaire pour le total cost. */ - $row[$num] = "TotalCost"; - $num++; - } + if ($num < 2) { + /* Ligne vide. */ + continue; + } + + if ($row_num == 1) { + /* Entête. */ - /* Ajout colonne temporaire selon le type de composant. Cela est - * nécessaire pour faire le tri correctement. */ - $row[$num] = "Type"; + if ($add_cost == true) { + /* Ajout colonne temporaire pour le total cost. */ + $row[$num] = "TotalCost"; $num++; + } - /* Index de la prochaine colonne disponible pour affichage. */ - $k = 0; + /* Ajout colonne temporaire selon le type de composant. Cela est + * nécessaire pour faire le tri correctement. */ + $row[$num] = "Type"; + $num++; - for ($c = 0; $c < $num; $c++) { - $id = $row[$c]; + /* Index de la prochaine colonne disponible pour affichage. */ + $k = 0; - determine_col_index($id, $c, $k); + for ($c = 0; $c < $num; $c++) { + $id = $row[$c]; - if (isset($csv_infos[$id])) { - if ($csv_infos[$id]['width'] != 0) { - $nom = $csv_infos[$id]['nom']; + determine_col_index($id, $c, $k); - $entete[$k] = $nom; + if (isset($csv_infos[$id])) { + if ($csv_infos[$id]['width'] != 0) { + $nom = $csv_infos[$id]['nom']; - $k++; - } - } else { - echo "Colonne '$id' non supportée.\n"; + $entete[$k] = $nom; + + $k++; } + } else { + echo "Colonne '$id' non supportée.\n"; } - } else { - $ref_prefix2 = substr($row[$col_id_to_num[DESIGNATOR_COL_NAME]], 0, 2); - $company_pn = substr($row[$col_id_to_num[COMPANY_PN_COL_NAME]], 0, 1); /* Pour les vieux BOMs qui n'ont pas - * l'attribut NOPOP. */ - if (($ref_prefix2 == "MH") || - ($ref_prefix2 == "TP") || - ($ref_prefix2 == "FI") || - (array_key_exists(ASSEMBLY_COL_NAME, $col_id_to_num) && ($row[$col_id_to_num[ASSEMBLY_COL_NAME]] == DO_NOT_POPULATE_KEYWORD)) || - ($company_pn == "*")) { - /* Enlève les composants non désirés. */ - continue; - } - - $ref_prefix = substr($ref_prefix2, 0, 1); - $row[$col_id_to_num['Type']] = $ref_prefix; - $data[] = $row; + } + } else { + $ref_prefix2 = substr($row[$col_id_to_num[DESIGNATOR_COL_NAME]], 0, 2); + $company_pn = substr($row[$col_id_to_num[COMPANY_PN_COL_NAME]], 0, 1); /* Pour les vieux BOMs qui n'ont pas + * l'attribut NOPOP. */ + if (($ref_prefix2 == "MH") || + ($ref_prefix2 == "TP") || + ($ref_prefix2 == "FI") || + (array_key_exists(ASSEMBLY_COL_NAME, $col_id_to_num) && ($row[$col_id_to_num[ASSEMBLY_COL_NAME]] == DO_NOT_POPULATE_KEYWORD)) || + ($company_pn == "*")) { + /* Enlève les composants non désirés. */ + continue; } - $row_num++; + $ref_prefix = substr($ref_prefix2, 0, 1); + $row[$col_id_to_num['Type']] = $ref_prefix; + $data[] = $row; } - fclose($handle); + + $row_num++; } +fclose($handle); + /* Combinaison de plusieurs lignes en une seule pour PCAD. */ refdes_combine($data, $num, $col_num_to_id, $col_id_to_num); -- 2.20.1