diff --git a/src/Gui/SelectionView.cpp b/src/Gui/SelectionView.cpp index 75d37683de..8b7b740ecd 100644 --- a/src/Gui/SelectionView.cpp +++ b/src/Gui/SelectionView.cpp @@ -34,6 +34,7 @@ /// Here the FreeCAD includes sorted by Base,App,Gui...... #include +#include #include "SelectionView.h" #include "Command.h" #include "Application.h" @@ -247,17 +248,34 @@ void SelectionView::toPython(void) // remove possible space from object name followed by label elements[1] = elements[1].split(QString::fromLatin1(" "))[0]; - QString cmd = QString::fromLatin1("obj = App.getDocument(\"%1\").getObject(\"%2\")").arg(elements[0]).arg(elements[1]); - Gui::Command::runCommand(Gui::Command::Gui,cmd.toLatin1()); - if (elements.length() > 2) { - elements[2] = elements[2].split(QString::fromLatin1(" "))[0]; - if ( elements[2].contains(QString::fromLatin1("Face")) || elements[2].contains(QString::fromLatin1("Edge")) ) { - cmd = QString::fromLatin1("shp = App.getDocument(\"%1\").getObject(\"%2\").Shape").arg(elements[0]).arg(elements[1]); - Gui::Command::runCommand(Gui::Command::Gui,cmd.toLatin1()); - cmd = QString::fromLatin1("elt = App.getDocument(\"%1\").getObject(\"%2\").Shape.%3").arg(elements[0]).arg(elements[1]).arg(elements[2]); + try { + QString cmd = QString::fromLatin1("obj = App.getDocument(\"%1\").getObject(\"%2\")").arg(elements[0]).arg(elements[1]); + Gui::Command::runCommand(Gui::Command::Gui,cmd.toLatin1()); + if (elements.length() > 2) { + elements[2] = elements[2].split(QString::fromLatin1(" "))[0]; + App::Document* doc = App::GetApplication().getDocument(elements[0].toLatin1()); + App::DocumentObject* obj = doc->getObject(elements[1].toLatin1()); + QString property = getProperty(obj); + + cmd = QString::fromLatin1("shp = App.getDocument(\"%1\").getObject(\"%2\").%3") + .arg(elements[0]) + .arg(elements[1]) + .arg(property); Gui::Command::runCommand(Gui::Command::Gui,cmd.toLatin1()); + + if (supportPart(obj, elements[2])) { + cmd = QString::fromLatin1("elt = App.getDocument(\"%1\").getObject(\"%2\").%3.%4") + .arg(elements[0]) + .arg(elements[1]) + .arg(property) + .arg(elements[2]); + Gui::Command::runCommand(Gui::Command::Gui,cmd.toLatin1()); + } } } + catch (const Base::Exception& e) { + e.ReportException(); + } } void SelectionView::showPart(void) @@ -268,14 +286,76 @@ void SelectionView::showPart(void) QStringList elements = item->text().split(QString::fromLatin1(".")); if (elements.length() > 2) { elements[2] = elements[2].split(QString::fromLatin1(" "))[0]; - if ( elements[2].contains(QString::fromLatin1("Face")) || elements[2].contains(QString::fromLatin1("Edge")) ) { - Gui::Command::addModule(Gui::Command::Gui,"Part"); - QString cmd = QString::fromLatin1("Part.show(App.getDocument(\"%1\").getObject(\"%2\").Shape.%3)").arg(elements[0]).arg(elements[1]).arg(elements[2]); - Gui::Command::runCommand(Gui::Command::Gui,cmd.toLatin1()); + App::Document* doc = App::GetApplication().getDocument(elements[0].toLatin1()); + App::DocumentObject* obj = doc->getObject(elements[1].toLatin1()); + QString module = getModule(obj->getTypeId().getName()); + QString property = getProperty(obj); + if (!module.isEmpty() && !property.isEmpty() && supportPart(obj, elements[2])) { + try { + Gui::Command::addModule(Gui::Command::Gui, module.toLatin1()); + QString cmd = QString::fromLatin1("%1.show(App.getDocument(\"%2\").getObject(\"%3\").%4.%5)") + .arg(module) + .arg(elements[0]) + .arg(elements[1]) + .arg(property) + .arg(elements[2]); + Gui::Command::runCommand(Gui::Command::Gui,cmd.toLatin1()); + } + catch (const Base::Exception& e) { + e.ReportException(); + } } } } +QString SelectionView::getModule(const char* type) const +{ + Base::Type partType = Base::Type::fromName("Part::Feature"); + Base::Type meshType = Base::Type::fromName("Mesh::Feature"); + Base::Type pntsType = Base::Type::fromName("Points::Feature"); + Base::Type typeId = Base::Type::fromName(type); + if (typeId.isDerivedFrom(partType)) + return QString::fromLatin1("Part"); + if (typeId.isDerivedFrom(meshType)) + return QString::fromLatin1("Mesh"); + if (typeId.isDerivedFrom(pntsType)) + return QString::fromLatin1("Points"); + return QString(); +} + +QString SelectionView::getProperty(App::DocumentObject* obj) const +{ + QString property; + if (obj->getTypeId().isDerivedFrom(App::GeoFeature::getClassTypeId())) { + App::GeoFeature* geo = static_cast(obj); + const App::PropertyComplexGeoData* data = geo->getPropertyOfGeometry(); + const char* name = data ? data->getName() : nullptr; + if (name) { + property = QString::fromLatin1(name); + } + } + + return property; +} + +bool SelectionView::supportPart(App::DocumentObject* obj, const QString& part) const +{ + if (obj->getTypeId().isDerivedFrom(App::GeoFeature::getClassTypeId())) { + App::GeoFeature* geo = static_cast(obj); + const App::PropertyComplexGeoData* data = geo->getPropertyOfGeometry(); + if (data) { + const Data::ComplexGeoData* geometry = data->getComplexData(); + std::vector types = geometry->getElementTypes(); + for (auto it : types) { + if (part.startsWith(QString::fromLatin1(it))) + return true; + } + } + } + + return false; +} + void SelectionView::onItemContextMenu(const QPoint& point) { QListWidgetItem *item = selectionView->itemAt(point); @@ -301,12 +381,10 @@ void SelectionView::onItemContextMenu(const QPoint& point) toPythonAction->setToolTip(tr("Reveals this object and its subelements in the python console.")); QStringList elements = item->text().split(QString::fromLatin1(".")); if (elements.length() > 2) { - if ( elements[2].contains(QString::fromLatin1("Face")) || elements[2].contains(QString::fromLatin1("Edge")) ) { - // subshape-specific entries - QAction *showPart = menu.addAction(tr("Duplicate subshape"),this,SLOT(showPart())); - showPart->setIcon(QIcon(QString::fromLatin1(":/icons/ClassBrowser/member.svg"))); - showPart->setToolTip(tr("Creates a standalone copy of this subshape in the document")); - } + // subshape-specific entries + QAction *showPart = menu.addAction(tr("Duplicate subshape"),this,SLOT(showPart())); + showPart->setIcon(QIcon(QString::fromLatin1(":/icons/ClassBrowser/member.svg"))); + showPart->setToolTip(tr("Creates a standalone copy of this subshape in the document")); } menu.exec(selectionView->mapToGlobal(point)); } diff --git a/src/Gui/SelectionView.h b/src/Gui/SelectionView.h index 791c283e5b..15153fbea1 100644 --- a/src/Gui/SelectionView.h +++ b/src/Gui/SelectionView.h @@ -31,6 +31,10 @@ class QListWidget; +namespace App { +class DocumentObject; +} + namespace Gui { namespace DockWnd { @@ -82,6 +86,10 @@ public Q_SLOTS: void touch(void); void showPart(void); +private: + QString getModule(const char* type) const; + QString getProperty(App::DocumentObject* obj) const; + bool supportPart(App::DocumentObject* obj, const QString& part) const; }; } // namespace DockWnd