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