From: Hugo Villeneuve Date: Wed, 22 Jan 2014 15:58:33 +0000 (-0500) Subject: Refactor code for clarity X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;h=2ee718daae21b50456b8ef57ed81af3c3cd6f2e4;p=eda-utils.git Refactor code for clarity --- diff --git a/bomgen/Bomgen/functions.inc.php b/bomgen/Bomgen/functions.inc.php index 924374e..b72c239 100644 --- a/bomgen/Bomgen/functions.inc.php +++ b/bomgen/Bomgen/functions.inc.php @@ -296,4 +296,145 @@ function get_manufacturer($item) return $manufacturer; } +function xls_configure($workbook, $worksheet) +{ + global $col_id_to_xls_num, $csv_infos; + + /* Configration des couleurs de cellules. */ + xls_set_colors($workbook); + + foreach ($csv_infos as $key => $value) { + $width = $value['width']; + + /* Ajustement de la largeur de chaque colonne. */ + if ($width) { + /* S'assure que la colonne a été exportée dans le fichier CSV. */ + if (isset($col_id_to_xls_num[$key])) { + $worksheet->setColumn($col_id_to_xls_num[$key], $col_id_to_xls_num[$key], + $width); + } else { + echo "Colonne '$key' manquante.\n"; + } + } + } + + $worksheet->setLandscape(); + $worksheet->setPaper(PAPER_US_LETTER); +} + +/* + * value: clé + * c: Numéro de colonne dans le fichier CSV. + * xls_c: Numéro de colonne dans Excel. + */ +function determine_col_index($value, $c, $xls_c) +{ + global $col_id_to_num, $col_num_to_id, $col_id_to_xls_num; + + $col_id_to_num[$value] = $c; + $col_num_to_id[$c] = $value; + $col_id_to_xls_num[$value] = $xls_c; +} + +/* Conversion colonne numérique à lettre: + * 0 -> A + * 1 -> B + * ... + */ +function col_to_letter($c) +{ + $c = intval($c + 1); /* c part à 0. */ + + if ($c <= 0) + return ''; + + $letter = ''; + + while($c != 0) { + $p = ($c - 1) % 26; + $c = intval(($c - $p) / 26); + $letter = chr(65 + $p) . $letter; + } + + return $letter; +} + +function bom_import_source($filename) +{ + global $col_id_to_num, $num, $row_num, $add_cost, $csv_infos, $entete, $data; + + $handle = fopen($filename, "r"); + + if ($handle == FALSE) { + echo "Error opening file: " . $filename . "\n"; + exit(1); + } + + /* Read and import BOM source file. */ + while (($row = fgetcsv($handle, 1024, ',')) !== false) { + $num = count($row); + + if ($num < 2) { + /* Ligne vide. */ + continue; + } + + if ($row_num == 1) { + /* Entête. */ + + if ($add_cost == true) { + /* Ajout colonne temporaire pour le total cost. */ + $row[$num] = "TotalCost"; + $num++; + } + + /* Ajout colonne temporaire selon le type de composant. Cela est + * nécessaire pour faire le tri correctement. */ + $row[$num] = "Type"; + $num++; + + /* Index de la prochaine colonne disponible pour affichage. */ + $k = 0; + + for ($c = 0; $c < $num; $c++) { + $id = $row[$c]; + + determine_col_index($id, $c, $k); + + if (isset($csv_infos[$id])) { + if ($csv_infos[$id]['width'] != 0) { + $nom = $csv_infos[$id]['nom']; + + $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; + } + + $row_num++; + } + + fclose($handle); +} + ?> diff --git a/bomgen/bomgen.php b/bomgen/bomgen.php index 1581e01..4ee08ee 100755 --- a/bomgen/bomgen.php +++ b/bomgen/bomgen.php @@ -12,38 +12,21 @@ * (at your option) any later version. */ -require_once 'Bomgen/conf.inc.php'; -require_once 'Spreadsheet/Excel/Writer.php'; -require_once 'Bomgen/functions.inc.php'; -require_once 'Bomgen/refdes.inc.php'; - -$debug = 0; - -$digikey_bom = false; -$kits = 1; -$use_inventory = false; +require_once 'Console/Getopt.php'; define("XLS_DATA_ROW_START", 3); define("PAPER_US_LETTER", 1); -$short_opts = "hci:kn:t:"; +$debug = 0; -function usage() -{ - echo "Usage: bomgen.php [OPTIONS] [INPUT FILE]\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"; - echo " -c Add cost information to the BOM\n"; - echo " -i Specifiy inventory CSV file\n"; - 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"; -} +/* Default values. */ +$add_cost = false; +$digikey_bom = false; +$kits = 1; +$use_inventory = false; +$comp_total = 0; +$titre = "BOM"; /* * Clé = Nom de la colonne. @@ -65,67 +48,22 @@ $col_num_to_id = array(); $entete = array(); -/* - * value: clé - * c: Numéro de colonne dans le fichier CSV. - * xls_c: Numéro de colonne dans Excel. - */ -function determine_col_index($value, $c, $xls_c) -{ - global $col_id_to_num, $col_num_to_id, $col_id_to_xls_num; - - $col_id_to_num[$value] = $c; - $col_num_to_id[$c] = $value; - $col_id_to_xls_num[$value] = $xls_c; -} - -/* Conversion colonne numérique à lettre: - * 0 -> A - * 1 -> B - * ... - */ -function col_to_letter($c) -{ - $c = intval($c + 1); /* c part à 0. */ - - if ($c <= 0) - return ''; - - $letter = ''; - - while($c != 0) { - $p = ($c - 1) % 26; - $c = intval(($c - $p) / 26); - $letter = chr(65 + $p) . $letter; - } - - return $letter; -} +$short_opts = "hci:kn:t:"; -function xls_configure($workbook, $worksheet) +function usage() { - global $col_id_to_xls_num, $csv_infos; - - /* Configration des couleurs de cellules. */ - xls_set_colors($workbook); - - foreach ($csv_infos as $key => $value) { - $width = $value['width']; - - /* Ajustement de la largeur de chaque colonne. */ - if ($width) { - /* S'assure que la colonne a été exportée dans le fichier CSV. */ - if (isset($col_id_to_xls_num[$key])) { - $worksheet->setColumn($col_id_to_xls_num[$key], $col_id_to_xls_num[$key], - $width); - } else { - echo "Colonne '$key' manquante.\n"; - } - } - } - - $worksheet->setLandscape(); - $worksheet->setPaper(PAPER_US_LETTER); + echo "Usage: bomgen.php [OPTIONS] [INPUT FILE]\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"; + echo " -c Add cost information to the BOM\n"; + echo " -i Specifiy inventory CSV file\n"; + 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"; } if ($argc < 2) { @@ -134,11 +72,6 @@ if ($argc < 2) { exit(1); } -$add_cost = false; - -$comp_total = 0; -$titre = "BOM"; - $cg = new Console_Getopt(); /* Read the command line. */ @@ -151,7 +84,6 @@ $ret = $cg->getopt($args, $short_opts); if (PEAR::isError($ret)) { die ("Error in command line: " . $ret->getMessage() . "\n"); } - /* Now parse the options array. */ $opts = $ret[0]; @@ -209,9 +141,16 @@ default: break; } +/* These files must be included before others. */ require_once 'Bomgen/mapping.inc.php'; require_once 'Bomgen/digikey.inc.php'; +require_once 'Spreadsheet/Excel/Writer.php'; + +require_once 'Bomgen/conf.inc.php'; +require_once 'Bomgen/functions.inc.php'; +require_once 'Bomgen/refdes.inc.php'; + $dest = strip_ext($src); /* Importation du BOM CSV. */ @@ -224,78 +163,8 @@ if ($add_cost == false) { $csv_infos['UnitCost']['width'] = 0; } -$handle = fopen($src, "r"); - -if ($handle == FALSE) { - echo "Error opening file: " . $src . "\n"; - exit(1); -} - /* Read and import BOM source file. */ -while (($row = fgetcsv($handle, 1024, ',')) !== false) { - $num = count($row); - - if ($num < 2) { - /* Ligne vide. */ - continue; - } - - if ($row_num == 1) { - /* Entête. */ - - if ($add_cost == true) { - /* Ajout colonne temporaire pour le total cost. */ - $row[$num] = "TotalCost"; - $num++; - } - - /* Ajout colonne temporaire selon le type de composant. Cela est - * nécessaire pour faire le tri correctement. */ - $row[$num] = "Type"; - $num++; - - /* Index de la prochaine colonne disponible pour affichage. */ - $k = 0; - - for ($c = 0; $c < $num; $c++) { - $id = $row[$c]; - - determine_col_index($id, $c, $k); - - if (isset($csv_infos[$id])) { - if ($csv_infos[$id]['width'] != 0) { - $nom = $csv_infos[$id]['nom']; - - $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; - } - - $row_num++; -} - -fclose($handle); +bom_import_source($src); /* Combinaison de plusieurs lignes en une seule pour PCAD. */ refdes_combine($data, $num, $col_num_to_id, $col_id_to_num);