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 "ModuleBase_ResultPrs.h"
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_Tools.h>
18 #include <AIS_InteractiveContext.hxx>
19 #include <AIS_Axis.hxx>
20 #include <AIS_Point.hxx>
21 #include <Geom_Line.hxx>
22 #include <BRep_Builder.hxx>
23 #include <TopoDS_Edge.hxx>
24 #include <Geom_Point.hxx>
25 #include <Geom_TrimmedCurve.hxx>
26 #include <Prs3d_DatumAspect.hxx>
28 #include <TColStd_ListIteratorOfListOfInteger.hxx>
29 #include <StdSelect_BRepOwner.hxx>
33 #define DEBUG_DELIVERY
35 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
36 : myWorkshop(theWorkshop)
40 QList<ModuleBase_ViewerPrs> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
42 QList<ModuleBase_ViewerPrs> aPresentations;
46 getSelectedInBrowser(aPresentations);
49 getSelectedInViewer(aPresentations);
52 getSelectedInViewer(aPresentations);
53 getSelectedInBrowser(aPresentations);
56 return aPresentations;
59 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrs& thePrs)
61 Handle(AIS_InteractiveObject) anIO = thePrs.interactive();
63 Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
64 if (!anOwner.IsNull())
65 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
67 if (anIO.IsNull() && thePrs.object()) {
68 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
69 AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs.object());
70 if (anAISObject.get())
71 anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
77 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrs>& thePresentations) const
79 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
80 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
81 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
82 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
83 ModuleBase_ViewerPrs aPrs;
84 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
86 if (aSelectedIds.contains((long)anOwner.Access()))
88 aSelectedIds.append((long)anOwner.Access());
90 fillPresentation(aPrs, anOwner);
92 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
93 thePresentations.append(aPrs);
98 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrs>& thePresentations) const
100 // collect the objects of the parameter presentation to avoid a repeted objects in the result
101 QObjectPtrList aPresentationObjects;
102 QList<ModuleBase_ViewerPrs>::const_iterator aPrsIt = thePresentations.begin(),
103 aPrsLast = thePresentations.end();
104 for (; aPrsIt != aPrsLast; aPrsIt++) {
105 aPresentationObjects.push_back((*aPrsIt).object());
108 QObjectPtrList anObjects = selectedObjects();
109 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
110 for (; anIt != aLast; anIt++) {
111 ObjectPtr anObject = *anIt;
112 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
113 thePresentations.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
118 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrs& thePrs,
119 const Handle(SelectMgr_EntityOwner)& theOwner) const
121 thePrs.setOwner(theOwner);
123 Handle(AIS_InteractiveObject) anIO =
124 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
125 thePrs.setInteractive(anIO);
127 // we should not check the appearance of this feature because there can be some selected shapes
129 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
130 if( !aBRO.IsNull() && aBRO->HasShape() ) {
131 // the located method is called in the context to obtain the shape by the SelectedShape() method,
132 // so the shape is located by the same rules
133 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
134 #ifndef DEBUG_DELIVERY
136 aShape = findAxisShape(anIO);
138 if (!aShape.IsNull())
139 thePrs.setShape(aShape);
141 #ifdef DEBUG_DELIVERY
142 // Fill by trihedron shapes
143 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
144 if (!aAxis.IsNull()) {
145 // an Axis from Trihedron
146 Handle(Geom_Line) aLine = aAxis->Component();
147 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
148 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
150 BRep_Builder aBuilder;
152 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
154 thePrs.setShape(aEdge);
156 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
157 if (!aPoint.IsNull()) {
158 // A point from trihedron
159 Handle(Geom_Point) aPnt = aPoint->Component();
160 BRep_Builder aBuilder;
161 TopoDS_Vertex aVertex;
162 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
163 if (!aVertex.IsNull())
164 thePrs.setShape(aVertex);
170 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
171 ObjectPtr aFeature = aDisplayer->getObject(anIO);
173 Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
174 if (!aCompSolidBRO.IsNull()) {
175 // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
176 // is On and we have to use parent result which corresponds to the CompSolid shape
177 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
179 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(aResult);
180 if (aCompSolid.get()) {
181 GeomShapePtr aShapePtr = aCompSolid->shape();
182 if (aShapePtr.get()) {
183 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
184 if (aShape.IsEqual(thePrs.shape())) {
185 thePrs.setObject(aCompSolid);
192 thePrs.setObject(aFeature);
195 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted() const
197 QList<ModuleBase_ViewerPrs> aPresentations;
198 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
199 if (aContext.IsNull())
200 return aPresentations;
202 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
203 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
204 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
205 ModuleBase_ViewerPrs aPrs;
206 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
207 if (aSelectedIds.contains((long)anIO.Access()))
210 aSelectedIds.append((long)anIO.Access());
211 aPrs.setInteractive(anIO);
213 ObjectPtr aResult = aDisplayer->getObject(anIO);
214 // we should not check the appearance of this feature because there can be some selected shapes
216 aPrs.setObject(aResult);
217 if (aContext->HasOpenedContext()) {
218 TopoDS_Shape aShape = aContext->DetectedShape();
219 if (!aShape.IsNull())
220 aPrs.setShape(aShape);
222 aPresentations.push_back(aPrs);
224 return aPresentations;
227 QObjectPtrList XGUI_Selection::selectedObjects() const
229 return myWorkshop->objectBrowser()->selectedObjects();
232 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
234 return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
237 QObjectPtrList XGUI_Selection::selectedPresentations() const
239 QObjectPtrList aSelectedList;
241 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
242 if (!aContext.IsNull()) {
243 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
244 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
245 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
247 aSelectedList.append(aResult);
250 return aSelectedList;
253 //**************************************************************
254 QModelIndexList XGUI_Selection::selectedIndexes() const
256 return myWorkshop->objectBrowser()->selectedIndexes();
259 //**************************************************************
260 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
264 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
265 if (!aContext.IsNull()) {
266 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
267 theList.Append(aContext->SelectedInteractive());
271 //**************************************************************
272 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
276 Handle(SelectMgr_EntityOwner) aEO = theOwner;
278 Handle(AIS_InteractiveObject) anObj =
279 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
280 anObject = myWorkshop->displayer()->getObject(anObj);
285 //**************************************************************
286 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList,
287 std::list<ObjectPtr>& theOwners) const
290 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
291 if (aContext.IsNull())
294 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
295 TopoDS_Shape aShape = aContext->SelectedShape();
296 if (aShape.IsNull()) {
297 aShape = findAxisShape(aContext->SelectedInteractive());
299 if (!aShape.IsNull()) {
300 theList.Append(aShape);
301 Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
303 Handle(AIS_InteractiveObject) anObj =
304 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
305 ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
306 theOwners.push_back(anObject);
312 //**************************************************************
313 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
315 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
316 if (!aContext.IsNull()) {
317 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
318 theSelectedOwners.Add(aContext->SelectedOwner());
323 //**************************************************************
324 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
325 SelectMgr_IndexedMapOfOwner& theOwners) const
327 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
328 if (aContext.IsNull() || theObject.IsNull())
331 TColStd_ListOfInteger aModes;
332 aContext->ActivatedModes(theObject, aModes);
334 TColStd_ListIteratorOfListOfInteger anIt(aModes);
335 for (; anIt.More(); anIt.Next()) {
336 int aMode = anIt.Value();
337 if (!theObject->HasSelection(aMode))
340 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
341 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
342 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
343 if (anEntity.IsNull())
345 Handle(SelectMgr_EntityOwner) anOwner =
346 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
347 if (!anOwner.IsNull())
348 theOwners.Add(anOwner);
353 //**************************************************************
354 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
357 // Fill by trihedron shapes
358 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
359 if (!aAxis.IsNull()) {
360 // an Axis from Trihedron
361 Handle(Geom_Line) aLine = aAxis->Component();
362 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
363 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
365 BRep_Builder aBuilder;
367 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
371 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
372 if (!aPoint.IsNull()) {
373 // A point from trihedron
374 Handle(Geom_Point) aPnt = aPoint->Component();
375 BRep_Builder aBuilder;
376 TopoDS_Vertex aVertex;
377 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
378 if (!aVertex.IsNull())