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