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