Salome HOME
Sketch selection in the browser for extrusion operation
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "PartSet_Module.h"
4 #include <PartSet_WidgetSketchLabel.h>
5 #include <PartSet_Validators.h>
6 #include <PartSet_Tools.h>
7 #include <PartSet_WidgetPoint2d.h>
8 #include <PartSet_WidgetPoint2dDistance.h>
9 #include <PartSet_WidgetShapeSelector.h>
10 #include <PartSet_WidgetConstraintShapeSelector.h>
11 #include <PartSet_SketcherMgr.h>
12
13 #include <ModuleBase_Operation.h>
14 #include <ModuleBase_IViewer.h>
15 #include <ModuleBase_IViewWindow.h>
16 #include <ModuleBase_IPropertyPanel.h>
17 #include <ModuleBase_WidgetEditor.h>
18 #include <ModuleBase_FilterFactory.h>
19 #include <ModuleBase_FilterLinearEdge.h>
20 #include <ModuleBase_FilterFace.h>
21 #include <ModuleBase_FilterMulti.h>
22 #include <ModuleBase_FilterCustom.h>
23 #include <ModuleBase_FilterNoConsructionSubShapes.h>
24 #include <PartSet_FilterSketchEntity.h>
25
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Events.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Data.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_ShapeValidator.h>
32
33 #include <GeomDataAPI_Point2D.h>
34 #include <GeomDataAPI_Point.h>
35 #include <GeomDataAPI_Dir.h>
36
37 #include <XGUI_Displayer.h>
38 #include <XGUI_Workshop.h>
39 #include <XGUI_OperationMgr.h>
40 #include <XGUI_PropertyPanel.h>
41 #include <XGUI_ModuleConnector.h>
42 #include <XGUI_ContextMenuMgr.h>
43 #include <XGUI_Tools.h>
44
45 #include <SketchPlugin_Feature.h>
46 #include <SketchPlugin_Sketch.h>
47 #include <SketchPlugin_Line.h>
48 //#include <SketchPlugin_Arc.h>
49 //#include <SketchPlugin_Circle.h>
50 #include <SketchPlugin_ConstraintLength.h>
51 #include <SketchPlugin_ConstraintDistance.h>
52 #include <SketchPlugin_ConstraintParallel.h>
53 #include <SketchPlugin_ConstraintPerpendicular.h>
54 #include <SketchPlugin_ConstraintRadius.h>
55 //#include <SketchPlugin_ConstraintRigid.h>
56
57 #include <Events_Loop.h>
58 #include <Config_PropManager.h>
59
60 #include <StdSelect_TypeOfFace.hxx>
61 #include <TopoDS_Vertex.hxx>
62 #include <TopoDS.hxx>
63 #include <TopoDS_Shape.hxx>
64 #include <BRep_Tool.hxx>
65
66 #include <QObject>
67 #include <QMouseEvent>
68 #include <QString>
69 #include <QTimer>
70 #include <QApplication>
71 #include <QMessageBox>
72 #include <QMainWindow>
73
74 #include <GeomAlgoAPI_FaceBuilder.h>
75 #include <GeomDataAPI_Dir.h>
76
77 #ifdef _DEBUG
78 #include <QDebug>
79 #endif
80
81 /*!Create and return new instance of XGUI_Module*/
82 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
83 {
84   return new PartSet_Module(theWshop);
85 }
86
87 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
88   : ModuleBase_IModule(theWshop), 
89   myRestartingMode(RM_None)
90 {
91   //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
92   mySketchMgr = new PartSet_SketcherMgr(this);
93
94   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
95   XGUI_Workshop* aWorkshop = aConnector->workshop();
96
97   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
98   connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
99   connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
100           this, SLOT(onOperationActivatedByPreselection()));
101
102   ModuleBase_IViewer* aViewer = theWshop->viewer();
103   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
104           this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
105
106   createActions();
107 }
108
109 PartSet_Module::~PartSet_Module()
110 {
111   if (!myDocumentShapeFilter.IsNull())
112     myDocumentShapeFilter.Nullify();
113 }
114
115 void PartSet_Module::registerValidators()
116 {
117   //Registering of validators
118   SessionPtr aMgr = ModelAPI_Session::get();
119   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
120   aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
121   aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
122   aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
123   aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
124   aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
125   aFactory->registerValidator("PartSet_RigidValidator", new PartSet_RigidValidator);
126   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
127   aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
128   aFactory->registerValidator("PartSet_SketchValidator", new PartSet_SketchValidator);
129 }
130
131 void PartSet_Module::registerFilters()
132 {
133   //Registering of selection filters
134   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
135   ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
136
137   aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge);
138   aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace);
139   aFactory->registerFilter("MultiFilter", new ModuleBase_FilterMulti);
140   Handle(SelectMgr_Filter) aSelectFilter = new ModuleBase_FilterNoConsructionSubShapes(workshop());
141   aFactory->registerFilter("NoConstructionSubShapesFilter",
142             new ModuleBase_FilterCustom(aSelectFilter));
143   aSelectFilter = new PartSet_FilterSketchEntity(workshop());
144   aFactory->registerFilter("SketchEntityFilter", new ModuleBase_FilterCustom(aSelectFilter));
145 }
146
147 void PartSet_Module::registerProperties()
148 {
149   Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
150                                    PLANE_SIZE);
151   Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
152                                    Config_Prop::Integer, SKETCH_WIDTH);
153 }
154
155 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation) 
156 {
157   if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
158     mySketchMgr->commitNestedSketch(theOperation);
159   }
160
161   if (theOperation->isEditOperation())
162     return;
163   // the selection is cleared after commit the create operation
164   // in order to do not use the same selected objects in the restarted operation
165   // for common behaviour, the selection is cleared even if the operation is not restarted
166   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
167   if (!aContext.IsNull())
168     aContext->ClearSelected();
169
170   /// Restart sketcher operations automatically
171   FeaturePtr aFeature = theOperation->feature();
172   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
173             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
174   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
175                      myRestartingMode == RM_EmptyFeatureUsed)) {
176     myLastOperationId = theOperation->id();
177     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
178     
179     launchOperation(myLastOperationId);
180   }
181   breakOperationSequence();
182 }
183
184 void PartSet_Module::breakOperationSequence()
185 {
186   myLastOperationId = "";
187   myLastFeature = FeaturePtr();
188   myRestartingMode = RM_None;
189 }
190
191 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
192 {
193   breakOperationSequence();
194 }
195
196 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
197 {
198   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
199     mySketchMgr->startSketch(theOperation);
200   }
201   else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
202     mySketchMgr->startNestedSketch(theOperation);
203   }
204
205   if (myDocumentShapeFilter.IsNull())
206     myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
207   myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
208 }
209
210 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
211 {
212   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
213     mySketchMgr->stopSketch(theOperation);
214   }
215   else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
216     mySketchMgr->stopNestedSketch(theOperation);
217   }
218   myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
219 }
220
221 ModuleBase_Operation* PartSet_Module::currentOperation() const
222 {
223   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
224   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
225   return anOpMgr->currentOperation();
226 }
227
228 bool PartSet_Module::canUndo() const
229 {
230   bool aCanUndo = false;
231   SessionPtr aMgr = ModelAPI_Session::get();
232   if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
233     aCanUndo = !aMgr->isOperation();
234     if (!aCanUndo) // check the enable state additionally by sketch manager
235       aCanUndo = aMgr->canUndo();
236   }
237   return aCanUndo;
238 }
239
240 bool PartSet_Module::canRedo() const
241 {
242   bool aCanRedo = false;
243   SessionPtr aMgr = ModelAPI_Session::get();
244   if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
245     aCanRedo = !aMgr->isOperation();
246     if (!aCanRedo) // check the enable state additionally by sketch manager
247       aCanRedo = aMgr->canRedo();
248   }
249   return aCanRedo;
250 }
251
252 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
253 {
254   bool aCanDisplay = false;
255   if (!mySketchMgr->canDisplayObject())
256     return aCanDisplay;
257   CompositeFeaturePtr aSketchFeature = mySketchMgr->activeSketch();
258   if (aSketchFeature.get() != NULL) {
259     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
260
261     // MPV: the second and third conditions to avoid crash on exit for application
262     if (aFeature.get() != NULL && aFeature->data().get() && aFeature->data()->isValid()) {
263       if (aFeature == aSketchFeature) {
264         aCanDisplay = false;
265       }
266       else if (aSketchFeature.get() && aSketchFeature->data().get() &&
267                aSketchFeature->data()->isValid()) {
268         for (int i = 0; i < aSketchFeature->numberOfSubs() && !aCanDisplay; i++) {
269           FeaturePtr aSubFeature = aSketchFeature->subFeature(i);
270           std::list<ResultPtr> aResults = aSubFeature->results();
271           std::list<ResultPtr>::const_iterator aIt;
272           for (aIt = aResults.begin(); aIt != aResults.end() && !aCanDisplay; ++aIt) {
273             if (theObject == (*aIt))
274               aCanDisplay = true;
275           }
276           if (aSubFeature == theObject)
277             aCanDisplay = true;
278         }
279       }
280     }
281   }
282   else {
283     aCanDisplay = ModuleBase_IModule::canDisplayObject(theObject);
284   }
285   return aCanDisplay;
286 }
287
288 void PartSet_Module::addViewerItems(QMenu* theMenu) const
289 {
290   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
291   if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
292       !PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
293     return;
294
295   ModuleBase_ISelection* aSelection = myWorkshop->selection();
296   QObjectPtrList aObjects = aSelection->selectedPresentations();
297   if (aObjects.size() > 0) {
298     bool hasFeature = false;
299     foreach(ObjectPtr aObject, aObjects)
300     {
301       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
302       if (aFeature.get() != NULL) {
303         hasFeature = true;
304       }
305     }
306     if (hasFeature) {
307       XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
308       XGUI_Workshop* aWorkshop = aConnector->workshop();
309       QAction* anAction = aWorkshop->contextMenuMgr()->action("DELETE_CMD");
310       theMenu->addAction(anAction);
311     }
312   }
313   bool isAuxiliary;
314   if (mySketchMgr->canSetAuxiliary(isAuxiliary)) {
315     QAction* anAction = action("AUXILIARY_CMD");
316     theMenu->addAction(anAction);
317     anAction->setChecked(isAuxiliary);
318   }
319 }
320
321 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
322 {
323   ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
324   if (PartSet_SketcherMgr::isSketchOperation(theOperation) &&  (theOperation->isEditOperation())) {
325     // we have to manually activate the sketch label in edit mode
326       aPanel->activateWidget(aPanel->modelWidgets().first());
327       return;
328   }
329
330   // Restart last operation type 
331   if ((theOperation->id() == myLastOperationId) && myLastFeature) {
332     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
333     if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
334       // Initialise new line with first point equal to end of previous
335       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
336       if (aPnt2dWgt) {
337         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
338         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
339           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
340         if (aPoint) {
341           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
342           PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(), 
343             aWgt->attributeID(), aPoint->x(), aPoint->y());
344           aPanel->activateNextWidget(aPnt2dWgt);
345         }
346       }
347     }
348   } else {
349     // Start editing constraint
350     if (theOperation->isEditOperation()) {
351       // TODO: #391 - to be removed
352       std::string aId = theOperation->id().toStdString();
353       if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation) &&
354           PartSet_SketcherMgr::isDistanceOperation(theOperation)) {
355         // Find and activate widget for management of point for dimension line position
356         QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
357         foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
358           PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
359           if (aPntWgt) {
360             aPanel->activateWidget(aPntWgt);
361             return;
362           }
363         }
364       }
365     }
366   }
367 }
368
369
370 void PartSet_Module::onSelectionChanged()
371 {
372   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
373   if (!aOperation)
374     return;
375
376   bool isSketcherOp = false;
377   // An edit operation is enable only if the current opeation is the sketch operation
378   if (mySketchMgr->activeSketch()) {
379     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
380       isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
381   }
382   if (isSketcherOp) {
383     // Editing of constraints can be done on selection
384     ModuleBase_ISelection* aSelect = myWorkshop->selection();
385     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
386     if (aSelected.size() == 1) {
387       ModuleBase_ViewerPrs aPrs = aSelected.first();
388       ObjectPtr aObject = aPrs.object();
389       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
390       if (aFeature) {
391         std::string aId = aFeature->getKind();
392         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
393             (aId == SketchPlugin_ConstraintLength::ID()) || 
394             (aId == SketchPlugin_ConstraintDistance::ID())) {
395           editFeature(aFeature);
396         }
397       }
398     }
399   } 
400 }
401
402 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
403 {
404   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
405   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
406   anOpMgr->onKeyReleased(theEvent);
407 }
408
409 void PartSet_Module::onEnterReleased()
410 {
411   myRestartingMode = RM_EmptyFeatureUsed;
412 }
413
414 void PartSet_Module::onOperationActivatedByPreselection()
415 {
416   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
417   if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
418     // Set final definitions if they are necessary
419     //propertyPanelDefined(aOperation);
420
421     /// Commit sketcher operations automatically
422     anOperation->commit();
423   }
424 }
425
426 void PartSet_Module::onNoMoreWidgets()
427 {
428   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
429   if (anOperation) {
430     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
431       if (myRestartingMode != RM_Forbided)
432         myRestartingMode = RM_LastFeatureUsed;
433       anOperation->commit();
434     }
435   }
436 }
437
438 void PartSet_Module::onVertexSelected()
439 {
440   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
441   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
442     /// If last line finished on vertex the lines creation sequence has to be break
443     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
444     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
445     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
446     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
447     bool aFoundWidget = false;
448     bool aFoundObligatory = false;
449     for (; anIt != aLast && !aFoundObligatory; anIt++) {
450       if (!aFoundWidget)
451         aFoundWidget = *anIt == anActiveWidget;
452       else
453         aFoundObligatory = (*anIt)->isObligatory();
454     }
455     if (!aFoundObligatory)
456       myRestartingMode = RM_Forbided;
457   }
458 }
459
460 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
461                                             Config_WidgetAPI* theWidgetApi, std::string theParentId)
462 {
463   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
464   XGUI_Workshop* aWorkshop = aConnector->workshop();
465   ModuleBase_ModelWidget* aWgt = NULL;
466   if (theType == "sketch-start-label") {
467     PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
468     aLabelWgt->setWorkshop(aWorkshop);
469     connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
470       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
471     aWgt = aLabelWgt;
472   } else if (theType == "sketch-2dpoint_selector") {
473     PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
474     aPointWgt->setWorkshop(aWorkshop);
475     aPointWgt->setSketch(mySketchMgr->activeSketch());
476     connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
477     aWgt = aPointWgt;
478   } if (theType == "point2ddistance") {
479     PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
480     aDistanceWgt->setWorkshop(aWorkshop);
481     aDistanceWgt->setSketch(mySketchMgr->activeSketch());
482     aWgt = aDistanceWgt;
483   } if (theType == "sketch_shape_selector") {
484     PartSet_WidgetShapeSelector* aShapeSelectorWgt =
485       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
486     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
487     aWgt = aShapeSelectorWgt;
488   } if (theType == "sketch_constraint_shape_selector") {
489     PartSet_WidgetConstraintShapeSelector* aConstraintShapeSelectorWgt =
490       new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
491     aConstraintShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
492     aWgt = aConstraintShapeSelectorWgt;
493   }
494   return aWgt;
495 }
496
497 void PartSet_Module::createActions()
498 {
499   QAction* anAction;
500
501   anAction = new QAction(tr("Auxiliary"), this);
502   anAction->setCheckable(true);
503   addAction("AUXILIARY_CMD", anAction);
504 }
505
506 QAction* PartSet_Module::action(const QString& theId) const
507 {
508   if (myActions.contains(theId))
509     return myActions[theId];
510   return 0;
511 }
512
513 void PartSet_Module::addAction(const QString& theId, QAction* theAction)
514 {
515   if (myActions.contains(theId))
516     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
517   theAction->setData(theId);
518   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
519   myActions[theId] = theAction;
520 }
521
522 void PartSet_Module::onAction(bool isChecked)
523 {
524   QAction* aAction = static_cast<QAction*>(sender());
525   QString anId = aAction->data().toString();
526
527   if (anId == "AUXILIARY_CMD") {
528     mySketchMgr->setAuxiliary(isChecked);
529   }
530 }
531
532 bool PartSet_Module::deleteObjects()
533 {
534   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
535   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
536        isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
537   if (!isSketchOp && !isNestedOp)
538     return false;
539
540   // sketch feature should be skipped, only sub-features can be removed
541   // when sketch operation is active
542   CompositeFeaturePtr aSketch = mySketchMgr->activeSketch();
543
544   // selected objects should be collected before the current operation abort because
545   // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
546   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
547   XGUI_Workshop* aWorkshop = aConnector->workshop();
548   ModuleBase_ISelection* aSel = aConnector->selection();
549   QObjectPtrList aSelectedObj = aSel->selectedPresentations();
550
551   // if there are no selected objects in the viewer, that means that the selection in another
552   // place cased this method. It is necessary to return the false value to understande in above
553   // method that delete is not processed
554   if (aSelectedObj.count() == 0)
555     return false;
556
557   if (isNestedOp)
558     anOperation->abort();
559
560   // the active nested sketch operation should be aborted unconditionally
561   if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
562     anOperation->abort();
563
564   std::set<FeaturePtr> aRefFeatures;
565   foreach (ObjectPtr aObj, aSelectedObj)
566   {
567     //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
568     //if (aPart) {
569       // TODO: check for what there is this condition. It is placed here historicaly because
570       // ther is this condition during remove features.
571     //} else {
572     FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
573     if (aFeature.get() != NULL) {
574       aObj->document()->refsToFeature(aFeature, aRefFeatures, false);
575     }
576     //}
577   }
578
579   QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
580   /**
581   // according to #355 feature, it is not necessary to inform about dependencies during
582   // sketch delete operation
583   // 
584   if (!aRefFeatures.empty()) {
585     QStringList aRefNames;
586     std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
587                                          aLast = aRefFeatures.end();
588     for (; anIt != aLast; anIt++) {
589       FeaturePtr aFeature = (*anIt);
590       if (aFeature == aSketch)
591         continue;
592       aRefNames.append((*anIt)->name().c_str());
593     }
594     if (!aRefNames.empty()) {
595       QString aNames = aRefNames.join(", ");
596       aDescription += aNames.prepend(" ");
597
598       QMainWindow* aDesktop = aWorkshop->desktop();
599       QMessageBox::StandardButton aRes = QMessageBox::warning(
600           aDesktop, tr("Delete features"),
601           QString(tr("Selected features are used in the following features: %1.\
602   These features will be deleted also. Would you like to continue?")).arg(aNames),
603           QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
604       if (aRes != QMessageBox::Yes)
605         return;
606     }
607   }*/
608
609   SessionPtr aMgr = ModelAPI_Session::get();
610   aMgr->startOperation(aDescription.toStdString());
611   std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
612                                        aLast = aRefFeatures.end();
613   for (; anIt != aLast; anIt++) {
614     FeaturePtr aRefFeature = (*anIt);
615     if (aRefFeature == aSketch)
616       continue;
617     aRefFeature->document()->removeFeature(aRefFeature);
618   }
619
620   foreach (ObjectPtr aObj, aSelectedObj)
621   {
622     DocumentPtr aDoc = aObj->document();
623     //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
624     //if (aPart) {
625     //  if (aDoc == aMgr->activeDocument()) {
626     //    aDoc->close();
627     //  }
628     //} else {
629       //FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
630     FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
631     if (aFeature.get() != NULL) {
632       aDoc->removeFeature(aFeature);
633     }
634     //}
635   }
636   aWorkshop->displayer()->updateViewer();
637   //myDisplayer->updateViewer();
638   aMgr->finishOperation();
639
640   return true;
641 }