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