FreeCAD

Форк
0
/
TaskThickness.cpp 
327 строк · 10.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2012 Werner Mayer <wmayer[at]users.sourceforge.net>     *
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 <QMessageBox>
26
#endif
27

28
#include <App/Application.h>
29
#include <App/Document.h>
30
#include <App/DocumentObject.h>
31
#include <Gui/Application.h>
32
#include <Gui/BitmapFactory.h>
33
#include <Gui/Command.h>
34
#include <Gui/CommandT.h>
35
#include <Gui/Document.h>
36
#include <Gui/Selection.h>
37
#include <Gui/SelectionFilter.h>
38
#include <Gui/SelectionObject.h>
39
#include <Gui/ViewProvider.h>
40
#include <Mod/Part/App/PartFeatures.h>
41

42
#include "TaskThickness.h"
43
#include "ui_TaskOffset.h"
44

45

46
using namespace PartGui;
47

48
class ThicknessWidget::Private
49
{
50
public:
51
    Ui_TaskOffset ui{};
52
    QString text;
53
    std::string selection;
54
    Part::Thickness* thickness{nullptr};
55

56
    class FaceSelection : public Gui::SelectionFilterGate
57
    {
58
        const App::DocumentObject* object;
59
    public:
60
        explicit FaceSelection(const App::DocumentObject* obj)
61
            : Gui::SelectionFilterGate(nullPointer()), object(obj)
62
        {
63
        }
64
        bool allow(App::Document* /*pDoc*/, App::DocumentObject*pObj, const char*sSubName) override
65
        {
66
            if (pObj != this->object)
67
                return false;
68
            if (!sSubName || sSubName[0] == '\0')
69
                return false;
70
            std::string element(sSubName);
71
            return element.substr(0,4) == "Face";
72
        }
73
    };
74
};
75

76
/* TRANSLATOR PartGui::ThicknessWidget */
77

78
ThicknessWidget::ThicknessWidget(Part::Thickness* thickness, QWidget* parent)
79
  : d(new Private())
80
{
81
    Q_UNUSED(parent);
82
    Gui::Command::runCommand(Gui::Command::App, "from FreeCAD import Base");
83
    Gui::Command::runCommand(Gui::Command::App, "import Part");
84

85
    d->thickness = thickness;
86
    d->ui.setupUi(this);
87
    setupConnections();
88

89
    d->ui.labelOffset->setText(tr("Thickness"));
90
    d->ui.fillOffset->hide();
91

92
    QSignalBlocker blockOffset(d->ui.spinOffset);
93
    d->ui.spinOffset->setRange(-INT_MAX, INT_MAX);
94
    d->ui.spinOffset->setSingleStep(0.1);
95
    d->ui.spinOffset->setValue(d->thickness->Value.getValue());
96

97
    int mode = d->thickness->Mode.getValue();
98
    d->ui.modeType->setCurrentIndex(mode);
99

100
    int join = d->thickness->Join.getValue();
101
    d->ui.joinType->setCurrentIndex(join);
102

103
    QSignalBlocker blockIntSct(d->ui.intersection);
104
    bool intsct = d->thickness->Intersection.getValue();
105
    d->ui.intersection->setChecked(intsct);
106

107
    QSignalBlocker blockSelfInt(d->ui.selfIntersection);
108
    bool selfint = d->thickness->SelfIntersection.getValue();
109
    d->ui.selfIntersection->setChecked(selfint);
110

111
    d->ui.spinOffset->bind(d->thickness->Value);
112
}
113

114
ThicknessWidget::~ThicknessWidget()
115
{
116
    delete d;
117
    Gui::Selection().rmvSelectionGate();
118
}
119

120
void ThicknessWidget::setupConnections()
121
{
122
    // clang-format off
123
    connect(d->ui.spinOffset, qOverload<double>(&Gui::QuantitySpinBox::valueChanged),
124
            this, &ThicknessWidget::onSpinOffsetValueChanged);
125
    connect(d->ui.modeType, qOverload<int>(&QComboBox::activated),
126
            this, &ThicknessWidget::onModeTypeActivated);
127
    connect(d->ui.joinType, qOverload<int>(&QComboBox::activated),
128
            this, &ThicknessWidget::onJoinTypeActivated);
129
    connect(d->ui.intersection, &QCheckBox::toggled,
130
            this, &ThicknessWidget::onIntersectionToggled);
131
    connect(d->ui.selfIntersection, &QCheckBox::toggled,
132
            this, &ThicknessWidget::onSelfIntersectionToggled);
133
    connect(d->ui.facesButton, &QPushButton::toggled,
134
            this, &ThicknessWidget::onFacesButtonToggled);
135
    connect(d->ui.updateView, &QCheckBox::toggled,
136
            this, &ThicknessWidget::onUpdateViewToggled);
137
    // clang-format on
138
}
139

140
Part::Thickness* ThicknessWidget::getObject() const
141
{
142
    return d->thickness;
143
}
144

145
void ThicknessWidget::onSpinOffsetValueChanged(double val)
146
{
147
    d->thickness->Value.setValue(val);
148
    if (d->ui.updateView->isChecked())
149
        d->thickness->getDocument()->recomputeFeature(d->thickness);
150
}
151

152
void ThicknessWidget::onModeTypeActivated(int val)
153
{
154
    d->thickness->Mode.setValue(val);
155
    if (d->ui.updateView->isChecked())
156
        d->thickness->getDocument()->recomputeFeature(d->thickness);
157
}
158

159
void ThicknessWidget::onJoinTypeActivated(int val)
160
{
161
    d->thickness->Join.setValue((long)val);
162
    if (d->ui.updateView->isChecked())
163
        d->thickness->getDocument()->recomputeFeature(d->thickness);
164
}
165

166
void ThicknessWidget::onIntersectionToggled(bool on)
167
{
168
    d->thickness->Intersection.setValue(on);
169
    if (d->ui.updateView->isChecked())
170
        d->thickness->getDocument()->recomputeFeature(d->thickness);
171
}
172

173
void ThicknessWidget::onSelfIntersectionToggled(bool on)
174
{
175
    d->thickness->SelfIntersection.setValue(on);
176
    if (d->ui.updateView->isChecked())
177
        d->thickness->getDocument()->recomputeFeature(d->thickness);
178
}
179

180
void ThicknessWidget::onFacesButtonToggled(bool on)
181
{
182
    if (on) {
183
        QList<QWidget*> c = this->findChildren<QWidget*>();
184
        for (auto it : c)
185
            it->setEnabled(false);
186
        d->ui.facesButton->setEnabled(true);
187
        d->ui.labelFaces->setText(tr("Select faces of the source object and press 'Done'"));
188
        d->ui.labelFaces->setEnabled(true);
189
        d->text = d->ui.facesButton->text();
190
        d->ui.facesButton->setText(tr("Done"));
191

192
        Gui::Application::Instance->showViewProvider(d->thickness->Faces.getValue());
193
        Gui::Application::Instance->hideViewProvider(d->thickness);
194
        Gui::Selection().clearSelection();
195
        Gui::Selection().addSelectionGate(new Private::FaceSelection(d->thickness->Faces.getValue()));
196
    }
197
    else {
198
        QList<QWidget*> c = this->findChildren<QWidget*>();
199
        for (auto it : c)
200
            it->setEnabled(true);
201
        d->ui.facesButton->setText(d->text);
202
        d->ui.labelFaces->clear();
203

204
        d->selection = Gui::Command::getPythonTuple
205
            (d->thickness->Faces.getValue()->getNameInDocument(), d->thickness->Faces.getSubValues());
206
        std::vector<Gui::SelectionObject> sel = Gui::Selection().getSelectionEx();
207
        for (auto & it : sel) {
208
            if (it.getObject() == d->thickness->Faces.getValue()) {
209
                d->thickness->Faces.setValue(it.getObject(), it.getSubNames());
210
                d->selection = it.getAsPropertyLinkSubString();
211
                break;
212
            }
213
        }
214

215
        Gui::Selection().rmvSelectionGate();
216
        Gui::Application::Instance->showViewProvider(d->thickness);
217
        Gui::Application::Instance->hideViewProvider(d->thickness->Faces.getValue());
218
        if (d->ui.updateView->isChecked())
219
            d->thickness->getDocument()->recomputeFeature(d->thickness);
220
    }
221
}
222

223
void ThicknessWidget::onUpdateViewToggled(bool on)
224
{
225
    if (on) {
226
        d->thickness->getDocument()->recomputeFeature(d->thickness);
227
    }
228
}
229

230
bool ThicknessWidget::accept()
231
{
232
    if (d->ui.facesButton->isChecked())
233
        return false;
234

235
    try {
236
        if (!d->selection.empty()) {
237
            Gui::cmdAppObjectArgs(d->thickness, "Faces = %s", d->selection.c_str());
238
        }
239
        Gui::cmdAppObjectArgs(d->thickness, "Value = %f", d->ui.spinOffset->value().getValue());
240
        Gui::cmdAppObjectArgs(d->thickness, "Mode = %d", d->ui.modeType->currentIndex());
241
        Gui::cmdAppObjectArgs(d->thickness, "Join = %d", d->ui.joinType->currentIndex());
242
        Gui::cmdAppObjectArgs(d->thickness, "Intersection = %s",
243
            d->ui.intersection->isChecked() ? "True" : "False");
244
        Gui::cmdAppObjectArgs(d->thickness, "SelfIntersection = %s",
245
            d->ui.selfIntersection->isChecked() ? "True" : "False");
246

247
        Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.recompute()");
248
        if (!d->thickness->isValid())
249
            throw Base::CADKernelError(d->thickness->getStatusString());
250
        Gui::Command::doCommand(Gui::Command::Gui,"Gui.ActiveDocument.resetEdit()");
251
        Gui::Command::commitCommand();
252
    }
253
    catch (const Base::Exception& e) {
254
        QMessageBox::warning(
255
            this, tr("Input error"), QCoreApplication::translate("Exception", e.what()));
256
        return false;
257
    }
258

259
    return true;
260
}
261

262
bool ThicknessWidget::reject()
263
{
264
    if (d->ui.facesButton->isChecked())
265
        return false;
266

267
    // save this and check if the object is still there after the
268
    // transaction is aborted
269
    std::string objname = d->thickness->getNameInDocument();
270
    App::DocumentObject* source = d->thickness->Faces.getValue();
271

272
    // roll back the done things
273
    Gui::Command::abortCommand();
274
    Gui::Command::doCommand(Gui::Command::Gui,"Gui.ActiveDocument.resetEdit()");
275
    Gui::Command::updateActive();
276

277
    // Thickness object was deleted
278
    if (source && !source->getDocument()->getObject(objname.c_str())) {
279
        Gui::Application::Instance->getViewProvider(source)->show();
280
    }
281

282
    return true;
283
}
284

285
void ThicknessWidget::changeEvent(QEvent *e)
286
{
287
    QWidget::changeEvent(e);
288
    if (e->type() == QEvent::LanguageChange) {
289
        d->ui.retranslateUi(this);
290
        d->ui.labelOffset->setText(tr("Thickness"));
291
    }
292
}
293

294

295
/* TRANSLATOR PartGui::TaskThickness */
296

297
TaskThickness::TaskThickness(Part::Thickness* offset)
298
{
299
    widget = new ThicknessWidget(offset);
300
    widget->setWindowTitle(ThicknessWidget::tr("Thickness"));
301
    addTaskBox(Gui::BitmapFactory().pixmap("Part_Thickness"), widget);
302
}
303

304
Part::Thickness* TaskThickness::getObject() const
305
{
306
    return widget->getObject();
307
}
308

309
void TaskThickness::open()
310
{
311
}
312

313
void TaskThickness::clicked(int)
314
{
315
}
316

317
bool TaskThickness::accept()
318
{
319
    return widget->accept();
320
}
321

322
bool TaskThickness::reject()
323
{
324
    return widget->reject();
325
}
326

327
#include "moc_TaskThickness.cpp"
328

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

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

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

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