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