Salome HOME
Merge from V5_1_4_BR 07/05/2010
[modules/smesh.git] / src / StdMeshers / StdMeshers_Penta_3D.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH StdMeshers_Penta_3D implementaion of SMESH idl descriptions
24 //  File   : StdMeshers_Penta_3D.cxx
25 //  Module : SMESH
26 //
27 #include "StdMeshers_Penta_3D.hxx"
28
29 #include "utilities.h"
30 #include "Utils_ExceptHandlers.hxx"
31
32 #include "SMDS_EdgePosition.hxx"
33 #include "SMDS_MeshElement.hxx"
34 #include "SMDS_VolumeOfNodes.hxx"
35 #include "SMDS_VolumeTool.hxx"
36 #include "SMESHDS_SubMesh.hxx"
37 #include "SMESH_Mesh.hxx"
38 #include "SMESH_MesherHelper.hxx"
39 #include "SMESH_subMesh.hxx"
40 #include "SMESH_subMeshEventListener.hxx"
41 #include "SMESH_Comment.hxx"
42
43 #include <BRep_Tool.hxx>
44 #include <TopExp.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <TopTools_ListOfShape.hxx>
50 #include <TopTools_SequenceOfShape.hxx>
51 #include <TopTools_MapOfShape.hxx>
52 #include <TopoDS.hxx>
53 #include <TopoDS_Edge.hxx>
54 #include <TopoDS_Shell.hxx>
55 #include <TopoDS_Vertex.hxx>
56 #include <gp_Pnt.hxx>
57
58 #include <stdio.h>
59 #include <algorithm>
60
61 using namespace std;
62
63 typedef map < int, int, less<int> >::iterator   \
64   StdMeshers_IteratorOfDataMapOfIntegerInteger;
65
66 enum { NB_WALL_FACES = 4 };
67
68 //=======================================================================
69 //function : StdMeshers_Penta_3D
70 //purpose  : 
71 //=======================================================================
72 StdMeshers_Penta_3D::StdMeshers_Penta_3D()
73   : myErrorStatus(SMESH_ComputeError::New())
74 {
75   myTol3D=0.1;
76   myWallNodesMaps.resize( SMESH_Block::NbFaces() );
77   myShapeXYZ.resize( SMESH_Block::NbSubShapes() );
78   myTool = 0;
79 }
80
81 //=======================================================================
82 //function : ~StdMeshers_Penta_3D
83 //purpose  : 
84 //=======================================================================
85
86 StdMeshers_Penta_3D::~StdMeshers_Penta_3D()
87 {
88 }
89
90 //=======================================================================
91 //function : Compute
92 //purpose  : 
93 //=======================================================================
94 bool StdMeshers_Penta_3D::Compute(SMESH_Mesh& aMesh, 
95                                   const TopoDS_Shape& aShape)
96 {
97   MESSAGE("StdMeshers_Penta_3D::Compute()");
98   //
99   bool bOK=false;
100   //
101   myShape=aShape;
102   SetMesh(aMesh);
103   //
104   CheckData();
105   if (!myErrorStatus->IsOK()) {
106     return bOK;
107   }
108
109   //
110   MakeBlock();
111   if (!myErrorStatus->IsOK()) {
112     return bOK;
113   }
114   //
115   ClearMeshOnFxy1();
116   if (!myErrorStatus->IsOK()) {
117     return bOK;
118   }
119
120   // now unnecessary faces removed, we can load medium nodes
121   SMESH_MesherHelper helper(aMesh);
122   myTool = &helper;
123   myCreateQuadratic = myTool->IsQuadraticSubMesh(aShape);
124
125   //
126   MakeNodes();
127   if (!myErrorStatus->IsOK()) {
128     return bOK;
129   }
130   //
131   MakeConnectingMap();
132   //
133   MakeMeshOnFxy1();
134   if (!myErrorStatus->IsOK()) {
135     return bOK;
136   }
137   //
138   MakeVolumeMesh();
139   //
140   return !bOK;
141 }
142
143 //=======================================================================
144 //function : MakeNodes
145 //purpose  : 
146 //=======================================================================
147 void StdMeshers_Penta_3D::MakeNodes()
148 {
149   const int aNbSIDs=9;
150   int i, j, k, ij, iNbN, aNodeID, aSize, iErr;
151   double aX, aY, aZ;
152   SMESH_Block::TShapeID aSID, aSIDs[aNbSIDs]={
153     SMESH_Block::ID_V000, SMESH_Block::ID_V100, 
154     SMESH_Block::ID_V110, SMESH_Block::ID_V010,
155     SMESH_Block::ID_Ex00, SMESH_Block::ID_E1y0, 
156     SMESH_Block::ID_Ex10, SMESH_Block::ID_E0y0,
157     SMESH_Block::ID_Fxy0
158   }; 
159   //
160   SMESH_Mesh* pMesh=GetMesh();
161   //
162   // 1. Define the sizes of mesh
163   //
164   // 1.1 Horizontal size
165   myJSize=0;
166   for (i=0; i<aNbSIDs; ++i) {
167     const TopoDS_Shape& aS = myBlock.Shape(aSIDs[i]);
168     SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aS);
169     ASSERT(aSubMesh);
170     SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
171     if(!myCreateQuadratic) {
172       iNbN = aSM->NbNodes();
173     }
174     else {
175       iNbN = 0;
176       SMDS_NodeIteratorPtr itn = aSM->GetNodes();
177       while(itn->more()) {
178         const SMDS_MeshNode* aNode = itn->next();
179         if(myTool->IsMedium(aNode))
180           continue;
181         iNbN++;
182       }
183     }
184     myJSize += iNbN;
185   }
186   //printf("***  Horizontal: number of nodes summary=%d\n", myJSize);
187   //
188   // 1.2 Vertical size
189   myISize=2;
190   {
191     const TopoDS_Shape& aS=myBlock.Shape(SMESH_Block::ID_E00z);
192     SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aS);
193     ASSERT(aSubMesh);
194     SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
195     if(!myCreateQuadratic) {
196       iNbN = aSM->NbNodes();
197     }
198     else {
199       iNbN = 0;
200       SMDS_NodeIteratorPtr itn = aSM->GetNodes();
201       while(itn->more()) {
202         const SMDS_MeshNode* aNode = itn->next();
203         if(myTool->IsMedium(aNode))
204           continue;
205         iNbN++;
206       }
207     }
208     myISize += iNbN;
209   }
210   //printf("***  Vertical: number of nodes on edges and vertices=%d\n", myISize);
211   //
212   aSize=myISize*myJSize;
213   myTNodes.resize(aSize);
214   //
215   StdMeshers_TNode aTNode;
216   gp_XYZ aCoords;
217   gp_Pnt aP3D;
218   //
219   // 2. Fill the repers on base face (Z=0)
220   i=0; j=0;
221   // vertices
222   for (k=0; k<aNbSIDs; ++k) {
223     aSID=aSIDs[k];
224     const TopoDS_Shape& aS = myBlock.Shape(aSID);
225     SMDS_NodeIteratorPtr ite = pMesh->GetSubMeshContaining(aS)->GetSubMeshDS()->GetNodes();
226     while(ite->more()) {
227       const SMDS_MeshNode* aNode = ite->next();
228       if(myTool->IsMedium(aNode))
229         continue;
230       aNodeID=aNode->GetID();
231       //
232       aTNode.SetNode(aNode);
233       aTNode.SetShapeSupportID(aSID);
234       aTNode.SetBaseNodeID(aNodeID);
235       //
236       if ( SMESH_Block::IsEdgeID (aSID)) {
237         const SMDS_EdgePosition* epos =
238           static_cast<const SMDS_EdgePosition*>(aNode->GetPosition().get());
239         myBlock.ComputeParameters( epos->GetUParameter(), aS, aCoords );
240       }
241       else {
242         aX=aNode->X();
243         aY=aNode->Y();
244         aZ=aNode->Z();
245         aP3D.SetCoord(aX, aY, aZ);
246         myBlock.ComputeParameters(aP3D, aS, aCoords);
247       }
248       iErr = myBlock.ErrorStatus();
249       if (iErr) {
250         MESSAGE("StdMeshers_Penta_3D::MakeNodes()," <<
251                 "SMESHBlock: ComputeParameters operation failed");
252         myErrorStatus=myBlock.GetError();
253         return;
254       }
255       aTNode.SetNormCoord(aCoords);
256       ij=i*myJSize+j;
257       myTNodes[ij]=aTNode;
258       ++j;
259     }
260   }
261
262   // 3.1 Fill maps of wall nodes
263   SMESH_Block::TShapeID wallFaceID[ NB_WALL_FACES ] = {
264     SMESH_Block::ID_Fx0z, SMESH_Block::ID_Fx1z,
265     SMESH_Block::ID_F0yz, SMESH_Block::ID_F1yz
266   };
267   SMESH_Block::TShapeID baseEdgeID[ NB_WALL_FACES ] = {
268     SMESH_Block::ID_Ex00, SMESH_Block::ID_Ex10,
269     SMESH_Block::ID_E0y0, SMESH_Block::ID_E1y0
270   };
271   for ( i = 0; i < NB_WALL_FACES ; ++i ) {
272     int fIndex = SMESH_Block::ShapeIndex( wallFaceID[ i ]);
273     bool ok = LoadIJNodes (myWallNodesMaps[ fIndex ],
274                            TopoDS::Face( myBlock.Shape( wallFaceID[ i ] )),
275                            TopoDS::Edge( myBlock.Shape( baseEdgeID[ i ] )),
276                            pMesh->GetMeshDS());
277     if ( !ok ) {
278       myErrorStatus->myName = COMPERR_BAD_INPUT_MESH;
279       myErrorStatus->myComment = SMESH_Comment() <<
280         "Can't find regular quadrangle mesh on a side face #" <<
281         pMesh->GetMeshDS()->ShapeToIndex( myBlock.Shape( wallFaceID[ i ]));
282       return;
283     }
284   }
285
286   // 3.2 find node columns for vertical edges and edge IDs
287   vector<const SMDS_MeshNode*> * verticEdgeNodes[ NB_WALL_FACES ];
288   SMESH_Block::TShapeID          verticEdgeID   [ NB_WALL_FACES ];
289   for ( i = 0; i < NB_WALL_FACES ; ++i ) { // 4 first base nodes are nodes on vertices
290     // edge ID
291     SMESH_Block::TShapeID eID, vID = aSIDs[ i ];
292     ShapeSupportID(false, vID, eID);
293     verticEdgeID[ i ] = eID;
294     // column nodes
295     StdMeshers_TNode& aTNode = myTNodes[ i ];
296     verticEdgeNodes[ i ] = 0;
297     for ( j = 0; j < NB_WALL_FACES ; ++j ) { // loop on 4 wall faces
298       int fIndex = SMESH_Block::ShapeIndex( wallFaceID[ j ]);
299       StdMeshers_IJNodeMap & ijNodes= myWallNodesMaps[ fIndex ];
300       if ( ijNodes.begin()->second[0] == aTNode.Node() )
301         verticEdgeNodes[ i ] = & ijNodes.begin()->second;
302       else if ( ijNodes.rbegin()->second[0] == aTNode.Node() )
303         verticEdgeNodes[ i ] = & ijNodes.rbegin()->second;
304       if ( verticEdgeNodes[ i ] )
305         break;
306     }
307   }
308
309   // 3.3 set XYZ of vertices, and initialize of the rest
310   SMESHDS_Mesh* aMesh = GetMesh()->GetMeshDS();
311   for ( int id = SMESH_Block::ID_V000; id < SMESH_Block::ID_Shell; ++id ) {
312     if ( SMESH_Block::IsVertexID( id )) {
313       TopoDS_Shape V = myBlock.Shape( id );
314       SMESHDS_SubMesh* sm = aMesh->MeshElements( V );
315       const SMDS_MeshNode* n = sm->GetNodes()->next();
316       myShapeXYZ[ id ].SetCoord( n->X(), n->Y(), n->Z() );
317     }
318     else
319       myShapeXYZ[ id ].SetCoord( 0., 0., 0. );
320   }
321
322
323   // 4. Fill the rest repers
324   bool bIsUpperLayer;
325   int iBNID;
326   SMESH_Block::TShapeID aSSID, aBNSSID;
327   StdMeshers_TNode aTN;
328   //
329
330   // create top face and find UV for it's corners
331   const TopoDS_Face& TopFace = TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy1));
332   SMESHDS_Mesh* meshDS = pMesh->GetMeshDS();
333   int topfaceID = meshDS->ShapeToIndex(TopFace);
334   const TopoDS_Vertex& v001 = TopoDS::Vertex(myBlock.Shape(SMESH_Block::ID_V001));
335   SMDS_NodeIteratorPtr itn = pMesh->GetSubMeshContaining(v001)->GetSubMeshDS()->GetNodes();
336   const SMDS_MeshNode* N = itn->next();
337   gp_XY UV001 = myTool->GetNodeUV(TopFace,N);
338   const TopoDS_Vertex& v101 = TopoDS::Vertex(myBlock.Shape(SMESH_Block::ID_V101));
339   itn = pMesh->GetSubMeshContaining(v101)->GetSubMeshDS()->GetNodes();
340   N = itn->next();
341   gp_XY UV101 = myTool->GetNodeUV(TopFace,N);
342   const TopoDS_Vertex& v011 = TopoDS::Vertex(myBlock.Shape(SMESH_Block::ID_V011));
343   itn = pMesh->GetSubMeshContaining(v011)->GetSubMeshDS()->GetNodes();
344   N = itn->next();
345   gp_XY UV011 = myTool->GetNodeUV(TopFace,N);
346   const TopoDS_Vertex& v111 = TopoDS::Vertex(myBlock.Shape(SMESH_Block::ID_V111));
347   itn = pMesh->GetSubMeshContaining(v111)->GetSubMeshDS()->GetNodes();
348   N = itn->next();
349   gp_XY UV111 = myTool->GetNodeUV(TopFace,N);
350
351   for (j=0; j<myJSize; ++j) { // loop on all nodes of the base face (ID_Fxy0)
352     // base node info
353     const StdMeshers_TNode& aBN = myTNodes[j];
354     aBNSSID = (SMESH_Block::TShapeID)aBN.ShapeSupportID();
355     iBNID = aBN.BaseNodeID();
356     const gp_XYZ& aBNXYZ = aBN.NormCoord();
357     bool createNode = ( aBNSSID == SMESH_Block::ID_Fxy0 ); // if base node is inside a bottom face
358     //
359     // set XYZ on horizontal edges and get node columns of faces:
360     // 2 columns for each face, between which a base node is located
361     vector<const SMDS_MeshNode*>* nColumns[8];
362     double ratio[ NB_WALL_FACES ]; // base node position between columns [0.-1.]
363     if ( createNode ) {
364       for ( k = 0; k < NB_WALL_FACES ; ++k ) {
365         ratio[ k ] = SetHorizEdgeXYZ (aBNXYZ, wallFaceID[ k ],
366                                       nColumns[k*2], nColumns[k*2+1]);
367       }
368     }
369     //
370     // XYZ on the bottom and top faces
371     const SMDS_MeshNode* n = aBN.Node();
372     myShapeXYZ[ SMESH_Block::ID_Fxy0 ].SetCoord( n->X(), n->Y(), n->Z() );
373     myShapeXYZ[ SMESH_Block::ID_Fxy1 ].SetCoord( 0., 0., 0. );
374     //
375     // first create or find a top node, then the rest ones in a column
376     for (i=myISize-1; i>0; --i) // vertical loop, from top to bottom
377     {
378       bIsUpperLayer = (i==(myISize-1));
379       gp_XY UV_Ex01, UV_Ex11, UV_E0y1, UV_E1y1;
380       if ( createNode ) // a base node is inside a top face
381       {
382         // set XYZ on vertical edges and faces
383         for ( k = 0; k < NB_WALL_FACES ; ++k ) {
384           // XYZ on a vertical edge 
385           const SMDS_MeshNode* n = (*verticEdgeNodes[ k ]) [ i ];
386           myShapeXYZ[ verticEdgeID[ k ] ].SetCoord( n->X(), n->Y(), n->Z() );
387           // XYZ on a face (part 1 from one column)
388           n = (*nColumns[k*2]) [ i ];
389           gp_XYZ xyz( n->X(), n->Y(), n->Z() );
390           myShapeXYZ[ wallFaceID[ k ]] = ( 1. - ratio[ k ]) * xyz;
391           gp_XY tmp1;
392           if( bIsUpperLayer ) {
393             tmp1 = myTool->GetNodeUV(TopFace,n);
394             tmp1 = ( 1. - ratio[ k ]) * tmp1;
395           }
396           // XYZ on a face (part 2 from other column)
397           n = (*nColumns[k*2+1]) [ i ];
398           xyz.SetCoord( n->X(), n->Y(), n->Z() );
399           myShapeXYZ[ wallFaceID[ k ]] += ratio[ k ] * xyz;
400           if( bIsUpperLayer ) {
401             gp_XY tmp2 = myTool->GetNodeUV(TopFace,n);
402             tmp1 +=  ratio[ k ] * tmp2;
403             if( k==0 )
404               UV_Ex01 = tmp1;
405             else if( k==1 )
406               UV_Ex11 = tmp1;
407             else if( k==2 )
408               UV_E0y1 = tmp1;
409             else
410               UV_E1y1 = tmp1;
411           }
412         }
413       }
414       // fill current node info
415       //   -index in aTNodes
416       ij=i*myJSize+j; 
417       //   -normalized coordinates  
418       aX=aBNXYZ.X();  
419       aY=aBNXYZ.Y();
420       //aZ=aZL[i];
421       aZ=(double)i/(double)(myISize-1);
422       aCoords.SetCoord(aX, aY, aZ);
423       //
424       //   suporting shape ID
425       ShapeSupportID(bIsUpperLayer, aBNSSID, aSSID);
426       if (!myErrorStatus->IsOK()) {
427         MESSAGE("StdMeshers_Penta_3D::MakeNodes() ");
428         return;
429       }
430       //
431       aTN.SetShapeSupportID(aSSID);
432       aTN.SetNormCoord(aCoords);
433       aTN.SetBaseNodeID(iBNID);
434       //
435       if (aSSID!=SMESH_Block::ID_NONE){
436         // try to find the node
437         const TopoDS_Shape& aS=myBlock.Shape((int)aSSID);
438         FindNodeOnShape(aS, aCoords, i, aTN);
439       }
440       else{
441         // create node and get its id
442         CreateNode (bIsUpperLayer, aCoords, aTN);
443         //
444         if ( bIsUpperLayer ) {
445           const SMDS_MeshNode* n = aTN.Node();
446           myShapeXYZ[ SMESH_Block::ID_Fxy1 ].SetCoord( n->X(), n->Y(), n->Z() );
447           // set node on top face:
448           // find UV parameter for this node
449           //              UV_Ex11
450           //   UV011+-----+----------+UV111
451           //        |                |
452           //        |                |
453           // UV_E0y1+     +node      +UV_E1y1
454           //        |                |
455           //        |                |
456           //        |                |
457           //   UV001+-----+----------+UV101
458           //              UV_Ex01
459           gp_Pnt2d aP;
460           double u = aCoords.X(), v = aCoords.Y();
461           double u1 = ( 1. - u ), v1 = ( 1. - v );
462           aP.ChangeCoord()  = UV_Ex01 * v1;
463           aP.ChangeCoord() += UV_Ex11 * v;
464           aP.ChangeCoord() += UV_E0y1 * u1;
465           aP.ChangeCoord() += UV_E1y1 * u;
466           aP.ChangeCoord() -= UV001 * u1 * v1;
467           aP.ChangeCoord() -= UV101 * u  * v1;
468           aP.ChangeCoord() -= UV011 * u1 * v;
469           aP.ChangeCoord() -= UV111 * u  * v;
470           meshDS->SetNodeOnFace((SMDS_MeshNode*)n, topfaceID, aP.X(), aP.Y());
471         }
472       }
473       if (!myErrorStatus->IsOK()) {
474         MESSAGE("StdMeshers_Penta_3D::MakeNodes() ");
475         return;
476       }
477       //
478       myTNodes[ij]=aTN;
479     }
480   }
481 }
482
483
484 //=======================================================================
485 //function : FindNodeOnShape
486 //purpose  : 
487 //=======================================================================
488
489 void StdMeshers_Penta_3D::FindNodeOnShape(const TopoDS_Shape& aS,
490                                           const gp_XYZ&       aParams,
491                                           const int           z,
492                                           StdMeshers_TNode&   aTN)
493 {
494   double aX, aY, aZ, aD, aTol2, minD;
495   gp_Pnt aP1, aP2;
496   //
497   SMESH_Mesh* pMesh = GetMesh();
498   aTol2 = myTol3D*myTol3D;
499   minD = 1.e100;
500   SMDS_MeshNode* pNode = NULL;
501   //
502   if ( aS.ShapeType() == TopAbs_FACE ||
503        aS.ShapeType() == TopAbs_EDGE ) {
504     // find a face ID to which aTN belongs to
505     int faceID;
506     if ( aS.ShapeType() == TopAbs_FACE )
507       faceID = myBlock.ShapeID( aS );
508     else { // edge maybe vertical or top horizontal
509       gp_XYZ aCoord = aParams;
510       if ( aCoord.Z() == 1. )
511         aCoord.SetZ( 0.5 ); // move from top down
512       else
513         aCoord.SetX( 0.5 ); // move along X
514       faceID = SMESH_Block::GetShapeIDByParams( aCoord );
515     }
516     ASSERT( SMESH_Block::IsFaceID( faceID ));
517     int fIndex = SMESH_Block::ShapeIndex( faceID );
518     StdMeshers_IJNodeMap & ijNodes = myWallNodesMaps[ fIndex ];
519     // look for a base node in ijNodes
520     const SMDS_MeshNode* baseNode = pMesh->GetMeshDS()->FindNode( aTN.BaseNodeID() );
521     StdMeshers_IJNodeMap::const_iterator par_nVec = ijNodes.begin();
522     for ( ; par_nVec != ijNodes.end(); par_nVec++ )
523       if ( par_nVec->second[ 0 ] == baseNode ) {
524         pNode = (SMDS_MeshNode*)par_nVec->second.at( z );
525         aTN.SetNode(pNode);
526         return;
527       }
528   }
529   //
530   myBlock.Point(aParams, aS, aP1);
531   //
532   SMDS_NodeIteratorPtr ite=
533     pMesh->GetSubMeshContaining(aS)->GetSubMeshDS()->GetNodes();
534   while(ite->more()) {
535     const SMDS_MeshNode* aNode = ite->next();
536     if(myTool->IsMedium(aNode))
537       continue;
538     aX=aNode->X();
539     aY=aNode->Y();
540     aZ=aNode->Z();
541     aP2.SetCoord(aX, aY, aZ);
542     aD=(double)aP1.SquareDistance(aP2);
543     //printf("** D=%lf ", aD, aTol2);
544     if (aD < minD) {
545       pNode=(SMDS_MeshNode*)aNode;
546       aTN.SetNode(pNode);
547       minD = aD;
548       //printf(" Ok\n");
549       if (aD<aTol2)
550         return; 
551     }
552   }
553   //
554   //printf(" KO\n");
555   //aTN.SetNode(pNode);
556   //MESSAGE("StdMeshers_Penta_3D::FindNodeOnShape(), can not find the node");
557   //myErrorStatus=11; // can not find the node;
558 }
559
560
561 //=======================================================================
562 //function : SetHorizEdgeXYZ
563 //purpose  : 
564 //=======================================================================
565
566 double StdMeshers_Penta_3D::SetHorizEdgeXYZ(const gp_XYZ&                  aBaseNodeParams,
567                                             const int                      aFaceID,
568                                             vector<const SMDS_MeshNode*>*& aCol1,
569                                             vector<const SMDS_MeshNode*>*& aCol2)
570 {
571   // find base and top edges of the face
572   enum { BASE = 0, TOP };
573   vector< int > edgeVec; // 0-base, 1-top
574   SMESH_Block::GetFaceEdgesIDs( aFaceID, edgeVec );
575   //
576   int coord = SMESH_Block::GetCoordIndOnEdge( edgeVec[ BASE ] );
577   bool isForward = myBlock.IsForwadEdge( edgeVec[ BASE ] );
578
579   double param = aBaseNodeParams.Coord( coord );
580   if ( !isForward)
581     param = 1. - param;
582   //
583   // look for columns around param
584   StdMeshers_IJNodeMap & ijNodes =
585     myWallNodesMaps[ SMESH_Block::ShapeIndex( aFaceID )];
586   StdMeshers_IJNodeMap::iterator par_nVec_1 = ijNodes.begin();
587   while ( par_nVec_1->first < param )
588     par_nVec_1++;
589   StdMeshers_IJNodeMap::iterator par_nVec_2 = par_nVec_1;
590   //
591   double r = 0;
592   if ( par_nVec_1 != ijNodes.begin() ) {
593     par_nVec_1--;
594     r = ( param - par_nVec_1->first ) / ( par_nVec_2->first - par_nVec_1->first );
595   }
596   aCol1 = & par_nVec_1->second;
597   aCol2 = & par_nVec_2->second;
598
599   // top edge
600   if (1) {
601     // this variant is better for cases with curved edges and
602     // different nodes distribution on top and base edges
603     const SMDS_MeshNode* n1 = aCol1->back();
604     const SMDS_MeshNode* n2 = aCol2->back();
605     gp_XYZ xyz1( n1->X(), n1->Y(), n1->Z() );
606     gp_XYZ xyz2( n2->X(), n2->Y(), n2->Z() );
607     myShapeXYZ[ edgeVec[ 1 ] ] = ( 1. - r ) * xyz1 + r * xyz2;
608   }
609   else {
610     // this variant is better for other cases
611     //   SMESH_MesherHelper helper( *GetMesh() );
612     //   const TopoDS_Edge & edge = TopoDS::Edge( myBlock.Shape( edgeVec[ TOP ]));
613     //   double u1 = helper.GetNodeU( edge, n1 );
614     //   double u2 = helper.GetNodeU( edge, n2 );
615     //   double u = ( 1. - r ) * u1 + r * u2;
616     //   gp_XYZ topNodeParams;
617     //   myBlock.Block().EdgeParameters( edgeVec[ TOP ], u, topNodeParams );
618     //   myBlock.Block().EdgePoint( edgeVec[ TOP ],
619     //                              topNodeParams,
620     //                              myShapeXYZ[ edgeVec[ TOP ]]);
621   }
622
623   // base edge
624   myBlock.Block().EdgePoint( edgeVec[ BASE ],
625                              aBaseNodeParams,
626                              myShapeXYZ[ edgeVec[ BASE ]]);
627   return r;
628 }
629
630
631 //=======================================================================
632 //function : MakeVolumeMesh
633 //purpose  : 
634 //=======================================================================
635 void StdMeshers_Penta_3D::MakeVolumeMesh()
636 {
637   int i, j, ij, ik, i1, i2, aSSID; 
638   //
639   SMESH_Mesh*   pMesh = GetMesh();
640   SMESHDS_Mesh* meshDS = pMesh->GetMeshDS();
641   //
642   int shapeID = meshDS->ShapeToIndex( myShape );
643   //
644   // 1. Set Node In Volume
645   ik = myISize-1;
646   for (i=1; i<ik; ++i){
647     for (j=0; j<myJSize; ++j){
648       ij=i*myJSize+j;
649       const StdMeshers_TNode& aTN = myTNodes[ij];
650       aSSID=aTN.ShapeSupportID();
651       if (aSSID==SMESH_Block::ID_NONE) {
652         SMDS_MeshNode* aNode = (SMDS_MeshNode*)aTN.Node();
653         meshDS->SetNodeInVolume(aNode, shapeID);
654       }
655     }
656   }
657   //
658   // 2. Make pentahedrons
659   int aID0, k , aJ[3];
660   vector<const SMDS_MeshNode*> aN;
661   //
662   SMDS_ElemIteratorPtr itf, aItNodes;
663   //
664   const TopoDS_Face& aFxy0=
665     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy0));
666   SMESH_subMesh *aSubMesh0 = pMesh->GetSubMeshContaining(aFxy0);
667   SMESHDS_SubMesh *aSM0 = aSubMesh0->GetSubMeshDS();
668   //
669   itf = aSM0->GetElements();
670   while(itf->more()) {
671     const SMDS_MeshElement* pE0 = itf->next();
672     //
673     int nbFaceNodes = pE0->NbNodes();
674     if(myCreateQuadratic)
675       nbFaceNodes = nbFaceNodes/2;
676     if ( aN.size() < nbFaceNodes * 2 )
677       aN.resize( nbFaceNodes * 2 );
678     //
679     k=0;
680     aItNodes=pE0->nodesIterator();
681     while (aItNodes->more()) {
682       //const SMDS_MeshElement* pNode = aItNodes->next();
683       const SMDS_MeshNode* pNode =
684         static_cast<const SMDS_MeshNode*> (aItNodes->next());
685       if(myTool->IsMedium(pNode))
686         continue;
687       aID0 = pNode->GetID();
688       aJ[k] = GetIndexOnLayer(aID0);
689       if (!myErrorStatus->IsOK()) {
690         MESSAGE("StdMeshers_Penta_3D::MakeVolumeMesh");
691         return;
692       }
693       //
694       ++k;
695     }
696     //
697     bool forward = true;
698     for (i=0; i<ik; ++i) {
699       i1=i;
700       i2=i+1;
701       for(j=0; j<nbFaceNodes; ++j) {
702         ij = i1*myJSize+aJ[j];
703         const StdMeshers_TNode& aTN1 = myTNodes[ij];
704         const SMDS_MeshNode* aN1 = aTN1.Node();
705         aN[j]=aN1;
706         //
707         ij=i2*myJSize+aJ[j];
708         const StdMeshers_TNode& aTN2 = myTNodes[ij];
709         const SMDS_MeshNode* aN2 = aTN2.Node();
710         aN[j+nbFaceNodes] = aN2;
711       }
712       // check if volume orientation will be ok
713       if ( i == 0 ) {
714         SMDS_VolumeTool vTool;
715         switch ( nbFaceNodes ) {
716         case 3: {
717           SMDS_VolumeOfNodes tmpVol (aN[0], aN[1], aN[2],
718                                      aN[3], aN[4], aN[5]);
719           vTool.Set( &tmpVol );
720           break;
721         }
722         case 4: {
723           SMDS_VolumeOfNodes tmpVol(aN[0], aN[1], aN[2], aN[3],
724                                     aN[4], aN[5], aN[6], aN[7]);
725           vTool.Set( &tmpVol );
726           break;
727         }
728         default:
729           continue;
730         }
731         forward = vTool.IsForward();
732       }
733       // add volume
734       SMDS_MeshVolume* aV = 0;
735       switch ( nbFaceNodes ) {
736       case 3:
737         if ( forward ) {
738           //aV = meshDS->AddVolume(aN[0], aN[1], aN[2],
739           //                       aN[3], aN[4], aN[5]);
740           aV = myTool->AddVolume(aN[0], aN[1], aN[2], aN[3], aN[4], aN[5]);
741         }
742         else {
743           //aV = meshDS->AddVolume(aN[0], aN[2], aN[1],
744           //                       aN[3], aN[5], aN[4]);
745           aV = myTool->AddVolume(aN[0], aN[2], aN[1], aN[3], aN[5], aN[4]);
746         }
747         break;
748       case 4:
749         if ( forward ) {
750           //aV = meshDS->AddVolume(aN[0], aN[1], aN[2], aN[3],
751           //                       aN[4], aN[5], aN[6], aN[7]);
752           aV = myTool->AddVolume(aN[0], aN[1], aN[2], aN[3],
753                                  aN[4], aN[5], aN[6], aN[7]);
754         }
755         else {
756           //aV = meshDS->AddVolume(aN[0], aN[3], aN[2], aN[1],
757           //                       aN[4], aN[7], aN[6], aN[5]);
758           aV = myTool->AddVolume(aN[0], aN[3], aN[2], aN[1],
759                                  aN[4], aN[7], aN[6], aN[5]);
760         }
761         break;
762       default:
763         continue;
764       }
765       meshDS->SetMeshElementOnShape(aV, shapeID);
766     }
767   }
768 }
769
770 //=======================================================================
771 //function : MakeMeshOnFxy1
772 //purpose  : 
773 //=======================================================================
774 void StdMeshers_Penta_3D::MakeMeshOnFxy1()
775 {
776   int aID0, aJ, aLevel, ij, aNbNodes, k;
777   //
778   SMDS_NodeIteratorPtr itn;
779   SMDS_ElemIteratorPtr itf, aItNodes;
780   SMDSAbs_ElementType aElementType;
781   //
782   const TopoDS_Face& aFxy0=
783     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy0));
784   const TopoDS_Face& aFxy1=
785     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy1));
786   //
787   SMESH_Mesh* pMesh = GetMesh();
788   SMESHDS_Mesh * meshDS = pMesh->GetMeshDS();
789   //
790   SMESH_subMesh *aSubMesh1 = pMesh->GetSubMeshContaining(aFxy1);
791   SMESH_subMesh *aSubMesh0 = pMesh->GetSubMeshContaining(aFxy0);
792   SMESHDS_SubMesh *aSM0 = aSubMesh0->GetSubMeshDS();
793   //
794   // set nodes on aFxy1
795   aLevel = myISize-1;
796   itn = aSM0->GetNodes();
797   aNbNodes = aSM0->NbNodes();
798   //printf("** aNbNodes=%d\n", aNbNodes);
799   myTool->SetSubShape( aFxy1 ); // to set medium nodes to aFxy1
800   //
801   // set elements on aFxy1
802   vector<const SMDS_MeshNode*> aNodes1;
803   //
804   itf = aSM0->GetElements();
805   while(itf->more()) {
806     const SMDS_MeshElement* pE0 = itf->next();
807     aElementType = pE0->GetType();
808     if (!aElementType==SMDSAbs_Face) {
809       continue;
810     }
811     aNbNodes = pE0->NbNodes();
812     if(myCreateQuadratic)
813       aNbNodes = aNbNodes/2;
814     if ( aNodes1.size() < aNbNodes )
815       aNodes1.resize( aNbNodes );
816     //
817     k = aNbNodes-1; // reverse a face
818     aItNodes = pE0->nodesIterator();
819     while (aItNodes->more()) {
820       const SMDS_MeshNode* pNode =
821         static_cast<const SMDS_MeshNode*> (aItNodes->next());
822       if(myTool->IsMedium(pNode))
823         continue;
824       aID0 = pNode->GetID();
825       aJ = GetIndexOnLayer(aID0);
826       if (!myErrorStatus->IsOK()) {
827         MESSAGE("StdMeshers_Penta_3D::MakeMeshOnFxy1() ");
828         return;
829       }
830       //
831       ij = aLevel*myJSize + aJ;
832       const StdMeshers_TNode& aTN1 = myTNodes[ij];
833       const SMDS_MeshNode* aN1 = aTN1.Node();
834       aNodes1[k] = aN1;
835       --k;
836     }
837     SMDS_MeshFace * face = 0;
838     switch ( aNbNodes ) {
839     case 3:
840       face = myTool->AddFace(aNodes1[0], aNodes1[1], aNodes1[2]);
841       break;
842     case 4:
843       face = myTool->AddFace(aNodes1[0], aNodes1[1], aNodes1[2], aNodes1[3]);
844       break;
845     default:
846       continue;
847     }
848     meshDS->SetMeshElementOnShape(face, aFxy1);
849   }
850   myTool->SetSubShape( myShape );
851
852   // update compute state of top face submesh
853   aSubMesh1->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
854
855   // assure that mesh on the top face will be cleaned when it is cleaned
856   // on the bottom face
857   SMESH_subMesh* volSM = pMesh->GetSubMesh( myTool->GetSubShape() );
858   volSM->SetEventListener( new SMESH_subMeshEventListener(true),
859                            SMESH_subMeshEventListenerData::MakeData( aSubMesh1 ),
860                            aSubMesh0 ); // translate CLEAN event of aSubMesh0 to aSubMesh1
861 }
862
863 //=======================================================================
864 //function : ClearMeshOnFxy1
865 //purpose  : 
866 //=======================================================================
867 void StdMeshers_Penta_3D::ClearMeshOnFxy1()
868 {
869   SMESH_subMesh* aSubMesh;
870   SMESH_Mesh* pMesh=GetMesh();
871   //
872   const TopoDS_Shape& aFxy1=myBlock.Shape(SMESH_Block::ID_Fxy1);
873   aSubMesh = pMesh->GetSubMeshContaining(aFxy1);
874   if (aSubMesh)
875     aSubMesh->ComputeStateEngine( SMESH_subMesh::CLEAN );
876 }
877
878 //=======================================================================
879 //function : GetIndexOnLayer
880 //purpose  : 
881 //=======================================================================
882 int StdMeshers_Penta_3D::GetIndexOnLayer(const int aID)
883 {
884   int j=-1;
885   StdMeshers_IteratorOfDataMapOfIntegerInteger aMapIt;
886   //
887   aMapIt=myConnectingMap.find(aID);
888   if (aMapIt==myConnectingMap.end()) {
889     myErrorStatus->myName    = 200;
890     myErrorStatus->myComment = "Internal error of StdMeshers_Penta_3D";
891     return j;
892   }
893   j=(*aMapIt).second;
894   return j;
895 }
896
897 //=======================================================================
898 //function : MakeConnectingMap
899 //purpose  : 
900 //=======================================================================
901 void StdMeshers_Penta_3D::MakeConnectingMap()
902 {
903   int j, aBNID;
904   //
905   for (j=0; j<myJSize; ++j) {
906     const StdMeshers_TNode& aBN=myTNodes[j];
907     aBNID=aBN.BaseNodeID();
908     myConnectingMap[aBNID]=j;
909   }
910 }
911
912 //=======================================================================
913 //function : CreateNode
914 //purpose  : 
915 //=======================================================================
916 void StdMeshers_Penta_3D::CreateNode(const bool bIsUpperLayer,
917                                      const gp_XYZ& aParams,
918                                      StdMeshers_TNode& aTN)
919 {
920   double aX, aY, aZ;
921   //
922   gp_Pnt aP;
923   //
924   SMDS_MeshNode* pNode=NULL; 
925   aTN.SetNode(pNode);  
926   //
927   //   if (bIsUpperLayer) {
928   //     // point on face Fxy1
929   //     const TopoDS_Shape& aS=myBlock.Shape(SMESH_Block::ID_Fxy1);
930   //     myBlock.Point(aParams, aS, aP);
931   //   }
932   //   else {
933   //     // point inside solid
934   //     myBlock.Point(aParams, aP);
935   //   }
936   if (bIsUpperLayer) {
937     double u = aParams.X(), v = aParams.Y();
938     double u1 = ( 1. - u ), v1 = ( 1. - v );
939     aP.ChangeCoord()  = myShapeXYZ[ SMESH_Block::ID_Ex01 ] * v1;
940     aP.ChangeCoord() += myShapeXYZ[ SMESH_Block::ID_Ex11 ] * v;
941     aP.ChangeCoord() += myShapeXYZ[ SMESH_Block::ID_E0y1 ] * u1;
942     aP.ChangeCoord() += myShapeXYZ[ SMESH_Block::ID_E1y1 ] * u;
943
944     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V001 ] * u1 * v1;
945     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V101 ] * u  * v1;
946     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V011 ] * u1 * v;
947     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V111 ] * u  * v;
948   }
949   else {
950     SMESH_Block::ShellPoint( aParams, myShapeXYZ, aP.ChangeCoord() );
951   }
952   //
953   //   iErr=myBlock.ErrorStatus();
954   //   if (iErr) {
955   //     myErrorStatus=12; // can not find the node point;
956   //     return;
957   //   }
958   //
959   aX=aP.X(); aY=aP.Y(); aZ=aP.Z(); 
960   //
961   SMESH_Mesh* pMesh = GetMesh();
962   SMESHDS_Mesh* pMeshDS = pMesh->GetMeshDS();
963   //
964   pNode = pMeshDS->AddNode(aX, aY, aZ);
965
966   aTN.SetNode(pNode);
967 }
968
969 //=======================================================================
970 //function : ShapeSupportID
971 //purpose  : 
972 //=======================================================================
973 void StdMeshers_Penta_3D::ShapeSupportID(const bool bIsUpperLayer,
974                                          const SMESH_Block::TShapeID aBNSSID,
975                                          SMESH_Block::TShapeID& aSSID)
976 {
977   switch (aBNSSID) {
978   case SMESH_Block::ID_V000:
979     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V001 : SMESH_Block::ID_E00z;
980     break;
981   case SMESH_Block::ID_V100:
982     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V101 : SMESH_Block::ID_E10z;
983     break; 
984   case SMESH_Block::ID_V110:
985     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V111 : SMESH_Block::ID_E11z;
986     break;
987   case SMESH_Block::ID_V010:
988     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V011 : SMESH_Block::ID_E01z;
989     break;
990   case SMESH_Block::ID_Ex00:
991     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_Ex01 : SMESH_Block::ID_Fx0z;
992     break;
993   case SMESH_Block::ID_Ex10:
994     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_Ex11 : SMESH_Block::ID_Fx1z;
995     break; 
996   case SMESH_Block::ID_E0y0:
997     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_E0y1 : SMESH_Block::ID_F0yz;
998     break; 
999   case SMESH_Block::ID_E1y0:
1000     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_E1y1 : SMESH_Block::ID_F1yz;
1001     break; 
1002   case SMESH_Block::ID_Fxy0:
1003     aSSID=SMESH_Block::ID_NONE;//(bIsUpperLayer) ?  Shape_ID_Fxy1 : Shape_ID_NONE;
1004     break;   
1005   default:
1006     aSSID=SMESH_Block::ID_NONE;
1007     myErrorStatus->myName=10; // Can not find supporting shape ID
1008     myErrorStatus->myComment = "Internal error of StdMeshers_Penta_3D";
1009     break;
1010   }
1011   return;
1012 }
1013 //=======================================================================
1014 //function : MakeBlock
1015 //purpose  : 
1016 //=======================================================================
1017 void StdMeshers_Penta_3D::MakeBlock()
1018 {
1019   bool bFound;
1020   int i, j, iNbEV, iNbE, iErr, iCnt, iNbNodes, iNbF;
1021   //
1022   TopoDS_Vertex aV000, aV001;
1023   TopoDS_Shape aFTr;
1024   TopTools_IndexedDataMapOfShapeListOfShape aMVES;
1025   TopTools_IndexedMapOfShape aME ,aMEV, aM;
1026   TopTools_ListIteratorOfListOfShape aIt;
1027   //
1028   TopExp::MapShapes(myShape, TopAbs_FACE, aM);
1029   //
1030   // 0. Find triangulated face aFTr
1031   SMDSAbs_ElementType aElementType;
1032   SMESH_Mesh* pMesh=GetMesh();
1033   //
1034   iCnt = 0;
1035   iNbF = aM.Extent();
1036   for (i=1; i<=iNbF; ++i) {
1037     const TopoDS_Shape& aF = aM(i);
1038     SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aF);
1039     ASSERT(aSubMesh);
1040     SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
1041     SMDS_ElemIteratorPtr itf = aSM->GetElements();
1042     while(itf->more()) {
1043       const SMDS_MeshElement * pElement = itf->next();
1044       aElementType = pElement->GetType();
1045       if (aElementType==SMDSAbs_Face) {
1046         iNbNodes = pElement->NbNodes();
1047         if ( iNbNodes==3 || (pElement->IsQuadratic() && iNbNodes==6) ) {
1048           aFTr = aF;
1049           ++iCnt;
1050           if (iCnt>1) {
1051             // \begin{E.A.}
1052             // The current algorithm fails if there is more that one
1053             // face wich contains triangles ...
1054             // In that case, replace return by break to try another
1055             // method (coded in "if (iCnt != 1) { ... }")
1056             //
1057             // MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1058             // myErrorStatus=5; // more than one face has triangulation
1059             // return;
1060             break;
1061             // \end{E.A.}
1062           }
1063           break; // next face
1064         }
1065       }
1066     }
1067   }
1068   //
1069   // \begin{E.A.}
1070   // The current algorithm fails if "iCnt != 1", the case "iCnt == 0"
1071   // was not reached 'cause it was not called from Hexa_3D ... Now it
1072   // can occurs and in my opinion, it is the most common case.
1073   //
1074   if (iCnt != 1) {
1075     // The suggested algorithm is the following :
1076     //
1077     // o Check that nb_of_faces == 6 and nb_of_edges == 12
1078     //   then the shape is tologically equivalent to a box
1079     // o In a box, there are three set of four // edges ...
1080     //   In the cascade notation, it seems to be the edges
1081     //   numbered : 
1082     //     - 1, 3, 5, 7
1083     //     - 2, 4, 6, 8
1084     //     - 9, 10, 11, 12
1085     // o For each one of this set, check if the four edges
1086     //   have the same number of element.
1087     // o If so, check if the "corresponding" // faces contains
1088     //   only quads. It's the faces numbered:
1089     //     - 1, 2, 3, 4
1090     //     - 1, 2, 5, 6
1091     //     - 3, 4, 5, 6
1092     // o If so, check if the opposite edges of each // faces
1093     //   have the same number of elements. It is the edges
1094     //   numbered :
1095     //     - 2 and 4, 6 and 8, 9 and 10, 11 and 12
1096     //     - 1 and 3, 5 and 7, 9 and 11, 10 and 12
1097     //     - 1 and 5, 3 and 7, 4 and 8, 2 and 6
1098     // o If so, check if the two other faces have the same
1099     //   number of elements. It is the faces numbered:
1100     //     - 5, 6
1101     //     - 3, 4
1102     //     - 1, 2
1103     //   This test should be improved to test if the nodes
1104     //   of the two faces are really "en face".
1105     // o If so, one of the two faces is a candidate to an extrusion,
1106     //   It is the faces numbered :
1107     //     - 5
1108     //     - 3
1109     //     - 1
1110     // o Finally, if there is only one candidate, let do the
1111     //   extrusion job for the corresponding face
1112     //
1113     int isOK = 0;
1114     //
1115     int iNbF = aM.Extent();
1116     if (iNbF == 6) {
1117       //
1118       int nb_f1 = pMesh->GetSubMeshContaining(aM(1))->GetSubMeshDS()->NbElements();
1119       int nb_f2 = pMesh->GetSubMeshContaining(aM(2))->GetSubMeshDS()->NbElements();
1120       int nb_f3 = pMesh->GetSubMeshContaining(aM(3))->GetSubMeshDS()->NbElements();
1121       int nb_f4 = pMesh->GetSubMeshContaining(aM(4))->GetSubMeshDS()->NbElements();
1122       int nb_f5 = pMesh->GetSubMeshContaining(aM(5))->GetSubMeshDS()->NbElements();
1123       int nb_f6 = pMesh->GetSubMeshContaining(aM(6))->GetSubMeshDS()->NbElements();
1124       //
1125       int has_only_quad_f1 = 1;
1126       int has_only_quad_f2 = 1;
1127       int has_only_quad_f3 = 1;
1128       int has_only_quad_f4 = 1;
1129       int has_only_quad_f5 = 1;
1130       int has_only_quad_f6 = 1;
1131       //
1132       for (i=1; i<=iNbF; ++i) {
1133         int ok = 1;
1134         const TopoDS_Shape& aF = aM(i);
1135         SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aF);
1136         SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
1137         SMDS_ElemIteratorPtr itf = aSM->GetElements();
1138         while(itf->more()) {
1139           const SMDS_MeshElement * pElement = itf->next();
1140           aElementType = pElement->GetType();
1141           if (aElementType==SMDSAbs_Face) {
1142             iNbNodes = pElement->NbNodes();
1143             if ( iNbNodes!=4 ) {
1144               ok = 0;
1145               break ;
1146             }
1147           }
1148         }
1149         if (i==1) has_only_quad_f1 = ok ;
1150         if (i==2) has_only_quad_f2 = ok ;
1151         if (i==3) has_only_quad_f3 = ok ;
1152         if (i==4) has_only_quad_f4 = ok ;
1153         if (i==5) has_only_quad_f5 = ok ;
1154         if (i==6) has_only_quad_f6 = ok ;
1155       }
1156       //
1157       TopTools_IndexedMapOfShape aE;
1158       TopExp::MapShapes(myShape, TopAbs_EDGE, aE);
1159       int iNbE = aE.Extent();
1160       if (iNbE == 12) {
1161         //
1162         int nb_e01 = pMesh->GetSubMeshContaining(aE(1))->GetSubMeshDS()->NbElements();
1163         int nb_e02 = pMesh->GetSubMeshContaining(aE(2))->GetSubMeshDS()->NbElements();
1164         int nb_e03 = pMesh->GetSubMeshContaining(aE(3))->GetSubMeshDS()->NbElements();
1165         int nb_e04 = pMesh->GetSubMeshContaining(aE(4))->GetSubMeshDS()->NbElements();
1166         int nb_e05 = pMesh->GetSubMeshContaining(aE(5))->GetSubMeshDS()->NbElements();
1167         int nb_e06 = pMesh->GetSubMeshContaining(aE(6))->GetSubMeshDS()->NbElements();
1168         int nb_e07 = pMesh->GetSubMeshContaining(aE(7))->GetSubMeshDS()->NbElements();
1169         int nb_e08 = pMesh->GetSubMeshContaining(aE(8))->GetSubMeshDS()->NbElements();
1170         int nb_e09 = pMesh->GetSubMeshContaining(aE(9))->GetSubMeshDS()->NbElements();
1171         int nb_e10 = pMesh->GetSubMeshContaining(aE(10))->GetSubMeshDS()->NbElements();
1172         int nb_e11 = pMesh->GetSubMeshContaining(aE(11))->GetSubMeshDS()->NbElements();
1173         int nb_e12 = pMesh->GetSubMeshContaining(aE(12))->GetSubMeshDS()->NbElements();
1174         //
1175         int nb_ok = 0 ;
1176         //
1177         if ( (nb_e01==nb_e03) && (nb_e03==nb_e05) && (nb_e05==nb_e07) ) {
1178           if ( has_only_quad_f1 && has_only_quad_f2 && has_only_quad_f3 && has_only_quad_f4 ) {
1179             if ( (nb_e09==nb_e10) && (nb_e08==nb_e06) && (nb_e11==nb_e12) && (nb_e04==nb_e02) ) {
1180               if (nb_f5==nb_f6) {
1181                 nb_ok += 1;
1182                 aFTr = aM(5);
1183               }
1184             }
1185           }
1186         }
1187         if ( (nb_e02==nb_e04) && (nb_e04==nb_e06) && (nb_e06==nb_e08) ) {
1188           if ( has_only_quad_f1 && has_only_quad_f2 && has_only_quad_f5 && has_only_quad_f6 ) {
1189             if ( (nb_e01==nb_e03) && (nb_e10==nb_e12) && (nb_e05==nb_e07) && (nb_e09==nb_e11) ) {
1190               if (nb_f3==nb_f4) {
1191                 nb_ok += 1;
1192                 aFTr = aM(3);
1193               }
1194             }
1195           }
1196         }
1197         if ( (nb_e09==nb_e10) && (nb_e10==nb_e11) && (nb_e11==nb_e12) ) {
1198           if ( has_only_quad_f3 && has_only_quad_f4 && has_only_quad_f5 && has_only_quad_f6 ) {
1199             if ( (nb_e01==nb_e05) && (nb_e02==nb_e06) && (nb_e03==nb_e07) && (nb_e04==nb_e08) ) {
1200               if (nb_f1==nb_f2) {
1201                 nb_ok += 1;
1202                 aFTr = aM(1);
1203               }
1204             }
1205           }
1206         }
1207         //
1208         if ( nb_ok == 1 ) {
1209           isOK = 1;
1210         }
1211         //
1212       }
1213     }
1214     if (!isOK) {
1215       myErrorStatus->myName=5; // more than one face has triangulation
1216       myErrorStatus->myComment="Incorrect input mesh";
1217       return;
1218     }
1219   }
1220   // \end{E.A.}
1221   // 
1222   // 1. Vetrices V00, V001;
1223   //
1224   TopExp::MapShapes(aFTr, TopAbs_EDGE, aME);
1225   TopExp::MapShapesAndAncestors(myShape, TopAbs_VERTEX, TopAbs_EDGE, aMVES);
1226   //
1227   // 1.1 Base vertex V000
1228   iNbE = aME.Extent();
1229   if (iNbE!= NB_WALL_FACES ){
1230     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1231     myErrorStatus->myName=7; // too few edges are in base face aFTr
1232     myErrorStatus->myComment=SMESH_Comment("Not a quadrilateral face #")
1233       <<pMesh->GetMeshDS()->ShapeToIndex( aFTr )<<": "<<iNbE<<" edges" ;
1234     return;
1235   }
1236   const TopoDS_Edge& aE1=TopoDS::Edge(aME(1));
1237   aV000=TopExp::FirstVertex(aE1);
1238   //
1239   const TopTools_ListOfShape& aLE=aMVES.FindFromKey(aV000);
1240   aIt.Initialize(aLE);
1241   for (; aIt.More(); aIt.Next()) {
1242     const TopoDS_Shape& aEx=aIt.Value();
1243     aMEV.Add(aEx);
1244   }
1245   iNbEV=aMEV.Extent();
1246   if (iNbEV!=3){
1247     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1248     myErrorStatus->myName=7; // too few edges meet in base vertex 
1249     myErrorStatus->myComment=SMESH_Comment("3 edges must share vertex #")
1250       <<pMesh->GetMeshDS()->ShapeToIndex( aV000 )<<" but there are "<<iNbEV<<" edges";
1251     return;
1252   }
1253   //
1254   // 1.2 Vertex V001
1255   bFound=false;
1256   for (j=1; j<=iNbEV; ++j) {
1257     const TopoDS_Shape& aEx=aMEV(j);
1258     if (!aME.Contains(aEx)) {
1259       TopoDS_Vertex aV[2];
1260       //
1261       const TopoDS_Edge& aE=TopoDS::Edge(aEx);
1262       TopExp::Vertices(aE, aV[0], aV[1]);
1263       for (i=0; i<2; ++i) {
1264         if (!aV[i].IsSame(aV000)) {
1265           aV001=aV[i];
1266           bFound=!bFound;
1267           break;
1268         }
1269       }
1270     }
1271   }
1272   //
1273   if (!bFound) {
1274     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1275     myErrorStatus->myName=8; // can not find reper V001
1276     myErrorStatus->myComment=SMESH_Comment("Can't find opposite vertex for vertex #")
1277       <<pMesh->GetMeshDS()->ShapeToIndex( aV000 );
1278     return;
1279   }
1280   //DEB
1281   //gp_Pnt aP000, aP001;
1282   //
1283   //aP000=BRep_Tool::Pnt(TopoDS::Vertex(aV000));
1284   //printf("*** aP000 { %lf, %lf, %lf }\n", aP000.X(), aP000.Y(), aP000.Z());
1285   //aP001=BRep_Tool::Pnt(TopoDS::Vertex(aV001));
1286   //printf("*** aP001 { %lf, %lf, %lf }\n", aP001.X(), aP001.Y(), aP001.Z());
1287   //DEB
1288   //
1289   aME.Clear();
1290   TopExp::MapShapes(myShape, TopAbs_SHELL, aME);
1291   iNbE=aME.Extent();
1292   if (iNbE!=1) {
1293     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1294     myErrorStatus->myName=9; // number of shells in source shape !=1
1295     myErrorStatus->myComment=SMESH_Comment("Unexpected nb of shells ")<<iNbE;
1296     return;
1297   }
1298   //
1299   // 2. Load Block
1300   const TopoDS_Shell& aShell=TopoDS::Shell(aME(1));
1301   myBlock.Load(aShell, aV000, aV001);
1302   iErr = myBlock.ErrorStatus();
1303   if (iErr) {
1304     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1305     myErrorStatus=myBlock.GetError(); // SMESHBlock: Load operation failed
1306     return;
1307   }
1308 }
1309 //=======================================================================
1310 //function : CheckData
1311 //purpose  : 
1312 //=======================================================================
1313 void StdMeshers_Penta_3D::CheckData()
1314 {
1315   int i, iNb;
1316   int iNbEx[]={8, 12, 6};
1317   //
1318   TopAbs_ShapeEnum aST;
1319   TopAbs_ShapeEnum aSTEx[]={
1320     TopAbs_VERTEX, TopAbs_EDGE, TopAbs_FACE
1321   }; 
1322   TopTools_IndexedMapOfShape aM;
1323   //
1324   if (myShape.IsNull()){
1325     MESSAGE("StdMeshers_Penta_3D::CheckData() ");
1326     myErrorStatus->myName=2; // null shape
1327     myErrorStatus->myComment="Null shape";
1328     return;
1329   }
1330   //
1331   aST=myShape.ShapeType();
1332   if (!(aST==TopAbs_SOLID || aST==TopAbs_SHELL)) {
1333     MESSAGE("StdMeshers_Penta_3D::CheckData() ");
1334     myErrorStatus->myName=3; // not compatible type of shape
1335     myErrorStatus->myComment=SMESH_Comment("Wrong shape type (TopAbs_ShapeEnum) ")<<aST;
1336     return;
1337   }
1338   //
1339   for (i=0; i<3; ++i) {
1340     aM.Clear();
1341     TopExp::MapShapes(myShape, aSTEx[i], aM);
1342     iNb=aM.Extent();
1343     if (iNb!=iNbEx[i]){
1344       MESSAGE("StdMeshers_Penta_3D::CheckData() ");
1345       myErrorStatus->myName=4; // number of subshape is not compatible
1346       myErrorStatus->myComment="Wrong number of subshapes of a block";
1347       return;
1348     }
1349   }
1350 }
1351
1352 //=======================================================================
1353 //function : LoadIJNodes
1354 //purpose  : Load nodes bound to theFace into column (vectors) and rows
1355 //           of theIJNodes.
1356 //           The value of theIJNodes map is a vector of ordered nodes so
1357 //           that the 0-the one lies on theBaseEdge.
1358 //           The key of theIJNodes map is a normalized parameter of each
1359 //           0-the node on theBaseEdge.
1360 //=======================================================================
1361
1362 bool StdMeshers_Penta_3D::LoadIJNodes(StdMeshers_IJNodeMap & theIJNodes,
1363                                       const TopoDS_Face&     theFace,
1364                                       const TopoDS_Edge&     theBaseEdge,
1365                                       SMESHDS_Mesh*          theMesh)
1366 {
1367   // get vertices of theBaseEdge
1368   TopoDS_Vertex vfb, vlb, vft; // first and last, bottom and top vertices
1369   TopoDS_Edge eFrw = TopoDS::Edge( theBaseEdge.Oriented( TopAbs_FORWARD ));
1370   TopExp::Vertices( eFrw, vfb, vlb );
1371
1372   // find the other edges of theFace and orientation of e1
1373   TopoDS_Edge e1, e2, eTop;
1374   bool rev1, CumOri = false;
1375   TopExp_Explorer exp( theFace, TopAbs_EDGE );
1376   int nbEdges = 0;
1377   for ( ; exp.More(); exp.Next() ) {
1378     if ( ++nbEdges > NB_WALL_FACES ) {
1379       return false; // more than 4 edges in theFace
1380     }
1381     TopoDS_Edge e = TopoDS::Edge( exp.Current() );
1382     if ( theBaseEdge.IsSame( e ))
1383       continue;
1384     TopoDS_Vertex vCommon;
1385     if ( !TopExp::CommonVertex( theBaseEdge, e, vCommon ))
1386       eTop = e;
1387     else if ( vCommon.IsSame( vfb )) {
1388       e1 = e;
1389       vft = TopExp::LastVertex( e1, CumOri );
1390       rev1 = vfb.IsSame( vft );
1391       if ( rev1 )
1392         vft = TopExp::FirstVertex( e1, CumOri );
1393     }
1394     else
1395       e2 = e;
1396   }
1397   if ( nbEdges < NB_WALL_FACES ) {
1398     return false; // less than 4 edges in theFace
1399   }
1400
1401   // submeshes corresponding to shapes
1402   SMESHDS_SubMesh* smFace = theMesh->MeshElements( theFace );
1403   SMESHDS_SubMesh* smb = theMesh->MeshElements( theBaseEdge );
1404   SMESHDS_SubMesh* smt = theMesh->MeshElements( eTop );
1405   SMESHDS_SubMesh* sm1 = theMesh->MeshElements( e1 );
1406   SMESHDS_SubMesh* sm2 = theMesh->MeshElements( e2 );
1407   SMESHDS_SubMesh* smVfb = theMesh->MeshElements( vfb );
1408   SMESHDS_SubMesh* smVlb = theMesh->MeshElements( vlb );
1409   SMESHDS_SubMesh* smVft = theMesh->MeshElements( vft );
1410   if (!smFace || !smb || !smt || !sm1 || !sm2 || !smVfb || !smVlb || !smVft ) {
1411     MESSAGE( "NULL submesh " <<smFace<<" "<<smb<<" "<<smt<<" "<<
1412              sm1<<" "<<sm2<<" "<<smVfb<<" "<<smVlb<<" "<<smVft);
1413     return false;
1414   }
1415   if ( smb->NbNodes() != smt->NbNodes() || sm1->NbNodes() != sm2->NbNodes() ) {
1416     MESSAGE(" Diff nb of nodes on opposite edges" );
1417     return false;
1418   }
1419   if (smVfb->NbNodes() != 1 || smVlb->NbNodes() != 1 || smVft->NbNodes() != 1) {
1420     MESSAGE("Empty submesh of vertex");
1421     return false;
1422   }
1423   if ( sm1->NbNodes() * smb->NbNodes() != smFace->NbNodes() ) {
1424     // check quadratic case
1425     if ( myCreateQuadratic ) {
1426       int n1 = sm1->NbNodes()/2;
1427       int n2 = smb->NbNodes()/2;
1428       int n3 = sm1->NbNodes() - n1;
1429       int n4 = smb->NbNodes() - n2;
1430       int nf = sm1->NbNodes()*smb->NbNodes() - n3*n4;
1431       if( nf != smFace->NbNodes() ) {
1432         MESSAGE( "Wrong nb face nodes: " <<
1433                  sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
1434         return false;
1435       }
1436     }
1437     else {
1438       MESSAGE( "Wrong nb face nodes: " <<
1439                sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
1440       return false;
1441     }
1442   }
1443   // IJ size
1444   int vsize = sm1->NbNodes() + 2;
1445   int hsize = smb->NbNodes() + 2;
1446   if(myCreateQuadratic) {
1447     vsize = vsize - sm1->NbNodes()/2 -1;
1448     hsize = hsize - smb->NbNodes()/2 -1;
1449   }
1450
1451   // load nodes from theBaseEdge
1452
1453   set<const SMDS_MeshNode*> loadedNodes;
1454   const SMDS_MeshNode* nullNode = 0;
1455
1456   vector<const SMDS_MeshNode*> & nVecf = theIJNodes[ 0.];
1457   nVecf.resize( vsize, nullNode );
1458   loadedNodes.insert( nVecf[ 0 ] = smVfb->GetNodes()->next() );
1459
1460   vector<const SMDS_MeshNode*> & nVecl = theIJNodes[ 1.];
1461   nVecl.resize( vsize, nullNode );
1462   loadedNodes.insert( nVecl[ 0 ] = smVlb->GetNodes()->next() );
1463
1464   double f, l;
1465   BRep_Tool::Range( eFrw, f, l );
1466   double range = l - f;
1467   SMDS_NodeIteratorPtr nIt = smb->GetNodes();
1468   const SMDS_MeshNode* node;
1469   while ( nIt->more() ) {
1470     node = nIt->next();
1471     if(myTool->IsMedium(node))
1472       continue;
1473     const SMDS_EdgePosition* pos =
1474       dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
1475     if ( !pos ) {
1476       return false;
1477     }
1478     double u = ( pos->GetUParameter() - f ) / range;
1479     vector<const SMDS_MeshNode*> & nVec = theIJNodes[ u ];
1480     nVec.resize( vsize, nullNode );
1481     loadedNodes.insert( nVec[ 0 ] = node );
1482   }
1483   if ( theIJNodes.size() != hsize ) {
1484     MESSAGE( "Wrong node positions on theBaseEdge" );
1485     return false;
1486   }
1487
1488   // load nodes from e1
1489
1490   map< double, const SMDS_MeshNode*> sortedNodes; // sort by param on edge
1491   nIt = sm1->GetNodes();
1492   while ( nIt->more() ) {
1493     node = nIt->next();
1494     if(myTool->IsMedium(node))
1495       continue;
1496     const SMDS_EdgePosition* pos =
1497       dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
1498     if ( !pos ) {
1499       return false;
1500     }
1501     sortedNodes.insert( make_pair( pos->GetUParameter(), node ));
1502   }
1503   loadedNodes.insert( nVecf[ vsize - 1 ] = smVft->GetNodes()->next() );
1504   map< double, const SMDS_MeshNode*>::iterator u_n = sortedNodes.begin();
1505   int row = rev1 ? vsize - 1 : 0;
1506   for ( ; u_n != sortedNodes.end(); u_n++ ) {
1507     if ( rev1 ) row--;
1508     else        row++;
1509     loadedNodes.insert( nVecf[ row ] = u_n->second );
1510   }
1511
1512   // try to load the rest nodes
1513
1514   // get all faces from theFace
1515   TIDSortedElemSet allFaces, foundFaces;
1516   SMDS_ElemIteratorPtr eIt = smFace->GetElements();
1517   while ( eIt->more() ) {
1518     const SMDS_MeshElement* e = eIt->next();
1519     if ( e->GetType() == SMDSAbs_Face )
1520       allFaces.insert( e );
1521   }
1522   // Starting from 2 neighbour nodes on theBaseEdge, look for a face
1523   // the nodes belong to, and between the nodes of the found face,
1524   // look for a not loaded node considering this node to be the next
1525   // in a column of the starting second node. Repeat, starting
1526   // from nodes next to the previous starting nodes in their columns,
1527   // and so on while a face can be found. Then go the the next pair
1528   // of nodes on theBaseEdge.
1529   StdMeshers_IJNodeMap::iterator par_nVec_1 = theIJNodes.begin();
1530   StdMeshers_IJNodeMap::iterator par_nVec_2 = par_nVec_1;
1531   // loop on columns
1532   int col = 0;
1533   for ( par_nVec_2++; par_nVec_2 != theIJNodes.end(); par_nVec_1++, par_nVec_2++ ) {
1534     col++;
1535     row = 0;
1536     const SMDS_MeshNode* n1 = par_nVec_1->second[ row ];
1537     const SMDS_MeshNode* n2 = par_nVec_2->second[ row ];
1538     const SMDS_MeshElement* face = 0;
1539     do {
1540       // look for a face by 2 nodes
1541       face = SMESH_MeshEditor::FindFaceInSet( n1, n2, allFaces, foundFaces );
1542       if ( face ) {
1543         int nbFaceNodes = face->NbNodes();
1544         if ( (!myCreateQuadratic && nbFaceNodes>4) ||
1545              (myCreateQuadratic && nbFaceNodes>8) ) {
1546           MESSAGE(" Too many nodes in a face: " << nbFaceNodes );
1547           return false;
1548         }
1549         // look for a not loaded node of the <face>
1550         bool found = false;
1551         const SMDS_MeshNode* n3 = 0; // a node defferent from n1 and n2
1552         eIt = face->nodesIterator() ;
1553         while ( !found && eIt->more() ) {
1554           node = static_cast<const SMDS_MeshNode*>( eIt->next() );
1555           if(myTool->IsMedium(node))
1556             continue;
1557           found = loadedNodes.insert( node ).second;
1558           if ( !found && node != n1 && node != n2 )
1559             n3 = node;
1560         }
1561         if ( found ) {
1562           if ( ++row > vsize - 1 ) {
1563             MESSAGE( "Too many nodes in column "<< col <<": "<< row+1);
1564             return false;
1565           }
1566           par_nVec_2->second[ row ] = node;
1567           foundFaces.insert( face );
1568           n2 = node;
1569           if ( nbFaceNodes==4 || (myCreateQuadratic && nbFaceNodes==8) ) {
1570             n1 = par_nVec_1->second[ row ];
1571           }
1572         }
1573         else if ( (nbFaceNodes==3 || (myCreateQuadratic && nbFaceNodes==6) )  &&
1574                   n3 == par_nVec_1->second[ row ] ) {
1575           n1 = n3;
1576         }
1577         else {
1578           MESSAGE( "Not quad mesh, column "<< col );
1579           return false;
1580         }
1581       }
1582     }
1583     while ( face && n1 && n2 );
1584
1585     if ( row < vsize - 1 ) {
1586       MESSAGE( "Too few nodes in column "<< col <<": "<< row+1);
1587       MESSAGE( "Base node 1: "<< par_nVec_1->second[0]);
1588       MESSAGE( "Base node 2: "<< par_nVec_2->second[0]);
1589       MESSAGE( "Current node 1: "<< n1);
1590       MESSAGE( "Current node 2: "<< n2);
1591       MESSAGE( "first base node: "<< theIJNodes.begin()->second[0]);
1592       MESSAGE( "last base node: "<< theIJNodes.rbegin()->second[0]);
1593       return false;
1594     }
1595   } // loop on columns
1596
1597   return true;
1598 }
1599
1600 //////////////////////////////////////////////////////////////////////////
1601 //
1602 //   StdMeshers_SMESHBlock
1603 //
1604 //////////////////////////////////////////////////////////////////////////
1605
1606 //=======================================================================
1607 //function : StdMeshers_SMESHBlock
1608 //purpose  : 
1609 //=======================================================================
1610 StdMeshers_SMESHBlock::StdMeshers_SMESHBlock()
1611 {
1612   myErrorStatus=1;
1613   myIsEdgeForward.resize( SMESH_Block::NbEdges(), -1 );
1614 }
1615
1616 //=======================================================================
1617 //function : IsForwadEdge
1618 //purpose  : 
1619 //=======================================================================
1620
1621 bool StdMeshers_SMESHBlock::IsForwadEdge(const int theEdgeID)
1622 {
1623   int index = myTBlock.ShapeIndex( theEdgeID );
1624   if ( !myTBlock.IsEdgeID( theEdgeID ))
1625     return false;
1626
1627   if ( myIsEdgeForward[ index ] < 0 )
1628     myIsEdgeForward[ index ] =
1629       myTBlock.IsForwardEdge( TopoDS::Edge( Shape( theEdgeID )), myShapeIDMap );
1630
1631   return myIsEdgeForward[ index ];
1632 }
1633
1634 //=======================================================================
1635 //function : ErrorStatus
1636 //purpose  : 
1637 //=======================================================================
1638 int StdMeshers_SMESHBlock::ErrorStatus() const
1639 {
1640   return myErrorStatus;
1641 }
1642
1643 //================================================================================
1644 /*!
1645  * \brief Return problem description
1646  */
1647 //================================================================================
1648
1649 SMESH_ComputeErrorPtr StdMeshers_SMESHBlock::GetError() const
1650 {
1651   SMESH_ComputeErrorPtr err = SMESH_ComputeError::New();
1652   string & text = err->myComment;
1653   switch ( myErrorStatus ) {
1654   case 2:
1655   case 3: text = "Internal error of StdMeshers_Penta_3D"; break; 
1656   case 4: text = "Can't compute normalized parameters of a point inside a block"; break;
1657   case 5: text = "Can't compute coordinates by normalized parameters inside a block"; break;
1658   case 6: text = "Can't detect block subshapes. Not a block?"; break;
1659   }
1660   if (!text.empty())
1661     err->myName = myErrorStatus;
1662   return err;
1663 }
1664
1665 //=======================================================================
1666 //function : Load
1667 //purpose  : 
1668 //=======================================================================
1669 void StdMeshers_SMESHBlock::Load(const TopoDS_Shell& theShell)
1670 {
1671   TopoDS_Vertex aV000, aV001;
1672   //
1673   Load(theShell, aV000, aV001);
1674 }
1675
1676 //=======================================================================
1677 //function : Load
1678 //purpose  : 
1679 //=======================================================================
1680 void StdMeshers_SMESHBlock::Load(const TopoDS_Shell& theShell,
1681                                  const TopoDS_Vertex& theV000,
1682                                  const TopoDS_Vertex& theV001)
1683 {
1684   myErrorStatus=0;
1685   //
1686   myShell=theShell;
1687   //
1688   bool bOk;
1689   //
1690   myShapeIDMap.Clear();  
1691   bOk = myTBlock.LoadBlockShapes(myShell, theV000, theV001, myShapeIDMap);
1692   if (!bOk) {
1693     myErrorStatus=6;
1694     return;
1695   }
1696 }
1697
1698 //=======================================================================
1699 //function : ComputeParameters
1700 //purpose  : 
1701 //=======================================================================
1702 void StdMeshers_SMESHBlock::ComputeParameters(const gp_Pnt& thePnt, 
1703                                               gp_XYZ& theXYZ)
1704 {
1705   ComputeParameters(thePnt, myShell, theXYZ);
1706 }
1707
1708 //=======================================================================
1709 //function : ComputeParameters
1710 //purpose  : 
1711 //=======================================================================
1712 void StdMeshers_SMESHBlock::ComputeParameters(const gp_Pnt& thePnt,
1713                                               const TopoDS_Shape& theShape,
1714                                               gp_XYZ& theXYZ)
1715 {
1716   myErrorStatus=0;
1717   //
1718   int aID;
1719   bool bOk;
1720   //
1721   aID = ShapeID(theShape);
1722   if (myErrorStatus) {
1723     return;
1724   }
1725   bOk = myTBlock.ComputeParameters(thePnt, theXYZ, aID);
1726   if (!bOk) {
1727     myErrorStatus=4; // problems with computation Parameters 
1728     return;
1729   }
1730 }
1731
1732 //=======================================================================
1733 //function : ComputeParameters
1734 //purpose  : 
1735 //=======================================================================
1736
1737 void StdMeshers_SMESHBlock::ComputeParameters(const double& theU,
1738                                               const TopoDS_Shape& theShape,
1739                                               gp_XYZ& theXYZ)
1740 {
1741   myErrorStatus=0;
1742   //
1743   int aID;
1744   bool bOk=false;
1745   //
1746   aID = ShapeID(theShape);
1747   if (myErrorStatus) {
1748     return;
1749   }
1750   if ( SMESH_Block::IsEdgeID( aID ))
1751     bOk = myTBlock.EdgeParameters( aID, theU, theXYZ );
1752   if (!bOk) {
1753     myErrorStatus=4; // problems with computation Parameters 
1754     return;
1755   }
1756 }
1757
1758 //=======================================================================
1759 //function : Point
1760 //purpose  : 
1761 //=======================================================================
1762 void StdMeshers_SMESHBlock::Point(const gp_XYZ& theParams, gp_Pnt& aP3D)
1763 {
1764   TopoDS_Shape aS;
1765   //
1766   Point(theParams, aS, aP3D);
1767 }
1768
1769 //=======================================================================
1770 //function : Point
1771 //purpose  : 
1772 //=======================================================================
1773 void StdMeshers_SMESHBlock::Point(const gp_XYZ& theParams,
1774                                   const TopoDS_Shape& theShape,
1775                                   gp_Pnt& aP3D)
1776 {
1777   myErrorStatus = 0;
1778   //
1779   int aID;
1780   bool bOk = false;
1781   gp_XYZ aXYZ(99.,99.,99.);
1782   aP3D.SetXYZ(aXYZ);
1783   //
1784   if (theShape.IsNull()) {
1785     bOk = myTBlock.ShellPoint(theParams, aXYZ);
1786   }
1787   //
1788   else {
1789     aID=ShapeID(theShape);
1790     if (myErrorStatus) {
1791       return;
1792     }
1793     //
1794     if (SMESH_Block::IsVertexID(aID)) {
1795       bOk = myTBlock.VertexPoint(aID, aXYZ);
1796     }
1797     else if (SMESH_Block::IsEdgeID(aID)) {
1798       bOk = myTBlock.EdgePoint(aID, theParams, aXYZ);
1799     }
1800     //
1801     else if (SMESH_Block::IsFaceID(aID)) {
1802       bOk = myTBlock.FacePoint(aID, theParams, aXYZ);
1803     }
1804   }
1805   if (!bOk) {
1806     myErrorStatus=5; // problems with point computation 
1807     return;
1808   }
1809   aP3D.SetXYZ(aXYZ);
1810 }
1811
1812 //=======================================================================
1813 //function : ShapeID
1814 //purpose  : 
1815 //=======================================================================
1816 int StdMeshers_SMESHBlock::ShapeID(const TopoDS_Shape& theShape)
1817 {
1818   myErrorStatus=0;
1819   //
1820   int aID=-1;
1821   TopoDS_Shape aSF, aSR;
1822   //
1823   aSF=theShape;
1824   aSF.Orientation(TopAbs_FORWARD);
1825   aSR=theShape;
1826   aSR.Orientation(TopAbs_REVERSED);
1827   //
1828   if (myShapeIDMap.Contains(aSF)) {
1829     aID=myShapeIDMap.FindIndex(aSF);
1830     return aID;
1831   }
1832   if (myShapeIDMap.Contains(aSR)) {
1833     aID=myShapeIDMap.FindIndex(aSR);
1834     return aID;
1835   }
1836   myErrorStatus=2; // unknown shape;
1837   return aID;
1838 }
1839
1840 //=======================================================================
1841 //function : Shape
1842 //purpose  : 
1843 //=======================================================================
1844 const TopoDS_Shape& StdMeshers_SMESHBlock::Shape(const int theID)
1845 {
1846   myErrorStatus=0;
1847   //
1848   int aNb;
1849   //
1850   aNb=myShapeIDMap.Extent();
1851   if (theID<1 || theID>aNb) {
1852     myErrorStatus=3; // ID is out of range
1853     return myEmptyShape;
1854   }
1855   //
1856   const TopoDS_Shape& aS=myShapeIDMap.FindKey(theID);
1857   return aS;
1858 }
1859
1860
1861 //=======================================================================
1862 //function : Evaluate
1863 //purpose  : 
1864 //=======================================================================
1865 bool StdMeshers_Penta_3D::Evaluate(SMESH_Mesh& aMesh, 
1866                                    const TopoDS_Shape& aShape,
1867                                    MapShapeNbElems& aResMap)
1868 {
1869   MESSAGE("StdMeshers_Penta_3D::Evaluate()");
1870
1871   // find face contains only triangles
1872   vector < SMESH_subMesh * >meshFaces;
1873   TopTools_SequenceOfShape aFaces;
1874   int NumBase = 0, i = 0;
1875   for (TopExp_Explorer exp(aShape, TopAbs_FACE); exp.More(); exp.Next()) {
1876     i++;
1877     aFaces.Append(exp.Current());
1878     SMESH_subMesh *aSubMesh = aMesh.GetSubMesh(exp.Current());
1879     meshFaces.push_back(aSubMesh);
1880     MapShapeNbElemsItr anIt = aResMap.find(meshFaces[i]);
1881     if( anIt == aResMap.end() ) {
1882       NumBase = 0;
1883       break;
1884     }
1885     std::vector<int> aVec = (*anIt).second;
1886     int nbtri = Max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
1887     int nbqua = Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1888     if( nbtri>0 && nbqua==0 ) {
1889       NumBase = i;
1890     }
1891   }
1892
1893   if(NumBase==0) {
1894     std::vector<int> aResVec(SMDSEntity_Last);
1895     for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1896     SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1897     aResMap.insert(std::make_pair(sm,aResVec));
1898     myErrorStatus->myName    = COMPERR_ALGO_FAILED;
1899     myErrorStatus->myComment = "Submesh can not be evaluated";
1900     return false;
1901   }
1902
1903   // find number of 1d elems for base face
1904   int nb1d = 0;
1905   TopTools_MapOfShape Edges1;
1906   for (TopExp_Explorer exp(aFaces.Value(NumBase), TopAbs_EDGE); exp.More(); exp.Next()) {
1907     Edges1.Add(exp.Current());
1908     SMESH_subMesh *sm = aMesh.GetSubMesh(exp.Current());
1909     if( sm ) {
1910       MapShapeNbElemsItr anIt = aResMap.find(sm);
1911       if( anIt == aResMap.end() ) continue;
1912       std::vector<int> aVec = (*anIt).second;
1913       nb1d += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
1914     }
1915   }
1916   // find face opposite to base face
1917   int OppNum = 0;
1918   for(i=1; i<=6; i++) {
1919     if(i==NumBase) continue;
1920     bool IsOpposite = true;
1921     for(TopExp_Explorer exp(aFaces.Value(i), TopAbs_EDGE); exp.More(); exp.Next()) {
1922       if( Edges1.Contains(exp.Current()) ) {
1923         IsOpposite = false;
1924         break;
1925       }
1926     }
1927     if(IsOpposite) {
1928       OppNum = i;
1929       break;
1930     }
1931   }
1932   // find number of 2d elems on side faces
1933   int nb2d = 0;
1934   for(i=1; i<=6; i++) {
1935     if( i==OppNum || i==NumBase ) continue;
1936     MapShapeNbElemsItr anIt = aResMap.find( meshFaces[i-1] );
1937     if( anIt == aResMap.end() ) continue;
1938     std::vector<int> aVec = (*anIt).second;
1939     nb2d += Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1940   }
1941
1942   MapShapeNbElemsItr anIt = aResMap.find( meshFaces[NumBase-1] );
1943   std::vector<int> aVec = (*anIt).second;
1944   int nb2d_face0 = Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1945   int nb0d_face0 = aVec[SMDSEntity_Node];
1946
1947   anIt = aResMap.find( meshFaces[OppNum-1] );
1948   for(i=SMDSEntity_Node; i<SMDSEntity_Last; i++)
1949     (*anIt).second[i] = aVec[i];
1950
1951   SMESH_MesherHelper aTool (aMesh);
1952   bool _quadraticMesh = aTool.IsQuadraticSubMesh(aShape);
1953
1954   std::vector<int> aResVec(SMDSEntity_Last);
1955   for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1956   if(_quadraticMesh) {
1957     aResVec[SMDSEntity_Quad_Penta] = nb2d_face0 * ( nb2d/nb1d );
1958     aResVec[SMDSEntity_Node] = nb0d_face0 * ( 2*nb2d/nb1d - 1 );
1959   }
1960   else {
1961     aResVec[SMDSEntity_Node] = nb0d_face0 * ( nb2d/nb1d - 1 );
1962     aResVec[SMDSEntity_Penta] = nb2d_face0 * ( nb2d/nb1d );
1963   }
1964   SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1965   aResMap.insert(std::make_pair(sm,aResVec));
1966
1967   return true;
1968 }
1969