1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "XGUI_Selection.h"
21 #include "XGUI_Workshop.h"
22 #include "XGUI_Displayer.h"
23 #include "XGUI_ViewerProxy.h"
24 #include "XGUI_ObjectsBrowser.h"
27 #include <AppElements_ViewCube.h>
30 #include "ModuleBase_BRepOwner.h"
31 #include "ModuleBase_ResultPrs.h"
32 #include "ModuleBase_ViewerPrs.h"
34 #include <ModelAPI_Feature.h>
35 #include <ModelAPI_Tools.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_ResultBody.h>
38 #include <ModelAPI_ResultConstruction.h>
40 #include <AIS_InteractiveContext.hxx>
41 #ifdef BEFORE_TRIHEDRON_PATCH
42 #include <AIS_Axis.hxx>
43 #include <AIS_Point.hxx>
45 #include <AIS_TrihedronOwner.hxx>
47 #include <Geom_Line.hxx>
48 #include <BRep_Builder.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <Geom_Point.hxx>
51 #include <Geom_TrimmedCurve.hxx>
52 #include <Prs3d_DatumAspect.hxx>
54 #include <TColStd_ListIteratorOfListOfInteger.hxx>
55 #include <StdSelect_BRepOwner.hxx>
59 #define DEBUG_DELIVERY
61 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
62 : myWorkshop(theWorkshop)
66 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
68 QList<ModuleBase_ViewerPrsPtr> aPresentations;
69 QList<ModuleBase_ViewerPrsPtr> aToRemove;
73 getSelectedInBrowser(aPresentations);
76 getSelectedInViewer(aPresentations);
79 // Get selection from object browser
80 getSelectedInBrowser(aPresentations);
82 // Filter out all objects except feature if there is no selected results in object browser
83 // Filter out all features if in object browser there are selected features and their results
84 bool aHasFeature = false;
85 bool aHasResult = false;
86 foreach(ModuleBase_ViewerPrsPtr aVal, aPresentations) {
87 if (aVal->object().get()) {
88 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aVal->object());
91 std::list<ResultPtr> aResList = aFeature->results();
92 std::list<ResultPtr>::const_iterator aIt;
93 for (aIt = aResList.cbegin(); aIt != aResList.cend(); aIt++) {
94 foreach(ModuleBase_ViewerPrsPtr aSel, aPresentations) {
95 if (aSel->object() == (*aIt)) {
105 if (aHasFeature && aHasResult)
108 //Get selection from a viewer
109 getSelectedInViewer(aPresentations);
111 // Filter out extra objects
112 if (aHasFeature && aHasResult) {
113 foreach(ModuleBase_ViewerPrsPtr aVal, aPresentations) {
114 if (aVal->object().get()) {
115 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aVal->object());
116 if (aFeature.get()) {
117 aToRemove.append(aVal);
122 else if (aHasFeature && (!aHasResult)) {
123 foreach(ModuleBase_ViewerPrsPtr aVal, aPresentations) {
124 if (aVal->object().get()) {
125 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aVal->object());
126 if (!aFeature.get()) {
127 aToRemove.append(aVal);
132 foreach(ModuleBase_ViewerPrsPtr aVal, aToRemove) {
133 aPresentations.removeAll(aVal);
137 return aPresentations;
140 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPtr& thePrs)
142 Handle(AIS_InteractiveObject) anIO = thePrs->interactive();
144 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
145 if (!anOwner.IsNull())
146 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
148 if (anIO.IsNull() && thePrs->object()) {
149 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
150 AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs->object());
151 if (anAISObject.get())
152 anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
158 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
160 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
161 if (!aContext.IsNull()) {
162 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
163 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
164 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
165 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
167 if (aSelectedIds.contains((size_t)anOwner.get()))
169 aSelectedIds.append((size_t)anOwner.get());
171 fillPresentation(aPrs, anOwner);
173 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
174 thePresentations.append(aPrs);
179 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
181 // collect the objects of the parameter presentation to avoid a repeted objects in the result
182 QObjectPtrList aPresentationObjects;
183 QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = thePresentations.begin(),
184 aPrsLast = thePresentations.end();
185 for (; aPrsIt != aPrsLast; aPrsIt++) {
186 aPresentationObjects.push_back((*aPrsIt)->object());
189 QObjectPtrList anObjects = selectedObjects();
190 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
191 for (; anIt != aLast; anIt++) {
192 ObjectPtr anObject = *anIt;
193 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
194 thePresentations.append(std::shared_ptr<ModuleBase_ViewerPrs>(
195 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
200 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
201 const Handle(SelectMgr_EntityOwner)& theOwner) const
203 Handle(AIS_InteractiveObject) anIO =
204 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
206 Handle(AppElements_ViewCube) aCube = Handle(AppElements_ViewCube)::DownCast(anIO);
211 thePrs->setOwner(theOwner);
212 thePrs->setInteractive(anIO);
214 // we should not check the appearance of this feature because there can be some selected shapes
216 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
217 if( !aBRO.IsNull() && aBRO->HasShape() ) {
218 TopoDS_Shape aShape = aBRO->Shape();
219 Handle(ModuleBase_ResultPrs) aPrsObj =
220 Handle(ModuleBase_ResultPrs)::DownCast(aBRO->Selectable());
221 if (!aPrsObj.IsNull()) {
222 if (aPrsObj->isSubstituted()) {
223 if (aPrsObj->Shape().IsSame(aShape))
224 aShape = aPrsObj->originalShape();
227 // the located method is called in the context to obtain the shape by the SelectedShape()
228 // method, so the shape is located by the same rules
229 aShape = aShape.Located(aBRO->Location() * aShape.Location());
230 #ifdef BEFORE_TRIHEDRON_PATCH
231 #ifndef DEBUG_DELIVERY
233 aShape = findAxisShape(anIO);
236 if (!aShape.IsNull()) {
237 std::shared_ptr<GeomAPI_Shape> aGeomShape =
238 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
239 aGeomShape->setImpl(new TopoDS_Shape(aShape));
240 thePrs->setShape(aGeomShape);
243 #ifdef DEBUG_DELIVERY
244 // Fill by trihedron shapes
245 #ifdef BEFORE_TRIHEDRON_PATCH
246 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
247 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
248 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
250 if (!aAxis.IsNull()) {
251 // an Axis from Trihedron
252 gp_Lin aLine = aAxis->Component()->Lin();
253 gp_Dir aDir = aLine.Direction();
257 else if (aDir.Y() == 1.)
259 else if (aDir.Z() == 1.)
261 if (aAxName.length() > 0) {
263 for (int i = 0; i < aSize; i++) {
264 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
265 if (aObj->data()->name() == aAxName) {
266 aAx = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
271 thePrs->setObject(aAx);
272 thePrs->setShape(aAx->shape());
277 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
278 if (!aPoint.IsNull()) {
279 // An origin point from trihedron
281 for (int i = 0; i < aSize; i++) {
282 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
283 if (aObj->data()->name() == "Origin") {
284 aOrigin = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
289 thePrs->setObject(aOrigin);
290 thePrs->setShape(aOrigin->shape());
296 /// find axis direction
297 Handle(AIS_TrihedronOwner) aTrihedronOwner = Handle(AIS_TrihedronOwner)::DownCast(theOwner);
298 if (!aTrihedronOwner.IsNull()) {
299 const Prs3d_DatumParts& aPart = aTrihedronOwner->DatumPart();
302 case Prs3d_DP_Origin: aName = "Origin"; break;
303 case Prs3d_DP_XAxis: aName = "OX"; break;
304 case Prs3d_DP_YAxis: aName = "OY"; break;
305 case Prs3d_DP_ZAxis: aName = "OZ"; break;
308 if (aName.length() > 0) {
309 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
310 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
313 for (int i = 0; i < aSize; i++) {
314 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
315 if (aObj->data()->name() == aName) {
316 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
321 thePrs->setObject(aResult);
322 thePrs->setShape(aResult->shape());
331 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
332 ObjectPtr aFeature = aDisplayer->getObject(anIO);
334 Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
335 if (!aCompSolidBRO.IsNull()) {
336 // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
337 // is On and we have to use parent result which corresponds to the CompSolid shape
338 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
340 ResultBodyPtr aCompSolid = ModelAPI_Tools::bodyOwner(aResult);
341 if (aCompSolid.get()) {
342 GeomShapePtr aShape = aCompSolid->shape();
343 if (aShape.get() && aShape->isEqual(thePrs->shape())) {
344 thePrs->setObject(aCompSolid);
350 thePrs->setObject(aFeature);
353 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
355 QList<ModuleBase_ViewerPrsPtr> aPresentations;
356 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
357 if (aContext.IsNull())
358 return aPresentations;
360 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
361 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
362 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
363 Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
364 if (!anOwner.IsNull()) {
365 if (aSelectedIds.contains((size_t)anOwner.get()))
367 aSelectedIds.append((size_t)anOwner.get());
369 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
370 fillPresentation(aPrs, anOwner);
371 aPresentations.push_back(aPrs);
374 return aPresentations;
377 QObjectPtrList XGUI_Selection::selectedObjects() const
379 if (myWorkshop->objectBrowser())
380 return myWorkshop->objectBrowser()->selectedObjects();
381 return QObjectPtrList();
384 QObjectPtrList XGUI_Selection::selectedPresentations() const
386 QObjectPtrList aSelectedList;
388 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
389 if (!aContext.IsNull()) {
390 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
391 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
392 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
394 aSelectedList.append(aResult);
397 return aSelectedList;
400 //**************************************************************
401 QModelIndexList XGUI_Selection::selectedIndexes() const
403 return myWorkshop->objectBrowser()->selectedIndexes();
406 //**************************************************************
407 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
411 Handle(SelectMgr_EntityOwner) aEO = theOwner;
413 Handle(AIS_InteractiveObject) anObj =
414 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
415 anObject = myWorkshop->displayer()->getObject(anObj);
420 //**************************************************************
421 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
423 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
424 if (!aContext.IsNull()) {
425 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
426 theSelectedOwners.Add(aContext->SelectedOwner());
431 //**************************************************************
432 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
433 SelectMgr_IndexedMapOfOwner& theOwners) const
435 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
436 if (aContext.IsNull() || theObject.IsNull())
439 TColStd_ListOfInteger aModes;
440 aContext->ActivatedModes(theObject, aModes);
442 TColStd_ListIteratorOfListOfInteger anIt(aModes);
443 for (; anIt.More(); anIt.Next()) {
444 int aMode = anIt.Value();
445 if (!theObject->HasSelection(aMode))
448 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
449 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
450 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
453 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
454 if (anEntity.IsNull())
456 Handle(SelectMgr_EntityOwner) anOwner =
457 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
458 if (!anOwner.IsNull())
459 theOwners.Add(anOwner);
464 //**************************************************************
465 #ifdef BEFORE_TRIHEDRON_PATCH
466 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
469 // Fill by trihedron shapes
470 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
471 if (!aAxis.IsNull()) {
472 // an Axis from Trihedron
473 Handle(Geom_Line) aLine = aAxis->Component();
474 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
475 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
477 BRep_Builder aBuilder;
479 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
483 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
484 if (!aPoint.IsNull()) {
485 // A point from trihedron
486 Handle(Geom_Point) aPnt = aPoint->Component();
487 BRep_Builder aBuilder;
488 TopoDS_Vertex aVertex;
489 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
490 if (!aVertex.IsNull())