2023-11-29 20:17:17 +01:00
|
|
|
<?php
|
|
|
|
|
/*
|
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
2023-12-03 14:42:20 +01:00
|
|
|
namespace App\Services\EDA;
|
2023-11-29 20:17:17 +01:00
|
|
|
|
2026-02-08 09:57:10 +01:00
|
|
|
use App\Entity\Attachments\Attachment;
|
2023-11-29 20:17:17 +01:00
|
|
|
use App\Entity\Parts\Category;
|
2023-12-03 20:22:47 +01:00
|
|
|
use App\Entity\Parts\Footprint;
|
2023-11-29 20:57:11 +01:00
|
|
|
use App\Entity\Parts\Part;
|
2023-11-29 20:49:16 +01:00
|
|
|
use App\Services\Cache\ElementCacheTagGenerator;
|
2025-03-27 20:34:32 +01:00
|
|
|
use App\Services\EntityURLGenerator;
|
2023-11-29 20:17:17 +01:00
|
|
|
use App\Services\Trees\NodesListBuilder;
|
2024-08-04 19:09:01 +02:00
|
|
|
use App\Settings\MiscSettings\KiCadEDASettings;
|
2023-11-29 20:17:17 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2023-12-03 15:03:00 +01:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2023-11-29 21:28:06 +01:00
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
2023-11-29 20:17:17 +01:00
|
|
|
use Symfony\Contracts\Cache\ItemInterface;
|
|
|
|
|
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
2023-11-29 21:28:06 +01:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2023-11-29 20:17:17 +01:00
|
|
|
|
2023-12-03 14:42:20 +01:00
|
|
|
class KiCadHelper
|
2023-11-29 20:17:17 +01:00
|
|
|
{
|
|
|
|
|
|
2024-08-04 19:09:01 +02:00
|
|
|
/** @var int The maximum level of the shown categories. 0 Means only the top level categories are shown. -1 means only a single one containing */
|
|
|
|
|
private readonly int $category_depth;
|
|
|
|
|
|
2026-02-12 21:51:30 +01:00
|
|
|
/** @var bool Whether to resolve actual datasheet PDF URLs (true) or use Part-DB page links (false) */
|
|
|
|
|
private readonly bool $datasheetAsPdf;
|
|
|
|
|
|
2023-11-29 20:17:17 +01:00
|
|
|
public function __construct(
|
|
|
|
|
private readonly NodesListBuilder $nodesListBuilder,
|
|
|
|
|
private readonly TagAwareCacheInterface $kicadCache,
|
|
|
|
|
private readonly EntityManagerInterface $em,
|
2023-11-29 20:49:16 +01:00
|
|
|
private readonly ElementCacheTagGenerator $tagGenerator,
|
2023-11-29 21:28:06 +01:00
|
|
|
private readonly UrlGeneratorInterface $urlGenerator,
|
2025-03-27 20:34:32 +01:00
|
|
|
private readonly EntityURLGenerator $entityURLGenerator,
|
2023-11-29 21:28:06 +01:00
|
|
|
private readonly TranslatorInterface $translator,
|
2024-08-04 19:09:01 +02:00
|
|
|
KiCadEDASettings $kiCadEDASettings,
|
2023-11-29 21:28:06 +01:00
|
|
|
) {
|
2024-08-04 19:09:01 +02:00
|
|
|
$this->category_depth = $kiCadEDASettings->categoryDepth;
|
2026-02-12 21:51:30 +01:00
|
|
|
$this->datasheetAsPdf = $kiCadEDASettings->datasheetAsPdf;
|
2023-11-29 20:17:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an array of objects containing all categories in the database in the format required by KiCAD.
|
|
|
|
|
* The categories are flattened and sorted by their full path.
|
|
|
|
|
* Categories, which contain no parts, are filtered out.
|
|
|
|
|
* The result is cached for performance and invalidated on category changes.
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getCategories(): array
|
|
|
|
|
{
|
2023-12-03 14:42:20 +01:00
|
|
|
return $this->kicadCache->get('kicad_categories_' . $this->category_depth, function (ItemInterface $item) {
|
2023-11-29 20:17:17 +01:00
|
|
|
//Invalidate the cache on category changes
|
2023-11-29 20:49:16 +01:00
|
|
|
$secure_class_name = $this->tagGenerator->getElementTypeCacheTag(Category::class);
|
2023-11-29 20:17:17 +01:00
|
|
|
$item->tag($secure_class_name);
|
|
|
|
|
|
2025-03-29 12:01:26 +01:00
|
|
|
//Invalidate the cache on part changes (as the visibility depends on parts, and the parts can change)
|
|
|
|
|
$secure_class_name = $this->tagGenerator->getElementTypeCacheTag(Part::class);
|
|
|
|
|
$item->tag($secure_class_name);
|
|
|
|
|
|
2023-12-03 20:22:47 +01:00
|
|
|
//If the category depth is smaller than 0, create only one dummy category
|
|
|
|
|
if ($this->category_depth < 0) {
|
2023-12-03 15:03:00 +01:00
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
'id' => '0',
|
|
|
|
|
'name' => 'Part-DB',
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Otherwise just get the categories and filter them
|
|
|
|
|
|
2023-11-29 20:17:17 +01:00
|
|
|
$categories = $this->nodesListBuilder->typeToNodesList(Category::class);
|
|
|
|
|
$repo = $this->em->getRepository(Category::class);
|
|
|
|
|
$result = [];
|
|
|
|
|
foreach ($categories as $category) {
|
2023-11-30 19:34:50 +01:00
|
|
|
//Skip invisible categories
|
2023-12-03 15:29:17 +01:00
|
|
|
if ($category->getEdaInfo()->getVisibility() === false) {
|
2023-11-30 19:34:50 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-03 14:42:20 +01:00
|
|
|
//Skip categories with a depth greater than the configured one
|
|
|
|
|
if ($category->getLevel() > $this->category_depth) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 20:17:17 +01:00
|
|
|
//Ensure that the category contains parts
|
2023-12-03 14:42:20 +01:00
|
|
|
//For the last level, we need to use a recursive query, otherwise we can use a simple query
|
2023-12-03 15:11:06 +01:00
|
|
|
/** @var Category $category */
|
2023-12-03 14:42:20 +01:00
|
|
|
$parts_count = $category->getLevel() >= $this->category_depth ? $repo->getPartsCountRecursive($category) : $repo->getPartsCount($category);
|
|
|
|
|
|
|
|
|
|
if ($parts_count < 1) {
|
2023-11-29 20:17:17 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-03 20:22:47 +01:00
|
|
|
//Check if the category should be visible
|
|
|
|
|
if (!$this->shouldCategoryBeVisible($category)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 20:17:17 +01:00
|
|
|
//Format the category for KiCAD
|
2026-02-11 00:29:34 +01:00
|
|
|
// Use the category comment as description if available, otherwise use the Part-DB URL
|
|
|
|
|
$description = $category->getComment();
|
|
|
|
|
if ($description === null || $description === '') {
|
|
|
|
|
$description = $this->entityURLGenerator->listPartsURL($category);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 20:17:17 +01:00
|
|
|
$result[] = [
|
2023-11-29 21:28:06 +01:00
|
|
|
'id' => (string)$category->getId(),
|
2023-11-29 20:17:17 +01:00
|
|
|
'name' => $category->getFullPath('/'),
|
2026-02-11 00:29:34 +01:00
|
|
|
'description' => $description,
|
2023-11-29 20:17:17 +01:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an array of objects containing all parts for the given category in the format required by KiCAD.
|
|
|
|
|
* The result is cached for performance and invalidated on category or part changes.
|
2023-12-03 15:03:00 +01:00
|
|
|
* @param Category|null $category
|
2026-02-11 00:29:34 +01:00
|
|
|
* @param bool $minimal If true, only return id and name (faster for symbol chooser listing)
|
2023-11-29 20:17:17 +01:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2026-02-11 00:29:34 +01:00
|
|
|
public function getCategoryParts(?Category $category, bool $minimal = false): array
|
2023-11-29 20:17:17 +01:00
|
|
|
{
|
2026-02-11 00:29:34 +01:00
|
|
|
$cacheKey = 'kicad_category_parts_'.($category?->getID() ?? 0) . '_' . $this->category_depth . ($minimal ? '_min' : '');
|
|
|
|
|
return $this->kicadCache->get($cacheKey,
|
2023-11-29 21:28:06 +01:00
|
|
|
function (ItemInterface $item) use ($category) {
|
|
|
|
|
$item->tag([
|
|
|
|
|
$this->tagGenerator->getElementTypeCacheTag(Category::class),
|
2023-12-03 20:22:47 +01:00
|
|
|
$this->tagGenerator->getElementTypeCacheTag(Part::class),
|
|
|
|
|
//Visibility can change based on the footprint
|
|
|
|
|
$this->tagGenerator->getElementTypeCacheTag(Footprint::class)
|
2023-11-29 21:28:06 +01:00
|
|
|
]);
|
2023-11-29 20:17:17 +01:00
|
|
|
|
2023-12-03 15:03:00 +01:00
|
|
|
if ($this->category_depth >= 0) {
|
|
|
|
|
//Ensure that the category is set
|
2024-06-22 00:31:43 +02:00
|
|
|
if ($category === null) {
|
2023-12-03 15:03:00 +01:00
|
|
|
throw new NotFoundHttpException('Category must be set, if category_depth is greater than 1!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$category_repo = $this->em->getRepository(Category::class);
|
|
|
|
|
if ($category->getLevel() >= $this->category_depth) {
|
|
|
|
|
//Get all parts for the category and its children
|
|
|
|
|
$parts = $category_repo->getPartsRecursive($category);
|
|
|
|
|
} else {
|
|
|
|
|
//Get only direct parts for the category (without children), as the category is not collapsed
|
|
|
|
|
$parts = $category_repo->getParts($category);
|
|
|
|
|
}
|
2023-12-03 14:42:20 +01:00
|
|
|
} else {
|
2023-12-03 15:03:00 +01:00
|
|
|
//Get all parts
|
|
|
|
|
$parts = $this->em->getRepository(Part::class)->findAll();
|
2023-12-03 14:42:20 +01:00
|
|
|
}
|
2023-11-29 20:17:17 +01:00
|
|
|
|
2023-11-29 21:28:06 +01:00
|
|
|
$result = [];
|
|
|
|
|
foreach ($parts as $part) {
|
2023-11-30 19:34:50 +01:00
|
|
|
//If the part is invisible, then skip it
|
2023-12-03 20:22:47 +01:00
|
|
|
if (!$this->shouldPartBeVisible($part)) {
|
2023-11-30 19:34:50 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 21:28:06 +01:00
|
|
|
$result[] = [
|
|
|
|
|
'id' => (string)$part->getId(),
|
|
|
|
|
'name' => $part->getName(),
|
|
|
|
|
'description' => $part->getDescription(),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-11 00:29:34 +01:00
|
|
|
/**
|
|
|
|
|
* @param bool $apiV2 If true, use API v2 format with volatile field support
|
|
|
|
|
*/
|
|
|
|
|
public function getKiCADPart(Part $part, bool $apiV2 = false): array
|
2023-11-29 21:28:06 +01:00
|
|
|
{
|
|
|
|
|
$result = [
|
|
|
|
|
'id' => (string)$part->getId(),
|
|
|
|
|
'name' => $part->getName(),
|
2023-11-30 19:34:50 +01:00
|
|
|
"symbolIdStr" => $part->getEdaInfo()->getKicadSymbol() ?? $part->getCategory()?->getEdaInfo()->getKicadSymbol() ?? "",
|
|
|
|
|
"exclude_from_bom" => $this->boolToKicadBool($part->getEdaInfo()->getExcludeFromBom() ?? $part->getCategory()?->getEdaInfo()->getExcludeFromBom() ?? false),
|
|
|
|
|
"exclude_from_board" => $this->boolToKicadBool($part->getEdaInfo()->getExcludeFromBoard() ?? $part->getCategory()?->getEdaInfo()->getExcludeFromBoard() ?? false),
|
2026-01-18 22:35:37 +01:00
|
|
|
"exclude_from_sim" => $this->boolToKicadBool($part->getEdaInfo()->getExcludeFromSim() ?? $part->getCategory()?->getEdaInfo()->getExcludeFromSim() ?? false),
|
2023-11-29 21:28:06 +01:00
|
|
|
"fields" => []
|
|
|
|
|
];
|
|
|
|
|
|
2023-11-30 19:34:50 +01:00
|
|
|
$result["fields"]["footprint"] = $this->createField($part->getEdaInfo()->getKicadFootprint() ?? $part->getFootprint()?->getEdaInfo()->getKicadFootprint() ?? "");
|
2024-03-16 18:31:35 +01:00
|
|
|
$result["fields"]["reference"] = $this->createField($part->getEdaInfo()->getReferencePrefix() ?? $part->getCategory()?->getEdaInfo()->getReferencePrefix() ?? 'U', true);
|
2023-11-30 19:34:50 +01:00
|
|
|
$result["fields"]["value"] = $this->createField($part->getEdaInfo()->getValue() ?? $part->getName(), true);
|
2023-11-29 21:28:06 +01:00
|
|
|
$result["fields"]["keywords"] = $this->createField($part->getTags());
|
|
|
|
|
|
2026-02-08 09:57:10 +01:00
|
|
|
//Use the part info page as Part-DB link. It must be an absolute URL.
|
|
|
|
|
$partUrl = $this->urlGenerator->generate(
|
|
|
|
|
'part_info',
|
|
|
|
|
['id' => $part->getId()],
|
|
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL
|
2023-11-29 21:28:06 +01:00
|
|
|
);
|
|
|
|
|
|
2026-02-12 21:51:30 +01:00
|
|
|
//Try to find an actual datasheet attachment (configurable: PDF URL vs Part-DB page link)
|
|
|
|
|
if ($this->datasheetAsPdf) {
|
|
|
|
|
$datasheetUrl = $this->findDatasheetUrl($part);
|
|
|
|
|
$result["fields"]["datasheet"] = $this->createField($datasheetUrl ?? $partUrl);
|
|
|
|
|
} else {
|
|
|
|
|
$result["fields"]["datasheet"] = $this->createField($partUrl);
|
|
|
|
|
}
|
2026-02-08 09:57:10 +01:00
|
|
|
$result["fields"]["Part-DB URL"] = $this->createField($partUrl);
|
|
|
|
|
|
2023-11-29 21:28:06 +01:00
|
|
|
//Add basic fields
|
|
|
|
|
$result["fields"]["description"] = $this->createField($part->getDescription());
|
2024-06-22 00:31:43 +02:00
|
|
|
if ($part->getCategory() !== null) {
|
2023-11-29 21:28:06 +01:00
|
|
|
$result["fields"]["Category"] = $this->createField($part->getCategory()->getFullPath('/'));
|
|
|
|
|
}
|
2024-06-22 00:31:43 +02:00
|
|
|
if ($part->getManufacturer() !== null) {
|
2023-11-29 21:28:06 +01:00
|
|
|
$result["fields"]["Manufacturer"] = $this->createField($part->getManufacturer()->getName());
|
|
|
|
|
}
|
|
|
|
|
if ($part->getManufacturerProductNumber() !== "") {
|
|
|
|
|
$result['fields']["MPN"] = $this->createField($part->getManufacturerProductNumber());
|
|
|
|
|
}
|
2024-06-22 00:31:43 +02:00
|
|
|
if ($part->getManufacturingStatus() !== null) {
|
2023-11-29 21:28:06 +01:00
|
|
|
$result["fields"]["Manufacturing Status"] = $this->createField(
|
2023-11-30 19:34:50 +01:00
|
|
|
//Always use the english translation
|
2023-11-29 21:28:06 +01:00
|
|
|
$this->translator->trans($part->getManufacturingStatus()->toTranslationKey(), locale: 'en')
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-06-22 00:31:43 +02:00
|
|
|
if ($part->getFootprint() !== null) {
|
2023-11-29 21:28:06 +01:00
|
|
|
$result["fields"]["Part-DB Footprint"] = $this->createField($part->getFootprint()->getName());
|
|
|
|
|
}
|
2024-06-22 00:31:43 +02:00
|
|
|
if ($part->getPartUnit() !== null) {
|
2023-11-29 21:28:06 +01:00
|
|
|
$unit = $part->getPartUnit()->getName();
|
|
|
|
|
if ($part->getPartUnit()->getUnit() !== "") {
|
2023-11-30 19:34:50 +01:00
|
|
|
$unit .= ' ('.$part->getPartUnit()->getUnit().')';
|
2023-11-29 20:57:11 +01:00
|
|
|
}
|
2023-11-29 21:28:06 +01:00
|
|
|
$result["fields"]["Part-DB Unit"] = $this->createField($unit);
|
|
|
|
|
}
|
2025-10-27 21:58:16 +01:00
|
|
|
if ($part->getPartCustomState() !== null) {
|
|
|
|
|
$customState = $part->getPartCustomState()->getName();
|
|
|
|
|
$result["fields"]["Part-DB Custom state"] = $this->createField($customState);
|
|
|
|
|
}
|
2023-11-29 21:28:06 +01:00
|
|
|
if ($part->getMass()) {
|
2023-11-30 19:34:50 +01:00
|
|
|
$result["fields"]["Mass"] = $this->createField($part->getMass() . ' g');
|
2023-11-29 21:28:06 +01:00
|
|
|
}
|
|
|
|
|
$result["fields"]["Part-DB ID"] = $this->createField($part->getId());
|
2024-06-22 00:31:43 +02:00
|
|
|
if ($part->getIpn() !== null && $part->getIpn() !== '' && $part->getIpn() !== '0') {
|
2023-11-29 21:28:06 +01:00
|
|
|
$result["fields"]["Part-DB IPN"] = $this->createField($part->getIpn());
|
|
|
|
|
}
|
2023-11-29 20:57:11 +01:00
|
|
|
|
2026-02-11 00:29:34 +01:00
|
|
|
//Add KiCost manufacturer fields (always present, independent of orderdetails)
|
|
|
|
|
if ($part->getManufacturer() !== null) {
|
|
|
|
|
$result["fields"]["manf"] = $this->createField($part->getManufacturer()->getName());
|
|
|
|
|
}
|
|
|
|
|
if ($part->getManufacturerProductNumber() !== "") {
|
|
|
|
|
$result['fields']['manf#'] = $this->createField($part->getManufacturerProductNumber());
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 13:12:04 +02:00
|
|
|
// Add supplier information from orderdetails (include obsolete orderdetails)
|
2026-02-11 00:29:34 +01:00
|
|
|
// If any orderdetail has kicad_export=true, only export those; otherwise export all (backward compat)
|
|
|
|
|
$allOrderdetails = $part->getOrderdetails(false);
|
|
|
|
|
if ($allOrderdetails->count() > 0) {
|
|
|
|
|
$hasKicadExportFlag = false;
|
|
|
|
|
foreach ($allOrderdetails as $od) {
|
|
|
|
|
if ($od->isKicadExport()) {
|
|
|
|
|
$hasKicadExportFlag = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 13:12:04 +02:00
|
|
|
$supplierCounts = [];
|
2026-02-11 00:29:34 +01:00
|
|
|
foreach ($allOrderdetails as $orderdetail) {
|
2025-07-06 13:12:04 +02:00
|
|
|
if ($orderdetail->getSupplier() !== null && $orderdetail->getSupplierPartNr() !== '') {
|
2026-02-11 00:29:34 +01:00
|
|
|
// Skip orderdetails not marked for export when the flag is used
|
|
|
|
|
if ($hasKicadExportFlag && !$orderdetail->isKicadExport()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-07-06 13:12:04 +02:00
|
|
|
|
2026-02-11 00:29:34 +01:00
|
|
|
$supplierName = $orderdetail->getSupplier()->getName() . ' SPN';
|
2025-07-06 13:12:04 +02:00
|
|
|
|
|
|
|
|
if (!isset($supplierCounts[$supplierName])) {
|
|
|
|
|
$supplierCounts[$supplierName] = 0;
|
|
|
|
|
}
|
|
|
|
|
$supplierCounts[$supplierName]++;
|
2026-02-11 00:29:34 +01:00
|
|
|
|
|
|
|
|
// Create field name with sequential number if more than one from same supplier
|
|
|
|
|
$fieldName = $supplierCounts[$supplierName] > 1
|
2025-07-06 13:12:04 +02:00
|
|
|
? $supplierName . ' ' . $supplierCounts[$supplierName]
|
|
|
|
|
: $supplierName;
|
2025-07-06 13:58:51 +02:00
|
|
|
|
|
|
|
|
$result["fields"][$fieldName] = $this->createField($orderdetail->getSupplierPartNr());
|
2026-02-11 00:29:34 +01:00
|
|
|
|
|
|
|
|
//Also add a KiCost-compatible field (supplier_name# = SPN)
|
|
|
|
|
$kicostFieldName = mb_strtolower($orderdetail->getSupplier()->getName()) . '#';
|
|
|
|
|
$result["fields"][$kicostFieldName] = $this->createField($orderdetail->getSupplierPartNr());
|
2025-07-06 13:58:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-29 21:28:06 +01:00
|
|
|
|
2026-02-08 10:46:53 +01:00
|
|
|
//Add stock quantity and storage locations (only count non-expired lots with known quantity)
|
2026-02-08 09:57:10 +01:00
|
|
|
$totalStock = 0;
|
|
|
|
|
$locations = [];
|
|
|
|
|
foreach ($part->getPartLots() as $lot) {
|
2026-02-08 10:46:53 +01:00
|
|
|
$isAvailable = !$lot->isInstockUnknown() && $lot->isExpired() !== true;
|
|
|
|
|
if ($isAvailable) {
|
2026-02-08 09:57:10 +01:00
|
|
|
$totalStock += $lot->getAmount();
|
2026-02-08 10:46:53 +01:00
|
|
|
if ($lot->getAmount() > 0 && $lot->getStorageLocation() !== null) {
|
|
|
|
|
$locations[] = $lot->getStorageLocation()->getName();
|
|
|
|
|
}
|
2026-02-08 09:57:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-11 00:29:34 +01:00
|
|
|
// In API v2, stock and location are volatile (shown but not saved to schematic)
|
|
|
|
|
$result['fields']['Stock'] = $this->createField($totalStock, false, $apiV2);
|
2026-02-08 09:57:10 +01:00
|
|
|
if ($locations !== []) {
|
2026-02-11 00:29:34 +01:00
|
|
|
$result['fields']['Storage Location'] = $this->createField(implode(', ', array_unique($locations)), false, $apiV2);
|
2026-02-08 09:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-08 21:46:28 +01:00
|
|
|
//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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 21:28:06 +01:00
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-03 20:22:47 +01:00
|
|
|
/**
|
|
|
|
|
* Determine if the given part should be visible for the EDA.
|
|
|
|
|
* @param Category $category
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function shouldCategoryBeVisible(Category $category): bool
|
|
|
|
|
{
|
|
|
|
|
$eda_info = $category->getEdaInfo();
|
|
|
|
|
|
|
|
|
|
//If the category visibility is explicitly set, then use it
|
|
|
|
|
if ($eda_info->getVisibility() !== null) {
|
|
|
|
|
return $eda_info->getVisibility();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//try to check if the fields were set
|
|
|
|
|
if ($eda_info->getKicadSymbol() !== null
|
|
|
|
|
|| $eda_info->getReferencePrefix() !== null) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Check if there is any part in this category, which should be visible
|
|
|
|
|
$category_repo = $this->em->getRepository(Category::class);
|
|
|
|
|
if ($category->getLevel() >= $this->category_depth) {
|
|
|
|
|
//Get all parts for the category and its children
|
|
|
|
|
$parts = $category_repo->getPartsRecursive($category);
|
|
|
|
|
} else {
|
|
|
|
|
//Get only direct parts for the category (without children), as the category is not collapsed
|
|
|
|
|
$parts = $category_repo->getParts($category);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
|
if ($this->shouldPartBeVisible($part)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Otherwise the category should be not visible
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the given part should be visible for the EDA.
|
|
|
|
|
* @param Part $part
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function shouldPartBeVisible(Part $part): bool
|
|
|
|
|
{
|
|
|
|
|
$eda_info = $part->getEdaInfo();
|
|
|
|
|
$category = $part->getCategory();
|
|
|
|
|
|
|
|
|
|
//If the user set a visibility, then use it
|
|
|
|
|
if ($eda_info->getVisibility() !== null) {
|
2026-02-11 00:29:34 +01:00
|
|
|
return $eda_info->getVisibility();
|
2023-12-03 20:22:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//If the part has a category, then use the category visibility if possible
|
|
|
|
|
if ($category && $category->getEdaInfo()->getVisibility() !== null) {
|
|
|
|
|
return $category->getEdaInfo()->getVisibility();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//If both are null, then we try to determine the visibility based on if fields are set
|
|
|
|
|
if ($eda_info->getKicadSymbol() !== null
|
|
|
|
|
|| $eda_info->getKicadFootprint() !== null
|
|
|
|
|
|| $eda_info->getReferencePrefix() !== null
|
|
|
|
|
|| $eda_info->getValue() !== null) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Check also if the fields are set for the category (if it exists)
|
|
|
|
|
if ($category && (
|
|
|
|
|
$category->getEdaInfo()->getKicadSymbol() !== null
|
|
|
|
|
|| $category->getEdaInfo()->getReferencePrefix() !== null
|
|
|
|
|
)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
//And on the footprint
|
|
|
|
|
//Otherwise the part should be not visible
|
2024-06-22 00:31:43 +02:00
|
|
|
return $part->getFootprint() && $part->getFootprint()->getEdaInfo()->getKicadFootprint() !== null;
|
2023-12-03 20:22:47 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-29 21:28:06 +01:00
|
|
|
/**
|
|
|
|
|
* Converts a boolean value to the format required by KiCAD.
|
|
|
|
|
* @param bool $value
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function boolToKicadBool(bool $value): string
|
|
|
|
|
{
|
|
|
|
|
return $value ? 'True' : 'False';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a field array for KiCAD
|
|
|
|
|
* @param string|int|float $value
|
|
|
|
|
* @param bool $visible
|
2026-02-11 00:29:34 +01:00
|
|
|
* @param bool $volatile If true (API v2), field is shown in KiCad but NOT saved to schematic
|
2023-11-29 21:28:06 +01:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2026-02-11 00:29:34 +01:00
|
|
|
private function createField(string|int|float $value, bool $visible = false, bool $volatile = false): array
|
2023-11-29 21:28:06 +01:00
|
|
|
{
|
2026-02-11 00:29:34 +01:00
|
|
|
$field = [
|
2023-11-29 21:28:06 +01:00
|
|
|
'value' => (string)$value,
|
|
|
|
|
'visible' => $this->boolToKicadBool($visible),
|
|
|
|
|
];
|
2026-02-11 00:29:34 +01:00
|
|
|
|
|
|
|
|
if ($volatile) {
|
|
|
|
|
$field['volatile'] = $this->boolToKicadBool(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $field;
|
2023-11-29 20:17:17 +01:00
|
|
|
}
|
2026-02-08 09:57:10 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Finds the URL to the actual datasheet file for the given part.
|
|
|
|
|
* Searches attachments by type name, attachment name, and file extension.
|
|
|
|
|
* @return string|null The datasheet URL, or null if no datasheet was found.
|
|
|
|
|
*/
|
|
|
|
|
private function findDatasheetUrl(Part $part): ?string
|
|
|
|
|
{
|
|
|
|
|
$firstPdf = null;
|
|
|
|
|
|
|
|
|
|
foreach ($part->getAttachments() as $attachment) {
|
|
|
|
|
//Check if the attachment type name contains "datasheet"
|
|
|
|
|
$typeName = $attachment->getAttachmentType()?->getName() ?? '';
|
|
|
|
|
if (str_contains(mb_strtolower($typeName), 'datasheet')) {
|
|
|
|
|
return $this->getAttachmentUrl($attachment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Check if the attachment name contains "datasheet"
|
|
|
|
|
$name = mb_strtolower($attachment->getName());
|
|
|
|
|
if (str_contains($name, 'datasheet') || str_contains($name, 'data sheet')) {
|
|
|
|
|
return $this->getAttachmentUrl($attachment);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 10:37:37 +01:00
|
|
|
//Track first PDF as fallback (check internal extension or external URL path)
|
|
|
|
|
if ($firstPdf === null) {
|
|
|
|
|
$extension = $attachment->getExtension();
|
|
|
|
|
if ($extension === null && $attachment->hasExternal()) {
|
2026-02-08 10:46:53 +01:00
|
|
|
$urlPath = parse_url($attachment->getExternalPath(), PHP_URL_PATH);
|
|
|
|
|
$extension = is_string($urlPath) ? strtolower(pathinfo($urlPath, PATHINFO_EXTENSION)) : null;
|
2026-02-08 10:37:37 +01:00
|
|
|
}
|
|
|
|
|
if ($extension === 'pdf') {
|
|
|
|
|
$firstPdf = $attachment;
|
|
|
|
|
}
|
2026-02-08 09:57:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Use first PDF attachment as fallback
|
|
|
|
|
if ($firstPdf !== null) {
|
|
|
|
|
return $this->getAttachmentUrl($firstPdf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an absolute URL for viewing the given attachment.
|
|
|
|
|
* Prefers the external URL (direct link) over the internal view route.
|
|
|
|
|
*/
|
|
|
|
|
private function getAttachmentUrl(Attachment $attachment): string
|
|
|
|
|
{
|
|
|
|
|
if ($attachment->hasExternal()) {
|
|
|
|
|
return $attachment->getExternalPath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->urlGenerator->generate(
|
|
|
|
|
'attachment_view',
|
|
|
|
|
['id' => $attachment->getId()],
|
|
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-11-29 20:17:17 +01:00
|
|
|
}
|