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