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

Add LDraw loader

This commit is contained in:
David Hübner 2017-01-14 16:12:16 +01:00
parent 1a22b6f431
commit a694788f85
4 changed files with 145 additions and 0 deletions

View File

@ -20,6 +20,9 @@ services:
loader.rebrickable: loader.rebrickable:
class: AppBundle\Service\RebrickableLoader class: AppBundle\Service\RebrickableLoader
arguments: ['@doctrine.orm.entity_manager', '@manager.rebrickable'] arguments: ['@doctrine.orm.entity_manager', '@manager.rebrickable']
loader.ldraw:
class: AppBundle\Service\LDrawLoader
arguments: ['@doctrine.orm.entity_manager', '%kernel.root_dir%/../bin/ldview', '%kernel.root_dir%/../var/data/ldraw']
app.form.filter_set: app.form.filter_set:
class: AppBundle\Form\FilterSetType class: AppBundle\Form\FilterSetType

BIN
bin/ldview Normal file

Binary file not shown.

View File

@ -0,0 +1,27 @@
<?php
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
class LoadLDrawCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('app:load:ldraw')
->setDescription('Loads LDraw library parts')
->setHelp("This command allows you to..")
->addArgument('ldraw', InputArgument::REQUIRED, 'Path to LDraw library folder')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getContainer()->get('loader.ldraw')->loadModels($input->getArgument('ldraw'));
}
}

View File

@ -0,0 +1,115 @@
<?php
namespace AppBundle\Service;
use AppBundle\Entity\Model;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Asset\Exception\LogicException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ProcessBuilder;
class LDrawLoader
{
/**
* @var EntityManager
*/
private $em;
/**
* @var string LDView binary file path
*/
private $ldview;
/**
* @var string LDraw library root path
*/
private $ldraw;
/**
* @var string
*/
private $dataPath;
public function __construct($em, $ldview, $dataPath)
{
$this->em = $em;
$this->ldview = $ldview;
$this->dataPath = $dataPath;
}
// LDraw
public function loadModels($LDrawLibrary)
{
$finder = new Finder();
$files = $finder->files()->name('*.dat')->depth('== 0')->in(getcwd().DIRECTORY_SEPARATOR.$LDrawLibrary.'/parts');
$this->ldraw = getcwd().DIRECTORY_SEPARATOR.$LDrawLibrary;
foreach ($files as $file) {
$this->loadModelHeader($file);
}
}
private function loadModelHeader(SplFileInfo $fileInfo)
{
$handle = fopen($fileInfo->getRealPath(), 'r');
if ($handle) {
$firstLine = fgets($handle);
$description = trim(substr($firstLine, 2));
$model = new Model();
$model->setFile($fileInfo->getFilename());
$p['category'] = explode(' ', trim($description))[0];
//TODO handle ~Moved to
while (!feof($handle)) {
$line = trim(fgets($handle));
if ($line && ($line[0] == '1')) {
break;
} elseif ($line && ($line[0] == '0' && strpos($line, '!CATEGORY '))) {
$p['category'] = trim(explode('!CATEGORY ', $line)[1]);
} elseif ($line && ($line[0] == '0' && strpos($line, '!KEYWORDS '))) {
$keywords = explode(',', explode('!KEYWORDS ', $line)[1]);
foreach ($keywords as $k) {
$p['keywords'][] = trim($k);
}
} elseif ($line && ($line[0] == '0' && strpos($line, 'Name: '))) {
$model->setNumber(trim(explode('.dat', explode('Name: ', $line)[1])[0]));
} elseif ($line && ($line[0] == '0' && strpos($line, 'Author: '))) {
$model->setAuthor(explode('Author: ', $line)[1]);
}
}
$builder = new ProcessBuilder();
$process = $builder
->setPrefix($this->ldview)
->setArguments([
$fileInfo->getRealPath(),
'-ExportFiles=1',
'-LDrawDir='.$this->ldraw,
'-ExportSuffix=.stl',
'-ExportsDir='.$this->dataPath,
])
->getProcess();
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
} else {
var_dump($process->getOutput());
}
// $this->em->persist($model);
// $this->em->flush();
} else {
throw new LogicException('loadHeader error'); //TODO
}
fclose($handle);
}
}