Salome HOME
An attempt of porting to current OCCT.
[modules/shaper.git] / src / ModuleBase / ModuleBase_ViewerFilters.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "ModuleBase_ViewerFilters.h"
22 #include "ModuleBase_IWorkshop.h"
23 #include "ModuleBase_IModule.h"
24
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Document.h>
27 #include <ModelAPI_ResultConstruction.h>
28
29 #include <GeomAPI_Edge.h>
30
31 #include <AIS_InteractiveObject.hxx>
32 #include <AIS_Shape.hxx>
33 #ifdef BEFORE_TRIHEDRON_PATCH
34 #include <AIS_Axis.hxx>
35 #include <AIS_Point.hxx>
36 #include <AIS_Plane.hxx>
37 #else
38 #include <AIS_Trihedron.hxx>
39 #include <AIS_TrihedronOwner.hxx>
40 #include <Geom_Axis2Placement.hxx>
41 #endif
42 #include <Geom_Point.hxx>
43 #include <Geom_Line.hxx>
44 #include <Geom_Plane.hxx>
45
46 #include <StdSelect_BRepOwner.hxx>
47
48 #include <BRep_Tool.hxx>
49 #include <TopoDS.hxx>
50 #include <TopoDS_Edge.hxx>
51 #include <Geom_Curve.hxx>
52 #include <gp_Pln.hxx>
53
54 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter);
55
56
57 //TODO (VSV): Check bug in OCCT: Filter result is ignored (bug25340)
58 Standard_Boolean ModuleBase_ShapeDocumentFilter::IsOk(
59                                             const Handle(SelectMgr_EntityOwner)& theOwner) const
60 {
61   bool aValid = true;
62
63   ModuleBase_Operation* anOperation = myWorkshop->module()->currentOperation();
64   // the shapes from different documents should be provided if there is no started operation
65   // in order to show/hide results
66   if (anOperation) {
67     aValid = Standard_False;
68     std::shared_ptr<GeomAPI_AISObject> aAISObj = AISObjectPtr(new GeomAPI_AISObject());
69     if (theOwner->HasSelectable()) {
70       Handle(AIS_InteractiveObject) aAisObj =
71                        Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
72       if (!aAisObj.IsNull()) {
73         aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aAisObj));
74       }
75     }
76     ObjectPtr aObj = myWorkshop->findPresentedObject(aAISObj);
77     if (aObj) {
78       DocumentPtr aDoc = aObj->document();
79       SessionPtr aMgr = ModelAPI_Session::get();
80       aValid = (aDoc == aMgr->activeDocument() || aDoc == aMgr->moduleDocument());
81     }
82     else {
83       // This object is not controlled by the filter
84       aValid = Standard_True;
85     }
86   }
87
88 #ifdef DEBUG_FILTERS
89   qDebug(QString("ModuleBase_ShapeDocumentFilter::IsOk = %1").arg(aValid).toStdString().c_str());
90 #endif
91   return aValid;
92 }
93
94 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
95
96 Standard_Boolean ModuleBase_ShapeInPlaneFilter::IsOk(
97                                         const Handle(SelectMgr_EntityOwner)& theOwner) const
98 {
99   bool aValid = true;
100
101   if (myPlane.get()) {
102     aValid = Standard_False;
103     // selectable may be empty if the object is selected in Object Browser and is not visualized
104     // in the viewer(virtual owner is created for this case)
105     //if (theOwner->HasSelectable())
106     {
107       gp_Pln aPlane = myPlane->impl<gp_Pln>();
108       Handle(StdSelect_BRepOwner) aShapeOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
109       if (!aShapeOwner.IsNull() && aShapeOwner->HasShape()) {
110         TopoDS_Shape aShape = aShapeOwner->Shape();
111         TopAbs_ShapeEnum aType = aShape.ShapeType();
112         switch (aType) {
113         case TopAbs_VERTEX:
114           {
115             gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
116             return aPlane.SquareDistance(aPnt) < Precision::SquareConfusion();
117           }
118         case TopAbs_EDGE:
119           {
120             std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
121             anEdge->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
122             return anEdge->isInPlane(myPlane);
123           }
124         default:
125           // The object can be selected in Object browser and contain, for example, compound.
126           // The compound could not belong to any plane, so the result is false
127           aValid = Standard_False;
128         break;
129         }
130       } else {
131         if (theOwner->HasSelectable()) {
132         // Check Trihedron sub-objects
133         Handle(SelectMgr_SelectableObject) aSelObj = theOwner->Selectable();
134         Handle(Standard_Type) aType = aSelObj->DynamicType();
135 #ifdef BEFORE_TRIHEDRON_PATCH
136         if (aType == STANDARD_TYPE(AIS_Axis)) {
137           Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(aSelObj);
138           gp_Lin aLine = aAxis->Component()->Lin();
139           return aPlane.Contains(aLine, Precision::Confusion(), Precision::Angular());
140
141         } else if (aType == STANDARD_TYPE(AIS_Point)) {
142           Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(aSelObj);
143           return aPlane.Distance(aPoint->Component()->Pnt()) < Precision::Confusion();
144
145         } else if (aType == STANDARD_TYPE(AIS_Plane)) {
146           Handle(AIS_Plane) aAisPlane = Handle(AIS_Plane)::DownCast(aSelObj);
147           gp_Pln aPln = aAisPlane->Component()->Pln();
148           return aPlane.Distance(aPln) < Precision::Confusion();
149         }
150 #else
151         if (aType == STANDARD_TYPE(AIS_Trihedron)) {
152           Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(aSelObj);
153           Handle(AIS_TrihedronOwner) aTrOwner = Handle(AIS_TrihedronOwner)::DownCast(theOwner);
154           if (!aTrOwner.IsNull())
155           {
156             const Prs3d_DatumParts& aPart = aTrOwner->DatumPart();
157             if (aPart >= Prs3d_DP_XAxis && aPart <= Prs3d_DP_ZAxis)
158             {
159 #ifdef USE_OCCT_720
160               gp_Ax2 anAxis = aTrihedron->Component()->Ax2();
161               gp_Dir aDir = anAxis.XDirection();
162               gp_Lin aLine(aTrihedron->Component()->Location(), aDir);
163               return aPlane.Contains(aLine, Precision::Confusion(), Precision::Angular());
164 #else
165               Handle(Prs3d_Drawer) aDrawer = aTrihedron->Attributes();
166               Handle(Prs3d_DatumAspect) aDatumAspect = aDrawer->DatumAspect();
167               Handle(Graphic3d_ArrayOfPrimitives) aPrimitives =
168                                                         aDatumAspect->ArrayOfPrimitives(aPart);
169               Standard_Real aX1, anY1, aZ1, aX2, anY2, aZ2;
170               aPrimitives->Vertice(1, aX1, anY1, aZ1);
171               aPrimitives->Vertice(2, aX2, anY2, aZ2);
172               gp_Pnt aPnt1(aX1, anY1, aZ1);
173               gp_Pnt aPnt2(aX2, anY2, aZ2);
174               gp_Lin aLine(aPnt1, gp_Dir(gp_Vec(aPnt1, aPnt2)));
175               return aPlane.Contains(aLine, Precision::Confusion(), Precision::Angular());
176 #endif
177             }
178           }
179         }
180 #endif
181         // This is not object controlled by the filter
182         aValid = Standard_True;
183         }
184       }
185     }
186   }
187 #ifdef DEBUG_FILTERS
188   qDebug(QString("ModuleBase_ShapeDocumentFilter::IsOk = %1").arg(aValid).toStdString().c_str());
189 #endif
190   return aValid;
191 }