Salome HOME
Porting to SALOME_8.2.0
[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 <GeomAPI_Edge.h>
17
18 #include <AIS_InteractiveObject.hxx>
19 #include <AIS_Shape.hxx>
20 #include <AIS_Axis.hxx>
21 #include <AIS_Point.hxx>
22 #include <AIS_Plane.hxx>
23 #include <Geom_Point.hxx>
24 #include <Geom_Line.hxx>
25 #include <Geom_Plane.hxx>
26
27 #include <StdSelect_BRepOwner.hxx>
28
29 #include <BRep_Tool.hxx>
30 #include <TopoDS.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <Geom_Curve.hxx>
33 #include <gp_Pln.hxx>
34
35 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter);
36
37
38 //TODO (VSV): Check bug in OCCT: Filter result is ignored (bug25340)
39 Standard_Boolean ModuleBase_ShapeDocumentFilter::IsOk(
40                                             const Handle(SelectMgr_EntityOwner)& theOwner) const
41 {
42   bool aValid = true;
43
44   ModuleBase_Operation* anOperation = myWorkshop->module()->currentOperation();
45   // the shapes from different documents should be provided if there is no started operation
46   // in order to show/hide results
47   if (anOperation) {
48     aValid = Standard_False;
49     std::shared_ptr<GeomAPI_AISObject> aAISObj = AISObjectPtr(new GeomAPI_AISObject());
50     if (theOwner->HasSelectable()) {
51       Handle(AIS_InteractiveObject) aAisObj =
52                        Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
53       if (!aAisObj.IsNull()) {
54         aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aAisObj));
55       }
56     }
57     ObjectPtr aObj = myWorkshop->findPresentedObject(aAISObj);
58     if (aObj) {
59       DocumentPtr aDoc = aObj->document();
60       SessionPtr aMgr = ModelAPI_Session::get();
61       aValid = (aDoc == aMgr->activeDocument() || aDoc == aMgr->moduleDocument());
62     }
63     else {
64       // This object is not controlled by the filter
65       aValid = Standard_True;
66     }
67   }
68
69 #ifdef DEBUG_FILTERS
70   qDebug(QString("ModuleBase_ShapeDocumentFilter::IsOk = %1").arg(aValid).toStdString().c_str());
71 #endif
72   return aValid;
73 }
74
75 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
76
77 Standard_Boolean ModuleBase_ShapeInPlaneFilter::IsOk(
78                                         const Handle(SelectMgr_EntityOwner)& theOwner) const
79 {
80   bool aValid = true;
81
82   if (myPlane.get()) {
83     aValid = Standard_False;
84     if (theOwner->HasSelectable()) {
85       gp_Pln aPlane = myPlane->impl<gp_Pln>();
86       Handle(StdSelect_BRepOwner) aShapeOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
87       if (!aShapeOwner.IsNull() && aShapeOwner->HasShape()) {
88         TopoDS_Shape aShape = aShapeOwner->Shape();
89         TopAbs_ShapeEnum aType = aShape.ShapeType();
90         switch (aType) {
91         case TopAbs_VERTEX:
92           {
93             gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
94             return aPlane.SquareDistance(aPnt) < Precision::SquareConfusion();
95           }
96         case TopAbs_EDGE:
97           {
98             std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
99             anEdge->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
100             return anEdge->isInPlane(myPlane);
101           }
102         default:
103           // The object can be selected in Object browser and contain, for example, compound.
104           // The compound could not belong to any plane, so the result is false
105           aValid = Standard_False;
106         break;
107         }
108       } else {
109         // Check Trihedron sub-objects
110         Handle(SelectMgr_SelectableObject) aSelObj = theOwner->Selectable();
111         Handle(Standard_Type) aType = aSelObj->DynamicType();
112         if (aType == STANDARD_TYPE(AIS_Axis)) {
113           Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(aSelObj);
114           gp_Lin aLine = aAxis->Component()->Lin();
115           return aPlane.Contains(aLine, Precision::Confusion(), Precision::Angular());
116
117         } else if (aType == STANDARD_TYPE(AIS_Point)) {
118           Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(aSelObj);
119           return aPlane.Distance(aPoint->Component()->Pnt()) < Precision::Confusion();
120
121         } else if (aType == STANDARD_TYPE(AIS_Plane)) {
122           Handle(AIS_Plane) aAisPlane = Handle(AIS_Plane)::DownCast(aSelObj);
123           gp_Pln aPln = aAisPlane->Component()->Pln();
124           return aPlane.Distance(aPln) < Precision::Confusion();
125         }
126         // This is not object controlled by the filter
127         aValid = Standard_True;
128       }
129     }
130   }
131 #ifdef DEBUG_FILTERS
132   qDebug(QString("ModuleBase_ShapeDocumentFilter::IsOk = %1").arg(aValid).toStdString().c_str());
133 #endif
134   return aValid;
135 }