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

Add LDViewService

Add service for converting LDRaw model files to stl format
This commit is contained in:
David Hübner 2017-03-10 23:18:37 +01:00
parent 0b589ba2ca
commit a2a87fc8dc
3 changed files with 76 additions and 1 deletions

View File

@ -23,3 +23,6 @@ parameters:
# Rebrickable API key obtainable at http://rebrickable.com/api/ # Rebrickable API key obtainable at http://rebrickable.com/api/
rebrickable_apikey: ~ rebrickable_apikey: ~
# Absolute path to OSMesa port of LDView
ldview_bin: /usr/bin/ldview

View File

@ -17,6 +17,10 @@ services:
class: AppBundle\Service\CollectionService class: AppBundle\Service\CollectionService
arguments: ['@doctrine.orm.entity_manager', '@manager.brickset','@manager.rebrickable'] arguments: ['@doctrine.orm.entity_manager', '@manager.brickset','@manager.rebrickable']
service.ldview:
class: AppBundle\Service\LDViewService
arguments: ['%ldview_bin%', '@oneup_flysystem.ldraw_filesystem']
loader.rebrickable: loader.rebrickable:
class: AppBundle\Command\Loader\RebrickableLoader class: AppBundle\Command\Loader\RebrickableLoader
arguments: ['@doctrine.orm.entity_manager', '@manager.rebrickable', '%rebrickable_url%' ] arguments: ['@doctrine.orm.entity_manager', '@manager.rebrickable', '%rebrickable_url%' ]

View File

@ -0,0 +1,68 @@
<?php
namespace AppBundle\Service;
use League\Flysystem\File;
use League\Flysystem\Filesystem;
use Symfony\Component\Asset\Exception\LogicException;
use Symfony\Component\Process\ProcessBuilder;
class LDViewService
{
/**
* @var string LDView binary file path
*/
private $ldview;
/**
* @var \League\Flysystem\Filesystem
*/
private $stlStorage;
/**
* LDViewService constructor.
*
* @param string $ldview Path to LDView OSMesa binary file
* @param Filesystem $stlStorage Filesystem for generated stl model files
*/
public function __construct($ldview, $stlStorage)
{
$this->ldview = $ldview;
$this->stlStorage = $stlStorage;
}
/**
* Convert LDraw model from .dat format to .stl by using LDView
* stores created file to $stlStorage filesystem
*
* @param Filesystem $LDrawDir
*
* @return File
*/
public function datToStl($file, $LDrawDir)
{
$stlFilename = $file['filename'].'.stl';
if (!$this->stlStorage->has($stlFilename)) {
$builder = new ProcessBuilder();
$process = $builder
->setPrefix($this->ldview)
->setArguments([
$LDrawDir->getAdapter()->getPathPrefix().$file['path'],
'-LDrawDir='.$LDrawDir->getAdapter()->getPathPrefix(),
'-ExportFile='.$this->stlStorage->getAdapter()->getPathPrefix().$stlFilename,
])
->getProcess();
$process->run();
if (!$this->stlStorage->has($stlFilename)) {
throw new LogicException($file['basename'].': new file not found'); //TODO
} elseif (!$process->isSuccessful()) {
throw new LogicException($file['basename'].' : '.$process->getOutput()); //TODO
}
}
return $this->stlStorage->get($stlFilename);
}
}