Salome HOME
'Extrusion Along Path' dialog box : rearrange widgets
[modules/smesh.git] / src / SMESH_I / SMESH_subMesh_i.cxx
1 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESH_subMesh_i.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
29 using namespace std;
30 #include "SMESH_subMesh_i.hxx"
31 #include "SMESH_Gen_i.hxx"
32 #include "SMESH_Mesh_i.hxx"
33
34 #include "Utils_CorbaException.hxx"
35 #include "utilities.h"
36 #include "OpUtil.hxx"
37 #include "Utils_ExceptHandlers.hxx"
38
39 //=============================================================================
40 /*!
41  *  
42  */
43 //=============================================================================
44
45 SMESH_subMesh_i::SMESH_subMesh_i()
46      : SALOME::GenericObj_i( PortableServer::POA::_nil() )
47 {
48   MESSAGE("SMESH_subMesh_i::SMESH_subMesh_i default, not for use");
49     ASSERT(0);
50 }
51
52 //=============================================================================
53 /*!
54  *  
55  */
56 //=============================================================================
57
58 SMESH_subMesh_i::SMESH_subMesh_i( PortableServer::POA_ptr thePOA,
59                                   SMESH_Gen_i*            gen_i,
60                                   SMESH_Mesh_i*           mesh_i,
61                                   int                     localId )
62      : SALOME::GenericObj_i( thePOA )
63 {
64   MESSAGE("SMESH_subMesh_i::SMESH_subMesh_i");
65   _gen_i = gen_i;
66   _mesh_i = mesh_i;
67   _localId = localId;
68   thePOA->activate_object( this );
69   // ****
70 }
71 //=============================================================================
72 /*!
73  *  
74  */
75 //=============================================================================
76
77 SMESH_subMesh_i::~SMESH_subMesh_i()
78 {
79   MESSAGE("SMESH_subMesh_i::~SMESH_subMesh_i");
80   // ****
81 }
82
83 //=============================================================================
84 /*!
85  *  
86  */
87 //=============================================================================
88
89 CORBA::Long SMESH_subMesh_i::GetNumberOfElements()
90   throw (SALOME::SALOME_Exception)
91 {
92   Unexpect aCatch(SALOME_SalomeException);
93   MESSAGE("SMESH_subMesh_i::GetNumberOfElements");
94   if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
95     return 0;
96
97   SMESHDS_SubMesh* aSubMeshDS = _mesh_i->_mapSubMesh[_localId]->GetSubMeshDS();
98   if ( aSubMeshDS == NULL )
99     return 0;
100
101   return aSubMeshDS->NbElements();
102 }
103
104 //=============================================================================
105 /*!
106  *  
107  */
108 //=============================================================================
109
110 CORBA::Long SMESH_subMesh_i::GetNumberOfNodes(CORBA::Boolean all)
111   throw (SALOME::SALOME_Exception)
112 {
113   Unexpect aCatch(SALOME_SalomeException);
114   MESSAGE("SMESH_subMesh_i::GetNumberOfNodes");
115   if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
116     return 0;
117
118   SMESHDS_SubMesh* aSubMeshDS = _mesh_i->_mapSubMesh[_localId]->GetSubMeshDS();
119   if ( aSubMeshDS == NULL )
120     return 0;
121
122   if ( all ) { // all nodes of submesh elements
123     set<int> nodeIds;
124     SMDS_ElemIteratorPtr eIt = aSubMeshDS->GetElements();
125     while ( eIt->more() ) {
126       const SMDS_MeshElement* anElem = eIt->next();
127       SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
128       while ( nIt->more() )
129         nodeIds.insert( nIt->next()->GetID() );
130     }
131     return nodeIds.size();
132   }
133     
134   return aSubMeshDS->NbNodes();
135 }
136
137 //=============================================================================
138 /*!
139  *  
140  */
141 //=============================================================================
142   
143 SMESH::long_array* SMESH_subMesh_i::GetElementsId()
144   throw (SALOME::SALOME_Exception)
145 {
146   Unexpect aCatch(SALOME_SalomeException);
147   MESSAGE("SMESH_subMesh_i::GetElementsId");
148   SMESH::long_array_var aResult = new SMESH::long_array();
149
150   if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
151     return aResult._retn();
152
153   SMESHDS_SubMesh* aSubMeshDS = _mesh_i->_mapSubMesh[_localId]->GetSubMeshDS();
154   if ( aSubMeshDS == NULL )
155     return aResult._retn();
156
157   aResult->length( aSubMeshDS->NbElements() );
158   SMDS_ElemIteratorPtr anIt = aSubMeshDS->GetElements();
159   for ( int i = 0, n = aSubMeshDS->NbElements(); i < n && anIt->more(); i++ )
160     aResult[i] = anIt->next()->GetID();
161
162   return aResult._retn();
163 }
164
165
166 //=============================================================================
167 /*!
168  *  
169  */
170 //=============================================================================
171
172 SMESH::long_array* SMESH_subMesh_i::GetElementsByType( SMESH::ElementType theElemType )
173     throw (SALOME::SALOME_Exception)
174 {
175   Unexpect aCatch(SALOME_SalomeException);
176   MESSAGE("SMESH_subMesh_i::GetElementsByType");
177   SMESH::long_array_var aResult = new SMESH::long_array();
178
179   if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
180     return aResult._retn();
181
182   SMESHDS_SubMesh* aSubMeshDS = _mesh_i->_mapSubMesh[_localId]->GetSubMeshDS();
183   if ( aSubMeshDS == NULL )
184     return aResult._retn();
185
186   // No sense in returning ids of elements along with ids of nodes:
187   // when theElemType == SMESH::ALL, return node ids only if
188   // there are no elements
189   bool retNodes = (theElemType == SMESH::NODE ||
190                    (theElemType == SMESH::ALL && aSubMeshDS->NbElements() == 0));
191
192   // PAL5440, return all nodes belonging to elements of submesh
193   set<int> nodeIds;
194   if ( retNodes ) {
195     SMDS_ElemIteratorPtr eIt = aSubMeshDS->GetElements();
196     while ( eIt->more() ) {
197       const SMDS_MeshElement* anElem = eIt->next();
198       SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
199       while ( nIt->more() )
200         nodeIds.insert( nIt->next()->GetID() );
201     }
202   }
203   
204   if ( theElemType == SMESH::ALL )
205     aResult->length( aSubMeshDS->NbElements() + nodeIds.size());
206   else if ( theElemType == SMESH::NODE )
207     aResult->length( nodeIds.size() );
208   else
209     aResult->length( aSubMeshDS->NbElements() );
210
211   int i = 0, n = aResult->length();
212
213   if ( retNodes && !nodeIds.empty() ) {
214     set<int>::iterator idIt = nodeIds.begin();
215     for ( ; i < n && idIt != nodeIds.end() ; i++, idIt++ )
216       aResult[i] = *idIt;
217   }
218
219   if ( theElemType == SMESH::ALL || theElemType != SMESH::NODE ) {
220     SMDS_ElemIteratorPtr anIt = aSubMeshDS->GetElements();
221     while ( i < n && anIt->more() ) {
222       const SMDS_MeshElement* anElem = anIt->next();
223       if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
224         aResult[i++] = anElem->GetID();
225     }
226   }
227
228   aResult->length( i );
229
230   return aResult._retn();
231 }
232
233 //=============================================================================
234 /*!
235  *  
236  */
237 //=============================================================================
238   
239 SMESH::long_array* SMESH_subMesh_i::GetNodesId()
240   throw (SALOME::SALOME_Exception)
241 {
242   Unexpect aCatch(SALOME_SalomeException);
243   MESSAGE("SMESH_subMesh_i::GetNodesId");
244   SMESH::long_array_var aResult = new SMESH::long_array();
245
246   if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
247     return aResult._retn();
248
249   SMESHDS_SubMesh* aSubMeshDS = _mesh_i->_mapSubMesh[_localId]->GetSubMeshDS();
250   if ( aSubMeshDS == NULL )
251     return aResult._retn();
252
253   aResult->length( aSubMeshDS->NbNodes() );
254   SMDS_NodeIteratorPtr anIt = aSubMeshDS->GetNodes();
255   for ( int i = 0, n = aSubMeshDS->NbNodes(); i < n && anIt->more(); i++ )
256     aResult[i] = anIt->next()->GetID();
257
258   return aResult._retn();
259 }
260
261 //=============================================================================
262 /*!
263  *  
264  */
265 //=============================================================================
266   
267 SMESH::SMESH_Mesh_ptr SMESH_subMesh_i::GetFather()
268   throw (SALOME::SALOME_Exception)
269 {
270   Unexpect aCatch(SALOME_SalomeException);
271   MESSAGE("SMESH_subMesh_i::GetFather");
272   return _mesh_i->_this();
273 }
274
275 //=============================================================================
276 /*!
277  *  
278  */
279 //=============================================================================
280   
281 CORBA::Long SMESH_subMesh_i::GetId()
282 {
283   MESSAGE("SMESH_subMesh_i::GetId");
284   return _localId;
285 }
286
287 //=======================================================================
288 //function : GetSubShape
289 //purpose  : 
290 //=======================================================================
291
292 GEOM::GEOM_Object_ptr SMESH_subMesh_i::GetSubShape()
293      throw (SALOME::SALOME_Exception)
294 {
295   Unexpect aCatch(SALOME_SalomeException);
296   GEOM::GEOM_Object_var aShapeObj;
297   try {
298     if ( _mesh_i->_mapSubMesh.find( _localId ) != _mesh_i->_mapSubMesh.end()) {
299       TopoDS_Shape S = _mesh_i->_mapSubMesh[ _localId ]->GetSubShape();
300       if ( !S.IsNull() )
301         aShapeObj = _gen_i->ShapeToGeomObject( S );
302     }
303   }
304   catch(SALOME_Exception & S_ex) {
305     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
306   }
307   return aShapeObj._retn();
308 }
309
310 //=============================================================================
311 /*!
312  *  
313  */
314 //=============================================================================
315 SALOME_MED::FAMILY_ptr SMESH_subMesh_i::GetFamily()
316   throw (SALOME::SALOME_Exception)
317 {
318   Unexpect aCatch(SALOME_SalomeException);
319   SALOME_MED::MESH_var MEDMesh = GetFather()->GetMEDMesh();
320
321   SALOME_MED::Family_array_var families = 
322     MEDMesh->getFamilies(SALOME_MED::MED_NODE);
323     
324   for ( int i = 0; i < families->length(); i++ ) {
325     if ( families[i]->getIdentifier() == ( _localId ) )
326       return families[i];
327   }
328   
329   return SALOME_MED::FAMILY::_nil();
330 }
331
332 //=============================================================================
333 /*!
334  *  
335  */
336 //=============================================================================
337 SMESH::long_array* SMESH_subMesh_i::GetIDs()
338 {
339   SMESH::long_array_var aResult = GetElementsId();
340   return aResult._retn();
341 }