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