Salome HOME
Fix suite_FEATURE_REVOLUTION tests: difference in few pixels
[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 <AIS_InteractiveObject.hxx>
17 #include <AIS_Shape.hxx>
18 #include <AIS_Axis.hxx>
19 #include <AIS_Point.hxx>
20 #include <AIS_Plane.hxx>
21 #include <Geom_Point.hxx>
22 #include <Geom_Line.hxx>
23 #include <Geom_Plane.hxx>
24
25 #include <StdSelect_BRepOwner.hxx>
26
27 #include <BRep_Tool.hxx>
28 #include <TopoDS.hxx>
29 #include <TopoDS_Edge.hxx>
30 #include <Geom_Curve.hxx>
31 #include <gp_Pln.hxx>
32
33 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter);
34 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter);
35
36
37 //TODO (VSV): Check bug in OCCT: Filter result is ignored (bug25340)
38 Standard_Boolean ModuleBase_ShapeDocumentFilter::IsOk(const Handle(SelectMgr_EntityOwner)& theOwner) const
39 {
40   bool aValid = true;
41
42   ModuleBase_Operation* anOperation = myWorkshop->module()->currentOperation();
43   // the shapes from different documents should be provided if there is no started operation
44   // in order to show/hide results
45   if (anOperation) {
46     aValid = Standard_False;
47     std::shared_ptr<GeomAPI_AISObject> aAISObj = AISObjectPtr(new GeomAPI_AISObject());
48     if (theOwner->HasSelectable()) {
49       Handle(AIS_InteractiveObject) aAisObj =
50                        Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
51       if (!aAisObj.IsNull()) {
52         aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aAisObj));
53       }
54     }
55     ObjectPtr aObj = myWorkshop->findPresentedObject(aAISObj);
56     if (aObj) {
57       DocumentPtr aDoc = aObj->document();
58       SessionPtr aMgr = ModelAPI_Session::get();
59       aValid = (aDoc == aMgr->activeDocument() || aDoc == aMgr->moduleDocument());
60     }
61     else {
62       // This object is not controlled by the filter
63       aValid = Standard_True;
64     }
65   }
66
67 #ifdef DEBUG_FILTERS
68   qDebug(QString("ModuleBase_ShapeDocumentFilter::IsOk = %1").arg(aValid).toStdString().c_str());
69 #endif
70   return aValid;
71 }
72
73 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
74 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
75
76 Standard_Boolean ModuleBase_ShapeInPlaneFilter::IsOk(const Handle(SelectMgr_EntityOwner)& theOwner) const
77 {
78   bool aValid = true;
79
80   if (myPlane.get()) {
81     aValid = Standard_False;
82     if (theOwner->HasSelectable()) {
83       gp_Pln aPlane = myPlane->impl<gp_Pln>();
84       Handle(StdSelect_BRepOwner) aShapeOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
85       if (!aShapeOwner.IsNull() && aShapeOwner->HasShape()) {
86         TopoDS_Shape aShape = aShapeOwner->Shape();
87         TopAbs_ShapeEnum aType = aShape.ShapeType();
88         switch (aType) {
89         case TopAbs_VERTEX:
90           {
91             gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
92             return aPlane.Distance(aPnt) < Precision::Confusion();
93           }
94         case TopAbs_EDGE:
95           {
96             TopoDS_Edge aEdge = TopoDS::Edge(aShape);
97             Standard_Real aFirst, aLast;
98             Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aFirst, aLast);
99             gp_Pnt aFirstPnt = aCurve->Value(aFirst);
100             gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
101             gp_Pnt aLastPnt = aCurve->Value(aLast);
102             bool aD1 = aPlane.Distance(aFirstPnt) < Precision::Confusion();
103             bool aD2 = aPlane.Distance(aMidPnt) < Precision::Confusion();
104             bool aD3 = aPlane.Distance(aLastPnt) < Precision::Confusion();
105             return aD1 && aD2 && aD3;
106           }
107         default:
108           // The object can be selected in Object browser and contain, for example, compound.
109           // The compound could not belong to any plane, so the result is false
110           aValid = Standard_False;
111         break;
112         }
113       } else {
114         // Check Trihedron sub-objects
115         Handle(SelectMgr_SelectableObject) aSelObj = theOwner->Selectable();
116         Handle(Standard_Type) aType = aSelObj->DynamicType();
117         if (aType == STANDARD_TYPE(AIS_Axis)) {
118           Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(aSelObj);
119           gp_Lin aLine = aAxis->Component()->Lin();
120           return aPlane.Contains(aLine, Precision::Confusion(), Precision::Angular());
121
122         } else if (aType == STANDARD_TYPE(AIS_Point)) {
123           Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(aSelObj);
124           return aPlane.Distance(aPoint->Component()->Pnt()) < Precision::Confusion();
125
126         } else if (aType == STANDARD_TYPE(AIS_Plane)) {
127           Handle(AIS_Plane) aAisPlane = Handle(AIS_Plane)::DownCast(aSelObj);
128           gp_Pln aPln = aAisPlane->Component()->Pln();
129           return aPlane.Distance(aPln) < Precision::Confusion();
130         }
131         // This is not object controlled by the filter
132         aValid = Standard_True;
133       }
134     }
135   }
136 #ifdef DEBUG_FILTERS
137   qDebug(QString("ModuleBase_ShapeDocumentFilter::IsOk = %1").arg(aValid).toStdString().c_str());
138 #endif
139   return aValid;
140 }