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"
14 #include <ModuleBase_ViewerPrs.h>
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_Tools.h>
19 #include <AIS_InteractiveContext.hxx>
20 #include <AIS_Axis.hxx>
21 #include <AIS_Point.hxx>
22 #include <Geom_Line.hxx>
23 #include <BRep_Builder.hxx>
24 #include <TopoDS_Edge.hxx>
25 #include <Geom_Point.hxx>
26 #include <Geom_TrimmedCurve.hxx>
27 #include <Prs3d_DatumAspect.hxx>
29 #include <TColStd_ListIteratorOfListOfInteger.hxx>
30 #include <StdSelect_BRepOwner.hxx>
34 #define DEBUG_DELIVERY
36 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
37 : myWorkshop(theWorkshop)
41 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
43 QList<ModuleBase_ViewerPrsPtr> aPresentations;
47 getSelectedInBrowser(aPresentations);
50 getSelectedInViewer(aPresentations);
53 getSelectedInViewer(aPresentations);
54 getSelectedInBrowser(aPresentations);
57 return aPresentations;
60 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPtr& thePrs)
62 Handle(AIS_InteractiveObject) anIO = thePrs->interactive();
64 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
65 if (!anOwner.IsNull())
66 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
68 if (anIO.IsNull() && thePrs->object()) {
69 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
70 AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs->object());
71 if (anAISObject.get())
72 anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
78 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
80 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
81 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
82 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
83 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
84 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
85 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
87 if (aSelectedIds.contains((long)anOwner.Access()))
89 aSelectedIds.append((long)anOwner.Access());
91 fillPresentation(aPrs, anOwner);
93 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
94 thePresentations.append(aPrs);
99 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
101 // collect the objects of the parameter presentation to avoid a repeted objects in the result
102 QObjectPtrList aPresentationObjects;
103 QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = thePresentations.begin(),
104 aPrsLast = thePresentations.end();
105 for (; aPrsIt != aPrsLast; aPrsIt++) {
106 aPresentationObjects.push_back((*aPrsIt)->object());
109 QObjectPtrList anObjects = selectedObjects();
110 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
111 for (; anIt != aLast; anIt++) {
112 ObjectPtr anObject = *anIt;
113 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
114 thePresentations.append(std::shared_ptr<ModuleBase_ViewerPrs>(
115 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
120 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
121 const Handle(SelectMgr_EntityOwner)& theOwner) const
123 thePrs->setOwner(theOwner);
125 Handle(AIS_InteractiveObject) anIO =
126 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
127 thePrs->setInteractive(anIO);
129 // we should not check the appearance of this feature because there can be some selected shapes
131 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
132 if( !aBRO.IsNull() && aBRO->HasShape() ) {
133 // the located method is called in the context to obtain the shape by the SelectedShape() method,
134 // so the shape is located by the same rules
135 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
136 #ifndef DEBUG_DELIVERY
138 aShape = findAxisShape(anIO);
140 if (!aShape.IsNull()) {
141 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
142 aGeomShape->setImpl(new TopoDS_Shape(aShape));
143 thePrs->setShape(aGeomShape);
146 #ifdef DEBUG_DELIVERY
147 // Fill by trihedron shapes
148 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
149 if (!aAxis.IsNull()) {
150 // an Axis from Trihedron
151 Handle(Geom_Line) aLine = aAxis->Component();
152 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
153 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
155 BRep_Builder aBuilder;
157 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
158 if (!aEdge.IsNull()) {
159 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
160 aGeomShape->setImpl(new TopoDS_Shape(aEdge));
161 thePrs->setShape(aGeomShape);
164 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
165 if (!aPoint.IsNull()) {
166 // A point from trihedron
167 Handle(Geom_Point) aPnt = aPoint->Component();
168 BRep_Builder aBuilder;
169 TopoDS_Vertex aVertex;
170 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
171 if (!aVertex.IsNull()) {
172 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
173 aGeomShape->setImpl(new TopoDS_Shape(aVertex));
174 thePrs->setShape(aGeomShape);
181 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
182 ObjectPtr aFeature = aDisplayer->getObject(anIO);
184 Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
185 if (!aCompSolidBRO.IsNull()) {
186 // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
187 // is On and we have to use parent result which corresponds to the CompSolid shape
188 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
190 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(aResult);
191 if (aCompSolid.get()) {
192 GeomShapePtr aShape = aCompSolid->shape();
193 if (aShape.get() && aShape->isEqual(thePrs->shape())) {
194 thePrs->setObject(aCompSolid);
200 thePrs->setObject(aFeature);
203 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
205 QList<ModuleBase_ViewerPrsPtr> aPresentations;
206 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
207 if (aContext.IsNull())
208 return aPresentations;
210 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
211 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
212 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
213 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
214 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
215 if (aSelectedIds.contains((long)anIO.Access()))
218 aSelectedIds.append((long)anIO.Access());
219 aPrs->setInteractive(anIO);
221 ObjectPtr aResult = aDisplayer->getObject(anIO);
222 // we should not check the appearance of this feature because there can be some selected shapes
224 aPrs->setObject(aResult);
225 if (aContext->HasOpenedContext()) {
226 TopoDS_Shape aShape = aContext->DetectedShape();
227 if (!aShape.IsNull()) {
228 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
229 aGeomShape->setImpl(new TopoDS_Shape(aShape));
230 aPrs->setShape(aGeomShape);
233 aPresentations.push_back(aPrs);
235 return aPresentations;
238 QObjectPtrList XGUI_Selection::selectedObjects() const
240 return myWorkshop->objectBrowser()->selectedObjects();
243 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
245 return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
248 QObjectPtrList XGUI_Selection::selectedPresentations() const
250 QObjectPtrList aSelectedList;
252 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
253 if (!aContext.IsNull()) {
254 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
255 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
256 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
258 aSelectedList.append(aResult);
261 return aSelectedList;
264 //**************************************************************
265 QModelIndexList XGUI_Selection::selectedIndexes() const
267 return myWorkshop->objectBrowser()->selectedIndexes();
270 //**************************************************************
271 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
275 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
276 if (!aContext.IsNull()) {
277 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
278 theList.Append(aContext->SelectedInteractive());
282 //**************************************************************
283 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
287 Handle(SelectMgr_EntityOwner) aEO = theOwner;
289 Handle(AIS_InteractiveObject) anObj =
290 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
291 anObject = myWorkshop->displayer()->getObject(anObj);
296 //**************************************************************
297 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList,
298 std::list<ObjectPtr>& theOwners) const
301 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
302 if (aContext.IsNull())
305 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
306 TopoDS_Shape aShape = aContext->SelectedShape();
307 if (aShape.IsNull()) {
308 aShape = findAxisShape(aContext->SelectedInteractive());
310 if (!aShape.IsNull()) {
311 theList.Append(aShape);
312 Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
314 Handle(AIS_InteractiveObject) anObj =
315 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
316 ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
317 theOwners.push_back(anObject);
323 //**************************************************************
324 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
326 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
327 if (!aContext.IsNull()) {
328 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
329 theSelectedOwners.Add(aContext->SelectedOwner());
334 //**************************************************************
335 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
336 SelectMgr_IndexedMapOfOwner& theOwners) const
338 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
339 if (aContext.IsNull() || theObject.IsNull())
342 TColStd_ListOfInteger aModes;
343 aContext->ActivatedModes(theObject, aModes);
345 TColStd_ListIteratorOfListOfInteger anIt(aModes);
346 for (; anIt.More(); anIt.Next()) {
347 int aMode = anIt.Value();
348 if (!theObject->HasSelection(aMode))
351 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
352 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
353 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
354 if (anEntity.IsNull())
356 Handle(SelectMgr_EntityOwner) anOwner =
357 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
358 if (!anOwner.IsNull())
359 theOwners.Add(anOwner);
364 //**************************************************************
365 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
368 // Fill by trihedron shapes
369 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
370 if (!aAxis.IsNull()) {
371 // an Axis from Trihedron
372 Handle(Geom_Line) aLine = aAxis->Component();
373 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
374 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
376 BRep_Builder aBuilder;
378 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
382 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
383 if (!aPoint.IsNull()) {
384 // A point from trihedron
385 Handle(Geom_Point) aPnt = aPoint->Component();
386 BRep_Builder aBuilder;
387 TopoDS_Vertex aVertex;
388 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
389 if (!aVertex.IsNull())