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>
18 #include <ModelAPI_Session.h>
19 #include <ModelAPI_ResultConstruction.h>
21 #include <AIS_InteractiveContext.hxx>
22 #include <AIS_Axis.hxx>
23 #include <AIS_Point.hxx>
24 #include <Geom_Line.hxx>
25 #include <BRep_Builder.hxx>
26 #include <TopoDS_Edge.hxx>
27 #include <Geom_Point.hxx>
28 #include <Geom_TrimmedCurve.hxx>
29 #include <Prs3d_DatumAspect.hxx>
31 #include <TColStd_ListIteratorOfListOfInteger.hxx>
32 #include <StdSelect_BRepOwner.hxx>
36 #define DEBUG_DELIVERY
38 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
39 : myWorkshop(theWorkshop)
43 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
45 QList<ModuleBase_ViewerPrsPtr> aPresentations;
49 getSelectedInBrowser(aPresentations);
52 getSelectedInViewer(aPresentations);
55 getSelectedInViewer(aPresentations);
56 getSelectedInBrowser(aPresentations);
59 return aPresentations;
62 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPtr& thePrs)
64 Handle(AIS_InteractiveObject) anIO = thePrs->interactive();
66 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
67 if (!anOwner.IsNull())
68 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
70 if (anIO.IsNull() && thePrs->object()) {
71 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
72 AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs->object());
73 if (anAISObject.get())
74 anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
80 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
82 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
83 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
84 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
85 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
86 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
87 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
89 if (aSelectedIds.contains((long)anOwner.Access()))
91 aSelectedIds.append((long)anOwner.Access());
93 fillPresentation(aPrs, anOwner);
95 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
96 thePresentations.append(aPrs);
101 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
103 // collect the objects of the parameter presentation to avoid a repeted objects in the result
104 QObjectPtrList aPresentationObjects;
105 QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = thePresentations.begin(),
106 aPrsLast = thePresentations.end();
107 for (; aPrsIt != aPrsLast; aPrsIt++) {
108 aPresentationObjects.push_back((*aPrsIt)->object());
111 QObjectPtrList anObjects = selectedObjects();
112 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
113 for (; anIt != aLast; anIt++) {
114 ObjectPtr anObject = *anIt;
115 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
116 thePresentations.append(std::shared_ptr<ModuleBase_ViewerPrs>(
117 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
122 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
123 const Handle(SelectMgr_EntityOwner)& theOwner) const
125 thePrs->setOwner(theOwner);
127 Handle(AIS_InteractiveObject) anIO =
128 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
129 thePrs->setInteractive(anIO);
131 // we should not check the appearance of this feature because there can be some selected shapes
133 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
134 if( !aBRO.IsNull() && aBRO->HasShape() ) {
135 // the located method is called in the context to obtain the shape by the SelectedShape() method,
136 // so the shape is located by the same rules
137 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
138 #ifndef DEBUG_DELIVERY
140 aShape = findAxisShape(anIO);
142 if (!aShape.IsNull()) {
143 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
144 aGeomShape->setImpl(new TopoDS_Shape(aShape));
145 thePrs->setShape(aGeomShape);
148 #ifdef DEBUG_DELIVERY
149 // Fill by trihedron shapes
150 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
151 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
152 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
154 if (!aAxis.IsNull()) {
155 // an Axis from Trihedron
156 gp_Lin aLine = aAxis->Component()->Lin();
157 gp_Dir aDir = aLine.Direction();
161 else if (aDir.Y() == 1.)
163 else if (aDir.Z() == 1.)
165 if (aAxName.length() > 0) {
167 for (int i = 0; i < aSize; i++) {
168 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
169 if (aObj->data()->name() == aAxName) {
170 aAx = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
175 thePrs->setObject(aAx);
176 thePrs->setShape(aAx->shape());
181 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
182 if (!aPoint.IsNull()) {
183 // An origin point from trihedron
185 for (int i = 0; i < aSize; i++) {
186 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
187 if (aObj->data()->name() == "Origin") {
188 aOrigin = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
193 thePrs->setObject(aOrigin);
194 thePrs->setShape(aOrigin->shape());
202 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
203 ObjectPtr aFeature = aDisplayer->getObject(anIO);
205 Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
206 if (!aCompSolidBRO.IsNull()) {
207 // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
208 // is On and we have to use parent result which corresponds to the CompSolid shape
209 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
211 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(aResult);
212 if (aCompSolid.get()) {
213 GeomShapePtr aShape = aCompSolid->shape();
214 if (aShape.get() && aShape->isEqual(thePrs->shape())) {
215 thePrs->setObject(aCompSolid);
221 thePrs->setObject(aFeature);
224 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
226 QList<ModuleBase_ViewerPrsPtr> aPresentations;
227 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
228 if (aContext.IsNull())
229 return aPresentations;
231 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
232 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
233 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
234 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
235 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
236 if (aSelectedIds.contains((long)anIO.Access()))
239 aSelectedIds.append((long)anIO.Access());
240 aPrs->setInteractive(anIO);
242 ObjectPtr aResult = aDisplayer->getObject(anIO);
243 // we should not check the appearance of this feature because there can be some selected shapes
245 aPrs->setObject(aResult);
246 if (aContext->HasOpenedContext()) {
247 TopoDS_Shape aShape = aContext->DetectedShape();
248 if (!aShape.IsNull()) {
249 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
250 aGeomShape->setImpl(new TopoDS_Shape(aShape));
251 aPrs->setShape(aGeomShape);
254 aPresentations.push_back(aPrs);
256 return aPresentations;
259 QObjectPtrList XGUI_Selection::selectedObjects() const
261 return myWorkshop->objectBrowser()->selectedObjects();
264 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
266 return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
269 QObjectPtrList XGUI_Selection::selectedPresentations() const
271 QObjectPtrList aSelectedList;
273 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
274 if (!aContext.IsNull()) {
275 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
276 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
277 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
279 aSelectedList.append(aResult);
282 return aSelectedList;
285 //**************************************************************
286 QModelIndexList XGUI_Selection::selectedIndexes() const
288 return myWorkshop->objectBrowser()->selectedIndexes();
291 //**************************************************************
292 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
296 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
297 if (!aContext.IsNull()) {
298 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
299 theList.Append(aContext->SelectedInteractive());
303 //**************************************************************
304 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
308 Handle(SelectMgr_EntityOwner) aEO = theOwner;
310 Handle(AIS_InteractiveObject) anObj =
311 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
312 anObject = myWorkshop->displayer()->getObject(anObj);
317 //**************************************************************
318 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
320 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
321 if (!aContext.IsNull()) {
322 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
323 theSelectedOwners.Add(aContext->SelectedOwner());
328 //**************************************************************
329 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
330 SelectMgr_IndexedMapOfOwner& theOwners) const
332 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
333 if (aContext.IsNull() || theObject.IsNull())
336 TColStd_ListOfInteger aModes;
337 aContext->ActivatedModes(theObject, aModes);
339 TColStd_ListIteratorOfListOfInteger anIt(aModes);
340 for (; anIt.More(); anIt.Next()) {
341 int aMode = anIt.Value();
342 if (!theObject->HasSelection(aMode))
345 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
346 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
347 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
348 if (anEntity.IsNull())
350 Handle(SelectMgr_EntityOwner) anOwner =
351 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
352 if (!anOwner.IsNull())
353 theOwners.Add(anOwner);
358 //**************************************************************
359 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
362 // Fill by trihedron shapes
363 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
364 if (!aAxis.IsNull()) {
365 // an Axis from Trihedron
366 Handle(Geom_Line) aLine = aAxis->Component();
367 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
368 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
370 BRep_Builder aBuilder;
372 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
376 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
377 if (!aPoint.IsNull()) {
378 // A point from trihedron
379 Handle(Geom_Point) aPnt = aPoint->Component();
380 BRep_Builder aBuilder;
381 TopoDS_Vertex aVertex;
382 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
383 if (!aVertex.IsNull())