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

Do not load ldraw shortcut parts of stickers

This commit is contained in:
David Hübner 2017-03-16 02:49:15 +01:00
parent 874d72c7bc
commit 97bae052ff

View File

@ -124,7 +124,7 @@ class LDrawLoader extends Loader
if ($file['type'] == 'file' && $file['extension'] == 'dat') { if ($file['type'] == 'file' && $file['extension'] == 'dat') {
$header = $this->getPartHeader($file); $header = $this->getPartHeader($file);
if ($this->fileFilter($header)) { if ($this->isPartIncluded($header)) {
if (null == ($part = $this->em->getRepository(Part::class)->find($header['id']))) { if (null == ($part = $this->em->getRepository(Part::class)->find($header['id']))) {
$part = new Part(); $part = new Part();
$part->setId($header['id']); $part->setId($header['id']);
@ -174,29 +174,39 @@ class LDrawLoader extends Loader
$this->em->persist($alias); $this->em->persist($alias);
} }
if (isset($header['subparts'])) { if (isset($header['subparts']) && $header['type'] != 'Print') {
$relationType = strpos($header['name'], '~Moved to ') === 0 ? 'Alias' : 'Subpart';
if ($header['type'] == 'Alias') { if ($header['type'] == 'Alias') {
if (count($header['subparts']) == 1) { if (count($header['subparts']) == 1) {
$relationType = 'Alias'; $relationType = 'Alias';
} else {
$relationType = 'Subpart';
} }
} else {
$relationType = 'Subpart';
} }
foreach ($header['subparts'] as $subId) { foreach ($header['subparts'] as $referenceId) {
if ($subId != $this->getPrinetedParentId($header['id'])) { if ($referenceId != $this->getPrinetedParentId($header['id'])) {
if ($this->ldraw->has('parts/'.$subId.'.dat') && $this->fileFilter($this->getPartHeader($this->ldraw->get('parts/'.$subId.'.dat')->getMetadata()))) { if ($this->ldraw->has('parts/'.$referenceId.'.dat') && $this->isPartIncluded($this->getPartHeader($this->ldraw->get('parts/'.$referenceId.'.dat')->getMetadata()))) {
if (($subPart = $this->em->getRepository(Part::class)->find($subId)) == null) { if (($referencedPart = $this->em->getRepository(Part::class)->find($referenceId)) == null) {
$subPart = new Part(); $referencedPart = new Part();
$subPart->setId($subId); $referencedPart->setId($referenceId);
$this->em->persist($subPart); $this->em->persist($referencedPart);
} }
if (($alias = $this->em->getRepository(Part_Relation::class)->find(['parent' => $part, 'child' => $subPart, 'type' => $relationType])) == null) { if($relationType == 'Alias') {
$parent = $referencedPart;
$child = $part;
} else {
$parent = $part;
$child = $referencedPart;
}
if (($alias = $this->em->getRepository(Part_Relation::class)->find(['parent' => $parent, 'child' => $child, 'type' => $relationType])) == null) {
$alias = new Part_Relation(); $alias = new Part_Relation();
$alias->setParent($part); $alias->setParent($parent);
$alias->setChild($subPart); $alias->setChild($child);
$alias->setCount(0); $alias->setCount(0);
$alias->setType($relationType); $alias->setType($relationType);
} }
@ -236,16 +246,16 @@ class LDrawLoader extends Loader
* *
* @return bool * @return bool
*/ */
private function fileFilter($header) private function isPartIncluded($header)
{ {
// Do not include sticker parts and incomplete parts // Do not include sticker parts and incomplete parts
if (strpos($header['name'], 'Sticker') !== 0 && strpos($header['id'], 's') !== 0 && $header['type'] != 'Subpart') { if (strpos($header['name'], 'Sticker') !== 0 && strpos($header['id'], 's') !== 0 && $header['type'] != 'Subpart' && !$this->isStickerShortcutPart($header['id'])) {
// If file is alias of another part determine if referenced file should be included // If file is alias of another part determine if referenced file should be included
if (strpos($header['name'], '~Moved to ') === 0) { if (strpos($header['name'], '~Moved to ') === 0) {
// Get file path of referenced part file // Get file path of referenced part file
$alias = 'parts/'.$this->getObsoleteParentId($header['name']).'.dat'; $alias = 'parts/'.$this->getObsoleteParentId($header['name']).'.dat';
if ($this->ldraw->has($alias)) { if ($this->ldraw->has($alias)) {
return $this->fileFilter($this->getPartHeader($this->ldraw->get($alias)->getMetadata())); return $this->isPartIncluded($this->getPartHeader($this->ldraw->get($alias)->getMetadata()));
} }
return false; return false;
@ -265,18 +275,37 @@ class LDrawLoader extends Loader
* *
* http://www.ldraw.org/library/tracker/ref/numberfaq/ * http://www.ldraw.org/library/tracker/ref/numberfaq/
* *
* @param $id
* *
* @param $filename * @return string|null LDraw number of printed part parent
*/ */
private function getPrinetedParentId($filename) private function getPrinetedParentId($id)
{ {
if (preg_match('/(^.*)(p[0-9a-z][0-9a-z][0-9a-z]{0,1})$/', $filename, $matches)) { if (preg_match('/(^.*)(p[0-9a-z][0-9a-z][0-9a-z]{0,1})$/', $id, $matches)) {
return $matches[1]; return $matches[1];
} }
return null; return null;
} }
/**
* Check if part is shortcut part of stricker and part
*
* part name in format:
* nnnDnn, nnnnDnn, nnnnnDnn (a = alpha, n= numeric, x = alphanumeric)
*
* http://www.ldraw.org/library/tracker/ref/numberfaq/
*
* @param $id
*
* @return string|null LDraw number of printed part parent
*/
private function isStickerShortcutPart($id)
{
// some of files are in format nnnDaa
return preg_match('/(^.*)(d[a-z0-9][a-z0-9])$/', $id);
}
/** /**
* Get parent of obsolete part kept for reference. * Get parent of obsolete part kept for reference.
* *
@ -393,13 +422,12 @@ class LDrawLoader extends Loader
} }
} elseif (strpos($line, '1 ') === 0) { } elseif (strpos($line, '1 ') === 0) {
$header['subparts'][] = $this->getAlias($line); $header['subparts'][] = $this->getAlias($line);
} elseif ($line != '') {
break;
} }
} }
if (strpos($header['name'], '~') === 0) { if (strpos($header['name'], '~Moved to') === 0) {
$header['name'] = ltrim($header['name'], '~'); $header['type'] = 'Alias';
} elseif (strpos($header['name'], '~') === 0) {
$header['type'] = 'Obsolete/Subpart'; $header['type'] = 'Obsolete/Subpart';
} elseif ($printParentId = $this->getPrinetedParentId($header['id'])) { } elseif ($printParentId = $this->getPrinetedParentId($header['id'])) {
$header['type'] = 'Print'; $header['type'] = 'Print';