// SPDX-License-Identifier: LGPL-2.0-or-later /*************************************************************************** * Copyright (c) 2024 WandererFan * * * * This file is part of the FreeCAD CAx development system. * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Library General Public * * License as published by the Free Software Foundation; either * * version 2 of the License, or (at your option) any later version. * * * * This library 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 Library General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this library; see the file COPYING.LIB. If not, * * write to the Free Software Foundation, Inc., 59 Temple Place, * * Suite 330, Boston, MA 02111-1307, USA * * * ***************************************************************************/ //! CommandHelpers is a collection of methods for common actions in commands #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "CommandHelpers.h" using namespace TechDraw; using namespace TechDrawGui; //! find the first DrawView in the current selection for use as a base view (owner) TechDraw::DrawView* CommandHelpers::firstViewInSelection(Gui::Command* cmd) { std::vector selection = cmd->getSelection().getSelectionEx(); TechDraw::DrawView* baseView{nullptr}; if (!selection.empty()) { for (auto& selobj : selection) { if (selobj.getObject()->isDerivedFrom()) { auto docobj = selobj.getObject(); baseView = static_cast(docobj); break; } } } return baseView; } std::vector CommandHelpers::getSelectedSubElements(Gui::Command* cmd, TechDraw::DrawViewPart* &dvp, std::string subType) { std::vector selectedSubs; std::vector subNames; dvp = nullptr; std::vector selection = cmd->getSelection().getSelectionEx(); std::vector::iterator itSel = selection.begin(); for (; itSel != selection.end(); itSel++) { if ((*itSel).getObject()->isDerivedFrom()) { dvp = static_cast ((*itSel).getObject()); subNames = (*itSel).getSubNames(); break; } } if (!dvp) { QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), QObject::tr("No part view in selection")); return selectedSubs; } for (auto& s: subNames) { if (TechDraw::DrawUtil::getGeomTypeFromName(s) == subType) { selectedSubs.push_back(s); } } if (selectedSubs.empty()) { QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), QObject::tr("No %1 in selection") .arg(QString::fromStdString(subType))); return selectedSubs; } return selectedSubs; } std::pair CommandHelpers::faceFromSelection() { auto selection = Gui::Selection().getSelectionEx( nullptr, App::DocumentObject::getClassTypeId(), Gui::ResolveMode::NoResolve); if (selection.empty()) { return { nullptr, "" }; } for (auto& sel : selection) { for (auto& sub : sel.getSubNames()) { if (TechDraw::DrawUtil::getGeomTypeFromName(sub) == "Face") { return { sel.getObject(), sub }; } } } return { nullptr, "" }; }