]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_SketcherMgr.cpp
Salome HOME
f76e254c6b761baa74904b6bec71dfeaa33047e4
[modules/shaper.git] / src / PartSet / PartSet_SketcherMgr.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_SketcherMgr.h"
21
22 #include "PartSet_Filters.h"
23 #include "PartSet_SketcherReentrantMgr.h"
24 #include "PartSet_Module.h"
25 #include "PartSet_MouseProcessor.h"
26 #include "PartSet_Tools.h"
27 #include "PartSet_WidgetSketchLabel.h"
28 #include "PartSet_WidgetEditor.h"
29 #include "PartSet_ResultSketchPrs.h"
30 #include "PartSet_ExternalPointsMgr.h"
31 #include "PartSet_PreviewSketchPlane.h"
32
33 #include <XGUI_ModuleConnector.h>
34 #include <XGUI_Displayer.h>
35 #include <XGUI_Workshop.h>
36 #include <XGUI_ContextMenuMgr.h>
37 #include <XGUI_Selection.h>
38 #include <XGUI_SelectionActivate.h>
39 #include <XGUI_SelectionMgr.h>
40 #include <XGUI_ModuleConnector.h>
41 #include <XGUI_PropertyPanel.h>
42 #include <XGUI_ViewerProxy.h>
43 #include <XGUI_OperationMgr.h>
44 #include <XGUI_ErrorMgr.h>
45 #include <XGUI_Tools.h>
46
47 #include <ModuleBase_IPropertyPanel.h>
48 #include <ModuleBase_ISelection.h>
49 #include <ModuleBase_IViewer.h>
50 #include <ModuleBase_IWorkshop.h>
51 #include <ModuleBase_IViewWindow.h>
52 #include <ModuleBase_ModelWidget.h>
53 #include <ModuleBase_Operation.h>
54 #include <ModuleBase_OperationFeature.h>
55 #include <ModuleBase_Operation.h>
56 #include <ModuleBase_WidgetEditor.h>
57 #include <ModuleBase_ViewerPrs.h>
58 #include <ModuleBase_Tools.h>
59 #include <ModuleBase_ResultPrs.h>
60 #include <ModuleBase_ViewerFilters.h>
61
62 #include <GeomDataAPI_Point2D.h>
63
64 #include <GeomAPI_Shape.h>
65
66 #include <Events_Loop.h>
67
68 #include <SketchPlugin_Line.h>
69 #include <SketchPlugin_Sketch.h>
70 #include <SketchPlugin_Point.h>
71 #include <SketchPlugin_Arc.h>
72 #include <SketchPlugin_Circle.h>
73 #include <SketchPlugin_ConstraintLength.h>
74 #include <SketchPlugin_ConstraintDistance.h>
75 #include <SketchPlugin_ConstraintParallel.h>
76 #include <SketchPlugin_ConstraintPerpendicular.h>
77 #include <SketchPlugin_ConstraintRadius.h>
78 #include <SketchPlugin_ConstraintRigid.h>
79 #include <SketchPlugin_ConstraintHorizontal.h>
80 #include <SketchPlugin_ConstraintVertical.h>
81 #include <SketchPlugin_ConstraintEqual.h>
82 #include <SketchPlugin_ConstraintTangent.h>
83 #include <SketchPlugin_ConstraintCoincidence.h>
84 #include <SketchPlugin_Fillet.h>
85 #include <SketchPlugin_ConstraintMirror.h>
86 #include <SketchPlugin_ConstraintAngle.h>
87 #include <SketchPlugin_ConstraintCollinear.h>
88 #include <SketchPlugin_ConstraintMiddle.h>
89 #include <SketchPlugin_MultiRotation.h>
90 #include <SketchPlugin_MultiTranslation.h>
91 #include <SketchPlugin_IntersectionPoint.h>
92 #include <SketchPlugin_Projection.h>
93 #include <SketchPlugin_ConstraintDistanceAlongDir.h>
94 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
95 #include <SketchPlugin_ConstraintDistanceVertical.h>
96
97 #include <SketcherPrs_Tools.h>
98
99 #include <SelectMgr_IndexedMapOfOwner.hxx>
100 #include <StdSelect_BRepOwner.hxx>
101
102 //#include <AIS_DimensionSelectionMode.hxx>
103 #include <AIS_Shape.hxx>
104 #include <AIS_Dimension.hxx>
105
106 #include <ModelAPI_Events.h>
107 #include <ModelAPI_Session.h>
108 #include <ModelAPI_AttributeString.h>
109
110 #include <ModelAPI_Validator.h>
111 #include <ModelAPI_Tools.h>
112
113 #include <QMouseEvent>
114 #include <QApplication>
115 #include <QCursor>
116 #include <QMessageBox>
117 #include <QMainWindow>
118
119 //#define DEBUG_DO_NOT_BY_ENTER
120 //#define DEBUG_SKETCHER_ENTITIES
121 //#define DEBUG_SKETCH_ENTITIES_ON_MOVE
122 //#define DRAGGING_DEBUG
123 //#define DEBUG_CURSOR
124
125
126 #ifdef DRAGGING_DEBUG
127 #include <QTime>
128 #endif
129
130 /// Fills attribute and result lists by the selected owner. In case if the attribute is found,
131 /// by the owner shape, it is put to the list. Otherwise if type of owner shape is edge,
132 /// put the function result as is to the list of results.
133 /// \param theOwner a viewer selected owner
134 /// \param theFeature a feature, where the attribute is searched
135 /// \param theSketch a current sketch
136 /// \param theSelectedAttribute an output list of attributes
137 /// \param theSelectedResults an output list of edge results
138 void getAttributesOrResults(const Handle(SelectMgr_EntityOwner)& theOwner,
139                             const FeaturePtr& theFeature, const FeaturePtr& theSketch,
140                             const ResultPtr& theResult,
141                             std::set<AttributePtr>& theSelectedAttributes,
142                             std::set<ResultPtr>& theSelectedResults,
143                             TopTools_MapOfShape& theShapes)
144 {
145   Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
146   if (aBRepOwner.IsNull())
147     return;
148   Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
149                                                                     aBRepOwner->Selectable());
150   if (aBRepOwner->HasShape()) {
151     const TopoDS_Shape& aShape = aBRepOwner->Shape();
152     theShapes.Add(aShape);
153     TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
154     if (aShapeType == TopAbs_VERTEX) {
155       AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
156                                                                     aShape, theSketch);
157       if (aPntAttr.get() != NULL)
158         theSelectedAttributes.insert(aPntAttr);
159     }
160     else if (aShapeType == TopAbs_EDGE &&
161              theSelectedResults.find(theResult) == theSelectedResults.end()) {
162       theSelectedResults.insert(theResult);
163     }
164   }
165 }
166
167 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
168   : QObject(theModule), myModule(theModule), myIsEditLaunching(false), myIsDragging(false),
169     myDragDone(false), myIsMouseOverWindow(false),
170     myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
171     myIsPopupMenuActive(false), myExternalPointsMgr(0)
172 {
173   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
174   ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
175
176   myPreviousDrawModeEnabled = true;//aViewer->isSelectionEnabled();
177
178   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
179           this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
180
181   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
182           this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
183
184   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
185           this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
186
187   connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
188           this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
189
190   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
191   XGUI_Workshop* aWorkshop = aConnector->workshop();
192   connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
193
194   myIsConstraintsShown[PartSet_Tools::Geometrical] = true;
195   myIsConstraintsShown[PartSet_Tools::Dimensional] = true;
196   myIsConstraintsShown[PartSet_Tools::Expressions] = false;
197
198   mySketchPlane = new PartSet_PreviewSketchPlane();
199
200   registerSelectionFilter(SF_SketchCirclePointFilter, new PartSet_CirclePointFilter(anIWorkshop));
201   registerSelectionFilter(SF_SketchPlaneFilter, new ModuleBase_ShapeInPlaneFilter());
202 }
203
204 PartSet_SketcherMgr::~PartSet_SketcherMgr()
205 {
206   delete mySketchPlane;
207 }
208
209 void PartSet_SketcherMgr::onEnterViewPort()
210 {
211   // 1. if the mouse over window, update the next flag. Do not perform update visibility of
212   // created feature because it should be done in onMouseMove(). Some widgets watch
213   // the mouse move and use the cursor position to update own values. If the presentaion is
214   // redisplayed before this update, the feature presentation jumps from reset value to current.
215   myIsMouseOverWindow = true;
216
217   #ifdef DEBUG_DO_NOT_BY_ENTER
218   return;
219   #endif
220
221   if (canChangeCursor(getCurrentOperation())) {
222     QCursor* aCurrentCursor = QApplication::overrideCursor();
223     if (!aCurrentCursor || aCurrentCursor->shape() != Qt::CrossCursor) {
224       QApplication::setOverrideCursor(QCursor(Qt::CrossCursor));
225 #ifdef DEBUG_CURSOR
226       qDebug("onEnterViewPort() : Qt::CrossCursor");
227 #endif
228     }
229   }
230
231   if (!isNestedCreateOperation(getCurrentOperation(), activeSketch()))
232     return;
233
234   operationMgr()->onValidateOperation();
235
236   // we need change displayed state of the current operation feature
237   // if the feature is presentable, e.g. distance construction. It has no results, so workshop does
238   // not accept a signal about the result created. Nothing is shown until mouse is moved out/in view
239   // port. If the isDisplayed flag is true, the presentable feature is displayed as soon as the
240   // presentation becomes valid and redisplay happens
241   //ModuleBase_Operation* aOperation = getCurrentOperation();
242   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
243                                                                            (getCurrentOperation());
244   if (aFOperation) {
245     FeaturePtr aFeature = aFOperation->feature();
246     if (aFeature.get() && aFeature->data()->isValid()) {
247       visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature), false);
248     }
249   }
250 }
251
252 void PartSet_SketcherMgr::onLeaveViewPort()
253 {
254   myIsMouseOverViewProcessed = false;
255   myIsMouseOverWindow = false;
256
257   #ifdef DEBUG_DO_NOT_BY_ENTER
258   return;
259   #endif
260
261   if (canChangeCursor(getCurrentOperation())) {
262     QApplication::restoreOverrideCursor();
263 #ifdef DEBUG_CURSOR
264     qDebug("onLeaveViewPort() : None");
265 #endif
266   }
267
268   if (!isNestedCreateOperation(getCurrentOperation(), activeSketch()))
269     return;
270
271   // the method should be performed if the popup menu is called,
272   // the reset of the current widget should not happen
273   if (myIsPopupMenuActive)
274     return;
275
276   // it is important to validate operation here only if sketch entity create operation is active
277   // because at this operation we reacts to the mouse leave/enter view port
278   operationMgr()->onValidateOperation();
279
280   // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
281   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
282   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
283   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
284   // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
285   // obtained after reset value
286   bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
287   ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
288   if (anActiveWidget)
289     anActiveWidget->reset();
290
291   // hides the presentation of the current operation feature
292   // the feature is to be erased here, but it is correct to call canDisplayObject because
293   // there can be additional check (e.g. editor widget in distance constraint)
294   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
295                                                                            (getCurrentOperation());
296   if (aFOperation) {
297     FeaturePtr aFeature = aFOperation->feature();
298     visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature));
299   }
300   // we should update viewer after the presentation are hidden in the viewer
301   // otherwise the reset presentation(line) appears in the viewer(by quick move from viewer to PP)
302   aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
303 }
304
305 /*
306 //Temporary commented as we do not modify values in property panel
307 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
308 {
309   if (!isNestedEditOperation(getCurrentOperation(), myModule->sketchMgr()->activeSketch()) ||
310       myModule->sketchReentranceMgr()->isInternalEditActive())
311     return;
312   // it is necessary to save current selection in order to restore it after the values are modifed
313   storeSelection(ST_SelectAndHighlightType);
314
315   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
316   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
317   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
318   myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
319 }
320
321 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
322 {
323   if (!isNestedEditOperation(getCurrentOperation(), myModule->sketchMgr()->activeSketch()) ||
324       myModule->sketchReentranceMgr()->isInternalEditActive()) {
325     myModule->sketchReentranceMgr()->updateInternalEditActiveState();
326     return;
327   }
328   // it is necessary to restore current selection in order to restore it after values are modified
329   restoreSelection();
330   myCurrentSelection.clear();
331
332   // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
333   // viewer happens by deselect/select the modified objects. The flag should be restored after
334   // the selection processing. The update viewer should be also called.
335   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
336   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
337   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
338   aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
339   aDisplayer->updateViewer();
340 }
341 */
342
343 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
344 {
345   // Clear dragging mode
346   myIsDragging = false;
347
348   if (myModule->sketchReentranceMgr()->processMousePressed(theWnd, theEvent))
349     return;
350   //get2dPoint(theWnd, theEvent, myClickedPoint);
351   if (!(theEvent->buttons() & Qt::LeftButton))
352     return;
353
354   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
355   ModuleBase_IViewer* aViewer = aWorkshop->viewer();
356   if (!aViewer->canDragByMouse())
357     return;
358
359   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
360                                                                (getCurrentOperation());
361   if (!aFOperation)
362     return;
363
364   if (aFOperation->isEditOperation()) {
365     // If the current widget is a selector, do nothing, it processes the mouse press
366     ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
367     if(anActiveWidget && anActiveWidget->isViewerSelector()) {
368       return;
369     }
370   }
371
372   // Use only for sketch operations
373   if (myCurrentSketch) {
374     if (!PartSet_Tools::sketchPlane(myCurrentSketch))
375       return;
376
377     bool isSketcher = isSketchOperation(aFOperation);
378     bool isSketchOpe = isNestedSketchOperation(aFOperation);
379
380     // Avoid non-sketch operations
381     if ((!isSketchOpe) && (!isSketcher))
382       return;
383
384     bool isEditing = aFOperation->isEditOperation();
385
386     // Ignore creation sketch operation
387     if ((!isSketcher) && (!isEditing))
388       return;
389
390     Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
391     // Remember highlighted objects for editing
392     ModuleBase_ISelection* aSelect = aWorkshop->selection();
393
394     bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
395     storeSelection(aHasShift ? ST_SelectAndHighlightType : ST_HighlightType, myCurrentSelection);
396
397     if (myCurrentSelection.empty()) {
398       if (isSketchOpe && (!isSketcher))
399         // commit previous operation
400         if (!aFOperation->commit())
401           aFOperation->abort();
402       return;
403     }
404     // Init flyout point for radius rotation
405     FeaturePtr aFeature = myCurrentSelection.begin().key();
406
407     get2dPoint(theWnd, theEvent, myCurrentPoint);
408     if (isSketcher) {
409       myIsDragging = true;
410       myDragDone = false;
411
412       myPreviousDrawModeEnabled = aViewer->enableDrawMode(false);
413       launchEditing();
414       if (aFeature.get() != NULL) {
415         std::shared_ptr<SketchPlugin_Feature> aSPFeature =
416                   std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
417         if (aSPFeature.get() &&
418           (aSPFeature->getKind() == SketchPlugin_ConstraintRadius::ID() ||
419            aSPFeature->getKind() == SketchPlugin_ConstraintAngle::ID())) {
420           DataPtr aData = aSPFeature->data();
421           AttributePtr aAttr = aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
422           std::shared_ptr<GeomDataAPI_Point2D> aFPAttr =
423             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aAttr);
424           aFPAttr->setValue(myCurrentPoint.myCurX, myCurrentPoint.myCurY);
425         }
426       }
427     } else if (isSketchOpe && isEditing) {
428       // If selected another object commit current result
429       bool aPrevLaunchingState = myIsEditLaunching;
430       /// store editing state for Edit operation in order to do not clear highlight by restart
431       /// of edit operation.
432       /// Internal edit should not be stored as editing operation as the result will be a
433       /// creation operation, where previous selection should not be used(and will be cleared)
434       myIsEditLaunching = !myModule->sketchReentranceMgr()->isInternalEditActive();
435       aFOperation->commit();
436
437       myIsDragging = true;
438       myDragDone = false;
439
440       myPreviousDrawModeEnabled = aViewer->enableDrawMode(false);
441       launchEditing();
442       myIsEditLaunching = aPrevLaunchingState;
443       if (aFeature.get() != NULL) {
444         std::shared_ptr<SketchPlugin_Feature> aSPFeature =
445                   std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
446         if (aSPFeature.get() &&
447           (aSPFeature->getKind() == SketchPlugin_ConstraintRadius::ID() ||
448            aSPFeature->getKind() == SketchPlugin_ConstraintAngle::ID())) {
449           DataPtr aData = aSPFeature->data();
450           AttributePtr aAttr = aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
451           std::shared_ptr<GeomDataAPI_Point2D> aFPAttr =
452             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aAttr);
453           aFPAttr->setValue(myCurrentPoint.myCurX, myCurrentPoint.myCurY);
454         }
455       }
456     }
457   }
458 }
459
460 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
461 {
462   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
463   if (myIsDragging)
464     aWorkshop->viewer()->enableDrawMode(myPreviousDrawModeEnabled);
465
466   bool aWasDragging = myIsDragging;
467   myIsDragging = false;
468
469   if (myModule->sketchReentranceMgr()->processMouseReleased(theWnd, theEvent))
470     return;
471
472   // if mouse is pressed when it was over view and at release the mouse is out of view, do nothing
473   if (!myIsMouseOverViewProcessed)
474     return;
475
476   ModuleBase_IViewer* aViewer = aWorkshop->viewer();
477   if (!aViewer->canDragByMouse())
478     return;
479   ModuleBase_Operation* aOp = getCurrentOperation();
480   if (aOp) {
481     if (isNestedSketchOperation(aOp)) {
482       // Only for sketcher operations
483       if (aWasDragging) {
484         if (myDragDone) {
485           /// the previous selection is lost by mouse release in the viewer(Select method), but
486           /// it is still stored in myCurrentSelection. So, it is possible to restore selection
487           /// It is important for drag(edit with mouse) of sketch entities.
488           restoreSelection(myCurrentSelection);
489           myCurrentSelection.clear();
490         }
491       }
492     }
493   }
494
495
496   ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
497   PartSet_MouseProcessor* aProcessor = dynamic_cast<PartSet_MouseProcessor*>(anActiveWidget);
498   if (aProcessor)
499     aProcessor->mouseReleased(theWnd, theEvent);
500 }
501
502 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
503 {
504 #ifdef DEBUG_SKETCH_ENTITIES_ON_MOVE
505   CompositeFeaturePtr aSketch = activeSketch();
506   if (aSketch.get()) {
507     std::cout << "mouse move SKETCH FEATURES [" << aSketch->numberOfSubs() << "]:" << std::endl;
508     QStringList anInfo;
509     for (int i = 0, aNbSubs = aSketch->numberOfSubs(); i < aNbSubs; i++) {
510       //std::cout << getFeatureInfo(aSketch->subFeature(i), false) << std::endl;
511       anInfo.append(ModuleBase_Tools::objectInfo(aSketch->subFeature(i)));
512     }
513     QString anInfoStr = anInfo.join("\n");
514     qDebug(QString("%1").arg(anInfo.size()).arg(anInfoStr).toStdString().c_str());
515   }
516 #endif
517
518   if (myModule->sketchReentranceMgr()->processMouseMoved(theWnd, theEvent))
519     return;
520
521   if (isNestedCreateOperation(getCurrentOperation(), activeSketch())) {
522 #ifdef DRAGGING_DEBUG
523     QTime t;
524     t.start();
525 #endif
526     // 1. perform the widget mouse move functionality and display the presentation
527     // the mouse move should be processed in the widget, if it can in order to visualize correct
528     // presentation. These widgets correct the feature attribute according to the mouse position
529     ModuleBase_ModelWidget* anActiveWidget = myModule->activeWidget();
530     PartSet_MouseProcessor* aProcessor = dynamic_cast<PartSet_MouseProcessor*>(anActiveWidget);
531     if (aProcessor)
532       aProcessor->mouseMoved(theWnd, theEvent);
533     if (!myIsMouseOverViewProcessed) {
534       myIsMouseOverViewProcessed = true;
535
536       // the feature is to be erased here, but it is correct to call canDisplayObject because
537       // there can be additional check (e.g. editor widget in distance constraint)
538       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
539                                                  (getCurrentOperation());
540       if (aFOperation) {
541         FeaturePtr aFeature = aFOperation->feature();
542         visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature));
543       }
544     }
545 #ifdef DRAGGING_DEBUG
546     cout << "Mouse move processing " << t.elapsed() << endl;
547 #endif
548   }
549   //myClickedPoint.clear();
550
551   if (myIsDragging) {
552     // 1. the current selection is saved in the mouse press method in order to restore it after
553     //    moving
554     // 2. the enable selection in the viewer should be temporary switched off in order to ignore
555     // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
556     // deselected). This flag should be restored in the slot, processed the mouse release signal.
557
558     ModuleBase_Operation* aCurrentOperation = getCurrentOperation();
559     if (!aCurrentOperation)
560       return;
561     if (isSketchOperation(aCurrentOperation))
562       return; // No edit operation activated
563
564     Handle(V3d_View) aView = theWnd->v3dView();
565     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
566     Point aMousePnt;
567     get2dPoint(theWnd, theEvent, aMousePnt);
568
569     std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition = std::shared_ptr<GeomAPI_Pnt2d>(
570                             new GeomAPI_Pnt2d(myCurrentPoint.myCurX, myCurrentPoint.myCurY));
571     std::shared_ptr<GeomAPI_Pnt2d> aCurrentPosition = std::shared_ptr<GeomAPI_Pnt2d>(
572                             new GeomAPI_Pnt2d(aMousePnt.myCurX, aMousePnt.myCurY));
573
574     ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
575     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
576     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
577     // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
578     // viewer happens by deselect/select the modified objects. The flag should be restored after
579     // the selection processing. The update viewer should be also called.
580     bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
581
582     static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
583     //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
584     FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
585                                           aLast = myCurrentSelection.end();
586     // 4. the features and attributes modification(move)
587     bool isModified = false;
588     for (; anIt != aLast; anIt++) {
589       FeaturePtr aFeature = anIt.key();
590
591       std::set<AttributePtr> anAttributes = anIt.value().myAttributes;
592       // Process selection by attribute: the priority to the attribute
593       if (!anAttributes.empty()) {
594         std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
595                                                anAttLast = anAttributes.end();
596         for (; anAttIt != anAttLast; anAttIt++) {
597           AttributePtr anAttr = *anAttIt;
598           if (anAttr.get() == NULL)
599             continue;
600           std::string aAttrId = anAttr->id();
601           DataPtr aData = aFeature->data();
602           if (aData->isValid()) {
603             std::shared_ptr<GeomDataAPI_Point2D> aPoint =
604               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
605             if (aPoint.get() != NULL) {
606               bool isImmutable = aPoint->setImmutable(true);
607
608               std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage = std::shared_ptr
609                        <ModelAPI_ObjectMovedMessage>(new ModelAPI_ObjectMovedMessage(this));
610               aMessage->setMovedAttribute(aPoint);
611               aMessage->setOriginalPosition(anOriginalPosition);
612               aMessage->setCurrentPosition(aCurrentPosition);
613               Events_Loop::loop()->send(aMessage);
614
615               isModified = true;
616               aPoint->setImmutable(isImmutable);
617             }
618           }
619         }
620       } else {
621         // Process selection by feature
622         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
623           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
624         if (aSketchFeature) {
625           std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage = std::shared_ptr
626                     <ModelAPI_ObjectMovedMessage>(new ModelAPI_ObjectMovedMessage(this));
627           aMessage->setMovedObject(aFeature);
628           aMessage->setOriginalPosition(anOriginalPosition);
629           aMessage->setCurrentPosition(aCurrentPosition);
630           Events_Loop::loop()->send(aMessage);
631           isModified = true;
632         }
633       }
634     }
635     // the modified state of the current operation should be updated if there are features, which
636     // were changed here
637     if (isModified) {
638       aCurrentOperation->onValuesChanged();
639       Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
640     }
641     //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
642
643     // 5. it is necessary to save current selection in order to restore it after the features moving
644     restoreSelection(myCurrentSelection);
645     // 6. restore the update viewer flag and call this update
646     aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
647     aDisplayer->updateViewer();
648
649     myDragDone = true;
650     myCurrentPoint = aMousePnt;
651   }
652 }
653
654 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
655 {
656   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
657                                                                (getCurrentOperation());
658   if (aFOperation && aFOperation->isEditOperation()) {
659     std::string aId = aFOperation->id().toStdString();
660     if (isDistanceOperation(aFOperation))
661     {
662       // Activate dimension value editing on double click
663       ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
664       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
665       // Find corresponded widget to activate value editing
666       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
667         std::string anId = aWgt->attributeID();
668         if (anId == SketchPlugin_Constraint::VALUE() ||
669           anId == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() ||
670           anId == SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()) {
671           PartSet_WidgetEditor* anEditor = dynamic_cast<PartSet_WidgetEditor*>(aWgt);
672           if (anEditor)
673             anEditor->showPopupEditor();
674           return;
675         }
676       }
677     }
678   }
679 }
680
681 void PartSet_SketcherMgr::onApplicationStarted()
682 {
683   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
684   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
685   XGUI_Workshop* aWorkshop = aConnector->workshop();
686   PartSet_SketcherReentrantMgr* aReentranceMgr = myModule->sketchReentranceMgr();
687
688   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
689   if (aPropertyPanel) {
690     //connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
691     //        this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
692
693     connect(aPropertyPanel, SIGNAL(noMoreWidgets(const std::string&)),
694             aReentranceMgr, SLOT(onNoMoreWidgets(const std::string&)));
695     //connect(aPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
696     //        aReentranceMgr, SLOT(onWidgetActivated()));
697   }
698
699   XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
700   connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
701   connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
702
703   XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
704   connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
705   connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
706 }
707
708 //void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
709 //{
710   //if (!myClickedPoint.myIsInitialized)
711   //  return;
712
713   //ModuleBase_Operation* aOperation = getCurrentOperation();
714   // the distance constraint feature should not use the clickedd point
715   // this is workaround in order to don't throw down the flyout point value,
716   // set by execute() method of these type of features
717   //if (isDistanceOperation(aOperation))
718   //  return;
719
720   //PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
721   //if (aPnt2dWgt) {
722   //  aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
723   //}
724 //}
725
726 void PartSet_SketcherMgr::onBeforeContextMenu()
727 {
728   myIsPopupMenuActive = true;
729 }
730
731 void PartSet_SketcherMgr::onAfterContextMenu()
732 {
733   myIsPopupMenuActive = false;
734 }
735
736 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
737                                      Point& thePoint)
738 {
739   Handle(V3d_View) aView = theWnd->v3dView();
740   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
741   double aX, anY;
742   PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
743   thePoint.setValue(aX, anY);
744 }
745
746 void PartSet_SketcherMgr::launchEditing()
747 {
748   if (!myCurrentSelection.empty()) {
749     FeaturePtr aFeature = myCurrentSelection.begin().key();
750     std::shared_ptr<SketchPlugin_Feature> aSPFeature =
751               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
752     if (aSPFeature) {
753       if (!aSPFeature->isExternal())
754         myModule->editFeature(aSPFeature);
755       else {
756         // need to edit a feature (Projection/IntersectionPoint),
757         // which produces current External feature
758         FeaturePtr aProducerFeature = PartSet_Tools::findRefsToMeFeature(aFeature,
759                                                         SketchPlugin_Projection::ID());
760         if (!aProducerFeature.get())
761           aProducerFeature = PartSet_Tools::findRefsToMeFeature(aFeature,
762                                                         SketchPlugin_IntersectionPoint::ID());
763         if (aProducerFeature.get())
764           myModule->editFeature(aProducerFeature);
765       }
766     }
767   }
768 }
769
770 bool PartSet_SketcherMgr::sketchSolverError()
771 {
772   bool anError = false;
773   CompositeFeaturePtr aSketch = activeSketch();
774   if (aSketch.get()) {
775     AttributeStringPtr aAttributeString = aSketch->string(SketchPlugin_Sketch::SOLVER_ERROR());
776     anError = !aAttributeString->value().empty();
777   }
778   return anError;
779 }
780
781 QString PartSet_SketcherMgr::getFeatureError(const FeaturePtr& theFeature)
782 {
783   QString anError;
784   if (!theFeature.get() || !theFeature->data()->isValid())
785     return anError;
786
787   CompositeFeaturePtr aSketch = activeSketch();
788   if (aSketch.get() && aSketch == theFeature) {
789     std::string aSolverError = aSketch->string(SketchPlugin_Sketch::SOLVER_ERROR())->value();
790     anError = ModuleBase_Tools::translate(aSketch->getKind(), aSolverError);
791   }
792   return anError;
793 }
794
795 void PartSet_SketcherMgr::clearClickedFlags()
796 {
797   //myClickedPoint.clear();
798   myCurrentPoint.clear();
799 }
800
801 const QStringList& PartSet_SketcherMgr::replicationsIdList()
802 {
803   static QStringList aReplicationIds;
804   if (aReplicationIds.size() == 0) {
805     aReplicationIds << SketchPlugin_ConstraintMirror::ID().c_str();
806     aReplicationIds << SketchPlugin_MultiRotation::ID().c_str();
807     aReplicationIds << SketchPlugin_MultiTranslation::ID().c_str();
808   }
809   return aReplicationIds;
810 }
811
812 const QStringList& PartSet_SketcherMgr::constraintsIdList()
813 {
814   static QStringList aConstraintIds;
815   if (aConstraintIds.size() == 0) {
816     aConstraintIds << SketchPlugin_ConstraintLength::ID().c_str();
817     aConstraintIds << SketchPlugin_ConstraintDistance::ID().c_str();
818     aConstraintIds << SketchPlugin_ConstraintRigid::ID().c_str();
819     aConstraintIds << SketchPlugin_ConstraintRadius::ID().c_str();
820     aConstraintIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
821     aConstraintIds << SketchPlugin_ConstraintParallel::ID().c_str();
822     aConstraintIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
823     aConstraintIds << SketchPlugin_ConstraintVertical::ID().c_str();
824     aConstraintIds << SketchPlugin_ConstraintEqual::ID().c_str();
825     aConstraintIds << SketchPlugin_ConstraintTangent::ID().c_str();
826     aConstraintIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
827     aConstraintIds << SketchPlugin_ConstraintAngle::ID().c_str();
828     aConstraintIds << SketchPlugin_ConstraintCollinear::ID().c_str();
829     aConstraintIds << SketchPlugin_ConstraintMiddle::ID().c_str();
830     aConstraintIds << SketchPlugin_ConstraintMirror::ID().c_str();
831     aConstraintIds << SketchPlugin_MultiTranslation::ID().c_str();
832     aConstraintIds << SketchPlugin_MultiRotation::ID().c_str();
833     aConstraintIds << SketchPlugin_ConstraintDistanceAlongDir::ID().c_str();
834     aConstraintIds << SketchPlugin_ConstraintDistanceHorizontal::ID().c_str();
835     aConstraintIds << SketchPlugin_ConstraintDistanceVertical::ID().c_str();
836   }
837   return aConstraintIds;
838 }
839
840 void PartSet_SketcherMgr::sketchSelectionModes(const CompositeFeaturePtr& theSketch,
841                                                QIntList& theModes)
842 {
843   if (!theSketch.get() || !PartSet_Tools::sketchPlane(theSketch).get())
844     return;
845
846   theModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
847   theModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
848   theModes.append(SketcherPrs_Tools::Sel_Constraint);
849   theModes.append(TopAbs_VERTEX);
850   theModes.append(TopAbs_EDGE);
851 }
852
853 Handle(AIS_InteractiveObject) PartSet_SketcherMgr::createPresentation(const ResultPtr& theResult)
854 {
855   Handle(AIS_InteractiveObject) aPrs;
856
857   FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
858   if (aFeature.get() && aFeature->getKind() == SketchPlugin_Sketch::ID()) {
859     aPrs = new PartSet_ResultSketchPrs(theResult);
860   }
861   return aPrs;
862 }
863
864 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
865 {
866   return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
867 }
868
869 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation) const
870 {
871   bool aNestedSketch = false;
872
873   FeaturePtr anActiveSketch = activeSketch();
874   if (anActiveSketch.get() && theOperation) {
875     ModuleBase_Operation* aSketchOperation = operationMgr()->findOperation(
876                                                               anActiveSketch->getKind().c_str());
877     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
878                                                                                   (theOperation);
879     if (aSketchOperation && aFOperation) {
880       FeaturePtr aFeature = aFOperation->feature();
881       if (aFeature.get()) {
882         QStringList aGrantedOpIds = aSketchOperation->grantedOperationIds();
883         aNestedSketch = aGrantedOpIds.contains(aFeature->getKind().c_str());
884       }
885     }
886   }
887   return aNestedSketch;
888 }
889
890 bool PartSet_SketcherMgr::isNestedSketchFeature(const QString& theFeatureKind) const
891 {
892   bool aNestedSketch = false;
893
894   FeaturePtr anActiveSketch = activeSketch();
895   if (anActiveSketch.get()) {
896     ModuleBase_Operation* aSketchOperation = operationMgr()->findOperation(
897                                                               anActiveSketch->getKind().c_str());
898     if (aSketchOperation) {
899       QStringList aGrantedOpIds = aSketchOperation->grantedOperationIds();
900       aNestedSketch = aGrantedOpIds.contains(theFeatureKind);
901     }
902   }
903   return aNestedSketch;
904 }
905
906 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation,
907                                                   const CompositeFeaturePtr& theSketch) const
908 {
909   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
910                                                                (theOperation);
911   return aFOperation && !aFOperation->isEditOperation() &&
912          isNestedSketchOperation(aFOperation);
913 }
914
915 bool PartSet_SketcherMgr::isNestedEditOperation(ModuleBase_Operation* theOperation,
916                                                 const CompositeFeaturePtr& theSketch) const
917 {
918   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
919                                                                (theOperation);
920   return aFOperation && aFOperation->isEditOperation() &&
921     isNestedSketchOperation(aFOperation);
922 }
923
924 bool PartSet_SketcherMgr::isEntity(const std::string& theId)
925 {
926   return (theId == SketchPlugin_Line::ID()) ||
927          (theId == SketchPlugin_Point::ID()) ||
928          (theId == SketchPlugin_Arc::ID()) ||
929          (theId == SketchPlugin_Circle::ID());
930 }
931
932 bool PartSet_SketcherMgr::isExternalFeature(const FeaturePtr& theFeature)
933 {
934   std::shared_ptr<SketchPlugin_Feature> aSPFeature =
935           std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
936   return aSPFeature.get() && aSPFeature->isExternal();
937 }
938
939 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
940 {
941   std::string anId = theOperation ? theOperation->id().toStdString() : "";
942
943   return isDistanceKind(anId);
944 }
945
946 bool PartSet_SketcherMgr::isDistanceKind(std::string& theKind)
947 {
948   return (theKind == SketchPlugin_ConstraintLength::ID()) ||
949          (theKind == SketchPlugin_ConstraintDistance::ID()) ||
950          (theKind == SketchPlugin_ConstraintRadius::ID()) ||
951          (theKind == SketchPlugin_ConstraintAngle::ID()) ||
952          (theKind == SketchPlugin_ConstraintDistanceHorizontal::ID()) ||
953          (theKind == SketchPlugin_ConstraintDistanceVertical::ID()) ||
954          (theKind == SketchPlugin_ConstraintDistanceAlongDir::ID());
955 }
956
957 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
958 {
959   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
960                                                                (getCurrentOperation());
961   if (!aFOperation)
962     return;
963
964   myModule->onViewTransformed();
965
966   // Display all sketcher sub-Objects
967   myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFOperation->feature());
968   double aSizeOfView = 0;
969   std::shared_ptr<GeomAPI_Pnt> aCentralPoint;
970   if (aFOperation->isEditOperation() &&
971       mySketchPlane->getDefaultSizeOfView(myCurrentSketch, aSizeOfView, aCentralPoint)) {
972     mySketchPlane->setSizeOfView(aSizeOfView, true, aCentralPoint);
973   }
974
975   mySketchPlane->createSketchPlane(myCurrentSketch, myModule->workshop());
976   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
977
978   // Hide sketcher result
979   std::list<ResultPtr> aResults = myCurrentSketch->results();
980   std::list<ResultPtr>::const_iterator aIt;
981   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
982     (*aIt)->setDisplayed(false);
983   }
984   myCurrentSketch->setDisplayed(false);
985
986   // Remove invalid sketch entities
987   std::set<FeaturePtr> anInvalidFeatures;
988   ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
989   int aNumberOfSubs = myCurrentSketch->numberOfSubs();
990   for (int i = 0; i < aNumberOfSubs; i++) {
991     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
992     if (aFeature.get()) {
993       if (!aFactory->validate(aFeature))
994         anInvalidFeatures.insert(aFeature);
995     }
996   }
997   if (!anInvalidFeatures.empty()) {
998     std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
999     ModelAPI_Tools::findAllReferences(anInvalidFeatures, aReferences, false);
1000
1001     std::set<FeaturePtr>::const_iterator anIt = anInvalidFeatures.begin(),
1002                                          aLast = anInvalidFeatures.end();
1003     // separate features to references to parameter features and references to others
1004     QStringList anInvalidFeatureNames;
1005     for (; anIt != aLast; anIt++) {
1006       FeaturePtr aFeature = *anIt;
1007       if (aFeature.get())
1008         anInvalidFeatureNames.append(aFeature->name().c_str());
1009     }
1010     std::string aPrefixInfo = QString("Invalid features of the sketch will be deleted: %1.\n\n").
1011                                   arg(anInvalidFeatureNames.join(", ")).toStdString().c_str();
1012     std::set<FeaturePtr> aFeatureRefsToDelete;
1013     if (ModuleBase_Tools::askToDelete(anInvalidFeatures, aReferences, aConnector->desktop(),
1014                                       aFeatureRefsToDelete, aPrefixInfo)) {
1015       if (!aFeatureRefsToDelete.empty())
1016         anInvalidFeatures.insert(aFeatureRefsToDelete.begin(), aFeatureRefsToDelete.end());
1017       ModelAPI_Tools::removeFeatures(anInvalidFeatures, true);
1018       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
1019       // TODO: call the next method in the XGUI_OperationMgr::onOperationStarted().
1020       workshop()->errorMgr()->updateAcceptAllAction(myCurrentSketch);
1021     }
1022   }
1023
1024   // update state of overconstraint listener should be done before sketch features/results
1025   // display (as the display will ask custom color from the listener)
1026   myModule->overconstraintListener()->setActive(true);
1027   // Display sketcher objects
1028   QStringList anInfo;
1029   Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1030   const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
1031   aNumberOfSubs = myCurrentSketch->numberOfSubs();
1032   for (int i = 0; i < aNumberOfSubs; i++) {
1033     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
1034 #ifdef DEBUG_SKETCHER_ENTITIES
1035     anInfo.append(ModuleBase_Tools::objectInfo(aFeature));
1036 #endif
1037     std::list<ResultPtr> aResults = aFeature->results();
1038     std::list<ResultPtr>::const_iterator aIt;
1039     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1040       if ((*aIt)->isDisplayed())
1041         // Display object if it was created outside of GUI
1042         aECreator->sendUpdated((*aIt), EVENT_DISP);
1043       else
1044         (*aIt)->setDisplayed(true);
1045     }
1046     if (aFeature->isDisplayed())
1047       aECreator->sendUpdated(aFeature, EVENT_DISP);
1048     else
1049       aFeature->setDisplayed(true);
1050   }
1051 #ifdef DEBUG_SKETCHER_ENTITIES
1052   QString anInfoStr = anInfo.join(";\t");
1053   qDebug(QString("startSketch: %1, %2").arg(anInfo.size()).arg(anInfoStr).toStdString().c_str());
1054 #endif
1055
1056   bool aHasPlane = false;
1057   std::shared_ptr<GeomAPI_Pln> aPln;
1058   aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
1059   Handle(SelectMgr_Filter) aFilter = myModule->selectionFilter(SF_SketchPlaneFilter);
1060   if (!aFilter.IsNull())
1061     Handle(ModuleBase_ShapeInPlaneFilter)::DownCast(aFilter)->setPlane(aPln);
1062
1063   workshop()->selectionActivate()->updateSelectionFilters();
1064   workshop()->selectionActivate()->updateSelectionModes();
1065
1066   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1067
1068   myExternalPointsMgr = new PartSet_ExternalPointsMgr(myModule->workshop(), myCurrentSketch);
1069
1070   workshop()->viewer()->set2dMode(true);
1071 }
1072
1073 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
1074 {
1075   myIsMouseOverWindow = false;
1076   myIsConstraintsShown[PartSet_Tools::Geometrical] = true;
1077   myIsConstraintsShown[PartSet_Tools::Dimensional] = true;
1078   myIsConstraintsShown[PartSet_Tools::Expressions] = false;
1079
1080   if (myExternalPointsMgr) {
1081     delete myExternalPointsMgr;
1082     myExternalPointsMgr = 0;
1083   }
1084   onShowPoints(false);
1085
1086   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
1087
1088   DataPtr aData = myCurrentSketch->data();
1089   if (!aData->isValid()) {
1090     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1091     // The sketch was aborted
1092     myCurrentSketch = CompositeFeaturePtr();
1093     mySketchPlane->eraseSketchPlane(myModule->workshop());
1094
1095     // Erase all sketcher objects
1096     QObjectPtrList aObjects = aDisplayer->displayedObjects();
1097     foreach (ObjectPtr aObj, aObjects) {
1098       DataPtr aObjData = aObj->data();
1099       if (!aObjData->isValid())
1100         aObj->setDisplayed(false);
1101     }
1102   }
1103   else {
1104     // Hide all sketcher sub-Objects
1105     int aNumberOfSubs = myCurrentSketch->numberOfSubs();
1106     for (int i = 0; i < aNumberOfSubs; i++) {
1107       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
1108       std::list<ResultPtr> aResults = aFeature->results();
1109       std::list<ResultPtr>::const_iterator aIt;
1110       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1111         (*aIt)->setDisplayed(false);
1112       }
1113       aFeature->setDisplayed(false);
1114     }
1115     // Display sketcher result
1116     std::list<ResultPtr> aResults = myCurrentSketch->results();
1117     std::list<ResultPtr>::const_iterator aIt;
1118     Events_Loop* aLoop = Events_Loop::loop();
1119     static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1120
1121     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1122                                                                            (theOperation);
1123     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1124       if (!aFOperation->isDisplayedOnStart(*aIt)) {
1125         (*aIt)->setDisplayed(true);
1126         // this display event is needed because sketch already may have "displayed" state,
1127         // but not displayed while it is still active (issue 613, abort of existing sketch)
1128         ModelAPI_EventCreator::get()->sendUpdated(*aIt, aDispEvent);
1129       }
1130     }
1131     if (!aFOperation->isDisplayedOnStart(myCurrentSketch))
1132       myCurrentSketch->setDisplayed(true);
1133
1134     myCurrentSketch = CompositeFeaturePtr();
1135     mySketchPlane->eraseSketchPlane(myModule->workshop());
1136
1137     Events_Loop::loop()->flush(aDispEvent);
1138   }
1139   workshop()->selectionActivate()->updateSelectionFilters();
1140   workshop()->selectionActivate()->updateSelectionModes();
1141   workshop()->viewer()->set2dMode(false);
1142 }
1143
1144 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* theOperation)
1145 {
1146   if (canChangeCursor(theOperation) && myIsMouseOverWindow) {
1147     QCursor* aCurrentCursor = QApplication::overrideCursor();
1148     if (!aCurrentCursor || aCurrentCursor->shape() != Qt::CrossCursor) {
1149       QApplication::setOverrideCursor(QCursor(Qt::CrossCursor));
1150 #ifdef DEBUG_CURSOR
1151       qDebug("startNestedSketch() : Qt::CrossCursor");
1152 #endif
1153     }
1154   }
1155 }
1156
1157 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
1158 {
1159   myIsMouseOverViewProcessed = true;
1160   operationMgr()->onValidateOperation();
1161   // when sketch nested operation is stopped the cursor should be restored unconditionally
1162   //if (canChangeCursor(theOperation)) {
1163     QApplication::restoreOverrideCursor();
1164 #ifdef DEBUG_CURSOR
1165     qDebug("stopNestedSketch() : None");
1166 #endif
1167   //}
1168   /// improvement to deselect automatically all eventual selected objects, when
1169   // returning to the neutral point of the Sketcher
1170   bool isClearSelectionPossible = true;
1171   if (myIsEditLaunching) {
1172     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1173                                                                           (theOperation);
1174     if (aFOperation) {
1175       FeaturePtr aFeature = aFOperation->feature();
1176       if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
1177         isClearSelectionPossible = false;
1178       }
1179     }
1180   }
1181   if (isClearSelectionPossible)
1182     workshop()->selector()->clearSelection();
1183   if (myPointsHighlight.size())
1184     onShowPoints(true);
1185 }
1186
1187 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
1188 {
1189   if (isNestedCreateOperation(theOperation, activeSketch())) {
1190     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1191                                                                              (theOperation);
1192     if (aFOperation) {
1193       FeaturePtr aFeature = aFOperation->feature();
1194       // it is necessary to check the the feature data validity because
1195       // some kind of features are removed by an operation commit(the macro state of a feature)
1196       if (aFeature.get() && aFeature->data()->isValid()) {
1197         visualizeFeature(aFeature, aFOperation->isEditOperation(), true);
1198       }
1199     }
1200   }
1201 }
1202
1203 bool PartSet_SketcherMgr::sketchSelectionFilter(const ModuleBase_SelectionFilterType theFilterType)
1204 {
1205   return mySelectionFilterTypes.find(theFilterType) != mySelectionFilterTypes.end();
1206 }
1207
1208 void PartSet_SketcherMgr::registerSelectionFilter(
1209   const ModuleBase_SelectionFilterType theFilterType, const Handle(SelectMgr_Filter)& theFilter)
1210 {
1211   mySelectionFilterTypes.insert(theFilterType);
1212   myModule->registerSelectionFilter(theFilterType, theFilter);
1213 }
1214
1215 bool PartSet_SketcherMgr::operationActivatedByPreselection()
1216 {
1217   bool isOperationStopped = false;
1218   ModuleBase_Operation* anOperation = getCurrentOperation();
1219   if(anOperation && isNestedSketchOperation(anOperation)) {
1220     // Set final definitions if they are necessary
1221     //propertyPanelDefined(aOperation);
1222     /// Commit sketcher operations automatically
1223     /// distance operation are able to show popup editor to modify the distance value
1224     /// after entering the value, the operation should be committed/aborted(by Esc key)
1225     bool aCanCommitOperation = true;
1226     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1227                                                                             (anOperation);
1228     if (aFOperation && PartSet_SketcherMgr::isDistanceOperation(aFOperation)) {
1229       bool aValueAccepted = setDistanceValueByPreselection(anOperation, myModule->workshop(),
1230                                                            aCanCommitOperation);
1231       if (!aValueAccepted)
1232         return isOperationStopped;
1233     }
1234
1235     if (aCanCommitOperation)
1236       isOperationStopped = anOperation->commit();
1237     else {
1238       anOperation->abort();
1239       isOperationStopped = true;
1240     }
1241   }
1242   return isOperationStopped;
1243 }
1244
1245 bool PartSet_SketcherMgr::canUndo() const
1246 {
1247   return isNestedCreateOperation(getCurrentOperation(), activeSketch());
1248 }
1249
1250 bool PartSet_SketcherMgr::canRedo() const
1251 {
1252   return isNestedCreateOperation(getCurrentOperation(), activeSketch());
1253 }
1254
1255 bool PartSet_SketcherMgr::canEraseObject(const ObjectPtr& theObject) const
1256 {
1257   bool aCanErase = true;
1258   // when the sketch operation is active, results of sketch sub-feature can not be hidden
1259   if (myCurrentSketch.get()) {
1260     return !isObjectOfSketch(theObject);
1261   }
1262   return aCanErase;
1263 }
1264
1265 bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
1266 {
1267   bool aCanDisplay = true;
1268
1269   bool aHasActiveSketch = activeSketch().get() != NULL;
1270   if (aHasActiveSketch) {
1271     // 1. the sketch feature should not be displayed during the sketch active operation
1272     // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch
1273     // nested features can be visualized
1274     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1275     if (aFeature.get() != NULL && aFeature == activeSketch()) {
1276       aCanDisplay = false;
1277     }
1278     std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1279                             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1280     /// some sketch entities should be never shown, e.g. projection feature
1281     if (aSketchFeature.get())
1282       aCanDisplay = aSketchFeature->canBeDisplayed();
1283   }
1284   else { // there are no an active sketch
1285     // 2. sketch sub-features should not be visualized if the sketch operation is not active
1286     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1287     if (aFeature.get() != NULL) {
1288       std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1289                               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1290       if (aSketchFeature.get()) {
1291         aCanDisplay = false;
1292       }
1293     }
1294   }
1295
1296   // 3. the method should not filter the objects, which are not related to the current operation.
1297   // The object is filtered just if it is a current operation feature or this feature result
1298   if (aCanDisplay) {
1299     bool isObjectFound = false;
1300     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1301                                                                  (getCurrentOperation());
1302     if (aFOperation) {
1303       FeaturePtr aFeature = aFOperation->feature();
1304       if (aFeature.get()) {
1305         std::list<ResultPtr> aResults = aFeature->results();
1306         if (theObject == aFeature)
1307           isObjectFound = true;
1308         else {
1309           std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1310           for (; anIt != aLast && !isObjectFound; anIt++) {
1311             isObjectFound = *anIt == theObject;
1312           }
1313         }
1314       }
1315     }
1316     if (isObjectFound) {
1317       // 4. For created nested feature operation do not display the created feature if
1318       // the mouse curstor leaves the OCC window.
1319       // The correction cases, which ignores this condition:
1320       // a. the property panel values modification
1321       // b. the popup menu activated
1322       // c. widget editor control
1323       #ifndef DEBUG_DO_NOT_BY_ENTER
1324       if (isNestedCreateOperation(getCurrentOperation(), activeSketch())) {
1325         ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
1326         ModuleBase_WidgetEditor* anEditorWdg =
1327           anActiveWidget ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWidget) : 0;
1328         // the active widget editor should not influence here. The presentation should be visible
1329         // always when this widget is active.
1330         if (!anEditorWdg && !myIsPopupMenuActive) {
1331           // during a nested create operation, the feature is redisplayed only
1332           // if the mouse over view
1333           // of there was a value modified in the property panel after the mouse left the view
1334           aCanDisplay = canDisplayCurrentCreatedFeature();
1335         }
1336       }
1337       #endif
1338     }
1339   }
1340
1341   // checks the sketcher constraints visibility according to active sketch check box states
1342   if (aCanDisplay) {
1343     bool aProcessed = false;
1344     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1345     if (aFeature.get()) {
1346       bool aConstraintDisplayed = canDisplayConstraint(aFeature, PartSet_Tools::Any, aProcessed);
1347       if (aProcessed)
1348         aCanDisplay = aConstraintDisplayed;
1349     }
1350   }
1351
1352   return aCanDisplay;
1353 }
1354
1355 bool PartSet_SketcherMgr::canDisplayConstraint(const FeaturePtr& theFeature,
1356                                              const PartSet_Tools::ConstraintVisibleState& theState,
1357                                              bool& isProcessed) const
1358 {
1359   bool aSwitchedOn = true;
1360
1361   const QStringList& aConstrIds = constraintsIdList();
1362
1363   std::string aKind = theFeature->getKind();
1364   if (aConstrIds.contains(QString(aKind.c_str()))) {
1365     bool isTypedConstraint = false;
1366
1367     switch (theState) {
1368       case PartSet_Tools::Dimensional: {
1369         bool isDistance = isDistanceKind(aKind);
1370         if (isDistance) {
1371           isProcessed = true;
1372           aSwitchedOn = myIsConstraintsShown[theState];
1373         }
1374       }
1375       break;
1376       case PartSet_Tools::Geometrical: {
1377         bool isGeometrical = !isDistanceKind(aKind);
1378         if (isGeometrical) {
1379           isProcessed = true;
1380           aSwitchedOn = myIsConstraintsShown[theState];
1381         }
1382       }
1383       break;
1384       case PartSet_Tools::Any: {
1385         isProcessed = true;
1386         bool isDistance = isDistanceKind(aKind);
1387         if (isDistance)
1388           aSwitchedOn = myIsConstraintsShown[PartSet_Tools::Dimensional];
1389         else
1390           aSwitchedOn = myIsConstraintsShown[PartSet_Tools::Geometrical];
1391       }
1392       break;
1393     default:
1394       break;
1395     }
1396   }
1397   return aSwitchedOn;
1398 }
1399
1400 /*void PartSet_SketcherMgr::processHiddenObject(const std::list<ObjectPtr>& theObjects)
1401 {
1402   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1403                                                                            (getCurrentOperation());
1404   if (aFOperation && myCurrentSketch.get()) {
1405     // find results of the current operation
1406     // these results should not be proposed to be deleted
1407     FeaturePtr anOperationFeature = aFOperation->feature();
1408     std::list<ResultPtr> anOperationResultList = anOperationFeature->results();
1409     std::set<ResultPtr> anOperationResults;
1410     std::list<ResultPtr>::const_iterator aRIt = anOperationResultList.begin(),
1411                                         aRLast = anOperationResultList.end();
1412     for (; aRIt != aRLast; aRIt++)
1413       anOperationResults.insert(*aRIt);
1414
1415     std::set<FeaturePtr> anObjectsToBeDeleted;
1416     QStringList anObjectsToBeDeletedNames;
1417     std::list<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
1418     for (; anIt != aLast; anIt++) {
1419       ObjectPtr anObject = *anIt;
1420       bool aCanErase = true;
1421       // when the sketch operation is active, results of sketch sub-feature can not be hidden
1422       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1423       // the result is found between current feature results
1424       if (anOperationResults.find(aResult) != anOperationResults.end())
1425         continue;
1426
1427       if (aResult.get()) {
1428         // Display sketcher objects
1429         for (int i = 0; i < myCurrentSketch->numberOfSubs() && aCanErase; i++) {
1430           FeaturePtr aFeature = myCurrentSketch->subFeature(i);
1431           std::list<ResultPtr> aResults = aFeature->results();
1432           std::list<ResultPtr>::const_iterator anIt;
1433           for (anIt = aResults.begin(); anIt != aResults.end() && aCanErase; ++anIt) {
1434             aCanErase = *anIt != aResult;
1435           }
1436         }
1437       }
1438       if (!aCanErase) {
1439         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
1440         if (aFeature.get() && anObjectsToBeDeleted.find(aFeature) == anObjectsToBeDeleted.end()) {
1441           anObjectsToBeDeleted.insert(aFeature);
1442           anObjectsToBeDeletedNames.append(aFeature->name().c_str());
1443         }
1444       }
1445     }
1446     if (!anObjectsToBeDeleted.empty()) {
1447       QString aFeatureNames = anObjectsToBeDeletedNames.join(", ");
1448       QString aMessage = tr("The following features have incorrect presentation and \
1449 will be hidden: %1. Would you like to delete them?")
1450                          .arg(aFeatureNames);
1451       int anAnswer = QMessageBox::question(qApp->activeWindow(), tr("Features hide"),
1452                                            aMessage, QMessageBox::Ok | QMessageBox::Cancel,
1453                                            QMessageBox::Cancel);
1454       if (anAnswer == QMessageBox::Ok) {
1455         QObjectPtrList anObjects;
1456         std::set<FeaturePtr>::const_iterator anIt = anObjectsToBeDeleted.begin(),
1457                                              aLast = anObjectsToBeDeleted.end();
1458         for (; anIt != aLast; anIt++)
1459           anObjects.append(*anIt);
1460         SessionPtr aMgr = ModelAPI_Session::get();
1461         DocumentPtr aDoc = aMgr->activeDocument();
1462         bool aIsOp = aMgr->isOperation();
1463         if (!aIsOp)
1464           aMgr->startOperation();
1465         workshop()->deleteFeatures(anObjects);
1466         //static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
1467         //static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
1468         //Events_Loop::loop()->flush(aDeletedEvent);
1469         //Events_Loop::loop()->flush(aRedispEvent);
1470
1471         if (!aIsOp)
1472           aMgr->finishOperation();
1473       }
1474     }
1475   }
1476 }*/
1477
1478 bool PartSet_SketcherMgr::canDisplayCurrentCreatedFeature() const
1479 {
1480   bool aCanDisplay = myIsMouseOverWindow;
1481   if (!aCanDisplay) {
1482     ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
1483     if (anActiveWidget)
1484       aCanDisplay = anActiveWidget->getValueState() == ModuleBase_ModelWidget::Stored;
1485   }
1486   return aCanDisplay;
1487 }
1488
1489 bool PartSet_SketcherMgr::canChangeCursor(ModuleBase_Operation* theOperation) const
1490 {
1491   return isNestedCreateOperation(theOperation, activeSketch()) ||
1492          myModule->sketchReentranceMgr()->isInternalEditActive();
1493 }
1494
1495 const QMap<PartSet_Tools::ConstraintVisibleState, bool>& PartSet_SketcherMgr::showConstraintStates()
1496 {
1497   return myIsConstraintsShown;
1498 }
1499
1500 bool PartSet_SketcherMgr::isObjectOfSketch(const ObjectPtr& theObject) const
1501 {
1502   bool isFoundObject = false;
1503
1504   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
1505   if (anObjectFeature.get()) {
1506     int aSize = myCurrentSketch->numberOfSubs();
1507     for (int i = 0; i < aSize && !isFoundObject; i++) {
1508       FeaturePtr aCurrentFeature = myCurrentSketch->subFeature(i);
1509       isFoundObject = myCurrentSketch->subFeature(i) == anObjectFeature;
1510     }
1511   }
1512   return isFoundObject;
1513 }
1514
1515 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePlane)
1516 {
1517   Handle(SelectMgr_Filter) aFilter = myModule->selectionFilter(SF_SketchPlaneFilter);
1518   if (!aFilter.IsNull())
1519     Handle(ModuleBase_ShapeInPlaneFilter)::DownCast(aFilter)->setPlane(thePlane);
1520
1521   workshop()->selectionActivate()->updateSelectionModes();
1522 }
1523
1524 bool PartSet_SketcherMgr::setDistanceValueByPreselection(ModuleBase_Operation* theOperation,
1525                                                          ModuleBase_IWorkshop* theWorkshop,
1526                                                          bool& theCanCommitOperation)
1527 {
1528   bool isValueAccepted = false;
1529   theCanCommitOperation = false;
1530
1531   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1532                                                                               (theOperation);
1533   FeaturePtr aFeature = aFOperation->feature();
1534   // editor is shown only if all attribute references are filled by preseletion
1535   bool anAllRefAttrInitialized = true;
1536
1537   std::list<AttributePtr> aRefAttrs = aFeature->data()->attributes(
1538                                               ModelAPI_AttributeRefAttr::typeId());
1539   std::list<AttributePtr>::const_iterator anIt = aRefAttrs.begin(), aLast = aRefAttrs.end();
1540   for (; anIt != aLast && anAllRefAttrInitialized; anIt++) {
1541     anAllRefAttrInitialized = (*anIt)->isInitialized();
1542   }
1543   if (anAllRefAttrInitialized) {
1544     // Activate dimension value editing on double click
1545     ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
1546     QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
1547     // Find corresponded widget to activate value editing
1548     foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
1549       if (aWgt->attributeID() == "ConstraintValue") {
1550         // the featue should be displayed in order to find the AIS text position,
1551         // the place where the editor will be shown
1552         aFeature->setDisplayed(true);
1553         /// the execute is necessary to perform in the feature compute for flyout position
1554         aFeature->execute();
1555
1556         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
1557         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1558
1559         PartSet_WidgetEditor* anEditor = dynamic_cast<PartSet_WidgetEditor*>(aWgt);
1560         if (anEditor) {
1561           int aX = 0, anY = 0;
1562
1563           XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(theWorkshop);
1564           XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1565           AISObjectPtr anAIS = aDisplayer->getAISObject(aFeature);
1566           Handle(AIS_InteractiveObject) anAISIO;
1567           if (anAIS.get() != NULL) {
1568             anAISIO = anAIS->impl<Handle(AIS_InteractiveObject)>();
1569           }
1570           if (anAIS.get() != NULL) {
1571             Handle(AIS_InteractiveObject) anAISIO = anAIS->impl<Handle(AIS_InteractiveObject)>();
1572
1573             if (!anAISIO.IsNull()) {
1574               Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAISIO);
1575               if (!aDim.IsNull()) {
1576                 gp_Pnt aPosition = aDim->GetTextPosition();
1577
1578                 ModuleBase_IViewer* aViewer = aWorkshop->viewer();
1579                 Handle(V3d_View) aView = aViewer->activeView();
1580                 int aCX, aCY;
1581                 aView->Convert(aPosition.X(), aPosition.Y(), aPosition.Z(), aCX, aCY);
1582
1583                 QWidget* aViewPort = aViewer->activeViewPort();
1584                 QPoint aGlPoint = aViewPort->mapToGlobal(QPoint(aCX, aCY));
1585                 aX = aGlPoint.x();
1586                 anY = aGlPoint.y();
1587               }
1588             }
1589             anEditor->setCursorPosition(aX, anY);
1590             isValueAccepted = anEditor->showPopupEditor(false);
1591             theCanCommitOperation = true;
1592           }
1593         }
1594       }
1595     }
1596   }
1597   return isValueAccepted;
1598 }
1599
1600 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
1601                                              const FeaturePtr& theSketch,
1602                                              ModuleBase_IWorkshop* theWorkshop,
1603                                              const FeatureToSelectionMap& theSelection,
1604                                              SelectMgr_IndexedMapOfOwner& theOwnersToSelect)
1605 {
1606   if (theFeature.get() == NULL)
1607     return;
1608
1609   FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
1610   SelectionInfo anInfo = anIt.value();
1611   std::set<AttributePtr> aSelectedAttributes = anInfo.myAttributes;
1612   std::set<ResultPtr> aSelectedResults = anInfo.myResults;
1613
1614   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
1615
1616   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
1617   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1618
1619   // 1. found the feature's owners. Check the AIS objects of the constructions
1620   AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
1621   if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
1622     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1623
1624     SelectMgr_IndexedMapOfOwner aSelectedOwners;
1625     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1626     for  (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
1627       Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
1628       if (!anOwner.IsNull())
1629         theOwnersToSelect.Add(anOwner);
1630     }
1631   }
1632
1633   // 2. found the feature results's owners
1634   std::list<ResultPtr> aResults = theFeature->results();
1635   std::list<ResultPtr>::const_iterator aIt;
1636
1637   bool isSameShape = false;
1638   if (aResults.size() > 0) {
1639     ResultPtr aFirstResult = theFeature->firstResult();
1640     if (aFirstResult.get() && aFirstResult->shape().get()) {
1641       TopoDS_Shape aFirstShape = aFirstResult->shape()->impl<TopoDS_Shape>();
1642       isSameShape = aFirstShape.IsEqual(anInfo.myFirstResultShape);
1643     }
1644   }
1645   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1646     ResultPtr aResult = *aIt;
1647     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
1648     if (aAISObj.get() == NULL)
1649       continue;
1650     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1651
1652     SelectMgr_IndexedMapOfOwner aSelectedOwners;
1653     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1654     bool aFoundLocalShape = false;
1655     for  ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
1656       Handle(StdSelect_BRepOwner) anOwner =
1657         Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
1658       if ( anOwner.IsNull() || !anOwner->HasShape() || theOwnersToSelect.FindIndex(anOwner))
1659         continue;
1660       const TopoDS_Shape& aShape = anOwner->Shape();
1661       TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
1662       if (aShapeType == TopAbs_VERTEX) {
1663         AttributePtr aPntAttr =
1664           PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
1665         if (aPntAttr.get() != NULL &&
1666             aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end())
1667           theOwnersToSelect.Add(anOwner);
1668         else if (isSameShape && anInfo.myLocalSelectedShapes.Contains(aShape)) {
1669           theOwnersToSelect.Add(anOwner);
1670         }
1671       }
1672       else if (aShapeType == TopAbs_EDGE) {
1673         if (isSameShape && anInfo.myLocalSelectedShapes.Contains(aShape)) {
1674           // try to restore local selection on Shape result
1675           // we can do this only if the shape was not changed
1676           theOwnersToSelect.Add(anOwner);
1677           aFoundLocalShape = true;
1678           break;
1679         }
1680       }
1681     }
1682     if (!aFoundLocalShape) {
1683       // result owners are put in the list of selection only if local selected shapes were not
1684       // found
1685       if (aSelectedResults.find(aResult) != aSelectedResults.end()) {
1686         for  ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
1687           Handle(StdSelect_BRepOwner) anOwner =
1688             Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
1689           if ( anOwner.IsNull() || !anOwner->HasShape() || theOwnersToSelect.FindIndex(anOwner))
1690             continue;
1691             // select whole result
1692             theOwnersToSelect.Add(anOwner);
1693         }
1694       }
1695     }
1696   }
1697 }
1698
1699 void PartSet_SketcherMgr::connectToPropertyPanel(ModuleBase_ModelWidget* theWidget,
1700                                                  const bool isToConnect)
1701 {
1702   //Temporary commented as we do not modify values in property panel
1703   if (isToConnect) {
1704     //connect(theWidget, SIGNAL(beforeValuesChanged()),
1705     //        this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1706     //connect(theWidget, SIGNAL(afterValuesChanged()),
1707     //        this, SLOT(onAfterValuesChangedInPropertyPanel()));
1708     connect(theWidget, SIGNAL(afterValuesChanged()),
1709             myModule->sketchReentranceMgr(), SLOT(onAfterValuesChangedInPropertyPanel()));
1710   }
1711   else {
1712     //disconnect(theWidget, SIGNAL(beforeValuesChanged()),
1713     //            this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1714     //disconnect(theWidget, SIGNAL(afterValuesChanged()),
1715     //            this, SLOT(onAfterValuesChangedInPropertyPanel()));
1716     disconnect(theWidget, SIGNAL(afterValuesChanged()),
1717                myModule->sketchReentranceMgr(), SLOT(onAfterValuesChangedInPropertyPanel()));
1718   }
1719 }
1720
1721 void PartSet_SketcherMgr::widgetStateChanged(int thePreviousState)
1722 {
1723   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1724                                                                            (getCurrentOperation());
1725   if (aFOperation) {
1726     if (PartSet_SketcherMgr::isSketchOperation(aFOperation) ||
1727         isNestedSketchOperation(aFOperation) &&
1728         thePreviousState == ModuleBase_ModelWidget::ModifiedInPP) {
1729       FeaturePtr aFeature = aFOperation->feature();
1730       visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature));
1731     }
1732   }
1733 }
1734
1735 void PartSet_SketcherMgr::customizePresentation(const ObjectPtr& theObject)
1736 {
1737   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1738                                                                            (getCurrentOperation());
1739   if (aFOperation && (PartSet_SketcherMgr::isSketchOperation(aFOperation) ||
1740                       isNestedSketchOperation(aFOperation)))
1741     SketcherPrs_Tools::sendExpressionShownEvent(myIsConstraintsShown[PartSet_Tools::Expressions]);
1742
1743   // update entities selection priorities
1744   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1745   if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
1746     // update priority for feature
1747     updateSelectionPriority(aFeature, aFeature);
1748     // update priority for results of the feature
1749     std::list<ResultPtr> aResults = aFeature->results();
1750     std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLastIt = aResults.end();
1751     for (; anIt != aLastIt; anIt++)
1752       updateSelectionPriority(*anIt, aFeature);
1753   }
1754 }
1755
1756 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1757 {
1758   return myModule->workshop()->currentOperation();
1759 }
1760
1761 //**************************************************************
1762 ModuleBase_ModelWidget* PartSet_SketcherMgr::getActiveWidget() const
1763 {
1764   ModuleBase_ModelWidget* aWidget = 0;
1765   ModuleBase_Operation* anOperation = getCurrentOperation();
1766   if (anOperation) {
1767     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
1768     if (aPanel)
1769       aWidget = aPanel->activeWidget();
1770   }
1771   return aWidget;
1772 }
1773
1774 void PartSet_SketcherMgr::visualizeFeature(const FeaturePtr& theFeature,
1775                                            const bool isEditOperation,
1776                                            const bool isToDisplay,
1777                                            const bool isFlushRedisplay)
1778 {
1779   #ifdef DEBUG_DO_NOT_BY_ENTER
1780   return;
1781   #endif
1782
1783   if (isEditOperation || !theFeature.get())
1784     return;
1785
1786   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1787   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1788
1789   // 1. change visibility of the object itself, here the presentable object is processed,
1790   // e.g. constraints features
1791   //FeaturePtr aFeature = aFOperation->feature();
1792   std::list<ResultPtr> aResults = theFeature->results();
1793   if (isToDisplay)
1794     theFeature->setDisplayed(true);
1795   else
1796     theFeature->setDisplayed(false);
1797
1798   // change visibility of the object results, e.g. non-constraint features
1799   std::list<ResultPtr>::const_iterator aIt;
1800   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1801     if (isToDisplay) {
1802       (*aIt)->setDisplayed(true);
1803     }
1804     else {
1805       (*aIt)->setDisplayed(false);
1806     }
1807   }
1808   if (isFlushRedisplay)
1809     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1810 }
1811
1812 void PartSet_SketcherMgr::storeSelection(const SelectionType theType,
1813                         PartSet_SketcherMgr::FeatureToSelectionMap& theCurrentSelection)
1814 {
1815   if (!myCurrentSketch.get())
1816     return;
1817
1818   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1819   ModuleBase_ISelection* aSelect = aWorkshop->selection();
1820   QList<ModuleBase_ViewerPrsPtr> aStoredPrs;
1821
1822   if (theType == ST_HighlightType || theType == ST_SelectAndHighlightType)
1823     aStoredPrs = aSelect->getHighlighted();
1824
1825   QList<FeaturePtr> aFeatureList;
1826   if (theType == ST_SelectAndHighlightType || theType == ST_SelectType) {
1827     QList<ModuleBase_ViewerPrsPtr> aSelected = aSelect->getSelected(
1828                                                               ModuleBase_ISelection::AllControls);
1829     aStoredPrs.append(aSelected);
1830   }
1831
1832   // 1. it is necessary to save current selection in order to restore it after the features moving
1833   theCurrentSelection.clear();
1834
1835   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = aStoredPrs.begin(),
1836                                                 aLast = aStoredPrs.end();
1837
1838   CompositeFeaturePtr aSketch = activeSketch();
1839   for (; anIt != aLast; anIt++) {
1840     ModuleBase_ViewerPrsPtr aPrs = *anIt;
1841     ObjectPtr anObject = aPrs->object();
1842     if (!anObject.get())
1843       continue;
1844
1845     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1846     FeaturePtr aFeature;
1847     if (aResult.get())
1848       aFeature = ModelAPI_Feature::feature(aResult);
1849     else
1850       aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
1851
1852     if (!aFeature.get())
1853       continue;
1854
1855     std::set<AttributePtr> aSelectedAttributes;
1856     std::set<ResultPtr> aSelectedResults;
1857     SelectionInfo anInfo;
1858     if (theCurrentSelection.find(aFeature) != theCurrentSelection.end())
1859       anInfo = theCurrentSelection.find(aFeature).value();
1860
1861     TopoDS_Shape aFirstShape;
1862     ResultPtr aFirstResult = aFeature->firstResult();
1863     if (aFirstResult.get() && aFirstResult->shape().get())
1864       aFirstShape = aFirstResult->shape()->impl<TopoDS_Shape>();
1865     anInfo.myFirstResultShape = aFirstShape;
1866     Handle(SelectMgr_EntityOwner) anOwner = aPrs->owner();
1867     if (aResult.get()) {
1868       getAttributesOrResults(anOwner, aFeature, aSketch, aResult,
1869           anInfo.myAttributes, anInfo.myResults, anInfo.myLocalSelectedShapes);
1870     }
1871     else {
1872       std::list<ResultPtr> aResults = aFeature->results();
1873       std::list<ResultPtr>::const_iterator aIt;
1874       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1875         ResultPtr aResult = *aIt;
1876         getAttributesOrResults(anOwner, aFeature, aSketch, aResult,
1877           anInfo.myAttributes, anInfo.myResults, anInfo.myLocalSelectedShapes);
1878       }
1879     }
1880     theCurrentSelection[aFeature] = anInfo;
1881   }
1882   //qDebug(QString("  storeSelection: %1").arg(theCurrentSelection.size()).toStdString().c_str());
1883 }
1884
1885 void PartSet_SketcherMgr::restoreSelection(
1886                                 PartSet_SketcherMgr::FeatureToSelectionMap& theCurrentSelection)
1887 {
1888   if (!myCurrentSketch.get())
1889     return;
1890
1891   //qDebug(QString("restoreSelection: %1").arg(theCurrentSelection.size()).toStdString().c_str());
1892   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1893   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1894   FeatureToSelectionMap::const_iterator aSIt = theCurrentSelection.begin(),
1895                                         aSLast = theCurrentSelection.end();
1896   SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1897   anOwnersToSelect.Clear();
1898   for (; aSIt != aSLast; aSIt++) {
1899     getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, theCurrentSelection,
1900                        anOwnersToSelect);
1901   }
1902   aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
1903 }
1904
1905 void PartSet_SketcherMgr::onShowConstraintsToggle(int theType, bool theState)
1906 {
1907   PartSet_Tools::ConstraintVisibleState aType = (PartSet_Tools::ConstraintVisibleState)theType;
1908
1909   updateBySketchParameters(aType, theState);
1910 }
1911
1912 void PartSet_SketcherMgr::updateBySketchParameters(
1913                                    const PartSet_Tools::ConstraintVisibleState& theType,
1914                                    bool theState)
1915 {
1916   if (myCurrentSketch.get() == NULL)
1917     return;
1918
1919   bool aPrevState = myIsConstraintsShown[theType];
1920   myIsConstraintsShown[theType] = theState;
1921
1922   switch (theType) {
1923     case PartSet_Tools::Geometrical:
1924     case PartSet_Tools::Dimensional: {
1925       if (aPrevState != theState) {
1926         ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1927         XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1928         int aNumberOfSubs = myCurrentSketch->numberOfSubs();
1929         for (int i = 0; i < aNumberOfSubs; i++) {
1930           FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
1931           bool aProcessed = false;
1932           bool aConstraintDisplayed = canDisplayConstraint(aSubFeature, theType, aProcessed);
1933           if (aProcessed)
1934             aSubFeature->setDisplayed(aConstraintDisplayed);
1935         }
1936         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1937       }
1938     }
1939     break;
1940     case PartSet_Tools::Expressions: {
1941       if (aPrevState != theState) {
1942         /// call all sketch features redisplay, the expression state will be corrected in customize
1943         /// of distance presentation
1944         Events_ID anEventId = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1945         PartSet_Tools::sendSubFeaturesEvent(myCurrentSketch, anEventId);
1946       }
1947     }
1948     break;
1949   }
1950 }
1951
1952 void PartSet_SketcherMgr::updateSelectionPriority(ObjectPtr theObject,
1953                                                   FeaturePtr theFeature)
1954 {
1955   if (!theObject.get() || !theFeature.get())
1956     return;
1957
1958   AISObjectPtr anAIS = workshop()->displayer()->getAISObject(theObject);
1959   Handle(AIS_InteractiveObject) anAISIO;
1960   if (anAIS.get() != NULL) {
1961     anAISIO = anAIS->impl<Handle(AIS_InteractiveObject)>();
1962   }
1963
1964   if (!anAISIO.IsNull()) { // the presentation for the object is visualized
1965     int anAdditionalPriority = 0;
1966     // current feature
1967     std::shared_ptr<SketchPlugin_Feature> aSPFeature =
1968             std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
1969     if (aSPFeature.get() != NULL) {
1970       // 1. Vertices
1971       // 2. Simple segments
1972       // 3. External objects (violet color)
1973       // 4. Auxiliary segments (dotted)
1974       // StdSelect_BRepSelectionTool::Load uses priority calculating:
1975       // Standard_Integer aPriority =
1976       // (thePriority == -1) ? GetStandardPriority (theShape, theType) : thePriority;
1977       // Priority of Vertex is 8, edge(segment) is 7.
1978       // It might be not corrected as provides the condition above.
1979       bool isExternal = aSPFeature->isExternal();
1980       bool isAuxiliary = PartSet_Tools::isAuxiliarySketchEntity(aSPFeature);
1981       // current feature
1982       if (!isExternal && !isAuxiliary)
1983         anAdditionalPriority = 30;
1984       // external feature
1985       if (isExternal)
1986         anAdditionalPriority = 20;
1987       // auxiliary feature
1988       if (isAuxiliary) {
1989         anAdditionalPriority = 10; /// auxiliary objects should have less priority that
1990         // edges/vertices of local selection on not-sketch objects
1991       }
1992       Handle(ModuleBase_ResultPrs) aResult = Handle(ModuleBase_ResultPrs)::DownCast(anAISIO);
1993       if (!aResult.IsNull()) {
1994         aResult->setAdditionalSelectionPriority(anAdditionalPriority);
1995       }
1996     }
1997   }
1998 }
1999
2000 XGUI_Workshop* PartSet_SketcherMgr::workshop() const
2001 {
2002   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
2003   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
2004   return aConnector->workshop();
2005 }
2006
2007 XGUI_OperationMgr* PartSet_SketcherMgr::operationMgr() const
2008 {
2009   return workshop()->operationMgr();
2010 }
2011
2012 void PartSet_SketcherMgr::onShowPoints(bool toShow)
2013 {
2014   if (!myCurrentSketch.get())
2015     return;
2016   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
2017   ModuleBase_IViewer* aViewer = aWorkshop->viewer();
2018   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
2019
2020   bool aToUpdate = false;
2021   if (toShow) {
2022     std::list<ResultPtr> aFreePoints = SketcherPrs_Tools::getFreePoints(myCurrentSketch);
2023
2024     // Delete obsolete presentations
2025     std::list<ResultPtr> aDelList;
2026     foreach(ResultPtr aObj, myPointsHighlight.keys()) {
2027       bool aFound = (std::find(aFreePoints.begin(), aFreePoints.end(), aObj) != aFreePoints.end());
2028       if (!aFound)
2029         aDelList.push_back(aObj);
2030     }
2031     foreach(ResultPtr aObj, aDelList) {
2032       aContext->Remove(myPointsHighlight[aObj], false);
2033       aToUpdate = true;
2034       myPointsHighlight.remove(aObj);
2035     }
2036
2037     // Display new objects
2038     QList<ResultPtr> aKeysList = myPointsHighlight.keys();
2039     std::list<ResultPtr>::const_iterator aIt;
2040     for (aIt = aFreePoints.cbegin(); aIt != aFreePoints.cend(); aIt++) {
2041       if (!aKeysList.contains(*aIt)) {
2042         GeomShapePtr aShapePtr = (*aIt)->shape();
2043         TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
2044         Handle(AIS_Shape) aShapePrs = new AIS_Shape(aShape);
2045         aShapePrs->SetColor(Quantity_NOC_BLUE1);
2046         aShapePrs->SetZLayer(Graphic3d_ZLayerId_Top);
2047         Handle(Prs3d_Drawer) aDrawer = aShapePrs->Attributes();
2048         if (aDrawer->HasOwnPointAspect()) {
2049           aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
2050           aDrawer->PointAspect()->SetColor(Quantity_NOC_BLUE1);
2051           aDrawer->PointAspect()->SetScale(2);
2052         }
2053         else
2054           aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_O_STAR, Quantity_NOC_BLUE1, 2));
2055         aContext->Display(aShapePrs, false);
2056         aContext->Deactivate(aShapePrs);
2057         myPointsHighlight[*aIt] = aShapePrs;
2058         aToUpdate = true;
2059       }
2060     }
2061   }
2062   else {
2063     foreach(Handle(AIS_Shape) aPrs, myPointsHighlight.values()) {
2064       aContext->Remove(aPrs, false);
2065       aToUpdate = true;
2066     }
2067     myPointsHighlight.clear();
2068   }
2069   if (aToUpdate)
2070     aViewer->update();
2071 }