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