Salome HOME
Boost has been removed from code
[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 #include <ModelAPI_ResultConstruction.h>
12
13 #include <AIS_InteractiveObject.hxx>
14 #include <AIS_Shape.hxx>
15
16 #include <StdSelect_BRepOwner.hxx>
17
18 #include <BRep_Tool.hxx>
19 #include <TopoDS.hxx>
20 #include <TopoDS_Edge.hxx>
21 #include <Geom_Curve.hxx>
22
23 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter);
24 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter);
25
26
27 //TODO (VSV): Check bug in OCCT: Filter result is ignored (bug25340)
28 Standard_Boolean ModuleBase_ShapeDocumentFilter::IsOk(const Handle(SelectMgr_EntityOwner)& theOwner) const
29 {
30   if (theOwner->HasSelectable()) {
31     Handle(AIS_InteractiveObject) aAisObj = 
32       Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
33     if (!aAisObj.IsNull()) {
34       std::shared_ptr<GeomAPI_AISObject> aAISObj = AISObjectPtr(new GeomAPI_AISObject());
35       aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aAisObj));
36       ObjectPtr aObj = myWorkshop->findPresentedObject(aAISObj);
37       if (aObj) {
38         DocumentPtr aDoc = aObj->document();
39         SessionPtr aMgr = ModelAPI_Session::get();
40         return (aDoc == aMgr->activeDocument() /* MPV: for the current moment selection in other document is not possible || (aDoc == aMgr->moduleDocument()*/);
41       }
42     }
43   }
44   return Standard_False;
45 }
46
47
48 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
49 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
50
51 Standard_Boolean ModuleBase_ShapeInPlaneFilter::IsOk(const Handle(SelectMgr_EntityOwner)& theOwner) const
52 {
53   if (theOwner->HasSelectable()) {
54     Handle(StdSelect_BRepOwner) aShapeOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
55     if (!aShapeOwner.IsNull()) {
56       TopoDS_Shape aShape = aShapeOwner->Shape();
57       TopAbs_ShapeEnum aType = aShape.ShapeType();
58       switch (aType) {
59       case TopAbs_VERTEX:
60         {
61           gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
62           return myPlane.Distance(aPnt) < Precision::Confusion();
63         }
64       case TopAbs_EDGE:
65         {
66           TopoDS_Edge aEdge = TopoDS::Edge(aShape);
67           Standard_Real aFirst, aLast;
68           Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aFirst, aLast);
69           gp_Pnt aFirstPnt = aCurve->Value(aFirst);
70           gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
71           gp_Pnt aLastPnt = aCurve->Value(aLast);
72           bool aD1 = myPlane.Distance(aFirstPnt) < Precision::Confusion();
73           bool aD2 = myPlane.Distance(aMidPnt) < Precision::Confusion();
74           bool aD3 = myPlane.Distance(aLastPnt) < Precision::Confusion();
75           return aD1 && aD2 && aD3;
76         }
77       }
78     } else {
79       // This is not object controlled by the filter
80       return Standard_True;
81     }
82   }
83   return Standard_False;
84 }
85
86
87 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ObjectTypesFilter, SelectMgr_Filter);
88 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ObjectTypesFilter, SelectMgr_Filter);
89
90
91 //TODO (VSV): Check bug in OCCT: Filter result is ignored (bug25340)
92 Standard_Boolean ModuleBase_ObjectTypesFilter::IsOk(const Handle(SelectMgr_EntityOwner)& theOwner) const
93 {
94   Standard_Boolean isOk = ModuleBase_ShapeDocumentFilter::IsOk(theOwner);
95   if (isOk && theOwner->HasSelectable()) {
96     Handle(AIS_InteractiveObject) aAisObj = 
97       Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
98     if (!aAisObj.IsNull()) {
99       std::shared_ptr<GeomAPI_AISObject> aAISObj = AISObjectPtr(new GeomAPI_AISObject());
100       aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aAisObj));
101       ObjectPtr aObj = myWorkshop->findPresentedObject(aAISObj);
102
103       foreach (QString aType, myTypes) {
104         if (aType.toLower() == "construction") {
105           ResultConstructionPtr aConstr = 
106             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
107           return (aConstr != NULL);
108         } // ToDo: Process other types of objects
109       }
110     }
111   }
112   return Standard_False;
113 }