FreeCAD

Форк
0
/
VisualInspection.cpp 
303 строки · 11.7 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2011 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 <cfloat>
26
#endif
27

28
#include <App/Document.h>
29
#include <App/DocumentObject.h>
30
#include <Gui/Application.h>
31
#include <Gui/Command.h>
32
#include <Gui/Document.h>
33
#include <Gui/MainWindow.h>
34
#include <Gui/PrefWidgets.h>
35
#include <Gui/ViewProvider.h>
36

37
#include "VisualInspection.h"
38
#include "ui_VisualInspection.h"
39

40

41
using namespace InspectionGui;
42

43
namespace InspectionGui
44
{
45
class SingleSelectionItem: public QTreeWidgetItem
46
{
47
public:
48
    explicit SingleSelectionItem(QTreeWidget* parent)
49
        : QTreeWidgetItem(parent)
50
        , _compItem(nullptr)
51
    {}
52

53
    explicit SingleSelectionItem(QTreeWidgetItem* parent)
54
        : QTreeWidgetItem(parent)
55
        , _compItem(nullptr)
56
    {}
57

58
    ~SingleSelectionItem() override = default;
59

60
    SingleSelectionItem* getCompetitiveItem() const
61
    {
62
        return _compItem;
63
    }
64

65
    void setCompetitiveItem(SingleSelectionItem* item)
66
    {
67
        _compItem = item;
68
    }
69

70
private:
71
    SingleSelectionItem* _compItem;
72
};
73
}  // namespace InspectionGui
74

75
/* TRANSLATOR InspectionGui::DlgVisualInspectionImp */
76

77
/**
78
 *  Constructs a VisualInspection as a child of 'parent', with the
79
 *  name 'name' and widget flags set to 'f'.
80
 */
81
VisualInspection::VisualInspection(QWidget* parent, Qt::WindowFlags fl)
82
    : QDialog(parent, fl)
83
    , ui(new Ui_VisualInspection)
84
{
85
    ui->setupUi(this);
86
    connect(ui->treeWidgetActual,
87
            &QTreeWidget::itemClicked,
88
            this,
89
            &VisualInspection::onActivateItem);
90
    connect(ui->treeWidgetNominal,
91
            &QTreeWidget::itemClicked,
92
            this,
93
            &VisualInspection::onActivateItem);
94
    connect(ui->buttonBox,
95
            &QDialogButtonBox::helpRequested,
96
            Gui::getMainWindow(),
97
            &Gui::MainWindow::whatsThis);
98

99
    // FIXME: Not used yet
100
    ui->textLabel2->hide();
101
    ui->thickness->hide();
102
    ui->searchRadius->setUnit(Base::Unit::Length);
103
    ui->searchRadius->setRange(0, DBL_MAX);
104
    ui->thickness->setUnit(Base::Unit::Length);
105
    ui->thickness->setRange(0, DBL_MAX);
106

107
    App::Document* doc = App::GetApplication().getActiveDocument();
108
    // disable Ok button and enable of at least one item in each view is on
109
    buttonOk = ui->buttonBox->button(QDialogButtonBox::Ok);
110
    buttonOk->setDisabled(true);
111

112
    if (!doc) {
113
        ui->treeWidgetActual->setDisabled(true);
114
        ui->treeWidgetNominal->setDisabled(true);
115
        return;
116
    }
117

118
    Gui::Document* gui = Gui::Application::Instance->getDocument(doc);
119

120
    std::vector<App::DocumentObject*> obj = doc->getObjects();
121
    Base::Type point = Base::Type::fromName("Points::Feature");
122
    Base::Type mesh = Base::Type::fromName("Mesh::Feature");
123
    Base::Type shape = Base::Type::fromName("Part::Feature");
124
    for (auto it : obj) {
125
        if (it->getTypeId().isDerivedFrom(point) || it->getTypeId().isDerivedFrom(mesh)
126
            || it->getTypeId().isDerivedFrom(shape)) {
127
            Gui::ViewProvider* view = gui->getViewProvider(it);
128
            QIcon px = view->getIcon();
129
            SingleSelectionItem* item1 = new SingleSelectionItem(ui->treeWidgetActual);
130
            item1->setText(0, QString::fromUtf8(it->Label.getValue()));
131
            item1->setData(0, Qt::UserRole, QString::fromLatin1(it->getNameInDocument()));
132
            item1->setCheckState(0, Qt::Unchecked);
133
            item1->setIcon(0, px);
134

135
            SingleSelectionItem* item2 = new SingleSelectionItem(ui->treeWidgetNominal);
136
            item2->setText(0, QString::fromUtf8(it->Label.getValue()));
137
            item2->setData(0, Qt::UserRole, QString::fromLatin1(it->getNameInDocument()));
138
            item2->setCheckState(0, Qt::Unchecked);
139
            item2->setIcon(0, px);
140

141
            item1->setCompetitiveItem(item2);
142
            item2->setCompetitiveItem(item1);
143
        }
144
    }
145

146
    loadSettings();
147
}
148

149
/*
150
 *  Destroys the object and frees any allocated resources
151
 */
152
VisualInspection::~VisualInspection()
153
{
154
    // no need to delete child widgets, Qt does it all for us
155
    delete ui;
156
}
157

158
void VisualInspection::loadSettings()
159
{
160
    ParameterGrp::handle handle = App::GetApplication().GetParameterGroupByPath(
161
        "User parameter:BaseApp/Preferences/Mod/Inspection/Inspection");
162

163
    double searchDistance = ui->searchRadius->value().getValue();
164
    searchDistance = handle->GetFloat("SearchDistance", searchDistance);
165
    ui->searchRadius->setValue(searchDistance);
166

167
    double thickness = ui->thickness->value().getValue();
168
    thickness = handle->GetFloat("Thickness", thickness);
169
    ui->thickness->setValue(thickness);
170
}
171

172
void VisualInspection::saveSettings()
173
{
174
    ParameterGrp::handle handle = App::GetApplication().GetParameterGroupByPath(
175
        "User parameter:BaseApp/Preferences/Mod/Inspection/Inspection");
176
    double searchDistance = ui->searchRadius->value().getValue();
177
    handle->SetFloat("SearchDistance", searchDistance);
178

179
    double thickness = ui->thickness->value().getValue();
180
    handle->SetFloat("Thickness", thickness);
181
}
182

183
void VisualInspection::onActivateItem(QTreeWidgetItem* item)
184
{
185
    if (item) {
186
        SingleSelectionItem* sel = static_cast<SingleSelectionItem*>(item);
187
        SingleSelectionItem* cmp = sel->getCompetitiveItem();
188
        if (cmp && cmp->checkState(0) == Qt::Checked) {
189
            cmp->setCheckState(0, Qt::Unchecked);
190
        }
191
    }
192

193
    bool ok = false;
194
    for (QTreeWidgetItemIterator it(ui->treeWidgetActual); *it; ++it) {
195
        SingleSelectionItem* sel = (SingleSelectionItem*)*it;
196
        if (sel->checkState(0) == Qt::Checked) {
197
            ok = true;
198
            break;
199
        }
200
    }
201

202
    if (ok) {
203
        ok = false;
204
        for (QTreeWidgetItemIterator it(ui->treeWidgetNominal); *it; ++it) {
205
            SingleSelectionItem* sel = (SingleSelectionItem*)*it;
206
            if (sel->checkState(0) == Qt::Checked) {
207
                ok = true;
208
                break;
209
            }
210
        }
211
    }
212

213
    buttonOk->setEnabled(ok);
214
}
215

216
void VisualInspection::accept()
217
{
218
    onActivateItem(nullptr);
219
    if (buttonOk->isEnabled()) {
220
        QDialog::accept();
221
        saveSettings();
222

223
        // collect all nominal geometries
224
        QStringList nominalNames;
225
        for (QTreeWidgetItemIterator it(ui->treeWidgetNominal); *it; it++) {
226
            SingleSelectionItem* sel = (SingleSelectionItem*)*it;
227
            if (sel->checkState(0) == Qt::Checked) {
228
                nominalNames << sel->data(0, Qt::UserRole).toString();
229
            }
230
        }
231

232
        double searchRadius = ui->searchRadius->value().getValue();
233
        double thickness = ui->thickness->value().getValue();
234

235
        // open a new command
236
        Gui::Document* doc = Gui::Application::Instance->activeDocument();
237
        doc->openCommand(QT_TRANSLATE_NOOP("Command", "Visual Inspection"));
238

239
        // create a group
240
        Gui::Command::runCommand(Gui::Command::App,
241
                                 "App_activeDocument___InspectionGroup=App.ActiveDocument."
242
                                 "addObject(\"Inspection::Group\",\"Inspection\")");
243

244
        // for each actual geometry create an inspection feature
245
        for (QTreeWidgetItemIterator it(ui->treeWidgetActual); *it; it++) {
246
            SingleSelectionItem* sel = (SingleSelectionItem*)*it;
247
            if (sel->checkState(0) == Qt::Checked) {
248
                QString actualName = sel->data(0, Qt::UserRole).toString();
249
                Gui::Command::doCommand(Gui::Command::App,
250
                                        "App_activeDocument___InspectionGroup.newObject("
251
                                        "\"Inspection::Feature\",\"%s_Inspect\")",
252
                                        (const char*)actualName.toLatin1());
253
                Gui::Command::doCommand(
254
                    Gui::Command::App,
255
                    "App.ActiveDocument.ActiveObject.Actual=App.ActiveDocument.%s\n"
256
                    "App_activeDocument___activeObject___Nominals=list()\n"
257
                    "App.ActiveDocument.ActiveObject.SearchRadius=%.3f\n"
258
                    "App.ActiveDocument.ActiveObject.Thickness=%.3f\n",
259
                    (const char*)actualName.toLatin1(),
260
                    searchRadius,
261
                    thickness);
262
                for (const auto& it : nominalNames) {
263
                    Gui::Command::doCommand(Gui::Command::App,
264
                                            "App_activeDocument___activeObject___Nominals.append("
265
                                            "App.ActiveDocument.%s)\n",
266
                                            (const char*)it.toLatin1());
267
                }
268
                Gui::Command::doCommand(Gui::Command::App,
269
                                        "App.ActiveDocument.ActiveObject.Nominals=App_"
270
                                        "activeDocument___activeObject___Nominals\n"
271
                                        "del App_activeDocument___activeObject___Nominals\n");
272
            }
273
        }
274

275
        Gui::Command::runCommand(Gui::Command::App, "del App_activeDocument___InspectionGroup\n");
276

277
        doc->commitCommand();
278
        doc->getDocument()->recompute();
279

280
        // hide the checked features
281
        for (QTreeWidgetItemIterator it(ui->treeWidgetActual); *it; it++) {
282
            SingleSelectionItem* sel = (SingleSelectionItem*)*it;
283
            if (sel->checkState(0) == Qt::Checked) {
284
                Gui::Command::doCommand(
285
                    Gui::Command::App,
286
                    "Gui.ActiveDocument.getObject(\"%s\").Visibility=False",
287
                    (const char*)sel->data(0, Qt::UserRole).toString().toLatin1());
288
            }
289
        }
290

291
        for (QTreeWidgetItemIterator it(ui->treeWidgetNominal); *it; it++) {
292
            SingleSelectionItem* sel = (SingleSelectionItem*)*it;
293
            if (sel->checkState(0) == Qt::Checked) {
294
                Gui::Command::doCommand(
295
                    Gui::Command::App,
296
                    "Gui.ActiveDocument.getObject(\"%s\").Visibility=False",
297
                    (const char*)sel->data(0, Qt::UserRole).toString().toLatin1());
298
            }
299
        }
300
    }
301
}
302

303
#include "moc_VisualInspection.cpp"
304

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

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

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

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