Salome HOME
Merge with OCC_development_01
[modules/smesh.git] / src / SMESH_I / SMESH_Pattern_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_Pattern_i.cxx
25 // Created   : Fri Aug 20 16:15:49 2004
26 // Author    : Edward AGAPOV (eap)
27 //  $Header: 
28
29 #include "SMESH_Pattern_i.hxx"
30
31 #include "GEOM_Client.hxx"
32 #include "SMESH_Gen_i.hxx"
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_Mesh_i.hxx"
35 #include "SMDS_MeshFace.hxx"
36 #include "SMDS_MeshVolume.hxx"
37
38 #include <TopExp_Explorer.hxx>
39 #include <TopoDS.hxx>
40 #include <TopoDS_Face.hxx>
41
42 #include <sstream>
43 #include <set>
44
45 // =============================================================================
46 //=============================================================================
47 /*!
48 // =============================================================================
49  *  SMESH_Gen_i::GetPattern
50  *
51  *  Create pattern mapper
52  */
53 //=============================================================================
54
55 SMESH::SMESH_Pattern_ptr SMESH_Gen_i::GetPattern()
56 {
57   SMESH_Pattern_i* i = new SMESH_Pattern_i( this );
58   SMESH::SMESH_Pattern_var anObj = i->_this();
59   return anObj._retn();
60 }
61
62 //=======================================================================
63 //function : SMESH_Pattern_i
64 //purpose  : 
65 //=======================================================================
66
67 SMESH_Pattern_i::SMESH_Pattern_i( SMESH_Gen_i* theGen_i ):
68        myGen( theGen_i )
69 {
70 }
71
72 //=======================================================================
73 //function : getMesh
74 //purpose  : 
75 //=======================================================================
76
77 ::SMESH_Mesh* SMESH_Pattern_i::getMesh( SMESH::SMESH_Mesh_ptr & theMesh )
78 {
79   SMESH_Mesh_i* anImplPtr = 
80     dynamic_cast<SMESH_Mesh_i*>( SMESH_Gen_i::GetServant( theMesh ).in() );
81   if ( anImplPtr )
82     return & anImplPtr->GetImpl();
83
84   return 0;
85 }
86
87 //=======================================================================
88 //function : LoadFromFile
89 //purpose  : 
90 //=======================================================================
91
92 CORBA::Boolean SMESH_Pattern_i::LoadFromFile(const char* theFileContents)
93 {
94   return myPattern.Load( theFileContents );
95 }
96
97 //=======================================================================
98 //function : LoadFromFace
99 //purpose  : 
100 //=======================================================================
101
102 CORBA::Boolean SMESH_Pattern_i::LoadFromFace(SMESH::SMESH_Mesh_ptr theMesh,
103                                              GEOM::GEOM_Object_ptr theFace,
104                                              CORBA::Boolean        theProject)
105 {
106   if ( theMesh->_is_nil() || theFace->_is_nil() )
107     return false;
108
109   ::SMESH_Mesh* aMesh = getMesh( theMesh );
110   if ( !aMesh )
111     return false;
112
113   TopoDS_Face aFace = TopoDS::Face( myGen->GeomObjectToShape( theFace ));
114   if ( aFace.IsNull() )
115     return false;
116
117   return myPattern.Load( aMesh, aFace, theProject );
118 }
119
120 //=======================================================================
121 //function : LoadFrom3DBlock
122 //purpose  : 
123 //=======================================================================
124
125 CORBA::Boolean SMESH_Pattern_i::LoadFrom3DBlock(SMESH::SMESH_Mesh_ptr theMesh,
126                                                 GEOM::GEOM_Object_ptr theBlock)
127 {
128   if ( theMesh->_is_nil() || theBlock->_is_nil() )
129     return false;
130
131   ::SMESH_Mesh* aMesh = getMesh( theMesh );
132   if ( !aMesh )
133     return false;
134
135   TopoDS_Shape aShape = myGen->GeomObjectToShape( theBlock );
136   if ( aShape.IsNull())
137     return false;
138
139   TopExp_Explorer exp ( aShape, TopAbs_SHELL );
140   if ( !exp.More() )
141     return false;
142
143   return myPattern.Load( aMesh, TopoDS::Shell( exp.Current() ));
144 }
145
146 //=======================================================================
147 //function : ApplyToFace
148 //purpose  : 
149 //=======================================================================
150
151 SMESH::point_array* SMESH_Pattern_i::ApplyToFace(GEOM::GEOM_Object_ptr theFace,
152                                                  GEOM::GEOM_Object_ptr theVertexOnKeyPoint1,
153                                                  CORBA::Boolean        theReverse)
154 {
155   SMESH::point_array_var points = new SMESH::point_array;
156   list<const gp_XYZ *> xyzList;
157
158   TopoDS_Shape F = myGen->GeomObjectToShape( theFace );
159   TopoDS_Shape V = myGen->GeomObjectToShape( theVertexOnKeyPoint1 );
160
161   if (!F.IsNull() && F.ShapeType() == TopAbs_FACE &&
162       !V.IsNull() && V.ShapeType() == TopAbs_VERTEX
163       &&
164       myPattern.Apply( TopoDS::Face( F ), TopoDS::Vertex( V ), theReverse ) &&
165       myPattern.GetMappedPoints( xyzList ))
166   {
167     points->length( xyzList.size() );
168     list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
169     for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
170       SMESH::PointStruct & p = points[ i++ ];
171       (*xyzIt)->Coord( p.x, p.y, p.z );
172     }
173   }
174
175   return points._retn();
176 }
177
178 //=======================================================================
179 //function : ApplyTo3DBlock
180 //purpose  : 
181 //=======================================================================
182
183 SMESH::point_array* SMESH_Pattern_i::ApplyTo3DBlock(GEOM::GEOM_Object_ptr theBlock,
184                                                     GEOM::GEOM_Object_ptr theVertex000,
185                                                     GEOM::GEOM_Object_ptr theVertex001)
186 {
187   SMESH::point_array_var points = new SMESH::point_array;
188   list<const gp_XYZ *> xyzList;
189
190   TopExp_Explorer exp( myGen->GeomObjectToShape( theBlock ), TopAbs_SHELL );
191   TopoDS_Shape V000 = myGen->GeomObjectToShape( theVertex000 );
192   TopoDS_Shape V001 = myGen->GeomObjectToShape( theVertex001 );
193
194   if (exp.More() &&
195       !V000.IsNull() && V000.ShapeType() == TopAbs_VERTEX &&
196       !V001.IsNull() && V001.ShapeType() == TopAbs_VERTEX 
197       &&
198       myPattern.Apply(TopoDS::Shell( exp.Current() ),
199                       TopoDS::Vertex( V000 ),
200                       TopoDS::Vertex( V001 )) &&
201       myPattern.GetMappedPoints( xyzList ))
202   {
203     points->length( xyzList.size() );
204     list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
205     for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
206       SMESH::PointStruct & p = points[ i++ ];
207       (*xyzIt)->Coord( p.x, p.y, p.z );
208     }
209   }
210
211   return points._retn();
212 }
213
214 //=======================================================================
215 //function : ApplyToMeshFaces
216 //purpose  : 
217 //=======================================================================
218
219 SMESH::point_array*
220   SMESH_Pattern_i::ApplyToMeshFaces(SMESH::SMESH_Mesh_ptr    theMesh,
221                                     const SMESH::long_array& theFacesIDs,
222                                     CORBA::Long              theNodeIndexOnKeyPoint1,
223                                     CORBA::Boolean           theReverse)
224 {
225   SMESH::point_array_var points = new SMESH::point_array;
226
227   ::SMESH_Mesh* aMesh = getMesh( theMesh );
228   if ( !aMesh )
229     return points._retn();
230
231   list<const gp_XYZ *> xyzList;
232   set<const SMDS_MeshFace*> fset;
233   for (int i = 0; i < theFacesIDs.length(); i++)
234   {
235     CORBA::Long index = theFacesIDs[i];
236     const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
237     if ( elem && elem->GetType() == SMDSAbs_Face )
238       fset.insert( static_cast<const SMDS_MeshFace *>( elem ));
239   }
240   if (myPattern.Apply( fset, theNodeIndexOnKeyPoint1, theReverse ) &&
241       myPattern.GetMappedPoints( xyzList ))
242   {
243     points->length( xyzList.size() );
244     list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
245     for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
246       SMESH::PointStruct & p = points[ i++ ];
247       (*xyzIt)->Coord( p.x, p.y, p.z );
248     }
249   }
250
251   return points._retn();
252 }
253
254 //=======================================================================
255 //function : ApplyToHexahedrons
256 //purpose  : 
257 //=======================================================================
258
259 SMESH::point_array*
260   SMESH_Pattern_i::ApplyToHexahedrons(SMESH::SMESH_Mesh_ptr    theMesh,
261                                       const SMESH::long_array& theVolumesIDs,
262                                       CORBA::Long              theNode000Index,
263                                       CORBA::Long              theNode001Index)
264 {
265   SMESH::point_array_var points = new SMESH::point_array;
266
267   ::SMESH_Mesh* aMesh = getMesh( theMesh );
268   if ( !aMesh )
269     return points._retn();
270
271   list<const gp_XYZ *> xyzList;
272   set<const SMDS_MeshVolume*> vset;
273   for (int i = 0; i < theVolumesIDs.length(); i++)
274   {
275     CORBA::Long index = theVolumesIDs[i];
276     const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
277     if ( elem && elem->GetType() == SMDSAbs_Volume && elem->NbNodes() == 8 )
278       vset.insert( static_cast<const SMDS_MeshVolume *>( elem ));
279   }
280   if (myPattern.Apply( vset, theNode000Index, theNode001Index ) &&
281       myPattern.GetMappedPoints( xyzList ))
282   {
283     points->length( xyzList.size() );
284     list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
285     for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
286       SMESH::PointStruct & p = points[ i++ ];
287       (*xyzIt)->Coord( p.x, p.y, p.z );
288     }
289   }
290
291   return points._retn();
292 }
293
294 //=======================================================================
295 //function : MakeMesh
296 //purpose  : 
297 //=======================================================================
298
299 CORBA::Boolean SMESH_Pattern_i::MakeMesh(SMESH::SMESH_Mesh_ptr theMesh)
300 {
301   ::SMESH_Mesh* aMesh = getMesh( theMesh );
302   if ( !aMesh )
303     return false;
304
305   return myPattern.MakeMesh( aMesh );
306 }
307
308 //=======================================================================
309 //function : GetString
310 //purpose  : 
311 //=======================================================================
312
313 char* SMESH_Pattern_i::GetString()
314 {
315   ostringstream os;
316   myPattern.Save( os );
317   
318   return CORBA::string_dup( os.str().c_str() );
319 }
320
321 //=======================================================================
322 //function : Is2D
323 //purpose  : 
324 //=======================================================================
325
326 CORBA::Boolean SMESH_Pattern_i::Is2D()
327 {
328   return myPattern.Is2D();
329 }
330
331 //=======================================================================
332 //function : GetPoints
333 //purpose  : 
334 //=======================================================================
335
336 SMESH::point_array* SMESH_Pattern_i::GetPoints()
337 {
338   SMESH::point_array_var points = new SMESH::point_array;
339   list<const gp_XYZ *> xyzList;
340
341   if (myPattern.GetPoints( xyzList ))
342   {
343     points->length( xyzList.size() );
344     list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
345     for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
346       SMESH::PointStruct & p = points[ i++ ];
347       (*xyzIt)->Coord( p.x, p.y, p.z );
348     }
349   }
350
351   return points._retn();
352 }
353
354 //=======================================================================
355 //function : GetKeyPoints
356 //purpose  : 
357 //=======================================================================
358
359 SMESH::long_array* SMESH_Pattern_i::GetKeyPoints()
360 {
361   SMESH::long_array_var ids = new SMESH::long_array;
362   if ( myPattern.IsLoaded() ) {
363     const list< int > & idList = myPattern.GetKeyPointIDs();
364     ids->length( idList.size() );
365     list< int >::const_iterator iIt = idList.begin();
366     for ( int i = 0; iIt != idList.end(); iIt++, i++ )
367       ids[ i ] = *iIt;
368   }
369   return ids._retn();
370 }
371
372 //=======================================================================
373 //function : GetElementPoints
374 //purpose  : 
375 //=======================================================================
376
377 SMESH::array_of_long_array* SMESH_Pattern_i::GetElementPoints()
378 {
379   SMESH::array_of_long_array_var arrayOfArray = new SMESH::array_of_long_array;
380
381   const list< list< int > >& listOfIdList = myPattern.GetElementPointIDs();
382   arrayOfArray->length( listOfIdList.size() );
383   list< list< int > >::const_iterator llIt = listOfIdList.begin();
384   for ( int i = 0 ; llIt != listOfIdList.end(); llIt++, i++ )
385   {
386     const list< int > & idList = (*llIt);
387     SMESH::long_array& ids = arrayOfArray[ i ];
388     ids.length( idList.size() );
389     list< int >::const_iterator iIt = idList.begin();
390     for ( int j = 0; iIt != idList.end(); iIt++, j++ )
391       ids[ j ] = *iIt;
392   }
393   return arrayOfArray._retn();
394 }
395
396 //=======================================================================
397 //function : GetErrorCode
398 //purpose  : 
399 //=======================================================================
400
401 #define RETCASE(enm) case ::SMESH_Pattern::enm: return SMESH::SMESH_Pattern::enm;
402
403 SMESH::SMESH_Pattern::ErrorCode SMESH_Pattern_i::GetErrorCode()
404 {
405   switch ( myPattern.GetErrorCode() ) {
406     RETCASE( ERR_OK );
407     RETCASE( ERR_READ_NB_POINTS );
408     RETCASE( ERR_READ_POINT_COORDS );
409     RETCASE( ERR_READ_TOO_FEW_POINTS );
410     RETCASE( ERR_READ_3D_COORD );
411     RETCASE( ERR_READ_NO_KEYPOINT );
412     RETCASE( ERR_READ_BAD_INDEX );
413     RETCASE( ERR_READ_ELEM_POINTS );
414     RETCASE( ERR_READ_NO_ELEMS );
415     RETCASE( ERR_READ_BAD_KEY_POINT );
416     RETCASE( ERR_SAVE_NOT_LOADED );
417     RETCASE( ERR_LOAD_EMPTY_SUBMESH );
418     RETCASE( ERR_LOADF_NARROW_FACE );
419     RETCASE( ERR_LOADF_CLOSED_FACE );
420     RETCASE( ERR_LOADV_BAD_SHAPE );
421     RETCASE( ERR_LOADV_COMPUTE_PARAMS );
422     RETCASE( ERR_APPL_NOT_LOADED );
423     RETCASE( ERR_APPL_BAD_DIMENTION );
424     RETCASE( ERR_APPL_BAD_NB_VERTICES );
425     RETCASE( ERR_APPLF_BAD_TOPOLOGY );
426     RETCASE( ERR_APPLF_BAD_VERTEX );
427     RETCASE( ERR_APPLF_INTERNAL_EEROR );
428     RETCASE( ERR_APPLV_BAD_SHAPE );
429     RETCASE( ERR_MAKEM_NOT_COMPUTED );
430   default:;
431   };
432   return SMESH::SMESH_Pattern::ERR_OK;
433 }
434