Salome HOME
PAL9963 - "Display", "Erase", "Display only" on mesh root object
[modules/smesh.git] / src / SMESHFiltersSelection / SMESH_NumberFilter.cxx
1 //  File   : SMESH_NumberFilter.cxx
2 //  Module : SMESH
3
4 #include "SMESH_NumberFilter.hxx"
5
6 #include "GEOMBase.h"
7
8 #include "SUIT_Application.h"
9 #include "SUIT_Session.h"
10
11 #include "SalomeApp_Study.h"
12 #include "SalomeApp_DataOwner.h"
13
14 #include "SALOME_InteractiveObject.hxx"
15 #include "SALOMEDSClient_SObject.hxx"
16 #include "SALOMEDS_SObject.hxx"
17
18 #include <TopTools_MapOfShape.hxx>
19 #include <TopExp_Explorer.hxx>
20
21 /*!
22  *  Class       : SMESH_NumberFilter
23  *  Description : Filter for geom objects.
24  *                Filter geom objects by number of subshapes of the given type
25  */
26
27 //=======================================================================
28 // name    : SMESH_NumberFilter::SMESH_NumberFilter
29 // Purpose : Constructor
30 //=======================================================================
31 SMESH_NumberFilter::SMESH_NumberFilter (const char*            theKind,
32                                         const TopAbs_ShapeEnum theSubShapeType,
33                                         const int              theNumber,
34                                         const TopAbs_ShapeEnum theShapeType,
35                                         GEOM::GEOM_Object_ptr  theMainObj,
36                                         const bool             theIsClosedOnly)
37 {
38   myKind = (char*)theKind;
39   mySubShapeType = theSubShapeType;
40   myNumber = theNumber;
41   myIsClosedOnly = theIsClosedOnly;
42   myShapeTypes.Add(theShapeType);
43   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
44 }
45
46 //=======================================================================
47 // name    : SMESH_NumberFilter::SMESH_NumberFilter
48 // Purpose : Constructor
49 //=======================================================================
50 SMESH_NumberFilter::SMESH_NumberFilter (const char*                 theKind,
51                                         const TopAbs_ShapeEnum      theSubShapeType,
52                                         const int                   theNumber,
53                                         const TColStd_MapOfInteger& theShapeTypes,
54                                         GEOM::GEOM_Object_ptr       theMainObj,
55                                         const bool                  theIsClosedOnly )
56 {
57   myKind = (char*)theKind;
58   mySubShapeType = theSubShapeType;
59   myNumber = theNumber;
60   myIsClosedOnly = theIsClosedOnly;
61   myShapeTypes = theShapeTypes;
62   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
63 }
64
65 SMESH_NumberFilter::~SMESH_NumberFilter()
66 {
67 }
68
69 //=======================================================================
70 // name    : SMESH_NumberFilter::SMESH_NumberFilter
71 // Purpose : Verify validity of entry object
72 //=======================================================================
73 bool SMESH_NumberFilter::isOk (const SUIT_DataOwner* theDataOwner) const
74 {
75   if (!theDataOwner)
76     return false;
77
78   // Get geom object from IO
79   GEOM::GEOM_Object_var aGeomObj = getGeom(theDataOwner);
80   if (aGeomObj->_is_nil())
81     return false;
82
83   // Get shape from geom object and verify its parameters
84   TopoDS_Shape aShape;
85   if (!GEOMBase::GetShape(aGeomObj, aShape) ||
86       aShape.IsNull() ||
87       !myShapeTypes.Contains(aShape.ShapeType()))
88     return false;
89
90   if (myIsClosedOnly && aShape.ShapeType() == TopAbs_SHELL && !aShape.Closed())
91     return false;
92
93   // Verify whether shape of entry object is sub-shape of myMainObj
94   if (!myMainObj->_is_nil()) {
95     TopoDS_Shape aMainShape;
96     if (!GEOMBase::GetShape(myMainObj, aMainShape))
97       return false;
98
99     bool isFound = false;
100     TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
101     TopExp_Explorer anExp (aMainShape, aShapeType);
102     for (; anExp.More(); anExp.Next()) {
103       if (anExp.Current() == aShape) {
104         isFound = true;
105         break;
106       }
107     }
108     if (!isFound)
109       return false;
110   }
111
112   // Verify number of sub-shapes
113   if (mySubShapeType == TopAbs_SHAPE);
114     return true;
115
116   TopExp_Explorer anExp2 (aShape, mySubShapeType);
117   TopTools_MapOfShape aMap;
118   for (; anExp2.More(); anExp2.Next())
119     aMap.Add(anExp2.Current());
120
121   return myNumber == aMap.Extent();
122 }
123
124 //=======================================================================
125 // name    : SMESH_NumberFilter::getGeom
126 // Purpose : Retrieve geom object from SALOME_InteractiveObject
127 //=======================================================================
128 GEOM::GEOM_Object_ptr SMESH_NumberFilter::getGeom
129   (const SUIT_DataOwner* theDataOwner) const
130 {
131   const SalomeApp_DataOwner* owner =
132     dynamic_cast<const SalomeApp_DataOwner*>(theDataOwner);
133   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
134     (SUIT_Session::session()->activeApplication()->activeStudy());
135
136   GEOM::GEOM_Object_var anObj;
137
138   if (!owner || !appStudy)
139     return GEOM::GEOM_Object::_nil();
140
141   _PTR(Study) study = appStudy->studyDS();
142   QString entry = owner->entry();
143
144   _PTR(SObject) aSO(study->FindObjectID(entry.latin1()));
145   if (!aSO)
146     return GEOM::GEOM_Object::_nil();
147
148   CORBA::Object_var anObject = _CAST(SObject,aSO)->GetObject();
149   anObj = GEOM::GEOM_Object::_narrow(anObject);
150   if (!CORBA::is_nil(anObj))
151     return anObj._retn();
152
153   // Get geom object corresponding to the mesh
154   _PTR(ChildIterator) anIter = study->NewChildIterator(aSO);
155   for (; anIter->More(); anIter->Next()) {
156     _PTR(SObject) aSO = anIter->Value();
157     if (!aSO)
158       continue;
159     _PTR(SObject) aRefSO;
160     _PTR(SObject) anObj;
161     if (aSO->ReferencedObject(aRefSO))
162       anObj = aRefSO;
163
164     if (!anObj)
165       anObj = aSO;
166
167     anObject = _CAST(SObject,anObj)->GetObject();
168     GEOM::GEOM_Object_var aMeshShape = GEOM::GEOM_Object::_narrow(anObject);
169
170     if (!aMeshShape->_is_nil())
171       return aMeshShape._retn();
172   }
173
174   return GEOM::GEOM_Object::_nil();
175 }
176
177 void SMESH_NumberFilter::SetSubShapeType (const TopAbs_ShapeEnum theSubShapeType)
178 {
179   mySubShapeType = theSubShapeType;
180 }
181
182 void SMESH_NumberFilter::SetNumber (const int theNumber)
183 {
184   myNumber = theNumber;
185 }
186
187 void SMESH_NumberFilter::SetClosedOnly (const bool theIsClosedOnly)
188 {
189   myIsClosedOnly = theIsClosedOnly;
190 }
191
192 void SMESH_NumberFilter::SetShapeType (const TopAbs_ShapeEnum theShapeType)
193 {
194   myShapeTypes.Add( theShapeType );
195 }
196
197 void SMESH_NumberFilter::SetMainShape (GEOM::GEOM_Object_ptr theMainObj)
198 {
199   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
200 }