Salome HOME
Merge branch 'Pre_2.8.0_development'
[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_ResultPrs.h"
28 #include <ModuleBase_ViewerPrs.h>
29
30 #include <ModelAPI_Feature.h>
31 #include <ModelAPI_Tools.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_ResultCompSolid.h>
34 #include <ModelAPI_ResultConstruction.h>
35
36 #include <AIS_InteractiveContext.hxx>
37 #ifdef BEFORE_TRIHEDRON_PATCH
38 #include <AIS_Axis.hxx>
39 #include <AIS_Point.hxx>
40 #else
41 #include <AIS_TrihedronOwner.hxx>
42 #endif
43 #include <Geom_Line.hxx>
44 #include <BRep_Builder.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <Geom_Point.hxx>
47 #include <Geom_TrimmedCurve.hxx>
48 #include <Prs3d_DatumAspect.hxx>
49
50 #include <TColStd_ListIteratorOfListOfInteger.hxx>
51 #include <StdSelect_BRepOwner.hxx>
52
53 #include <set>
54
55 #define DEBUG_DELIVERY
56
57 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
58     : myWorkshop(theWorkshop)
59 {
60 }
61
62 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
63 {
64   QList<ModuleBase_ViewerPrsPtr> aPresentations;
65
66   switch (thePlace) {
67     case Browser:
68       getSelectedInBrowser(aPresentations);
69     break;
70     case Viewer:
71       getSelectedInViewer(aPresentations);
72     break;
73   case AllControls:
74       getSelectedInViewer(aPresentations);
75       getSelectedInBrowser(aPresentations);
76     break;
77   }
78   return aPresentations;
79 }
80
81 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPtr& thePrs)
82 {
83   Handle(AIS_InteractiveObject) anIO = thePrs->interactive();
84   if (anIO.IsNull()) {
85     Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
86     if (!anOwner.IsNull())
87       anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
88
89     if (anIO.IsNull() && thePrs->object()) {
90       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
91       AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs->object());
92       if (anAISObject.get())
93         anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
94     }
95   }
96   return anIO;
97 }
98
99 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
100 {
101   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
102   if (!aContext.IsNull()) {
103     QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
104     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
105       ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
106       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
107
108       if (aSelectedIds.contains((long)anOwner.get()))
109         continue;
110       aSelectedIds.append((long)anOwner.get());
111
112       fillPresentation(aPrs, anOwner);
113
114       if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
115         thePresentations.append(aPrs);
116     }
117   }
118 }
119
120 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
121 {
122   // collect the objects  of the parameter presentation to avoid a repeted objects in the result
123   QObjectPtrList aPresentationObjects;
124   QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = thePresentations.begin(),
125                                               aPrsLast = thePresentations.end();
126   for (; aPrsIt != aPrsLast; aPrsIt++) {
127     aPresentationObjects.push_back((*aPrsIt)->object());
128   }
129
130   QObjectPtrList anObjects = selectedObjects();
131   QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
132   for (; anIt != aLast; anIt++) {
133     ObjectPtr anObject = *anIt;
134     if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
135       thePresentations.append(std::shared_ptr<ModuleBase_ViewerPrs>(
136                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
137     }
138   }
139 }
140
141 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
142                                       const Handle(SelectMgr_EntityOwner)& theOwner) const
143 {
144   thePrs->setOwner(theOwner);
145   Handle(SelectMgr_SelectableObject) aSelectable = theOwner->Selectable();
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 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
312 {
313   return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
314 }
315
316 QObjectPtrList XGUI_Selection::selectedPresentations() const
317 {
318   QObjectPtrList aSelectedList;
319
320   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
321   if (!aContext.IsNull()) {
322     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
323       Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
324       ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
325       if (aResult)
326         aSelectedList.append(aResult);
327     }
328   }
329   return aSelectedList;
330 }
331
332 //**************************************************************
333 QModelIndexList XGUI_Selection::selectedIndexes() const
334 {
335   return myWorkshop->objectBrowser()->selectedIndexes();
336 }
337
338 //**************************************************************
339 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
340 {
341   theList.Clear();
342
343   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
344   if (!aContext.IsNull()) {
345     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
346       theList.Append(aContext->SelectedInteractive());
347   }
348 }
349
350 //**************************************************************
351 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
352 {
353   ObjectPtr anObject;
354
355   Handle(SelectMgr_EntityOwner) aEO = theOwner;
356   if (!aEO.IsNull()) {
357     Handle(AIS_InteractiveObject) anObj =
358       Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
359     anObject = myWorkshop->displayer()->getObject(anObj);
360   }
361   return anObject;
362 }
363
364 //**************************************************************
365 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
366 {
367   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
368   if (!aContext.IsNull()) {
369     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
370       theSelectedOwners.Add(aContext->SelectedOwner());
371     }
372   }
373 }
374
375 //**************************************************************
376 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
377                                   SelectMgr_IndexedMapOfOwner& theOwners) const
378 {
379   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
380   if (aContext.IsNull() || theObject.IsNull())
381     return;
382
383   TColStd_ListOfInteger aModes;
384   aContext->ActivatedModes(theObject, aModes);
385
386   TColStd_ListIteratorOfListOfInteger anIt(aModes);
387   for (; anIt.More(); anIt.Next()) {
388     int aMode = anIt.Value();
389     if (!theObject->HasSelection(aMode))
390       continue;
391
392     Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
393     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
394       Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
395       if (anEntity.IsNull())
396         continue;
397       Handle(SelectMgr_EntityOwner) anOwner =
398         Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
399       if (!anOwner.IsNull())
400         theOwners.Add(anOwner);
401     }
402   }
403 }
404
405 //**************************************************************
406 #ifdef BEFORE_TRIHEDRON_PATCH
407 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
408 {
409   TopoDS_Shape aShape;
410   // Fill by trihedron shapes
411   Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
412   if (!aAxis.IsNull()) {
413     // an Axis from Trihedron
414     Handle(Geom_Line) aLine = aAxis->Component();
415     Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
416     Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
417
418     BRep_Builder aBuilder;
419     TopoDS_Edge aEdge;
420     aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
421     if (!aEdge.IsNull())
422       aShape = aEdge;
423   } else {
424     Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
425     if (!aPoint.IsNull()) {
426       // A point from trihedron
427       Handle(Geom_Point) aPnt = aPoint->Component();
428       BRep_Builder aBuilder;
429       TopoDS_Vertex aVertex;
430       aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
431       if (!aVertex.IsNull())
432         aShape = aVertex;
433     }
434   }
435   return aShape;
436 }
437 #endif