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