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