Salome HOME
64820f7cd3612094d4a357f62c79524f3e98193b
[modules/shaper.git] / src / XGUI / XGUI_Selection.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "XGUI_Selection.h"
21 #include "XGUI_Workshop.h"
22 #include "XGUI_Displayer.h"
23 #include "XGUI_ViewerProxy.h"
24 #include "XGUI_ObjectsBrowser.h"
25
26 #include "ModuleBase_BRepOwner.h"
27 #include "ModuleBase_ResultPrs.h"
28 #include "ModuleBase_ViewerPrs.h"
29
30 #include <ModelAPI_Feature.h>
31 #include <ModelAPI_Tools.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_ResultBody.h>
34 #include <ModelAPI_ResultConstruction.h>
35
36 #include <AIS_InteractiveContext.hxx>
37 #ifdef BEFORE_TRIHEDRON_PATCH
38 #include <AIS_Axis.hxx>
39 #include <AIS_Point.hxx>
40 #else
41 #include <AIS_TrihedronOwner.hxx>
42 #endif
43 #include <Geom_Line.hxx>
44 #include <BRep_Builder.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <Geom_Point.hxx>
47 #include <Geom_TrimmedCurve.hxx>
48 #include <Prs3d_DatumAspect.hxx>
49
50 #include <TColStd_ListIteratorOfListOfInteger.hxx>
51 #include <StdSelect_BRepOwner.hxx>
52
53 #include <set>
54
55 #define DEBUG_DELIVERY
56
57 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
58     : myWorkshop(theWorkshop)
59 {
60 }
61
62 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
63 {
64   QList<ModuleBase_ViewerPrsPtr> aPresentations;
65   QList<ModuleBase_ViewerPrsPtr> aToRemove;
66
67   switch (thePlace) {
68     case Browser:
69       getSelectedInBrowser(aPresentations);
70     break;
71     case Viewer:
72       getSelectedInViewer(aPresentations);
73     break;
74   case AllControls:
75     // Get selection from object browser
76     getSelectedInBrowser(aPresentations);
77
78     // Filter out all objects except feature if there is no selected results in object browser
79     // Filter out all features if in object browser there are selected features and their results
80     bool aHasFeature = false;
81     bool aHasResult = false;
82     foreach(ModuleBase_ViewerPrsPtr aVal, aPresentations) {
83       if (aVal->object().get()) {
84         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aVal->object());
85         if (aFeature.get()) {
86           aHasFeature = true;
87           std::list<ResultPtr> aResList = aFeature->results();
88           std::list<ResultPtr>::const_iterator aIt;
89           for (aIt = aResList.cbegin(); aIt != aResList.cend(); aIt++) {
90             foreach(ModuleBase_ViewerPrsPtr aSel, aPresentations) {
91               if (aSel->object() == (*aIt)) {
92                 aHasResult = true;
93                 break;
94               }
95             }
96             if (aHasResult)
97               break;
98           }
99         }
100       }
101       if (aHasFeature && aHasResult)
102         break;
103     }
104     //Get selection from a viewer
105     getSelectedInViewer(aPresentations);
106
107     // Filter out extra objects
108     if (aHasFeature && aHasResult) {
109       foreach(ModuleBase_ViewerPrsPtr aVal, aPresentations) {
110         if (aVal->object().get()) {
111           FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aVal->object());
112           if (aFeature.get()) {
113             aToRemove.append(aVal);
114           }
115         }
116       }
117     }
118     else if (aHasFeature && (!aHasResult)) {
119       foreach(ModuleBase_ViewerPrsPtr aVal, aPresentations) {
120         if (aVal->object().get()) {
121           FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aVal->object());
122           if (!aFeature.get()) {
123             aToRemove.append(aVal);
124           }
125         }
126       }
127     }
128     foreach(ModuleBase_ViewerPrsPtr aVal, aToRemove) {
129       aPresentations.removeAll(aVal);
130     }
131     break;
132   }
133   return aPresentations;
134 }
135
136 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPtr& thePrs)
137 {
138   Handle(AIS_InteractiveObject) anIO = thePrs->interactive();
139   if (anIO.IsNull()) {
140     Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
141     if (!anOwner.IsNull())
142       anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
143
144     if (anIO.IsNull() && thePrs->object()) {
145       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
146       AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs->object());
147       if (anAISObject.get())
148         anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
149     }
150   }
151   return anIO;
152 }
153
154 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
155 {
156   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
157   if (!aContext.IsNull()) {
158     QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
159     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
160       ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
161       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
162
163       if (aSelectedIds.contains((size_t)anOwner.get()))
164         continue;
165       aSelectedIds.append((size_t)anOwner.get());
166
167       fillPresentation(aPrs, anOwner);
168
169       if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
170         thePresentations.append(aPrs);
171     }
172   }
173 }
174
175 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
176 {
177   // collect the objects  of the parameter presentation to avoid a repeted objects in the result
178   QObjectPtrList aPresentationObjects;
179   QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = thePresentations.begin(),
180                                               aPrsLast = thePresentations.end();
181   for (; aPrsIt != aPrsLast; aPrsIt++) {
182     aPresentationObjects.push_back((*aPrsIt)->object());
183   }
184
185   QObjectPtrList anObjects = selectedObjects();
186   QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
187   for (; anIt != aLast; anIt++) {
188     ObjectPtr anObject = *anIt;
189     if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
190       thePresentations.append(std::shared_ptr<ModuleBase_ViewerPrs>(
191                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
192     }
193   }
194 }
195
196 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
197                                       const Handle(SelectMgr_EntityOwner)& theOwner) const
198 {
199   thePrs->setOwner(theOwner);
200   Handle(AIS_InteractiveObject) anIO =
201                            Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
202   thePrs->setInteractive(anIO);
203
204   // we should not check the appearance of this feature because there can be some selected shapes
205   // for one feature
206   Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
207   if( !aBRO.IsNull() && aBRO->HasShape() ) {
208     TopoDS_Shape aShape = aBRO->Shape();
209     Handle(ModuleBase_ResultPrs) aPrsObj =
210       Handle(ModuleBase_ResultPrs)::DownCast(aBRO->Selectable());
211     if (!aPrsObj.IsNull()) {
212       if (aPrsObj->isSubstituted()) {
213         if (aPrsObj->Shape().IsSame(aShape))
214           aShape = aPrsObj->originalShape();
215       }
216     }
217     // the located method is called in the context to obtain the shape by the SelectedShape()
218     // method, so the shape is located by the same rules
219     aShape = aShape.Located(aBRO->Location() * aShape.Location());
220 #ifdef BEFORE_TRIHEDRON_PATCH
221 #ifndef DEBUG_DELIVERY
222     if (aShape.IsNull())
223       aShape = findAxisShape(anIO);
224 #endif
225 #endif
226     if (!aShape.IsNull()) {
227       std::shared_ptr<GeomAPI_Shape> aGeomShape =
228         std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
229       aGeomShape->setImpl(new TopoDS_Shape(aShape));
230       thePrs->setShape(aGeomShape);
231     }
232   } else {
233 #ifdef DEBUG_DELIVERY
234     // Fill by trihedron shapes
235 #ifdef BEFORE_TRIHEDRON_PATCH
236     Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
237     DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
238     int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
239     ObjectPtr aObj;
240     if (!aAxis.IsNull()) {
241       // an Axis from Trihedron
242       gp_Lin aLine = aAxis->Component()->Lin();
243       gp_Dir aDir = aLine.Direction();
244       std::string aAxName;
245       if (aDir.X() == 1.)
246         aAxName = "OX";
247       else if (aDir.Y() == 1.)
248         aAxName = "OY";
249       else if (aDir.Z() == 1.)
250         aAxName = "OZ";
251       if (aAxName.length() > 0) {
252         ResultPtr aAx;
253         for (int i = 0; i < aSize; i++) {
254           aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
255           if (aObj->data()->name() == aAxName) {
256             aAx = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
257             break;
258           }
259         }
260         if (aAx.get()) {
261           thePrs->setObject(aAx);
262           thePrs->setShape(aAx->shape());
263           return;
264         }
265       }
266     } else {
267       Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
268       if (!aPoint.IsNull()) {
269         // An origin point from trihedron
270         ResultPtr aOrigin;
271         for (int i = 0; i < aSize; i++) {
272           aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
273           if (aObj->data()->name() == "Origin") {
274             aOrigin = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
275             break;
276           }
277         }
278         if (aOrigin.get()) {
279           thePrs->setObject(aOrigin);
280           thePrs->setShape(aOrigin->shape());
281           return;
282         }
283       }
284     }
285 #else
286     /// find axis direction
287     Handle(AIS_TrihedronOwner) aTrihedronOwner = Handle(AIS_TrihedronOwner)::DownCast(theOwner);
288     if (!aTrihedronOwner.IsNull()) {
289       const Prs3d_DatumParts& aPart = aTrihedronOwner->DatumPart();
290       std::string aName;
291       switch (aPart) {
292         case Prs3d_DP_Origin: aName = "Origin"; break;
293         case Prs3d_DP_XAxis: aName = "OX"; break;
294         case Prs3d_DP_YAxis: aName = "OY"; break;
295         case Prs3d_DP_ZAxis: aName = "OZ"; break;
296         default: break;
297       }
298       if (aName.length() > 0) {
299         DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
300         int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
301         ObjectPtr aObj;
302         ResultPtr aResult;
303         for (int i = 0; i < aSize; i++) {
304           aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
305           if (aObj->data()->name() == aName) {
306             aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
307             break;
308           }
309         }
310         if (aResult.get()) {
311           thePrs->setObject(aResult);
312           thePrs->setShape(aResult->shape());
313           return;
314         }
315       }
316     }
317 #endif
318 #endif
319   }
320
321   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
322   ObjectPtr aFeature = aDisplayer->getObject(anIO);
323
324   Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
325   if (!aCompSolidBRO.IsNull()) {
326     // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
327     // is On and we have to use parent result which corresponds to the CompSolid shape
328     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
329     if (aResult.get()) {
330       ResultBodyPtr aCompSolid = ModelAPI_Tools::bodyOwner(aResult);
331       if (aCompSolid.get()) {
332         GeomShapePtr aShape = aCompSolid->shape();
333         if (aShape.get() && aShape->isEqual(thePrs->shape())) {
334           thePrs->setObject(aCompSolid);
335           return;
336         }
337       }
338     }
339   }
340   thePrs->setObject(aFeature);
341 }
342
343 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
344 {
345   QList<ModuleBase_ViewerPrsPtr> aPresentations;
346   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
347   if (aContext.IsNull())
348     return aPresentations;
349
350   QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
351   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
352   for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
353     Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
354     if (!anOwner.IsNull()) {
355       if (aSelectedIds.contains((size_t)anOwner.get()))
356         continue;
357       aSelectedIds.append((size_t)anOwner.get());
358
359       ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
360       fillPresentation(aPrs, anOwner);
361       aPresentations.push_back(aPrs);
362     }
363   }
364   return aPresentations;
365 }
366
367 QObjectPtrList XGUI_Selection::selectedObjects() const
368 {
369   if (myWorkshop->objectBrowser())
370     return myWorkshop->objectBrowser()->selectedObjects();
371   return QObjectPtrList();
372 }
373
374 QObjectPtrList XGUI_Selection::selectedPresentations() const
375 {
376   QObjectPtrList aSelectedList;
377
378   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
379   if (!aContext.IsNull()) {
380     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
381       Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
382       ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
383       if (aResult)
384         aSelectedList.append(aResult);
385     }
386   }
387   return aSelectedList;
388 }
389
390 //**************************************************************
391 QModelIndexList XGUI_Selection::selectedIndexes() const
392 {
393   return myWorkshop->objectBrowser()->selectedIndexes();
394 }
395
396 //**************************************************************
397 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
398 {
399   ObjectPtr anObject;
400
401   Handle(SelectMgr_EntityOwner) aEO = theOwner;
402   if (!aEO.IsNull()) {
403     Handle(AIS_InteractiveObject) anObj =
404       Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
405     anObject = myWorkshop->displayer()->getObject(anObj);
406   }
407   return anObject;
408 }
409
410 //**************************************************************
411 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
412 {
413   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
414   if (!aContext.IsNull()) {
415     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
416       theSelectedOwners.Add(aContext->SelectedOwner());
417     }
418   }
419 }
420
421 //**************************************************************
422 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
423                                   SelectMgr_IndexedMapOfOwner& theOwners) const
424 {
425   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
426   if (aContext.IsNull() || theObject.IsNull())
427     return;
428
429   TColStd_ListOfInteger aModes;
430   aContext->ActivatedModes(theObject, aModes);
431
432   TColStd_ListIteratorOfListOfInteger anIt(aModes);
433   for (; anIt.More(); anIt.Next()) {
434     int aMode = anIt.Value();
435     if (!theObject->HasSelection(aMode))
436       continue;
437
438     Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
439     NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
440     for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
441          anIt.More();
442          anIt.Next()) {
443       Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
444       if (anEntity.IsNull())
445         continue;
446        Handle(SelectMgr_EntityOwner) anOwner =
447         Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
448       if (!anOwner.IsNull())
449         theOwners.Add(anOwner);
450     }
451   }
452 }
453
454 //**************************************************************
455 #ifdef BEFORE_TRIHEDRON_PATCH
456 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
457 {
458   TopoDS_Shape aShape;
459   // Fill by trihedron shapes
460   Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
461   if (!aAxis.IsNull()) {
462     // an Axis from Trihedron
463     Handle(Geom_Line) aLine = aAxis->Component();
464     Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
465     Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
466
467     BRep_Builder aBuilder;
468     TopoDS_Edge aEdge;
469     aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
470     if (!aEdge.IsNull())
471       aShape = aEdge;
472   } else {
473     Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
474     if (!aPoint.IsNull()) {
475       // A point from trihedron
476       Handle(Geom_Point) aPnt = aPoint->Component();
477       BRep_Builder aBuilder;
478       TopoDS_Vertex aVertex;
479       aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
480       if (!aVertex.IsNull())
481         aShape = aVertex;
482     }
483   }
484   return aShape;
485 }
486 #endif