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_ResultCompSolid.h>
20 #include <ModelAPI_ResultConstruction.h>
22 #include <AIS_InteractiveContext.hxx>
23 #ifdef BEFORE_TRIHEDRON_PATCH
24 #include <AIS_Axis.hxx>
25 #include <AIS_Point.hxx>
27 #include <AIS_TrihedronOwner.hxx>
29 #include <Geom_Line.hxx>
30 #include <BRep_Builder.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <Geom_Point.hxx>
33 #include <Geom_TrimmedCurve.hxx>
34 #include <Prs3d_DatumAspect.hxx>
36 #include <TColStd_ListIteratorOfListOfInteger.hxx>
37 #include <StdSelect_BRepOwner.hxx>
41 #define DEBUG_DELIVERY
43 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
44 : myWorkshop(theWorkshop)
48 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
50 QList<ModuleBase_ViewerPrsPtr> aPresentations;
54 getSelectedInBrowser(aPresentations);
57 getSelectedInViewer(aPresentations);
60 getSelectedInViewer(aPresentations);
61 getSelectedInBrowser(aPresentations);
64 return aPresentations;
67 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPtr& thePrs)
69 Handle(AIS_InteractiveObject) anIO = thePrs->interactive();
71 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
72 if (!anOwner.IsNull())
73 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
75 if (anIO.IsNull() && thePrs->object()) {
76 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
77 AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs->object());
78 if (anAISObject.get())
79 anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
85 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
87 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
88 if (!aContext.IsNull()) {
89 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
90 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
91 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
92 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
94 if (aSelectedIds.contains((long)anOwner.get()))
96 aSelectedIds.append((long)anOwner.get());
98 fillPresentation(aPrs, anOwner);
100 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
101 thePresentations.append(aPrs);
106 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
108 // collect the objects of the parameter presentation to avoid a repeted objects in the result
109 QObjectPtrList aPresentationObjects;
110 QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = thePresentations.begin(),
111 aPrsLast = thePresentations.end();
112 for (; aPrsIt != aPrsLast; aPrsIt++) {
113 aPresentationObjects.push_back((*aPrsIt)->object());
116 QObjectPtrList anObjects = selectedObjects();
117 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
118 for (; anIt != aLast; anIt++) {
119 ObjectPtr anObject = *anIt;
120 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
121 thePresentations.append(std::shared_ptr<ModuleBase_ViewerPrs>(
122 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
127 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
128 const Handle(SelectMgr_EntityOwner)& theOwner) const
130 thePrs->setOwner(theOwner);
131 Handle(SelectMgr_SelectableObject) aSelectable = theOwner->Selectable();
132 Handle(AIS_InteractiveObject) anIO =
133 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
134 thePrs->setInteractive(anIO);
136 // we should not check the appearance of this feature because there can be some selected shapes
138 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
139 if( !aBRO.IsNull() && aBRO->HasShape() ) {
140 // the located method is called in the context to obtain the shape by the SelectedShape()
141 // method, so the shape is located by the same rules
142 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
143 #ifdef BEFORE_TRIHEDRON_PATCH
144 #ifndef DEBUG_DELIVERY
146 aShape = findAxisShape(anIO);
149 if (!aShape.IsNull()) {
150 std::shared_ptr<GeomAPI_Shape> aGeomShape =
151 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
152 aGeomShape->setImpl(new TopoDS_Shape(aShape));
153 thePrs->setShape(aGeomShape);
156 #ifdef DEBUG_DELIVERY
157 // Fill by trihedron shapes
158 #ifdef BEFORE_TRIHEDRON_PATCH
159 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
160 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
161 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
163 if (!aAxis.IsNull()) {
164 // an Axis from Trihedron
165 gp_Lin aLine = aAxis->Component()->Lin();
166 gp_Dir aDir = aLine.Direction();
170 else if (aDir.Y() == 1.)
172 else if (aDir.Z() == 1.)
174 if (aAxName.length() > 0) {
176 for (int i = 0; i < aSize; i++) {
177 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
178 if (aObj->data()->name() == aAxName) {
179 aAx = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
184 thePrs->setObject(aAx);
185 thePrs->setShape(aAx->shape());
190 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
191 if (!aPoint.IsNull()) {
192 // An origin point from trihedron
194 for (int i = 0; i < aSize; i++) {
195 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
196 if (aObj->data()->name() == "Origin") {
197 aOrigin = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
202 thePrs->setObject(aOrigin);
203 thePrs->setShape(aOrigin->shape());
209 /// find axis direction
210 Handle(AIS_TrihedronOwner) aTrihedronOwner = Handle(AIS_TrihedronOwner)::DownCast(theOwner);
211 if (!aTrihedronOwner.IsNull()) {
212 const Prs3d_DatumParts& aPart = aTrihedronOwner->DatumPart();
215 case Prs3d_DP_Origin: aName = "Origin"; break;
216 case Prs3d_DP_XAxis: aName = "OX"; break;
217 case Prs3d_DP_YAxis: aName = "OY"; break;
218 case Prs3d_DP_ZAxis: aName = "OZ"; break;
221 if (aName.length() > 0) {
222 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
223 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
226 for (int i = 0; i < aSize; i++) {
227 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
228 if (aObj->data()->name() == aName) {
229 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
234 thePrs->setObject(aResult);
235 thePrs->setShape(aResult->shape());
244 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
245 ObjectPtr aFeature = aDisplayer->getObject(anIO);
247 Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
248 if (!aCompSolidBRO.IsNull()) {
249 // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
250 // is On and we have to use parent result which corresponds to the CompSolid shape
251 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
253 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(aResult);
254 if (aCompSolid.get()) {
255 GeomShapePtr aShape = aCompSolid->shape();
256 if (aShape.get() && aShape->isEqual(thePrs->shape())) {
257 thePrs->setObject(aCompSolid);
263 thePrs->setObject(aFeature);
266 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
268 QList<ModuleBase_ViewerPrsPtr> aPresentations;
269 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
270 if (aContext.IsNull())
271 return aPresentations;
273 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
274 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
275 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
276 Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
277 if (!anOwner.IsNull()) {
278 if (aSelectedIds.contains((long)anOwner.get()))
280 aSelectedIds.append((long)anOwner.get());
282 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
283 fillPresentation(aPrs, anOwner);
284 aPresentations.push_back(aPrs);
287 return aPresentations;
290 QObjectPtrList XGUI_Selection::selectedObjects() const
292 if (myWorkshop->objectBrowser())
293 return myWorkshop->objectBrowser()->selectedObjects();
294 return QObjectPtrList();
297 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
299 return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
302 QObjectPtrList XGUI_Selection::selectedPresentations() const
304 QObjectPtrList aSelectedList;
306 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
307 if (!aContext.IsNull()) {
308 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
309 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
310 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
312 aSelectedList.append(aResult);
315 return aSelectedList;
318 //**************************************************************
319 QModelIndexList XGUI_Selection::selectedIndexes() const
321 return myWorkshop->objectBrowser()->selectedIndexes();
324 //**************************************************************
325 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
329 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
330 if (!aContext.IsNull()) {
331 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
332 theList.Append(aContext->SelectedInteractive());
336 //**************************************************************
337 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
341 Handle(SelectMgr_EntityOwner) aEO = theOwner;
343 Handle(AIS_InteractiveObject) anObj =
344 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
345 anObject = myWorkshop->displayer()->getObject(anObj);
350 //**************************************************************
351 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
353 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
354 if (!aContext.IsNull()) {
355 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
356 theSelectedOwners.Add(aContext->SelectedOwner());
361 //**************************************************************
362 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
363 SelectMgr_IndexedMapOfOwner& theOwners) const
365 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
366 if (aContext.IsNull() || theObject.IsNull())
369 TColStd_ListOfInteger aModes;
370 aContext->ActivatedModes(theObject, aModes);
372 TColStd_ListIteratorOfListOfInteger anIt(aModes);
373 for (; anIt.More(); anIt.Next()) {
374 int aMode = anIt.Value();
375 if (!theObject->HasSelection(aMode))
378 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
379 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
380 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
381 if (anEntity.IsNull())
383 Handle(SelectMgr_EntityOwner) anOwner =
384 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
385 if (!anOwner.IsNull())
386 theOwners.Add(anOwner);
391 //**************************************************************
392 #ifdef BEFORE_TRIHEDRON_PATCH
393 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
396 // Fill by trihedron shapes
397 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
398 if (!aAxis.IsNull()) {
399 // an Axis from Trihedron
400 Handle(Geom_Line) aLine = aAxis->Component();
401 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
402 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
404 BRep_Builder aBuilder;
406 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
410 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
411 if (!aPoint.IsNull()) {
412 // A point from trihedron
413 Handle(Geom_Point) aPnt = aPoint->Component();
414 BRep_Builder aBuilder;
415 TopoDS_Vertex aVertex;
416 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
417 if (!aVertex.IsNull())