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