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