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