FreeCAD

Форк
0
/
CommandAlterGeometry.cpp 
252 строки · 11.2 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2011 Jürgen Riegel <juergen.riegel@web.de>              *
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 "PreCompiled.h"
24
#ifndef _PreComp_
25
#endif
26

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>
37

38
#include "GeometryCreationMode.h"
39
#include "Utils.h"
40
#include "ViewProviderSketch.h"
41

42

43
using namespace std;
44
using namespace SketcherGui;
45
using namespace Sketcher;
46

47
bool isAlterGeoActive(Gui::Document* doc)
48
{
49
    if (doc) {
50
        // checks if a Sketch Viewprovider is in Edit
51
        if (doc->getInEdit()
52
            && doc->getInEdit()->isDerivedFrom(SketcherGui::ViewProviderSketch::getClassTypeId())) {
53
            return true;
54
        }
55
    }
56

57
    return false;
58
}
59

60
namespace SketcherGui
61
{
62

63
extern GeometryCreationMode geometryCreationMode;
64

65
/* Constrain commands =======================================================*/
66
DEF_STD_CMD_AU(CmdSketcherToggleConstruction)
67

68
CmdSketcherToggleConstruction::CmdSketcherToggleConstruction()
69
    : Command("Sketcher_ToggleConstruction")
70
{
71
    sAppModule = "Sketcher";
72
    sGroup = "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";
78
    sAccel = "G, N";
79
    eType = ForEdit;
80

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");
119
}
120

121
void CmdSketcherToggleConstruction::updateAction(int mode)
122
{
123
    auto act = getAction();
124
    if (act) {
125
        switch (static_cast<GeometryCreationMode>(mode)) {
126
            case GeometryCreationMode::Normal:
127
                act->setIcon(Gui::BitmapFactory().iconFromTheme("Sketcher_ToggleConstruction"));
128
                break;
129
            case GeometryCreationMode::Construction:
130
                act->setIcon(
131
                    Gui::BitmapFactory().iconFromTheme("Sketcher_ToggleConstruction_Constr"));
132
                break;
133
        }
134
    }
135
}
136

137
void CmdSketcherToggleConstruction::activated(int iMsg)
138
{
139
    Q_UNUSED(iMsg);
140
    // Option A: nothing is selected change creation mode from/to construction
141
    if (Gui::Selection().countObjectsOfType(Sketcher::SketchObject::getClassTypeId()) == 0) {
142

143
        Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager();
144

145
        if (geometryCreationMode == GeometryCreationMode::Construction) {
146
            geometryCreationMode = GeometryCreationMode::Normal;
147
        }
148
        else {
149
            geometryCreationMode = GeometryCreationMode::Construction;
150
        }
151

152
        rcCmdMgr.updateCommands("ToggleConstruction", static_cast<int>(geometryCreationMode));
153
    }
154
    else  // there was a selection, so operate in toggle mode.
155
    {
156
        // get the selection
157
        std::vector<Gui::SelectionObject> selection;
158
        selection =
159
            getSelection().getSelectionEx(nullptr, Sketcher::SketchObject::getClassTypeId());
160

161
        Sketcher::SketchObject* Obj =
162
            static_cast<Sketcher::SketchObject*>(selection[0].getObject());
163

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."));
169
            return;
170
        }
171

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."));
178
            return;
179
        }
180

181
        // undo command open
182
        openCommand(QT_TRANSLATE_NOOP("Command", "Toggle draft from/to draft"));
183

184
        // go through the selected subelements
185
        bool verticesonly = true;
186

187
        for (const auto& subname : SubNames) {
188
            if (subname.size() > 4 && subname.substr(0, 4) == "Edge") {
189
                verticesonly = false;
190
            }
191
        }
192

193
        for (std::vector<std::string>::const_iterator it = SubNames.begin(); it != SubNames.end();
194
             ++it) {
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.
199
            //
200
            // Why?
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.
205

206

207
            // only handle edges
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);
212
            }
213
            else if (verticesonly && it->size() > 6 && it->substr(0, 6) == "Vertex") {
214
                int vertexId = std::atoi(it->substr(6, 4000).c_str()) - 1;
215

216
                int geoId;
217
                PointPos pos;
218
                Obj->getGeoVertexIndex(vertexId, geoId, pos);
219

220
                auto geo = Obj->getGeometry(geoId);
221

222
                if (geo && geo->is<Part::GeomPoint>()) {
223
                    // issue the actual commands to toggle
224
                    Gui::cmdAppObjectArgs(selection[0].getObject(),
225
                                          "toggleConstruction(%d) ",
226
                                          geoId);
227
                }
228
            }
229
        }
230
        // finish the transaction and update
231
        commitCommand();
232

233
        tryAutoRecompute(Obj);
234

235
        // clear the selection (convenience)
236
        getSelection().clearSelection();
237
    }
238
}
239

240
bool CmdSketcherToggleConstruction::isActive()
241
{
242
    return isAlterGeoActive(getActiveGuiDocument());
243
}
244

245
}  // namespace SketcherGui
246

247
void CreateSketcherCommandsAlterGeo()
248
{
249
    Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager();
250

251
    rcCmdMgr.addCommand(new CmdSketcherToggleConstruction());
252
}
253

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.