1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: XGUI_Selection.cpp
4 // Created: 8 July 2014
5 // Author: Vitaly SMETANNIKOV
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"
13 #include <ModelAPI_Feature.h>
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>
25 #include <TColStd_ListIteratorOfListOfInteger.hxx>
26 #include <StdSelect_BRepOwner.hxx>
30 #define DEBUG_DELIVERY
32 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
33 : myWorkshop(theWorkshop)
37 QList<ModuleBase_ViewerPrs> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
39 QList<ModuleBase_ViewerPrs> aPresentations;
43 getSelectedInBrowser(aPresentations);
46 getSelectedInViewer(aPresentations);
49 getSelectedInViewer(aPresentations);
50 getSelectedInBrowser(aPresentations);
53 return aPresentations;
56 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrs& thePrs)
58 Handle(AIS_InteractiveObject) anIO = thePrs.interactive();
60 Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
61 if (!anOwner.IsNull())
62 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
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)>();
74 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrs>& thePresentations) const
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();
83 if (aSelectedIds.contains((long)anOwner.Access()))
85 aSelectedIds.append((long)anOwner.Access());
87 fillPresentation(aPrs, anOwner);
89 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
90 thePresentations.append(aPrs);
95 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrs>& thePresentations) const
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());
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));
115 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrs& thePrs,
116 const Handle(SelectMgr_EntityOwner)& theOwner) const
118 thePrs.setOwner(theOwner);
120 Handle(AIS_InteractiveObject) anIO =
121 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
122 thePrs.setInteractive(anIO);
124 // we should not check the appearance of this feature because there can be some selected shapes
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
133 aShape = findAxisShape(anIO);
135 if (!aShape.IsNull())
136 thePrs.setShape(aShape);
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());
147 BRep_Builder aBuilder;
149 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
151 thePrs.setShape(aEdge);
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);
167 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
168 ObjectPtr aFeature = aDisplayer->getObject(anIO);
169 thePrs.setObject(aFeature);
172 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted() const
174 QList<ModuleBase_ViewerPrs> aPresentations;
175 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
176 if (aContext.IsNull())
177 return aPresentations;
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()))
187 aSelectedIds.append((long)anIO.Access());
188 aPrs.setInteractive(anIO);
190 ObjectPtr aResult = aDisplayer->getObject(anIO);
191 // we should not check the appearance of this feature because there can be some selected shapes
193 aPrs.setObject(aResult);
194 if (aContext->HasOpenedContext()) {
195 TopoDS_Shape aShape = aContext->DetectedShape();
196 if (!aShape.IsNull())
197 aPrs.setShape(aShape);
199 aPresentations.push_back(aPrs);
201 return aPresentations;
204 QObjectPtrList XGUI_Selection::selectedObjects() const
206 return myWorkshop->objectBrowser()->selectedObjects();
209 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
211 return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
214 QObjectPtrList XGUI_Selection::selectedPresentations() const
216 QObjectPtrList aSelectedList;
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);
224 aSelectedList.append(aResult);
227 return aSelectedList;
230 //**************************************************************
231 QModelIndexList XGUI_Selection::selectedIndexes() const
233 return myWorkshop->objectBrowser()->selectedIndexes();
236 //**************************************************************
237 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
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());
248 //**************************************************************
249 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
253 Handle(SelectMgr_EntityOwner) aEO = theOwner;
255 Handle(AIS_InteractiveObject) anObj =
256 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
257 anObject = myWorkshop->displayer()->getObject(anObj);
262 //**************************************************************
263 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList,
264 std::list<ObjectPtr>& theOwners) const
267 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
268 if (aContext.IsNull())
271 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
272 TopoDS_Shape aShape = aContext->SelectedShape();
273 if (aShape.IsNull()) {
274 aShape = findAxisShape(aContext->SelectedInteractive());
276 if (!aShape.IsNull()) {
277 theList.Append(aShape);
278 Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
280 Handle(AIS_InteractiveObject) anObj =
281 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
282 ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
283 theOwners.push_back(anObject);
289 //**************************************************************
290 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
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());
300 //**************************************************************
301 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
302 SelectMgr_IndexedMapOfOwner& theOwners) const
304 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
305 if (aContext.IsNull() || theObject.IsNull())
308 TColStd_ListOfInteger aModes;
309 aContext->ActivatedModes(theObject, aModes);
311 TColStd_ListIteratorOfListOfInteger anIt(aModes);
312 for (; anIt.More(); anIt.Next()) {
313 int aMode = anIt.Value();
314 if (!theObject->HasSelection(aMode))
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())
322 Handle(SelectMgr_EntityOwner) anOwner =
323 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
324 if (!anOwner.IsNull())
325 theOwners.Add(anOwner);
330 //**************************************************************
331 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
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());
342 BRep_Builder aBuilder;
344 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
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())