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