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