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