1
/***************************************************************************
2
* Copyright (c) 2011 Jürgen Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
27
#include <Gui/Action.h>
28
#include <Gui/Application.h>
29
#include <Gui/BitmapFactory.h>
30
#include <Gui/CommandT.h>
31
#include <Gui/Document.h>
32
#include <Gui/MainWindow.h>
33
#include <Gui/Notifications.h>
34
#include <Gui/Selection.h>
35
#include <Gui/SelectionObject.h>
36
#include <Mod/Sketcher/App/SketchObject.h>
38
#include "GeometryCreationMode.h"
40
#include "ViewProviderSketch.h"
44
using namespace SketcherGui;
45
using namespace Sketcher;
47
bool isAlterGeoActive(Gui::Document* doc)
50
// checks if a Sketch Viewprovider is in Edit
52
&& doc->getInEdit()->isDerivedFrom(SketcherGui::ViewProviderSketch::getClassTypeId())) {
63
extern GeometryCreationMode geometryCreationMode;
65
/* Constrain commands =======================================================*/
66
DEF_STD_CMD_AU(CmdSketcherToggleConstruction)
68
CmdSketcherToggleConstruction::CmdSketcherToggleConstruction()
69
: Command("Sketcher_ToggleConstruction")
71
sAppModule = "Sketcher";
73
sMenuText = QT_TR_NOOP("Toggle construction geometry");
74
sToolTipText = QT_TR_NOOP("Toggles the toolbar or selected geometry to/from construction mode");
75
sWhatsThis = "Sketcher_ToggleConstruction";
76
sStatusTip = sToolTipText;
77
sPixmap = "Sketcher_ToggleConstruction";
81
// list of toggle construction commands
82
Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager();
83
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateLine");
84
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreatePolyline");
85
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CompLine");
86
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateRectangle");
87
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateRectangle_Center");
88
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateOblong");
89
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CompCreateRectangles");
90
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateArcSlot");
91
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateSlot");
92
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CompSlot");
93
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateArc");
94
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_Create3PointArc");
95
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateEllipseByCenter");
96
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateEllipseBy3Points");
97
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateArcOfEllipse");
98
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateArcOfHyperbola");
99
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateArcOfParabola");
100
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CompCreateArc");
101
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateCircle");
102
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_Create3PointCircle");
103
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CompCreateConic");
104
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateTriangle");
105
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateSquare");
106
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreatePentagon");
107
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateHexagon");
108
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateHeptagon");
109
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateOctagon");
110
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateRegularPolygon");
111
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CompCreateRegularPolygon");
112
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateBSpline");
113
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreatePeriodicBSpline");
114
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreateBSplineByInterpolation");
115
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CreatePeriodicBSplineByInterpolation");
116
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CompCreateBSpline");
117
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_CarbonCopy");
118
rcCmdMgr.addCommandMode("ToggleConstruction", "Sketcher_ToggleConstruction");
121
void CmdSketcherToggleConstruction::updateAction(int mode)
123
auto act = getAction();
125
switch (static_cast<GeometryCreationMode>(mode)) {
126
case GeometryCreationMode::Normal:
127
act->setIcon(Gui::BitmapFactory().iconFromTheme("Sketcher_ToggleConstruction"));
129
case GeometryCreationMode::Construction:
131
Gui::BitmapFactory().iconFromTheme("Sketcher_ToggleConstruction_Constr"));
137
void CmdSketcherToggleConstruction::activated(int iMsg)
140
// Option A: nothing is selected change creation mode from/to construction
141
if (Gui::Selection().countObjectsOfType(Sketcher::SketchObject::getClassTypeId()) == 0) {
143
Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager();
145
if (geometryCreationMode == GeometryCreationMode::Construction) {
146
geometryCreationMode = GeometryCreationMode::Normal;
149
geometryCreationMode = GeometryCreationMode::Construction;
152
rcCmdMgr.updateCommands("ToggleConstruction", static_cast<int>(geometryCreationMode));
154
else // there was a selection, so operate in toggle mode.
157
std::vector<Gui::SelectionObject> selection;
159
getSelection().getSelectionEx(nullptr, Sketcher::SketchObject::getClassTypeId());
161
Sketcher::SketchObject* Obj =
162
static_cast<Sketcher::SketchObject*>(selection[0].getObject());
164
// only one sketch with its subelements are allowed to be selected
165
if (selection.size() != 1) {
166
Gui::TranslatedUserWarning(Obj,
167
QObject::tr("Wrong selection"),
168
QObject::tr("Select edge(s) from the sketch."));
172
// get the needed lists and objects
173
const std::vector<std::string>& SubNames = selection[0].getSubNames();
174
if (SubNames.empty()) {
175
Gui::TranslatedUserWarning(Obj,
176
QObject::tr("Wrong selection"),
177
QObject::tr("Select edge(s) from the sketch."));
182
openCommand(QT_TRANSLATE_NOOP("Command", "Toggle draft from/to draft"));
184
// go through the selected subelements
185
bool verticesonly = true;
187
for (const auto& subname : SubNames) {
188
if (subname.size() > 4 && subname.substr(0, 4) == "Edge") {
189
verticesonly = false;
193
for (std::vector<std::string>::const_iterator it = SubNames.begin(); it != SubNames.end();
195
// It was decided to provide a special behaviour:
196
// Vertices will only be toggled to/from construction IF ONLY
197
// vertices are within the group.
198
// If there are a mixture of edges and vertices, vertices will be ignored.
201
// Because it is quite common to box select geometry for toggling (specially in
202
// connection with carbon copy operations). In 99% of the cases the user does not
203
// want to toggle individual points during such operations. For the remaining 1%,
204
// in 90% of the cases the uses will select just the points only naturally.
208
if (it->size() > 4 && it->substr(0, 4) == "Edge") {
209
int GeoId = std::atoi(it->substr(4, 4000).c_str()) - 1;
210
// issue the actual commands to toggle
211
Gui::cmdAppObjectArgs(selection[0].getObject(), "toggleConstruction(%d) ", GeoId);
213
else if (verticesonly && it->size() > 6 && it->substr(0, 6) == "Vertex") {
214
int vertexId = std::atoi(it->substr(6, 4000).c_str()) - 1;
218
Obj->getGeoVertexIndex(vertexId, geoId, pos);
220
auto geo = Obj->getGeometry(geoId);
222
if (geo && geo->is<Part::GeomPoint>()) {
223
// issue the actual commands to toggle
224
Gui::cmdAppObjectArgs(selection[0].getObject(),
225
"toggleConstruction(%d) ",
230
// finish the transaction and update
233
tryAutoRecompute(Obj);
235
// clear the selection (convenience)
236
getSelection().clearSelection();
240
bool CmdSketcherToggleConstruction::isActive()
242
return isAlterGeoActive(getActiveGuiDocument());
245
} // namespace SketcherGui
247
void CreateSketcherCommandsAlterGeo()
249
Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager();
251
rcCmdMgr.addCommand(new CmdSketcherToggleConstruction());