Salome HOME
PAL16834 (smesh Prism don't work with NETGEN_2D algorithm)
[modules/smesh.git] / src / StdMeshers / StdMeshers_Penta_3D.cxx
1 //  SMESH StdMeshers_Penta_3D implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  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 //
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_MeshEditor.hxx"
39 #include "SMESH_subMesh.hxx"
40 #include "SMESH_subMeshEventListener.hxx"
41 #include "SMESH_Comment.hxx"
42
43 #include <BRepTools.hxx>
44 #include <BRepTools_WireExplorer.hxx>
45 #include <BRep_Tool.hxx>
46 #include <TopAbs_ShapeEnum.hxx>
47 #include <TopExp.hxx>
48 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <TopTools_IndexedMapOfShape.hxx>
51 #include <TopTools_ListIteratorOfListOfShape.hxx>
52 #include <TopTools_ListOfShape.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   SMESH_MesherHelper helper(aMesh);
112   myTool = &helper;
113   myCreateQuadratic = myTool->IsQuadraticSubMesh(aShape);
114
115   //
116   MakeBlock();
117   if (!myErrorStatus->IsOK()) {
118     return bOK;
119   }
120   //
121   ClearMeshOnFxy1();
122   if (!myErrorStatus->IsOK()) {
123     return bOK;
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 it 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   //DEB
482   /*
483   {
484     int iSSID, iBNID, aID;
485     //
486     for (i=0; i<myISize; ++i) {
487       printf(" Layer# %d\n", i);
488       for (j=0; j<myJSize; ++j) {
489         ij=i*myJSize+j; 
490         const StdMeshers_TNode& aTN=myTNodes[ij];
491         //const StdMeshers_TNode& aTN=aTNodes[ij];
492         const gp_XYZ& aXYZ=aTN.NormCoord();
493         iSSID=aTN.ShapeSupportID();
494         iBNID=aTN.BaseNodeID();
495         //
496         const SMDS_MeshNode* aNode=aTN.Node();
497         aID=aNode->GetID(); 
498         aX=aNode->X();
499         aY=aNode->Y();
500         aZ=aNode->Z();
501         printf("*** j:%d BNID#%d iSSID:%d ID:%d { %lf %lf %lf },  { %lf %lf %lf }\n",
502                j,  iBNID, iSSID, aID, aXYZ.X(),  aXYZ.Y(), aXYZ.Z(), aX, aY, aZ);
503       }
504     }
505   }
506   */
507   //DEB t
508 }
509
510
511 //=======================================================================
512 //function : FindNodeOnShape
513 //purpose  : 
514 //=======================================================================
515
516 void StdMeshers_Penta_3D::FindNodeOnShape(const TopoDS_Shape& aS,
517                                           const gp_XYZ&       aParams,
518                                           const int           z,
519                                           StdMeshers_TNode&   aTN)
520 {
521   double aX, aY, aZ, aD, aTol2, minD;
522   gp_Pnt aP1, aP2;
523   //
524   SMESH_Mesh* pMesh = GetMesh();
525   aTol2 = myTol3D*myTol3D;
526   minD = 1.e100;
527   SMDS_MeshNode* pNode = NULL;
528   //
529   if ( aS.ShapeType() == TopAbs_FACE ||
530        aS.ShapeType() == TopAbs_EDGE ) {
531     // find a face ID to which aTN belongs to
532     int faceID;
533     if ( aS.ShapeType() == TopAbs_FACE )
534       faceID = myBlock.ShapeID( aS );
535     else { // edge maybe vertical or top horizontal
536       gp_XYZ aCoord = aParams;
537       if ( aCoord.Z() == 1. )
538         aCoord.SetZ( 0.5 ); // move from top down
539       else
540         aCoord.SetX( 0.5 ); // move along X
541       faceID = SMESH_Block::GetShapeIDByParams( aCoord );
542     }
543     ASSERT( SMESH_Block::IsFaceID( faceID ));
544     int fIndex = SMESH_Block::ShapeIndex( faceID );
545     StdMeshers_IJNodeMap & ijNodes = myWallNodesMaps[ fIndex ];
546     // look for a base node in ijNodes
547     const SMDS_MeshNode* baseNode = pMesh->GetMeshDS()->FindNode( aTN.BaseNodeID() );
548     StdMeshers_IJNodeMap::const_iterator par_nVec = ijNodes.begin();
549     for ( ; par_nVec != ijNodes.end(); par_nVec++ )
550       if ( par_nVec->second[ 0 ] == baseNode ) {
551         pNode = (SMDS_MeshNode*)par_nVec->second.at( z );
552         aTN.SetNode(pNode);
553         return;
554       }
555   }
556   //
557   myBlock.Point(aParams, aS, aP1);
558   //
559   SMDS_NodeIteratorPtr ite=
560     pMesh->GetSubMeshContaining(aS)->GetSubMeshDS()->GetNodes();
561   while(ite->more()) {
562     const SMDS_MeshNode* aNode = ite->next();
563     if(myTool->IsMedium(aNode))
564       continue;
565     aX=aNode->X();
566     aY=aNode->Y();
567     aZ=aNode->Z();
568     aP2.SetCoord(aX, aY, aZ);
569     aD=(double)aP1.SquareDistance(aP2);
570     //printf("** D=%lf ", aD, aTol2);
571     if (aD < minD) {
572       pNode=(SMDS_MeshNode*)aNode;
573       aTN.SetNode(pNode);
574       minD = aD;
575       //printf(" Ok\n");
576       if (aD<aTol2)
577         return; 
578     }
579   }
580   //
581   //printf(" KO\n");
582   //aTN.SetNode(pNode);
583   //MESSAGE("StdMeshers_Penta_3D::FindNodeOnShape(), can not find the node");
584   //myErrorStatus=11; // can not find the node;
585 }
586
587
588 //=======================================================================
589 //function : SetHorizEdgeXYZ
590 //purpose  : 
591 //=======================================================================
592
593 double StdMeshers_Penta_3D::SetHorizEdgeXYZ(const gp_XYZ&                  aBaseNodeParams,
594                                             const int                      aFaceID,
595                                             vector<const SMDS_MeshNode*>*& aCol1,
596                                             vector<const SMDS_MeshNode*>*& aCol2)
597 {
598   // find base and top edges of the face
599   enum { BASE = 0, TOP };
600   vector< int > edgeVec; // 0-base, 1-top
601   SMESH_Block::GetFaceEdgesIDs( aFaceID, edgeVec );
602   //
603   int coord = SMESH_Block::GetCoordIndOnEdge( edgeVec[ BASE ] );
604   bool isForward = myBlock.IsForwadEdge( edgeVec[ BASE ] );
605
606   double param = aBaseNodeParams.Coord( coord );
607   if ( !isForward)
608     param = 1. - param;
609   //
610   // look for columns around param
611   StdMeshers_IJNodeMap & ijNodes =
612     myWallNodesMaps[ SMESH_Block::ShapeIndex( aFaceID )];
613   StdMeshers_IJNodeMap::iterator par_nVec_1 = ijNodes.begin();
614   while ( par_nVec_1->first < param )
615     par_nVec_1++;
616   StdMeshers_IJNodeMap::iterator par_nVec_2 = par_nVec_1;
617   //
618   double r = 0;
619   if ( par_nVec_1 != ijNodes.begin() ) {
620     par_nVec_1--;
621     r = ( param - par_nVec_1->first ) / ( par_nVec_2->first - par_nVec_1->first );
622   }
623   aCol1 = & par_nVec_1->second;
624   aCol2 = & par_nVec_2->second;
625
626   // top edge
627   if (1) {
628     // this variant is better for cases with curved edges and
629     // different nodes distribution on top and base edges
630     const SMDS_MeshNode* n1 = aCol1->back();
631     const SMDS_MeshNode* n2 = aCol2->back();
632     gp_XYZ xyz1( n1->X(), n1->Y(), n1->Z() );
633     gp_XYZ xyz2( n2->X(), n2->Y(), n2->Z() );
634     myShapeXYZ[ edgeVec[ 1 ] ] = ( 1. - r ) * xyz1 + r * xyz2;
635   }
636   else {
637     // this variant is better for other cases
638 //   SMESH_MesherHelper helper( *GetMesh() );
639 //   const TopoDS_Edge & edge = TopoDS::Edge( myBlock.Shape( edgeVec[ TOP ]));
640 //   double u1 = helper.GetNodeU( edge, n1 );
641 //   double u2 = helper.GetNodeU( edge, n2 );
642 //   double u = ( 1. - r ) * u1 + r * u2;
643 //   gp_XYZ topNodeParams;
644 //   myBlock.Block().EdgeParameters( edgeVec[ TOP ], u, topNodeParams );
645 //   myBlock.Block().EdgePoint( edgeVec[ TOP ],
646 //                              topNodeParams,
647 //                              myShapeXYZ[ edgeVec[ TOP ]]);
648   }
649
650   // base edge
651   myBlock.Block().EdgePoint( edgeVec[ BASE ],
652                              aBaseNodeParams,
653                              myShapeXYZ[ edgeVec[ BASE ]]);
654   return r;
655 }
656
657
658 //=======================================================================
659 //function : MakeVolumeMesh
660 //purpose  : 
661 //=======================================================================
662 void StdMeshers_Penta_3D::MakeVolumeMesh()
663 {
664   int i, j, ij, ik, i1, i2, aSSID; 
665   //
666   SMESH_Mesh*   pMesh = GetMesh();
667   SMESHDS_Mesh* meshDS = pMesh->GetMeshDS();
668   //
669   int shapeID = meshDS->ShapeToIndex( myShape );
670   //
671   // 1. Set Node In Volume
672   ik = myISize-1;
673   for (i=1; i<ik; ++i){
674     for (j=0; j<myJSize; ++j){
675       ij=i*myJSize+j;
676       const StdMeshers_TNode& aTN = myTNodes[ij];
677       aSSID=aTN.ShapeSupportID();
678       if (aSSID==SMESH_Block::ID_NONE) {
679         SMDS_MeshNode* aNode = (SMDS_MeshNode*)aTN.Node();
680         meshDS->SetNodeInVolume(aNode, shapeID);
681       }
682     }
683   }
684   //
685   // 2. Make pentahedrons
686   int aID0, k , aJ[3];
687   vector<const SMDS_MeshNode*> aN;
688   //
689   SMDS_ElemIteratorPtr itf, aItNodes;
690   //
691   const TopoDS_Face& aFxy0=
692     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy0));
693   SMESH_subMesh *aSubMesh0 = pMesh->GetSubMeshContaining(aFxy0);
694   SMESHDS_SubMesh *aSM0 = aSubMesh0->GetSubMeshDS();
695   //
696   itf = aSM0->GetElements();
697   while(itf->more()) {
698     const SMDS_MeshElement* pE0 = itf->next();
699     //
700     int nbFaceNodes = pE0->NbNodes();
701     if(myCreateQuadratic)
702       nbFaceNodes = nbFaceNodes/2;
703     if ( aN.size() < nbFaceNodes * 2 )
704       aN.resize( nbFaceNodes * 2 );
705     //
706     k=0;
707     aItNodes=pE0->nodesIterator();
708     while (aItNodes->more()) {
709       //const SMDS_MeshElement* pNode = aItNodes->next();
710       const SMDS_MeshNode* pNode =
711         static_cast<const SMDS_MeshNode*> (aItNodes->next());
712       if(myTool->IsMedium(pNode))
713         continue;
714       aID0 = pNode->GetID();
715       aJ[k] = GetIndexOnLayer(aID0);
716       if (!myErrorStatus->IsOK()) {
717         MESSAGE("StdMeshers_Penta_3D::MakeVolumeMesh");
718         return;
719       }
720       //
721       ++k;
722     }
723     //
724     bool forward = true;
725     for (i=0; i<ik; ++i) {
726       i1=i;
727       i2=i+1;
728       for(j=0; j<nbFaceNodes; ++j) {
729         ij = i1*myJSize+aJ[j];
730         const StdMeshers_TNode& aTN1 = myTNodes[ij];
731         const SMDS_MeshNode* aN1 = aTN1.Node();
732         aN[j]=aN1;
733         //
734         ij=i2*myJSize+aJ[j];
735         const StdMeshers_TNode& aTN2 = myTNodes[ij];
736         const SMDS_MeshNode* aN2 = aTN2.Node();
737         aN[j+nbFaceNodes] = aN2;
738       }
739       // check if volume orientation will be ok
740       if ( i == 0 ) {
741         SMDS_VolumeTool vTool;
742         switch ( nbFaceNodes ) {
743         case 3: {
744           SMDS_VolumeOfNodes tmpVol (aN[0], aN[1], aN[2],
745                                      aN[3], aN[4], aN[5]);
746           vTool.Set( &tmpVol );
747           break;
748         }
749         case 4: {
750           SMDS_VolumeOfNodes tmpVol(aN[0], aN[1], aN[2], aN[3],
751                                     aN[4], aN[5], aN[6], aN[7]);
752           vTool.Set( &tmpVol );
753           break;
754         }
755         default:
756           continue;
757         }
758         forward = vTool.IsForward();
759       }
760       // add volume
761       SMDS_MeshVolume* aV = 0;
762       switch ( nbFaceNodes ) {
763       case 3:
764         if ( forward ) {
765           //aV = meshDS->AddVolume(aN[0], aN[1], aN[2],
766           //                       aN[3], aN[4], aN[5]);
767           aV = myTool->AddVolume(aN[0], aN[1], aN[2], aN[3], aN[4], aN[5]);
768         }
769         else {
770           //aV = meshDS->AddVolume(aN[0], aN[2], aN[1],
771           //                       aN[3], aN[5], aN[4]);
772           aV = myTool->AddVolume(aN[0], aN[2], aN[1], aN[3], aN[5], aN[4]);
773         }
774         break;
775       case 4:
776         if ( forward ) {
777           //aV = meshDS->AddVolume(aN[0], aN[1], aN[2], aN[3],
778           //                       aN[4], aN[5], aN[6], aN[7]);
779           aV = myTool->AddVolume(aN[0], aN[1], aN[2], aN[3],
780                                  aN[4], aN[5], aN[6], aN[7]);
781         }
782         else {
783           //aV = meshDS->AddVolume(aN[0], aN[3], aN[2], aN[1],
784           //                       aN[4], aN[7], aN[6], aN[5]);
785           aV = myTool->AddVolume(aN[0], aN[3], aN[2], aN[1],
786                                  aN[4], aN[7], aN[6], aN[5]);
787         }
788         break;
789       default:
790         continue;
791       }
792       meshDS->SetMeshElementOnShape(aV, shapeID);
793     }
794   }
795 }
796
797 //=======================================================================
798 //function : MakeMeshOnFxy1
799 //purpose  : 
800 //=======================================================================
801 void StdMeshers_Penta_3D::MakeMeshOnFxy1()
802 {
803   int aID0, aJ, aLevel, ij, aNbNodes, k;
804   //
805   SMDS_NodeIteratorPtr itn;
806   SMDS_ElemIteratorPtr itf, aItNodes;
807   SMDSAbs_ElementType aElementType;
808   //
809   const TopoDS_Face& aFxy0=
810     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy0));
811   const TopoDS_Face& aFxy1=
812     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy1));
813   SMESH_MesherHelper faceHelper( *GetMesh() );
814   faceHelper.IsQuadraticSubMesh(aFxy1);
815   //
816   SMESH_Mesh* pMesh = GetMesh();
817   SMESHDS_Mesh * meshDS = pMesh->GetMeshDS();
818   //
819   SMESH_subMesh *aSubMesh1 = pMesh->GetSubMeshContaining(aFxy1);
820   SMESH_subMesh *aSubMesh0 = pMesh->GetSubMeshContaining(aFxy0);
821   SMESHDS_SubMesh *aSM0 = aSubMesh0->GetSubMeshDS();
822   //
823   // set nodes on aFxy1
824   aLevel = myISize-1;
825   itn = aSM0->GetNodes();
826   aNbNodes = aSM0->NbNodes();
827   //printf("** aNbNodes=%d\n", aNbNodes);
828
829   //
830   // set elements on aFxy1
831   vector<const SMDS_MeshNode*> aNodes1;
832   //
833   itf = aSM0->GetElements();
834   while(itf->more()) {
835     const SMDS_MeshElement* pE0 = itf->next();
836     aElementType = pE0->GetType();
837     if (!aElementType==SMDSAbs_Face) {
838       continue;
839     }
840     aNbNodes = pE0->NbNodes();
841     if(myCreateQuadratic)
842       aNbNodes = aNbNodes/2;
843     if ( aNodes1.size() < aNbNodes )
844       aNodes1.resize( aNbNodes );
845     //
846     k = aNbNodes-1; // reverse a face
847     aItNodes = pE0->nodesIterator();
848     while (aItNodes->more()) {
849       const SMDS_MeshNode* pNode =
850         static_cast<const SMDS_MeshNode*> (aItNodes->next());
851       if(myTool->IsMedium(pNode))
852         continue;
853       aID0 = pNode->GetID();
854       aJ = GetIndexOnLayer(aID0);
855       if (!myErrorStatus->IsOK()) {
856         MESSAGE("StdMeshers_Penta_3D::MakeMeshOnFxy1() ");
857         return;
858       }
859       //
860       ij = aLevel*myJSize + aJ;
861       const StdMeshers_TNode& aTN1 = myTNodes[ij];
862       const SMDS_MeshNode* aN1 = aTN1.Node();
863       aNodes1[k] = aN1;
864       --k;
865     }
866     SMDS_MeshFace * face = 0;
867     switch ( aNbNodes ) {
868     case 3:
869       face = faceHelper.AddFace(aNodes1[0], aNodes1[1], aNodes1[2]);
870       break;
871     case 4:
872       face = faceHelper.AddFace(aNodes1[0], aNodes1[1], aNodes1[2], aNodes1[3]);
873       break;
874     default:
875       continue;
876     }
877     meshDS->SetMeshElementOnShape(face, aFxy1);
878   }
879
880   // update compute state of top face submesh
881   aSubMesh1->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
882
883   // assure that mesh on the top face will be cleaned when it is cleaned
884   // on the bottom face
885   SMESH_subMesh* volSM = pMesh->GetSubMesh( myTool->GetSubShape() );
886   volSM->SetEventListener( new SMESH_subMeshEventListener(true),
887                            SMESH_subMeshEventListenerData::MakeData( aSubMesh1 ),
888                            aSubMesh0 ); // translate CLEAN event of aSubMesh0 to aSubMesh1
889 }
890
891 //=======================================================================
892 //function : ClearMeshOnFxy1
893 //purpose  : 
894 //=======================================================================
895 void StdMeshers_Penta_3D::ClearMeshOnFxy1()
896 {
897   SMESH_subMesh* aSubMesh;
898   SMESH_Mesh* pMesh=GetMesh();
899   //
900   const TopoDS_Shape& aFxy1=myBlock.Shape(SMESH_Block::ID_Fxy1);
901   aSubMesh = pMesh->GetSubMeshContaining(aFxy1);
902   if (aSubMesh)
903     aSubMesh->ComputeStateEngine( SMESH_subMesh::CLEAN );
904 }
905
906 //=======================================================================
907 //function : GetIndexOnLayer
908 //purpose  : 
909 //=======================================================================
910 int StdMeshers_Penta_3D::GetIndexOnLayer(const int aID)
911 {
912   int j=-1;
913   StdMeshers_IteratorOfDataMapOfIntegerInteger aMapIt;
914   //
915   aMapIt=myConnectingMap.find(aID);
916   if (aMapIt==myConnectingMap.end()) {
917     myErrorStatus->myName    = 200;
918     myErrorStatus->myComment = "Internal error of StdMeshers_Penta_3D";
919     return j;
920   }
921   j=(*aMapIt).second;
922   return j;
923 }
924
925 //=======================================================================
926 //function : MakeConnectingMap
927 //purpose  : 
928 //=======================================================================
929 void StdMeshers_Penta_3D::MakeConnectingMap()
930 {
931   int j, aBNID;
932   //
933   for (j=0; j<myJSize; ++j) {
934     const StdMeshers_TNode& aBN=myTNodes[j];
935     aBNID=aBN.BaseNodeID();
936     myConnectingMap[aBNID]=j;
937   }
938 }
939
940 //=======================================================================
941 //function : CreateNode
942 //purpose  : 
943 //=======================================================================
944 void StdMeshers_Penta_3D::CreateNode(const bool bIsUpperLayer,
945                                      const gp_XYZ& aParams,
946                                      StdMeshers_TNode& aTN)
947 {
948   double aX, aY, aZ;
949   //
950   gp_Pnt aP;
951   //
952   SMDS_MeshNode* pNode=NULL; 
953   aTN.SetNode(pNode);  
954   //
955 //   if (bIsUpperLayer) {
956 //     // point on face Fxy1
957 //     const TopoDS_Shape& aS=myBlock.Shape(SMESH_Block::ID_Fxy1);
958 //     myBlock.Point(aParams, aS, aP);
959 //   }
960 //   else {
961 //     // point inside solid
962 //     myBlock.Point(aParams, aP);
963 //   }
964   if (bIsUpperLayer) {
965     double u = aParams.X(), v = aParams.Y();
966     double u1 = ( 1. - u ), v1 = ( 1. - v );
967     aP.ChangeCoord()  = myShapeXYZ[ SMESH_Block::ID_Ex01 ] * v1;
968     aP.ChangeCoord() += myShapeXYZ[ SMESH_Block::ID_Ex11 ] * v;
969     aP.ChangeCoord() += myShapeXYZ[ SMESH_Block::ID_E0y1 ] * u1;
970     aP.ChangeCoord() += myShapeXYZ[ SMESH_Block::ID_E1y1 ] * u;
971
972     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V001 ] * u1 * v1;
973     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V101 ] * u  * v1;
974     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V011 ] * u1 * v;
975     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V111 ] * u  * v;
976   }
977   else {
978     SMESH_Block::ShellPoint( aParams, myShapeXYZ, aP.ChangeCoord() );
979   }
980   //
981 //   iErr=myBlock.ErrorStatus();
982 //   if (iErr) {
983 //     myErrorStatus=12; // can not find the node point;
984 //     return;
985 //   }
986   //
987   aX=aP.X(); aY=aP.Y(); aZ=aP.Z(); 
988   //
989   SMESH_Mesh* pMesh = GetMesh();
990   SMESHDS_Mesh* pMeshDS = pMesh->GetMeshDS();
991   //
992   pNode = pMeshDS->AddNode(aX, aY, aZ);
993   
994   aTN.SetNode(pNode);
995 }
996
997 //=======================================================================
998 //function : ShapeSupportID
999 //purpose  : 
1000 //=======================================================================
1001 void StdMeshers_Penta_3D::ShapeSupportID(const bool bIsUpperLayer,
1002                                          const SMESH_Block::TShapeID aBNSSID,
1003                                          SMESH_Block::TShapeID& aSSID)
1004 {
1005   switch (aBNSSID) {
1006     case SMESH_Block::ID_V000:
1007       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V001 : SMESH_Block::ID_E00z;
1008       break;
1009     case SMESH_Block::ID_V100:
1010       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V101 : SMESH_Block::ID_E10z;
1011       break; 
1012     case SMESH_Block::ID_V110:
1013       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V111 : SMESH_Block::ID_E11z;
1014       break;
1015     case SMESH_Block::ID_V010:
1016       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V011 : SMESH_Block::ID_E01z;
1017       break;
1018     case SMESH_Block::ID_Ex00:
1019       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_Ex01 : SMESH_Block::ID_Fx0z;
1020       break;
1021     case SMESH_Block::ID_Ex10:
1022       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_Ex11 : SMESH_Block::ID_Fx1z;
1023       break; 
1024     case SMESH_Block::ID_E0y0:
1025       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_E0y1 : SMESH_Block::ID_F0yz;
1026       break; 
1027     case SMESH_Block::ID_E1y0:
1028       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_E1y1 : SMESH_Block::ID_F1yz;
1029       break; 
1030     case SMESH_Block::ID_Fxy0:
1031       aSSID=SMESH_Block::ID_NONE;//(bIsUpperLayer) ?  Shape_ID_Fxy1 : Shape_ID_NONE;
1032       break;   
1033     default:
1034       aSSID=SMESH_Block::ID_NONE;
1035       myErrorStatus->myName=10; // Can not find supporting shape ID
1036       myErrorStatus->myComment = "Internal error of StdMeshers_Penta_3D";
1037       break;
1038   }
1039   return;
1040 }
1041 //=======================================================================
1042 //function : MakeBlock
1043 //purpose  : 
1044 //=======================================================================
1045 void StdMeshers_Penta_3D::MakeBlock()
1046 {
1047   bool bFound;
1048   int i, j, iNbEV, iNbE, iErr, iCnt, iNbNodes, iNbF;
1049   //
1050   TopoDS_Vertex aV000, aV001;
1051   TopoDS_Shape aFTr;
1052   TopTools_IndexedDataMapOfShapeListOfShape aMVES;
1053   TopTools_IndexedMapOfShape aME ,aMEV, aM;
1054   TopTools_ListIteratorOfListOfShape aIt;
1055   //
1056   TopExp::MapShapes(myShape, TopAbs_FACE, aM);
1057   //
1058   // 0. Find triangulated face aFTr
1059   SMDSAbs_ElementType aElementType;
1060   SMESH_Mesh* pMesh=GetMesh();
1061   //
1062   iCnt = 0;
1063   iNbF = aM.Extent();
1064   for (i=1; i<=iNbF; ++i) {
1065     const TopoDS_Shape& aF = aM(i);
1066     SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aF);
1067     ASSERT(aSubMesh);
1068     SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
1069     SMDS_ElemIteratorPtr itf = aSM->GetElements();
1070     while(itf->more()) {
1071       const SMDS_MeshElement * pElement = itf->next();
1072       aElementType = pElement->GetType();
1073       if (aElementType==SMDSAbs_Face) {
1074         iNbNodes = pElement->NbNodes();
1075         if ( iNbNodes==3 || (myCreateQuadratic && iNbNodes==6) ) {
1076           aFTr = aF;
1077           ++iCnt;
1078           if (iCnt>1) {
1079             // \begin{E.A.}
1080             // The current algorithm fails if there is more that one
1081             // face wich contains triangles ...
1082             // In that case, replace return by break to try another
1083             // method (coded in "if (iCnt != 1) { ... }")
1084             //
1085             // MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1086             // myErrorStatus=5; // more than one face has triangulation
1087             // return;
1088             break;
1089             // \end{E.A.}
1090           }
1091           break; // next face
1092         }
1093       }
1094     }
1095   }
1096   //
1097   // \begin{E.A.}
1098   // The current algorithm fails if "iCnt != 1", the case "iCnt == 0"
1099   // was not reached 'cause it was not called from Hexa_3D ... Now it
1100   // can occurs and in my opinion, it is the most common case.
1101   //
1102   if (iCnt != 1) {
1103     // The suggested algorithm is the following :
1104     //
1105     // o Check that nb_of_faces == 6 and nb_of_edges == 12
1106     //   then the shape is tologically equivalent to a box
1107     // o In a box, there are three set of four // edges ...
1108     //   In the cascade notation, it seems to be the edges
1109     //   numbered : 
1110     //     - 1, 3, 5, 7
1111     //     - 2, 4, 6, 8
1112     //     - 9, 10, 11, 12
1113     // o For each one of this set, check if the four edges
1114     //   have the same number of element.
1115     // o If so, check if the "corresponding" // faces contains
1116     //   only quads. It's the faces numbered:
1117     //     - 1, 2, 3, 4
1118     //     - 1, 2, 5, 6
1119     //     - 3, 4, 5, 6
1120     // o If so, check if the opposite edges of each // faces
1121     //   have the same number of elements. It is the edges
1122     //   numbered :
1123     //     - 2 and 4, 6 and 8, 9 and 10, 11 and 12
1124     //     - 1 and 3, 5 and 7, 9 and 11, 10 and 12
1125     //     - 1 and 5, 3 and 7, 4 and 8, 2 and 6
1126     // o If so, check if the two other faces have the same
1127     //   number of elements. It is the faces numbered:
1128     //     - 5, 6
1129     //     - 3, 4
1130     //     - 1, 2
1131     //   This test should be improved to test if the nodes
1132     //   of the two faces are really "en face".
1133     // o If so, one of the two faces is a candidate to an extrusion,
1134     //   It is the faces numbered :
1135     //     - 5
1136     //     - 3
1137     //     - 1
1138     // o Finally, if there is only one candidate, let do the
1139     //   extrusion job for the corresponding face
1140     //
1141     int isOK = 0;
1142     //
1143     int iNbF = aM.Extent();
1144     if (iNbF == 6) {
1145       //
1146       int nb_f1 = pMesh->GetSubMeshContaining(aM(1))->GetSubMeshDS()->NbElements();
1147       int nb_f2 = pMesh->GetSubMeshContaining(aM(2))->GetSubMeshDS()->NbElements();
1148       int nb_f3 = pMesh->GetSubMeshContaining(aM(3))->GetSubMeshDS()->NbElements();
1149       int nb_f4 = pMesh->GetSubMeshContaining(aM(4))->GetSubMeshDS()->NbElements();
1150       int nb_f5 = pMesh->GetSubMeshContaining(aM(5))->GetSubMeshDS()->NbElements();
1151       int nb_f6 = pMesh->GetSubMeshContaining(aM(6))->GetSubMeshDS()->NbElements();
1152       //
1153       int has_only_quad_f1 = 1;
1154       int has_only_quad_f2 = 1;
1155       int has_only_quad_f3 = 1;
1156       int has_only_quad_f4 = 1;
1157       int has_only_quad_f5 = 1;
1158       int has_only_quad_f6 = 1;
1159       //
1160       for (i=1; i<=iNbF; ++i) {
1161         int ok = 1;
1162         const TopoDS_Shape& aF = aM(i);
1163         SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aF);
1164         SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
1165         SMDS_ElemIteratorPtr itf = aSM->GetElements();
1166         while(itf->more()) {
1167           const SMDS_MeshElement * pElement = itf->next();
1168           aElementType = pElement->GetType();
1169           if (aElementType==SMDSAbs_Face) {
1170             iNbNodes = pElement->NbNodes();
1171             if ( iNbNodes!=4 ) {
1172               ok = 0;
1173               break ;
1174             }
1175           }
1176         }
1177         if (i==1) has_only_quad_f1 = ok ;
1178         if (i==2) has_only_quad_f2 = ok ;
1179         if (i==3) has_only_quad_f3 = ok ;
1180         if (i==4) has_only_quad_f4 = ok ;
1181         if (i==5) has_only_quad_f5 = ok ;
1182         if (i==6) has_only_quad_f6 = ok ;
1183       }
1184       //
1185       TopTools_IndexedMapOfShape aE;
1186       TopExp::MapShapes(myShape, TopAbs_EDGE, aE);
1187       int iNbE = aE.Extent();
1188       if (iNbE == 12) {
1189         //
1190         int nb_e01 = pMesh->GetSubMeshContaining(aE(1))->GetSubMeshDS()->NbElements();
1191         int nb_e02 = pMesh->GetSubMeshContaining(aE(2))->GetSubMeshDS()->NbElements();
1192         int nb_e03 = pMesh->GetSubMeshContaining(aE(3))->GetSubMeshDS()->NbElements();
1193         int nb_e04 = pMesh->GetSubMeshContaining(aE(4))->GetSubMeshDS()->NbElements();
1194         int nb_e05 = pMesh->GetSubMeshContaining(aE(5))->GetSubMeshDS()->NbElements();
1195         int nb_e06 = pMesh->GetSubMeshContaining(aE(6))->GetSubMeshDS()->NbElements();
1196         int nb_e07 = pMesh->GetSubMeshContaining(aE(7))->GetSubMeshDS()->NbElements();
1197         int nb_e08 = pMesh->GetSubMeshContaining(aE(8))->GetSubMeshDS()->NbElements();
1198         int nb_e09 = pMesh->GetSubMeshContaining(aE(9))->GetSubMeshDS()->NbElements();
1199         int nb_e10 = pMesh->GetSubMeshContaining(aE(10))->GetSubMeshDS()->NbElements();
1200         int nb_e11 = pMesh->GetSubMeshContaining(aE(11))->GetSubMeshDS()->NbElements();
1201         int nb_e12 = pMesh->GetSubMeshContaining(aE(12))->GetSubMeshDS()->NbElements();
1202         //
1203         int nb_ok = 0 ;
1204         //
1205         if ( (nb_e01==nb_e03) && (nb_e03==nb_e05) && (nb_e05==nb_e07) ) {
1206           if ( has_only_quad_f1 && has_only_quad_f2 && has_only_quad_f3 && has_only_quad_f4 ) {
1207             if ( (nb_e09==nb_e10) && (nb_e08==nb_e06) && (nb_e11==nb_e12) && (nb_e04==nb_e02) ) {
1208               if (nb_f5==nb_f6) {
1209                 nb_ok += 1;
1210                 aFTr = aM(5);
1211               }
1212             }
1213           }
1214         }
1215         if ( (nb_e02==nb_e04) && (nb_e04==nb_e06) && (nb_e06==nb_e08) ) {
1216           if ( has_only_quad_f1 && has_only_quad_f2 && has_only_quad_f5 && has_only_quad_f6 ) {
1217             if ( (nb_e01==nb_e03) && (nb_e10==nb_e12) && (nb_e05==nb_e07) && (nb_e09==nb_e11) ) {
1218               if (nb_f3==nb_f4) {
1219                 nb_ok += 1;
1220                 aFTr = aM(3);
1221               }
1222             }
1223           }
1224         }
1225         if ( (nb_e09==nb_e10) && (nb_e10==nb_e11) && (nb_e11==nb_e12) ) {
1226           if ( has_only_quad_f3 && has_only_quad_f4 && has_only_quad_f5 && has_only_quad_f6 ) {
1227             if ( (nb_e01==nb_e05) && (nb_e02==nb_e06) && (nb_e03==nb_e07) && (nb_e04==nb_e08) ) {
1228               if (nb_f1==nb_f2) {
1229                 nb_ok += 1;
1230                 aFTr = aM(1);
1231               }
1232             }
1233           }
1234         }
1235         //
1236         if ( nb_ok == 1 ) {
1237           isOK = 1;
1238         }
1239         //
1240       }
1241     }
1242     if (!isOK) {
1243       myErrorStatus->myName=5; // more than one face has triangulation
1244       myErrorStatus->myComment="Incorrect input mesh";
1245       return;
1246     }
1247   }
1248   // \end{E.A.}
1249   // 
1250   // 1. Vetrices V00, V001;
1251   //
1252   TopExp::MapShapes(aFTr, TopAbs_EDGE, aME);
1253   TopExp::MapShapesAndAncestors(myShape, TopAbs_VERTEX, TopAbs_EDGE, aMVES);
1254   //
1255   // 1.1 Base vertex V000
1256   iNbE = aME.Extent();
1257   if (iNbE!= NB_WALL_FACES ){
1258     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1259     myErrorStatus->myName=7; // too few edges are in base face aFTr
1260     myErrorStatus->myComment=SMESH_Comment("Not a quadrilateral face #")
1261       <<pMesh->GetMeshDS()->ShapeToIndex( aFTr )<<": "<<iNbE<<" edges" ;
1262     return;
1263   }
1264   const TopoDS_Edge& aE1=TopoDS::Edge(aME(1));
1265   aV000=TopExp::FirstVertex(aE1);
1266   //
1267   const TopTools_ListOfShape& aLE=aMVES.FindFromKey(aV000);
1268   aIt.Initialize(aLE);
1269   for (; aIt.More(); aIt.Next()) {
1270     const TopoDS_Shape& aEx=aIt.Value();
1271     aMEV.Add(aEx);
1272   }
1273   iNbEV=aMEV.Extent();
1274   if (iNbEV!=3){
1275     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1276     myErrorStatus->myName=7; // too few edges meet in base vertex 
1277     myErrorStatus->myComment=SMESH_Comment("3 edges must share vertex #")
1278       <<pMesh->GetMeshDS()->ShapeToIndex( aV000 )<<" but there are "<<iNbEV<<" edges";
1279     return;
1280   }
1281   //
1282   // 1.2 Vertex V001
1283   bFound=false;
1284   for (j=1; j<=iNbEV; ++j) {
1285     const TopoDS_Shape& aEx=aMEV(j);
1286     if (!aME.Contains(aEx)) {
1287       TopoDS_Vertex aV[2];
1288       //
1289       const TopoDS_Edge& aE=TopoDS::Edge(aEx);
1290       TopExp::Vertices(aE, aV[0], aV[1]);
1291       for (i=0; i<2; ++i) {
1292         if (!aV[i].IsSame(aV000)) {
1293           aV001=aV[i];
1294           bFound=!bFound;
1295           break;
1296         }
1297       }
1298     }
1299   }
1300   //
1301   if (!bFound) {
1302     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1303     myErrorStatus->myName=8; // can not find reper V001
1304     myErrorStatus->myComment=SMESH_Comment("Can't find opposite vertex for vertex #")
1305       <<pMesh->GetMeshDS()->ShapeToIndex( aV000 );
1306     return;
1307   }
1308   //DEB
1309   //gp_Pnt aP000, aP001;
1310   //
1311   //aP000=BRep_Tool::Pnt(TopoDS::Vertex(aV000));
1312   //printf("*** aP000 { %lf, %lf, %lf }\n", aP000.X(), aP000.Y(), aP000.Z());
1313   //aP001=BRep_Tool::Pnt(TopoDS::Vertex(aV001));
1314   //printf("*** aP001 { %lf, %lf, %lf }\n", aP001.X(), aP001.Y(), aP001.Z());
1315   //DEB
1316   //
1317   aME.Clear();
1318   TopExp::MapShapes(myShape, TopAbs_SHELL, aME);
1319   iNbE=aME.Extent();
1320   if (iNbE!=1) {
1321     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1322     myErrorStatus->myName=9; // number of shells in source shape !=1
1323     myErrorStatus->myComment=SMESH_Comment("Unexpected nb of shells ")<<iNbE;
1324     return;
1325   }
1326   //
1327   // 2. Load Block
1328   const TopoDS_Shell& aShell=TopoDS::Shell(aME(1));
1329   myBlock.Load(aShell, aV000, aV001);
1330   iErr = myBlock.ErrorStatus();
1331   if (iErr) {
1332     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1333     myErrorStatus=myBlock.GetError(); // SMESHBlock: Load operation failed
1334     return;
1335   }
1336 }
1337 //=======================================================================
1338 //function : CheckData
1339 //purpose  : 
1340 //=======================================================================
1341 void StdMeshers_Penta_3D::CheckData()
1342 {
1343   int i, iNb;
1344   int iNbEx[]={8, 12, 6};
1345   //
1346   TopAbs_ShapeEnum aST;
1347   TopAbs_ShapeEnum aSTEx[]={
1348     TopAbs_VERTEX, TopAbs_EDGE, TopAbs_FACE
1349   }; 
1350   TopTools_IndexedMapOfShape aM;
1351   //
1352   if (myShape.IsNull()){
1353     MESSAGE("StdMeshers_Penta_3D::CheckData() ");
1354     myErrorStatus->myName=2; // null shape
1355     myErrorStatus->myComment="Null shape";
1356     return;
1357   }
1358   //
1359   aST=myShape.ShapeType();
1360   if (!(aST==TopAbs_SOLID || aST==TopAbs_SHELL)) {
1361     MESSAGE("StdMeshers_Penta_3D::CheckData() ");
1362     myErrorStatus->myName=3; // not compatible type of shape
1363     myErrorStatus->myComment=SMESH_Comment("Wrong shape type (TopAbs_ShapeEnum) ")<<aST;
1364     return;
1365   }
1366   //
1367   for (i=0; i<3; ++i) {
1368     aM.Clear();
1369     TopExp::MapShapes(myShape, aSTEx[i], aM);
1370     iNb=aM.Extent();
1371     if (iNb!=iNbEx[i]){
1372       MESSAGE("StdMeshers_Penta_3D::CheckData() ");
1373       myErrorStatus->myName=4; // number of subshape is not compatible
1374       myErrorStatus->myComment="Wrong number of subshapes of a block";
1375       return;
1376     }
1377   }
1378 }
1379
1380 //=======================================================================
1381 //function : LoadIJNodes
1382 //purpose  : Load nodes bound to theFace into column (vectors) and rows
1383 //           of theIJNodes.
1384 //           The value of theIJNodes map is a vector of ordered nodes so
1385 //           that the 0-the one lies on theBaseEdge.
1386 //           The key of theIJNodes map is a normalized parameter of each
1387 //           0-the node on theBaseEdge.
1388 //=======================================================================
1389
1390 bool StdMeshers_Penta_3D::LoadIJNodes(StdMeshers_IJNodeMap & theIJNodes,
1391                                       const TopoDS_Face&     theFace,
1392                                       const TopoDS_Edge&     theBaseEdge,
1393                                       SMESHDS_Mesh*          theMesh)
1394 {
1395   // get vertices of theBaseEdge
1396   TopoDS_Vertex vfb, vlb, vft; // first and last, bottom and top vertices
1397   TopoDS_Edge eFrw = TopoDS::Edge( theBaseEdge.Oriented( TopAbs_FORWARD ));
1398   TopExp::Vertices( eFrw, vfb, vlb );
1399
1400   // find the other edges of theFace and orientation of e1
1401   TopoDS_Edge e1, e2, eTop;
1402   bool rev1, CumOri = false;
1403   TopExp_Explorer exp( theFace, TopAbs_EDGE );
1404   int nbEdges = 0;
1405   for ( ; exp.More(); exp.Next() ) {
1406     if ( ++nbEdges > NB_WALL_FACES ) {
1407       return false; // more than 4 edges in theFace
1408     }
1409     TopoDS_Edge e = TopoDS::Edge( exp.Current() );
1410     if ( theBaseEdge.IsSame( e ))
1411       continue;
1412     TopoDS_Vertex vCommon;
1413     if ( !TopExp::CommonVertex( theBaseEdge, e, vCommon ))
1414       eTop = e;
1415     else if ( vCommon.IsSame( vfb )) {
1416       e1 = e;
1417       vft = TopExp::LastVertex( e1, CumOri );
1418       rev1 = vfb.IsSame( vft );
1419       if ( rev1 )
1420         vft = TopExp::FirstVertex( e1, CumOri );
1421     }
1422     else
1423       e2 = e;
1424   }
1425   if ( nbEdges < NB_WALL_FACES ) {
1426     return false; // less than 4 edges in theFace
1427   }
1428
1429   // submeshes corresponding to shapes
1430   SMESHDS_SubMesh* smFace = theMesh->MeshElements( theFace );
1431   SMESHDS_SubMesh* smb = theMesh->MeshElements( theBaseEdge );
1432   SMESHDS_SubMesh* smt = theMesh->MeshElements( eTop );
1433   SMESHDS_SubMesh* sm1 = theMesh->MeshElements( e1 );
1434   SMESHDS_SubMesh* sm2 = theMesh->MeshElements( e2 );
1435   SMESHDS_SubMesh* smVfb = theMesh->MeshElements( vfb );
1436   SMESHDS_SubMesh* smVlb = theMesh->MeshElements( vlb );
1437   SMESHDS_SubMesh* smVft = theMesh->MeshElements( vft );
1438   if (!smFace || !smb || !smt || !sm1 || !sm2 || !smVfb || !smVlb || !smVft ) {
1439     MESSAGE( "NULL submesh " <<smFace<<" "<<smb<<" "<<smt<<" "<<
1440             sm1<<" "<<sm2<<" "<<smVfb<<" "<<smVlb<<" "<<smVft);
1441     return false;
1442   }
1443   if ( smb->NbNodes() != smt->NbNodes() || sm1->NbNodes() != sm2->NbNodes() ) {
1444     MESSAGE(" Diff nb of nodes on opposite edges" );
1445     return false;
1446   }
1447   if (smVfb->NbNodes() != 1 || smVlb->NbNodes() != 1 || smVft->NbNodes() != 1) {
1448     MESSAGE("Empty submesh of vertex");
1449     return false;
1450   }
1451   if ( sm1->NbNodes() * smb->NbNodes() != smFace->NbNodes() ) {
1452     // check quadratic case
1453     if ( myCreateQuadratic ) {
1454       int n1 = sm1->NbNodes()/2;
1455       int n2 = smb->NbNodes()/2;
1456       int n3 = sm1->NbNodes() - n1;
1457       int n4 = smb->NbNodes() - n2;
1458       int nf = sm1->NbNodes()*smb->NbNodes() - n3*n4;
1459       if( nf != smFace->NbNodes() ) {
1460         MESSAGE( "Wrong nb face nodes: " <<
1461                 sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
1462         return false;
1463       }
1464     }
1465     else {
1466       MESSAGE( "Wrong nb face nodes: " <<
1467               sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
1468       return false;
1469     }
1470   }
1471   // IJ size
1472   int vsize = sm1->NbNodes() + 2;
1473   int hsize = smb->NbNodes() + 2;
1474   if(myCreateQuadratic) {
1475     vsize = vsize - sm1->NbNodes()/2 -1;
1476     hsize = hsize - smb->NbNodes()/2 -1;
1477   }
1478
1479   // load nodes from theBaseEdge
1480
1481   set<const SMDS_MeshNode*> loadedNodes;
1482   const SMDS_MeshNode* nullNode = 0;
1483
1484   vector<const SMDS_MeshNode*> & nVecf = theIJNodes[ 0.];
1485   nVecf.resize( vsize, nullNode );
1486   loadedNodes.insert( nVecf[ 0 ] = smVfb->GetNodes()->next() );
1487
1488   vector<const SMDS_MeshNode*> & nVecl = theIJNodes[ 1.];
1489   nVecl.resize( vsize, nullNode );
1490   loadedNodes.insert( nVecl[ 0 ] = smVlb->GetNodes()->next() );
1491
1492   double f, l;
1493   BRep_Tool::Range( eFrw, f, l );
1494   double range = l - f;
1495   SMDS_NodeIteratorPtr nIt = smb->GetNodes();
1496   const SMDS_MeshNode* node;
1497   while ( nIt->more() ) {
1498     node = nIt->next();
1499     if(myTool->IsMedium(node))
1500       continue;
1501     const SMDS_EdgePosition* pos =
1502       dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
1503     if ( !pos ) {
1504       return false;
1505     }
1506     double u = ( pos->GetUParameter() - f ) / range;
1507     vector<const SMDS_MeshNode*> & nVec = theIJNodes[ u ];
1508     nVec.resize( vsize, nullNode );
1509     loadedNodes.insert( nVec[ 0 ] = node );
1510   }
1511   if ( theIJNodes.size() != hsize ) {
1512     MESSAGE( "Wrong node positions on theBaseEdge" );
1513     return false;
1514   }
1515
1516   // load nodes from e1
1517
1518   map< double, const SMDS_MeshNode*> sortedNodes; // sort by param on edge
1519   nIt = sm1->GetNodes();
1520   while ( nIt->more() ) {
1521     node = nIt->next();
1522     if(myTool->IsMedium(node))
1523       continue;
1524     const SMDS_EdgePosition* pos =
1525       dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
1526     if ( !pos ) {
1527       return false;
1528     }
1529     sortedNodes.insert( make_pair( pos->GetUParameter(), node ));
1530   }
1531   loadedNodes.insert( nVecf[ vsize - 1 ] = smVft->GetNodes()->next() );
1532   map< double, const SMDS_MeshNode*>::iterator u_n = sortedNodes.begin();
1533   int row = rev1 ? vsize - 1 : 0;
1534   for ( ; u_n != sortedNodes.end(); u_n++ ) {
1535     if ( rev1 ) row--;
1536     else        row++;
1537     loadedNodes.insert( nVecf[ row ] = u_n->second );
1538   }
1539
1540   // try to load the rest nodes
1541
1542   // get all faces from theFace
1543   TIDSortedElemSet allFaces, foundFaces;
1544   SMDS_ElemIteratorPtr eIt = smFace->GetElements();
1545   while ( eIt->more() ) {
1546     const SMDS_MeshElement* e = eIt->next();
1547     if ( e->GetType() == SMDSAbs_Face )
1548       allFaces.insert( e );
1549   }
1550   // Starting from 2 neighbour nodes on theBaseEdge, look for a face
1551   // the nodes belong to, and between the nodes of the found face,
1552   // look for a not loaded node considering this node to be the next
1553   // in a column of the starting second node. Repeat, starting
1554   // from nodes next to the previous starting nodes in their columns,
1555   // and so on while a face can be found. Then go the the next pair
1556   // of nodes on theBaseEdge.
1557   StdMeshers_IJNodeMap::iterator par_nVec_1 = theIJNodes.begin();
1558   StdMeshers_IJNodeMap::iterator par_nVec_2 = par_nVec_1;
1559   // loop on columns
1560   int col = 0;
1561   for ( par_nVec_2++; par_nVec_2 != theIJNodes.end(); par_nVec_1++, par_nVec_2++ ) {
1562     col++;
1563     row = 0;
1564     const SMDS_MeshNode* n1 = par_nVec_1->second[ row ];
1565     const SMDS_MeshNode* n2 = par_nVec_2->second[ row ];
1566     const SMDS_MeshElement* face = 0;
1567     do {
1568       // look for a face by 2 nodes
1569       face = SMESH_MeshEditor::FindFaceInSet( n1, n2, allFaces, foundFaces );
1570       if ( face ) {
1571         int nbFaceNodes = face->NbNodes();
1572         if ( (!myCreateQuadratic && nbFaceNodes>4) ||
1573              (myCreateQuadratic && nbFaceNodes>8) ) {
1574           MESSAGE(" Too many nodes in a face: " << nbFaceNodes );
1575           return false;
1576         }
1577         // look for a not loaded node of the <face>
1578         bool found = false;
1579         const SMDS_MeshNode* n3 = 0; // a node defferent from n1 and n2
1580         eIt = face->nodesIterator() ;
1581         while ( !found && eIt->more() ) {
1582           node = static_cast<const SMDS_MeshNode*>( eIt->next() );
1583           if(myTool->IsMedium(node))
1584             continue;
1585           found = loadedNodes.insert( node ).second;
1586           if ( !found && node != n1 && node != n2 )
1587             n3 = node;
1588         }
1589         if ( found ) {
1590           if ( ++row > vsize - 1 ) {
1591             MESSAGE( "Too many nodes in column "<< col <<": "<< row+1);
1592             return false;
1593           }
1594           par_nVec_2->second[ row ] = node;
1595           foundFaces.insert( face );
1596           n2 = node;
1597           if ( nbFaceNodes==4 || (myCreateQuadratic && nbFaceNodes==8) ) {
1598             n1 = par_nVec_1->second[ row ];
1599           }
1600         }
1601         else if ( (nbFaceNodes==3 || (myCreateQuadratic && nbFaceNodes==6) )  &&
1602                  n3 == par_nVec_1->second[ row ] ) {
1603           n1 = n3;
1604         }
1605         else {
1606           MESSAGE( "Not quad mesh, column "<< col );
1607           return false;
1608         }
1609       }
1610     }
1611     while ( face && n1 && n2 );
1612
1613     if ( row < vsize - 1 ) {
1614       MESSAGE( "Too few nodes in column "<< col <<": "<< row+1);
1615       MESSAGE( "Base node 1: "<< par_nVec_1->second[0]);
1616       MESSAGE( "Base node 2: "<< par_nVec_2->second[0]);
1617       MESSAGE( "Current node 1: "<< n1);
1618       MESSAGE( "Current node 2: "<< n2);
1619       MESSAGE( "first base node: "<< theIJNodes.begin()->second[0]);
1620       MESSAGE( "last base node: "<< theIJNodes.rbegin()->second[0]);
1621       return false;
1622     }
1623   } // loop on columns
1624
1625   return true;
1626 }
1627
1628 //////////////////////////////////////////////////////////////////////////
1629 //
1630 //   StdMeshers_SMESHBlock
1631 //
1632 //////////////////////////////////////////////////////////////////////////
1633
1634 //=======================================================================
1635 //function : StdMeshers_SMESHBlock
1636 //purpose  : 
1637 //=======================================================================
1638 StdMeshers_SMESHBlock::StdMeshers_SMESHBlock()
1639 {
1640   myErrorStatus=1;
1641   myIsEdgeForward.resize( SMESH_Block::NbEdges(), -1 );
1642 }
1643
1644 //=======================================================================
1645 //function : IsForwadEdge
1646 //purpose  : 
1647 //=======================================================================
1648
1649 bool StdMeshers_SMESHBlock::IsForwadEdge(const int theEdgeID)
1650 {
1651   int index = myTBlock.ShapeIndex( theEdgeID );
1652   if ( !myTBlock.IsEdgeID( theEdgeID ))
1653     return false;
1654
1655   if ( myIsEdgeForward[ index ] < 0 )
1656     myIsEdgeForward[ index ] =
1657       myTBlock.IsForwardEdge( TopoDS::Edge( Shape( theEdgeID )), myShapeIDMap );
1658
1659   return myIsEdgeForward[ index ];
1660 }
1661
1662 //=======================================================================
1663 //function : ErrorStatus
1664 //purpose  : 
1665 //=======================================================================
1666 int StdMeshers_SMESHBlock::ErrorStatus() const
1667 {
1668   return myErrorStatus;
1669 }
1670
1671 //================================================================================
1672 /*!
1673  * \brief Return problem description
1674  */
1675 //================================================================================
1676
1677 SMESH_ComputeErrorPtr StdMeshers_SMESHBlock::GetError() const
1678 {
1679   SMESH_ComputeErrorPtr err = SMESH_ComputeError::New();
1680   string & text = err->myComment;
1681   switch ( myErrorStatus ) {
1682   case 2:
1683   case 3: text = "Internal error of StdMeshers_Penta_3D"; break; 
1684   case 4: text = "Can't compute normalized parameters of a point inside a block"; break;
1685   case 5: text = "Can't compute coordinates by normalized parameters inside a block"; break;
1686   case 6: text = "Can't detect block subshapes. Not a block?"; break;
1687   }
1688   if (!text.empty())
1689     err->myName = myErrorStatus;
1690   return err;
1691 }
1692
1693 //=======================================================================
1694 //function : Load
1695 //purpose  : 
1696 //=======================================================================
1697 void StdMeshers_SMESHBlock::Load(const TopoDS_Shell& theShell)
1698 {
1699   TopoDS_Vertex aV000, aV001;
1700   //
1701   Load(theShell, aV000, aV001);
1702 }
1703
1704 //=======================================================================
1705 //function : Load
1706 //purpose  : 
1707 //=======================================================================
1708 void StdMeshers_SMESHBlock::Load(const TopoDS_Shell& theShell,
1709                                  const TopoDS_Vertex& theV000,
1710                                  const TopoDS_Vertex& theV001)
1711 {
1712   myErrorStatus=0;
1713   //
1714   myShell=theShell;
1715   //
1716   bool bOk;
1717   //
1718   myShapeIDMap.Clear();  
1719   bOk = myTBlock.LoadBlockShapes(myShell, theV000, theV001, myShapeIDMap);
1720   if (!bOk) {
1721     myErrorStatus=6;
1722     return;
1723   }
1724 }
1725
1726 //=======================================================================
1727 //function : ComputeParameters
1728 //purpose  : 
1729 //=======================================================================
1730 void StdMeshers_SMESHBlock::ComputeParameters(const gp_Pnt& thePnt, 
1731                                               gp_XYZ& theXYZ)
1732 {
1733   ComputeParameters(thePnt, myShell, theXYZ);
1734 }
1735
1736 //=======================================================================
1737 //function : ComputeParameters
1738 //purpose  : 
1739 //=======================================================================
1740 void StdMeshers_SMESHBlock::ComputeParameters(const gp_Pnt& thePnt,
1741                                               const TopoDS_Shape& theShape,
1742                                               gp_XYZ& theXYZ)
1743 {
1744   myErrorStatus=0;
1745   //
1746   int aID;
1747   bool bOk;
1748   //
1749   aID = ShapeID(theShape);
1750   if (myErrorStatus) {
1751     return;
1752   }
1753   bOk = myTBlock.ComputeParameters(thePnt, theXYZ, aID);
1754   if (!bOk) {
1755     myErrorStatus=4; // problems with computation Parameters 
1756     return;
1757   }
1758 }
1759
1760 //=======================================================================
1761 //function : ComputeParameters
1762 //purpose  : 
1763 //=======================================================================
1764
1765 void StdMeshers_SMESHBlock::ComputeParameters(const double& theU,
1766                                               const TopoDS_Shape& theShape,
1767                                               gp_XYZ& theXYZ)
1768 {
1769   myErrorStatus=0;
1770   //
1771   int aID;
1772   bool bOk=false;
1773   //
1774   aID = ShapeID(theShape);
1775   if (myErrorStatus) {
1776     return;
1777   }
1778   if ( SMESH_Block::IsEdgeID( aID ))
1779       bOk = myTBlock.EdgeParameters( aID, theU, theXYZ );
1780   if (!bOk) {
1781     myErrorStatus=4; // problems with computation Parameters 
1782     return;
1783   }
1784 }
1785
1786 //=======================================================================
1787 //function : Point
1788 //purpose  : 
1789 //=======================================================================
1790  void StdMeshers_SMESHBlock::Point(const gp_XYZ& theParams,
1791                                    gp_Pnt& aP3D)
1792 {
1793   TopoDS_Shape aS;
1794   //
1795   Point(theParams, aS, aP3D);
1796 }
1797
1798 //=======================================================================
1799 //function : Point
1800 //purpose  : 
1801 //=======================================================================
1802  void StdMeshers_SMESHBlock::Point(const gp_XYZ& theParams,
1803                                    const TopoDS_Shape& theShape,
1804                                    gp_Pnt& aP3D)
1805 {
1806   myErrorStatus = 0;
1807   //
1808   int aID;
1809   bool bOk = false;
1810   gp_XYZ aXYZ(99.,99.,99.);
1811   aP3D.SetXYZ(aXYZ);
1812   //
1813   if (theShape.IsNull()) {
1814     bOk = myTBlock.ShellPoint(theParams, aXYZ);
1815   }
1816   //
1817   else {
1818     aID=ShapeID(theShape);
1819     if (myErrorStatus) {
1820       return;
1821     }
1822     //
1823     if (SMESH_Block::IsVertexID(aID)) {
1824       bOk = myTBlock.VertexPoint(aID, aXYZ);
1825     }
1826     else if (SMESH_Block::IsEdgeID(aID)) {
1827       bOk = myTBlock.EdgePoint(aID, theParams, aXYZ);
1828     }
1829     //
1830     else if (SMESH_Block::IsFaceID(aID)) {
1831       bOk = myTBlock.FacePoint(aID, theParams, aXYZ);
1832     }
1833   }
1834   if (!bOk) {
1835     myErrorStatus=5; // problems with point computation 
1836     return;
1837   }
1838   aP3D.SetXYZ(aXYZ);
1839 }
1840
1841 //=======================================================================
1842 //function : ShapeID
1843 //purpose  : 
1844 //=======================================================================
1845 int StdMeshers_SMESHBlock::ShapeID(const TopoDS_Shape& theShape)
1846 {
1847   myErrorStatus=0;
1848   //
1849   int aID=-1;
1850   TopoDS_Shape aSF, aSR;
1851   //
1852   aSF=theShape;
1853   aSF.Orientation(TopAbs_FORWARD);
1854   aSR=theShape;
1855   aSR.Orientation(TopAbs_REVERSED);
1856   //
1857   if (myShapeIDMap.Contains(aSF)) {
1858     aID=myShapeIDMap.FindIndex(aSF);
1859     return aID;
1860   }
1861   if (myShapeIDMap.Contains(aSR)) {
1862     aID=myShapeIDMap.FindIndex(aSR);
1863     return aID;
1864   }
1865   myErrorStatus=2; // unknown shape;
1866   return aID;
1867 }
1868
1869 //=======================================================================
1870 //function : Shape
1871 //purpose  : 
1872 //=======================================================================
1873 const TopoDS_Shape& StdMeshers_SMESHBlock::Shape(const int theID)
1874 {
1875   myErrorStatus=0;
1876   //
1877   int aNb;
1878   //
1879   aNb=myShapeIDMap.Extent();
1880   if (theID<1 || theID>aNb) {
1881     myErrorStatus=3; // ID is out of range
1882     return myEmptyShape;
1883   }
1884   //
1885   const TopoDS_Shape& aS=myShapeIDMap.FindKey(theID);
1886   return aS;
1887 }
1888
1889