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