diff --git a/app/config/services.yml b/app/config/services.yml index cead72e..db4c4dc 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -20,6 +20,9 @@ services: loader.rebrickable: class: AppBundle\Service\RebrickableLoader 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: class: AppBundle\Form\FilterSetType diff --git a/bin/ldview b/bin/ldview new file mode 100644 index 0000000..6f2efe9 Binary files /dev/null and b/bin/ldview differ diff --git a/src/AppBundle/Command/LoadLDrawCommand.php b/src/AppBundle/Command/LoadLDrawCommand.php new file mode 100644 index 0000000..13642f6 --- /dev/null +++ b/src/AppBundle/Command/LoadLDrawCommand.php @@ -0,0 +1,27 @@ +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')); + } +} diff --git a/src/AppBundle/Service/LDrawLoader.php b/src/AppBundle/Service/LDrawLoader.php new file mode 100644 index 0000000..9f5a45c --- /dev/null +++ b/src/AppBundle/Service/LDrawLoader.php @@ -0,0 +1,115 @@ +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); + } +}