Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / ModuleBase / ModuleBase_ViewerFilters.cpp
1 // File:        ModuleBase_ViewerFilters.cpp
2 // Created:     07 Okt 2014
3 // Author:      Vitaly SMETANNIKOV
4
5
6 #include "ModuleBase_ViewerFilters.h"
7 #include "ModuleBase_IWorkshop.h"
8
9 #include <ModelAPI_Session.h>
10 #include <ModelAPI_Document.h>
11
12 #include <AIS_InteractiveObject.hxx>
13 #include <AIS_Shape.hxx>
14
15 #include <StdSelect_BRepOwner.hxx>
16
17 #include <BRep_Tool.hxx>
18 #include <TopoDS.hxx>
19 #include <TopoDS_Edge.hxx>
20 #include <Geom_Curve.hxx>
21
22 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter);
23 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter);
24
25
26 //TODO (VSV): Check bug in OCCT: Filter result is ignored (bug25340)
27 Standard_Boolean ModuleBase_ShapeDocumentFilter::IsOk(const Handle(SelectMgr_EntityOwner)& theOwner) const
28 {
29   if (theOwner->HasSelectable()) {
30     Handle(AIS_InteractiveObject) aAisObj = 
31       Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
32     if (!aAisObj.IsNull()) {
33       boost::shared_ptr<GeomAPI_AISObject> aAISObj = AISObjectPtr(new GeomAPI_AISObject());
34       aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aAisObj));
35       ObjectPtr aObj = myWorkshop->findPresentedObject(aAISObj);
36       if (aObj) {
37         DocumentPtr aDoc = aObj->document();
38         SessionPtr aMgr = ModelAPI_Session::get();
39         return (aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument());
40       }
41     }
42   }
43   return Standard_False;
44 }
45
46
47 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
48 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
49
50 Standard_Boolean ModuleBase_ShapeInPlaneFilter::IsOk(const Handle(SelectMgr_EntityOwner)& theOwner) const
51 {
52   if (theOwner->HasSelectable()) {
53     Handle(StdSelect_BRepOwner) aShapeOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
54     if (!aShapeOwner.IsNull()) {
55       TopoDS_Shape aShape = aShapeOwner->Shape();
56       TopAbs_ShapeEnum aType = aShape.ShapeType();
57       switch (aType) {
58       case TopAbs_VERTEX:
59         {
60           gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
61           return myPlane.Distance(aPnt) < Precision::Confusion();
62         }
63       case TopAbs_EDGE:
64         {
65           TopoDS_Edge aEdge = TopoDS::Edge(aShape);
66           Standard_Real aFirst, aLast;
67           Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aFirst, aLast);
68           gp_Pnt aFirstPnt = aCurve->Value(aFirst);
69           gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
70           gp_Pnt aLastPnt = aCurve->Value(aLast);
71           bool aD1 = myPlane.Distance(aFirstPnt) < Precision::Confusion();
72           bool aD2 = myPlane.Distance(aMidPnt) < Precision::Confusion();
73           bool aD3 = myPlane.Distance(aLastPnt) < Precision::Confusion();
74           return aD1 && aD2 && aD3;
75         }
76       }
77     } else {
78       // This is not object controlled by the filter
79       return Standard_True;
80     }
81   }
82   return Standard_False;
83 }