Salome HOME
correct previous integration (Porting to Python 2.6)
[modules/smesh.git] / src / StdMeshers / StdMeshers_Penta_3D.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH StdMeshers_Penta_3D implementaion of SMESH idl descriptions
23 //  File   : StdMeshers_Penta_3D.cxx
24 //  Module : SMESH
25 //
26 #include "StdMeshers_Penta_3D.hxx"
27
28 #include "utilities.h"
29 #include "Utils_ExceptHandlers.hxx"
30
31 #include "SMDS_EdgePosition.hxx"
32 #include "SMDS_MeshElement.hxx"
33 #include "SMDS_VolumeOfNodes.hxx"
34 #include "SMDS_VolumeTool.hxx"
35 #include "SMESHDS_SubMesh.hxx"
36 #include "SMESH_Mesh.hxx"
37 #include "SMESH_MesherHelper.hxx"
38 #include "SMESH_subMesh.hxx"
39 #include "SMESH_subMeshEventListener.hxx"
40 #include "SMESH_Comment.hxx"
41
42 #include <BRep_Tool.hxx>
43 #include <TopExp.hxx>
44 #include <TopExp_Explorer.hxx>
45 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <TopTools_ListIteratorOfListOfShape.hxx>
48 #include <TopTools_ListOfShape.hxx>
49 #include <TopTools_SequenceOfShape.hxx>
50 #include <TopTools_MapOfShape.hxx>
51 #include <TopoDS.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Shell.hxx>
54 #include <TopoDS_Vertex.hxx>
55 #include <gp_Pnt.hxx>
56
57 #include <stdio.h>
58 #include <algorithm>
59
60 using namespace std;
61
62 typedef map < int, int, less<int> >::iterator   \
63   StdMeshers_IteratorOfDataMapOfIntegerInteger;
64
65 enum { NB_WALL_FACES = 4 };
66
67 //=======================================================================
68 //function : StdMeshers_Penta_3D
69 //purpose  : 
70 //=======================================================================
71 StdMeshers_Penta_3D::StdMeshers_Penta_3D()
72   : myErrorStatus(SMESH_ComputeError::New())
73 {
74   myTol3D=0.1;
75   myWallNodesMaps.resize( SMESH_Block::NbFaces() );
76   myShapeXYZ.resize( SMESH_Block::NbSubShapes() );
77   myTool = 0;
78 }
79
80 //=======================================================================
81 //function : ~StdMeshers_Penta_3D
82 //purpose  : 
83 //=======================================================================
84
85 StdMeshers_Penta_3D::~StdMeshers_Penta_3D()
86 {
87 }
88
89 //=======================================================================
90 //function : Compute
91 //purpose  : 
92 //=======================================================================
93 bool StdMeshers_Penta_3D::Compute(SMESH_Mesh& aMesh, 
94                                   const TopoDS_Shape& aShape)
95 {
96   MESSAGE("StdMeshers_Penta_3D::Compute()");
97   //
98   bool bOK=false;
99   //
100   myShape=aShape;
101   SetMesh(aMesh);
102   //
103   CheckData();
104   if (!myErrorStatus->IsOK()) {
105     return bOK;
106   }
107
108   //
109   MakeBlock();
110   if (!myErrorStatus->IsOK()) {
111     return bOK;
112   }
113   //
114   ClearMeshOnFxy1();
115   if (!myErrorStatus->IsOK()) {
116     return bOK;
117   }
118
119   // now unnecessary faces removed, we can load medium nodes
120   SMESH_MesherHelper helper(aMesh);
121   myTool = &helper;
122   myCreateQuadratic = myTool->IsQuadraticSubMesh(aShape);
123
124   //
125   MakeNodes();
126   if (!myErrorStatus->IsOK()) {
127     return bOK;
128   }
129   //
130   MakeConnectingMap();
131   //
132   MakeMeshOnFxy1();
133   if (!myErrorStatus->IsOK()) {
134     return bOK;
135   }
136   //
137   MakeVolumeMesh();
138   //
139   return !bOK;
140 }
141
142 //=======================================================================
143 //function : MakeNodes
144 //purpose  : 
145 //=======================================================================
146 void StdMeshers_Penta_3D::MakeNodes()
147 {
148   const int aNbSIDs=9;
149   int i, j, k, ij, iNbN, aNodeID, aSize, iErr;
150   double aX, aY, aZ;
151   SMESH_Block::TShapeID aSID, aSIDs[aNbSIDs]={
152     SMESH_Block::ID_V000, SMESH_Block::ID_V100, 
153     SMESH_Block::ID_V110, SMESH_Block::ID_V010,
154     SMESH_Block::ID_Ex00, SMESH_Block::ID_E1y0, 
155     SMESH_Block::ID_Ex10, SMESH_Block::ID_E0y0,
156     SMESH_Block::ID_Fxy0
157   }; 
158   //
159   SMESH_Mesh* pMesh=GetMesh();
160   //
161   // 1. Define the sizes of mesh
162   //
163   // 1.1 Horizontal size
164   myJSize=0;
165   for (i=0; i<aNbSIDs; ++i) {
166     const TopoDS_Shape& aS = myBlock.Shape(aSIDs[i]);
167     SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aS);
168     ASSERT(aSubMesh);
169     SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
170     if(!myCreateQuadratic) {
171       iNbN = aSM->NbNodes();
172     }
173     else {
174       iNbN = 0;
175       SMDS_NodeIteratorPtr itn = aSM->GetNodes();
176       while(itn->more()) {
177         const SMDS_MeshNode* aNode = itn->next();
178         if(myTool->IsMedium(aNode))
179           continue;
180         iNbN++;
181       }
182     }
183     myJSize += iNbN;
184   }
185   //printf("***  Horizontal: number of nodes summary=%d\n", myJSize);
186   //
187   // 1.2 Vertical size
188   myISize=2;
189   {
190     const TopoDS_Shape& aS=myBlock.Shape(SMESH_Block::ID_E00z);
191     SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aS);
192     ASSERT(aSubMesh);
193     SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
194     if(!myCreateQuadratic) {
195       iNbN = aSM->NbNodes();
196     }
197     else {
198       iNbN = 0;
199       SMDS_NodeIteratorPtr itn = aSM->GetNodes();
200       while(itn->more()) {
201         const SMDS_MeshNode* aNode = itn->next();
202         if(myTool->IsMedium(aNode))
203           continue;
204         iNbN++;
205       }
206     }
207     myISize += iNbN;
208   }
209   //printf("***  Vertical: number of nodes on edges and vertices=%d\n", myISize);
210   //
211   aSize=myISize*myJSize;
212   myTNodes.resize(aSize);
213   //
214   StdMeshers_TNode aTNode;
215   gp_XYZ aCoords;
216   gp_Pnt aP3D;
217   //
218   // 2. Fill the repers on base face (Z=0)
219   i=0; j=0;
220   // vertices
221   for (k=0; k<aNbSIDs; ++k) {
222     aSID=aSIDs[k];
223     const TopoDS_Shape& aS = myBlock.Shape(aSID);
224     SMDS_NodeIteratorPtr ite = pMesh->GetSubMeshContaining(aS)->GetSubMeshDS()->GetNodes();
225     while(ite->more()) {
226       const SMDS_MeshNode* aNode = ite->next();
227       if(myTool->IsMedium(aNode))
228         continue;
229       aNodeID=aNode->GetID();
230       //
231       aTNode.SetNode(aNode);
232       aTNode.SetShapeSupportID(aSID);
233       aTNode.SetBaseNodeID(aNodeID);
234       //
235       if ( SMESH_Block::IsEdgeID (aSID)) {
236         const SMDS_EdgePosition* epos =
237           static_cast<const SMDS_EdgePosition*>(aNode->GetPosition().get());
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 ]; // 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       //   suporting shape ID
424       ShapeSupportID(bIsUpperLayer, aBNSSID, aSSID);
425       if (!myErrorStatus->IsOK()) {
426         MESSAGE("StdMeshers_Penta_3D::MakeNodes() ");
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() ");
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.IsForwadEdge( 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[3];
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 ( aN.size() < nbFaceNodes * 2 )
676       aN.resize( nbFaceNodes * 2 );
677     //
678     k=0;
679     aItNodes=pE0->nodesIterator();
680     while (aItNodes->more()) {
681       //const SMDS_MeshElement* pNode = aItNodes->next();
682       const SMDS_MeshNode* pNode =
683         static_cast<const SMDS_MeshNode*> (aItNodes->next());
684       if(myTool->IsMedium(pNode))
685         continue;
686       aID0 = pNode->GetID();
687       aJ[k] = GetIndexOnLayer(aID0);
688       if (!myErrorStatus->IsOK()) {
689         MESSAGE("StdMeshers_Penta_3D::MakeVolumeMesh");
690         return;
691       }
692       //
693       ++k;
694     }
695     //
696     bool forward = true;
697     for (i=0; i<ik; ++i) {
698       i1=i;
699       i2=i+1;
700       for(j=0; j<nbFaceNodes; ++j) {
701         ij = i1*myJSize+aJ[j];
702         const StdMeshers_TNode& aTN1 = myTNodes[ij];
703         const SMDS_MeshNode* aN1 = aTN1.Node();
704         aN[j]=aN1;
705         //
706         ij=i2*myJSize+aJ[j];
707         const StdMeshers_TNode& aTN2 = myTNodes[ij];
708         const SMDS_MeshNode* aN2 = aTN2.Node();
709         aN[j+nbFaceNodes] = aN2;
710       }
711       // check if volume orientation will be ok
712       if ( i == 0 ) {
713         SMDS_VolumeTool vTool;
714         switch ( nbFaceNodes ) {
715         case 3: {
716           SMDS_VolumeOfNodes tmpVol (aN[0], aN[1], aN[2],
717                                      aN[3], aN[4], aN[5]);
718           vTool.Set( &tmpVol );
719           break;
720         }
721         case 4: {
722           SMDS_VolumeOfNodes tmpVol(aN[0], aN[1], aN[2], aN[3],
723                                     aN[4], aN[5], aN[6], aN[7]);
724           vTool.Set( &tmpVol );
725           break;
726         }
727         default:
728           continue;
729         }
730         forward = vTool.IsForward();
731       }
732       // add volume
733       SMDS_MeshVolume* aV = 0;
734       switch ( nbFaceNodes ) {
735       case 3:
736         if ( forward ) {
737           //aV = meshDS->AddVolume(aN[0], aN[1], aN[2],
738           //                       aN[3], aN[4], aN[5]);
739           aV = myTool->AddVolume(aN[0], aN[1], aN[2], aN[3], aN[4], aN[5]);
740         }
741         else {
742           //aV = meshDS->AddVolume(aN[0], aN[2], aN[1],
743           //                       aN[3], aN[5], aN[4]);
744           aV = myTool->AddVolume(aN[0], aN[2], aN[1], aN[3], aN[5], aN[4]);
745         }
746         break;
747       case 4:
748         if ( forward ) {
749           //aV = meshDS->AddVolume(aN[0], aN[1], aN[2], aN[3],
750           //                       aN[4], aN[5], aN[6], aN[7]);
751           aV = myTool->AddVolume(aN[0], aN[1], aN[2], aN[3],
752                                  aN[4], aN[5], aN[6], aN[7]);
753         }
754         else {
755           //aV = meshDS->AddVolume(aN[0], aN[3], aN[2], aN[1],
756           //                       aN[4], aN[7], aN[6], aN[5]);
757           aV = myTool->AddVolume(aN[0], aN[3], aN[2], aN[1],
758                                  aN[4], aN[7], aN[6], aN[5]);
759         }
760         break;
761       default:
762         continue;
763       }
764       meshDS->SetMeshElementOnShape(aV, shapeID);
765     }
766   }
767 }
768
769 //=======================================================================
770 //function : MakeMeshOnFxy1
771 //purpose  : 
772 //=======================================================================
773 void StdMeshers_Penta_3D::MakeMeshOnFxy1()
774 {
775   int aID0, aJ, aLevel, ij, aNbNodes, k;
776   //
777   SMDS_NodeIteratorPtr itn;
778   SMDS_ElemIteratorPtr itf, aItNodes;
779   SMDSAbs_ElementType aElementType;
780   //
781   const TopoDS_Face& aFxy0=
782     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy0));
783   const TopoDS_Face& aFxy1=
784     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy1));
785   //
786   SMESH_Mesh* pMesh = GetMesh();
787   SMESHDS_Mesh * meshDS = pMesh->GetMeshDS();
788   //
789   SMESH_subMesh *aSubMesh1 = pMesh->GetSubMeshContaining(aFxy1);
790   SMESH_subMesh *aSubMesh0 = pMesh->GetSubMeshContaining(aFxy0);
791   SMESHDS_SubMesh *aSM0 = aSubMesh0->GetSubMeshDS();
792   //
793   // set nodes on aFxy1
794   aLevel = myISize-1;
795   itn = aSM0->GetNodes();
796   aNbNodes = aSM0->NbNodes();
797   //printf("** aNbNodes=%d\n", aNbNodes);
798   myTool->SetSubShape( aFxy1 ); // to set medium nodes to aFxy1
799   //
800   // set elements on aFxy1
801   vector<const SMDS_MeshNode*> aNodes1;
802   //
803   itf = aSM0->GetElements();
804   while(itf->more()) {
805     const SMDS_MeshElement* pE0 = itf->next();
806     aElementType = pE0->GetType();
807     if (!aElementType==SMDSAbs_Face) {
808       continue;
809     }
810     aNbNodes = pE0->NbNodes();
811     if(myCreateQuadratic)
812       aNbNodes = aNbNodes/2;
813     if ( aNodes1.size() < aNbNodes )
814       aNodes1.resize( aNbNodes );
815     //
816     k = aNbNodes-1; // reverse a face
817     aItNodes = pE0->nodesIterator();
818     while (aItNodes->more()) {
819       const SMDS_MeshNode* pNode =
820         static_cast<const SMDS_MeshNode*> (aItNodes->next());
821       if(myTool->IsMedium(pNode))
822         continue;
823       aID0 = pNode->GetID();
824       aJ = GetIndexOnLayer(aID0);
825       if (!myErrorStatus->IsOK()) {
826         MESSAGE("StdMeshers_Penta_3D::MakeMeshOnFxy1() ");
827         return;
828       }
829       //
830       ij = aLevel*myJSize + aJ;
831       const StdMeshers_TNode& aTN1 = myTNodes[ij];
832       const SMDS_MeshNode* aN1 = aTN1.Node();
833       aNodes1[k] = aN1;
834       --k;
835     }
836     SMDS_MeshFace * face = 0;
837     switch ( aNbNodes ) {
838     case 3:
839       face = myTool->AddFace(aNodes1[0], aNodes1[1], aNodes1[2]);
840       break;
841     case 4:
842       face = myTool->AddFace(aNodes1[0], aNodes1[1], aNodes1[2], aNodes1[3]);
843       break;
844     default:
845       continue;
846     }
847     meshDS->SetMeshElementOnShape(face, aFxy1);
848   }
849   myTool->SetSubShape( myShape );
850
851   // update compute state of top face submesh
852   aSubMesh1->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
853
854   // assure that mesh on the top face will be cleaned when it is cleaned
855   // on the bottom face
856   SMESH_subMesh* volSM = pMesh->GetSubMesh( myTool->GetSubShape() );
857   volSM->SetEventListener( new SMESH_subMeshEventListener(true),
858                            SMESH_subMeshEventListenerData::MakeData( aSubMesh1 ),
859                            aSubMesh0 ); // translate CLEAN event of aSubMesh0 to aSubMesh1
860 }
861
862 //=======================================================================
863 //function : ClearMeshOnFxy1
864 //purpose  : 
865 //=======================================================================
866 void StdMeshers_Penta_3D::ClearMeshOnFxy1()
867 {
868   SMESH_subMesh* aSubMesh;
869   SMESH_Mesh* pMesh=GetMesh();
870   //
871   const TopoDS_Shape& aFxy1=myBlock.Shape(SMESH_Block::ID_Fxy1);
872   aSubMesh = pMesh->GetSubMeshContaining(aFxy1);
873   if (aSubMesh)
874     aSubMesh->ComputeStateEngine( SMESH_subMesh::CLEAN );
875 }
876
877 //=======================================================================
878 //function : GetIndexOnLayer
879 //purpose  : 
880 //=======================================================================
881 int StdMeshers_Penta_3D::GetIndexOnLayer(const int aID)
882 {
883   int j=-1;
884   StdMeshers_IteratorOfDataMapOfIntegerInteger aMapIt;
885   //
886   aMapIt=myConnectingMap.find(aID);
887   if (aMapIt==myConnectingMap.end()) {
888     myErrorStatus->myName    = 200;
889     myErrorStatus->myComment = "Internal error of StdMeshers_Penta_3D";
890     return j;
891   }
892   j=(*aMapIt).second;
893   return j;
894 }
895
896 //=======================================================================
897 //function : MakeConnectingMap
898 //purpose  : 
899 //=======================================================================
900 void StdMeshers_Penta_3D::MakeConnectingMap()
901 {
902   int j, aBNID;
903   //
904   for (j=0; j<myJSize; ++j) {
905     const StdMeshers_TNode& aBN=myTNodes[j];
906     aBNID=aBN.BaseNodeID();
907     myConnectingMap[aBNID]=j;
908   }
909 }
910
911 //=======================================================================
912 //function : CreateNode
913 //purpose  : 
914 //=======================================================================
915 void StdMeshers_Penta_3D::CreateNode(const bool bIsUpperLayer,
916                                      const gp_XYZ& aParams,
917                                      StdMeshers_TNode& aTN)
918 {
919   double aX, aY, aZ;
920   //
921   gp_Pnt aP;
922   //
923   SMDS_MeshNode* pNode=NULL; 
924   aTN.SetNode(pNode);  
925   //
926   //   if (bIsUpperLayer) {
927   //     // point on face Fxy1
928   //     const TopoDS_Shape& aS=myBlock.Shape(SMESH_Block::ID_Fxy1);
929   //     myBlock.Point(aParams, aS, aP);
930   //   }
931   //   else {
932   //     // point inside solid
933   //     myBlock.Point(aParams, aP);
934   //   }
935   if (bIsUpperLayer) {
936     double u = aParams.X(), v = aParams.Y();
937     double u1 = ( 1. - u ), v1 = ( 1. - v );
938     aP.ChangeCoord()  = myShapeXYZ[ SMESH_Block::ID_Ex01 ] * v1;
939     aP.ChangeCoord() += myShapeXYZ[ SMESH_Block::ID_Ex11 ] * v;
940     aP.ChangeCoord() += myShapeXYZ[ SMESH_Block::ID_E0y1 ] * u1;
941     aP.ChangeCoord() += myShapeXYZ[ SMESH_Block::ID_E1y1 ] * u;
942
943     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V001 ] * u1 * v1;
944     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V101 ] * u  * v1;
945     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V011 ] * u1 * v;
946     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V111 ] * u  * v;
947   }
948   else {
949     SMESH_Block::ShellPoint( aParams, myShapeXYZ, aP.ChangeCoord() );
950   }
951   //
952   //   iErr=myBlock.ErrorStatus();
953   //   if (iErr) {
954   //     myErrorStatus=12; // can not find the node point;
955   //     return;
956   //   }
957   //
958   aX=aP.X(); aY=aP.Y(); aZ=aP.Z(); 
959   //
960   SMESH_Mesh* pMesh = GetMesh();
961   SMESHDS_Mesh* pMeshDS = pMesh->GetMeshDS();
962   //
963   pNode = pMeshDS->AddNode(aX, aY, aZ);
964
965   aTN.SetNode(pNode);
966 }
967
968 //=======================================================================
969 //function : ShapeSupportID
970 //purpose  : 
971 //=======================================================================
972 void StdMeshers_Penta_3D::ShapeSupportID(const bool bIsUpperLayer,
973                                          const SMESH_Block::TShapeID aBNSSID,
974                                          SMESH_Block::TShapeID& aSSID)
975 {
976   switch (aBNSSID) {
977   case SMESH_Block::ID_V000:
978     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V001 : SMESH_Block::ID_E00z;
979     break;
980   case SMESH_Block::ID_V100:
981     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V101 : SMESH_Block::ID_E10z;
982     break; 
983   case SMESH_Block::ID_V110:
984     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V111 : SMESH_Block::ID_E11z;
985     break;
986   case SMESH_Block::ID_V010:
987     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V011 : SMESH_Block::ID_E01z;
988     break;
989   case SMESH_Block::ID_Ex00:
990     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_Ex01 : SMESH_Block::ID_Fx0z;
991     break;
992   case SMESH_Block::ID_Ex10:
993     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_Ex11 : SMESH_Block::ID_Fx1z;
994     break; 
995   case SMESH_Block::ID_E0y0:
996     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_E0y1 : SMESH_Block::ID_F0yz;
997     break; 
998   case SMESH_Block::ID_E1y0:
999     aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_E1y1 : SMESH_Block::ID_F1yz;
1000     break; 
1001   case SMESH_Block::ID_Fxy0:
1002     aSSID=SMESH_Block::ID_NONE;//(bIsUpperLayer) ?  Shape_ID_Fxy1 : Shape_ID_NONE;
1003     break;   
1004   default:
1005     aSSID=SMESH_Block::ID_NONE;
1006     myErrorStatus->myName=10; // Can not find supporting shape ID
1007     myErrorStatus->myComment = "Internal error of StdMeshers_Penta_3D";
1008     break;
1009   }
1010   return;
1011 }
1012 //=======================================================================
1013 //function : MakeBlock
1014 //purpose  : 
1015 //=======================================================================
1016 void StdMeshers_Penta_3D::MakeBlock()
1017 {
1018   bool bFound;
1019   int i, j, iNbEV, iNbE, iErr, iCnt, iNbNodes, iNbF;
1020   //
1021   TopoDS_Vertex aV000, aV001;
1022   TopoDS_Shape aFTr;
1023   TopTools_IndexedDataMapOfShapeListOfShape aMVES;
1024   TopTools_IndexedMapOfShape aME ,aMEV, aM;
1025   TopTools_ListIteratorOfListOfShape aIt;
1026   //
1027   TopExp::MapShapes(myShape, TopAbs_FACE, aM);
1028   //
1029   // 0. Find triangulated face aFTr
1030   SMDSAbs_ElementType aElementType;
1031   SMESH_Mesh* pMesh=GetMesh();
1032   //
1033   iCnt = 0;
1034   iNbF = aM.Extent();
1035   for (i=1; i<=iNbF; ++i) {
1036     const TopoDS_Shape& aF = aM(i);
1037     SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aF);
1038     ASSERT(aSubMesh);
1039     SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
1040     SMDS_ElemIteratorPtr itf = aSM->GetElements();
1041     while(itf->more()) {
1042       const SMDS_MeshElement * pElement = itf->next();
1043       aElementType = pElement->GetType();
1044       if (aElementType==SMDSAbs_Face) {
1045         iNbNodes = pElement->NbNodes();
1046         if ( iNbNodes==3 || (pElement->IsQuadratic() && iNbNodes==6) ) {
1047           aFTr = aF;
1048           ++iCnt;
1049           if (iCnt>1) {
1050             // \begin{E.A.}
1051             // The current algorithm fails if there is more that one
1052             // face wich contains triangles ...
1053             // In that case, replace return by break to try another
1054             // method (coded in "if (iCnt != 1) { ... }")
1055             //
1056             // MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1057             // myErrorStatus=5; // more than one face has triangulation
1058             // return;
1059             break;
1060             // \end{E.A.}
1061           }
1062           break; // next face
1063         }
1064       }
1065     }
1066   }
1067   //
1068   // \begin{E.A.}
1069   // The current algorithm fails if "iCnt != 1", the case "iCnt == 0"
1070   // was not reached 'cause it was not called from Hexa_3D ... Now it
1071   // can occurs and in my opinion, it is the most common case.
1072   //
1073   if (iCnt != 1) {
1074     // The suggested algorithm is the following :
1075     //
1076     // o Check that nb_of_faces == 6 and nb_of_edges == 12
1077     //   then the shape is tologically equivalent to a box
1078     // o In a box, there are three set of four // edges ...
1079     //   In the cascade notation, it seems to be the edges
1080     //   numbered : 
1081     //     - 1, 3, 5, 7
1082     //     - 2, 4, 6, 8
1083     //     - 9, 10, 11, 12
1084     // o For each one of this set, check if the four edges
1085     //   have the same number of element.
1086     // o If so, check if the "corresponding" // faces contains
1087     //   only quads. It's the faces numbered:
1088     //     - 1, 2, 3, 4
1089     //     - 1, 2, 5, 6
1090     //     - 3, 4, 5, 6
1091     // o If so, check if the opposite edges of each // faces
1092     //   have the same number of elements. It is the edges
1093     //   numbered :
1094     //     - 2 and 4, 6 and 8, 9 and 10, 11 and 12
1095     //     - 1 and 3, 5 and 7, 9 and 11, 10 and 12
1096     //     - 1 and 5, 3 and 7, 4 and 8, 2 and 6
1097     // o If so, check if the two other faces have the same
1098     //   number of elements. It is the faces numbered:
1099     //     - 5, 6
1100     //     - 3, 4
1101     //     - 1, 2
1102     //   This test should be improved to test if the nodes
1103     //   of the two faces are really "en face".
1104     // o If so, one of the two faces is a candidate to an extrusion,
1105     //   It is the faces numbered :
1106     //     - 5
1107     //     - 3
1108     //     - 1
1109     // o Finally, if there is only one candidate, let do the
1110     //   extrusion job for the corresponding face
1111     //
1112     int isOK = 0;
1113     //
1114     int iNbF = aM.Extent();
1115     if (iNbF == 6) {
1116       //
1117       int nb_f1 = pMesh->GetSubMeshContaining(aM(1))->GetSubMeshDS()->NbElements();
1118       int nb_f2 = pMesh->GetSubMeshContaining(aM(2))->GetSubMeshDS()->NbElements();
1119       int nb_f3 = pMesh->GetSubMeshContaining(aM(3))->GetSubMeshDS()->NbElements();
1120       int nb_f4 = pMesh->GetSubMeshContaining(aM(4))->GetSubMeshDS()->NbElements();
1121       int nb_f5 = pMesh->GetSubMeshContaining(aM(5))->GetSubMeshDS()->NbElements();
1122       int nb_f6 = pMesh->GetSubMeshContaining(aM(6))->GetSubMeshDS()->NbElements();
1123       //
1124       int has_only_quad_f1 = 1;
1125       int has_only_quad_f2 = 1;
1126       int has_only_quad_f3 = 1;
1127       int has_only_quad_f4 = 1;
1128       int has_only_quad_f5 = 1;
1129       int has_only_quad_f6 = 1;
1130       //
1131       for (i=1; i<=iNbF; ++i) {
1132         int ok = 1;
1133         const TopoDS_Shape& aF = aM(i);
1134         SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aF);
1135         SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
1136         SMDS_ElemIteratorPtr itf = aSM->GetElements();
1137         while(itf->more()) {
1138           const SMDS_MeshElement * pElement = itf->next();
1139           aElementType = pElement->GetType();
1140           if (aElementType==SMDSAbs_Face) {
1141             iNbNodes = pElement->NbNodes();
1142             if ( iNbNodes!=4 ) {
1143               ok = 0;
1144               break ;
1145             }
1146           }
1147         }
1148         if (i==1) has_only_quad_f1 = ok ;
1149         if (i==2) has_only_quad_f2 = ok ;
1150         if (i==3) has_only_quad_f3 = ok ;
1151         if (i==4) has_only_quad_f4 = ok ;
1152         if (i==5) has_only_quad_f5 = ok ;
1153         if (i==6) has_only_quad_f6 = ok ;
1154       }
1155       //
1156       TopTools_IndexedMapOfShape aE;
1157       TopExp::MapShapes(myShape, TopAbs_EDGE, aE);
1158       int iNbE = aE.Extent();
1159       if (iNbE == 12) {
1160         //
1161         int nb_e01 = pMesh->GetSubMeshContaining(aE(1))->GetSubMeshDS()->NbElements();
1162         int nb_e02 = pMesh->GetSubMeshContaining(aE(2))->GetSubMeshDS()->NbElements();
1163         int nb_e03 = pMesh->GetSubMeshContaining(aE(3))->GetSubMeshDS()->NbElements();
1164         int nb_e04 = pMesh->GetSubMeshContaining(aE(4))->GetSubMeshDS()->NbElements();
1165         int nb_e05 = pMesh->GetSubMeshContaining(aE(5))->GetSubMeshDS()->NbElements();
1166         int nb_e06 = pMesh->GetSubMeshContaining(aE(6))->GetSubMeshDS()->NbElements();
1167         int nb_e07 = pMesh->GetSubMeshContaining(aE(7))->GetSubMeshDS()->NbElements();
1168         int nb_e08 = pMesh->GetSubMeshContaining(aE(8))->GetSubMeshDS()->NbElements();
1169         int nb_e09 = pMesh->GetSubMeshContaining(aE(9))->GetSubMeshDS()->NbElements();
1170         int nb_e10 = pMesh->GetSubMeshContaining(aE(10))->GetSubMeshDS()->NbElements();
1171         int nb_e11 = pMesh->GetSubMeshContaining(aE(11))->GetSubMeshDS()->NbElements();
1172         int nb_e12 = pMesh->GetSubMeshContaining(aE(12))->GetSubMeshDS()->NbElements();
1173         //
1174         int nb_ok = 0 ;
1175         //
1176         if ( (nb_e01==nb_e03) && (nb_e03==nb_e05) && (nb_e05==nb_e07) ) {
1177           if ( has_only_quad_f1 && has_only_quad_f2 && has_only_quad_f3 && has_only_quad_f4 ) {
1178             if ( (nb_e09==nb_e10) && (nb_e08==nb_e06) && (nb_e11==nb_e12) && (nb_e04==nb_e02) ) {
1179               if (nb_f5==nb_f6) {
1180                 nb_ok += 1;
1181                 aFTr = aM(5);
1182               }
1183             }
1184           }
1185         }
1186         if ( (nb_e02==nb_e04) && (nb_e04==nb_e06) && (nb_e06==nb_e08) ) {
1187           if ( has_only_quad_f1 && has_only_quad_f2 && has_only_quad_f5 && has_only_quad_f6 ) {
1188             if ( (nb_e01==nb_e03) && (nb_e10==nb_e12) && (nb_e05==nb_e07) && (nb_e09==nb_e11) ) {
1189               if (nb_f3==nb_f4) {
1190                 nb_ok += 1;
1191                 aFTr = aM(3);
1192               }
1193             }
1194           }
1195         }
1196         if ( (nb_e09==nb_e10) && (nb_e10==nb_e11) && (nb_e11==nb_e12) ) {
1197           if ( has_only_quad_f3 && has_only_quad_f4 && has_only_quad_f5 && has_only_quad_f6 ) {
1198             if ( (nb_e01==nb_e05) && (nb_e02==nb_e06) && (nb_e03==nb_e07) && (nb_e04==nb_e08) ) {
1199               if (nb_f1==nb_f2) {
1200                 nb_ok += 1;
1201                 aFTr = aM(1);
1202               }
1203             }
1204           }
1205         }
1206         //
1207         if ( nb_ok == 1 ) {
1208           isOK = 1;
1209         }
1210         //
1211       }
1212     }
1213     if (!isOK) {
1214       myErrorStatus->myName=5; // more than one face has triangulation
1215       myErrorStatus->myComment="Incorrect input mesh";
1216       return;
1217     }
1218   }
1219   // \end{E.A.}
1220   // 
1221   // 1. Vetrices V00, V001;
1222   //
1223   TopExp::MapShapes(aFTr, TopAbs_EDGE, aME);
1224   TopExp::MapShapesAndAncestors(myShape, TopAbs_VERTEX, TopAbs_EDGE, aMVES);
1225   //
1226   // 1.1 Base vertex V000
1227   iNbE = aME.Extent();
1228   if (iNbE!= NB_WALL_FACES ){
1229     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1230     myErrorStatus->myName=7; // too few edges are in base face aFTr
1231     myErrorStatus->myComment=SMESH_Comment("Not a quadrilateral face #")
1232       <<pMesh->GetMeshDS()->ShapeToIndex( aFTr )<<": "<<iNbE<<" edges" ;
1233     return;
1234   }
1235   const TopoDS_Edge& aE1=TopoDS::Edge(aME(1));
1236   aV000=TopExp::FirstVertex(aE1);
1237   //
1238   const TopTools_ListOfShape& aLE=aMVES.FindFromKey(aV000);
1239   aIt.Initialize(aLE);
1240   for (; aIt.More(); aIt.Next()) {
1241     const TopoDS_Shape& aEx=aIt.Value();
1242     aMEV.Add(aEx);
1243   }
1244   iNbEV=aMEV.Extent();
1245   if (iNbEV!=3){
1246     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1247     myErrorStatus->myName=7; // too few edges meet in base vertex 
1248     myErrorStatus->myComment=SMESH_Comment("3 edges must share vertex #")
1249       <<pMesh->GetMeshDS()->ShapeToIndex( aV000 )<<" but there are "<<iNbEV<<" edges";
1250     return;
1251   }
1252   //
1253   // 1.2 Vertex V001
1254   bFound=false;
1255   for (j=1; j<=iNbEV; ++j) {
1256     const TopoDS_Shape& aEx=aMEV(j);
1257     if (!aME.Contains(aEx)) {
1258       TopoDS_Vertex aV[2];
1259       //
1260       const TopoDS_Edge& aE=TopoDS::Edge(aEx);
1261       TopExp::Vertices(aE, aV[0], aV[1]);
1262       for (i=0; i<2; ++i) {
1263         if (!aV[i].IsSame(aV000)) {
1264           aV001=aV[i];
1265           bFound=!bFound;
1266           break;
1267         }
1268       }
1269     }
1270   }
1271   //
1272   if (!bFound) {
1273     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1274     myErrorStatus->myName=8; // can not find reper V001
1275     myErrorStatus->myComment=SMESH_Comment("Can't find opposite vertex for vertex #")
1276       <<pMesh->GetMeshDS()->ShapeToIndex( aV000 );
1277     return;
1278   }
1279   //DEB
1280   //gp_Pnt aP000, aP001;
1281   //
1282   //aP000=BRep_Tool::Pnt(TopoDS::Vertex(aV000));
1283   //printf("*** aP000 { %lf, %lf, %lf }\n", aP000.X(), aP000.Y(), aP000.Z());
1284   //aP001=BRep_Tool::Pnt(TopoDS::Vertex(aV001));
1285   //printf("*** aP001 { %lf, %lf, %lf }\n", aP001.X(), aP001.Y(), aP001.Z());
1286   //DEB
1287   //
1288   aME.Clear();
1289   TopExp::MapShapes(myShape, TopAbs_SHELL, aME);
1290   iNbE=aME.Extent();
1291   if (iNbE!=1) {
1292     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1293     myErrorStatus->myName=9; // number of shells in source shape !=1
1294     myErrorStatus->myComment=SMESH_Comment("Unexpected nb of shells ")<<iNbE;
1295     return;
1296   }
1297   //
1298   // 2. Load Block
1299   const TopoDS_Shell& aShell=TopoDS::Shell(aME(1));
1300   myBlock.Load(aShell, aV000, aV001);
1301   iErr = myBlock.ErrorStatus();
1302   if (iErr) {
1303     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1304     myErrorStatus=myBlock.GetError(); // SMESHBlock: Load operation failed
1305     return;
1306   }
1307 }
1308 //=======================================================================
1309 //function : CheckData
1310 //purpose  : 
1311 //=======================================================================
1312 void StdMeshers_Penta_3D::CheckData()
1313 {
1314   int i, iNb;
1315   int iNbEx[]={8, 12, 6};
1316   //
1317   TopAbs_ShapeEnum aST;
1318   TopAbs_ShapeEnum aSTEx[]={
1319     TopAbs_VERTEX, TopAbs_EDGE, TopAbs_FACE
1320   }; 
1321   TopTools_IndexedMapOfShape aM;
1322   //
1323   if (myShape.IsNull()){
1324     MESSAGE("StdMeshers_Penta_3D::CheckData() ");
1325     myErrorStatus->myName=2; // null shape
1326     myErrorStatus->myComment="Null shape";
1327     return;
1328   }
1329   //
1330   aST=myShape.ShapeType();
1331   if (!(aST==TopAbs_SOLID || aST==TopAbs_SHELL)) {
1332     MESSAGE("StdMeshers_Penta_3D::CheckData() ");
1333     myErrorStatus->myName=3; // not compatible type of shape
1334     myErrorStatus->myComment=SMESH_Comment("Wrong shape type (TopAbs_ShapeEnum) ")<<aST;
1335     return;
1336   }
1337   //
1338   for (i=0; i<3; ++i) {
1339     aM.Clear();
1340     TopExp::MapShapes(myShape, aSTEx[i], aM);
1341     iNb=aM.Extent();
1342     if (iNb!=iNbEx[i]){
1343       MESSAGE("StdMeshers_Penta_3D::CheckData() ");
1344       myErrorStatus->myName=4; // number of subshape is not compatible
1345       myErrorStatus->myComment="Wrong number of subshapes of a block";
1346       return;
1347     }
1348   }
1349 }
1350
1351 //=======================================================================
1352 //function : LoadIJNodes
1353 //purpose  : Load nodes bound to theFace into column (vectors) and rows
1354 //           of theIJNodes.
1355 //           The value of theIJNodes map is a vector of ordered nodes so
1356 //           that the 0-the one lies on theBaseEdge.
1357 //           The key of theIJNodes map is a normalized parameter of each
1358 //           0-the node on theBaseEdge.
1359 //=======================================================================
1360
1361 bool StdMeshers_Penta_3D::LoadIJNodes(StdMeshers_IJNodeMap & theIJNodes,
1362                                       const TopoDS_Face&     theFace,
1363                                       const TopoDS_Edge&     theBaseEdge,
1364                                       SMESHDS_Mesh*          theMesh)
1365 {
1366   // get vertices of theBaseEdge
1367   TopoDS_Vertex vfb, vlb, vft; // first and last, bottom and top vertices
1368   TopoDS_Edge eFrw = TopoDS::Edge( theBaseEdge.Oriented( TopAbs_FORWARD ));
1369   TopExp::Vertices( eFrw, vfb, vlb );
1370
1371   // find the other edges of theFace and orientation of e1
1372   TopoDS_Edge e1, e2, eTop;
1373   bool rev1, CumOri = false;
1374   TopExp_Explorer exp( theFace, TopAbs_EDGE );
1375   int nbEdges = 0;
1376   for ( ; exp.More(); exp.Next() ) {
1377     if ( ++nbEdges > NB_WALL_FACES ) {
1378       return false; // more than 4 edges in theFace
1379     }
1380     TopoDS_Edge e = TopoDS::Edge( exp.Current() );
1381     if ( theBaseEdge.IsSame( e ))
1382       continue;
1383     TopoDS_Vertex vCommon;
1384     if ( !TopExp::CommonVertex( theBaseEdge, e, vCommon ))
1385       eTop = e;
1386     else if ( vCommon.IsSame( vfb )) {
1387       e1 = e;
1388       vft = TopExp::LastVertex( e1, CumOri );
1389       rev1 = vfb.IsSame( vft );
1390       if ( rev1 )
1391         vft = TopExp::FirstVertex( e1, CumOri );
1392     }
1393     else
1394       e2 = e;
1395   }
1396   if ( nbEdges < NB_WALL_FACES ) {
1397     return false; // less than 4 edges in theFace
1398   }
1399
1400   // submeshes corresponding to shapes
1401   SMESHDS_SubMesh* smFace = theMesh->MeshElements( theFace );
1402   SMESHDS_SubMesh* smb = theMesh->MeshElements( theBaseEdge );
1403   SMESHDS_SubMesh* smt = theMesh->MeshElements( eTop );
1404   SMESHDS_SubMesh* sm1 = theMesh->MeshElements( e1 );
1405   SMESHDS_SubMesh* sm2 = theMesh->MeshElements( e2 );
1406   SMESHDS_SubMesh* smVfb = theMesh->MeshElements( vfb );
1407   SMESHDS_SubMesh* smVlb = theMesh->MeshElements( vlb );
1408   SMESHDS_SubMesh* smVft = theMesh->MeshElements( vft );
1409   if (!smFace || !smb || !smt || !sm1 || !sm2 || !smVfb || !smVlb || !smVft ) {
1410     MESSAGE( "NULL submesh " <<smFace<<" "<<smb<<" "<<smt<<" "<<
1411              sm1<<" "<<sm2<<" "<<smVfb<<" "<<smVlb<<" "<<smVft);
1412     return false;
1413   }
1414   if ( smb->NbNodes() != smt->NbNodes() || sm1->NbNodes() != sm2->NbNodes() ) {
1415     MESSAGE(" Diff nb of nodes on opposite edges" );
1416     return false;
1417   }
1418   if (smVfb->NbNodes() != 1 || smVlb->NbNodes() != 1 || smVft->NbNodes() != 1) {
1419     MESSAGE("Empty submesh of vertex");
1420     return false;
1421   }
1422   if ( sm1->NbNodes() * smb->NbNodes() != smFace->NbNodes() ) {
1423     // check quadratic case
1424     if ( myCreateQuadratic ) {
1425       int n1 = sm1->NbNodes()/2;
1426       int n2 = smb->NbNodes()/2;
1427       int n3 = sm1->NbNodes() - n1;
1428       int n4 = smb->NbNodes() - n2;
1429       int nf = sm1->NbNodes()*smb->NbNodes() - n3*n4;
1430       if( nf != smFace->NbNodes() ) {
1431         MESSAGE( "Wrong nb face nodes: " <<
1432                  sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
1433         return false;
1434       }
1435     }
1436     else {
1437       MESSAGE( "Wrong nb face nodes: " <<
1438                sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
1439       return false;
1440     }
1441   }
1442   // IJ size
1443   int vsize = sm1->NbNodes() + 2;
1444   int hsize = smb->NbNodes() + 2;
1445   if(myCreateQuadratic) {
1446     vsize = vsize - sm1->NbNodes()/2 -1;
1447     hsize = hsize - smb->NbNodes()/2 -1;
1448   }
1449
1450   // load nodes from theBaseEdge
1451
1452   set<const SMDS_MeshNode*> loadedNodes;
1453   const SMDS_MeshNode* nullNode = 0;
1454
1455   vector<const SMDS_MeshNode*> & nVecf = theIJNodes[ 0.];
1456   nVecf.resize( vsize, nullNode );
1457   loadedNodes.insert( nVecf[ 0 ] = smVfb->GetNodes()->next() );
1458
1459   vector<const SMDS_MeshNode*> & nVecl = theIJNodes[ 1.];
1460   nVecl.resize( vsize, nullNode );
1461   loadedNodes.insert( nVecl[ 0 ] = smVlb->GetNodes()->next() );
1462
1463   double f, l;
1464   BRep_Tool::Range( eFrw, f, l );
1465   double range = l - f;
1466   SMDS_NodeIteratorPtr nIt = smb->GetNodes();
1467   const SMDS_MeshNode* node;
1468   while ( nIt->more() ) {
1469     node = nIt->next();
1470     if(myTool->IsMedium(node))
1471       continue;
1472     const SMDS_EdgePosition* pos =
1473       dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
1474     if ( !pos ) {
1475       return false;
1476     }
1477     double u = ( pos->GetUParameter() - f ) / range;
1478     vector<const SMDS_MeshNode*> & nVec = theIJNodes[ u ];
1479     nVec.resize( vsize, nullNode );
1480     loadedNodes.insert( nVec[ 0 ] = node );
1481   }
1482   if ( theIJNodes.size() != hsize ) {
1483     MESSAGE( "Wrong node positions on theBaseEdge" );
1484     return false;
1485   }
1486
1487   // load nodes from e1
1488
1489   map< double, const SMDS_MeshNode*> sortedNodes; // sort by param on edge
1490   nIt = sm1->GetNodes();
1491   while ( nIt->more() ) {
1492     node = nIt->next();
1493     if(myTool->IsMedium(node))
1494       continue;
1495     const SMDS_EdgePosition* pos =
1496       dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
1497     if ( !pos ) {
1498       return false;
1499     }
1500     sortedNodes.insert( make_pair( pos->GetUParameter(), node ));
1501   }
1502   loadedNodes.insert( nVecf[ vsize - 1 ] = smVft->GetNodes()->next() );
1503   map< double, const SMDS_MeshNode*>::iterator u_n = sortedNodes.begin();
1504   int row = rev1 ? vsize - 1 : 0;
1505   for ( ; u_n != sortedNodes.end(); u_n++ ) {
1506     if ( rev1 ) row--;
1507     else        row++;
1508     loadedNodes.insert( nVecf[ row ] = u_n->second );
1509   }
1510
1511   // try to load the rest nodes
1512
1513   // get all faces from theFace
1514   TIDSortedElemSet allFaces, foundFaces;
1515   SMDS_ElemIteratorPtr eIt = smFace->GetElements();
1516   while ( eIt->more() ) {
1517     const SMDS_MeshElement* e = eIt->next();
1518     if ( e->GetType() == SMDSAbs_Face )
1519       allFaces.insert( e );
1520   }
1521   // Starting from 2 neighbour nodes on theBaseEdge, look for a face
1522   // the nodes belong to, and between the nodes of the found face,
1523   // look for a not loaded node considering this node to be the next
1524   // in a column of the starting second node. Repeat, starting
1525   // from nodes next to the previous starting nodes in their columns,
1526   // and so on while a face can be found. Then go the the next pair
1527   // of nodes on theBaseEdge.
1528   StdMeshers_IJNodeMap::iterator par_nVec_1 = theIJNodes.begin();
1529   StdMeshers_IJNodeMap::iterator par_nVec_2 = par_nVec_1;
1530   // loop on columns
1531   int col = 0;
1532   for ( par_nVec_2++; par_nVec_2 != theIJNodes.end(); par_nVec_1++, par_nVec_2++ ) {
1533     col++;
1534     row = 0;
1535     const SMDS_MeshNode* n1 = par_nVec_1->second[ row ];
1536     const SMDS_MeshNode* n2 = par_nVec_2->second[ row ];
1537     const SMDS_MeshElement* face = 0;
1538     do {
1539       // look for a face by 2 nodes
1540       face = SMESH_MeshEditor::FindFaceInSet( n1, n2, allFaces, foundFaces );
1541       if ( face ) {
1542         int nbFaceNodes = face->NbNodes();
1543         if ( (!myCreateQuadratic && nbFaceNodes>4) ||
1544              (myCreateQuadratic && nbFaceNodes>8) ) {
1545           MESSAGE(" Too many nodes in a face: " << nbFaceNodes );
1546           return false;
1547         }
1548         // look for a not loaded node of the <face>
1549         bool found = false;
1550         const SMDS_MeshNode* n3 = 0; // a node defferent from n1 and n2
1551         eIt = face->nodesIterator() ;
1552         while ( !found && eIt->more() ) {
1553           node = static_cast<const SMDS_MeshNode*>( eIt->next() );
1554           if(myTool->IsMedium(node))
1555             continue;
1556           found = loadedNodes.insert( node ).second;
1557           if ( !found && node != n1 && node != n2 )
1558             n3 = node;
1559         }
1560         if ( found ) {
1561           if ( ++row > vsize - 1 ) {
1562             MESSAGE( "Too many nodes in column "<< col <<": "<< row+1);
1563             return false;
1564           }
1565           par_nVec_2->second[ row ] = node;
1566           foundFaces.insert( face );
1567           n2 = node;
1568           if ( nbFaceNodes==4 || (myCreateQuadratic && nbFaceNodes==8) ) {
1569             n1 = par_nVec_1->second[ row ];
1570           }
1571         }
1572         else if ( (nbFaceNodes==3 || (myCreateQuadratic && nbFaceNodes==6) )  &&
1573                   n3 == par_nVec_1->second[ row ] ) {
1574           n1 = n3;
1575         }
1576         else {
1577           MESSAGE( "Not quad mesh, column "<< col );
1578           return false;
1579         }
1580       }
1581     }
1582     while ( face && n1 && n2 );
1583
1584     if ( row < vsize - 1 ) {
1585       MESSAGE( "Too few nodes in column "<< col <<": "<< row+1);
1586       MESSAGE( "Base node 1: "<< par_nVec_1->second[0]);
1587       MESSAGE( "Base node 2: "<< par_nVec_2->second[0]);
1588       MESSAGE( "Current node 1: "<< n1);
1589       MESSAGE( "Current node 2: "<< n2);
1590       MESSAGE( "first base node: "<< theIJNodes.begin()->second[0]);
1591       MESSAGE( "last base node: "<< theIJNodes.rbegin()->second[0]);
1592       return false;
1593     }
1594   } // loop on columns
1595
1596   return true;
1597 }
1598
1599 //////////////////////////////////////////////////////////////////////////
1600 //
1601 //   StdMeshers_SMESHBlock
1602 //
1603 //////////////////////////////////////////////////////////////////////////
1604
1605 //=======================================================================
1606 //function : StdMeshers_SMESHBlock
1607 //purpose  : 
1608 //=======================================================================
1609 StdMeshers_SMESHBlock::StdMeshers_SMESHBlock()
1610 {
1611   myErrorStatus=1;
1612   myIsEdgeForward.resize( SMESH_Block::NbEdges(), -1 );
1613 }
1614
1615 //=======================================================================
1616 //function : IsForwadEdge
1617 //purpose  : 
1618 //=======================================================================
1619
1620 bool StdMeshers_SMESHBlock::IsForwadEdge(const int theEdgeID)
1621 {
1622   int index = myTBlock.ShapeIndex( theEdgeID );
1623   if ( !myTBlock.IsEdgeID( theEdgeID ))
1624     return false;
1625
1626   if ( myIsEdgeForward[ index ] < 0 )
1627     myIsEdgeForward[ index ] =
1628       myTBlock.IsForwardEdge( TopoDS::Edge( Shape( theEdgeID )), myShapeIDMap );
1629
1630   return myIsEdgeForward[ index ];
1631 }
1632
1633 //=======================================================================
1634 //function : ErrorStatus
1635 //purpose  : 
1636 //=======================================================================
1637 int StdMeshers_SMESHBlock::ErrorStatus() const
1638 {
1639   return myErrorStatus;
1640 }
1641
1642 //================================================================================
1643 /*!
1644  * \brief Return problem description
1645  */
1646 //================================================================================
1647
1648 SMESH_ComputeErrorPtr StdMeshers_SMESHBlock::GetError() const
1649 {
1650   SMESH_ComputeErrorPtr err = SMESH_ComputeError::New();
1651   string & text = err->myComment;
1652   switch ( myErrorStatus ) {
1653   case 2:
1654   case 3: text = "Internal error of StdMeshers_Penta_3D"; break; 
1655   case 4: text = "Can't compute normalized parameters of a point inside a block"; break;
1656   case 5: text = "Can't compute coordinates by normalized parameters inside a block"; break;
1657   case 6: text = "Can't detect block subshapes. Not a block?"; break;
1658   }
1659   if (!text.empty())
1660     err->myName = myErrorStatus;
1661   return err;
1662 }
1663
1664 //=======================================================================
1665 //function : Load
1666 //purpose  : 
1667 //=======================================================================
1668 void StdMeshers_SMESHBlock::Load(const TopoDS_Shell& theShell)
1669 {
1670   TopoDS_Vertex aV000, aV001;
1671   //
1672   Load(theShell, aV000, aV001);
1673 }
1674
1675 //=======================================================================
1676 //function : Load
1677 //purpose  : 
1678 //=======================================================================
1679 void StdMeshers_SMESHBlock::Load(const TopoDS_Shell& theShell,
1680                                  const TopoDS_Vertex& theV000,
1681                                  const TopoDS_Vertex& theV001)
1682 {
1683   myErrorStatus=0;
1684   //
1685   myShell=theShell;
1686   //
1687   bool bOk;
1688   //
1689   myShapeIDMap.Clear();  
1690   bOk = myTBlock.LoadBlockShapes(myShell, theV000, theV001, myShapeIDMap);
1691   if (!bOk) {
1692     myErrorStatus=6;
1693     return;
1694   }
1695 }
1696
1697 //=======================================================================
1698 //function : ComputeParameters
1699 //purpose  : 
1700 //=======================================================================
1701 void StdMeshers_SMESHBlock::ComputeParameters(const gp_Pnt& thePnt, 
1702                                               gp_XYZ& theXYZ)
1703 {
1704   ComputeParameters(thePnt, myShell, theXYZ);
1705 }
1706
1707 //=======================================================================
1708 //function : ComputeParameters
1709 //purpose  : 
1710 //=======================================================================
1711 void StdMeshers_SMESHBlock::ComputeParameters(const gp_Pnt& thePnt,
1712                                               const TopoDS_Shape& theShape,
1713                                               gp_XYZ& theXYZ)
1714 {
1715   myErrorStatus=0;
1716   //
1717   int aID;
1718   bool bOk;
1719   //
1720   aID = ShapeID(theShape);
1721   if (myErrorStatus) {
1722     return;
1723   }
1724   bOk = myTBlock.ComputeParameters(thePnt, theXYZ, aID);
1725   if (!bOk) {
1726     myErrorStatus=4; // problems with computation Parameters 
1727     return;
1728   }
1729 }
1730
1731 //=======================================================================
1732 //function : ComputeParameters
1733 //purpose  : 
1734 //=======================================================================
1735
1736 void StdMeshers_SMESHBlock::ComputeParameters(const double& theU,
1737                                               const TopoDS_Shape& theShape,
1738                                               gp_XYZ& theXYZ)
1739 {
1740   myErrorStatus=0;
1741   //
1742   int aID;
1743   bool bOk=false;
1744   //
1745   aID = ShapeID(theShape);
1746   if (myErrorStatus) {
1747     return;
1748   }
1749   if ( SMESH_Block::IsEdgeID( aID ))
1750     bOk = myTBlock.EdgeParameters( aID, theU, theXYZ );
1751   if (!bOk) {
1752     myErrorStatus=4; // problems with computation Parameters 
1753     return;
1754   }
1755 }
1756
1757 //=======================================================================
1758 //function : Point
1759 //purpose  : 
1760 //=======================================================================
1761 void StdMeshers_SMESHBlock::Point(const gp_XYZ& theParams, gp_Pnt& aP3D)
1762 {
1763   TopoDS_Shape aS;
1764   //
1765   Point(theParams, aS, aP3D);
1766 }
1767
1768 //=======================================================================
1769 //function : Point
1770 //purpose  : 
1771 //=======================================================================
1772 void StdMeshers_SMESHBlock::Point(const gp_XYZ& theParams,
1773                                   const TopoDS_Shape& theShape,
1774                                   gp_Pnt& aP3D)
1775 {
1776   myErrorStatus = 0;
1777   //
1778   int aID;
1779   bool bOk = false;
1780   gp_XYZ aXYZ(99.,99.,99.);
1781   aP3D.SetXYZ(aXYZ);
1782   //
1783   if (theShape.IsNull()) {
1784     bOk = myTBlock.ShellPoint(theParams, aXYZ);
1785   }
1786   //
1787   else {
1788     aID=ShapeID(theShape);
1789     if (myErrorStatus) {
1790       return;
1791     }
1792     //
1793     if (SMESH_Block::IsVertexID(aID)) {
1794       bOk = myTBlock.VertexPoint(aID, aXYZ);
1795     }
1796     else if (SMESH_Block::IsEdgeID(aID)) {
1797       bOk = myTBlock.EdgePoint(aID, theParams, aXYZ);
1798     }
1799     //
1800     else if (SMESH_Block::IsFaceID(aID)) {
1801       bOk = myTBlock.FacePoint(aID, theParams, aXYZ);
1802     }
1803   }
1804   if (!bOk) {
1805     myErrorStatus=5; // problems with point computation 
1806     return;
1807   }
1808   aP3D.SetXYZ(aXYZ);
1809 }
1810
1811 //=======================================================================
1812 //function : ShapeID
1813 //purpose  : 
1814 //=======================================================================
1815 int StdMeshers_SMESHBlock::ShapeID(const TopoDS_Shape& theShape)
1816 {
1817   myErrorStatus=0;
1818   //
1819   int aID=-1;
1820   TopoDS_Shape aSF, aSR;
1821   //
1822   aSF=theShape;
1823   aSF.Orientation(TopAbs_FORWARD);
1824   aSR=theShape;
1825   aSR.Orientation(TopAbs_REVERSED);
1826   //
1827   if (myShapeIDMap.Contains(aSF)) {
1828     aID=myShapeIDMap.FindIndex(aSF);
1829     return aID;
1830   }
1831   if (myShapeIDMap.Contains(aSR)) {
1832     aID=myShapeIDMap.FindIndex(aSR);
1833     return aID;
1834   }
1835   myErrorStatus=2; // unknown shape;
1836   return aID;
1837 }
1838
1839 //=======================================================================
1840 //function : Shape
1841 //purpose  : 
1842 //=======================================================================
1843 const TopoDS_Shape& StdMeshers_SMESHBlock::Shape(const int theID)
1844 {
1845   myErrorStatus=0;
1846   //
1847   int aNb;
1848   //
1849   aNb=myShapeIDMap.Extent();
1850   if (theID<1 || theID>aNb) {
1851     myErrorStatus=3; // ID is out of range
1852     return myEmptyShape;
1853   }
1854   //
1855   const TopoDS_Shape& aS=myShapeIDMap.FindKey(theID);
1856   return aS;
1857 }
1858
1859
1860 //=======================================================================
1861 //function : Evaluate
1862 //purpose  : 
1863 //=======================================================================
1864 bool StdMeshers_Penta_3D::Evaluate(SMESH_Mesh& aMesh, 
1865                                    const TopoDS_Shape& aShape,
1866                                    MapShapeNbElems& aResMap)
1867 {
1868   MESSAGE("StdMeshers_Penta_3D::Evaluate()");
1869
1870   // find face contains only triangles
1871   vector < SMESH_subMesh * >meshFaces;
1872   TopTools_SequenceOfShape aFaces;
1873   int NumBase = 0, i = 0;
1874   for (TopExp_Explorer exp(aShape, TopAbs_FACE); exp.More(); exp.Next()) {
1875     i++;
1876     aFaces.Append(exp.Current());
1877     SMESH_subMesh *aSubMesh = aMesh.GetSubMesh(exp.Current());
1878     meshFaces.push_back(aSubMesh);
1879     MapShapeNbElemsItr anIt = aResMap.find(meshFaces[i]);
1880     if( anIt == aResMap.end() ) {
1881       NumBase = 0;
1882       break;
1883     }
1884     std::vector<int> aVec = (*anIt).second;
1885     int nbtri = Max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
1886     int nbqua = Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1887     if( nbtri>0 && nbqua==0 ) {
1888       NumBase = i;
1889     }
1890   }
1891
1892   if(NumBase==0) {
1893     std::vector<int> aResVec(SMDSEntity_Last);
1894     for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1895     SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1896     aResMap.insert(std::make_pair(sm,aResVec));
1897     myErrorStatus->myName    = COMPERR_ALGO_FAILED;
1898     myErrorStatus->myComment = "Submesh can not be evaluated";
1899     return false;
1900   }
1901
1902   // find number of 1d elems for base face
1903   int nb1d = 0;
1904   TopTools_MapOfShape Edges1;
1905   for (TopExp_Explorer exp(aFaces.Value(NumBase), TopAbs_EDGE); exp.More(); exp.Next()) {
1906     Edges1.Add(exp.Current());
1907     SMESH_subMesh *sm = aMesh.GetSubMesh(exp.Current());
1908     if( sm ) {
1909       MapShapeNbElemsItr anIt = aResMap.find(sm);
1910       if( anIt == aResMap.end() ) continue;
1911       std::vector<int> aVec = (*anIt).second;
1912       nb1d += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
1913     }
1914   }
1915   // find face opposite to base face
1916   int OppNum = 0;
1917   for(i=1; i<=6; i++) {
1918     if(i==NumBase) continue;
1919     bool IsOpposite = true;
1920     for(TopExp_Explorer exp(aFaces.Value(i), TopAbs_EDGE); exp.More(); exp.Next()) {
1921       if( Edges1.Contains(exp.Current()) ) {
1922         IsOpposite = false;
1923         break;
1924       }
1925     }
1926     if(IsOpposite) {
1927       OppNum = i;
1928       break;
1929     }
1930   }
1931   // find number of 2d elems on side faces
1932   int nb2d = 0;
1933   for(i=1; i<=6; i++) {
1934     if( i==OppNum || i==NumBase ) continue;
1935     MapShapeNbElemsItr anIt = aResMap.find( meshFaces[i-1] );
1936     if( anIt == aResMap.end() ) continue;
1937     std::vector<int> aVec = (*anIt).second;
1938     nb2d += Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1939   }
1940
1941   MapShapeNbElemsItr anIt = aResMap.find( meshFaces[NumBase-1] );
1942   std::vector<int> aVec = (*anIt).second;
1943   int nb2d_face0 = Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1944   int nb0d_face0 = aVec[SMDSEntity_Node];
1945
1946   anIt = aResMap.find( meshFaces[OppNum-1] );
1947   for(i=SMDSEntity_Node; i<SMDSEntity_Last; i++)
1948     (*anIt).second[i] = aVec[i];
1949
1950   SMESH_MesherHelper aTool (aMesh);
1951   bool _quadraticMesh = aTool.IsQuadraticSubMesh(aShape);
1952
1953   std::vector<int> aResVec(SMDSEntity_Last);
1954   for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1955   if(_quadraticMesh) {
1956     aResVec[SMDSEntity_Quad_Penta] = nb2d_face0 * ( nb2d/nb1d );
1957     aResVec[SMDSEntity_Node] = nb0d_face0 * ( 2*nb2d/nb1d - 1 );
1958   }
1959   else {
1960     aResVec[SMDSEntity_Node] = nb0d_face0 * ( nb2d/nb1d - 1 );
1961     aResVec[SMDSEntity_Penta] = nb2d_face0 * ( nb2d/nb1d );
1962   }
1963   SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1964   aResMap.insert(std::make_pair(sm,aResVec));
1965
1966   return true;
1967 }
1968