1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
23 // File : SMESH_Pattern_i.cxx
24 // Created : Fri Aug 20 16:15:49 2004
25 // Author : Edward AGAPOV (eap)
28 #include "SMESH_Pattern_i.hxx"
30 #include "GEOM_Client.hxx"
31 #include "SMESH_Gen_i.hxx"
32 #include "SMESH_Mesh.hxx"
33 #include "SMESH_Mesh_i.hxx"
34 #include "SMESH_PythonDump.hxx"
35 #include "SMDS_MeshFace.hxx"
36 #include "SMDS_MeshVolume.hxx"
38 #include <TopExp_Explorer.hxx>
40 #include <TopoDS_Face.hxx>
42 #include <Standard_Failure.hxx>
43 #include <Standard_ErrorHandler.hxx>
48 using SMESH::TPythonDump;
51 //=======================================================================
52 //function : dumpErrorCode
54 //=======================================================================
56 static void addErrorCode(const char* thePyCommand)
58 TPythonDump() << "if (isDone != 1):";
59 TPythonDump() << "\tprint '" << thePyCommand << " :', pattern.GetErrorCode()";
62 //=============================================================================
64 * SMESH_Gen_i::GetPattern
66 * Create pattern mapper
68 //=============================================================================
70 SMESH::SMESH_Pattern_ptr SMESH_Gen_i::GetPattern()
72 // Update Python script
73 TPythonDump() << "pattern = " << this << ".GetPattern()";
75 SMESH_Pattern_i* i = new SMESH_Pattern_i( this );
76 SMESH::SMESH_Pattern_var anObj = i->_this();
80 //=======================================================================
81 //function : SMESH_Pattern_i
83 //=======================================================================
85 SMESH_Pattern_i::SMESH_Pattern_i( SMESH_Gen_i* theGen_i ):
90 //=======================================================================
93 //=======================================================================
95 ::SMESH_Mesh* SMESH_Pattern_i::getMesh( SMESH::SMESH_Mesh_ptr & theMesh )
97 SMESH_Mesh_i* anImplPtr =
98 dynamic_cast<SMESH_Mesh_i*>( SMESH_Gen_i::GetServant( theMesh ).in() );
100 return & anImplPtr->GetImpl();
105 //=======================================================================
106 //function : LoadFromFile
108 //=======================================================================
110 CORBA::Boolean SMESH_Pattern_i::LoadFromFile(const char* theFileContents)
112 // remove some gabage from the end
113 TCollection_AsciiString patternDescription = (char*) theFileContents;
114 int pos = patternDescription.Length();
115 while (! isdigit( patternDescription.Value( pos )))
117 if ( pos != patternDescription.Length() ) {
118 patternDescription.Trunc( pos );
121 // Update Python script
122 TPythonDump() << "isDone = pattern.LoadFromFile("
123 << TPythonDump::LongStringStart("Pattern")
124 << patternDescription
125 << TPythonDump::LongStringEnd()
127 addErrorCode( "LoadFromFile" );
129 return myPattern.Load( theFileContents );
132 //=======================================================================
133 //function : LoadFromFace
135 //=======================================================================
137 CORBA::Boolean SMESH_Pattern_i::LoadFromFace(SMESH::SMESH_Mesh_ptr theMesh,
138 GEOM::GEOM_Object_ptr theFace,
139 CORBA::Boolean theProject)
141 if ( theMesh->_is_nil() || theFace->_is_nil() )
144 ::SMESH_Mesh* aMesh = getMesh( theMesh );
148 TopoDS_Shape aFace = myGen->GeomObjectToShape( theFace );
149 if ( aFace.IsNull() || aFace.ShapeType() != TopAbs_FACE )
152 // Update Python script
153 TPythonDump() << "isDone = pattern.LoadFromFace( " << theMesh << ", "
154 << theFace << ", " << theProject << " )";
155 addErrorCode( "LoadFromFace" );
157 return myPattern.Load( aMesh, TopoDS::Face( aFace ), theProject );
160 //=======================================================================
161 //function : LoadFrom3DBlock
163 //=======================================================================
165 CORBA::Boolean SMESH_Pattern_i::LoadFrom3DBlock(SMESH::SMESH_Mesh_ptr theMesh,
166 GEOM::GEOM_Object_ptr theBlock)
168 if ( theMesh->_is_nil() || theBlock->_is_nil() )
171 ::SMESH_Mesh* aMesh = getMesh( theMesh );
175 TopoDS_Shape aShape = myGen->GeomObjectToShape( theBlock );
176 if ( aShape.IsNull())
179 TopExp_Explorer exp ( aShape, TopAbs_SHELL );
183 // Update Python script
184 TPythonDump() << "isDone = pattern.LoadFrom3DBlock( " << theMesh << ", " << theBlock << " )";
185 addErrorCode( "LoadFrom3DBlock" );
187 return myPattern.Load( aMesh, TopoDS::Shell( exp.Current() ));
190 //=======================================================================
191 //function : ApplyToFace
193 //=======================================================================
195 SMESH::point_array* SMESH_Pattern_i::ApplyToFace(GEOM::GEOM_Object_ptr theFace,
196 GEOM::GEOM_Object_ptr theVertexOnKeyPoint1,
197 CORBA::Boolean theReverse)
199 SMESH::point_array_var points = new SMESH::point_array;
200 list<const gp_XYZ *> xyzList;
202 TopoDS_Shape F = myGen->GeomObjectToShape( theFace );
203 TopoDS_Shape V = myGen->GeomObjectToShape( theVertexOnKeyPoint1 );
205 if (!F.IsNull() && F.ShapeType() == TopAbs_FACE &&
206 !V.IsNull() && V.ShapeType() == TopAbs_VERTEX
208 myPattern.Apply( TopoDS::Face( F ), TopoDS::Vertex( V ), theReverse ) &&
209 myPattern.GetMappedPoints( xyzList ))
211 points->length( xyzList.size() );
212 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
213 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
214 SMESH::PointStruct & p = points[ i++ ];
215 (*xyzIt)->Coord( p.x, p.y, p.z );
219 // Update Python script
220 TPythonDump() << "pattern.ApplyToFace( " << theFace << ", "
221 << theVertexOnKeyPoint1 << ", " << theReverse << " )";
223 return points._retn();
226 //=======================================================================
227 //function : ApplyTo3DBlock
229 //=======================================================================
231 SMESH::point_array* SMESH_Pattern_i::ApplyTo3DBlock(GEOM::GEOM_Object_ptr theBlock,
232 GEOM::GEOM_Object_ptr theVertex000,
233 GEOM::GEOM_Object_ptr theVertex001)
235 SMESH::point_array_var points = new SMESH::point_array;
236 list<const gp_XYZ *> xyzList;
238 TopExp_Explorer exp( myGen->GeomObjectToShape( theBlock ), TopAbs_SHELL );
239 TopoDS_Shape V000 = myGen->GeomObjectToShape( theVertex000 );
240 TopoDS_Shape V001 = myGen->GeomObjectToShape( theVertex001 );
243 !V000.IsNull() && V000.ShapeType() == TopAbs_VERTEX &&
244 !V001.IsNull() && V001.ShapeType() == TopAbs_VERTEX
246 myPattern.Apply(TopoDS::Shell( exp.Current() ),
247 TopoDS::Vertex( V000 ),
248 TopoDS::Vertex( V001 )) &&
249 myPattern.GetMappedPoints( xyzList ))
251 points->length( xyzList.size() );
252 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
253 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
254 SMESH::PointStruct & p = points[ i++ ];
255 (*xyzIt)->Coord( p.x, p.y, p.z );
259 // Update Python script
260 TPythonDump() << "pattern.ApplyTo3DBlock( " << theBlock << ", "
261 << theVertex000 << ", " << theVertex001 << " )";
263 return points._retn();
266 //=======================================================================
267 //function : ApplyToMeshFaces
269 //=======================================================================
272 SMESH_Pattern_i::ApplyToMeshFaces(SMESH::SMESH_Mesh_ptr theMesh,
273 const SMESH::long_array& theFacesIDs,
274 CORBA::Long theNodeIndexOnKeyPoint1,
275 CORBA::Boolean theReverse)
277 SMESH::point_array_var points = new SMESH::point_array;
279 ::SMESH_Mesh* aMesh = getMesh( theMesh );
281 return points._retn();
283 list<const gp_XYZ *> xyzList;
284 set<const SMDS_MeshFace*> fset;
285 for (int i = 0; i < theFacesIDs.length(); i++)
287 CORBA::Long index = theFacesIDs[i];
288 const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
289 if ( elem && elem->GetType() == SMDSAbs_Face )
290 fset.insert( static_cast<const SMDS_MeshFace *>( elem ));
294 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
297 ok = myPattern.Apply( aMesh, fset, theNodeIndexOnKeyPoint1, theReverse );
299 catch (Standard_Failure& exc) {
300 MESSAGE("OCCT Exception in SMESH_Pattern: " << exc.GetMessageString());
302 catch ( std::exception& exc ) {
303 MESSAGE("STD Exception in SMESH_Pattern: << exc.what()");
306 MESSAGE("Unknown Exception in SMESH_Pattern");
309 if ( ok && myPattern.GetMappedPoints( xyzList ))
311 points->length( xyzList.size() );
312 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
313 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
314 SMESH::PointStruct & p = points[ i++ ];
315 (*xyzIt)->Coord( p.x, p.y, p.z );
319 // Update Python script
320 TPythonDump() << "pattern.ApplyToMeshFaces( " << theMesh << ", "
321 << theFacesIDs << ", "
322 << theNodeIndexOnKeyPoint1 << ", " << theReverse << " )";
324 return points._retn();
327 //=======================================================================
328 //function : ApplyToHexahedrons
330 //=======================================================================
333 SMESH_Pattern_i::ApplyToHexahedrons(SMESH::SMESH_Mesh_ptr theMesh,
334 const SMESH::long_array& theVolumesIDs,
335 CORBA::Long theNode000Index,
336 CORBA::Long theNode001Index)
338 SMESH::point_array_var points = new SMESH::point_array;
340 ::SMESH_Mesh* aMesh = getMesh( theMesh );
342 return points._retn();
344 list<const gp_XYZ *> xyzList;
345 set<const SMDS_MeshVolume*> vset;
346 for (int i = 0; i < theVolumesIDs.length(); i++)
348 CORBA::Long index = theVolumesIDs[i];
349 const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
350 if ( elem && elem->GetType() == SMDSAbs_Volume && elem->NbNodes() == 8 )
351 vset.insert( static_cast<const SMDS_MeshVolume *>( elem ));
353 if (myPattern.Apply( vset, theNode000Index, theNode001Index ) &&
354 myPattern.GetMappedPoints( xyzList ))
356 points->length( xyzList.size() );
357 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
358 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
359 SMESH::PointStruct & p = points[ i++ ];
360 (*xyzIt)->Coord( p.x, p.y, p.z );
364 // Update Python script
365 TPythonDump() << "pattern.ApplyToHexahedrons( " << theMesh << ", "
366 << theVolumesIDs << ", "
367 << theNode000Index << ", " << theNode001Index << " )";
369 return points._retn();
372 //=======================================================================
373 //function : MakeMesh
375 //=======================================================================
377 CORBA::Boolean SMESH_Pattern_i::MakeMesh (SMESH::SMESH_Mesh_ptr theMesh,
378 const CORBA::Boolean CreatePolygons,
379 const CORBA::Boolean CreatePolyedrs)
381 ::SMESH_Mesh* aMesh = getMesh( theMesh );
385 // Update Python script
386 TPythonDump() << "isDone = pattern.MakeMesh( " << theMesh << ", "
387 << CreatePolygons << ", " << CreatePolyedrs << " )";
388 addErrorCode( "MakeMesh" );
390 return myPattern.MakeMesh( aMesh, CreatePolygons, CreatePolyedrs );
393 //=======================================================================
394 //function : GetString
396 //=======================================================================
398 char* SMESH_Pattern_i::GetString()
401 myPattern.Save( os );
403 return CORBA::string_dup( os.str().c_str() );
406 //=======================================================================
409 //=======================================================================
411 CORBA::Boolean SMESH_Pattern_i::Is2D()
413 return myPattern.Is2D();
416 //=======================================================================
417 //function : GetPoints
419 //=======================================================================
421 SMESH::point_array* SMESH_Pattern_i::GetPoints()
423 SMESH::point_array_var points = new SMESH::point_array;
424 list<const gp_XYZ *> xyzList;
426 if (myPattern.GetPoints( xyzList ))
428 points->length( xyzList.size() );
429 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
430 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
431 SMESH::PointStruct & p = points[ i++ ];
432 (*xyzIt)->Coord( p.x, p.y, p.z );
436 return points._retn();
439 //=======================================================================
440 //function : GetKeyPoints
442 //=======================================================================
444 SMESH::long_array* SMESH_Pattern_i::GetKeyPoints()
446 SMESH::long_array_var ids = new SMESH::long_array;
447 if ( myPattern.IsLoaded() ) {
448 const list< int > & idList = myPattern.GetKeyPointIDs();
449 ids->length( idList.size() );
450 list< int >::const_iterator iIt = idList.begin();
451 for ( int i = 0; iIt != idList.end(); iIt++, i++ )
457 //=======================================================================
458 //function : GetElementPoints
460 //=======================================================================
462 SMESH::array_of_long_array* SMESH_Pattern_i::GetElementPoints(CORBA::Boolean applied)
464 SMESH::array_of_long_array_var arrayOfArray = new SMESH::array_of_long_array;
466 const list< list< int > >& listOfIdList = myPattern.GetElementPointIDs(applied);
467 arrayOfArray->length( listOfIdList.size() );
468 list< list< int > >::const_iterator llIt = listOfIdList.begin();
469 for ( int i = 0 ; llIt != listOfIdList.end(); llIt++, i++ )
471 const list< int > & idList = (*llIt);
472 SMESH::long_array& ids = arrayOfArray[ i ];
473 ids.length( idList.size() );
474 list< int >::const_iterator iIt = idList.begin();
475 for ( int j = 0; iIt != idList.end(); iIt++, j++ )
478 return arrayOfArray._retn();
481 //=======================================================================
482 //function : GetErrorCode
484 //=======================================================================
486 #define RETCASE(enm) case ::SMESH_Pattern::enm: return SMESH::SMESH_Pattern::enm;
488 SMESH::SMESH_Pattern::ErrorCode SMESH_Pattern_i::GetErrorCode()
490 switch ( myPattern.GetErrorCode() ) {
492 RETCASE( ERR_READ_NB_POINTS );
493 RETCASE( ERR_READ_POINT_COORDS );
494 RETCASE( ERR_READ_TOO_FEW_POINTS );
495 RETCASE( ERR_READ_3D_COORD );
496 RETCASE( ERR_READ_NO_KEYPOINT );
497 RETCASE( ERR_READ_BAD_INDEX );
498 RETCASE( ERR_READ_ELEM_POINTS );
499 RETCASE( ERR_READ_NO_ELEMS );
500 RETCASE( ERR_READ_BAD_KEY_POINT );
501 RETCASE( ERR_SAVE_NOT_LOADED );
502 RETCASE( ERR_LOAD_EMPTY_SUBMESH );
503 RETCASE( ERR_LOADF_NARROW_FACE );
504 RETCASE( ERR_LOADF_CLOSED_FACE );
505 RETCASE( ERR_LOADF_CANT_PROJECT );
506 RETCASE( ERR_LOADV_BAD_SHAPE );
507 RETCASE( ERR_LOADV_COMPUTE_PARAMS );
508 RETCASE( ERR_APPL_NOT_LOADED );
509 RETCASE( ERR_APPL_BAD_DIMENTION );
510 RETCASE( ERR_APPL_BAD_NB_VERTICES );
511 RETCASE( ERR_APPLF_BAD_TOPOLOGY );
512 RETCASE( ERR_APPLF_BAD_VERTEX );
513 RETCASE( ERR_APPLF_INTERNAL_EEROR );
514 RETCASE( ERR_APPLV_BAD_SHAPE );
515 RETCASE( ERR_MAKEM_NOT_COMPUTED );
518 return SMESH::SMESH_Pattern::ERR_OK;