1 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESH_Pattern_i.cxx
25 // Created : Fri Aug 20 16:15:49 2004
26 // Author : Edward AGAPOV (eap)
29 #include "SMESH_Pattern_i.hxx"
31 #include "GEOM_Client.hxx"
32 #include "SMESH_Gen_i.hxx"
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_Mesh_i.hxx"
35 #include "SMESH_PythonDump.hxx"
36 #include "SMDS_MeshFace.hxx"
37 #include "SMDS_MeshVolume.hxx"
39 #include <TopExp_Explorer.hxx>
41 #include <TopoDS_Face.hxx>
46 using SMESH::TPythonDump;
48 //=======================================================================
49 //function : dumpErrorCode
51 //=======================================================================
53 static void addErrorCode(const char* thePyCommand)
55 TPythonDump() << "if (isDone != 1):";
56 TPythonDump() << "\tprint '" << thePyCommand << " :', pattern.GetErrorCode()";
59 //=============================================================================
61 * SMESH_Gen_i::GetPattern
63 * Create pattern mapper
65 //=============================================================================
67 SMESH::SMESH_Pattern_ptr SMESH_Gen_i::GetPattern()
69 // Update Python script
70 TPythonDump() << "pattern = " << this << ".GetPattern()";
72 SMESH_Pattern_i* i = new SMESH_Pattern_i( this );
73 SMESH::SMESH_Pattern_var anObj = i->_this();
77 //=======================================================================
78 //function : SMESH_Pattern_i
80 //=======================================================================
82 SMESH_Pattern_i::SMESH_Pattern_i( SMESH_Gen_i* theGen_i ):
87 //=======================================================================
90 //=======================================================================
92 ::SMESH_Mesh* SMESH_Pattern_i::getMesh( SMESH::SMESH_Mesh_ptr & theMesh )
94 SMESH_Mesh_i* anImplPtr =
95 dynamic_cast<SMESH_Mesh_i*>( SMESH_Gen_i::GetServant( theMesh ).in() );
97 return & anImplPtr->GetImpl();
102 //=======================================================================
103 //function : LoadFromFile
105 //=======================================================================
107 CORBA::Boolean SMESH_Pattern_i::LoadFromFile(const char* theFileContents)
109 // remove some gabage from the end
110 TCollection_AsciiString patternDescription = (char*) theFileContents;
111 int pos = patternDescription.Length();
112 while (! isdigit( patternDescription.Value( pos )))
114 if ( pos != patternDescription.Length() ) {
115 patternDescription.Trunc( pos );
118 // Update Python script
119 TPythonDump() << "isDone = pattern.LoadFromFile("
120 << TPythonDump::LongStringStart("Pattern")
121 << patternDescription
122 << TPythonDump::LongStringEnd()
124 addErrorCode( "LoadFromFile" );
126 return myPattern.Load( theFileContents );
129 //=======================================================================
130 //function : LoadFromFace
132 //=======================================================================
134 CORBA::Boolean SMESH_Pattern_i::LoadFromFace(SMESH::SMESH_Mesh_ptr theMesh,
135 GEOM::GEOM_Object_ptr theFace,
136 CORBA::Boolean theProject)
138 if ( theMesh->_is_nil() || theFace->_is_nil() )
141 ::SMESH_Mesh* aMesh = getMesh( theMesh );
145 TopoDS_Shape aFace = myGen->GeomObjectToShape( theFace );
146 if ( aFace.IsNull() || aFace.ShapeType() != TopAbs_FACE )
149 // Update Python script
150 TPythonDump() << "isDone = pattern.LoadFromFace( " << theMesh << ", "
151 << theFace << ", " << theProject << " )";
152 addErrorCode( "LoadFromFace" );
154 return myPattern.Load( aMesh, TopoDS::Face( aFace ), theProject );
157 //=======================================================================
158 //function : LoadFrom3DBlock
160 //=======================================================================
162 CORBA::Boolean SMESH_Pattern_i::LoadFrom3DBlock(SMESH::SMESH_Mesh_ptr theMesh,
163 GEOM::GEOM_Object_ptr theBlock)
165 if ( theMesh->_is_nil() || theBlock->_is_nil() )
168 ::SMESH_Mesh* aMesh = getMesh( theMesh );
172 TopoDS_Shape aShape = myGen->GeomObjectToShape( theBlock );
173 if ( aShape.IsNull())
176 TopExp_Explorer exp ( aShape, TopAbs_SHELL );
180 // Update Python script
181 TPythonDump() << "isDone = pattern.LoadFrom3DBlock( " << theMesh << ", " << theBlock << " )";
182 addErrorCode( "LoadFrom3DBlock" );
184 return myPattern.Load( aMesh, TopoDS::Shell( exp.Current() ));
187 //=======================================================================
188 //function : ApplyToFace
190 //=======================================================================
192 SMESH::point_array* SMESH_Pattern_i::ApplyToFace(GEOM::GEOM_Object_ptr theFace,
193 GEOM::GEOM_Object_ptr theVertexOnKeyPoint1,
194 CORBA::Boolean theReverse)
196 SMESH::point_array_var points = new SMESH::point_array;
197 list<const gp_XYZ *> xyzList;
199 TopoDS_Shape F = myGen->GeomObjectToShape( theFace );
200 TopoDS_Shape V = myGen->GeomObjectToShape( theVertexOnKeyPoint1 );
202 if (!F.IsNull() && F.ShapeType() == TopAbs_FACE &&
203 !V.IsNull() && V.ShapeType() == TopAbs_VERTEX
205 myPattern.Apply( TopoDS::Face( F ), TopoDS::Vertex( V ), theReverse ) &&
206 myPattern.GetMappedPoints( xyzList ))
208 points->length( xyzList.size() );
209 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
210 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
211 SMESH::PointStruct & p = points[ i++ ];
212 (*xyzIt)->Coord( p.x, p.y, p.z );
216 // Update Python script
217 TPythonDump() << "pattern.ApplyToFace( " << theFace << ", "
218 << theVertexOnKeyPoint1 << ", " << theReverse << " )";
220 return points._retn();
223 //=======================================================================
224 //function : ApplyTo3DBlock
226 //=======================================================================
228 SMESH::point_array* SMESH_Pattern_i::ApplyTo3DBlock(GEOM::GEOM_Object_ptr theBlock,
229 GEOM::GEOM_Object_ptr theVertex000,
230 GEOM::GEOM_Object_ptr theVertex001)
232 SMESH::point_array_var points = new SMESH::point_array;
233 list<const gp_XYZ *> xyzList;
235 TopExp_Explorer exp( myGen->GeomObjectToShape( theBlock ), TopAbs_SHELL );
236 TopoDS_Shape V000 = myGen->GeomObjectToShape( theVertex000 );
237 TopoDS_Shape V001 = myGen->GeomObjectToShape( theVertex001 );
240 !V000.IsNull() && V000.ShapeType() == TopAbs_VERTEX &&
241 !V001.IsNull() && V001.ShapeType() == TopAbs_VERTEX
243 myPattern.Apply(TopoDS::Shell( exp.Current() ),
244 TopoDS::Vertex( V000 ),
245 TopoDS::Vertex( V001 )) &&
246 myPattern.GetMappedPoints( xyzList ))
248 points->length( xyzList.size() );
249 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
250 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
251 SMESH::PointStruct & p = points[ i++ ];
252 (*xyzIt)->Coord( p.x, p.y, p.z );
256 // Update Python script
257 TPythonDump() << "pattern.ApplyTo3DBlock( " << theBlock << ", "
258 << theVertex000 << ", " << theVertex001 << " )";
260 return points._retn();
263 //=======================================================================
264 //function : ApplyToMeshFaces
266 //=======================================================================
269 SMESH_Pattern_i::ApplyToMeshFaces(SMESH::SMESH_Mesh_ptr theMesh,
270 const SMESH::long_array& theFacesIDs,
271 CORBA::Long theNodeIndexOnKeyPoint1,
272 CORBA::Boolean theReverse)
274 SMESH::point_array_var points = new SMESH::point_array;
276 ::SMESH_Mesh* aMesh = getMesh( theMesh );
278 return points._retn();
280 list<const gp_XYZ *> xyzList;
281 set<const SMDS_MeshFace*> fset;
282 for (int i = 0; i < theFacesIDs.length(); i++)
284 CORBA::Long index = theFacesIDs[i];
285 const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
286 if ( elem && elem->GetType() == SMDSAbs_Face )
287 fset.insert( static_cast<const SMDS_MeshFace *>( elem ));
289 if (myPattern.Apply( fset, theNodeIndexOnKeyPoint1, theReverse ) &&
290 myPattern.GetMappedPoints( xyzList ))
292 points->length( xyzList.size() );
293 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
294 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
295 SMESH::PointStruct & p = points[ i++ ];
296 (*xyzIt)->Coord( p.x, p.y, p.z );
300 // Update Python script
301 TPythonDump() << "pattern.ApplyToMeshFaces( " << theMesh << ", "
302 << theFacesIDs << ", "
303 << theNodeIndexOnKeyPoint1 << ", " << theReverse << " )";
305 return points._retn();
308 //=======================================================================
309 //function : ApplyToHexahedrons
311 //=======================================================================
314 SMESH_Pattern_i::ApplyToHexahedrons(SMESH::SMESH_Mesh_ptr theMesh,
315 const SMESH::long_array& theVolumesIDs,
316 CORBA::Long theNode000Index,
317 CORBA::Long theNode001Index)
319 SMESH::point_array_var points = new SMESH::point_array;
321 ::SMESH_Mesh* aMesh = getMesh( theMesh );
323 return points._retn();
325 list<const gp_XYZ *> xyzList;
326 set<const SMDS_MeshVolume*> vset;
327 for (int i = 0; i < theVolumesIDs.length(); i++)
329 CORBA::Long index = theVolumesIDs[i];
330 const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
331 if ( elem && elem->GetType() == SMDSAbs_Volume && elem->NbNodes() == 8 )
332 vset.insert( static_cast<const SMDS_MeshVolume *>( elem ));
334 if (myPattern.Apply( vset, theNode000Index, theNode001Index ) &&
335 myPattern.GetMappedPoints( xyzList ))
337 points->length( xyzList.size() );
338 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
339 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
340 SMESH::PointStruct & p = points[ i++ ];
341 (*xyzIt)->Coord( p.x, p.y, p.z );
345 // Update Python script
346 TPythonDump() << "pattern.ApplyToHexahedrons( " << theMesh << ", "
347 << theVolumesIDs << ", "
348 << theNode000Index << ", " << theNode001Index << " )";
350 return points._retn();
353 //=======================================================================
354 //function : MakeMesh
356 //=======================================================================
358 CORBA::Boolean SMESH_Pattern_i::MakeMesh (SMESH::SMESH_Mesh_ptr theMesh,
359 const CORBA::Boolean CreatePolygons,
360 const CORBA::Boolean CreatePolyedrs)
362 ::SMESH_Mesh* aMesh = getMesh( theMesh );
366 // Update Python script
367 TPythonDump() << "isDone = pattern.MakeMesh( " << theMesh << ", "
368 << CreatePolygons << ", " << CreatePolyedrs << " )";
369 addErrorCode( "MakeMesh" );
371 return myPattern.MakeMesh( aMesh, CreatePolygons, CreatePolyedrs );
374 //=======================================================================
375 //function : GetString
377 //=======================================================================
379 char* SMESH_Pattern_i::GetString()
382 myPattern.Save( os );
384 return CORBA::string_dup( os.str().c_str() );
387 //=======================================================================
390 //=======================================================================
392 CORBA::Boolean SMESH_Pattern_i::Is2D()
394 return myPattern.Is2D();
397 //=======================================================================
398 //function : GetPoints
400 //=======================================================================
402 SMESH::point_array* SMESH_Pattern_i::GetPoints()
404 SMESH::point_array_var points = new SMESH::point_array;
405 list<const gp_XYZ *> xyzList;
407 if (myPattern.GetPoints( xyzList ))
409 points->length( xyzList.size() );
410 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
411 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
412 SMESH::PointStruct & p = points[ i++ ];
413 (*xyzIt)->Coord( p.x, p.y, p.z );
417 return points._retn();
420 //=======================================================================
421 //function : GetKeyPoints
423 //=======================================================================
425 SMESH::long_array* SMESH_Pattern_i::GetKeyPoints()
427 SMESH::long_array_var ids = new SMESH::long_array;
428 if ( myPattern.IsLoaded() ) {
429 const list< int > & idList = myPattern.GetKeyPointIDs();
430 ids->length( idList.size() );
431 list< int >::const_iterator iIt = idList.begin();
432 for ( int i = 0; iIt != idList.end(); iIt++, i++ )
438 //=======================================================================
439 //function : GetElementPoints
441 //=======================================================================
443 SMESH::array_of_long_array* SMESH_Pattern_i::GetElementPoints(CORBA::Boolean applied)
445 SMESH::array_of_long_array_var arrayOfArray = new SMESH::array_of_long_array;
447 const list< list< int > >& listOfIdList = myPattern.GetElementPointIDs(applied);
448 arrayOfArray->length( listOfIdList.size() );
449 list< list< int > >::const_iterator llIt = listOfIdList.begin();
450 for ( int i = 0 ; llIt != listOfIdList.end(); llIt++, i++ )
452 const list< int > & idList = (*llIt);
453 SMESH::long_array& ids = arrayOfArray[ i ];
454 ids.length( idList.size() );
455 list< int >::const_iterator iIt = idList.begin();
456 for ( int j = 0; iIt != idList.end(); iIt++, j++ )
459 return arrayOfArray._retn();
462 //=======================================================================
463 //function : GetErrorCode
465 //=======================================================================
467 #define RETCASE(enm) case ::SMESH_Pattern::enm: return SMESH::SMESH_Pattern::enm;
469 SMESH::SMESH_Pattern::ErrorCode SMESH_Pattern_i::GetErrorCode()
471 switch ( myPattern.GetErrorCode() ) {
473 RETCASE( ERR_READ_NB_POINTS );
474 RETCASE( ERR_READ_POINT_COORDS );
475 RETCASE( ERR_READ_TOO_FEW_POINTS );
476 RETCASE( ERR_READ_3D_COORD );
477 RETCASE( ERR_READ_NO_KEYPOINT );
478 RETCASE( ERR_READ_BAD_INDEX );
479 RETCASE( ERR_READ_ELEM_POINTS );
480 RETCASE( ERR_READ_NO_ELEMS );
481 RETCASE( ERR_READ_BAD_KEY_POINT );
482 RETCASE( ERR_SAVE_NOT_LOADED );
483 RETCASE( ERR_LOAD_EMPTY_SUBMESH );
484 RETCASE( ERR_LOADF_NARROW_FACE );
485 RETCASE( ERR_LOADF_CLOSED_FACE );
486 RETCASE( ERR_LOADF_CANT_PROJECT );
487 RETCASE( ERR_LOADV_BAD_SHAPE );
488 RETCASE( ERR_LOADV_COMPUTE_PARAMS );
489 RETCASE( ERR_APPL_NOT_LOADED );
490 RETCASE( ERR_APPL_BAD_DIMENTION );
491 RETCASE( ERR_APPL_BAD_NB_VERTICES );
492 RETCASE( ERR_APPLF_BAD_TOPOLOGY );
493 RETCASE( ERR_APPLF_BAD_VERTEX );
494 RETCASE( ERR_APPLF_INTERNAL_EEROR );
495 RETCASE( ERR_APPLV_BAD_SHAPE );
496 RETCASE( ERR_MAKEM_NOT_COMPUTED );
499 return SMESH::SMESH_Pattern::ERR_OK;