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