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