]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESHFiltersSelection/SMESH_NumberFilter.cxx
Salome HOME
*** empty log message ***
[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 QString&         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 = 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 QString&              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 = 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   _PTR(SComponent) objComponent = aSO->GetFatherComponent();
149   if( !objComponent || objComponent->ComponentDataType()!=myKind )
150     return GEOM::GEOM_Object::_nil();
151
152
153   CORBA::Object_var anObject = _CAST(SObject,aSO)->GetObject();
154   anObj = GEOM::GEOM_Object::_narrow(anObject);
155   if (!CORBA::is_nil(anObj))
156     return anObj._retn();
157
158   // Get geom object corresponding to the mesh
159   _PTR(ChildIterator) anIter = study->NewChildIterator(aSO);
160   for (; anIter->More(); anIter->Next()) {
161     _PTR(SObject) aSO = anIter->Value();
162     if (!aSO)
163       continue;
164     _PTR(SObject) aRefSO;
165     _PTR(SObject) anObj;
166     if (aSO->ReferencedObject(aRefSO))
167       anObj = aRefSO;
168
169     if (!anObj)
170       anObj = aSO;
171
172     anObject = _CAST(SObject,anObj)->GetObject();
173     GEOM::GEOM_Object_var aMeshShape = GEOM::GEOM_Object::_narrow(anObject);
174
175     if (!aMeshShape->_is_nil())
176       return aMeshShape._retn();
177   }
178
179   return GEOM::GEOM_Object::_nil();
180 }
181
182 void SMESH_NumberFilter::SetSubShapeType (const TopAbs_ShapeEnum theSubShapeType)
183 {
184   mySubShapeType = theSubShapeType;
185 }
186
187 void SMESH_NumberFilter::SetNumber (const int theNumber)
188 {
189   myNumber = theNumber;
190 }
191
192 void SMESH_NumberFilter::SetClosedOnly (const bool theIsClosedOnly)
193 {
194   myIsClosedOnly = theIsClosedOnly;
195 }
196
197 void SMESH_NumberFilter::SetShapeType (const TopAbs_ShapeEnum theShapeType)
198 {
199   myShapeTypes.Add( theShapeType );
200 }
201
202 void SMESH_NumberFilter::SetMainShape (GEOM::GEOM_Object_ptr theMainObj)
203 {
204   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
205 }