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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
43 #include <Standard_Failure.hxx>
44 #include <Standard_ErrorHandler.hxx>
49 using SMESH::TPythonDump;
52 //=======================================================================
53 //function : dumpErrorCode
55 //=======================================================================
57 static void addErrorCode(const char* thePyCommand)
59 TPythonDump() << "if (isDone != 1):";
60 TPythonDump() << "\tprint '" << thePyCommand << " :', pattern.GetErrorCode()";
63 //=============================================================================
65 * SMESH_Gen_i::GetPattern
67 * Create pattern mapper
69 //=============================================================================
71 SMESH::SMESH_Pattern_ptr SMESH_Gen_i::GetPattern()
73 // Update Python script
74 TPythonDump() << "pattern = " << this << ".GetPattern()";
76 SMESH_Pattern_i* i = new SMESH_Pattern_i( this );
77 SMESH::SMESH_Pattern_var anObj = i->_this();
81 //=======================================================================
82 //function : SMESH_Pattern_i
84 //=======================================================================
86 SMESH_Pattern_i::SMESH_Pattern_i( SMESH_Gen_i* theGen_i ):
91 //=======================================================================
94 //=======================================================================
96 ::SMESH_Mesh* SMESH_Pattern_i::getMesh( SMESH::SMESH_Mesh_ptr & theMesh )
98 SMESH_Mesh_i* anImplPtr =
99 dynamic_cast<SMESH_Mesh_i*>( SMESH_Gen_i::GetServant( theMesh ).in() );
101 return & anImplPtr->GetImpl();
106 //=======================================================================
107 //function : LoadFromFile
109 //=======================================================================
111 CORBA::Boolean SMESH_Pattern_i::LoadFromFile(const char* theFileContents)
113 // remove some gabage from the end
114 TCollection_AsciiString patternDescription = (char*) theFileContents;
115 int pos = patternDescription.Length();
116 while (! isdigit( patternDescription.Value( pos )))
118 if ( pos != patternDescription.Length() ) {
119 patternDescription.Trunc( pos );
122 // Update Python script
123 TPythonDump() << "isDone = pattern.LoadFromFile("
124 << TPythonDump::LongStringStart("Pattern")
125 << patternDescription
126 << TPythonDump::LongStringEnd()
128 addErrorCode( "LoadFromFile" );
130 return myPattern.Load( theFileContents );
133 //=======================================================================
134 //function : LoadFromFace
136 //=======================================================================
138 CORBA::Boolean SMESH_Pattern_i::LoadFromFace(SMESH::SMESH_Mesh_ptr theMesh,
139 GEOM::GEOM_Object_ptr theFace,
140 CORBA::Boolean theProject)
142 if ( theMesh->_is_nil() || theFace->_is_nil() )
145 ::SMESH_Mesh* aMesh = getMesh( theMesh );
149 TopoDS_Shape aFace = myGen->GeomObjectToShape( theFace );
150 if ( aFace.IsNull() || aFace.ShapeType() != TopAbs_FACE )
153 // Update Python script
154 TPythonDump() << "isDone = pattern.LoadFromFace( " << theMesh << ", "
155 << theFace << ", " << theProject << " )";
156 addErrorCode( "LoadFromFace" );
158 return myPattern.Load( aMesh, TopoDS::Face( aFace ), theProject );
161 //=======================================================================
162 //function : LoadFrom3DBlock
164 //=======================================================================
166 CORBA::Boolean SMESH_Pattern_i::LoadFrom3DBlock(SMESH::SMESH_Mesh_ptr theMesh,
167 GEOM::GEOM_Object_ptr theBlock)
169 if ( theMesh->_is_nil() || theBlock->_is_nil() )
172 ::SMESH_Mesh* aMesh = getMesh( theMesh );
176 TopoDS_Shape aShape = myGen->GeomObjectToShape( theBlock );
177 if ( aShape.IsNull())
180 TopExp_Explorer exp ( aShape, TopAbs_SHELL );
184 // Update Python script
185 TPythonDump() << "isDone = pattern.LoadFrom3DBlock( " << theMesh << ", " << theBlock << " )";
186 addErrorCode( "LoadFrom3DBlock" );
188 return myPattern.Load( aMesh, TopoDS::Shell( exp.Current() ));
191 //=======================================================================
192 //function : ApplyToFace
194 //=======================================================================
196 SMESH::point_array* SMESH_Pattern_i::ApplyToFace(GEOM::GEOM_Object_ptr theFace,
197 GEOM::GEOM_Object_ptr theVertexOnKeyPoint1,
198 CORBA::Boolean theReverse)
200 SMESH::point_array_var points = new SMESH::point_array;
201 list<const gp_XYZ *> xyzList;
203 TopoDS_Shape F = myGen->GeomObjectToShape( theFace );
204 TopoDS_Shape V = myGen->GeomObjectToShape( theVertexOnKeyPoint1 );
206 if (!F.IsNull() && F.ShapeType() == TopAbs_FACE &&
207 !V.IsNull() && V.ShapeType() == TopAbs_VERTEX
209 myPattern.Apply( TopoDS::Face( F ), TopoDS::Vertex( V ), theReverse ) &&
210 myPattern.GetMappedPoints( xyzList ))
212 points->length( xyzList.size() );
213 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
214 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
215 SMESH::PointStruct & p = points[ i++ ];
216 (*xyzIt)->Coord( p.x, p.y, p.z );
220 // Update Python script
221 TPythonDump() << "pattern.ApplyToFace( " << theFace << ", "
222 << theVertexOnKeyPoint1 << ", " << theReverse << " )";
224 return points._retn();
227 //=======================================================================
228 //function : ApplyTo3DBlock
230 //=======================================================================
232 SMESH::point_array* SMESH_Pattern_i::ApplyTo3DBlock(GEOM::GEOM_Object_ptr theBlock,
233 GEOM::GEOM_Object_ptr theVertex000,
234 GEOM::GEOM_Object_ptr theVertex001)
236 SMESH::point_array_var points = new SMESH::point_array;
237 list<const gp_XYZ *> xyzList;
239 TopExp_Explorer exp( myGen->GeomObjectToShape( theBlock ), TopAbs_SHELL );
240 TopoDS_Shape V000 = myGen->GeomObjectToShape( theVertex000 );
241 TopoDS_Shape V001 = myGen->GeomObjectToShape( theVertex001 );
244 !V000.IsNull() && V000.ShapeType() == TopAbs_VERTEX &&
245 !V001.IsNull() && V001.ShapeType() == TopAbs_VERTEX
247 myPattern.Apply(TopoDS::Shell( exp.Current() ),
248 TopoDS::Vertex( V000 ),
249 TopoDS::Vertex( V001 )) &&
250 myPattern.GetMappedPoints( xyzList ))
252 points->length( xyzList.size() );
253 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
254 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
255 SMESH::PointStruct & p = points[ i++ ];
256 (*xyzIt)->Coord( p.x, p.y, p.z );
260 // Update Python script
261 TPythonDump() << "pattern.ApplyTo3DBlock( " << theBlock << ", "
262 << theVertex000 << ", " << theVertex001 << " )";
264 return points._retn();
267 //=======================================================================
268 //function : ApplyToMeshFaces
270 //=======================================================================
273 SMESH_Pattern_i::ApplyToMeshFaces(SMESH::SMESH_Mesh_ptr theMesh,
274 const SMESH::long_array& theFacesIDs,
275 CORBA::Long theNodeIndexOnKeyPoint1,
276 CORBA::Boolean theReverse)
278 SMESH::point_array_var points = new SMESH::point_array;
280 ::SMESH_Mesh* aMesh = getMesh( theMesh );
282 return points._retn();
284 list<const gp_XYZ *> xyzList;
285 set<const SMDS_MeshFace*> fset;
286 for (int i = 0; i < theFacesIDs.length(); i++)
288 CORBA::Long index = theFacesIDs[i];
289 const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
290 if ( elem && elem->GetType() == SMDSAbs_Face )
291 fset.insert( static_cast<const SMDS_MeshFace *>( elem ));
295 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
298 ok = myPattern.Apply( aMesh, fset, theNodeIndexOnKeyPoint1, theReverse );
300 catch (Standard_Failure& exc) {
301 MESSAGE("OCCT Exception in SMESH_Pattern: " << exc.GetMessageString());
303 catch ( std::exception& exc ) {
304 MESSAGE("STD Exception in SMESH_Pattern: << exc.what()");
307 MESSAGE("Unknown Exception in SMESH_Pattern");
310 if ( ok && myPattern.GetMappedPoints( xyzList ))
312 points->length( xyzList.size() );
313 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
314 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
315 SMESH::PointStruct & p = points[ i++ ];
316 (*xyzIt)->Coord( p.x, p.y, p.z );
320 // Update Python script
321 TPythonDump() << "pattern.ApplyToMeshFaces( " << theMesh << ", "
322 << theFacesIDs << ", "
323 << theNodeIndexOnKeyPoint1 << ", " << theReverse << " )";
325 return points._retn();
328 //=======================================================================
329 //function : ApplyToHexahedrons
331 //=======================================================================
334 SMESH_Pattern_i::ApplyToHexahedrons(SMESH::SMESH_Mesh_ptr theMesh,
335 const SMESH::long_array& theVolumesIDs,
336 CORBA::Long theNode000Index,
337 CORBA::Long theNode001Index)
339 SMESH::point_array_var points = new SMESH::point_array;
341 ::SMESH_Mesh* aMesh = getMesh( theMesh );
343 return points._retn();
345 list<const gp_XYZ *> xyzList;
346 set<const SMDS_MeshVolume*> vset;
347 for (int i = 0; i < theVolumesIDs.length(); i++)
349 CORBA::Long index = theVolumesIDs[i];
350 const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
351 if ( elem && elem->GetType() == SMDSAbs_Volume && elem->NbNodes() == 8 )
352 vset.insert( static_cast<const SMDS_MeshVolume *>( elem ));
354 if (myPattern.Apply( vset, theNode000Index, theNode001Index ) &&
355 myPattern.GetMappedPoints( xyzList ))
357 points->length( xyzList.size() );
358 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
359 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
360 SMESH::PointStruct & p = points[ i++ ];
361 (*xyzIt)->Coord( p.x, p.y, p.z );
365 // Update Python script
366 TPythonDump() << "pattern.ApplyToHexahedrons( " << theMesh << ", "
367 << theVolumesIDs << ", "
368 << theNode000Index << ", " << theNode001Index << " )";
370 return points._retn();
373 //=======================================================================
374 //function : MakeMesh
376 //=======================================================================
378 CORBA::Boolean SMESH_Pattern_i::MakeMesh (SMESH::SMESH_Mesh_ptr theMesh,
379 const CORBA::Boolean CreatePolygons,
380 const CORBA::Boolean CreatePolyedrs)
382 ::SMESH_Mesh* aMesh = getMesh( theMesh );
386 // Update Python script
387 TPythonDump() << "isDone = pattern.MakeMesh( " << theMesh << ", "
388 << CreatePolygons << ", " << CreatePolyedrs << " )";
389 addErrorCode( "MakeMesh" );
391 return myPattern.MakeMesh( aMesh, CreatePolygons, CreatePolyedrs );
394 //=======================================================================
395 //function : GetString
397 //=======================================================================
399 char* SMESH_Pattern_i::GetString()
402 myPattern.Save( os );
404 return CORBA::string_dup( os.str().c_str() );
407 //=======================================================================
410 //=======================================================================
412 CORBA::Boolean SMESH_Pattern_i::Is2D()
414 return myPattern.Is2D();
417 //=======================================================================
418 //function : GetPoints
420 //=======================================================================
422 SMESH::point_array* SMESH_Pattern_i::GetPoints()
424 SMESH::point_array_var points = new SMESH::point_array;
425 list<const gp_XYZ *> xyzList;
427 if (myPattern.GetPoints( xyzList ))
429 points->length( xyzList.size() );
430 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
431 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
432 SMESH::PointStruct & p = points[ i++ ];
433 (*xyzIt)->Coord( p.x, p.y, p.z );
437 return points._retn();
440 //=======================================================================
441 //function : GetKeyPoints
443 //=======================================================================
445 SMESH::long_array* SMESH_Pattern_i::GetKeyPoints()
447 SMESH::long_array_var ids = new SMESH::long_array;
448 if ( myPattern.IsLoaded() ) {
449 const list< int > & idList = myPattern.GetKeyPointIDs();
450 ids->length( idList.size() );
451 list< int >::const_iterator iIt = idList.begin();
452 for ( int i = 0; iIt != idList.end(); iIt++, i++ )
458 //=======================================================================
459 //function : GetElementPoints
461 //=======================================================================
463 SMESH::array_of_long_array* SMESH_Pattern_i::GetElementPoints(CORBA::Boolean applied)
465 SMESH::array_of_long_array_var arrayOfArray = new SMESH::array_of_long_array;
467 const list< list< int > >& listOfIdList = myPattern.GetElementPointIDs(applied);
468 arrayOfArray->length( listOfIdList.size() );
469 list< list< int > >::const_iterator llIt = listOfIdList.begin();
470 for ( int i = 0 ; llIt != listOfIdList.end(); llIt++, i++ )
472 const list< int > & idList = (*llIt);
473 SMESH::long_array& ids = arrayOfArray[ i ];
474 ids.length( idList.size() );
475 list< int >::const_iterator iIt = idList.begin();
476 for ( int j = 0; iIt != idList.end(); iIt++, j++ )
479 return arrayOfArray._retn();
482 //=======================================================================
483 //function : GetErrorCode
485 //=======================================================================
487 #define RETCASE(enm) case ::SMESH_Pattern::enm: return SMESH::SMESH_Pattern::enm;
489 SMESH::SMESH_Pattern::ErrorCode SMESH_Pattern_i::GetErrorCode()
491 switch ( myPattern.GetErrorCode() ) {
493 RETCASE( ERR_READ_NB_POINTS );
494 RETCASE( ERR_READ_POINT_COORDS );
495 RETCASE( ERR_READ_TOO_FEW_POINTS );
496 RETCASE( ERR_READ_3D_COORD );
497 RETCASE( ERR_READ_NO_KEYPOINT );
498 RETCASE( ERR_READ_BAD_INDEX );
499 RETCASE( ERR_READ_ELEM_POINTS );
500 RETCASE( ERR_READ_NO_ELEMS );
501 RETCASE( ERR_READ_BAD_KEY_POINT );
502 RETCASE( ERR_SAVE_NOT_LOADED );
503 RETCASE( ERR_LOAD_EMPTY_SUBMESH );
504 RETCASE( ERR_LOADF_NARROW_FACE );
505 RETCASE( ERR_LOADF_CLOSED_FACE );
506 RETCASE( ERR_LOADF_CANT_PROJECT );
507 RETCASE( ERR_LOADV_BAD_SHAPE );
508 RETCASE( ERR_LOADV_COMPUTE_PARAMS );
509 RETCASE( ERR_APPL_NOT_LOADED );
510 RETCASE( ERR_APPL_BAD_DIMENTION );
511 RETCASE( ERR_APPL_BAD_NB_VERTICES );
512 RETCASE( ERR_APPLF_BAD_TOPOLOGY );
513 RETCASE( ERR_APPLF_BAD_VERTEX );
514 RETCASE( ERR_APPLF_INTERNAL_EEROR );
515 RETCASE( ERR_APPLV_BAD_SHAPE );
516 RETCASE( ERR_MAKEM_NOT_COMPUTED );
519 return SMESH::SMESH_Pattern::ERR_OK;