00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "SMESH_Pattern_i.hxx"
00030
00031 #include "GEOM_Client.hxx"
00032 #include "SMESH_Gen_i.hxx"
00033 #include "SMESH_Mesh.hxx"
00034 #include "SMESH_Mesh_i.hxx"
00035 #include "SMESH_PythonDump.hxx"
00036 #include "SMDS_MeshFace.hxx"
00037 #include "SMDS_MeshVolume.hxx"
00038
00039 #include <TopExp_Explorer.hxx>
00040 #include <TopoDS.hxx>
00041 #include <TopoDS_Face.hxx>
00042
00043 #include <Standard_Failure.hxx>
00044 #include <Standard_ErrorHandler.hxx>
00045
00046 #include <sstream>
00047 #include <set>
00048
00049 using SMESH::TPythonDump;
00050
00051
00052
00053
00054
00055
00056 static void addErrorCode(const char* thePyCommand)
00057 {
00058 TPythonDump() << "if (isDone != 1):";
00059 TPythonDump() << "\tprint '" << thePyCommand << " :', pattern.GetErrorCode()";
00060 }
00061
00062
00068
00069
00070 SMESH::SMESH_Pattern_ptr SMESH_Gen_i::GetPattern()
00071 {
00072
00073 TPythonDump() << "pattern = " << this << ".GetPattern()";
00074
00075 SMESH_Pattern_i* i = new SMESH_Pattern_i( this );
00076 SMESH::SMESH_Pattern_var anObj = i->_this();
00077 return anObj._retn();
00078 }
00079
00080
00081
00082
00083
00084
00085 SMESH_Pattern_i::SMESH_Pattern_i( SMESH_Gen_i* theGen_i ):
00086 myGen( theGen_i )
00087 {
00088 }
00089
00090
00091
00092
00093
00094
00095 ::SMESH_Mesh* SMESH_Pattern_i::getMesh( SMESH::SMESH_Mesh_ptr & theMesh )
00096 {
00097 SMESH_Mesh_i* anImplPtr =
00098 dynamic_cast<SMESH_Mesh_i*>( SMESH_Gen_i::GetServant( theMesh ).in() );
00099 if ( anImplPtr )
00100 return & anImplPtr->GetImpl();
00101
00102 return 0;
00103 }
00104
00105
00106
00107
00108
00109
00110 CORBA::Boolean SMESH_Pattern_i::LoadFromFile(const char* theFileContents)
00111 {
00112
00113 TCollection_AsciiString patternDescription = (char*) theFileContents;
00114 int pos = patternDescription.Length();
00115 while (! isdigit( patternDescription.Value( pos )))
00116 pos--;
00117 if ( pos != patternDescription.Length() ) {
00118 patternDescription.Trunc( pos );
00119 }
00120
00121
00122 TPythonDump() << "isDone = pattern.LoadFromFile("
00123 << TPythonDump::LongStringStart("Pattern")
00124 << patternDescription
00125 << TPythonDump::LongStringEnd()
00126 << ")";
00127 addErrorCode( "LoadFromFile" );
00128
00129 return myPattern.Load( theFileContents );
00130 }
00131
00132
00133
00134
00135
00136
00137 CORBA::Boolean SMESH_Pattern_i::LoadFromFace(SMESH::SMESH_Mesh_ptr theMesh,
00138 GEOM::GEOM_Object_ptr theFace,
00139 CORBA::Boolean theProject)
00140 {
00141 if ( theMesh->_is_nil() || theFace->_is_nil() )
00142 return false;
00143
00144 ::SMESH_Mesh* aMesh = getMesh( theMesh );
00145 if ( !aMesh )
00146 return false;
00147
00148 TopoDS_Shape aFace = myGen->GeomObjectToShape( theFace );
00149 if ( aFace.IsNull() || aFace.ShapeType() != TopAbs_FACE )
00150 return false;
00151
00152
00153 TPythonDump() << "isDone = pattern.LoadFromFace( " << theMesh << ".GetMesh(), "
00154 << theFace << ", " << theProject << " )";
00155 addErrorCode( "LoadFromFace" );
00156
00157 return myPattern.Load( aMesh, TopoDS::Face( aFace ), theProject );
00158 }
00159
00160
00161
00162
00163
00164
00165 CORBA::Boolean SMESH_Pattern_i::LoadFrom3DBlock(SMESH::SMESH_Mesh_ptr theMesh,
00166 GEOM::GEOM_Object_ptr theBlock)
00167 {
00168 if ( theMesh->_is_nil() || theBlock->_is_nil() )
00169 return false;
00170
00171 ::SMESH_Mesh* aMesh = getMesh( theMesh );
00172 if ( !aMesh )
00173 return false;
00174
00175 TopoDS_Shape aShape = myGen->GeomObjectToShape( theBlock );
00176 if ( aShape.IsNull())
00177 return false;
00178
00179 TopExp_Explorer exp ( aShape, TopAbs_SHELL );
00180 if ( !exp.More() )
00181 return false;
00182
00183
00184 TPythonDump() << "isDone = pattern.LoadFrom3DBlock( " << theMesh << ".GetMesh(), " << theBlock << " )";
00185 addErrorCode( "LoadFrom3DBlock" );
00186
00187 return myPattern.Load( aMesh, TopoDS::Shell( exp.Current() ));
00188 }
00189
00190
00191
00192
00193
00194
00195 SMESH::point_array* SMESH_Pattern_i::ApplyToFace(GEOM::GEOM_Object_ptr theFace,
00196 GEOM::GEOM_Object_ptr theVertexOnKeyPoint1,
00197 CORBA::Boolean theReverse)
00198 {
00199 SMESH::point_array_var points = new SMESH::point_array;
00200 list<const gp_XYZ *> xyzList;
00201
00202 TopoDS_Shape F = myGen->GeomObjectToShape( theFace );
00203 TopoDS_Shape V = myGen->GeomObjectToShape( theVertexOnKeyPoint1 );
00204
00205 if (!F.IsNull() && F.ShapeType() == TopAbs_FACE &&
00206 !V.IsNull() && V.ShapeType() == TopAbs_VERTEX
00207 &&
00208 myPattern.Apply( TopoDS::Face( F ), TopoDS::Vertex( V ), theReverse ) &&
00209 myPattern.GetMappedPoints( xyzList ))
00210 {
00211 points->length( xyzList.size() );
00212 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
00213 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
00214 SMESH::PointStruct & p = points[ i++ ];
00215 (*xyzIt)->Coord( p.x, p.y, p.z );
00216 }
00217 }
00218
00219 TPythonDump() << "pattern.ApplyToFace( " << theFace << ", "
00220 << theVertexOnKeyPoint1 << ", " << theReverse << " )";
00221
00222 return points._retn();
00223 }
00224
00225
00226
00227
00228
00229
00230 SMESH::point_array* SMESH_Pattern_i::ApplyTo3DBlock(GEOM::GEOM_Object_ptr theBlock,
00231 GEOM::GEOM_Object_ptr theVertex000,
00232 GEOM::GEOM_Object_ptr theVertex001)
00233 {
00234 SMESH::point_array_var points = new SMESH::point_array;
00235 list<const gp_XYZ *> xyzList;
00236
00237 TopExp_Explorer exp( myGen->GeomObjectToShape( theBlock ), TopAbs_SHELL );
00238 TopoDS_Shape V000 = myGen->GeomObjectToShape( theVertex000 );
00239 TopoDS_Shape V001 = myGen->GeomObjectToShape( theVertex001 );
00240
00241 if (exp.More() &&
00242 !V000.IsNull() && V000.ShapeType() == TopAbs_VERTEX &&
00243 !V001.IsNull() && V001.ShapeType() == TopAbs_VERTEX
00244 &&
00245 myPattern.Apply(TopoDS::Shell( exp.Current() ),
00246 TopoDS::Vertex( V000 ),
00247 TopoDS::Vertex( V001 )) &&
00248 myPattern.GetMappedPoints( xyzList ))
00249 {
00250 points->length( xyzList.size() );
00251 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
00252 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
00253 SMESH::PointStruct & p = points[ i++ ];
00254 (*xyzIt)->Coord( p.x, p.y, p.z );
00255 }
00256 }
00257
00258
00259 TPythonDump() << "pattern.ApplyTo3DBlock( " << theBlock << ", "
00260 << theVertex000 << ", " << theVertex001 << " )";
00261
00262 return points._retn();
00263 }
00264
00265
00266
00267
00268
00269
00270 SMESH::point_array*
00271 SMESH_Pattern_i::ApplyToMeshFaces(SMESH::SMESH_Mesh_ptr theMesh,
00272 const SMESH::long_array& theFacesIDs,
00273 CORBA::Long theNodeIndexOnKeyPoint1,
00274 CORBA::Boolean theReverse)
00275 {
00276 SMESH::point_array_var points = new SMESH::point_array;
00277
00278 ::SMESH_Mesh* aMesh = getMesh( theMesh );
00279 if ( !aMesh )
00280 return points._retn();
00281
00282 list<const gp_XYZ *> xyzList;
00283 set<const SMDS_MeshFace*> fset;
00284 for (int i = 0; i < theFacesIDs.length(); i++)
00285 {
00286 CORBA::Long index = theFacesIDs[i];
00287 const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
00288 if ( elem && elem->GetType() == SMDSAbs_Face )
00289 fset.insert( static_cast<const SMDS_MeshFace *>( elem ));
00290 }
00291 bool ok = false;
00292 try {
00293 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
00294 OCC_CATCH_SIGNALS;
00295 #endif
00296 ok = myPattern.Apply( aMesh, fset, theNodeIndexOnKeyPoint1, theReverse );
00297 }
00298 catch (Standard_Failure& exc) {
00299 MESSAGE("OCCT Exception in SMESH_Pattern: " << exc.GetMessageString());
00300 }
00301 catch ( std::exception& exc ) {
00302 MESSAGE("STD Exception in SMESH_Pattern: << exc.what()");
00303 }
00304 catch ( ... ) {
00305 MESSAGE("Unknown Exception in SMESH_Pattern");
00306 }
00307
00308 if ( ok && myPattern.GetMappedPoints( xyzList ))
00309 {
00310 points->length( xyzList.size() );
00311 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
00312 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
00313 SMESH::PointStruct & p = points[ i++ ];
00314 (*xyzIt)->Coord( p.x, p.y, p.z );
00315 }
00316 }
00317
00318
00319 TPythonDump() << "pattern.ApplyToMeshFaces( " << theMesh << ".GetMesh(), "
00320 << theFacesIDs << ", "
00321 << theNodeIndexOnKeyPoint1 << ", " << theReverse << " )";
00322
00323 return points._retn();
00324 }
00325
00326
00327
00328
00329
00330
00331 SMESH::point_array*
00332 SMESH_Pattern_i::ApplyToHexahedrons(SMESH::SMESH_Mesh_ptr theMesh,
00333 const SMESH::long_array& theVolumesIDs,
00334 CORBA::Long theNode000Index,
00335 CORBA::Long theNode001Index)
00336 {
00337 SMESH::point_array_var points = new SMESH::point_array;
00338
00339 ::SMESH_Mesh* aMesh = getMesh( theMesh );
00340 if ( !aMesh )
00341 return points._retn();
00342
00343 list<const gp_XYZ *> xyzList;
00344 set<const SMDS_MeshVolume*> vset;
00345 for (int i = 0; i < theVolumesIDs.length(); i++)
00346 {
00347 CORBA::Long index = theVolumesIDs[i];
00348 const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
00349 if ( elem && elem->GetType() == SMDSAbs_Volume && elem->NbNodes() == 8 )
00350 vset.insert( static_cast<const SMDS_MeshVolume *>( elem ));
00351 }
00352 if (myPattern.Apply( vset, theNode000Index, theNode001Index ) &&
00353 myPattern.GetMappedPoints( xyzList ))
00354 {
00355 points->length( xyzList.size() );
00356 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
00357 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
00358 SMESH::PointStruct & p = points[ i++ ];
00359 (*xyzIt)->Coord( p.x, p.y, p.z );
00360 }
00361 }
00362
00363
00364 TPythonDump() << "pattern.ApplyToHexahedrons( " << theMesh << ".GetMesh(), "
00365 << theVolumesIDs << ", "
00366 << theNode000Index << ", " << theNode001Index << " )";
00367
00368 return points._retn();
00369 }
00370
00371
00372
00373
00374
00375
00376 CORBA::Boolean SMESH_Pattern_i::MakeMesh (SMESH::SMESH_Mesh_ptr theMesh,
00377 const CORBA::Boolean CreatePolygons,
00378 const CORBA::Boolean CreatePolyedrs)
00379 {
00380 ::SMESH_Mesh* aMesh = getMesh( theMesh );
00381 if ( !aMesh )
00382 return false;
00383
00384
00385 TPythonDump() << "isDone = pattern.MakeMesh( " << theMesh << ".GetMesh(), "
00386 << CreatePolygons << ", " << CreatePolyedrs << " )";
00387 addErrorCode( "MakeMesh" );
00388
00389 int nb = aMesh->NbNodes() + aMesh->NbEdges() + aMesh->NbFaces() + aMesh->NbVolumes();
00390
00391 bool res = myPattern.MakeMesh( aMesh, CreatePolygons, CreatePolyedrs );
00392
00393 if ( nb > 0 && nb != aMesh->NbNodes() + aMesh->NbEdges() + aMesh->NbFaces() + aMesh->NbVolumes())
00394 {
00395 aMesh->SetIsModified(true);
00396 aMesh->GetMeshDS()->Modified();
00397 }
00398 return res;
00399 }
00400
00401
00402
00403
00404
00405
00406 char* SMESH_Pattern_i::GetString()
00407 {
00408 ostringstream os;
00409 myPattern.Save( os );
00410
00411 return CORBA::string_dup( os.str().c_str() );
00412 }
00413
00414
00415
00416
00417
00418
00419 CORBA::Boolean SMESH_Pattern_i::Is2D()
00420 {
00421 return myPattern.Is2D();
00422 }
00423
00424
00425
00426
00427
00428
00429 SMESH::point_array* SMESH_Pattern_i::GetPoints()
00430 {
00431 SMESH::point_array_var points = new SMESH::point_array;
00432 list<const gp_XYZ *> xyzList;
00433
00434 if (myPattern.GetPoints( xyzList ))
00435 {
00436 points->length( xyzList.size() );
00437 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
00438 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
00439 SMESH::PointStruct & p = points[ i++ ];
00440 (*xyzIt)->Coord( p.x, p.y, p.z );
00441 }
00442 }
00443
00444 return points._retn();
00445 }
00446
00447
00448
00449
00450
00451
00452 SMESH::long_array* SMESH_Pattern_i::GetKeyPoints()
00453 {
00454 SMESH::long_array_var ids = new SMESH::long_array;
00455 if ( myPattern.IsLoaded() ) {
00456 const list< int > & idList = myPattern.GetKeyPointIDs();
00457 ids->length( idList.size() );
00458 list< int >::const_iterator iIt = idList.begin();
00459 for ( int i = 0; iIt != idList.end(); iIt++, i++ )
00460 ids[ i ] = *iIt;
00461 }
00462 return ids._retn();
00463 }
00464
00465
00466
00467
00468
00469
00470 SMESH::array_of_long_array* SMESH_Pattern_i::GetElementPoints(CORBA::Boolean applied)
00471 {
00472 SMESH::array_of_long_array_var arrayOfArray = new SMESH::array_of_long_array;
00473
00474 const list< list< int > >& listOfIdList = myPattern.GetElementPointIDs(applied);
00475 arrayOfArray->length( listOfIdList.size() );
00476 list< list< int > >::const_iterator llIt = listOfIdList.begin();
00477 for ( int i = 0 ; llIt != listOfIdList.end(); llIt++, i++ )
00478 {
00479 const list< int > & idList = (*llIt);
00480 SMESH::long_array& ids = arrayOfArray[ i ];
00481 ids.length( idList.size() );
00482 list< int >::const_iterator iIt = idList.begin();
00483 for ( int j = 0; iIt != idList.end(); iIt++, j++ )
00484 ids[ j ] = *iIt;
00485 }
00486 return arrayOfArray._retn();
00487 }
00488
00489
00490
00491
00492
00493
00494 #define RETCASE(enm) case ::SMESH_Pattern::enm: return SMESH::SMESH_Pattern::enm;
00495
00496 SMESH::SMESH_Pattern::ErrorCode SMESH_Pattern_i::GetErrorCode()
00497 {
00498 switch ( myPattern.GetErrorCode() ) {
00499 RETCASE( ERR_OK );
00500 RETCASE( ERR_READ_NB_POINTS );
00501 RETCASE( ERR_READ_POINT_COORDS );
00502 RETCASE( ERR_READ_TOO_FEW_POINTS );
00503 RETCASE( ERR_READ_3D_COORD );
00504 RETCASE( ERR_READ_NO_KEYPOINT );
00505 RETCASE( ERR_READ_BAD_INDEX );
00506 RETCASE( ERR_READ_ELEM_POINTS );
00507 RETCASE( ERR_READ_NO_ELEMS );
00508 RETCASE( ERR_READ_BAD_KEY_POINT );
00509 RETCASE( ERR_SAVE_NOT_LOADED );
00510 RETCASE( ERR_LOAD_EMPTY_SUBMESH );
00511 RETCASE( ERR_LOADF_NARROW_FACE );
00512 RETCASE( ERR_LOADF_CLOSED_FACE );
00513 RETCASE( ERR_LOADF_CANT_PROJECT );
00514 RETCASE( ERR_LOADV_BAD_SHAPE );
00515 RETCASE( ERR_LOADV_COMPUTE_PARAMS );
00516 RETCASE( ERR_APPL_NOT_LOADED );
00517 RETCASE( ERR_APPL_BAD_DIMENTION );
00518 RETCASE( ERR_APPL_BAD_NB_VERTICES );
00519 RETCASE( ERR_APPLF_BAD_TOPOLOGY );
00520 RETCASE( ERR_APPLF_BAD_VERTEX );
00521 RETCASE( ERR_APPLF_INTERNAL_EEROR );
00522 RETCASE( ERR_APPLV_BAD_SHAPE );
00523 RETCASE( ERR_MAKEM_NOT_COMPUTED );
00524 default:;
00525 };
00526 return SMESH::SMESH_Pattern::ERR_OK;
00527 }
00528