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