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.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 TopoDS_Shape& aTDSShape = thePrs.shape();
57 // if only result is selected, an empty shape is set to the model
58 if (aTDSShape.IsNull()) {
61 aShape = GeomShapePtr(new GeomAPI_Shape());
62 aShape->setImpl(new TopoDS_Shape(aTDSShape));
63 // If the result object is built on the same shape, the result shape here is empty one
64 ResultPtr aResult = getResult(thePrs);
65 if (aResult.get() && aShape->isEqual(aResult->shape()))
66 aShape = GeomShapePtr();
71 //********************************************************************
72 QList<ModuleBase_ViewerPrs> ModuleBase_ISelection::getViewerPrs(const QObjectPtrList& theObjects)
74 QList<ModuleBase_ViewerPrs> aSelectedPrs;
75 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
76 for (; anIt != aLast; anIt++) {
77 ObjectPtr anObject = *anIt;
78 if (anObject.get() != NULL) {
79 aSelectedPrs.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
85 //********************************************************************
86 void ModuleBase_ISelection::filterSelectionOnEqualPoints
87 (QList<ModuleBase_ViewerPrs>& theSelected)
89 QList<ModuleBase_ViewerPrs> aCandidatesToRemove;
90 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theSelected.begin(),
91 aLast = theSelected.end();
92 QList<ModuleBase_ViewerPrs>::const_iterator aSubIt;
93 for (; anIt != aLast; anIt++) {
96 for (; aSubIt != aLast; aSubIt++) {
97 if (isEqualVertices(*anIt, *aSubIt)) {
98 aCandidatesToRemove.append(*aSubIt);
103 QList<ModuleBase_ViewerPrs>::const_iterator aRemIt = aCandidatesToRemove.begin(),
104 aRemLast = aCandidatesToRemove.end();
105 for (; aRemIt != aRemLast; aRemIt++) {
106 theSelected.removeAll(*aRemIt);
110 bool ModuleBase_ISelection::isEqualVertices(const ModuleBase_ViewerPrs thePrs1,
111 const ModuleBase_ViewerPrs thePrs2)
113 bool isEqual = false;
114 Handle(StdSelect_BRepOwner) anOwner1 = Handle(StdSelect_BRepOwner)::DownCast(thePrs1.owner());
115 Handle(StdSelect_BRepOwner) anOwner2 = Handle(StdSelect_BRepOwner)::DownCast(thePrs2.owner());
117 if (!anOwner1.IsNull() && anOwner1->HasShape() &&
118 !anOwner2.IsNull() && anOwner2->HasShape()) {
119 const TopoDS_Shape& aShape1 = anOwner1->Shape();
120 const TopoDS_Shape& aShape2 = anOwner2->Shape();
121 //TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
122 if (aShape1.ShapeType() == TopAbs_VERTEX &&
123 aShape2.ShapeType() == TopAbs_VERTEX) {
124 const TopoDS_Vertex& aVertex1 = TopoDS::Vertex(aShape1);
125 const TopoDS_Vertex& aVertex2 = TopoDS::Vertex(aShape2);
126 if (!aVertex1.IsNull() && !aVertex2.IsNull()) {
127 gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex1);
128 gp_Pnt aPoint2 = BRep_Tool::Pnt(aVertex2);
130 std::shared_ptr<GeomAPI_Pnt> aPnt1 = std::shared_ptr<GeomAPI_Pnt>
131 (new GeomAPI_Pnt(aPoint1.X(), aPoint1.Y(), aPoint1.Z()));
132 std::shared_ptr<GeomAPI_Pnt> aPnt2 = std::shared_ptr<GeomAPI_Pnt>
133 (new GeomAPI_Pnt(aPoint2.X(), aPoint2.Y(), aPoint2.Z()));
134 isEqual = aPnt1->isEqual(aPnt2);