Salome HOME
updated copyright message
[modules/shaper.git] / src / ModuleBase / ModuleBase_ViewerFilters.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModuleBase_ViewerFilters.h"
21 #include "ModuleBase_IWorkshop.h"
22 #include "ModuleBase_IModule.h"
23 #include "ModuleBase_Operation.h"
24 #include "ModuleBase_IPropertyPanel.h"
25 #include "ModuleBase_ModelWidget.h"
26
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_ResultConstruction.h>
30
31 #include <GeomAPI_Edge.h>
32
33 #include <AIS_InteractiveObject.hxx>
34 #include <AIS_Shape.hxx>
35 #ifdef BEFORE_TRIHEDRON_PATCH
36 #include <AIS_Axis.hxx>
37 #include <AIS_Point.hxx>
38 #include <AIS_Plane.hxx>
39 #else
40 #include <AIS_Trihedron.hxx>
41 #include <AIS_TrihedronOwner.hxx>
42 #include <Geom_Axis2Placement.hxx>
43 #endif
44 #include <Geom_Point.hxx>
45 #include <Geom_Line.hxx>
46 #include <Geom_Plane.hxx>
47
48 #include <StdSelect_BRepOwner.hxx>
49
50 #include <BRep_Tool.hxx>
51 #include <TopoDS.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <Geom_Curve.hxx>
54 #include <gp_Pln.hxx>
55
56 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeDocumentFilter, SelectMgr_Filter);
57
58
59 //TODO (VSV): Check bug in OCCT: Filter result is ignored (bug25340)
60 Standard_Boolean ModuleBase_ShapeDocumentFilter::IsOk(
61                                             const Handle(SelectMgr_EntityOwner)& theOwner) const
62 {
63   bool aValid = true;
64
65   ModuleBase_Operation* anOperation = myWorkshop->module()->currentOperation();
66   // the shapes from different documents should be provided if there is no started operation
67   // in order to show/hide results
68   if (anOperation) {
69     aValid = Standard_False;
70     std::shared_ptr<GeomAPI_AISObject> aAISObj = AISObjectPtr(new GeomAPI_AISObject());
71     if (theOwner->HasSelectable()) {
72       Handle(AIS_InteractiveObject) aAisObj =
73                        Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
74       if (!aAisObj.IsNull()) {
75         aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aAisObj));
76       }
77     }
78     ObjectPtr aObj = myWorkshop->findPresentedObject(aAISObj);
79     if (aObj) {
80       DocumentPtr aDoc = aObj->document();
81       SessionPtr aMgr = ModelAPI_Session::get();
82
83       if (anOperation->propertyPanel()) {
84         ModuleBase_ModelWidget* aWidget = anOperation->propertyPanel()->activeWidget();
85         if (aWidget && aWidget->canUseExternalParts())
86           return Standard_True;
87       }
88       return (aDoc == aMgr->activeDocument() || aDoc == aMgr->moduleDocument());
89     }
90     else {
91       // This object is not controlled by the filter
92       aValid = Standard_True;
93     }
94   }
95
96 #ifdef DEBUG_FILTERS
97   qDebug(QString("ModuleBase_ShapeDocumentFilter::IsOk = %1").arg(aValid).toStdString().c_str());
98 #endif
99   return aValid;
100 }
101
102 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ShapeInPlaneFilter, SelectMgr_Filter);
103
104 Standard_Boolean ModuleBase_ShapeInPlaneFilter::IsOk(
105                                         const Handle(SelectMgr_EntityOwner)& theOwner) const
106 {
107   bool aValid = true;
108
109   if (myPlane.get()) {
110     aValid = Standard_False;
111     // selectable may be empty if the object is selected in Object Browser and is not visualized
112     // in the viewer(virtual owner is created for this case)
113     //if (theOwner->HasSelectable())
114     {
115       gp_Pln aPlane = myPlane->impl<gp_Pln>();
116       Handle(StdSelect_BRepOwner) aShapeOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
117       if (!aShapeOwner.IsNull() && aShapeOwner->HasShape()) {
118         TopoDS_Shape aShape = aShapeOwner->Shape();
119         TopAbs_ShapeEnum aType = aShape.ShapeType();
120         switch (aType) {
121         case TopAbs_VERTEX:
122           {
123             gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
124             return aPlane.SquareDistance(aPnt) < Precision::SquareConfusion();
125           }
126         case TopAbs_EDGE:
127           {
128             std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
129             anEdge->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
130             return anEdge->isInPlane(myPlane);
131           }
132         default:
133           // The object can be selected in Object browser and contain, for example, compound.
134           // The compound could not belong to any plane, so the result is false
135           aValid = Standard_False;
136         break;
137         }
138       } else {
139         if (theOwner->HasSelectable()) {
140           // Check Trihedron sub-objects
141           Handle(SelectMgr_SelectableObject) aSelObj = theOwner->Selectable();
142           Handle(Standard_Type) aType = aSelObj->DynamicType();
143           if (aType == STANDARD_TYPE(AIS_Trihedron)) {
144             Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(aSelObj);
145             Handle(AIS_TrihedronOwner) aTrOwner = Handle(AIS_TrihedronOwner)::DownCast(theOwner);
146             if (!aTrOwner.IsNull()) {
147               const Prs3d_DatumParts& aPart = aTrOwner->DatumPart();
148               if (aPart >= Prs3d_DP_XAxis && aPart <= Prs3d_DP_ZAxis) {
149                 gp_Ax2 anAxis = aTrihedron->Component()->Ax2();
150                 gp_Dir aDir;
151                 switch (aPart) {
152                 case Prs3d_DP_XAxis:
153                   aDir = anAxis.XDirection();
154                   break;
155                 case Prs3d_DP_YAxis:
156                   aDir = anAxis.YDirection();
157                   break;
158                 case Prs3d_DP_ZAxis:
159                   aDir = anAxis.Direction();
160                   break;
161                 default: // [to avoid compilation warning]
162                   break;
163                 }
164                 gp_Lin aLine(aTrihedron->Component()->Location(), aDir);
165                 return aPlane.Contains(aLine, Precision::Confusion(), Precision::Angular());
166               }
167             }
168           }
169           // This is not object controlled by the filter
170           aValid = Standard_True;
171         }
172       }
173     }
174   }
175 #ifdef DEBUG_FILTERS
176   qDebug(QString("ModuleBase_ShapeDocumentFilter::IsOk = %1").arg(aValid).toStdString().c_str());
177 #endif
178   return aValid;
179 }