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