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:
local:
directory: "%kernel.root_dir%/../var/media/"
ldraw_library:
local:
directory: "%ldraw_library%"
filesystems:
media:
adapter: media
ldraw_library:
adapter: ldraw_library

View File

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

View File

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

View File

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

View File

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

View File

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