Salome HOME
Fix for the issue #1766 : treat face as equal with different orientation to find...
[modules/shaper.git] / src / ModuleBase / ModuleBase_ViewerFilters.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_ViewerFilters.cpp
4 // Created:     07 Okt 2014
5 // Author:      Vitaly SMETANNIKOV
6
7
8 #include "ModuleBase_ViewerFilters.h"
9 #include "ModuleBase_IWorkshop.h"
10 #include "ModuleBase_IModule.h"
11
12 #include <ModelAPI_Session.h>
13 #include <ModelAPI_Document.h>
14 #include <ModelAPI_ResultConstruction.h>
15
16 #include <GeomAPI_Edge.h>
17
18 #include <AIS_InteractiveObject.hxx>
19 #include <AIS_Shape.hxx>
20 #include <AIS_Axis.hxx>
21 #include <AIS_Point.hxx>
22 #include <AIS_Plane.hxx>
23 #include <Geom_Point.hxx>
24 #include <Geom_Line.hxx>
25 #include <Geom_Plane.hxx>
26
27 #include <StdSelect_BRepOwner.hxx>
28
29 #include <BRep_Tool.hxx>
30 #include <TopoDS.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <Geom_Curve.hxx>
33 #include <gp_Pln.hxx>
34
35 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter);
36 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter);
37
38
39 //TODO (VSV): Check bug in OCCT: Filter result is ignored (bug25340)
40 Standard_Boolean ModuleBase_ShapeDocumentFilter::IsOk(
41                                             const Handle(SelectMgr_EntityOwner)& theOwner) const
42 {
43   bool aValid = true;
44
45   ModuleBase_Operation* anOperation = myWorkshop->module()->currentOperation();
46   // the shapes from different documents should be provided if there is no started operation
47   // in order to show/hide results
48   if (anOperation) {
49     aValid = Standard_False;
50     std::shared_ptr<GeomAPI_AISObject> aAISObj = AISObjectPtr(new GeomAPI_AISObject());
51     if (theOwner->HasSelectable()) {
52       Handle(AIS_InteractiveObject) aAisObj =
53                        Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
54       if (!aAisObj.IsNull()) {
55         aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aAisObj));
56       }
57     }
58     ObjectPtr aObj = myWorkshop->findPresentedObject(aAISObj);
59     if (aObj) {
60       DocumentPtr aDoc = aObj->document();
61       SessionPtr aMgr = ModelAPI_Session::get();
62       aValid = (aDoc == aMgr->activeDocument() || aDoc == aMgr->moduleDocument());
63     }
64     else {
65       // This object is not controlled by the filter
66       aValid = Standard_True;
67     }
68   }
69
70 #ifdef DEBUG_FILTERS
71   qDebug(QString("ModuleBase_ShapeDocumentFilter::IsOk = %1").arg(aValid).toStdString().c_str());
72 #endif
73   return aValid;
74 }
75
76 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
77 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
78
79 Standard_Boolean ModuleBase_ShapeInPlaneFilter::IsOk(
80                                         const Handle(SelectMgr_EntityOwner)& theOwner) const
81 {
82   bool aValid = true;
83
84   if (myPlane.get()) {
85     aValid = Standard_False;
86     if (theOwner->HasSelectable()) {
87       gp_Pln aPlane = myPlane->impl<gp_Pln>();
88       Handle(StdSelect_BRepOwner) aShapeOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
89       if (!aShapeOwner.IsNull() && aShapeOwner->HasShape()) {
90         TopoDS_Shape aShape = aShapeOwner->Shape();
91         TopAbs_ShapeEnum aType = aShape.ShapeType();
92         switch (aType) {
93         case TopAbs_VERTEX:
94           {
95             gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
96             return aPlane.SquareDistance(aPnt) < Precision::SquareConfusion();
97           }
98         case TopAbs_EDGE:
99           {
100             std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
101             anEdge->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
102             return anEdge->isInPlane(myPlane);
103           }
104         default:
105           // The object can be selected in Object browser and contain, for example, compound.
106           // The compound could not belong to any plane, so the result is false
107           aValid = Standard_False;
108         break;
109         }
110       } else {
111         // Check Trihedron sub-objects
112         Handle(SelectMgr_SelectableObject) aSelObj = theOwner->Selectable();
113         Handle(Standard_Type) aType = aSelObj->DynamicType();
114         if (aType == STANDARD_TYPE(AIS_Axis)) {
115           Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(aSelObj);
116           gp_Lin aLine = aAxis->Component()->Lin();
117           return aPlane.Contains(aLine, Precision::Confusion(), Precision::Angular());
118
119         } else if (aType == STANDARD_TYPE(AIS_Point)) {
120           Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(aSelObj);
121           return aPlane.Distance(aPoint->Component()->Pnt()) < Precision::Confusion();
122
123         } else if (aType == STANDARD_TYPE(AIS_Plane)) {
124           Handle(AIS_Plane) aAisPlane = Handle(AIS_Plane)::DownCast(aSelObj);
125           gp_Pln aPln = aAisPlane->Component()->Pln();
126           return aPlane.Distance(aPln) < Precision::Confusion();
127         }
128         // This is not object controlled by the filter
129         aValid = Standard_True;
130       }
131     }
132   }
133 #ifdef DEBUG_FILTERS
134   qDebug(QString("ModuleBase_ShapeDocumentFilter::IsOk = %1").arg(aValid).toStdString().c_str());
135 #endif
136   return aValid;
137 }