Salome HOME
default values for the constraint point in order to enable the "Apply" button
[modules/shaper.git] / src / PartSet / PartSet_SketcherMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_SketcherMgr.cpp
4 // Created:     19 Dec 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "PartSet_SketcherMgr.h"
8 #include "PartSet_Module.h"
9 #include "PartSet_WidgetPoint2d.h"
10 #include "PartSet_Tools.h"
11
12 #include <XGUI_ModuleConnector.h>
13 #include <XGUI_Displayer.h>
14 #include <XGUI_Workshop.h>
15 #include <XGUI_Selection.h>
16 #include <XGUI_SelectionMgr.h>
17 #include <ModuleBase_ModelWidget.h>
18 #include <XGUI_ModuleConnector.h>
19 #include <XGUI_PropertyPanel.h>
20
21 #include <ModuleBase_IViewer.h>
22 #include <ModuleBase_IWorkshop.h>
23 #include <ModuleBase_IViewWindow.h>
24 #include <ModuleBase_Operation.h>
25 #include <ModuleBase_ISelection.h>
26 #include <ModuleBase_IPropertyPanel.h>
27 #include <ModuleBase_Operation.h>
28
29 #include <GeomDataAPI_Point2D.h>
30
31 #include <Events_Loop.h>
32
33 #include <SketchPlugin_Line.h>
34 #include <SketchPlugin_Sketch.h>
35 #include <SketchPlugin_Point.h>
36 #include <SketchPlugin_Arc.h>
37 #include <SketchPlugin_Circle.h>
38 #include <SketchPlugin_ConstraintLength.h>
39 #include <SketchPlugin_ConstraintDistance.h>
40 #include <SketchPlugin_ConstraintParallel.h>
41 #include <SketchPlugin_ConstraintPerpendicular.h>
42 #include <SketchPlugin_ConstraintRadius.h>
43 #include <SketchPlugin_ConstraintRigid.h>
44
45 #include <SelectMgr_IndexedMapOfOwner.hxx>
46 #include <StdSelect_BRepOwner.hxx>
47
48 //#include <AIS_DimensionSelectionMode.hxx>
49 //#include <AIS_Shape.hxx>
50
51 #include <ModelAPI_Events.h>
52
53 #include <QMouseEvent>
54 #include <QApplication>
55
56
57 /// Returns list of unique objects by sum of objects from List1 and List2
58 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
59                                        const QList<ModuleBase_ViewerPrs>& theList2)
60 {
61   QList<ModuleBase_ViewerPrs> aRes;
62   foreach (ModuleBase_ViewerPrs aPrs, theList1) {
63     if (!aRes.contains(aPrs))
64       aRes.append(aPrs);
65   }
66   foreach (ModuleBase_ViewerPrs aPrs, theList2) {
67     if (!aRes.contains(aPrs))
68       aRes.append(aPrs);
69   }
70   return aRes;
71 }*/
72
73 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
74                            QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
75                            const FeaturePtr theSketch)
76 {
77   QList<ModuleBase_ViewerPrs> aRes;
78
79   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
80                                               aLast = theList.end();
81   for (; anIt != aLast; anIt++)
82   {
83     ModuleBase_ViewerPrs aPrs = *anIt;
84     FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
85     if (aFeature.get() == NULL)
86       continue;
87
88     QList<AttributePtr> anAttributes;
89     if (theFeature2AttributeMap.contains(aFeature)) {
90       anAttributes = theFeature2AttributeMap[aFeature];
91     }
92     AttributePtr anAttr;
93     TopoDS_Shape aShape = aPrs.shape();
94     if (!aShape.IsNull()) {
95       if (aShape.ShapeType() == TopAbs_VERTEX) {
96         anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
97         if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
98           anAttributes.push_back(anAttr);
99       }
100     }
101     theFeature2AttributeMap[aFeature] = anAttributes;
102   }
103 }
104
105
106
107
108 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
109   : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false)
110 {
111   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
112   ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
113
114   myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
115
116   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
117           this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
118
119   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
120           this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
121
122   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
123           this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
124
125   connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
126           this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
127
128   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
129   XGUI_Workshop* aWorkshop = aConnector->workshop();
130   connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
131 }
132
133 PartSet_SketcherMgr::~PartSet_SketcherMgr()
134 {
135   if (!myPlaneFilter.IsNull())
136     myPlaneFilter.Nullify();
137 }
138
139 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
140 {
141   get2dPoint(theWnd, theEvent, myClickedPoint);
142
143   if (!(theEvent->buttons() & Qt::LeftButton))
144     return;
145
146   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
147   ModuleBase_Operation* aOperation = aWorkshop->currentOperation();
148   if (aOperation && aOperation->isEditOperation()) {
149     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
150     ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
151     // If the current widget is a selector, do do nothing, it processes the mouse press
152     if(aActiveWgt && aActiveWgt->isViewerSelector()) {
153       return;
154     }
155   }
156
157   // Clear dragging mode
158   myIsDragging = false;
159
160   // Use only for sketch operations
161   if (aOperation && myCurrentSketch) {
162     if (!PartSet_Tools::sketchPlane(myCurrentSketch))
163       return;
164
165     bool isSketcher = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
166     bool isSketchOpe = sketchOperationIdList().contains(aOperation->id());
167
168     // Avoid non-sketch operations
169     if ((!isSketchOpe) && (!isSketcher))
170       return;
171
172     bool isEditing = aOperation->isEditOperation();
173
174     // Ignore creation sketch operation
175     if ((!isSketcher) && (!isEditing))
176       return;
177
178     // MoveTo in order to highlight current object
179     ModuleBase_IViewer* aViewer = aWorkshop->viewer();
180     aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
181
182     // Remember highlighted objects for editing
183     ModuleBase_ISelection* aSelect = aWorkshop->selection();
184     QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
185     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
186     myFeature2AttributeMap.clear();
187
188     bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
189     if (aHasShift) {
190       fillFeature2Attribute(aHighlighted, myFeature2AttributeMap, myCurrentSketch);
191       fillFeature2Attribute(aSelected, myFeature2AttributeMap, myCurrentSketch);
192     }
193     else {
194       fillFeature2Attribute(aHighlighted, myFeature2AttributeMap, myCurrentSketch);
195     }
196
197     if (myFeature2AttributeMap.empty()) {
198       if (isSketchOpe && (!isSketcher))
199         // commit previous operation
200         if (!aOperation->commit())
201           aOperation->abort();
202       return;
203     }
204
205     if (isSketcher) {
206       myIsDragging = true;
207       get2dPoint(theWnd, theEvent, myCurrentPoint);
208       myDragDone = false;
209       launchEditing();
210
211     } else if (isSketchOpe && isEditing) {
212       // If selected another object commit current result
213       aOperation->commit();
214
215       myIsDragging = true;
216       get2dPoint(theWnd, theEvent, myCurrentPoint);
217       myDragDone = false;
218
219       // This is necessary in order to finalize previous operation
220       QApplication::processEvents();
221       launchEditing();
222     }
223   }
224 }
225
226 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
227 {
228   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
229   ModuleBase_Operation* aOp = aWorkshop->currentOperation();
230   if (aOp) {
231     if (sketchOperationIdList().contains(aOp->id())) {
232   get2dPoint(theWnd, theEvent, myClickedPoint);
233
234       // Only for sketcher operations
235       ModuleBase_IViewer* aViewer = aWorkshop->viewer();
236       if (myIsDragging) {
237         if (myDragDone) {
238           //aOp->commit();
239           myFeature2AttributeMap.clear();
240
241           // Reselect edited object
242           /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
243           if (theEvent->modifiers() & Qt::ShiftModifier)
244             aViewer->AISContext()->ShiftSelect();
245           else
246             aViewer->AISContext()->Select();
247             */
248         }
249       }
250     }
251   }
252   aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
253   myIsDragging = false;
254 }
255
256 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
257 {
258   myClickedPoint.clear();
259
260   if (myIsDragging) {
261     ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
262     // 1. it is necessary to save current selection in order to restore it after the features moving
263     FeatureToSelectionMap aCurrentSelection;
264     getCurrentSelection(myFeature2AttributeMap, myCurrentSketch, aWorkshop, aCurrentSelection);
265
266     // 2. the enable selection in the viewer should be temporary switched off in order to ignore
267     // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
268     // deselected). This flag should be restored in the slot, processed the mouse release signal.
269     ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
270     aViewer->enableSelection(false);
271
272     ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
273     if (!aOperation)
274       return;
275     if (aOperation->id().toStdString() == SketchPlugin_Sketch::ID())
276       return; // No edit operation activated
277
278     Handle(V3d_View) aView = theWnd->v3dView();
279     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
280     double aX, aY;
281     PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
282     double dX =  aX - myCurrentPoint.myCurX;
283     double dY =  aY - myCurrentPoint.myCurY;
284
285     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
286     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
287     // 3. the flag to disable the update viewer should be set in order to avoid blinking in the 
288     // viewer happens by deselect/select the modified objects. The flag should be restored after
289     // the selection processing. The update viewer should be also called.
290     bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
291
292     static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
293     //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
294
295     FeatureToAttributesMap::const_iterator anIt = myFeature2AttributeMap.begin(),
296                                            aLast = myFeature2AttributeMap.end();
297     // 4. the features and attributes modification(move)
298     for (; anIt != aLast; anIt++) {
299       FeaturePtr aFeature = anIt.key();
300
301       AttributeList anAttributes = anIt.value();
302       // Process selection by attribute: the priority to the attribute
303       if (!anAttributes.empty()) {
304         AttributeList::const_iterator anAttIt = anAttributes.begin(), anAttLast = anAttributes.end();
305         for (; anAttIt != anAttLast; anAttIt++) {
306           AttributePtr anAttr = *anAttIt;
307           if (anAttr.get() == NULL)
308             continue;
309           std::string aAttrId = anAttr->id();
310           DataPtr aData = aFeature->data();
311           if (aData.get() != NULL) {
312             std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
313               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
314             if (aPoint.get() != NULL) {
315               bool isImmutable = aPoint->setImmutable(true);
316               aPoint->move(dX, dY);
317               ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
318               aPoint->setImmutable(isImmutable);
319             }
320           }
321         }
322       } else {
323         // Process selection by feature
324         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
325           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
326         if (aSketchFeature) {
327           aSketchFeature->move(dX, dY);
328           ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
329         }
330       }
331     }
332     Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
333     //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
334
335     // 5. it is necessary to save current selection in order to restore it after the features moving
336     FeatureToSelectionMap::const_iterator aSIt = aCurrentSelection.begin(),
337                                           aSLast = aCurrentSelection.end();
338     SelectMgr_IndexedMapOfOwner anOwnersToSelect;
339     for (; aSIt != aSLast; aSIt++) {
340       anOwnersToSelect.Clear();
341       getSelectionOwners(aSIt->first, myCurrentSketch, aWorkshop, aCurrentSelection,
342                          anOwnersToSelect);
343       aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
344     }
345
346     // 6. restore the update viewer flag and call this update
347     aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
348     aDisplayer->updateViewer();
349     myDragDone = true;
350     myCurrentPoint.setValue(aX, aY);
351   }
352 }
353
354 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
355 {
356   ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
357   if (aOperation && aOperation->isEditOperation()) {
358     std::string aId = aOperation->id().toStdString();
359     if (isDistanceOperation(aOperation))
360     {
361       // Activate dimension value editing on double click
362       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
363       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
364       // Find corresponded widget to activate value editing
365       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
366         if (aWgt->attributeID() == "ConstraintValue") {
367           aWgt->focusTo();
368           return;
369         }
370       }
371     }
372   }
373 }
374
375 void PartSet_SketcherMgr::onApplicationStarted()
376 {
377   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
378   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
379   XGUI_Workshop* aWorkshop = aConnector->workshop();
380   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
381   if (aPropertyPanel) {
382     connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
383             this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
384   }
385 }
386
387 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
388 {
389   if (!myClickedPoint.myIsInitialized)
390     return;
391
392   ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
393   // the distance constraint feature should not use the clickedd point
394   // this is workaround in order to don't throw down the flyout point value,
395   // set by execute() method of these type of features
396   if (isDistanceOperation(aOperation))
397     return;
398
399   PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
400   if (aPnt2dWgt) {
401     aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
402   }
403 }
404
405 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation) const
406 {
407   std::string aId = theOperation ? theOperation->id().toStdString() : "";
408
409   return (aId == SketchPlugin_ConstraintLength::ID()) ||
410          (aId == SketchPlugin_ConstraintDistance::ID()) ||
411          (aId == SketchPlugin_ConstraintRadius::ID());
412 }
413
414 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
415                                      Point& thePoint)
416 {
417   Handle(V3d_View) aView = theWnd->v3dView();
418   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
419   double aX, anY;
420   PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
421   thePoint.setValue(aX, anY);
422 }
423
424 void PartSet_SketcherMgr::launchEditing()
425 {
426   // there should be activate the vertex selection mode because the edit can happens by the selected
427   // point
428   QIntList aModes;
429   aModes << TopAbs_VERTEX << TopAbs_EDGE;
430   // TODO: #391 - to be uncommented
431   /*aModes.append(AIS_DSM_Text);
432   aModes.append(AIS_DSM_Line);
433   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
434   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));*/
435
436   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
437   aConnector->activateSubShapesSelection(aModes);
438
439   if (!myFeature2AttributeMap.empty()) {
440     FeaturePtr aFeature = myFeature2AttributeMap.begin().key();
441     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
442               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
443     if (aSPFeature) {
444       myModule->editFeature(aSPFeature);
445     }
446   }
447  
448 }
449
450
451 QStringList PartSet_SketcherMgr::sketchOperationIdList()
452 {
453   static QStringList aIds;
454   if (aIds.size() == 0) {
455     aIds << SketchPlugin_Line::ID().c_str();
456     aIds << SketchPlugin_Point::ID().c_str();
457     aIds << SketchPlugin_Arc::ID().c_str();
458     aIds << SketchPlugin_Circle::ID().c_str();
459     aIds << SketchPlugin_ConstraintLength::ID().c_str();
460     aIds << SketchPlugin_ConstraintDistance::ID().c_str();
461     aIds << SketchPlugin_ConstraintRigid::ID().c_str();
462     aIds << SketchPlugin_ConstraintRadius::ID().c_str();
463     aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
464     aIds << SketchPlugin_ConstraintParallel::ID().c_str();
465   }
466   return aIds;
467 }
468
469 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
470 {
471   // Display all sketcher sub-Objects
472   myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
473   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
474   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
475
476   // Hide sketcher result
477   std::list<ResultPtr> aResults = myCurrentSketch->results();
478   std::list<ResultPtr>::const_iterator aIt;
479   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
480     aDisplayer->erase((*aIt), false);
481   }
482   aDisplayer->erase(myCurrentSketch, false);
483
484   // Display sketcher objects
485   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
486     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
487     std::list<ResultPtr> aResults = aFeature->results();
488     std::list<ResultPtr>::const_iterator aIt;
489     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
490       aDisplayer->display((*aIt), false);
491     }
492     aDisplayer->display(aFeature, false);
493   }
494
495   if (myPlaneFilter.IsNull()) 
496     myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
497
498   myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
499   if (theOperation->isEditOperation()) {
500     // If it is editing of sketch then it means that plane is already defined
501     std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
502     myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
503   }
504   aDisplayer->updateViewer();
505 }
506
507 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
508 {
509   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
510   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
511
512   DataPtr aData = myCurrentSketch->data();
513   if ((!aData) || (!aData->isValid())) {
514     // The sketch was aborted
515     myCurrentSketch = CompositeFeaturePtr();
516     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
517
518     // Erase all sketcher objects
519     QStringList aSketchIds = sketchOperationIdList();
520     QObjectPtrList aObjects = aDisplayer->displayedObjects();
521     foreach (ObjectPtr aObj, aObjects) {
522       DataPtr aObjData = aObj->data();
523       if ((!aObjData) || (!aObjData->isValid()))
524         aDisplayer->erase(aObj);
525     }
526     return; 
527   }
528   // Hide all sketcher sub-Objects
529   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
530     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
531     std::list<ResultPtr> aResults = aFeature->results();
532     std::list<ResultPtr>::const_iterator aIt;
533     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
534       aDisplayer->erase((*aIt), false);
535     }
536     aDisplayer->erase(aFeature, false);
537   }
538   // Display sketcher result
539   std::list<ResultPtr> aResults = myCurrentSketch->results();
540   std::list<ResultPtr>::const_iterator aIt;
541   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
542     aDisplayer->display((*aIt), false);
543   }
544   aDisplayer->display(myCurrentSketch);
545     
546   myCurrentSketch = CompositeFeaturePtr();
547   myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
548   aDisplayer->updateViewer();
549 }
550
551
552 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
553 {
554   myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
555 }
556
557 void PartSet_SketcherMgr::getCurrentSelection(const FeatureToAttributesMap& theFeatureToAttributes,
558                                               const FeaturePtr& theSketch,
559                                               ModuleBase_IWorkshop* theWorkshop,
560                                               FeatureToSelectionMap& theSelection)
561 {
562   FeatureToAttributesMap::const_iterator anIt = theFeatureToAttributes.begin(),
563                                          aLast = theFeatureToAttributes.end();
564   for (; anIt != aLast; anIt++) {
565     FeaturePtr aFeature = anIt.key();
566     getCurrentSelection(aFeature, theSketch, theWorkshop, theSelection);
567   }
568 }
569
570 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
571                                               const FeaturePtr& theSketch,
572                                               ModuleBase_IWorkshop* theWorkshop,
573                                               FeatureToSelectionMap& theSelection)
574 {
575   if (theFeature.get() == NULL)
576     return;
577
578   std::set<AttributePtr> aSelectedAttributes;
579   std::set<ResultPtr> aSelectedResults;
580
581   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
582   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
583   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
584   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
585
586   std::list<ResultPtr> aResults = theFeature->results();
587   std::list<ResultPtr>::const_iterator aIt;
588   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
589   {
590     ResultPtr aResult = *aIt;
591     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
592     if (aAISObj.get() == NULL)
593       continue;
594     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
595     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
596     {
597       Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
598                                                                       aContext->SelectedOwner());
599       if (aBRepOwner.IsNull())
600         continue;
601       Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
602                                                                         aBRepOwner->Selectable());
603       if (anIO != anAISIO)
604         continue;
605
606       if (aBRepOwner->HasShape()) {
607         const TopoDS_Shape& aShape = aBRepOwner->Shape();
608         TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
609         if (aShapeType == TopAbs_VERTEX) {
610           AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
611                                                                         aShape, theSketch);
612           if (aPntAttr.get() != NULL)
613             aSelectedAttributes.insert(aPntAttr);
614         }
615         else if (aShapeType == TopAbs_EDGE &&
616                  aSelectedResults.find(aResult) == aSelectedResults.end()) {
617           aSelectedResults.insert(aResult);
618         }
619       }
620     }
621   }
622   theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
623 }
624
625 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
626                                              const FeaturePtr& theSketch,
627                                              ModuleBase_IWorkshop* theWorkshop,
628                                              const FeatureToSelectionMap& theSelection,
629                                              SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
630 {
631   if (theFeature.get() == NULL)
632     return;
633
634   FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
635   std::set<AttributePtr> aSelectedAttributes = anIt->second.first;
636   std::set<ResultPtr> aSelectedResults = anIt->second.second;
637
638   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
639   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
640   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
641   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
642
643   std::list<ResultPtr> aResults = theFeature->results();
644   std::list<ResultPtr>::const_iterator aIt;
645   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
646   {
647     ResultPtr aResult = *aIt;
648     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
649     if (aAISObj.get() == NULL)
650       continue; 
651     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
652
653     SelectMgr_IndexedMapOfOwner aSelectedOwners;  
654     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
655     for  ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
656       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
657       if ( anOwner.IsNull() || !anOwner->HasShape() )
658         continue;
659       const TopoDS_Shape& aShape = anOwner->Shape();
660       TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
661       if (aShapeType == TopAbs_VERTEX) {
662         AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
663         if (aPntAttr.get() != NULL &&
664             aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
665           anOwnersToSelect.Add(anOwner);
666         }
667       }
668       else if (aShapeType == TopAbs_EDGE) {
669         bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
670         if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
671             anOwnersToSelect.FindIndex(anOwner) <= 0)
672           anOwnersToSelect.Add(anOwner);
673       }
674     }
675   }
676 }