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