FreeCAD

Форк
0
/
CommandSketcherVirtualSpace.cpp 
212 строк · 8.0 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2017 Abdullah Tahiri <abdullah.tahiri.yo@gmail.com>     *
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
#include <cfloat>
26
#endif
27

28
#include <App/Application.h>
29
#include <Base/Console.h>
30
#include <Gui/Action.h>
31
#include <Gui/Application.h>
32
#include <Gui/BitmapFactory.h>
33
#include <Gui/CommandT.h>
34
#include <Gui/Document.h>
35
#include <Gui/MainWindow.h>
36
#include <Gui/Notifications.h>
37
#include <Gui/Selection.h>
38
#include <Gui/SelectionObject.h>
39

40
#include "DrawSketchHandler.h"
41
#include "ViewProviderSketch.h"
42

43
#include <Mod/Part/App/Geometry.h>
44
#include <Mod/Sketcher/App/SketchObject.h>
45

46
#include "Utils.h"
47

48

49
using namespace std;
50
using namespace SketcherGui;
51
using namespace Sketcher;
52

53
bool isSketcherVirtualSpaceActive(Gui::Document* doc, bool actsOnSelection)
54
{
55
    if (doc) {
56
        // checks if a Sketch Viewprovider is in Edit and is in no special mode
57
        if (doc->getInEdit()
58
            && doc->getInEdit()->isDerivedFrom(SketcherGui::ViewProviderSketch::getClassTypeId())) {
59
            if (static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit())->getSketchMode()
60
                == ViewProviderSketch::STATUS_NONE) {
61
                if (!actsOnSelection) {
62
                    return true;
63
                }
64
                else if (Gui::Selection().countObjectsOfType(
65
                             Sketcher::SketchObject::getClassTypeId())
66
                         > 0) {
67
                    return true;
68
                }
69
            }
70
        }
71
    }
72
    return false;
73
}
74

75
void ActivateVirtualSpaceHandler(Gui::Document* doc, DrawSketchHandler* handler)
76
{
77
    std::unique_ptr<DrawSketchHandler> ptr(handler);
78
    if (doc) {
79
        if (doc->getInEdit()
80
            && doc->getInEdit()->isDerivedFrom(SketcherGui::ViewProviderSketch::getClassTypeId())) {
81
            SketcherGui::ViewProviderSketch* vp =
82
                static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());
83
            vp->purgeHandler();
84
            vp->activateHandler(std::move(ptr));
85
        }
86
    }
87
}
88

89
// Show/Hide B-spline degree
90
DEF_STD_CMD_A(CmdSketcherSwitchVirtualSpace)
91

92
CmdSketcherSwitchVirtualSpace::CmdSketcherSwitchVirtualSpace()
93
    : Command("Sketcher_SwitchVirtualSpace")
94
{
95
    sAppModule = "Sketcher";
96
    sGroup = "Sketcher";
97
    sMenuText = QT_TR_NOOP("Switch virtual space");
98
    sToolTipText =
99
        QT_TR_NOOP("Switches the selected constraints or the view to the other virtual space");
100
    sWhatsThis = "Sketcher_SwitchVirtualSpace";
101
    sStatusTip = sToolTipText;
102
    sPixmap = "Sketcher_SwitchVirtualSpace";
103
    sAccel = "Z, Z";
104
    eType = ForEdit;
105
}
106

107
void CmdSketcherSwitchVirtualSpace::activated(int iMsg)
108
{
109
    Q_UNUSED(iMsg);
110
    bool modeChange = true;
111

112
    std::vector<Gui::SelectionObject> selection;
113

114
    if (Gui::Selection().countObjectsOfType(Sketcher::SketchObject::getClassTypeId()) > 0) {
115
        // Now we check whether we have a constraint selected or not.
116
        selection = getSelection().getSelectionEx();
117

118
        // only one sketch with its subelements are allowed to be selected
119
        if (selection.size() != 1
120
            || !selection[0].isObjectTypeOf(Sketcher::SketchObject::getClassTypeId())) {
121
            Gui::TranslatedUserWarning(getActiveGuiDocument(),
122
                                       QObject::tr("Wrong selection"),
123
                                       QObject::tr("Select constraint(s) from the sketch."));
124
            return;
125
        }
126

127
        // get the needed lists and objects
128
        const std::vector<std::string>& SubNames = selection[0].getSubNames();
129
        if (SubNames.empty()) {
130
            Gui::TranslatedUserWarning(getActiveGuiDocument(),
131
                                       QObject::tr("Wrong selection"),
132
                                       QObject::tr("Select constraint(s) from the sketch."));
133
            return;
134
        }
135

136
        for (std::vector<std::string>::const_iterator it = SubNames.begin(); it != SubNames.end();
137
             ++it) {
138
            // see if we have constraints, if we do it is not a mode change, but a toggle.
139
            if (it->size() > 10 && it->substr(0, 10) == "Constraint") {
140
                modeChange = false;
141
            }
142
        }
143
    }
144

145
    if (modeChange) {
146
        Gui::Document* doc = getActiveGuiDocument();
147

148
        SketcherGui::ViewProviderSketch* vp =
149
            static_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());
150
        vp->setIsShownVirtualSpace(!vp->getIsShownVirtualSpace());
151
    }
152
    // toggle the selected constraint(s)
153
    else {
154
        // get the needed lists and objects
155
        const std::vector<std::string>& SubNames = selection[0].getSubNames();
156
        if (SubNames.empty()) {
157
            Gui::TranslatedUserWarning(getActiveGuiDocument(),
158
                                       QObject::tr("Wrong selection"),
159
                                       QObject::tr("Select constraint(s) from the sketch."));
160

161
            return;
162
        }
163

164
        SketcherGui::ViewProviderSketch* sketchgui =
165
            static_cast<SketcherGui::ViewProviderSketch*>(getActiveGuiDocument()->getInEdit());
166
        Sketcher::SketchObject* Obj = sketchgui->getSketchObject();
167

168
        // undo command open
169
        openCommand(QT_TRANSLATE_NOOP("Command", "Toggle constraints to the other virtual space"));
170

171
        int successful = SubNames.size();
172
        // go through the selected subelements
173
        for (std::vector<std::string>::const_iterator it = SubNames.begin(); it != SubNames.end();
174
             ++it) {
175
            // only handle constraints
176
            if (it->size() > 10 && it->substr(0, 10) == "Constraint") {
177
                int ConstrId = Sketcher::PropertyConstraintList::getIndexFromConstraintName(*it);
178
                Gui::Command::openCommand(
179
                    QT_TRANSLATE_NOOP("Command", "Update constraint's virtual space"));
180
                try {
181
                    Gui::cmdAppObjectArgs(Obj, "toggleVirtualSpace(%d)", ConstrId);
182
                }
183
                catch (const Base::Exception&) {
184
                    successful--;
185
                }
186
            }
187
        }
188

189
        if (successful > 0) {
190
            commitCommand();
191
        }
192
        else {
193
            abortCommand();
194
        }
195

196
        // recomputer and clear the selection (convenience)
197
        tryAutoRecompute(Obj);
198
        getSelection().clearSelection();
199
    }
200
}
201

202
bool CmdSketcherSwitchVirtualSpace::isActive()
203
{
204
    return isSketcherVirtualSpaceActive(getActiveGuiDocument(), false);
205
}
206

207
void CreateSketcherCommandsVirtualSpace()
208
{
209
    Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager();
210

211
    rcCmdMgr.addCommand(new CmdSketcherSwitchVirtualSpace());
212
}
213

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

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

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

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