Salome HOME
updated copyright message
[modules/shaper.git] / src / PartSet / PartSet_Filters.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 "PartSet_Filters.h"
21
22 #include <ModuleBase_IWorkshop.h>
23 #include "ModuleBase_IModule.h"
24 #include <ModuleBase_ResultPrs.h>
25
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_ResultPart.h>
28 #include <ModelAPI_ResultGroup.h>
29 #include <ModelAPI_ResultField.h>
30 #include <ModelAPI_Session.h>
31
32 #include <AIS_InteractiveObject.hxx>
33 #include <AIS_Shape.hxx>
34 #include <AIS_Trihedron.hxx>
35 #include <AIS_ViewCube.hxx>
36 #include <StdSelect_BRepOwner.hxx>
37
38
39 IMPLEMENT_STANDARD_RTTIEXT(PartSet_GlobalFilter, ModuleBase_ShapeDocumentFilter);
40
41 PartSet_GlobalFilter::PartSet_GlobalFilter(ModuleBase_IWorkshop* theWorkshop)
42   : ModuleBase_ShapeDocumentFilter(theWorkshop)
43 {
44   addNonSelectableType(ModelAPI_ResultField::group().c_str());
45   addNonSelectableType(ModelAPI_ResultGroup::group().c_str());
46 }
47
48
49 Standard_Boolean PartSet_GlobalFilter::IsOk(const Handle(SelectMgr_EntityOwner)& theOwner) const
50 {
51   bool aValid = true;
52   std::shared_ptr<GeomAPI_AISObject> aAISObj = AISObjectPtr(new GeomAPI_AISObject());
53   Handle(AIS_InteractiveObject) aAisObj;
54   if (theOwner->HasSelectable()) {
55     aAisObj = Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
56     if (!aAisObj.IsNull()) {
57       aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aAisObj));
58     }
59   }
60   ObjectPtr aObj = myWorkshop->findPresentedObject(aAISObj);
61   ModuleBase_Operation* anOperation = myWorkshop->module()->currentOperation();
62
63 #ifdef HAVE_SALOME
64   if (!aObj.get()) {
65     // Workaround to enable View Cube and Trihedron selection
66     if (!aAisObj.IsNull()) {
67       if (aAisObj->IsKind(STANDARD_TYPE(AIS_Trihedron)) ||
68           aAisObj->IsKind(STANDARD_TYPE(AIS_ViewCube))) {
69         return true;
70       }
71     }
72
73     // Issue #3161: Do not use presentations for non-SHAPER objects
74     if (!anOperation)
75       return false;
76   }
77 #endif
78
79   // the shapes from different documents should be provided if there is no started operation
80   // in order to show/hide results
81   if (anOperation) {
82     aValid = false;
83     if (ModuleBase_ShapeDocumentFilter::IsOk(theOwner)) {
84       if (aObj) {
85         ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
86         // result of parts belongs to PartSet document and can be selected only when PartSet
87         //  is active in order to do not select the result of the active part.
88         if (aResult.get()) {
89           std::string aResultGroupName = aResult->groupName();
90           if (aResultGroupName == ModelAPI_ResultPart::group()) {
91             SessionPtr aMgr = ModelAPI_Session::get();
92             aValid = aMgr->activeDocument() == aMgr->moduleDocument();
93           } else if (myNonSelectableTypes.contains(aResultGroupName.c_str())) {
94             aValid = Standard_False;
95           } else
96             aValid = Standard_True;
97         }
98         else { // possibly this code is obsolete, as a feature object can be selected in recovery
99           // only and there can not be Group feature
100           FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
101           if (aFeature) {
102             aValid = !myNonSelectableTypes.contains(aFeature->getKind().c_str());
103           } else
104             aValid = Standard_True;
105         }
106       } else
107         // This is not object controlled by the filter
108         aValid = Standard_True;
109     }
110   }
111 #ifdef DEBUG_FILTERS
112   qDebug(QString("PartSet_GlobalFilter::IsOk = %1").arg(aValid).toStdString().c_str());
113 #endif
114   return aValid;
115 }
116
117 IMPLEMENT_STANDARD_RTTIEXT(PartSet_ResultGroupNameFilter, SelectMgr_Filter);
118
119 void PartSet_ResultGroupNameFilter::setGroupNames(const std::set<std::string>& theGroupNames)
120 {
121   myGroupNames = theGroupNames;
122 }
123
124 Standard_Boolean PartSet_ResultGroupNameFilter::IsOk(
125   const Handle(SelectMgr_EntityOwner)& theOwner) const
126 {
127   std::shared_ptr<GeomAPI_AISObject> aAISObj = AISObjectPtr(new GeomAPI_AISObject());
128   if (theOwner->HasSelectable()) {
129     Handle(AIS_InteractiveObject) aAisObj =
130       Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
131     if (!aAisObj.IsNull()) {
132       aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aAisObj));
133     }
134   }
135   ObjectPtr anObject = myWorkshop->findPresentedObject(aAISObj);
136   if (!anObject.get())
137     return true;
138
139   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
140   // result of parts belongs to PartSet document and can be selected only when PartSet
141   //  is active in order to do not select the result of the active part.
142   if (!aResult.get())
143     return true;
144
145   std::string aGroupName = aResult->groupName();// == ModelAPI_ResultPart::group()) {
146   if (myGroupNames.find(aGroupName) != myGroupNames.end())
147     return false; // the object of the filtered type is found
148
149   return true;
150 }
151
152
153 IMPLEMENT_STANDARD_RTTIEXT(PartSet_CirclePointFilter, SelectMgr_Filter);
154
155 Standard_Boolean
156   PartSet_CirclePointFilter::IsOk(const Handle(SelectMgr_EntityOwner)& theOwner) const
157 {
158   ModuleBase_Operation* anOperation = myWorkshop->module()->currentOperation();
159   if(!anOperation) {
160     return Standard_True;
161   }
162
163   if(theOwner->HasSelectable() == Standard_False) {
164     return Standard_True;
165   }
166
167   Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
168   if(aBrepOwner.IsNull()) {
169     return Standard_True;
170   }
171
172   const TopoDS_Shape& aShape = aBrepOwner->Shape();
173   if(aShape.IsNull() || aShape.ShapeType() != TopAbs_VERTEX) {
174     return Standard_True;
175   }
176
177   Handle(ModuleBase_ResultPrs) aResultPrs =
178     Handle(ModuleBase_ResultPrs)::DownCast(theOwner->Selectable());
179   if(aResultPrs.IsNull()) {
180     return Standard_True;
181   }
182
183   std::shared_ptr<GeomAPI_AISObject> aGeomAISObj(new GeomAPI_AISObject());
184   aGeomAISObj->setImpl(new Handle(AIS_InteractiveObject)(aResultPrs));
185   ObjectPtr anObj = myWorkshop->findPresentedObject(aGeomAISObj);
186   if(!anObj.get()) {
187     return Standard_True;
188   }
189
190   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
191   if(!aResult.get()) {
192     return Standard_True;
193   }
194
195   DocumentPtr aDocument = aResult->document();
196   if(!aDocument.get()) {
197     return Standard_True;
198   }
199
200   FeaturePtr aFeature = aDocument->feature(aResult);
201   if(!aFeature.get() ||
202     ((aFeature->getKind() != "SketchCircle") &&
203     (aFeature->getKind() != "SketchBSplinePeriodic"))) {
204     return Standard_True;
205   }
206
207   const TopoDS_Shape anOwnerShape = aResultPrs->Shape();
208   if(anOwnerShape.ShapeType() == TopAbs_EDGE) {
209     return Standard_False;
210   }
211
212 #ifdef DEBUG_FILTERS
213   qDebug(QString("PartSet_ShapeDocumentFilter::IsOk = %1").arg(aValid).toStdString().c_str());
214 #endif
215
216   return Standard_True;
217 }