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