1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "XGUI_Selection.h"
22 #include "XGUI_Workshop.h"
23 #include "XGUI_Displayer.h"
24 #include "XGUI_ViewerProxy.h"
25 #include "XGUI_ObjectsBrowser.h"
27 #include "ModuleBase_BRepOwner.h"
28 #include "ModuleBase_ResultPrs.h"
29 #include "ModuleBase_ViewerPrs.h"
31 #include <ModelAPI_Feature.h>
32 #include <ModelAPI_Tools.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_ResultBody.h>
35 #include <ModelAPI_ResultConstruction.h>
37 #include <AIS_InteractiveContext.hxx>
38 #ifdef BEFORE_TRIHEDRON_PATCH
39 #include <AIS_Axis.hxx>
40 #include <AIS_Point.hxx>
42 #include <AIS_TrihedronOwner.hxx>
44 #include <Geom_Line.hxx>
45 #include <BRep_Builder.hxx>
46 #include <TopoDS_Edge.hxx>
47 #include <Geom_Point.hxx>
48 #include <Geom_TrimmedCurve.hxx>
49 #include <Prs3d_DatumAspect.hxx>
51 #include <TColStd_ListIteratorOfListOfInteger.hxx>
52 #include <StdSelect_BRepOwner.hxx>
56 #define DEBUG_DELIVERY
58 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
59 : myWorkshop(theWorkshop)
63 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
65 QList<ModuleBase_ViewerPrsPtr> aPresentations;
69 getSelectedInBrowser(aPresentations);
72 getSelectedInViewer(aPresentations);
75 getSelectedInViewer(aPresentations);
76 getSelectedInBrowser(aPresentations);
79 return aPresentations;
82 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPtr& thePrs)
84 Handle(AIS_InteractiveObject) anIO = thePrs->interactive();
86 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
87 if (!anOwner.IsNull())
88 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
90 if (anIO.IsNull() && thePrs->object()) {
91 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
92 AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs->object());
93 if (anAISObject.get())
94 anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
100 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
102 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
103 if (!aContext.IsNull()) {
104 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
105 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
106 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
107 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
109 if (aSelectedIds.contains((long)anOwner.get()))
111 aSelectedIds.append((long)anOwner.get());
113 fillPresentation(aPrs, anOwner);
115 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
116 thePresentations.append(aPrs);
121 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
123 // collect the objects of the parameter presentation to avoid a repeted objects in the result
124 QObjectPtrList aPresentationObjects;
125 QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = thePresentations.begin(),
126 aPrsLast = thePresentations.end();
127 for (; aPrsIt != aPrsLast; aPrsIt++) {
128 aPresentationObjects.push_back((*aPrsIt)->object());
131 QObjectPtrList anObjects = selectedObjects();
132 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
133 for (; anIt != aLast; anIt++) {
134 ObjectPtr anObject = *anIt;
135 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
136 thePresentations.append(std::shared_ptr<ModuleBase_ViewerPrs>(
137 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
142 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
143 const Handle(SelectMgr_EntityOwner)& theOwner) const
145 thePrs->setOwner(theOwner);
146 Handle(AIS_InteractiveObject) anIO =
147 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
148 thePrs->setInteractive(anIO);
150 // we should not check the appearance of this feature because there can be some selected shapes
152 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
153 if( !aBRO.IsNull() && aBRO->HasShape() ) {
154 // the located method is called in the context to obtain the shape by the SelectedShape()
155 // method, so the shape is located by the same rules
156 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
157 #ifdef BEFORE_TRIHEDRON_PATCH
158 #ifndef DEBUG_DELIVERY
160 aShape = findAxisShape(anIO);
163 if (!aShape.IsNull()) {
164 std::shared_ptr<GeomAPI_Shape> aGeomShape =
165 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
166 aGeomShape->setImpl(new TopoDS_Shape(aShape));
167 thePrs->setShape(aGeomShape);
170 #ifdef DEBUG_DELIVERY
171 // Fill by trihedron shapes
172 #ifdef BEFORE_TRIHEDRON_PATCH
173 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
174 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
175 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
177 if (!aAxis.IsNull()) {
178 // an Axis from Trihedron
179 gp_Lin aLine = aAxis->Component()->Lin();
180 gp_Dir aDir = aLine.Direction();
184 else if (aDir.Y() == 1.)
186 else if (aDir.Z() == 1.)
188 if (aAxName.length() > 0) {
190 for (int i = 0; i < aSize; i++) {
191 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
192 if (aObj->data()->name() == aAxName) {
193 aAx = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
198 thePrs->setObject(aAx);
199 thePrs->setShape(aAx->shape());
204 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
205 if (!aPoint.IsNull()) {
206 // An origin point from trihedron
208 for (int i = 0; i < aSize; i++) {
209 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
210 if (aObj->data()->name() == "Origin") {
211 aOrigin = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
216 thePrs->setObject(aOrigin);
217 thePrs->setShape(aOrigin->shape());
223 /// find axis direction
224 Handle(AIS_TrihedronOwner) aTrihedronOwner = Handle(AIS_TrihedronOwner)::DownCast(theOwner);
225 if (!aTrihedronOwner.IsNull()) {
226 const Prs3d_DatumParts& aPart = aTrihedronOwner->DatumPart();
229 case Prs3d_DP_Origin: aName = "Origin"; break;
230 case Prs3d_DP_XAxis: aName = "OX"; break;
231 case Prs3d_DP_YAxis: aName = "OY"; break;
232 case Prs3d_DP_ZAxis: aName = "OZ"; break;
235 if (aName.length() > 0) {
236 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
237 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
240 for (int i = 0; i < aSize; i++) {
241 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
242 if (aObj->data()->name() == aName) {
243 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
248 thePrs->setObject(aResult);
249 thePrs->setShape(aResult->shape());
258 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
259 ObjectPtr aFeature = aDisplayer->getObject(anIO);
261 Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
262 if (!aCompSolidBRO.IsNull()) {
263 // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
264 // is On and we have to use parent result which corresponds to the CompSolid shape
265 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
267 ResultBodyPtr aCompSolid = ModelAPI_Tools::bodyOwner(aResult);
268 if (aCompSolid.get()) {
269 GeomShapePtr aShape = aCompSolid->shape();
270 if (aShape.get() && aShape->isEqual(thePrs->shape())) {
271 thePrs->setObject(aCompSolid);
277 thePrs->setObject(aFeature);
280 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
282 QList<ModuleBase_ViewerPrsPtr> aPresentations;
283 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
284 if (aContext.IsNull())
285 return aPresentations;
287 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
288 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
289 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
290 Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
291 if (!anOwner.IsNull()) {
292 if (aSelectedIds.contains((long)anOwner.get()))
294 aSelectedIds.append((long)anOwner.get());
296 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
297 fillPresentation(aPrs, anOwner);
298 aPresentations.push_back(aPrs);
301 return aPresentations;
304 QObjectPtrList XGUI_Selection::selectedObjects() const
306 if (myWorkshop->objectBrowser())
307 return myWorkshop->objectBrowser()->selectedObjects();
308 return QObjectPtrList();
311 QObjectPtrList XGUI_Selection::selectedPresentations() const
313 QObjectPtrList aSelectedList;
315 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
316 if (!aContext.IsNull()) {
317 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
318 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
319 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
321 aSelectedList.append(aResult);
324 return aSelectedList;
327 //**************************************************************
328 QModelIndexList XGUI_Selection::selectedIndexes() const
330 return myWorkshop->objectBrowser()->selectedIndexes();
333 //**************************************************************
334 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
338 Handle(SelectMgr_EntityOwner) aEO = theOwner;
340 Handle(AIS_InteractiveObject) anObj =
341 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
342 anObject = myWorkshop->displayer()->getObject(anObj);
347 //**************************************************************
348 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
350 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
351 if (!aContext.IsNull()) {
352 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
353 theSelectedOwners.Add(aContext->SelectedOwner());
358 //**************************************************************
359 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
360 SelectMgr_IndexedMapOfOwner& theOwners) const
362 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
363 if (aContext.IsNull() || theObject.IsNull())
366 TColStd_ListOfInteger aModes;
367 aContext->ActivatedModes(theObject, aModes);
369 TColStd_ListIteratorOfListOfInteger anIt(aModes);
370 for (; anIt.More(); anIt.Next()) {
371 int aMode = anIt.Value();
372 if (!theObject->HasSelection(aMode))
375 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
376 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
377 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
378 if (anEntity.IsNull())
380 Handle(SelectMgr_EntityOwner) anOwner =
381 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
382 if (!anOwner.IsNull())
383 theOwners.Add(anOwner);
388 //**************************************************************
389 #ifdef BEFORE_TRIHEDRON_PATCH
390 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
393 // Fill by trihedron shapes
394 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
395 if (!aAxis.IsNull()) {
396 // an Axis from Trihedron
397 Handle(Geom_Line) aLine = aAxis->Component();
398 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
399 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
401 BRep_Builder aBuilder;
403 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
407 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
408 if (!aPoint.IsNull()) {
409 // A point from trihedron
410 Handle(Geom_Point) aPnt = aPoint->Component();
411 BRep_Builder aBuilder;
412 TopoDS_Vertex aVertex;
413 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
414 if (!aVertex.IsNull())