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, GeomShapePtr(), NULL));
36 //********************************************************************
37 ResultPtr ModuleBase_ISelection::getResult(const ModuleBase_ViewerPrs& thePrs)
41 if (thePrs.object().get())
42 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
43 else if (!thePrs.owner().IsNull()) {
44 ObjectPtr anObject = getSelectableObject(thePrs.owner());
45 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
51 //********************************************************************
52 GeomShapePtr ModuleBase_ISelection::getShape(const ModuleBase_ViewerPrs& thePrs)
56 const GeomShapePtr& aPrsShape = thePrs.shape();
57 // if only result is selected, an empty shape is set to the model
58 if (!aPrsShape.get() || aPrsShape->isNull()) {
62 // If the result object is built on the same shape, the result shape here is empty one
63 ResultPtr aResult = getResult(thePrs);
64 if (aResult.get() && aShape->isEqual(aResult->shape()))
65 aShape = GeomShapePtr();
70 //********************************************************************
71 QList<ModuleBase_ViewerPrs> ModuleBase_ISelection::getViewerPrs(const QObjectPtrList& theObjects)
73 QList<ModuleBase_ViewerPrs> aSelectedPrs;
74 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
75 for (; anIt != aLast; anIt++) {
76 ObjectPtr anObject = *anIt;
77 if (anObject.get() != NULL) {
78 aSelectedPrs.append(ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL));
84 //********************************************************************
85 void ModuleBase_ISelection::filterSelectionOnEqualPoints
86 (QList<ModuleBase_ViewerPrs>& theSelected)
88 QList<ModuleBase_ViewerPrs> aCandidatesToRemove;
89 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theSelected.begin(),
90 aLast = theSelected.end();
91 QList<ModuleBase_ViewerPrs>::const_iterator aSubIt;
92 for (; anIt != aLast; anIt++) {
95 for (; aSubIt != aLast; aSubIt++) {
96 if (isEqualVertices(*anIt, *aSubIt)) {
97 aCandidatesToRemove.append(*aSubIt);
102 QList<ModuleBase_ViewerPrs>::const_iterator aRemIt = aCandidatesToRemove.begin(),
103 aRemLast = aCandidatesToRemove.end();
104 for (; aRemIt != aRemLast; aRemIt++) {
105 theSelected.removeAll(*aRemIt);
109 bool ModuleBase_ISelection::isEqualVertices(const ModuleBase_ViewerPrs thePrs1,
110 const ModuleBase_ViewerPrs thePrs2)
112 bool isEqual = false;
113 Handle(StdSelect_BRepOwner) anOwner1 = Handle(StdSelect_BRepOwner)::DownCast(thePrs1.owner());
114 Handle(StdSelect_BRepOwner) anOwner2 = Handle(StdSelect_BRepOwner)::DownCast(thePrs2.owner());
116 if (!anOwner1.IsNull() && anOwner1->HasShape() &&
117 !anOwner2.IsNull() && anOwner2->HasShape()) {
118 const TopoDS_Shape& aShape1 = anOwner1->Shape();
119 const TopoDS_Shape& aShape2 = anOwner2->Shape();
120 //TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
121 if (aShape1.ShapeType() == TopAbs_VERTEX &&
122 aShape2.ShapeType() == TopAbs_VERTEX) {
123 const TopoDS_Vertex& aVertex1 = TopoDS::Vertex(aShape1);
124 const TopoDS_Vertex& aVertex2 = TopoDS::Vertex(aShape2);
125 if (!aVertex1.IsNull() && !aVertex2.IsNull()) {
126 gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex1);
127 gp_Pnt aPoint2 = BRep_Tool::Pnt(aVertex2);
129 std::shared_ptr<GeomAPI_Pnt> aPnt1 = std::shared_ptr<GeomAPI_Pnt>
130 (new GeomAPI_Pnt(aPoint1.X(), aPoint1.Y(), aPoint1.Z()));
131 std::shared_ptr<GeomAPI_Pnt> aPnt2 = std::shared_ptr<GeomAPI_Pnt>
132 (new GeomAPI_Pnt(aPoint2.X(), aPoint2.Y(), aPoint2.Z()));
133 isEqual = aPnt1->isEqual(aPnt2);