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