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