Allow to specify the 3d model for a footprint.

This commit is contained in:
Jan Böhmer 2019-10-03 14:04:09 +02:00
parent d9fe77d0e8
commit 6645ab0b61
8 changed files with 100 additions and 45 deletions

View file

@ -37,6 +37,7 @@ use App\Entity\Attachments\AttachmentType;
use App\Entity\Attachments\FootprintAttachment; use App\Entity\Attachments\FootprintAttachment;
use App\Entity\Parts\Footprint; use App\Entity\Parts\Footprint;
use App\Form\AdminPages\BaseEntityAdminForm; use App\Form\AdminPages\BaseEntityAdminForm;
use App\Form\AdminPages\FootprintAdminForm;
use App\Services\EntityExporter; use App\Services\EntityExporter;
use App\Services\EntityImporter; use App\Services\EntityImporter;
use App\Services\StructuralElementRecursionHelper; use App\Services\StructuralElementRecursionHelper;
@ -55,7 +56,7 @@ class FootprintController extends BaseAdminController
protected $entity_class = Footprint::class; protected $entity_class = Footprint::class;
protected $twig_template = 'AdminPages/FootprintAdmin.html.twig'; protected $twig_template = 'AdminPages/FootprintAdmin.html.twig';
protected $form_class = BaseEntityAdminForm::class; protected $form_class = FootprintAdminForm::class;
protected $route_base = 'footprint'; protected $route_base = 'footprint';
protected $attachment_class = FootprintAttachment::class; protected $attachment_class = FootprintAttachment::class;

View file

@ -53,16 +53,21 @@ abstract class Attachment extends NamedDBElement
* Based on: https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types * Based on: https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types
* It will be used to determine if a attachment is a picture and therefore will be shown to user as preview. * It will be used to determine if a attachment is a picture and therefore will be shown to user as preview.
*/ */
const PICTURE_EXTS = ['apng', 'bmp', 'gif', 'ico', 'cur', 'jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png', public const PICTURE_EXTS = ['apng', 'bmp', 'gif', 'ico', 'cur', 'jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png',
'svg', 'webp']; 'svg', 'webp'];
/**
* A list of extensions that will be treated as a 3D Model that can be shown to user directly in Part-DB.
*/
public const MODEL_EXTS = ['x3d'];
/** /**
* When the path begins with one of this placeholders * When the path begins with one of this placeholders
*/ */
const INTERNAL_PLACEHOLDER = ['%BASE%', '%MEDIA%']; public const INTERNAL_PLACEHOLDER = ['%BASE%', '%MEDIA%'];
/** @var array Placeholders for attachments which using built in files. */ /** @var array Placeholders for attachments which using built in files. */
const BUILTIN_PLACEHOLDER = ['%FOOTPRINTS%', '%FOOTPRINTS3D%']; public const BUILTIN_PLACEHOLDER = ['%FOOTPRINTS%', '%FOOTPRINTS3D%'];
/** /**
* @var bool * @var bool
@ -101,7 +106,7 @@ abstract class Attachment extends NamedDBElement
/** /**
* Check if this attachement is a picture (analyse the file's extension). * Check if this attachement is a picture (analyse the file's extension).
* If the link is external, it is assumed that this is false. * If the link is external, it is assumed that this is true.
* *
* @return bool * true if the file extension is a picture extension * @return bool * true if the file extension is a picture extension
* * otherwise false * * otherwise false
@ -118,6 +123,23 @@ abstract class Attachment extends NamedDBElement
return in_array(strtolower($extension), static::PICTURE_EXTS, true); return in_array(strtolower($extension), static::PICTURE_EXTS, true);
} }
/**
* Check if this attachment is a 3D model and therfore can be directly shown to user.
* If the attachment is external, false is returned (3D Models must be internal).
* @return bool
*/
public function is3DModel() : bool
{
//We just assume that 3D Models are internally saved, otherwise we get problems loading them.
if ($this->isExternal()) {
return false;
}
$extension = pathinfo($this->getPath(), PATHINFO_EXTENSION);
return in_array(strtolower($extension), static::MODEL_EXTS, true);
}
/** /**
* Checks if the attachment file is externally saved (the database saves an URL) * Checks if the attachment file is externally saved (the database saves an URL)
* @return bool true, if the file is saved externally * @return bool true, if the file is saved externally

View file

@ -98,7 +98,7 @@ class Footprint extends PartsContainingDBElement
protected $parts; protected $parts;
/** /**
* @var FootprintAttachment * @var FootprintAttachment|null
* @ORM\ManyToOne(targetEntity="App\Entity\Attachments\FootprintAttachment") * @ORM\ManyToOne(targetEntity="App\Entity\Attachments\FootprintAttachment")
* @ORM\JoinColumn(name="id_footprint_3d", referencedColumnName="id") * @ORM\JoinColumn(name="id_footprint_3d", referencedColumnName="id")
*/ */
@ -120,24 +120,12 @@ class Footprint extends PartsContainingDBElement
****************************************/ ****************************************/
/** /**
* Get the filename of the picture (absolute path from filesystem root). * Returns the 3D Model associated with this footprint.
* * @return FootprintAttachment|null
* @return string the saved filename in the DB
* * an empty string if there is no picture
*/ */
public function getFilename(): string public function getFootprint3d() : ?FootprintAttachment
{ {
return $this->filename; return $this->footprint_3d;
}
/**
* Get the filename of the 3d model (absolute path from filesystem root).
* @return string * the absolute path to the model (from filesystem root), as a UNIX path (with slashes)
* * an empty string if there is no model
*/
public function get3dFilename(): string
{
return $this->filename_3d;
} }
/******************************************************************************** /********************************************************************************
@ -147,27 +135,14 @@ class Footprint extends PartsContainingDBElement
*********************************************************************************/ *********************************************************************************/
/** /**
* Change the filename of this footprint. * Sets the 3D Model associated with this footprint.
* @param string $new_filename The new file name * @param FootprintAttachment|null $new_attachment
* @return Footprint
*/
public function setFilename(string $new_filename): self
{
$this->filename = $new_filename;
return $this;
}
/**
* Change the 3d model filename of this footprint.
* @param string $new_filename The new filename
*
* @return Footprint * @return Footprint
*/ */
public function set3dFilename(string $new_filename): self public function setFootprint3d(?FootprintAttachment $new_attachment) : Footprint
{ {
$this->filename = $new_filename; $this->footprint_3d = $new_attachment;
return $this; return $this;
} }
} }

View file

@ -0,0 +1,51 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Form\AdminPages;
use App\Entity\Base\NamedDBElement;
use App\Form\Type\MasterPictureAttachmentType;
use Symfony\Component\Form\FormBuilderInterface;
class FootprintAdminForm extends BaseEntityAdminForm
{
public function additionalFormElements(FormBuilderInterface $builder, array $options, NamedDBElement $entity)
{
$builder->add('footprint_3d', MasterPictureAttachmentType::class, [
'required' => false,
'disabled' => !$this->security->isGranted($entity->getID() === null ? 'create' : 'edit', $entity),
'label' => $this->trans->trans('footprint.edit.3d_model'),
'filter' => '3d_model',
'entity' => $entity
]);
}
}

View file

@ -58,6 +58,8 @@ class MasterPictureAttachmentType extends AbstractType
if ($options['filter'] === 'picture' && !$choice->isPicture()) { if ($options['filter'] === 'picture' && !$choice->isPicture()) {
$tmp += ['disabled' => 'disabled']; $tmp += ['disabled' => 'disabled'];
} elseif ($options['filter'] === '3d_model' && !$choice->is3DModel()) {
$tmp += ['disabled' => 'disabled'];
} }
return $tmp; return $tmp;
@ -85,7 +87,7 @@ class MasterPictureAttachmentType extends AbstractType
} }
]); ]);
$resolver->setAllowedValues('filter', ['', 'picture', '3d_models']); $resolver->setAllowedValues('filter', ['', 'picture', '3d_model']);
} }
public function getParent() public function getParent()

View file

@ -90,6 +90,9 @@
<div class="tab-pane" id="attachments"> <div class="tab-pane" id="attachments">
{% include "AdminPages/_attachments.html.twig" %} {% include "AdminPages/_attachments.html.twig" %}
{% block master_picture_block %}
{{ form_row(form.master_picture_attachment) }}
{% endblock %}
</div> </div>
</div> </div>

View file

@ -2,4 +2,9 @@
{% block card_title %} {% block card_title %}
<i class="fas fa-microchip fa-fw"></i> {% trans %}footprint.labelp{% endtrans %} <i class="fas fa-microchip fa-fw"></i> {% trans %}footprint.labelp{% endtrans %}
{% endblock %}
{% block master_picture_block %}
{{ form_row(form.master_picture_attachment) }}
{{ form_row(form.footprint_3d) }}
{% endblock %} {% endblock %}

View file

@ -67,10 +67,6 @@
{% trans %}attachment.create{% endtrans %} {% trans %}attachment.create{% endtrans %}
</button> </button>
{% block master_picture_block %}
{{ form_row(form.master_picture_attachment) }}
{% endblock %}
<script> <script>
function delete_attachment_entry(btn) { function delete_attachment_entry(btn) {
window.bootbox.confirm('{% trans %}part_lot.edit.delete.confirm{% endtrans %}', function (result) { window.bootbox.confirm('{% trans %}part_lot.edit.delete.confirm{% endtrans %}', function (result) {