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

Add ADMesh stlfixer

This commit is contained in:
David Hübner 2017-05-05 21:00:11 +02:00
parent e684d30c45
commit 70341dd390
8 changed files with 128 additions and 28 deletions

View File

@ -4,7 +4,7 @@
parameters: parameters:
database_host: 127.0.0.1 database_host: 127.0.0.1
database_port: ~ database_port: ~
database_name: symfony database_name: printabrick
database_user: root database_user: root
database_password: ~ database_password: ~
# You should uncomment this if you want use pdo_sqlite # You should uncomment this if you want use pdo_sqlite
@ -24,5 +24,9 @@ parameters:
# Path to POV-Ray binary - http://www.povray.org/ # Path to POV-Ray binary - http://www.povray.org/
povray_bin: /usr/bin/povray povray_bin: /usr/bin/povray
# Path to stl2pov (version 3.3.0) - https://github.com/rsmith-nl/stltools/releases/tag/3.3 # Path to stl2pov (version 3.3.0) - https://github.com/rsmith-nl/stltools/releases/tag/3.3
stl2pov_bin: /usr/bin/stl2pov stl2pov_bin: /usr/bin/stl2pov
# Path to admesh
admesh_bin: /usr/bin/admesh

View File

@ -22,5 +22,5 @@ services:
service.loader.image: service.loader.image:
class: AppBundle\Service\Loader\ImageLoader class: AppBundle\Service\Loader\ImageLoader
arguments: ['@oneup_flysystem.media_filesystem', '%app.rebrickable_downloads_url%','@service.renderer.stl'] arguments: ['@oneup_flysystem.media_filesystem', '%app.rebrickable_downloads_url%','@service.stl.renderer']
parent: service.loader.base parent: service.loader.base

View File

@ -3,13 +3,17 @@ services:
class: Doctrine\Common\Cache\PhpFileCache class: Doctrine\Common\Cache\PhpFileCache
arguments: ["%kernel.cache_dir%/brickset", ".cache.php"] arguments: ["%kernel.cache_dir%/brickset", ".cache.php"]
service.renderer.stl: service.stl.renderer:
class: AppBundle\Service\StlRendererService class: AppBundle\Service\Stl\StlRendererService
arguments: ['%kernel.root_dir%/Resources/povray_layout/layout.tmpl', '%povray_bin%','%stl2pov_bin%'] arguments: ['%kernel.root_dir%/Resources/povray_layout/layout.tmpl', '%povray_bin%','%stl2pov_bin%']
service.ldview: service.stl.converter:
class: AppBundle\Service\LDViewService class: AppBundle\Service\Stl\StlConverterService
arguments: ['%ldview_bin%', '@oneup_flysystem.media_filesystem'] arguments: ['%ldview_bin%', '@oneup_flysystem.media_filesystem', '@service.stl.fixer']
service.stl.fixer:
class: AppBundle\Service\Stl\StlFixerService
arguments: ['%admesh_bin%']
service.zip: service.zip:
class: AppBundle\Service\ZipService class: AppBundle\Service\ZipService

View File

@ -3,7 +3,7 @@
namespace AppBundle\Service\Loader; namespace AppBundle\Service\Loader;
use AppBundle\Entity\LDraw\Model; use AppBundle\Entity\LDraw\Model;
use AppBundle\Service\StlRendererService; use AppBundle\Service\Stl\StlRendererService;
use League\Flysystem\Filesystem; use League\Flysystem\Filesystem;
class ImageLoader extends BaseLoader class ImageLoader extends BaseLoader

View File

@ -11,7 +11,7 @@ use AppBundle\Entity\LDraw\Subpart;
use AppBundle\Exception\ConvertingFailedException; use AppBundle\Exception\ConvertingFailedException;
use AppBundle\Exception\FileException; use AppBundle\Exception\FileException;
use AppBundle\Exception\ParseErrorException; use AppBundle\Exception\ParseErrorException;
use AppBundle\Service\LDViewService; use AppBundle\Service\Stl\StlConverterService;
use AppBundle\Util\LDModelParser; use AppBundle\Util\LDModelParser;
use AppBundle\Util\RelationMapper; use AppBundle\Util\RelationMapper;
use League\Flysystem\Adapter\Local; use League\Flysystem\Adapter\Local;
@ -28,9 +28,9 @@ class ModelLoader extends BaseLoader
private $ldrawLibraryContext; private $ldrawLibraryContext;
/** /**
* @var LDViewService * @var StlConverterService
*/ */
private $LDViewService; private $stlConverter;
/** @var LDModelParser */ /** @var LDModelParser */
private $ldModelParser; private $ldModelParser;
@ -41,18 +41,22 @@ class ModelLoader extends BaseLoader
/** @var Finder */ /** @var Finder */
private $finder; private $finder;
/** @var string */
private $LDLibraryUrl;
private $rewite = false; private $rewite = false;
/** /**
* LDrawLoaderService constructor. * LDrawLoaderService constructor.
* *
* @param LDViewService $LDViewService * @param StlConverterService $stlConverter
* @param RelationMapper $relationMapper * @param RelationMapper $relationMapper
*/ */
public function __construct($LDViewService, $relationMapper) public function __construct($stlConverter, $relationMapper, $LDLibraryUrl)
{ {
$this->LDViewService = $LDViewService; $this->stlConverter = $stlConverter;
$this->relationMapper = $relationMapper; $this->relationMapper = $relationMapper;
$this->LDLibraryUrl = $LDLibraryUrl;
$this->ldModelParser = new LDModelParser(); $this->ldModelParser = new LDModelParser();
$this->finder = new Finder(); $this->finder = new Finder();
} }
@ -73,7 +77,7 @@ class ModelLoader extends BaseLoader
try { try {
$adapter = new Local($ldrawLibrary); $adapter = new Local($ldrawLibrary);
$this->ldrawLibraryContext = new Filesystem($adapter); $this->ldrawLibraryContext = new Filesystem($adapter);
$this->LDViewService->setLDrawLibraryContext($this->ldrawLibraryContext); $this->stlConverter->setLDrawLibraryContext($this->ldrawLibraryContext);
} catch (Exception $exception) { } catch (Exception $exception) {
$this->logger->error($exception->getMessage()); $this->logger->error($exception->getMessage());
} }
@ -235,7 +239,7 @@ class ModelLoader extends BaseLoader
try { try {
// update model only if newer version // update model only if newer version
if (!$model->getModified() || ($model->getModified() < $modelArray['modified'])) { if (!$model->getModified() || ($model->getModified() < $modelArray['modified'])) {
$stl = $this->LDViewService->datToStl($file, $this->rewite)->getPath(); $stl = $this->stlConverter->datToStl($file, $this->rewite)->getPath();
$model->setPath($stl); $model->setPath($stl);
$model $model
@ -349,6 +353,10 @@ class ModelLoader extends BaseLoader
if (in_array($modelArray['type'], ['48_Primitive', '8_Primitive', 'Primitive', 'Subpart'])) { if (in_array($modelArray['type'], ['48_Primitive', '8_Primitive', 'Primitive', 'Subpart'])) {
return false; return false;
} }
// Do not include Pov-RAY file
elseif ($modelArray['category'] == 'Pov-RAY') {
return false;
}
// Do not include sticker models // Do not include sticker models
elseif ($modelArray['type'] == 'Sticker') { elseif ($modelArray['type'] == 'Sticker') {
$this->logger->info('Model skipped.', ['number' => $modelArray['id'], 'type' => $modelArray['type']]); $this->logger->info('Model skipped.', ['number' => $modelArray['id'], 'type' => $modelArray['type']]);

View File

@ -1,21 +1,24 @@
<?php <?php
namespace AppBundle\Service; namespace AppBundle\Service\Stl;
use AppBundle\Exception\ConvertingFailedException; use AppBundle\Exception\ConvertingFailedException;
use AppBundle\Exception\LDView\LDLibraryMissingException;
use League\Flysystem\File; use League\Flysystem\File;
use League\Flysystem\Filesystem; use League\Flysystem\Filesystem;
use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ProcessBuilder; use Symfony\Component\Process\ProcessBuilder;
//TODO enable file overwrite class StlConverterService
class LDViewService
{ {
/** /**
* @var string LDView binary file path * @var string LDView binary file path
*/ */
private $ldview; private $ldview;
/** @var StlFixerService */
private $stlFixer;
/** /**
* @var Filesystem * @var Filesystem
*/ */
@ -27,15 +30,21 @@ class LDViewService
private $ldrawLibraryContext; private $ldrawLibraryContext;
/** /**
* LDViewService constructor. * @var bool
*/
private $rewrite = false;
/**
* StlConverterService constructor.
* *
* @param string $ldview Path to LDView OSMesa binary file * @param string $ldview Path to LDView OSMesa binary file
* @param Filesystem $mediaFilesystem Filesystem for generated web assets * @param Filesystem $mediaFilesystem Filesystem for generated web assets
*/ */
public function __construct($ldview, $mediaFilesystem) public function __construct($ldview, $mediaFilesystem, $stlFixer)
{ {
$this->ldview = $ldview; $this->ldview = $ldview;
$this->mediaFilesystem = $mediaFilesystem; $this->mediaFilesystem = $mediaFilesystem;
$this->stlFixer = $stlFixer;
} }
/** /**
@ -56,15 +65,19 @@ class LDViewService
* *
* @return File * @return File
*/ */
public function datToStl($file, $rewrite = false) public function datToStl($file)
{ {
if (!$this->ldrawLibraryContext) {
throw new LDLibraryMissingException();
}
if (!$this->mediaFilesystem->has('models')) { if (!$this->mediaFilesystem->has('models')) {
$this->mediaFilesystem->createDir('models'); $this->mediaFilesystem->createDir('models');
} }
$newFile = 'models'.DIRECTORY_SEPARATOR.basename($file, '.dat').'.stl'; $newFile = 'models'.DIRECTORY_SEPARATOR.basename($file, '.dat').'.stl';
if (!$this->mediaFilesystem->has($newFile) || $rewrite) { if (!$this->mediaFilesystem->has($newFile) || $this->rewrite) {
$this->runLDView([ $this->runLDView([
$file, $file,
'-LDrawDir='.$this->ldrawLibraryContext->getAdapter()->getPathPrefix(), '-LDrawDir='.$this->ldrawLibraryContext->getAdapter()->getPathPrefix(),
@ -75,6 +88,8 @@ class LDViewService
// Check if file created successfully // Check if file created successfully
if ($this->mediaFilesystem->has($newFile)) { if ($this->mediaFilesystem->has($newFile)) {
$this->stlFixer->fix($this->mediaFilesystem->getAdapter()->getPathPrefix().$newFile);
return $this->mediaFilesystem->get($newFile); return $this->mediaFilesystem->get($newFile);
} }
} else { } else {
@ -94,8 +109,12 @@ class LDViewService
* *
* @return File * @return File
*/ */
public function datToPng($file, $rewrite = false) public function datToPng($file)
{ {
if (!$this->ldrawLibraryContext) {
throw new LDLibraryMissingException();
}
if (!$this->mediaFilesystem->has('images')) { if (!$this->mediaFilesystem->has('images')) {
$this->mediaFilesystem->createDir('images'); $this->mediaFilesystem->createDir('images');
} }
@ -149,7 +168,7 @@ class LDViewService
$process->run(); $process->run();
if (!$process->isSuccessful()) { if (!$process->isSuccessful()) {
throw new ProcessFailedException($process); //TODO throw new ProcessFailedException($process);
} }
} }
} }

View File

@ -0,0 +1,64 @@
<?php
namespace AppBundle\Service\Stl;
use AppBundle\Exception\FileNotFoundException;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ProcessBuilder;
class StlFixerService
{
private $ADMesh;
/**
* StlFixerService constructor.
*
* @param $ADMesh
*/
public function __construct($ADMesh)
{
$this->ADMesh = $ADMesh;
}
/**
* Rotate, scale stl file and save in binary format.
*
* @param $file
*
* @throws FileNotFoundException
*/
public function fix($file)
{
if (file_exists($file)) {
$this->runADMesh([
$file,
'--x-rotate=-90',
'--scale=10',
'--no-check',
"--write-binary-stl={$file}",
]);
} else {
throw new FileNotFoundException($file);
}
}
/**
* Call ADMesh process with $arguments.
*
* @param array $arguments
*/
private function runADMesh(array $arguments)
{
$builder = new ProcessBuilder();
$process = $builder
->setPrefix($this->ADMesh)
->setArguments($arguments)
->getProcess();
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
}
}

View File

@ -1,6 +1,6 @@
<?php <?php
namespace AppBundle\Service; namespace AppBundle\Service\Stl;
use AppBundle\Exception\ConvertingFailedException; use AppBundle\Exception\ConvertingFailedException;
use AppBundle\Exception\FileNotFoundException; use AppBundle\Exception\FileNotFoundException;
@ -8,6 +8,7 @@ use AppBundle\Exception\RenderFailedException;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder; use Symfony\Component\Process\ProcessBuilder;
// TODO create images/{color} directory
class StlRendererService class StlRendererService
{ {
/** /**