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, GeomShapePtr(), 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 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
140 aGeomShape->setImpl(new TopoDS_Shape(aShape));
141 thePrs.setShape(aGeomShape);
144 #ifdef DEBUG_DELIVERY
145 // Fill by trihedron shapes
146 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
147 if (!aAxis.IsNull()) {
148 // an Axis from Trihedron
149 Handle(Geom_Line) aLine = aAxis->Component();
150 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
151 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
153 BRep_Builder aBuilder;
155 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
156 if (!aEdge.IsNull()) {
157 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
158 aGeomShape->setImpl(new TopoDS_Shape(aEdge));
159 thePrs.setShape(aGeomShape);
162 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
163 if (!aPoint.IsNull()) {
164 // A point from trihedron
165 Handle(Geom_Point) aPnt = aPoint->Component();
166 BRep_Builder aBuilder;
167 TopoDS_Vertex aVertex;
168 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
169 if (!aVertex.IsNull()) {
170 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
171 aGeomShape->setImpl(new TopoDS_Shape(aVertex));
172 thePrs.setShape(aGeomShape);
179 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
180 ObjectPtr aFeature = aDisplayer->getObject(anIO);
182 Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
183 if (!aCompSolidBRO.IsNull()) {
184 // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
185 // is On and we have to use parent result which corresponds to the CompSolid shape
186 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
188 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(aResult);
189 if (aCompSolid.get()) {
190 GeomShapePtr aShape = aCompSolid->shape();
191 if (aShape.get() && aShape->isEqual(thePrs.shape())) {
192 thePrs.setObject(aCompSolid);
198 thePrs.setObject(aFeature);
201 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted() const
203 QList<ModuleBase_ViewerPrs> aPresentations;
204 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
205 if (aContext.IsNull())
206 return aPresentations;
208 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
209 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
210 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
211 ModuleBase_ViewerPrs aPrs;
212 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
213 if (aSelectedIds.contains((long)anIO.Access()))
216 aSelectedIds.append((long)anIO.Access());
217 aPrs.setInteractive(anIO);
219 ObjectPtr aResult = aDisplayer->getObject(anIO);
220 // we should not check the appearance of this feature because there can be some selected shapes
222 aPrs.setObject(aResult);
223 if (aContext->HasOpenedContext()) {
224 TopoDS_Shape aShape = aContext->DetectedShape();
225 if (!aShape.IsNull()) {
226 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
227 aGeomShape->setImpl(new TopoDS_Shape(aShape));
228 aPrs.setShape(aGeomShape);
231 aPresentations.push_back(aPrs);
233 return aPresentations;
236 QObjectPtrList XGUI_Selection::selectedObjects() const
238 return myWorkshop->objectBrowser()->selectedObjects();
241 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
243 return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
246 QObjectPtrList XGUI_Selection::selectedPresentations() const
248 QObjectPtrList aSelectedList;
250 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
251 if (!aContext.IsNull()) {
252 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
253 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
254 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
256 aSelectedList.append(aResult);
259 return aSelectedList;
262 //**************************************************************
263 QModelIndexList XGUI_Selection::selectedIndexes() const
265 return myWorkshop->objectBrowser()->selectedIndexes();
268 //**************************************************************
269 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
273 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
274 if (!aContext.IsNull()) {
275 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
276 theList.Append(aContext->SelectedInteractive());
280 //**************************************************************
281 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
285 Handle(SelectMgr_EntityOwner) aEO = theOwner;
287 Handle(AIS_InteractiveObject) anObj =
288 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
289 anObject = myWorkshop->displayer()->getObject(anObj);
294 //**************************************************************
295 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList,
296 std::list<ObjectPtr>& theOwners) const
299 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
300 if (aContext.IsNull())
303 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
304 TopoDS_Shape aShape = aContext->SelectedShape();
305 if (aShape.IsNull()) {
306 aShape = findAxisShape(aContext->SelectedInteractive());
308 if (!aShape.IsNull()) {
309 theList.Append(aShape);
310 Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
312 Handle(AIS_InteractiveObject) anObj =
313 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
314 ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
315 theOwners.push_back(anObject);
321 //**************************************************************
322 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
324 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
325 if (!aContext.IsNull()) {
326 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
327 theSelectedOwners.Add(aContext->SelectedOwner());
332 //**************************************************************
333 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
334 SelectMgr_IndexedMapOfOwner& theOwners) const
336 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
337 if (aContext.IsNull() || theObject.IsNull())
340 TColStd_ListOfInteger aModes;
341 aContext->ActivatedModes(theObject, aModes);
343 TColStd_ListIteratorOfListOfInteger anIt(aModes);
344 for (; anIt.More(); anIt.Next()) {
345 int aMode = anIt.Value();
346 if (!theObject->HasSelection(aMode))
349 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
350 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
351 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
352 if (anEntity.IsNull())
354 Handle(SelectMgr_EntityOwner) anOwner =
355 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
356 if (!anOwner.IsNull())
357 theOwners.Add(anOwner);
362 //**************************************************************
363 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
366 // Fill by trihedron shapes
367 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
368 if (!aAxis.IsNull()) {
369 // an Axis from Trihedron
370 Handle(Geom_Line) aLine = aAxis->Component();
371 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
372 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
374 BRep_Builder aBuilder;
376 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
380 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
381 if (!aPoint.IsNull()) {
382 // A point from trihedron
383 Handle(Geom_Point) aPnt = aPoint->Component();
384 BRep_Builder aBuilder;
385 TopoDS_Vertex aVertex;
386 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
387 if (!aVertex.IsNull())