From: Hugo Villeneuve Date: Tue, 17 Dec 2013 20:01:44 +0000 (-0500) Subject: Adapt script to work with PCAD boms X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;h=7ceba672cf563a54ad733e418e8f3f5efcc23671;p=eda-utils.git Adapt script to work with PCAD boms --- diff --git a/bomgen/Bomgen/functions.inc.php b/bomgen/Bomgen/functions.inc.php index b2abe0b..1340e11 100644 --- a/bomgen/Bomgen/functions.inc.php +++ b/bomgen/Bomgen/functions.inc.php @@ -146,7 +146,7 @@ function xls_write_cell_gen($row, $col, $value, $format_texte, $entete, $size, $ $value = mb_convert_encoding($value, "ISO-8859-1", "UTF-8"); /* Création object format. */ - $format =& $workbook->addFormat(); + $format =& $workbook->addFormat(); /* Grosseur du texte. */ $format->setSize($size); @@ -157,6 +157,7 @@ function xls_write_cell_gen($row, $col, $value, $format_texte, $entete, $size, $ if ($format_texte == XLS_FORMAT_TEXT) { $format->setAlign('left'); + $format->setAlign('top'); } else if ($format_texte == XLS_FORMAT_MONETARY) { $format->setNumFormat('$0.00'); } diff --git a/bomgen/Bomgen/refdes.inc.php b/bomgen/Bomgen/refdes.inc.php new file mode 100644 index 0000000..a7a6a37 --- /dev/null +++ b/bomgen/Bomgen/refdes.inc.php @@ -0,0 +1,72 @@ + + * + * 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. + */ + +/* + * Pour les composants identiques, PCAD les exporte chacun sur une ligne + * différente. + * Seul le premier du groupe a sa quantité spécifiée: les autres ont une + * quantité vide. + * Cette fonction combine donc toutes les lignes pour un même type de composant + * en une seule, et en combinant tous les refdes ensembles, séparés par des + * virgules. + */ +function refdes_combine(&$data, $num, $col_num_to_id, $col_id_to_num) +{ + global $debug; + + $newpart = 0; /* Set to 1 when encountering a new part. */ + $reflist = ""; + $newpartkey = ""; + + foreach ($data as $key => $row) { + + for ($c = 0; $c < $num; $c++) { + if ($col_num_to_id[$c] == DESIGNATOR_COL_NAME) { + $refdes = $row[$c]; + } + + if ($col_num_to_id[$c] == QTY_COL_NAME) { + if ($row[$c] == "") { + $newpart = 0; + } else { + $newpart = 1; + + /* Save previous reflist if applicable. */ + if ($reflist != "") { + if ($debug) { + echo " REFLIST = " . $reflist . "\n"; + } + + $data[$newpartkey][$col_id_to_num[DESIGNATOR_COL_NAME]] = $reflist; + } + + $newpartkey = $key; + $reflist = ""; + } + } + } + + if ($newpart == 1) { + if ($debug) { + echo "DEBUG: NEWPART\n"; + } + + $reflist = $refdes; + } else { + $reflist = $reflist . "," . $refdes; + + /* Discard row. */ + unset($data[$key]); + } + } +} + +?> diff --git a/bomgen/bomgen.php b/bomgen/bomgen.php index e61d73e..d38f116 100755 --- a/bomgen/bomgen.php +++ b/bomgen/bomgen.php @@ -14,6 +14,9 @@ require_once 'Spreadsheet/Excel/Writer.php'; require_once 'Bomgen/functions.inc.php'; +require_once 'Bomgen/refdes.inc.php'; + +$debug = 0; define("DATA_ROW_START", 3); @@ -23,6 +26,10 @@ define("PAPER_US_A3", 8); define("PAPER_US_A4", 9); define("PAPER_US_A5", 11); +define("QTY_COL_NAME", "Count"); +define("COMPANY_PN_COL_NAME", "P/N_LSI"); +define("DESIGNATOR_COL_NAME", "RefDes"); + /* width = 0 -> Ne pas afficher la colonne. */ $csv_infos = array("Count" => array("nom" => "Qty", @@ -47,12 +54,6 @@ $csv_infos = "width" => 15), "PatternName" => array("nom" => "Footprint", "width" => 20), - "Revision" => array("nom" => "Revision", - "width" => 0), - "DocumentNumber" => array("nom" => "DocumentNumber", - "width" => 0), - "NOPOP" => array("nom" => "NOPOP", - "width" => 0), "UnitCost" => array("nom" => "Unit Cost", "width" => 7), "TotalCost" => array("nom" => "Total Cost", /* Colonne ajoutée par ce script. */ @@ -61,8 +62,8 @@ $csv_infos = "width" => 0), ); -function usage() -{ +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"; @@ -271,12 +272,12 @@ if (($handle = fopen($src, "r")) !== FALSE) { } } } else { - $ref_prefix2 = substr($row[$col_id_to_num['Designator']], 0, 2); - $company_pn = substr($row[$col_id_to_num['Comment']], 0, 1); /* Pour les vieux BOMs qui n'ont pas + $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('NOPOP', $col_id_to_num) && ($row[$col_id_to_num['NOPOP']] == "NOPOP")) || ($company_pn == "*")) { /* Enlève les composants non désirés. */ @@ -293,6 +294,9 @@ if (($handle = fopen($src, "r")) !== FALSE) { fclose($handle); } +/* Combinaison de plusieurs lignes en une seule pour PCAD. */ +refdes_combine($data, $num, $col_num_to_id, $col_id_to_num); + /* Génère une liste des colonnes pour le tri. */ foreach ($data as $key => $row) { $sort_type[$key] = $row[$col_id_to_num['Type']]; @@ -302,7 +306,7 @@ foreach ($data as $key => $row) { } /* - * Triage des lignes selon la référence (colonne "Designator"), + * Triage des lignes selon la référence (colonne DESIGNATOR_COL_NAME), * et ensuite selon la description, la valeur et enfin le voltage. */ array_multisort($sort_type, SORT_ASC, SORT_STRING, @@ -318,13 +322,6 @@ foreach ($data as $key => $row) { $comp_total++; /* Compte le nombre de composants total. */ $num = count($row); - if (empty($project_name)) { - $project_name = $row[$col_id_to_num['DocumentNumber']]; - } - if (empty($revision)) { - $revision = $row[$col_id_to_num['Revision']]; - } - /* Index de la prochaine colonne disponible pour affichage. */ $k = 0; @@ -334,7 +331,7 @@ foreach ($data as $key => $row) { if (($add_cost == true) && ($col_num_to_id[$c] == "UnitCost")) { /* Lecture du prix dans Syteline. */ - $price = get_price($row[$col_id_to_num['Comment']]); + $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]; @@ -357,7 +354,7 @@ foreach ($data as $key => $row) { if (empty($mpn)) { /* Lecture du "MPN" dans Syteline si celui dans Altium est absent. */ - $mpn = get_mpn($row[$col_id_to_num['Comment']]); + $mpn = get_mpn($row[$col_id_to_num[COMPANY_PN_COL_NAME]]); } xls_write_cell($row_num, $k, $mpn, XLS_FORMAT_TEXT); @@ -368,7 +365,7 @@ foreach ($data as $key => $row) { if (empty($manufacturer)) { /* Lecture du "Manufacturer" dans Syteline si celui dans Altium est absent. */ - $manufacturer = get_manufacturer($row[$col_id_to_num['Comment']]); + $manufacturer = get_manufacturer($row[$col_id_to_num[COMPANY_PN_COL_NAME]]); } xls_write_cell($row_num, $k, $manufacturer, XLS_FORMAT_TEXT); @@ -377,7 +374,7 @@ foreach ($data as $key => $row) { } else { if ($width) { $value = $row[$c]; - + /* Ligne de données. */ xls_write_cell($row_num, $k, $value, XLS_FORMAT_TEXT); $k++; @@ -397,6 +394,14 @@ if ($add_cost == true) { $row_num . ")"); } +/* 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);