From 9178154986c8af4152cd4e59a2328b002e9f4a18 Mon Sep 17 00:00:00 2001 From: Sebastian Almberg <83243306+Sebbeben@users.noreply.github.com> Date: Sun, 8 Feb 2026 21:46:28 +0100 Subject: [PATCH] Add configurable KiCad field export for part parameters Add a kicad_export checkbox to parameters, allowing users to control which specifications appear as fields in the KiCad HTTP library API. Parameters with kicad_export enabled are included using their formatted value, without overwriting hardcoded fields like description or Stock. --- migrations/Version20260208190000.php | 47 +++++++++++ src/Entity/Parameters/AbstractParameter.php | 22 +++++ src/Form/ParameterType.php | 6 ++ src/Services/EDA/KiCadHelper.php | 11 +++ .../parts/edit/_specifications.html.twig | 1 + .../parts/edit/edit_form_styles.html.twig | 1 + tests/Services/EDA/KiCadHelperTest.php | 80 +++++++++++++++++++ translations/messages.en.xlf | 6 ++ 8 files changed, 174 insertions(+) create mode 100644 migrations/Version20260208190000.php diff --git a/migrations/Version20260208190000.php b/migrations/Version20260208190000.php new file mode 100644 index 00000000..3ff1a80d --- /dev/null +++ b/migrations/Version20260208190000.php @@ -0,0 +1,47 @@ +addSql('ALTER TABLE parameters ADD kicad_export TINYINT(1) NOT NULL DEFAULT 0'); + } + + public function mySQLDown(Schema $schema): void + { + $this->addSql('ALTER TABLE parameters DROP COLUMN kicad_export'); + } + + public function sqLiteUp(Schema $schema): void + { + $this->addSql('ALTER TABLE parameters ADD COLUMN kicad_export BOOLEAN NOT NULL DEFAULT 0'); + } + + public function sqLiteDown(Schema $schema): void + { + // SQLite does not support DROP COLUMN in older versions; recreate table if needed + $this->addSql('ALTER TABLE parameters DROP COLUMN kicad_export'); + } + + public function postgreSQLUp(Schema $schema): void + { + $this->addSql('ALTER TABLE parameters ADD kicad_export BOOLEAN NOT NULL DEFAULT FALSE'); + } + + public function postgreSQLDown(Schema $schema): void + { + $this->addSql('ALTER TABLE parameters DROP COLUMN kicad_export'); + } +} diff --git a/src/Entity/Parameters/AbstractParameter.php b/src/Entity/Parameters/AbstractParameter.php index d84e68ad..2762657a 100644 --- a/src/Entity/Parameters/AbstractParameter.php +++ b/src/Entity/Parameters/AbstractParameter.php @@ -172,6 +172,13 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu #[Assert\Length(max: 255)] protected string $group = ''; + /** + * @var bool Whether this parameter should be exported as a KiCad field in the EDA HTTP library API + */ + #[Groups(['full', 'parameter:read', 'parameter:write', 'import'])] + #[ORM\Column(type: Types::BOOLEAN)] + protected bool $kicad_export = false; + /** * Mapping is done in subclasses. * @@ -471,6 +478,21 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu return static::ALLOWED_ELEMENT_CLASS; } + public function isKicadExport(): bool + { + return $this->kicad_export; + } + + /** + * @return $this + */ + public function setKicadExport(bool $kicad_export): self + { + $this->kicad_export = $kicad_export; + + return $this; + } + public function getComparableFields(): array { return ['name' => $this->name, 'group' => $this->group, 'element' => $this->element?->getId()]; diff --git a/src/Form/ParameterType.php b/src/Form/ParameterType.php index 4c2174ae..3a773f4e 100644 --- a/src/Form/ParameterType.php +++ b/src/Form/ParameterType.php @@ -55,6 +55,7 @@ use App\Entity\Parameters\SupplierParameter; use App\Entity\Parts\MeasurementUnit; use App\Form\Type\ExponentialNumberType; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\NumberType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; @@ -147,6 +148,11 @@ class ParameterType extends AbstractType 'class' => 'form-control-sm', ], ]); + + $builder->add('kicad_export', CheckboxType::class, [ + 'label' => false, + 'required' => false, + ]); } public function finishView(FormView $view, FormInterface $form, array $options): void diff --git a/src/Services/EDA/KiCadHelper.php b/src/Services/EDA/KiCadHelper.php index 37b94f33..1617e886 100644 --- a/src/Services/EDA/KiCadHelper.php +++ b/src/Services/EDA/KiCadHelper.php @@ -311,6 +311,17 @@ class KiCadHelper $result['fields']['Storage Location'] = $this->createField(implode(', ', array_unique($locations))); } + //Add parameters marked for KiCad export + foreach ($part->getParameters() as $parameter) { + if ($parameter->isKicadExport() && $parameter->getName() !== '') { + $fieldName = $parameter->getName(); + //Don't overwrite hardcoded fields + if (!isset($result['fields'][$fieldName])) { + $result['fields'][$fieldName] = $this->createField($parameter->getFormattedValue()); + } + } + } + return $result; } diff --git a/templates/parts/edit/_specifications.html.twig b/templates/parts/edit/_specifications.html.twig index 25b00133..3226e2c0 100644 --- a/templates/parts/edit/_specifications.html.twig +++ b/templates/parts/edit/_specifications.html.twig @@ -14,6 +14,7 @@ {% trans %}specifications.unit{% endtrans %} {% trans %}specifications.text{% endtrans %} {% trans %}specifications.group{% endtrans %} + diff --git a/templates/parts/edit/edit_form_styles.html.twig b/templates/parts/edit/edit_form_styles.html.twig index 844c8700..5376f754 100644 --- a/templates/parts/edit/edit_form_styles.html.twig +++ b/templates/parts/edit/edit_form_styles.html.twig @@ -79,6 +79,7 @@ {{ form_widget(form.unit, {"attr": {"data-pages--parameters-autocomplete-target": "unit", "data-pages--latex-preview-target": "input"}}) }}{{ form_errors(form.unit) }} {{ form_widget(form.value_text) }}{{ form_errors(form.value_text) }} {{ form_widget(form.group) }}{{ form_errors(form.group) }} + {{ form_widget(form.kicad_export) }}