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_ResultCompSolid.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(SelectMgr_SelectableObject) aSelectable = theOwner->Selectable();
147 Handle(AIS_InteractiveObject) anIO =
148 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
149 thePrs->setInteractive(anIO);
151 // we should not check the appearance of this feature because there can be some selected shapes
153 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
154 if( !aBRO.IsNull() && aBRO->HasShape() ) {
155 // the located method is called in the context to obtain the shape by the SelectedShape()
156 // method, so the shape is located by the same rules
157 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
158 #ifdef BEFORE_TRIHEDRON_PATCH
159 #ifndef DEBUG_DELIVERY
161 aShape = findAxisShape(anIO);
164 if (!aShape.IsNull()) {
165 std::shared_ptr<GeomAPI_Shape> aGeomShape =
166 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
167 aGeomShape->setImpl(new TopoDS_Shape(aShape));
168 thePrs->setShape(aGeomShape);
171 #ifdef DEBUG_DELIVERY
172 // Fill by trihedron shapes
173 #ifdef BEFORE_TRIHEDRON_PATCH
174 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
175 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
176 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
178 if (!aAxis.IsNull()) {
179 // an Axis from Trihedron
180 gp_Lin aLine = aAxis->Component()->Lin();
181 gp_Dir aDir = aLine.Direction();
185 else if (aDir.Y() == 1.)
187 else if (aDir.Z() == 1.)
189 if (aAxName.length() > 0) {
191 for (int i = 0; i < aSize; i++) {
192 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
193 if (aObj->data()->name() == aAxName) {
194 aAx = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
199 thePrs->setObject(aAx);
200 thePrs->setShape(aAx->shape());
205 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
206 if (!aPoint.IsNull()) {
207 // An origin point from trihedron
209 for (int i = 0; i < aSize; i++) {
210 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
211 if (aObj->data()->name() == "Origin") {
212 aOrigin = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
217 thePrs->setObject(aOrigin);
218 thePrs->setShape(aOrigin->shape());
224 /// find axis direction
225 Handle(AIS_TrihedronOwner) aTrihedronOwner = Handle(AIS_TrihedronOwner)::DownCast(theOwner);
226 if (!aTrihedronOwner.IsNull()) {
227 const Prs3d_DatumParts& aPart = aTrihedronOwner->DatumPart();
230 case Prs3d_DP_Origin: aName = "Origin"; break;
231 case Prs3d_DP_XAxis: aName = "OX"; break;
232 case Prs3d_DP_YAxis: aName = "OY"; break;
233 case Prs3d_DP_ZAxis: aName = "OZ"; break;
236 if (aName.length() > 0) {
237 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
238 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
241 for (int i = 0; i < aSize; i++) {
242 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
243 if (aObj->data()->name() == aName) {
244 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
249 thePrs->setObject(aResult);
250 thePrs->setShape(aResult->shape());
259 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
260 ObjectPtr aFeature = aDisplayer->getObject(anIO);
262 Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
263 if (!aCompSolidBRO.IsNull()) {
264 // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
265 // is On and we have to use parent result which corresponds to the CompSolid shape
266 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
268 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(aResult);
269 if (aCompSolid.get()) {
270 GeomShapePtr aShape = aCompSolid->shape();
271 if (aShape.get() && aShape->isEqual(thePrs->shape())) {
272 thePrs->setObject(aCompSolid);
278 thePrs->setObject(aFeature);
281 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
283 QList<ModuleBase_ViewerPrsPtr> aPresentations;
284 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
285 if (aContext.IsNull())
286 return aPresentations;
288 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
289 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
290 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
291 Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
292 if (!anOwner.IsNull()) {
293 if (aSelectedIds.contains((long)anOwner.get()))
295 aSelectedIds.append((long)anOwner.get());
297 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
298 fillPresentation(aPrs, anOwner);
299 aPresentations.push_back(aPrs);
302 return aPresentations;
305 QObjectPtrList XGUI_Selection::selectedObjects() const
307 if (myWorkshop->objectBrowser())
308 return myWorkshop->objectBrowser()->selectedObjects();
309 return QObjectPtrList();
312 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
314 return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
317 QObjectPtrList XGUI_Selection::selectedPresentations() const
319 QObjectPtrList aSelectedList;
321 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
322 if (!aContext.IsNull()) {
323 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
324 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
325 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
327 aSelectedList.append(aResult);
330 return aSelectedList;
333 //**************************************************************
334 QModelIndexList XGUI_Selection::selectedIndexes() const
336 return myWorkshop->objectBrowser()->selectedIndexes();
339 //**************************************************************
340 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
344 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
345 if (!aContext.IsNull()) {
346 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
347 theList.Append(aContext->SelectedInteractive());
351 //**************************************************************
352 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
356 Handle(SelectMgr_EntityOwner) aEO = theOwner;
358 Handle(AIS_InteractiveObject) anObj =
359 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
360 anObject = myWorkshop->displayer()->getObject(anObj);
365 //**************************************************************
366 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
368 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
369 if (!aContext.IsNull()) {
370 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
371 theSelectedOwners.Add(aContext->SelectedOwner());
376 //**************************************************************
377 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
378 SelectMgr_IndexedMapOfOwner& theOwners) const
380 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
381 if (aContext.IsNull() || theObject.IsNull())
384 TColStd_ListOfInteger aModes;
385 aContext->ActivatedModes(theObject, aModes);
387 TColStd_ListIteratorOfListOfInteger anIt(aModes);
388 for (; anIt.More(); anIt.Next()) {
389 int aMode = anIt.Value();
390 if (!theObject->HasSelection(aMode))
393 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
394 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
395 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
396 if (anEntity.IsNull())
398 Handle(SelectMgr_EntityOwner) anOwner =
399 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
400 if (!anOwner.IsNull())
401 theOwners.Add(anOwner);
406 //**************************************************************
407 #ifdef BEFORE_TRIHEDRON_PATCH
408 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
411 // Fill by trihedron shapes
412 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
413 if (!aAxis.IsNull()) {
414 // an Axis from Trihedron
415 Handle(Geom_Line) aLine = aAxis->Component();
416 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
417 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
419 BRep_Builder aBuilder;
421 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
425 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
426 if (!aPoint.IsNull()) {
427 // A point from trihedron
428 Handle(Geom_Point) aPnt = aPoint->Component();
429 BRep_Builder aBuilder;
430 TopoDS_Vertex aVertex;
431 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
432 if (!aVertex.IsNull())