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