1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "ModuleBase_ISelection.h"
5 #include <StdSelect_BRepOwner.hxx>
6 #include <TopoDS_Vertex.hxx>
8 #include <BRep_Tool.hxx>
9 #include <GeomAPI_Pnt.h>
11 //********************************************************************
12 void ModuleBase_ISelection::appendSelected(const QList<ModuleBase_ViewerPrs> theValues,
13 QList<ModuleBase_ViewerPrs>& theValuesTo)
15 // collect the objects from the viewer
16 QObjectPtrList anExistedObjects;
17 QList<ModuleBase_ViewerPrs>::const_iterator aPrsIt = theValuesTo.begin(),
18 aPrsLast = theValuesTo.end();
19 for (; aPrsIt != aPrsLast; aPrsIt++) {
20 if ((*aPrsIt).owner() && (*aPrsIt).object())
21 anExistedObjects.push_back((*aPrsIt).object());
25 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(),
26 aLast = theValues.end();
27 for (; anIt != aLast; anIt++) {
28 ObjectPtr anObject = (*anIt).object();
29 if (anObject.get() != NULL && !anExistedObjects.contains(anObject)) {
30 theValuesTo.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
36 //********************************************************************
37 ResultPtr ModuleBase_ISelection::getResult(const ModuleBase_ViewerPrs& thePrs)
41 if (!thePrs.owner().IsNull()) {
42 ObjectPtr anObject = getSelectableObject(thePrs.owner());
43 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
46 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
52 //********************************************************************
53 GeomShapePtr ModuleBase_ISelection::getShape(const ModuleBase_ViewerPrs& thePrs)
57 const TopoDS_Shape& aTDSShape = thePrs.shape();
58 // if only result is selected, an empty shape is set to the model
59 if (aTDSShape.IsNull()) {
62 aShape = GeomShapePtr(new GeomAPI_Shape());
63 aShape->setImpl(new TopoDS_Shape(aTDSShape));
64 // If the result object is built on the same shape, the result shape here is empty one
65 ResultPtr aResult = getResult(thePrs);
66 if (aResult.get() && aShape->isEqual(aResult->shape()))
67 aShape = GeomShapePtr();
72 //********************************************************************
73 QList<ModuleBase_ViewerPrs> ModuleBase_ISelection::getViewerPrs(const QObjectPtrList& theObjects)
75 QList<ModuleBase_ViewerPrs> aSelectedPrs;
76 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
77 for (; anIt != aLast; anIt++) {
78 ObjectPtr anObject = *anIt;
79 if (anObject.get() != NULL) {
80 aSelectedPrs.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
86 //********************************************************************
87 void ModuleBase_ISelection::filterSelectionOnEqualPoints
88 (QList<ModuleBase_ViewerPrs>& theSelected)
90 QList<ModuleBase_ViewerPrs> aCandidatesToRemove;
91 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theSelected.begin(),
92 aLast = theSelected.end();
93 QList<ModuleBase_ViewerPrs>::const_iterator aSubIt;
94 for (; anIt != aLast; anIt++) {
97 for (; aSubIt != aLast; aSubIt++) {
98 if (isEqualVertices(*anIt, *aSubIt)) {
99 aCandidatesToRemove.append(*aSubIt);
104 QList<ModuleBase_ViewerPrs>::const_iterator aRemIt = aCandidatesToRemove.begin(),
105 aRemLast = aCandidatesToRemove.end();
106 for (; aRemIt != aRemLast; aRemIt++) {
107 theSelected.removeAll(*aRemIt);
111 bool ModuleBase_ISelection::isEqualVertices(const ModuleBase_ViewerPrs thePrs1,
112 const ModuleBase_ViewerPrs thePrs2)
114 bool isEqual = false;
115 Handle(StdSelect_BRepOwner) anOwner1 = Handle(StdSelect_BRepOwner)::DownCast(thePrs1.owner());
116 Handle(StdSelect_BRepOwner) anOwner2 = Handle(StdSelect_BRepOwner)::DownCast(thePrs2.owner());
118 if (!anOwner1.IsNull() && anOwner1->HasShape() &&
119 !anOwner2.IsNull() && anOwner2->HasShape()) {
120 const TopoDS_Shape& aShape1 = anOwner1->Shape();
121 const TopoDS_Shape& aShape2 = anOwner2->Shape();
122 //TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
123 if (aShape1.ShapeType() == TopAbs_VERTEX &&
124 aShape2.ShapeType() == TopAbs_VERTEX) {
125 const TopoDS_Vertex& aVertex1 = TopoDS::Vertex(aShape1);
126 const TopoDS_Vertex& aVertex2 = TopoDS::Vertex(aShape2);
127 if (!aVertex1.IsNull() && !aVertex2.IsNull()) {
128 gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex1);
129 gp_Pnt aPoint2 = BRep_Tool::Pnt(aVertex2);
131 std::shared_ptr<GeomAPI_Pnt> aPnt1 = std::shared_ptr<GeomAPI_Pnt>
132 (new GeomAPI_Pnt(aPoint1.X(), aPoint1.Y(), aPoint1.Z()));
133 std::shared_ptr<GeomAPI_Pnt> aPnt2 = std::shared_ptr<GeomAPI_Pnt>
134 (new GeomAPI_Pnt(aPoint2.X(), aPoint2.Y(), aPoint2.Z()));
135 isEqual = aPnt1->isEqual(aPnt2);