Salome HOME
Fixed crash when attribute External in feature Projection changed.
[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 #ifdef BEFORE_TRIHEDRON_PATCH
21 #include <AIS_Axis.hxx>
22 #include <AIS_Point.hxx>
23 #include <AIS_Plane.hxx>
24 #else
25 #include <AIS_Trihedron.hxx>
26 #include <AIS_TrihedronOwner.hxx>
27 #include <Geom_Axis2Placement.hxx>
28 #endif
29 #include <Geom_Point.hxx>
30 #include <Geom_Line.hxx>
31 #include <Geom_Plane.hxx>
32
33 #include <StdSelect_BRepOwner.hxx>
34
35 #include <BRep_Tool.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <Geom_Curve.hxx>
39 #include <gp_Pln.hxx>
40
41 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter);
42
43
44 //TODO (VSV): Check bug in OCCT: Filter result is ignored (bug25340)
45 Standard_Boolean ModuleBase_ShapeDocumentFilter::IsOk(
46                                             const Handle(SelectMgr_EntityOwner)& theOwner) const
47 {
48   bool aValid = true;
49
50   ModuleBase_Operation* anOperation = myWorkshop->module()->currentOperation();
51   // the shapes from different documents should be provided if there is no started operation
52   // in order to show/hide results
53   if (anOperation) {
54     aValid = Standard_False;
55     std::shared_ptr<GeomAPI_AISObject> aAISObj = AISObjectPtr(new GeomAPI_AISObject());
56     if (theOwner->HasSelectable()) {
57       Handle(AIS_InteractiveObject) aAisObj =
58                        Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
59       if (!aAisObj.IsNull()) {
60         aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aAisObj));
61       }
62     }
63     ObjectPtr aObj = myWorkshop->findPresentedObject(aAISObj);
64     if (aObj) {
65       DocumentPtr aDoc = aObj->document();
66       SessionPtr aMgr = ModelAPI_Session::get();
67       aValid = (aDoc == aMgr->activeDocument() || aDoc == aMgr->moduleDocument());
68     }
69     else {
70       // This object is not controlled by the filter
71       aValid = Standard_True;
72     }
73   }
74
75 #ifdef DEBUG_FILTERS
76   qDebug(QString("ModuleBase_ShapeDocumentFilter::IsOk = %1").arg(aValid).toStdString().c_str());
77 #endif
78   return aValid;
79 }
80
81 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
82
83 Standard_Boolean ModuleBase_ShapeInPlaneFilter::IsOk(
84                                         const Handle(SelectMgr_EntityOwner)& theOwner) const
85 {
86   bool aValid = true;
87
88   if (myPlane.get()) {
89     aValid = Standard_False;
90     if (theOwner->HasSelectable()) {
91       gp_Pln aPlane = myPlane->impl<gp_Pln>();
92       Handle(StdSelect_BRepOwner) aShapeOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
93       if (!aShapeOwner.IsNull() && aShapeOwner->HasShape()) {
94         TopoDS_Shape aShape = aShapeOwner->Shape();
95         TopAbs_ShapeEnum aType = aShape.ShapeType();
96         switch (aType) {
97         case TopAbs_VERTEX:
98           {
99             gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
100             return aPlane.SquareDistance(aPnt) < Precision::SquareConfusion();
101           }
102         case TopAbs_EDGE:
103           {
104             std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
105             anEdge->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
106             return anEdge->isInPlane(myPlane);
107           }
108         default:
109           // The object can be selected in Object browser and contain, for example, compound.
110           // The compound could not belong to any plane, so the result is false
111           aValid = Standard_False;
112         break;
113         }
114       } else {
115         // Check Trihedron sub-objects
116         Handle(SelectMgr_SelectableObject) aSelObj = theOwner->Selectable();
117         Handle(Standard_Type) aType = aSelObj->DynamicType();
118 #ifdef BEFORE_TRIHEDRON_PATCH
119         if (aType == STANDARD_TYPE(AIS_Axis)) {
120           Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(aSelObj);
121           gp_Lin aLine = aAxis->Component()->Lin();
122           return aPlane.Contains(aLine, Precision::Confusion(), Precision::Angular());
123
124         } else if (aType == STANDARD_TYPE(AIS_Point)) {
125           Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(aSelObj);
126           return aPlane.Distance(aPoint->Component()->Pnt()) < Precision::Confusion();
127
128         } else if (aType == STANDARD_TYPE(AIS_Plane)) {
129           Handle(AIS_Plane) aAisPlane = Handle(AIS_Plane)::DownCast(aSelObj);
130           gp_Pln aPln = aAisPlane->Component()->Pln();
131           return aPlane.Distance(aPln) < Precision::Confusion();
132         }
133 #else
134         if (aType == STANDARD_TYPE(AIS_Trihedron)) {
135           Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(aSelObj);
136           Handle(AIS_TrihedronOwner) aTrOwner = Handle(AIS_TrihedronOwner)::DownCast(theOwner);
137           if (!aTrOwner.IsNull())
138           {
139             const Prs3d_DatumParts& aPart = aTrOwner->DatumPart();
140             if (aPart >= Prs3d_DP_XAxis && aPart <= Prs3d_DP_ZAxis)
141             {
142               Handle(Prs3d_Drawer) aDrawer = aTrihedron->Attributes();
143               Handle(Prs3d_DatumAspect) aDatumAspect = aDrawer->DatumAspect();
144               Handle(Graphic3d_ArrayOfPrimitives) aPrimitives =
145                                                         aDatumAspect->ArrayOfPrimitives(aPart);
146               Standard_Real aX1, anY1, aZ1, aX2, anY2, aZ2;
147               aPrimitives->Vertice(1, aX1, anY1, aZ1);
148               aPrimitives->Vertice(2, aX2, anY2, aZ2);
149               gp_Pnt aPnt1(aX1, anY1, aZ1);
150               gp_Pnt aPnt2(aX2, anY2, aZ2);
151               gp_Lin aLine(aPnt1, gp_Dir(gp_Vec(aPnt1, aPnt2)));
152               return aPlane.Contains(aLine, Precision::Confusion(), Precision::Angular());
153             }
154           }
155         }
156 #endif
157         // This is not object controlled by the filter
158         aValid = Standard_True;
159       }
160     }
161   }
162 #ifdef DEBUG_FILTERS
163   qDebug(QString("ModuleBase_ShapeDocumentFilter::IsOk = %1").arg(aValid).toStdString().c_str());
164 #endif
165   return aValid;
166 }