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