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