-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
TaskFeaturePick.cpp
596 lines (497 loc) · 21.9 KB
/
TaskFeaturePick.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/******************************************************************************
* Copyright (c) 2012 Jan Rheinländer *
* <jrheinlaender@users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
******************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QListIterator>
# include <QTimer>
# include <QListWidgetItem>
#endif
#include <Gui/Application.h>
#include <Gui/BitmapFactory.h>
#include <Gui/MainWindow.h>
#include <Gui/Document.h>
#include <Gui/Control.h>
#include <Gui/ViewProviderOrigin.h>
#include <App/Document.h>
#include <App/Origin.h>
#include <App/OriginFeature.h>
#include <App/Part.h>
#include <Base/Tools.h>
#include <Base/Reader.h>
#include <Base/Console.h>
#include <Mod/PartDesign/App/Body.h>
#include <Mod/Sketcher/App/SketchObject.h>
#include "Utils.h"
#include "ui_TaskFeaturePick.h"
#include "TaskFeaturePick.h"
#include <Mod/PartDesign/App/ShapeBinder.h>
#include <Mod/PartDesign/App/DatumPoint.h>
#include <Mod/PartDesign/App/DatumLine.h>
#include <Mod/PartDesign/App/DatumPlane.h>
#include <Mod/PartDesign/App/FeaturePrimitive.h>
#include <Mod/Part/App/DatumFeature.h>
using namespace PartDesignGui;
using namespace Attacher;
// TODO Do ve should snap here to App:Part or GeoFeatureGroup/DocumentObjectGroup ? (2015-09-04, Fat-Zer)
const QString TaskFeaturePick::getFeatureStatusString(const featureStatus st)
{
switch (st) {
case validFeature: return tr("Valid");
case invalidShape: return tr("Invalid shape");
case noWire: return tr("No wire in sketch");
case isUsed: return tr("Sketch already used by other feature");
case otherBody: return tr("Belongs to another body");
case otherPart: return tr("Belongs to another part");
case notInBody: return tr("Doesn't belong to any body");
case basePlane: return tr("Base plane");
case afterTip: return tr("Feature is located after the tip feature");
}
return QString();
}
TaskFeaturePick::TaskFeaturePick(std::vector<App::DocumentObject*>& objects,
const std::vector<featureStatus>& status,
bool singleFeatureSelect,
QWidget* parent)
: TaskBox(Gui::BitmapFactory().pixmap("edit-select-box"),
tr("Select feature"), true, parent)
, ui(new Ui_TaskFeaturePick)
, doSelection(false)
{
proxy = new QWidget(this);
ui->setupUi(proxy);
connect(ui->checkUsed, SIGNAL(toggled(bool)), this, SLOT(onUpdate(bool)));
connect(ui->checkOtherBody, SIGNAL(toggled(bool)), this, SLOT(onUpdate(bool)));
connect(ui->checkOtherPart, SIGNAL(toggled(bool)), this, SLOT(onUpdate(bool)));
connect(ui->radioIndependent, SIGNAL(toggled(bool)), this, SLOT(onUpdate(bool)));
connect(ui->radioDependent, SIGNAL(toggled(bool)), this, SLOT(onUpdate(bool)));
connect(ui->radioXRef, SIGNAL(toggled(bool)), this, SLOT(onUpdate(bool)));
connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onItemSelectionChanged()));
connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(onDoubleClick(QListWidgetItem *)));
if (!singleFeatureSelect) {
ui->listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
}
enum { axisBit=0, planeBit = 1};
// NOTE: generally there shouldn't be more then one origin
std::map <App::Origin*, std::bitset<2> > originVisStatus;
auto statusIt = status.cbegin();
auto objIt = objects.begin();
assert(status.size() == objects.size());
bool attached = false;
for (; statusIt != status.end(); ++statusIt, ++objIt) {
QListWidgetItem* item = new QListWidgetItem(
QString::fromLatin1("%1 (%2)")
.arg(QString::fromUtf8((*objIt)->Label.getValue()))
.arg(getFeatureStatusString(*statusIt)
)
);
item->setData(Qt::UserRole, QString::fromLatin1((*objIt)->getNameInDocument()));
ui->listWidget->addItem(item);
App::Document* pDoc = (*objIt)->getDocument();
documentName = pDoc->getName();
if (!attached) {
attached = true;
attachDocument(Gui::Application::Instance->getDocument(pDoc));
}
//check if we need to set any origin in temporary visibility mode
if (*statusIt != invalidShape && (*objIt)->isDerivedFrom ( App::OriginFeature::getClassTypeId () )) {
App::Origin *origin = static_cast<App::OriginFeature*> (*objIt)->getOrigin ();
if (origin) {
if ((*objIt)->isDerivedFrom (App::Plane::getClassTypeId())) {
originVisStatus[ origin ].set (planeBit, true);
}
else if ( (*objIt)->isDerivedFrom (App::Line::getClassTypeId())) {
originVisStatus[ origin ].set (axisBit, true);
}
}
}
}
// Setup the origin's temporary visibility
for (const auto & originPair: originVisStatus) {
const auto &origin = originPair.first;
Gui::ViewProviderOrigin* vpo = static_cast<Gui::ViewProviderOrigin*> (
Gui::Application::Instance->getViewProvider ( origin ) );
if (vpo) {
vpo->setTemporaryVisibility( originVisStatus[origin][axisBit],
originVisStatus[origin][planeBit]);
origins.push_back(vpo);
}
}
// TODO may be update origin API to show only some objects (2015-08-31, Fat-Zer)
groupLayout()->addWidget(proxy);
statuses = status;
updateList();
}
TaskFeaturePick::~TaskFeaturePick()
{
for(Gui::ViewProviderOrigin* vpo : origins)
vpo->resetTemporaryVisibility();
}
void TaskFeaturePick::updateList()
{
int index = 0;
for (std::vector<featureStatus>::const_iterator st = statuses.begin(); st != statuses.end(); st++) {
QListWidgetItem* item = ui->listWidget->item(index);
switch (*st) {
case validFeature: item->setHidden(false); break;
case invalidShape: item->setHidden(true); break;
case isUsed: item->setHidden(!ui->checkUsed->isChecked()); break;
case noWire: item->setHidden(true); break;
case otherBody: item->setHidden(!ui->checkOtherBody->isChecked()); break;
case otherPart: item->setHidden(!ui->checkOtherPart->isChecked()); break;
case notInBody: item->setHidden(!ui->checkOtherPart->isChecked()); break;
case basePlane: item->setHidden(false); break;
case afterTip: item->setHidden(true); break;
}
index++;
}
}
void TaskFeaturePick::onUpdate(bool)
{
bool enable = false;
if (ui->checkOtherBody->isChecked() || ui->checkOtherPart->isChecked())
enable = true;
ui->radioDependent->setEnabled(enable);
ui->radioIndependent->setEnabled(enable);
ui->radioXRef->setEnabled(enable);
updateList();
}
std::vector<App::DocumentObject*> TaskFeaturePick::getFeatures()
{
features.clear();
QListIterator<QListWidgetItem*> i(ui->listWidget->selectedItems());
while (i.hasNext()) {
auto item = i.next();
if (item->isHidden())
continue;
QString t = item->data(Qt::UserRole).toString();
features.push_back(t);
}
std::vector<App::DocumentObject*> result;
for (std::vector<QString>::const_iterator s = features.begin(); s != features.end(); ++s)
result.push_back(App::GetApplication().getDocument(documentName.c_str())->getObject(s->toLatin1().data()));
return result;
}
std::vector<App::DocumentObject*> TaskFeaturePick::buildFeatures()
{
int index = 0;
std::vector<App::DocumentObject*> result;
try {
auto activeBody = PartDesignGui::getBody(false);
if (!activeBody)
return result;
auto activePart = PartDesignGui::getPartFor(activeBody, false);
for (std::vector<featureStatus>::const_iterator st = statuses.begin(); st != statuses.end(); st++) {
QListWidgetItem* item = ui->listWidget->item(index);
if (item->isSelected() && !item->isHidden()) {
QString t = item->data(Qt::UserRole).toString();
auto obj = App::GetApplication().getDocument(documentName.c_str())->getObject(t.toLatin1().data());
//build the dependent copy or reference if wanted by the user
if (*st == otherBody || *st == otherPart || *st == notInBody) {
if (!ui->radioXRef->isChecked()) {
auto copy = makeCopy(obj, "", ui->radioIndependent->isChecked());
if (*st == otherBody) {
activeBody->addObject(copy);
}
else if (*st == otherPart) {
auto oBody = PartDesignGui::getBodyFor(obj, false);
if (!oBody)
activePart->addObject(copy);
else
activeBody->addObject(copy);
}
else if (*st == notInBody) {
activeBody->addObject(copy);
// doesn't supposed to get here anything but sketch but to be on the safe side better to check
if (copy->getTypeId().isDerivedFrom(Sketcher::SketchObject::getClassTypeId())) {
Sketcher::SketchObject *sketch = static_cast<Sketcher::SketchObject*>(copy);
PartDesignGui::fixSketchSupport(sketch);
}
}
result.push_back(copy);
}
else {
result.push_back(obj);
}
}
else {
result.push_back(obj);
}
}
index++;
}
}
catch (const Base::Exception& e) {
e.ReportException();
}
catch (Py::Exception& e) {
// reported by code analyzers
e.clear();
Base::Console().Warning("Unexpected PyCXX exception\n");
}
catch (const boost::exception&) {
// reported by code analyzers
Base::Console().Warning("Unexpected boost exception\n");
}
return result;
}
App::DocumentObject* TaskFeaturePick::makeCopy(App::DocumentObject* obj, std::string sub, bool independent) {
App::DocumentObject* copy = nullptr;
// Check for null to avoid segfault
if (!obj)
return copy;
if( independent &&
(obj->isDerivedFrom(Sketcher::SketchObject::getClassTypeId()) ||
obj->isDerivedFrom(PartDesign::FeaturePrimitive::getClassTypeId()))) {
//we do know that the created instance is a document object, as obj is one. But we do not know which
//exact type
auto name = std::string("Copy") + std::string(obj->getNameInDocument());
copy = App::GetApplication().getActiveDocument()->addObject(obj->getTypeId().getName(), name.c_str());
//copy over all properties
std::vector<App::Property*> props;
std::vector<App::Property*> cprops;
obj->getPropertyList(props);
copy->getPropertyList(cprops);
auto it = cprops.begin();
for( App::Property* prop : props ) {
//independent copies don't have links and are not attached
if(independent && (
prop->getTypeId().isDerivedFrom(App::PropertyLink::getClassTypeId()) ||
prop->getTypeId().isDerivedFrom(App::PropertyLinkList::getClassTypeId()) ||
prop->getTypeId().isDerivedFrom(App::PropertyLinkSub::getClassTypeId()) ||
prop->getTypeId().isDerivedFrom(App::PropertyLinkSubList::getClassTypeId())||
( prop->getGroup() && strcmp(prop->getGroup(),"Attachment")==0) )) {
++it;
continue;
}
App::Property* cprop = *it++;
if( prop->getName() && strcmp(prop->getName(), "Label") == 0 ) {
static_cast<App::PropertyString*>(cprop)->setValue(name.c_str());
continue;
}
cprop->Paste(*prop);
//we are a independent copy, therefore no external geometry was copied. WE therefore can delete all
//constraints
if(obj->isDerivedFrom(Sketcher::SketchObject::getClassTypeId()))
static_cast<Sketcher::SketchObject*>(copy)->delConstraintsToExternal();
}
}
else {
std::string name;
if(!independent)
name = std::string("Reference");
else
name = std::string("Copy");
name += std::string(obj->getNameInDocument());
std::string entity;
if(!sub.empty())
entity = sub;
Part::PropertyPartShape* shapeProp = nullptr;
// TODO Replace it with commands (2015-09-11, Fat-Zer)
if(obj->isDerivedFrom(Part::Datum::getClassTypeId())) {
copy = App::GetApplication().getActiveDocument()->addObject(
obj->getTypeId().getName(), name.c_str() );
assert(copy->isDerivedFrom(Part::Datum::getClassTypeId()));
//we need to reference the individual datums and make again datums. This is important as
//datum adjust their size dependent on the part size, hence simply copying the shape is
//not enough
long int mode = mmDeactivated;
Part::Datum *datumCopy = static_cast<Part::Datum*>(copy);
if(obj->getTypeId() == PartDesign::Point::getClassTypeId()) {
mode = mm0Vertex;
}
else if(obj->getTypeId() == PartDesign::Line::getClassTypeId()) {
mode = mm1TwoPoints;
}
else if(obj->getTypeId() == PartDesign::Plane::getClassTypeId()) {
mode = mmFlatFace;
}
else
return copy;
// TODO Recheck this. This looks strange in case of independent copy (2015-10-31, Fat-Zer)
if(!independent) {
datumCopy->Support.setValue(obj, entity.c_str());
datumCopy->MapMode.setValue(mode);
}
else if(!entity.empty()) {
datumCopy->Shape.setValue(static_cast<Part::Datum*>(obj)->Shape.getShape().getSubShape(entity.c_str()));
} else {
datumCopy->Shape.setValue(static_cast<Part::Datum*>(obj)->Shape.getValue());
}
}
else if(obj->getTypeId() == PartDesign::ShapeBinder::getClassTypeId() ||
obj->isDerivedFrom(Part::Feature::getClassTypeId())) {
copy = App::GetApplication().getActiveDocument()->addObject("PartDesign::ShapeBinder", name.c_str());
if(!independent)
static_cast<PartDesign::ShapeBinder*>(copy)->Support.setValue(obj, entity.c_str());
else
shapeProp = &static_cast<PartDesign::ShapeBinder*>(copy)->Shape;
}
else if(obj->isDerivedFrom(App::Plane::getClassTypeId()) ||
obj->isDerivedFrom(App::Line::getClassTypeId())) {
copy = App::GetApplication().getActiveDocument()->addObject("PartDesign::ShapeBinder", name.c_str());
if (!independent) {
static_cast<PartDesign::ShapeBinder*>(copy)->Support.setValue(obj, entity.c_str());
}
else {
App::GeoFeature* geo = static_cast<App::GeoFeature*>(obj);
std::vector<std::string> subvalues;
subvalues.push_back(entity);
Part::TopoShape shape = PartDesign::ShapeBinder::buildShapeFromReferences(geo, subvalues);
static_cast<PartDesign::ShapeBinder*>(copy)->Shape.setValue(shape);
}
}
if (independent && shapeProp) {
if (entity.empty())
shapeProp->setValue(static_cast<Part::Feature*>(obj)->Shape.getValue());
else
shapeProp->setValue(static_cast<Part::Feature*>(obj)->Shape.getShape().getSubShape(entity.c_str()));
}
}
return copy;
}
void TaskFeaturePick::onSelectionChanged(const Gui::SelectionChanges& /*msg*/)
{
if (doSelection)
return;
doSelection = true;
ui->listWidget->clearSelection();
for (Gui::SelectionSingleton::SelObj obj : Gui::Selection().getSelection()) {
for (int row = 0; row < ui->listWidget->count(); row++) {
QListWidgetItem *item = ui->listWidget->item(row);
QString t = item->data(Qt::UserRole).toString();
if (t.compare(QString::fromLatin1(obj.FeatName))==0) {
item->setSelected(true);
}
}
}
doSelection = false;
}
void TaskFeaturePick::onItemSelectionChanged()
{
if (doSelection)
return;
doSelection = true;
ui->listWidget->blockSignals(true);
Gui::Selection().clearSelection();
for (int row = 0; row < ui->listWidget->count(); row++) {
QListWidgetItem *item = ui->listWidget->item(row);
QString t = item->data(Qt::UserRole).toString();
if (item->isSelected()) {
Gui::Selection().addSelection(documentName.c_str(), t.toLatin1());
}
}
ui->listWidget->blockSignals(false);
doSelection = false;
}
void TaskFeaturePick::onDoubleClick(QListWidgetItem *item)
{
if (doSelection)
return;
doSelection = true;
QString t = item->data(Qt::UserRole).toString();
Gui::Selection().addSelection(documentName.c_str(), t.toLatin1());
doSelection = false;
QMetaObject::invokeMethod(qobject_cast<Gui::ControlSingleton*>(&Gui::Control()), "accept", Qt::QueuedConnection);
}
void TaskFeaturePick::slotDeletedObject(const Gui::ViewProviderDocumentObject& Obj)
{
std::vector<Gui::ViewProviderOrigin*>::iterator it;
it = std::find(origins.begin(), origins.end(), &Obj);
if (it != origins.end()) {
origins.erase(it);
}
}
void TaskFeaturePick::slotUndoDocument(const Gui::Document&)
{
if (origins.empty()) {
QTimer::singleShot(100, &Gui::Control(), SLOT(closeDialog()));
}
}
void TaskFeaturePick::slotDeleteDocument(const Gui::Document&)
{
origins.clear();
QTimer::singleShot(100, &Gui::Control(), SLOT(closeDialog()));
}
void TaskFeaturePick::showExternal(bool val)
{
ui->checkOtherBody->setChecked(val);
ui->checkOtherPart->setChecked(val);
updateList();
}
//**************************************************************************
//**************************************************************************
// TaskDialog
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TaskDlgFeaturePick::TaskDlgFeaturePick( std::vector<App::DocumentObject*> &objects,
const std::vector<TaskFeaturePick::featureStatus> &status,
boost::function<bool (std::vector<App::DocumentObject*>)> afunc,
boost::function<void (std::vector<App::DocumentObject*>)> wfunc,
bool singleFeatureSelect,
boost::function<void (void)> abortfunc /* = NULL */ )
: TaskDialog(), accepted(false)
{
pick = new TaskFeaturePick(objects, status, singleFeatureSelect);
Content.push_back(pick);
acceptFunction = afunc;
workFunction = wfunc;
abortFunction = abortfunc;
}
TaskDlgFeaturePick::~TaskDlgFeaturePick()
{
//do the work now as before in accept() the dialog is still open, hence the work
//function could not open another dialog
if (accepted) {
try { workFunction(pick->buildFeatures()); } catch (...) {}
} else if (abortFunction) {
// Get rid of the TaskFeaturePick before the TaskDialog dtor does. The
// TaskFeaturePick holds pointers to things (ie any implicitly created
// Body objects) that might be modified/removed by abortFunction.
for (auto it : Content) {
delete it;
}
Content.clear();
try { abortFunction(); } catch (...) {}
}
}
//==== calls from the TaskView ===============================================================
void TaskDlgFeaturePick::open()
{
}
void TaskDlgFeaturePick::clicked(int)
{
}
bool TaskDlgFeaturePick::accept()
{
accepted = acceptFunction(pick->getFeatures());
return accepted;
}
bool TaskDlgFeaturePick::reject()
{
accepted = false;
return true;
}
void TaskDlgFeaturePick::showExternal(bool val)
{
pick->showExternal(val);
}
#include "moc_TaskFeaturePick.cpp"