--- /dev/null
+<?php
+
+/*
+ * Copyright (C) 2013 Hugo Villeneuve <hugo@hugovil.com>
+ *
+ * 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),
+ );
+
+?>
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;
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";
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";
}
/*
case 'h':
usage();
exit(1);
+ case 't':
+ $bom_type = strtolower($o[1]);
+ break;
}
}
}
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. */
$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);