Salome HOME
Issue #1431: Show message if not all results can be highlighted in object browser
[modules/shaper.git] / src / XGUI / XGUI_Selection.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        XGUI_Selection.cpp
4 // Created:     8 July 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "XGUI_Selection.h"
8 #include "XGUI_Workshop.h"
9 #include "XGUI_Displayer.h"
10 #include "XGUI_ViewerProxy.h"
11 #include "XGUI_ObjectsBrowser.h"
12
13 #include "ModuleBase_ResultPrs.h"
14 #include <ModuleBase_ViewerPrs.h>
15
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_Tools.h>
18
19 #include <AIS_InteractiveContext.hxx>
20 #include <AIS_Axis.hxx>
21 #include <AIS_Point.hxx>
22 #include <Geom_Line.hxx>
23 #include <BRep_Builder.hxx>
24 #include <TopoDS_Edge.hxx>
25 #include <Geom_Point.hxx>
26 #include <Geom_TrimmedCurve.hxx>
27 #include <Prs3d_DatumAspect.hxx>
28
29 #include <TColStd_ListIteratorOfListOfInteger.hxx>
30 #include <StdSelect_BRepOwner.hxx>
31
32 #include <set>
33
34 #define DEBUG_DELIVERY
35
36 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
37     : myWorkshop(theWorkshop)
38 {
39 }
40
41 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
42 {
43   QList<ModuleBase_ViewerPrsPtr> aPresentations;
44
45   switch (thePlace) {
46     case Browser:
47       getSelectedInBrowser(aPresentations);
48     break;
49     case Viewer:
50       getSelectedInViewer(aPresentations);
51     break;
52   case AllControls:
53       getSelectedInViewer(aPresentations);
54       getSelectedInBrowser(aPresentations);
55     break;
56   }
57   return aPresentations;
58 }
59
60 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPtr& thePrs)
61 {
62   Handle(AIS_InteractiveObject) anIO = thePrs->interactive();
63   if (anIO.IsNull()) {
64     Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
65     if (!anOwner.IsNull())
66       anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
67
68     if (anIO.IsNull() && thePrs->object()) {
69       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
70       AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs->object());
71       if (anAISObject.get())
72         anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
73     }
74   }
75   return anIO;
76 }
77
78 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
79 {
80   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
81   if (!aContext.IsNull() && aContext->HasOpenedContext()) {
82     QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
83     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
84       ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
85       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
86
87       if (aSelectedIds.contains((long)anOwner.Access()))
88         continue;
89       aSelectedIds.append((long)anOwner.Access());
90
91       fillPresentation(aPrs, anOwner);
92
93       if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
94         thePresentations.append(aPrs);
95     }
96   }
97 }
98
99 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
100 {
101   // collect the objects  of the parameter presentation to avoid a repeted objects in the result
102   QObjectPtrList aPresentationObjects;
103   QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = thePresentations.begin(),
104                                               aPrsLast = thePresentations.end();
105   for (; aPrsIt != aPrsLast; aPrsIt++) {
106     aPresentationObjects.push_back((*aPrsIt)->object());
107   }
108
109   QObjectPtrList anObjects = selectedObjects();
110   QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
111   for (; anIt != aLast; anIt++) {
112     ObjectPtr anObject = *anIt;
113     if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
114       thePresentations.append(std::shared_ptr<ModuleBase_ViewerPrs>(
115                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
116     }
117   }
118 }
119
120 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
121                                       const Handle(SelectMgr_EntityOwner)& theOwner) const
122 {
123   thePrs->setOwner(theOwner);
124
125   Handle(AIS_InteractiveObject) anIO = 
126                            Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
127   thePrs->setInteractive(anIO);
128
129   // we should not check the appearance of this feature because there can be some selected shapes
130   // for one feature
131   Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
132   if( !aBRO.IsNull() && aBRO->HasShape() ) {
133     // the located method is called in the context to obtain the shape by the SelectedShape() method,
134     // so the shape is located by the same rules
135     TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
136 #ifndef DEBUG_DELIVERY
137     if (aShape.IsNull())
138       aShape = findAxisShape(anIO);
139 #endif
140     if (!aShape.IsNull()) {
141       std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
142       aGeomShape->setImpl(new TopoDS_Shape(aShape));
143       thePrs->setShape(aGeomShape);
144     }
145   } else {
146 #ifdef DEBUG_DELIVERY
147     // Fill by trihedron shapes
148     Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
149     if (!aAxis.IsNull()) {
150       // an Axis from Trihedron
151       Handle(Geom_Line) aLine = aAxis->Component();
152       Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
153       Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
154
155       BRep_Builder aBuilder;      
156       TopoDS_Edge aEdge;
157       aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
158       if (!aEdge.IsNull()) {
159         std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
160         aGeomShape->setImpl(new TopoDS_Shape(aEdge));
161         thePrs->setShape(aGeomShape);
162       }
163     } else {
164       Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
165       if (!aPoint.IsNull()) {
166         // A point from trihedron
167         Handle(Geom_Point) aPnt = aPoint->Component();
168         BRep_Builder aBuilder;
169         TopoDS_Vertex aVertex;
170         aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
171         if (!aVertex.IsNull()) {
172           std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
173           aGeomShape->setImpl(new TopoDS_Shape(aVertex));
174           thePrs->setShape(aGeomShape);
175         }
176       }
177     }
178 #endif
179   }
180      
181   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
182   ObjectPtr aFeature = aDisplayer->getObject(anIO);
183
184   Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
185   if (!aCompSolidBRO.IsNull()) {
186     // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
187     // is On and we have to use parent result which corresponds to the CompSolid shape
188     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
189     if (aResult.get()) {
190       ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(aResult);
191       if (aCompSolid.get()) {
192         GeomShapePtr aShape = aCompSolid->shape();
193         if (aShape.get() && aShape->isEqual(thePrs->shape())) {
194           thePrs->setObject(aCompSolid);
195           return;
196         }
197       }
198     }
199   }
200   thePrs->setObject(aFeature);
201 }
202
203 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
204 {
205   QList<ModuleBase_ViewerPrsPtr> aPresentations;
206   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
207   if (aContext.IsNull())
208     return aPresentations;
209
210   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
211   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
212   for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
213     ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
214     Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
215     if (aSelectedIds.contains((long)anIO.Access()))
216       continue;
217     
218     aSelectedIds.append((long)anIO.Access());
219     aPrs->setInteractive(anIO);
220
221     ObjectPtr aResult = aDisplayer->getObject(anIO);
222     // we should not check the appearance of this feature because there can be some selected shapes
223     // for one feature
224     aPrs->setObject(aResult);
225     if (aContext->HasOpenedContext()) {
226       TopoDS_Shape aShape = aContext->DetectedShape();
227       if (!aShape.IsNull()) {
228         std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
229         aGeomShape->setImpl(new TopoDS_Shape(aShape));
230         aPrs->setShape(aGeomShape);
231       }
232     }
233     aPresentations.push_back(aPrs);
234   }
235   return aPresentations;
236 }
237
238 QObjectPtrList XGUI_Selection::selectedObjects() const
239 {
240   return myWorkshop->objectBrowser()->selectedObjects();
241 }
242
243 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
244 {
245   return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
246 }
247
248 QObjectPtrList XGUI_Selection::selectedPresentations() const
249 {
250   QObjectPtrList aSelectedList;
251
252   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
253   if (!aContext.IsNull()) {
254     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
255       Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
256       ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
257       if (aResult)
258         aSelectedList.append(aResult);
259     }
260   }
261   return aSelectedList;
262 }
263
264 //**************************************************************
265 QModelIndexList XGUI_Selection::selectedIndexes() const
266 {
267   return myWorkshop->objectBrowser()->selectedIndexes();
268 }
269
270 //**************************************************************
271 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
272 {
273   theList.Clear();
274
275   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
276   if (!aContext.IsNull()) {
277     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
278       theList.Append(aContext->SelectedInteractive());
279   }
280 }
281
282 //**************************************************************
283 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
284 {
285   ObjectPtr anObject;
286
287   Handle(SelectMgr_EntityOwner) aEO = theOwner;
288   if (!aEO.IsNull()) {
289     Handle(AIS_InteractiveObject) anObj = 
290       Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
291     anObject = myWorkshop->displayer()->getObject(anObj);
292   }
293   return anObject;
294 }
295
296 //**************************************************************
297 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
298 {
299   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
300   if (!aContext.IsNull()) {
301     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
302       theSelectedOwners.Add(aContext->SelectedOwner());
303     }
304   }
305 }
306
307 //**************************************************************
308 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
309                                   SelectMgr_IndexedMapOfOwner& theOwners) const
310 {
311   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
312   if (aContext.IsNull() || theObject.IsNull())
313     return;
314
315   TColStd_ListOfInteger aModes;
316   aContext->ActivatedModes(theObject, aModes);
317
318   TColStd_ListIteratorOfListOfInteger anIt(aModes);
319   for (; anIt.More(); anIt.Next()) {
320     int aMode = anIt.Value();
321     if (!theObject->HasSelection(aMode))
322       continue;
323
324     Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
325     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
326       Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
327       if (anEntity.IsNull())
328         continue;
329       Handle(SelectMgr_EntityOwner) anOwner =
330         Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
331       if (!anOwner.IsNull())
332         theOwners.Add(anOwner);
333     }
334   }
335 }
336
337 //**************************************************************
338 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
339 {
340   TopoDS_Shape aShape;
341   // Fill by trihedron shapes
342   Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
343   if (!aAxis.IsNull()) {
344     // an Axis from Trihedron
345     Handle(Geom_Line) aLine = aAxis->Component();
346     Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
347     Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
348
349     BRep_Builder aBuilder;      
350     TopoDS_Edge aEdge;
351     aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
352     if (!aEdge.IsNull())
353       aShape = aEdge;
354   } else {
355     Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
356     if (!aPoint.IsNull()) {
357       // A point from trihedron
358       Handle(Geom_Point) aPnt = aPoint->Component();
359       BRep_Builder aBuilder;
360       TopoDS_Vertex aVertex;
361       aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
362       if (!aVertex.IsNull())
363         aShape = aVertex;
364     }
365   }
366   return aShape;
367 }