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