Salome HOME
ed54a60a27ab034ff8100903077474cb28b4320a
[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(SelectMgr_SelectableObject) aSelectable = theOwner->Selectable();
147   Handle(AIS_InteractiveObject) anIO =
148                            Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
149   thePrs->setInteractive(anIO);
150
151   // we should not check the appearance of this feature because there can be some selected shapes
152   // for one feature
153   Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
154   if( !aBRO.IsNull() && aBRO->HasShape() ) {
155     // the located method is called in the context to obtain the shape by the SelectedShape()
156     // method, so the shape is located by the same rules
157     TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
158 #ifdef BEFORE_TRIHEDRON_PATCH
159 #ifndef DEBUG_DELIVERY
160     if (aShape.IsNull())
161       aShape = findAxisShape(anIO);
162 #endif
163 #endif
164     if (!aShape.IsNull()) {
165       std::shared_ptr<GeomAPI_Shape> aGeomShape =
166         std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
167       aGeomShape->setImpl(new TopoDS_Shape(aShape));
168       thePrs->setShape(aGeomShape);
169     }
170   } else {
171 #ifdef DEBUG_DELIVERY
172     // Fill by trihedron shapes
173 #ifdef BEFORE_TRIHEDRON_PATCH
174     Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
175     DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
176     int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
177     ObjectPtr aObj;
178     if (!aAxis.IsNull()) {
179       // an Axis from Trihedron
180       gp_Lin aLine = aAxis->Component()->Lin();
181       gp_Dir aDir = aLine.Direction();
182       std::string aAxName;
183       if (aDir.X() == 1.)
184         aAxName = "OX";
185       else if (aDir.Y() == 1.)
186         aAxName = "OY";
187       else if (aDir.Z() == 1.)
188         aAxName = "OZ";
189       if (aAxName.length() > 0) {
190         ResultPtr aAx;
191         for (int i = 0; i < aSize; i++) {
192           aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
193           if (aObj->data()->name() == aAxName) {
194             aAx = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
195             break;
196           }
197         }
198         if (aAx.get()) {
199           thePrs->setObject(aAx);
200           thePrs->setShape(aAx->shape());
201           return;
202         }
203       }
204     } else {
205       Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
206       if (!aPoint.IsNull()) {
207         // An origin point from trihedron
208         ResultPtr aOrigin;
209         for (int i = 0; i < aSize; i++) {
210           aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
211           if (aObj->data()->name() == "Origin") {
212             aOrigin = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
213             break;
214           }
215         }
216         if (aOrigin.get()) {
217           thePrs->setObject(aOrigin);
218           thePrs->setShape(aOrigin->shape());
219           return;
220         }
221       }
222     }
223 #else
224     /// find axis direction
225     Handle(AIS_TrihedronOwner) aTrihedronOwner = Handle(AIS_TrihedronOwner)::DownCast(theOwner);
226     if (!aTrihedronOwner.IsNull()) {
227       const Prs3d_DatumParts& aPart = aTrihedronOwner->DatumPart();
228       std::string aName;
229       switch (aPart) {
230         case Prs3d_DP_Origin: aName = "Origin"; break;
231         case Prs3d_DP_XAxis: aName = "OX"; break;
232         case Prs3d_DP_YAxis: aName = "OY"; break;
233         case Prs3d_DP_ZAxis: aName = "OZ"; break;
234         default: break;
235       }
236       if (aName.length() > 0) {
237         DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
238         int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
239         ObjectPtr aObj;
240         ResultPtr aResult;
241         for (int i = 0; i < aSize; i++) {
242           aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
243           if (aObj->data()->name() == aName) {
244             aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
245             break;
246           }
247         }
248         if (aResult.get()) {
249           thePrs->setObject(aResult);
250           thePrs->setShape(aResult->shape());
251           return;
252         }
253       }
254     }
255 #endif
256 #endif
257   }
258
259   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
260   ObjectPtr aFeature = aDisplayer->getObject(anIO);
261
262   Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
263   if (!aCompSolidBRO.IsNull()) {
264     // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
265     // is On and we have to use parent result which corresponds to the CompSolid shape
266     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
267     if (aResult.get()) {
268       ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(aResult);
269       if (aCompSolid.get()) {
270         GeomShapePtr aShape = aCompSolid->shape();
271         if (aShape.get() && aShape->isEqual(thePrs->shape())) {
272           thePrs->setObject(aCompSolid);
273           return;
274         }
275       }
276     }
277   }
278   thePrs->setObject(aFeature);
279 }
280
281 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
282 {
283   QList<ModuleBase_ViewerPrsPtr> aPresentations;
284   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
285   if (aContext.IsNull())
286     return aPresentations;
287
288   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
289   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
290   for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
291     Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
292     if (!anOwner.IsNull()) {
293       if (aSelectedIds.contains((long)anOwner.get()))
294         continue;
295       aSelectedIds.append((long)anOwner.get());
296
297       ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
298       fillPresentation(aPrs, anOwner);
299       aPresentations.push_back(aPrs);
300     }
301   }
302   return aPresentations;
303 }
304
305 QObjectPtrList XGUI_Selection::selectedObjects() const
306 {
307   if (myWorkshop->objectBrowser())
308     return myWorkshop->objectBrowser()->selectedObjects();
309   return QObjectPtrList();
310 }
311
312 QObjectPtrList XGUI_Selection::selectedPresentations() const
313 {
314   QObjectPtrList aSelectedList;
315
316   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
317   if (!aContext.IsNull()) {
318     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
319       Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
320       ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
321       if (aResult)
322         aSelectedList.append(aResult);
323     }
324   }
325   return aSelectedList;
326 }
327
328 //**************************************************************
329 QModelIndexList XGUI_Selection::selectedIndexes() const
330 {
331   return myWorkshop->objectBrowser()->selectedIndexes();
332 }
333
334 //**************************************************************
335 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
336 {
337   ObjectPtr anObject;
338
339   Handle(SelectMgr_EntityOwner) aEO = theOwner;
340   if (!aEO.IsNull()) {
341     Handle(AIS_InteractiveObject) anObj =
342       Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
343     anObject = myWorkshop->displayer()->getObject(anObj);
344   }
345   return anObject;
346 }
347
348 //**************************************************************
349 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
350 {
351   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
352   if (!aContext.IsNull()) {
353     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
354       theSelectedOwners.Add(aContext->SelectedOwner());
355     }
356   }
357 }
358
359 //**************************************************************
360 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
361                                   SelectMgr_IndexedMapOfOwner& theOwners) const
362 {
363   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
364   if (aContext.IsNull() || theObject.IsNull())
365     return;
366
367   TColStd_ListOfInteger aModes;
368   aContext->ActivatedModes(theObject, aModes);
369
370   TColStd_ListIteratorOfListOfInteger anIt(aModes);
371   for (; anIt.More(); anIt.Next()) {
372     int aMode = anIt.Value();
373     if (!theObject->HasSelection(aMode))
374       continue;
375
376     Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
377     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
378       Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
379       if (anEntity.IsNull())
380         continue;
381       Handle(SelectMgr_EntityOwner) anOwner =
382         Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
383       if (!anOwner.IsNull())
384         theOwners.Add(anOwner);
385     }
386   }
387 }
388
389 //**************************************************************
390 #ifdef BEFORE_TRIHEDRON_PATCH
391 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
392 {
393   TopoDS_Shape aShape;
394   // Fill by trihedron shapes
395   Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
396   if (!aAxis.IsNull()) {
397     // an Axis from Trihedron
398     Handle(Geom_Line) aLine = aAxis->Component();
399     Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
400     Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
401
402     BRep_Builder aBuilder;
403     TopoDS_Edge aEdge;
404     aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
405     if (!aEdge.IsNull())
406       aShape = aEdge;
407   } else {
408     Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
409     if (!aPoint.IsNull()) {
410       // A point from trihedron
411       Handle(Geom_Point) aPnt = aPoint->Component();
412       BRep_Builder aBuilder;
413       TopoDS_Vertex aVertex;
414       aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
415       if (!aVertex.IsNull())
416         aShape = aVertex;
417     }
418   }
419   return aShape;
420 }
421 #endif