| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2017 Stefan Tröger <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <QApplication> |
| 24 |
|
| 25 |
#include <App/GroupExtension.h> |
| 26 |
#include <App/Document.h> |
| 27 |
|
| 28 |
#include "Command.h" |
| 29 |
#include "ActiveObjectList.h" |
| 30 |
#include "Application.h" |
| 31 |
#include "Document.h" |
| 32 |
#include "MDIView.h" |
| 33 |
#include "ViewProviderDocumentObject.h" |
| 34 |
#include "Selection.h" |
| 35 |
|
| 36 |
using namespace Gui; |
| 37 |
|
| 38 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 39 |
|
| 40 |
//=========================================================================== |
| 41 |
// Std_Part |
| 42 |
//=========================================================================== |
| 43 |
DEF_STD_CMD_A(StdCmdPart) |
| 44 |
|
| 45 |
StdCmdPart::StdCmdPart() |
| 46 |
: Command("Std_Part") |
| 47 |
{ |
| 48 |
sGroup = "Structure"; |
| 49 |
sMenuText = QT_TR_NOOP("New Part"); |
| 50 |
sToolTipText = QT_TR_NOOP( |
| 51 |
"Creates a part, which is a general-purpose container to group objects so they " |
| 52 |
"act as a unit in the 3D view. It is intended to arrange objects that have a part " |
| 53 |
"TopoShape, like part primitives, Part Design bodies, and other parts." |
| 54 |
); |
| 55 |
sWhatsThis = "Std_Part"; |
| 56 |
sStatusTip = sToolTipText; |
| 57 |
sPixmap = "Geofeaturegroup"; |
| 58 |
} |
| 59 |
|
| 60 |
void StdCmdPart::activated(int iMsg) |
| 61 |
{ |
| 62 |
Q_UNUSED(iMsg); |
| 63 |
|
| 64 |
openCommand(QT_TRANSLATE_NOOP("Command", "Add a part")); |
| 65 |
std::string FeatName = getUniqueObjectName("Part"); |
| 66 |
|
| 67 |
std::string PartName; |
| 68 |
PartName = getUniqueObjectName("Part"); |
| 69 |
doCommand( |
| 70 |
Doc, |
| 71 |
"App.activeDocument().Tip = App.activeDocument().addObject('App::Part','%s')", |
| 72 |
PartName.c_str() |
| 73 |
); |
| 74 |
// TODO We really must set label ourselves? (2015-08-17, Fat-Zer) |
| 75 |
doCommand( |
| 76 |
Doc, |
| 77 |
"App.activeDocument().%s.Label = '%s'", |
| 78 |
PartName.c_str(), |
| 79 |
QObject::tr(PartName.c_str()).toUtf8().data() |
| 80 |
); |
| 81 |
|
| 82 |
doCommand( |
| 83 |
Doc, |
| 84 |
"selected_objects = Gui.Selection.getSelection()\n" |
| 85 |
"if len(selected_objects) > 1:\n" |
| 86 |
" for obj in selected_objects:\n" |
| 87 |
" # Add subobjects if obj is a container\n" |
| 88 |
" if hasattr(obj, 'OutList') and len(obj.OutList) > 0:\n" |
| 89 |
" for child in obj.OutList:\n" |
| 90 |
" App.activeDocument().%s.addObject(child)\n" |
| 91 |
" App.activeDocument().%s.addObject(obj)\n", |
| 92 |
PartName.c_str(), |
| 93 |
PartName.c_str() |
| 94 |
); |
| 95 |
|
| 96 |
doCommand( |
| 97 |
Gui::Command::Gui, |
| 98 |
"Gui.activateView('Gui::View3DInventor', True)\n" |
| 99 |
"Gui.activeView().setActiveObject('%s', App.activeDocument().%s)", |
| 100 |
PARTKEY, |
| 101 |
PartName.c_str() |
| 102 |
); |
| 103 |
|
| 104 |
updateActive(); |
| 105 |
} |
| 106 |
|
| 107 |
bool StdCmdPart::isActive() |
| 108 |
{ |
| 109 |
return hasActiveDocument(); |
| 110 |
} |
| 111 |
|
| 112 |
//=========================================================================== |
| 113 |
// Std_Group |
| 114 |
//=========================================================================== |
| 115 |
DEF_STD_CMD_A(StdCmdGroup) |
| 116 |
|
| 117 |
StdCmdGroup::StdCmdGroup() |
| 118 |
: Command("Std_Group") |
| 119 |
{ |
| 120 |
sGroup = "Structure"; |
| 121 |
sMenuText = QT_TR_NOOP("New Group"); |
| 122 |
sToolTipText = QT_TR_NOOP( |
| 123 |
"Creates a group, which is a general-purpose container to group objects in the " |
| 124 |
"tree view, regardless of their data type. It is a simple folder to organize " |
| 125 |
"the objects in a model." |
| 126 |
); |
| 127 |
sWhatsThis = "Std_Group"; |
| 128 |
sStatusTip = sToolTipText; |
| 129 |
sPixmap = "folder"; |
| 130 |
} |
| 131 |
|
| 132 |
void StdCmdGroup::activated(int iMsg) |
| 133 |
{ |
| 134 |
Q_UNUSED(iMsg); |
| 135 |
|
| 136 |
openCommand(QT_TRANSLATE_NOOP("Command", "Add a group")); |
| 137 |
|
| 138 |
std::string GroupName; |
| 139 |
GroupName = getUniqueObjectName("Group"); |
| 140 |
QString label = QApplication::translate("Std_Group", "Group"); |
| 141 |
|
| 142 |
// create a group |
| 143 |
doCommand( |
| 144 |
Doc, |
| 145 |
"group = App.activeDocument().addObject('App::DocumentObjectGroup','%s')", |
| 146 |
GroupName.c_str() |
| 147 |
); |
| 148 |
doCommand(Doc, "group.Label = '%s'", label.toUtf8().data()); |
| 149 |
doCommand(Doc, "App.activeDocument().Tip = group"); |
| 150 |
|
| 151 |
// try to add the group to any active object that supports grouping (has GroupExtension) |
| 152 |
if (auto* activeDoc = Gui::Application::Instance->activeDocument()) { |
| 153 |
if (auto* activeView = activeDoc->getActiveView()) { |
| 154 |
// find the first active object with GroupExtension |
| 155 |
if (auto* activeObj = activeView->getActiveObjectWithExtension( |
| 156 |
App::GroupExtension::getExtensionClassTypeId() |
| 157 |
)) { |
| 158 |
doCommand( |
| 159 |
Doc, |
| 160 |
"active_obj = App.activeDocument().getObject('%s')\n" |
| 161 |
"if active_obj and active_obj.allowObject(group):\n" |
| 162 |
" active_obj.Group += [group]", |
| 163 |
activeObj->getNameInDocument() |
| 164 |
); |
| 165 |
} |
| 166 |
} |
| 167 |
} // if we have no active object, group will be added to root doc |
| 168 |
|
| 169 |
commitCommand(); |
| 170 |
|
| 171 |
Gui::Document* gui = Application::Instance->activeDocument(); |
| 172 |
App::Document* app = gui->getDocument(); |
| 173 |
ViewProvider* vp = gui->getViewProvider(app->getActiveObject()); |
| 174 |
if (vp && vp->isDerivedFrom<ViewProviderDocumentObject>()) { |
| 175 |
gui->signalScrollToObject(*static_cast<ViewProviderDocumentObject*>(vp)); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
bool StdCmdGroup::isActive() |
| 180 |
{ |
| 181 |
return hasActiveDocument(); |
| 182 |
} |
| 183 |
|
| 184 |
//=========================================================================== |
| 185 |
// Std_VarSet |
| 186 |
//=========================================================================== |
| 187 |
DEF_STD_CMD_A(StdCmdVarSet) |
| 188 |
|
| 189 |
StdCmdVarSet::StdCmdVarSet() |
| 190 |
: Command("Std_VarSet") |
| 191 |
{ |
| 192 |
sGroup = "Structure"; |
| 193 |
sMenuText = QT_TR_NOOP("Variable Set"); |
| 194 |
sToolTipText |
| 195 |
= QT_TR_NOOP("Creates a variable set, which is an object that maintains a set of properties to be used as variables"); |
| 196 |
sWhatsThis = "Std_VarSet"; |
| 197 |
sStatusTip = sToolTipText; |
| 198 |
sPixmap = "VarSet"; |
| 199 |
} |
| 200 |
|
| 201 |
void StdCmdVarSet::activated(int iMsg) |
| 202 |
{ |
| 203 |
Q_UNUSED(iMsg); |
| 204 |
|
| 205 |
openCommand(QT_TRANSLATE_NOOP("Command", "Add a variable set")); |
| 206 |
|
| 207 |
std::string VarSetName; |
| 208 |
VarSetName = getUniqueObjectName("VarSet"); |
| 209 |
doCommand(Doc, "App.activeDocument().addObject('App::VarSet','%s')", VarSetName.c_str()); |
| 210 |
|
| 211 |
Gui::Document* docGui = Application::Instance->activeDocument(); |
| 212 |
App::Document* doc = docGui->getDocument(); |
| 213 |
|
| 214 |
// add the varset to a group if it is selected |
| 215 |
auto sels = Selection().getSelectionEx( |
| 216 |
nullptr, |
| 217 |
App::DocumentObject::getClassTypeId(), |
| 218 |
ResolveMode::OldStyleElement, |
| 219 |
true |
| 220 |
); |
| 221 |
if (sels.size() == 1) { |
| 222 |
App::DocumentObject* obj = sels[0].getObject(); |
| 223 |
auto group = obj->getExtension<App::GroupExtension>(); |
| 224 |
if (group) { |
| 225 |
group->addObject(doc->getObject(VarSetName.c_str())); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
// select the new varset |
| 230 |
Selection().clearSelection(); |
| 231 |
Selection().addSelection(doc->getName(), VarSetName.c_str()); |
| 232 |
|
| 233 |
commitCommand(); |
| 234 |
|
| 235 |
doCommand(Doc, "App.ActiveDocument.getObject('%s').ViewObject.doubleClicked()", VarSetName.c_str()); |
| 236 |
} |
| 237 |
|
| 238 |
bool StdCmdVarSet::isActive() |
| 239 |
{ |
| 240 |
return hasActiveDocument(); |
| 241 |
} |
| 242 |
|
| 243 |
namespace Gui |
| 244 |
{ |
| 245 |
|
| 246 |
void CreateStructureCommands() |
| 247 |
{ |
| 248 |
CommandManager& rcCmdMgr = Application::Instance->commandManager(); |
| 249 |
|
| 250 |
rcCmdMgr.addCommand(new StdCmdPart()); |
| 251 |
rcCmdMgr.addCommand(new StdCmdGroup()); |
| 252 |
rcCmdMgr.addCommand(new StdCmdVarSet()); |
| 253 |
} |
| 254 |
|
| 255 |
} // namespace Gui |