Salome HOME
Merge branch 'Dev_0.6' of newgeom:newgeom into Dev_0.6
[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() || aDoc == aMgr->moduleDocument());
41       }
42       else {
43         // This is not object controlled by the filter
44         return Standard_True;
45       }
46     }
47   }
48   return Standard_False;
49 }
50
51
52 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
53 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
54
55 Standard_Boolean ModuleBase_ShapeInPlaneFilter::IsOk(const Handle(SelectMgr_EntityOwner)& theOwner) const
56 {
57   if (theOwner->HasSelectable()) {
58     Handle(StdSelect_BRepOwner) aShapeOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
59     if (!aShapeOwner.IsNull()) {
60       TopoDS_Shape aShape = aShapeOwner->Shape();
61       TopAbs_ShapeEnum aType = aShape.ShapeType();
62       switch (aType) {
63       case TopAbs_VERTEX:
64         {
65           gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
66           return myPlane.Distance(aPnt) < Precision::Confusion();
67         }
68       case TopAbs_EDGE:
69         {
70           TopoDS_Edge aEdge = TopoDS::Edge(aShape);
71           Standard_Real aFirst, aLast;
72           Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aFirst, aLast);
73           gp_Pnt aFirstPnt = aCurve->Value(aFirst);
74           gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
75           gp_Pnt aLastPnt = aCurve->Value(aLast);
76           bool aD1 = myPlane.Distance(aFirstPnt) < Precision::Confusion();
77           bool aD2 = myPlane.Distance(aMidPnt) < Precision::Confusion();
78           bool aD3 = myPlane.Distance(aLastPnt) < Precision::Confusion();
79           return aD1 && aD2 && aD3;
80         }
81       default:
82         // This is not object controlled by the filter
83         return Standard_True;
84       break;
85       }
86     } else {
87       // This is not object controlled by the filter
88       return Standard_True;
89     }
90   }
91   return Standard_False;
92 }
93
94
95 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ObjectTypesFilter, SelectMgr_Filter);
96 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ObjectTypesFilter, SelectMgr_Filter);
97
98
99 //TODO (VSV): Check bug in OCCT: Filter result is ignored (bug25340)
100 Standard_Boolean ModuleBase_ObjectTypesFilter::IsOk(const Handle(SelectMgr_EntityOwner)& theOwner) const
101 {
102   Standard_Boolean isOk = ModuleBase_ShapeDocumentFilter::IsOk(theOwner);
103   if (isOk && theOwner->HasSelectable()) {
104     Handle(AIS_InteractiveObject) aAisObj = 
105       Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
106     if (!aAisObj.IsNull()) {
107       std::shared_ptr<GeomAPI_AISObject> aAISObj = AISObjectPtr(new GeomAPI_AISObject());
108       aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aAisObj));
109       ObjectPtr aObj = myWorkshop->findPresentedObject(aAISObj);
110
111       foreach (QString aType, myTypes) {
112         if (aType.toLower() == "construction") {
113           ResultConstructionPtr aConstr = 
114             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
115           return (aConstr != NULL);
116         } // ToDo: Process other types of objects
117       }
118     }
119   }
120   return Standard_False;
121 }