diff --git a/app/Resources/povray_layout/layout.tmpl b/app/Resources/povray_layout/layout.tmpl index f5b8580..eaf8344 100644 --- a/app/Resources/povray_layout/layout.tmpl +++ b/app/Resources/povray_layout/layout.tmpl @@ -15,14 +15,6 @@ global_settings { assumed_gamma 2 } -camera { - location <-1.3, -1.1, 0.7> - angle 40 - sky z - look_at 0 - right -1*x -} - #declare Min = min_extent( main_mesh ); #declare Max = max_extent( main_mesh ); @@ -32,13 +24,25 @@ camera { Max.z-Min.z ); +// Calculate camera distace factor - if object is large on Y axis and Z axis move camera further +#declare cameraDistance = (((Max.x-Min.x)*(1/MaxLength) > 0.3) & ((Max.y-Min.y)*(1/MaxLength) > 0.95) ? 1: 0.9); + +camera +{ + location <-1.5*cameraDistance, -1*cameraDistance, 1.1*cameraDistance> + angle 45 + sky z + look_at 0 + translate <0,0.05,-0.15> + right -1*x +} + object { main_mesh Center_Trans(main_mesh, x+y+z) // rotate <-90,0,0> - rotate <0,90,0> texture @@ -49,4 +53,3 @@ object scale (1/MaxLength) } - diff --git a/src/AppBundle/Service/StlRendererService.php b/src/AppBundle/Service/StlRendererService.php index 89a01a8..db49a54 100644 --- a/src/AppBundle/Service/StlRendererService.php +++ b/src/AppBundle/Service/StlRendererService.php @@ -85,8 +85,7 @@ class StlRendererService * * Generated file is saved to tmp directory specifed in constructor of this class and path to file is returned * - * - * stl2pov (version 2.5.0) - https://rsmith.home.xs4all.nl/software/py-stl-stl2pov.html + * stl2pov (version 3.3.0) - https://rsmith.home.xs4all.nl/software/py-stl-stl2pov.html * * @param string $file The full path to stl file * @return string Return the full path to the generated pov scene @@ -97,42 +96,54 @@ class StlRendererService if(!file_exists($file)) { throw new FileNotFoundException($file); } + if(pathinfo($file, PATHINFO_EXTENSION) != 'stl') { + throw new ConvertingFailedException($file, 'POV', 'Wrong input filetype'); + } + // Save the current working directory and change directory to tmp dir + // stl2pov outputs converted file to current directory and destination can not be changed + $cwd = getcwd(); + chdir($this->tmpDir); + + $filename = pathinfo($file)['filename']; + + // Run stl2pov conversion $processBuilder = new ProcessBuilder(); $process = $processBuilder->setPrefix($this->stl2pov) ->setArguments([ $file ]) ->getProcess(); + $process->mustRun(); - $process->start(); - - $modelInc = null; - - // Read std output from stl2pov command - foreach ($process as $type => $data) { - if (Process::OUT === $type) { - $modelInc .= $data; - } - }; + // Check if file created successfully + if (!file_exists($filename.'.inc')) { + throw new ConvertingFailedException($file, 'POV'); + } + // Load contents of .inc file to variable + $incFile = file_get_contents($filename.'.inc',LOCK_EX); // Replace mesh name in loaded inc file to match declaration in scene layout - $modelInc = preg_replace('/#declare m_(.*) = mesh/','#declare m_MYSOLID = mesh',$modelInc); + $incFile = preg_replace('/# declare m_(.*) = mesh/','#declare m_MYSOLID = mesh',$incFile); - // Get filename of new file - $outputFile = $this->tmpDir.DIRECTORY_SEPARATOR.pathinfo($file)['filename'].'.pov'; + // Remove no longer needed inc file + unlink($filename.'.inc'); + chdir($cwd); + + // Get desired filepath of new pov file + $outputFile = $this->tmpDir.DIRECTORY_SEPARATOR.$filename.'.pov'; // Load contents of pov-ray layout file $layout = file_get_contents($this->layout); - // Try to write contents of converted inc file and concat int with scene definition - if (!file_put_contents($outputFile, $modelInc, LOCK_EX)) { + // Try to write contents of converted inc file and concat int with scene definitions + if (!file_put_contents($outputFile, $incFile, LOCK_EX)) { throw new ConvertingFailedException($file, 'POV'); } if(!file_put_contents($outputFile, $layout, FILE_APPEND | LOCK_EX)) { throw new ConvertingFailedException($file, 'POV'); } - if (!strlen(file_get_contents($outputFile))) { + if (!file_exists($outputFile)) { throw new ConvertingFailedException($file, 'POV'); } @@ -183,8 +194,7 @@ class StlRendererService "+A0.5", "-D", ])->getProcess(); - - $process->run(); + $process->mustRun(); if(!file_exists($outputFile)) { throw new RenderFailedException("{$to}{$filename}.png");