Made KiCAD bom import independent from the language of the header rows

This fixes issue #604
This commit is contained in:
Jan Böhmer 2024-04-29 00:09:10 +02:00
parent b3b2782f45
commit a96c10a13e

View file

@ -36,12 +36,12 @@ class BOMImporter
{ {
private const MAP_KICAD_PCB_FIELDS = [ private const MAP_KICAD_PCB_FIELDS = [
'ID' => 'Id', 0 => 'Id',
'Bezeichner' => 'Designator', 1 => 'Designator',
'Footprint' => 'Package', 2 => 'Package',
'Stückzahl' => 'Quantity', 3 => 'Quantity',
'Bezeichnung' => 'Designation', 4 => 'Designation',
'Anbieter und Referenz' => 'Supplier and ref', 5 => 'Supplier and ref',
]; ];
public function __construct() public function __construct()
@ -110,7 +110,7 @@ class BOMImporter
foreach ($csv->getRecords() as $offset => $entry) { foreach ($csv->getRecords() as $offset => $entry) {
//Translate the german field names to english //Translate the german field names to english
$entry = array_combine(array_map(static fn($key) => self::MAP_KICAD_PCB_FIELDS[$key] ?? $key, array_keys($entry)), $entry); $entry = $this->normalizeColumnNames($entry);
//Ensure that the entry has all required fields //Ensure that the entry has all required fields
if (!isset ($entry['Designator'])) { if (!isset ($entry['Designator'])) {
@ -137,4 +137,26 @@ class BOMImporter
return $bom_entries; return $bom_entries;
} }
/**
* This function uses the order of the fields in the CSV files to make them locale independent.
* @param array $entry
* @return array
*/
private function normalizeColumnNames(array $entry): array
{
$out = [];
//Map the entry order to the correct column names
foreach (array_values($entry) as $index => $field) {
if ($index > 5) {
break;
}
$new_index = self::MAP_KICAD_PCB_FIELDS[$index] ?? throw new \UnexpectedValueException('Invalid field index!');
$out[$new_index] = $field;
}
return $out;
}
} }