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