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

Move ldraw dir dependency from config to command

This commit is contained in:
David Hübner 2017-04-11 17:30:20 +02:00
parent 59eff0e0bd
commit 794202d05d
7 changed files with 111 additions and 113 deletions

View File

@ -142,11 +142,6 @@ oneup_flysystem:
media: media:
local: local:
directory: "%kernel.root_dir%/../var/media/" directory: "%kernel.root_dir%/../var/media/"
ldraw_library:
local:
directory: "%ldraw_library%"
filesystems: filesystems:
media: media:
adapter: media adapter: media
ldraw_library:
adapter: ldraw_library

View File

@ -26,6 +26,3 @@ parameters:
# Absolute path to OSMesa port of LDView # Absolute path to OSMesa port of LDView
ldview_bin: /usr/bin/ldview ldview_bin: /usr/bin/ldview
# Path to LDraw library
ldraw_library: ~

View File

@ -14,9 +14,9 @@ services:
arguments: ['%rebrickable_url%'] arguments: ['%rebrickable_url%']
parent: service.loader parent: service.loader
service.loader.ldraw: service.loader.model:
class: AppBundle\Service\Loader\LDrawLoaderService class: AppBundle\Service\Loader\ModelLoaderService
arguments: ['@oneup_flysystem.ldraw_library_filesystem','@service.ldview', '@manager.ldraw', '@app.relation.mapper'] arguments: ['@service.ldview', '@manager.ldraw', '@app.relation.mapper']
parent: service.loader parent: service.loader
service.loader.relation: service.loader.relation:

View File

@ -3,6 +3,7 @@
namespace AppBundle\Command; namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
@ -11,6 +12,8 @@ use Symfony\Component\Console\Output\OutputInterface;
class LoadModelsCommand extends ContainerAwareCommand class LoadModelsCommand extends ContainerAwareCommand
{ {
use LockableTrait;
protected function configure() protected function configure()
{ {
$this $this
@ -18,35 +21,45 @@ class LoadModelsCommand extends ContainerAwareCommand
->setDescription('Loads LDraw library models into database') ->setDescription('Loads LDraw library models into database')
->setHelp('This command allows you to load LDraw library models into while converting .dat files to .stl') ->setHelp('This command allows you to load LDraw library models into while converting .dat files to .stl')
->setDefinition( ->setDefinition(
new InputDefinition(array( new InputDefinition([
new InputOption('images', 'i'), new InputArgument('ldraw', InputArgument::REQUIRED, 'Path to LDraw library directory'),
new InputOption('ldraw', 'l', InputOption::VALUE_REQUIRED), new InputOption('images', 'i',InputOption::VALUE_NONE, 'Do you want to generate images of models?'),
new InputOption('file', 'f', InputOption::VALUE_REQUIRED), new InputOption('all','a',InputOption::VALUE_NONE, 'Do you want to load whole LDraw libary?'),
)) new InputOption('file','f',InputOption::VALUE_REQUIRED, 'Path to DAT file that should be loaded into database')
])
); );
} }
//TODO log errors
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$ldrawLoader = $this->getContainer()->get('service.loader.ldraw'); if (!$this->lock()) {
$ldrawLoader->setOutput($output); $output->writeln('The command is already running in another process.');
return 0;
}
$ldrawLoader = $this->getContainer()->get('service.loader.model');
$ldrawLoader->setOutput($output);
$ldrawLoader->setLDrawLibraryContext(realpath($input->getArgument('ldraw')));
//TODO log errors
try { try {
// TODO handle relative path to dir
if (($ldrawPath = $input->getOption('file')) != null) { if (($ldrawPath = $input->getOption('file')) != null) {
$ldrawLoader->loadFileContext($ldrawPath); $ldrawLoader->loadFileContext(dirname(realpath($ldrawPath)));
$model = $ldrawLoader->loadModel($ldrawPath); $model = $ldrawLoader->loadModel($ldrawPath);
if($model !== null) { if($model !== null) {
$this->getContainer()->get('manager.ldraw.model')->getRepository()->save($model); $this->getContainer()->get('manager.ldraw.model')->getRepository()->save($model);
} }
} else { }
$ldrawLoader->loadAllModels(); if ($input->getOption('all')) {
$ldrawLoader->loadAllModels();
} }
} catch (\Exception $e) { } catch (\Exception $e) {
printf($e->getMessage()); printf($e->getMessage());
} }
$this->release();
} }
} }

View File

@ -2,15 +2,26 @@
namespace AppBundle\Manager; namespace AppBundle\Manager;
use AppBundle\Repository\BaseRepository;
use Doctrine\ORM\EntityManager;
class BaseManager class BaseManager
{ {
/** @var EntityManager $em */
protected $em;
protected $repository; protected $repository;
/** /**
* @return mixed * @return BaseRepository
*/ */
public function getRepository() public function getRepository()
{ {
return $this->repository; return $this->repository;
} }
public function setEntityManager(EntityManager $entityManager)
{
$this->em = $entityManager;
}
} }

View File

@ -1,18 +1,12 @@
<?php <?php
/**
* Created by PhpStorm.
* User: hubnedav
* Date: 4/1/17
* Time: 2:40 AM.
*/
namespace AppBundle\Manager; namespace AppBundle\Manager;
use AppBundle\Entity\Rebrickable\Set; use AppBundle\Entity\Rebrickable\Theme;
class RebrickableManager extends BaseManager class RebrickableManager extends BaseManager
{ {
public function findInventoryParts(Set $set) public function findAllThemes() {
{ return $this->em->getRepository(Theme::class)->findAll();
} }
} }

View File

@ -5,22 +5,23 @@ namespace AppBundle\Service\Loader;
use AppBundle\Entity\LDraw\Alias; use AppBundle\Entity\LDraw\Alias;
use AppBundle\Entity\LDraw\Model; use AppBundle\Entity\LDraw\Model;
use AppBundle\Entity\LDraw\Type; use AppBundle\Entity\LDraw\Type;
use AppBundle\Exception\ConvertingFailedException;
use AppBundle\Manager\LDrawManager; use AppBundle\Manager\LDrawManager;
use AppBundle\Service\LDViewService; use AppBundle\Service\LDViewService;
use AppBundle\Utils\DatParser; use AppBundle\Utils\DatParser;
use AppBundle\Utils\RelationMapper; use AppBundle\Utils\RelationMapper;
use League\Flysystem\Adapter\Local; use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem; use League\Flysystem\Filesystem;
use Symfony\Component\Finder\Finder;
//use Symfony\Component\Asset\Exception\LogicException; use Symfony\Component\Finder\SplFileInfo;
//TODO refactor //TODO refactor
class LDrawLoaderService extends BaseLoaderService class ModelLoaderService extends BaseLoaderService
{ {
/** /**
* @var Filesystem * @var Filesystem
*/ */
private $ldrawLibrary; private $ldrawLibraryContext;
/** /**
* @var Filesystem * @var Filesystem
@ -43,57 +44,54 @@ class LDrawLoaderService extends BaseLoaderService
/** @var RelationMapper */ /** @var RelationMapper */
protected $relationMapper; protected $relationMapper;
/** @var Finder */
private $finder;
/** /**
* LDrawLoaderService constructor. * LDrawLoaderService constructor.
* @param Filesystem $ldrawLibraryFilesystem
* @param LDViewService $LDViewService * @param LDViewService $LDViewService
* @param LDrawManager $ldrawService * @param LDrawManager $ldrawService
* @param RelationMapper $relationMapper * @param RelationMapper $relationMapper
*/ */
public function __construct($ldrawLibraryFilesystem, $LDViewService, $ldrawService, $relationMapper) public function __construct($LDViewService, $ldrawService, $relationMapper)
{ {
$this->LDViewService = $LDViewService; $this->LDViewService = $LDViewService;
$this->ldrawService = $ldrawService; $this->ldrawService = $ldrawService;
$this->datParser = new DatParser();
$this->ldrawLibrary = $ldrawLibraryFilesystem;
$this->relationMapper = $relationMapper; $this->relationMapper = $relationMapper;
$this->datParser = new DatParser();
$this->finder = new Finder();
} }
private function dumpModel($model, $level = 1) { public function setLDrawLibraryContext($ldrawLibrary)
$level = $level + 1; {
dump(str_repeat("-", 2*$level).'> '.$model->getNumber()); $adapter = new Local($ldrawLibrary);
foreach ($model->getSubparts() as $subpart) { $this->ldrawLibraryContext = new Filesystem($adapter);
$this->dumpModel($subpart->getSubpart(), $level);
} $this->LDViewService->setLdrawFilesystem($this->ldrawLibraryContext);
}
public function loadFileContext($file) {
$adapter = new Local($file);
$this->fileContext = new Filesystem($adapter);
} }
public function loadAllModels() public function loadAllModels()
{ {
$files = $this->ldrawLibrary->get('parts')->getContents(); $files = $this->finder->in(['/home/hubnedav/Documents/ldraw'])->path('parts/')->name('*.dat')->depth(1)->files();
$modelManager = $this->ldrawService->getModelManager(); $modelManager = $this->ldrawService->getModelManager();
$this->initProgressBar(count($files)); $this->initProgressBar($files->count());
/** @var SplFileInfo $file */
foreach ($files as $file) { foreach ($files as $file) {
if ($file['type'] == 'file' && $file['extension'] == 'dat') { $this->newModels = [];
$this->newModels = []; $model = $this->loadModel($file->getRealPath());
$model = $this->loadModel($this->ldrawLibrary->getAdapter()->getPathPrefix().$file['path']); if($model !== null) {
$modelManager->getRepository()->save($model);
if($model !== null) {
// dump($model->getNumber());
try {
$modelManager->getRepository()->save($model);
} catch (\Exception $exception) {
dump($exception);
// dump($model);
$this->dumpModel($model);
exit();
}
}
} }
$this->progressBar->advance(); $this->progressBar->advance();
@ -116,33 +114,22 @@ class LDrawLoaderService extends BaseLoaderService
$subpartManager = $this->ldrawService->getSubpartManager(); $subpartManager = $this->ldrawService->getSubpartManager();
if(($model = $modelManager->findByNumber(basename($file,'.dat'))) || ($model = isset($this->newModels[basename($file,'.dat')]) ? $this->newModels[basename($file,'.dat')] : null)) { if(($model = $modelManager->findByNumber(basename($file,'.dat'))) || ($model = isset($this->newModels[basename($file,'.dat')]) ? $this->newModels[basename($file,'.dat')] : null)) {
$this->LDViewService->datToPng($file);
return $model; return $model;
} }
try { try {
$header = $this->datParser->parse($file); $modelArray = $this->datParser->parse($file);
} catch (\Exception $e) { } catch (\Exception $e) {
dump($e); dump($e->getMessage());
return null; return null;
} }
if ($this->isModelIncluded($header)) { if ($this->isModelIncluded($modelArray)) {
if($this->relationMapper->find($header['id'], 'alias_model') !== $header['id']) { if ($parentModelFile = $this->getParentModelFile($modelArray)) {
$parentFile = $this->findModelFile($this->relationMapper->find($header['id'], 'alias_model'));
} else {
$parentFile = isset($header['parent']) && strpos($header['parent'], 's\\') === false ? $this->findModelFile($header['parent']) : null;
}
if ($parentFile) {
try { try {
$parentModel = $this->loadModel($parentFile); if(($parentModel = $this->loadModel($parentModelFile))!= null) {
$alias = $this->ldrawService->getAliasManager()->create($modelArray['id'], $parentModel);
if($parentModel) {
$alias = new Alias();
$alias->setNumber($header['id']);
$alias->setModel($parentModel);
$parentModel->addAlias($alias); $parentModel->addAlias($alias);
$this->newModels[$parentModel->getNumber()] = $parentModel; $this->newModels[$parentModel->getNumber()] = $parentModel;
@ -154,26 +141,23 @@ class LDrawLoaderService extends BaseLoaderService
return null; return null;
} }
} else { } else {
$model = $modelManager->create($header['id']); $model = $modelManager->create($modelArray['id']);
$model->setName($header['name']);
$model->setCategory($this->ldrawService->getCategoryManager()->create($header['category']));
if (isset($header['keywords'])) { if (isset($modelArray['keywords'])) {
foreach ($header['keywords'] as $keyword) { foreach ($modelArray['keywords'] as $keyword) {
$keyword = stripslashes(strtolower(trim($keyword))); $keyword = stripslashes(strtolower(trim($keyword)));
$model->addKeyword($this->ldrawService->getKeywordManager()->create($keyword)); $model->addKeyword($this->ldrawService->getKeywordManager()->create($keyword));
} }
} }
if (isset($header['subparts'])) { if (isset($modelArray['subparts'])) {
foreach ($header['subparts'] as $subpartId => $count) { foreach ($modelArray['subparts'] as $subpartId => $count) {
if(strpos($subpartId, 's\\') === false) { if(strpos($subpartId, 's\\') === false) {
if(($subpartFile = $this->findModelFile($subpartId)) != null) { if(($subpartFile = $this->findModelFile($subpartId)) != null) {
try { try {
$subModel = $this->loadModel($subpartFile); if ($subModel = $this->loadModel($subpartFile)) {
if ($subModel) {
$subpart = $subpartManager->create($model,$subModel,$count); $subpart = $subpartManager->create($model,$subModel,$count);
$model->addSubpart($subpart); $model->addSubpart($subpart);
} }
} catch (\Exception $e) { } catch (\Exception $e) {
@ -185,9 +169,14 @@ class LDrawLoaderService extends BaseLoaderService
} }
} }
$model->setAuthor($header['author']); $model
$model->setModified($header['modified']); ->setName($modelArray['name'])
$model->setPath($this->loadStlModel($file)); ->setCategory($this->ldrawService->getCategoryManager()->create($modelArray['category']))
->setAuthor($modelArray['author'])
->setModified($modelArray['modified'])
->setPath($this->loadStlModel($file));
$this->LDViewService->datToPng($file);
$this->newModels[$model->getNumber()] = $model; $this->newModels[$model->getNumber()] = $model;
} }
@ -196,6 +185,14 @@ class LDrawLoaderService extends BaseLoaderService
return $model; return $model;
} }
private function getParentModelFile($modelArray) {
if($this->relationMapper->find($modelArray['id'], 'alias_model') !== $modelArray['id']) {
return $this->findModelFile($this->relationMapper->find($modelArray['id'], 'alias_model'));
} else {
return strpos($modelArray['parent'], 's\\') === false ? $this->findModelFile($modelArray['parent']) : null;
}
}
/** /**
* Find model file on ldraw filesystem. * Find model file on ldraw filesystem.
* *
@ -209,33 +206,24 @@ class LDrawLoaderService extends BaseLoaderService
if($this->fileContext && $this->fileContext->has($filename)) { if($this->fileContext && $this->fileContext->has($filename)) {
return $this->fileContext->getAdapter()->getPathPrefix().$filename; return $this->fileContext->getAdapter()->getPathPrefix().$filename;
} else if ($this->ldrawLibrary->has('parts/'.$filename)) { } else if ($this->ldrawLibraryContext->has('parts/'.$filename)) {
return $this->ldrawLibrary->getAdapter()->getPathPrefix().'parts/'.$filename; return $this->ldrawLibraryContext->getAdapter()->getPathPrefix().'parts/'.$filename;
} }
return null; return null;
} }
public function loadFileContext($file) {
$adapter = new Local(dirname(realpath($file)));
$this->fileContext = new Filesystem($adapter);
}
/** /**
* Determine if model file should be loaded into database. * Determine if model file should be loaded into database.
* *
* @param $header * @param $modelArray
* *
* @return bool * @return bool
*/ */
private function isModelIncluded($header) private function isModelIncluded($modelArray)
{ {
// Do not include sticker parts and incomplete parts // Do not include sticker parts and incomplete parts
if ( if ( $modelArray['type'] != 'Subpart' && $modelArray['type'] != 'Sticker' ) {
$header['type'] != 'Subpart'
&& $header['type'] != 'Sticker'
// && !(($header['type'] == 'Printed') && $this->findModelFile($header['parent']))
) {
return true; return true;
} }
@ -255,7 +243,7 @@ class LDrawLoaderService extends BaseLoaderService
{ {
try { try {
return $this->LDViewService->datToStl($file)->getPath(); return $this->LDViewService->datToStl($file)->getPath();
} catch (\Exception $e) { } catch (ConvertingFailedException $e) {
throw $e; //TODO throw $e; //TODO
} }
} }