1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-17 21:00:09 -07:00

Fix loading of inverse submodels

This commit is contained in:
David Hübner 2017-05-05 21:07:40 +02:00
parent ff00c761dc
commit bfcd36d4ff
6 changed files with 84 additions and 35 deletions

View File

@ -17,7 +17,7 @@ services:
service.loader.relation: service.loader.relation:
class: AppBundle\Service\Loader\RelationLoader class: AppBundle\Service\Loader\RelationLoader
arguments: ['@api.manager.rebrickable', '@app.relation.mapper'] arguments: ['@app.relation.mapper','@repository.ldraw.model', '@repository.rebrickable.part']
parent: service.loader.base parent: service.loader.base
service.loader.image: service.loader.image:

View File

@ -0,0 +1,7 @@
<?php
namespace AppBundle\Exception\RelationMapper;
class InvalidResourceException extends \LogicException
{
}

View File

@ -0,0 +1,16 @@
<?php
namespace AppBundle\Exception\RelationMapper;
use AppBundle\Exception\FileException;
use Throwable;
class ResourceNotFoundException extends FileException
{
public function __construct($path, $message = '', $code = 0, Throwable $previous = null)
{
$message = sprintf('Resource "%s" not found.', $path);
parent::__construct($path, $message, $code, $previous);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace AppBundle\Exception\Stl;
use Symfony\Component\Form\Exception\LogicException;
use Throwable;
class LDLibraryMissingException extends LogicException
{
public function __construct($message = '', $code = 0, Throwable $previous = null)
{
$message = 'Invalid LDraw library.';
parent::__construct($message, $code, $previous);
}
}

View File

@ -2,40 +2,46 @@
namespace AppBundle\Service\Loader; namespace AppBundle\Service\Loader;
use AppBundle\Api\Manager\RebrickableManager;
use AppBundle\Entity\LDraw\Model; use AppBundle\Entity\LDraw\Model;
use AppBundle\Entity\Rebrickable\Part; use AppBundle\Entity\Rebrickable\Part;
use AppBundle\Utils\RelationMapper; use AppBundle\Repository\LDraw\ModelRepository;
use AppBundle\Repository\Rebrickable\PartRepository;
use AppBundle\Util\RelationMapper;
class RelationLoader extends BaseLoader class RelationLoader extends BaseLoader
{ {
/** @var RelationMapper */ /** @var RelationMapper */
private $relationMapper; private $relationMapper;
/** @var RebrickableManager */ /** @var ModelRepository */
private $rebrickableAPIManager; private $modelRepository;
/** @var PartRepository */
private $partRepository;
/** /**
* RelationLoader constructor. * RelationLoader constructor.
* *
* @param RebrickableManager $rebrickableApiManager * @param RelationMapper $relationMapper
* @param RelationMapper $relationMapper * @param ModelRepository $modelRepository
* @param PartRepository $partRepository
*/ */
public function __construct($rebrickableApiManager, $relationMapper) public function __construct($relationMapper, $modelRepository, $partRepository)
{ {
$this->rebrickableAPIManager = $rebrickableApiManager;
$this->relationMapper = $relationMapper; $this->relationMapper = $relationMapper;
$this->modelRepository = $modelRepository;
$this->partRepository = $partRepository;
} }
public function loadAll() public function loadAll()
{ {
$parts = $this->em->getRepository(Part::class)->findAll(); $parts = $this->partRepository->findAll();
$this->load($parts); $this->load($parts);
} }
public function loadNotPaired($parts) public function loadNotPaired($parts)
{ {
$parts = $this->em->getRepository(Part::class)->findAllNotPaired(); $parts = $this->partRepository->findAllNotPaired();
$this->load($parts); $this->load($parts);
} }
@ -44,7 +50,13 @@ class RelationLoader extends BaseLoader
$this->initProgressBar(count($parts)); $this->initProgressBar(count($parts));
/** @var Part $part */ /** @var Part $part */
foreach ($parts as $part) { foreach ($parts as $part) {
$this->loadPartRelation($part); $model = $this->loadPartRelation($part);
if ($model) {
$part->setModel($model);
$this->partRepository->save($part);
}
$this->progressBar->setMessage($part->getNumber()); $this->progressBar->setMessage($part->getNumber());
$this->progressBar->advance(); $this->progressBar->advance();
} }
@ -56,33 +68,28 @@ class RelationLoader extends BaseLoader
* *
* @param Part $part * @param Part $part
* *
* @return Model $m * @return Model|null
*/ */
private function loadPartRelation(Part $part) private function loadPartRelation(Part $part)
{ {
$modelRepository = $this->em->getRepository(Model::class);
$number = $part->getNumber(); $number = $part->getNumber();
$model = $modelRepository->findOneByNumber($number); $model = $this->modelRepository->findOneByNumber($number);
if (!$model) { if (!$model) {
$number = $this->relationMapper->find($this->getPrintedParentId($number), 'part_model'); $number = $this->relationMapper->find($this->getPrintedParentId($number), 'part_model');
$model = $modelRepository->findOneByNumber($number); $model = $this->modelRepository->findOneByNumber($number);
if (!$model) { if (!$model) {
$model = $modelRepository->findOneByName($part->getName()); $model = $this->modelRepository->findOneByName($part->getName());
} }
} }
if ($model) { return $model;
$part->setModel($model);
$this->em->getRepository(Part::class)->save($part);
}
} }
/** /**
* Get printed part parent number. * Get id of parent for printed parts form part id.
* *
* @param $id * @param $number
* *
* @return string|null LDraw number of printed part parent * @return string|null LDraw number of printed part parent
*/ */

View File

@ -101,16 +101,16 @@ class LDModelParser
$model['license'] = preg_replace('/(^!LICENSE )(.*) : (.*)$/', '$2', $line); $model['license'] = preg_replace('/(^!LICENSE )(.*) : (.*)$/', '$2', $line);
} }
} elseif (strpos($line, '1 ') === 0) { } elseif (strpos($line, '1 ') === 0) {
$reference = $this->getReferencedModelNumber($line); if ($reference = $this->getReferencedModelNumber($line)) {
$id = strtolower($reference['id']);
$color = strtolower($reference['color']);
$id = strtolower($reference['id']); // group subparts by color and id
$color = strtolower($reference['color']); if (isset($model['subparts'][$id][$color])) {
$model['subparts'][$id][$color] = $model['subparts'][$id][$color] + 1;
// group subparts by color and id } else {
if (isset($model['subparts'][$id][$color])) { $model['subparts'][$id][$color] = 1;
$model['subparts'][$id][$color] = $model['subparts'][$id][$color] + 1; }
} else {
$model['subparts'][$id][$color] = 1;
} }
} elseif (!empty($line) && !in_array($line[0], ['2', '3', '4', '5'])) { } elseif (!empty($line) && !in_array($line[0], ['2', '3', '4', '5'])) {
throw new ErrorParsingLineException($model['id'], $line); throw new ErrorParsingLineException($model['id'], $line);
@ -141,13 +141,16 @@ class LDModelParser
* *
* @param $line * @param $line
* *
* @return string|null Filename of referenced part * @return array|null Filename of referenced part
*/ */
public function getReferencedModelNumber($line) public function getReferencedModelNumber($line)
{ {
$line = strtolower(preg_replace('!\s+!', ' ', $line)); $line = strtolower(preg_replace('!\s+!', ' ', $line));
if (preg_match('/^1 ([0-9]{1,3}) (.*) (.*)\.dat$/', $line, $matches)) { // Do not load inverse part as subpart of model
if (preg_match('/^1 ([0-9]{1,3}) (0 0 0 -1 0 0 0 1 0 0 0 1) (.*)\.dat$/', $line, $matches)) {
return null;
} elseif (preg_match('/^1 ([0-9]{1,3}) (.*) (.*)\.dat$/', $line, $matches)) {
$id = str_replace('\\', DIRECTORY_SEPARATOR, $matches[3]); $id = str_replace('\\', DIRECTORY_SEPARATOR, $matches[3]);
$color = $matches[1]; $color = $matches[1];