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