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