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