Salome HOME
Update copyright
[modules/smesh.git] / src / StdMeshers / StdMeshers_Penta_3D.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH StdMeshers_Penta_3D implementaion of SMESH idl descriptions
24 //  File   : StdMeshers_Penta_3D.cxx
25 //  Module : SMESH
26 //
27 #include "StdMeshers_Penta_3D.hxx"
28
29 #include "utilities.h"
30 #include "Utils_ExceptHandlers.hxx"
31
32 #include "SMDS_EdgePosition.hxx"
33 #include "SMDS_MeshElement.hxx"
34 #include "SMDS_VolumeOfNodes.hxx"
35 #include "SMDS_VolumeTool.hxx"
36 #include "SMESHDS_SubMesh.hxx"
37 #include "SMESH_Mesh.hxx"
38 #include "SMESH_MesherHelper.hxx"
39 #include "SMESH_subMesh.hxx"
40 #include "SMESH_subMeshEventListener.hxx"
41 #include "SMESH_Comment.hxx"
42
43 #include <BRep_Tool.hxx>
44 #include <TopExp.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <TopTools_ListOfShape.hxx>
50 #include <TopTools_SequenceOfShape.hxx>
51 #include <TopTools_MapOfShape.hxx>
52 #include <TopoDS.hxx>
53 #include <TopoDS_Edge.hxx>
54 #include <TopoDS_Shell.hxx>
55 #include <TopoDS_Vertex.hxx>
56 #include <gp_Pnt.hxx>
57
58 #include <stdio.h>
59 #include <algorithm>
60
61 using namespace std;
62
63 typedef map < int, int, less<int> >::iterator   \
64   StdMeshers_IteratorOfDataMapOfIntegerInteger;
65
66 enum { NB_WALL_FACES = 4 };
67
68 //=======================================================================
69 //function : StdMeshers_Penta_3D
70 //purpose  : 
71 //=======================================================================
72 StdMeshers_Penta_3D::StdMeshers_Penta_3D()
73   : myErrorStatus(SMESH_ComputeError::New())
74 {
75   myTol3D=0.1;
76   myWallNodesMaps.resize( SMESH_Block::NbFaces() );
77   myShapeXYZ.resize( SMESH_Block::NbSubShapes() );
78   myTool = 0;
79 }
80
81 //=======================================================================
82 //function : ~StdMeshers_Penta_3D
83 //purpose  : 
84 //=======================================================================
85
86 StdMeshers_Penta_3D::~StdMeshers_Penta_3D()
87 {
88 }
89
90 //=======================================================================
91 //function : Compute
92 //purpose  : 
93 //=======================================================================
94 bool StdMeshers_Penta_3D::Compute(SMESH_Mesh& aMesh, 
95                                   const TopoDS_Shape& aShape)
96 {
97   MESSAGE("StdMeshers_Penta_3D::Compute()");
98   //
99   bool bOK=false;
100   //
101   myShape=aShape;
102   SetMesh(aMesh);
103   //
104   CheckData();
105   if (!myErrorStatus->IsOK()) {
106     return bOK;
107   }
108
109   //
110   MakeBlock();
111   if (!myErrorStatus->IsOK()) {
112     return bOK;
113   }
114   //
115   ClearMeshOnFxy1();
116   if (!myErrorStatus->IsOK()) {
117     return bOK;
118   }
119
120   // now unnecessary faces removed, we can load medium nodes
121   SMESH_MesherHelper helper(aMesh);
122   myTool = &helper;
123   myCreateQuadratic = myTool->IsQuadraticSubMesh(aShape);
124
125   //
126   MakeNodes();
127   if (!myErrorStatus->IsOK()) {
128     return bOK;
129   }
130   //
131   MakeConnectingMap();
132   //
133   MakeMeshOnFxy1();
134   if (!myErrorStatus->IsOK()) {
135     return bOK;
136   }
137   //
138   MakeVolumeMesh();
139   //
140   return !bOK;
141 }
142
143 //=======================================================================
144 //function : MakeNodes
145 //purpose  : 
146 //=======================================================================
147 void StdMeshers_Penta_3D::MakeNodes()
148 {
149   const int aNbSIDs=9;
150   int i, j, k, ij, iNbN, aNodeID, aSize, iErr;
151   double aX, aY, aZ;
152   SMESH_Block::TShapeID aSID, aSIDs[aNbSIDs]={
153     SMESH_Block::ID_V000, SMESH_Block::ID_V100, 
154     SMESH_Block::ID_V110, SMESH_Block::ID_V010,
155     SMESH_Block::ID_Ex00, SMESH_Block::ID_E1y0, 
156     SMESH_Block::ID_Ex10, SMESH_Block::ID_E0y0,
157     SMESH_Block::ID_Fxy0
158   }; 
159   //
160   SMESH_Mesh* pMesh=GetMesh();
161   //
162   // 1. Define the sizes of mesh
163   //
164   // 1.1 Horizontal size
165   myJSize=0;
166   for (i=0; i<aNbSIDs; ++i) {
167     const TopoDS_Shape& aS = myBlock.Shape(aSIDs[i]);
168     SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aS);
169     ASSERT(aSubMesh);
170     SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
171     if(!myCreateQuadratic) {
172       iNbN = aSM->NbNodes();
173     }
174     else {
175       iNbN = 0;
176       SMDS_NodeIteratorPtr itn = aSM->GetNodes();
177       while(itn->more()) {
178         const SMDS_MeshNode* aNode = itn->next();
179         if(myTool->IsMedium(aNode))
180           continue;
181         iNbN++;
182       }
183     }
184     myJSize += iNbN;
185   }
186   //printf("***  Horizontal: number of nodes summary=%d\n", myJSize);
187   //
188   // 1.2 Vertical size
189   myISize=2;
190   {
191     const TopoDS_Shape& aS=myBlock.Shape(SMESH_Block::ID_E00z);
192     SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aS);
193     ASSERT(aSubMesh);
194     SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
195     if(!myCreateQuadratic) {
196       iNbN = aSM->NbNodes();
197     }
198     else {
199       iNbN = 0;
200       SMDS_NodeIteratorPtr itn = aSM->GetNodes();
201       while(itn->more()) {
202         const SMDS_MeshNode* aNode = itn->next();
203         if(myTool->IsMedium(aNode))
204           continue;
205         iNbN++;
206       }
207     }
208     myISize += iNbN;
209   }
210   //printf("***  Vertical: number of nodes on edges and vertices=%d\n", myISize);
211   //
212   aSize=myISize*myJSize;
213   myTNodes.resize(aSize);
214   //
215   StdMeshers_TNode aTNode;
216   gp_XYZ aCoords;
217   gp_Pnt aP3D;
218   //
219   // 2. Fill the repers on base face (Z=0)
220   i=0; j=0;
221   // vertices
222   for (k=0; k<aNbSIDs; ++k) {
223     aSID=aSIDs[k];
224     const TopoDS_Shape& aS = myBlock.Shape(aSID);
225     SMDS_NodeIteratorPtr ite = pMesh->GetSubMeshContaining(aS)->GetSubMeshDS()->GetNodes();
226     while(ite->more()) {
227       const SMDS_MeshNode* aNode = ite->next();
228       if(myTool->IsMedium(aNode))
229         continue;
230       aNodeID=aNode->GetID();
231       //
232       aTNode.SetNode(aNode);
233       aTNode.SetShapeSupportID(aSID);
234       aTNode.SetBaseNodeID(aNodeID);
235       //
236       if ( SMESH_Block::IsEdgeID (aSID)) {
237         const SMDS_EdgePosition* epos =
238           static_cast<const SMDS_EdgePosition*>(aNode->GetPosition());
239         myBlock.ComputeParameters( epos->GetUParameter(), aS, aCoords );
240       }
241       else {
242         aX=aNode->X();
243         aY=aNode->Y();
244         aZ=aNode->Z();
245         aP3D.SetCoord(aX, aY, aZ);
246         myBlock.ComputeParameters(aP3D, aS, aCoords);
247       }
248       iErr = myBlock.ErrorStatus();
249       if (iErr) {
250         MESSAGE("StdMeshers_Penta_3D::MakeNodes()," <<
251                 "SMESHBlock: ComputeParameters operation failed");
252         myErrorStatus=myBlock.GetError();
253         return;
254       }
255       aTNode.SetNormCoord(aCoords);
256       ij=i*myJSize+j;
257       myTNodes[ij]=aTNode;
258       ++j;
259     }
260   }
261
262   // 3.1 Fill maps of wall nodes
263   SMESH_Block::TShapeID wallFaceID[ NB_WALL_FACES ] = {
264     SMESH_Block::ID_Fx0z, SMESH_Block::ID_Fx1z,
265     SMESH_Block::ID_F0yz, SMESH_Block::ID_F1yz
266   };
267   SMESH_Block::TShapeID baseEdgeID[ NB_WALL_FACES ] = {
268     SMESH_Block::ID_Ex00, SMESH_Block::ID_Ex10,
269     SMESH_Block::ID_E0y0, SMESH_Block::ID_E1y0
270   };
271   for ( i = 0; i < NB_WALL_FACES ; ++i ) {
272     int fIndex = SMESH_Block::ShapeIndex( wallFaceID[ i ]);
273     bool ok = LoadIJNodes (myWallNodesMaps[ fIndex ],
274                            TopoDS::Face( myBlock.Shape( wallFaceID[ i ] )),
275                            TopoDS::Edge( myBlock.Shape( baseEdgeID[ i ] )),
276                            pMesh->GetMeshDS());
277     if ( !ok ) {
278       myErrorStatus->myName = COMPERR_BAD_INPUT_MESH;
279       myErrorStatus->myComment = SMESH_Comment() <<
280         "Can't find regular quadrangle mesh on a side face #" <<
281         pMesh->GetMeshDS()->ShapeToIndex( myBlock.Shape( wallFaceID[ i ]));
282       return;
283     }
284   }
285
286   // 3.2 find node columns for vertical edges and edge IDs
287   vector<const SMDS_MeshNode*> * verticEdgeNodes[ NB_WALL_FACES ];
288   SMESH_Block::TShapeID          verticEdgeID   [ NB_WALL_FACES ];
289   for ( i = 0; i < NB_WALL_FACES ; ++i ) { // 4 first base nodes are nodes on vertices
290     // edge ID
291     SMESH_Block::TShapeID eID, vID = aSIDs[ i ];
292     ShapeSupportID(false, vID, eID);
293     verticEdgeID[ i ] = eID;
294     // column nodes
295     StdMeshers_TNode& aTNode = myTNodes[ i ];
296     verticEdgeNodes[ i ] = 0;
297     for ( j = 0; j < NB_WALL_FACES ; ++j ) { // loop on 4 wall faces
298       int fIndex = SMESH_Block::ShapeIndex( wallFaceID[ j ]);
299       StdMeshers_IJNodeMap & ijNodes= myWallNodesMaps[ fIndex ];
300       if ( ijNodes.begin()->second[0] == aTNode.Node() )
301         verticEdgeNodes[ i ] = & ijNodes.begin()->second;
302       else if ( ijNodes.rbegin()->second[0] == aTNode.Node() )
303         verticEdgeNodes[ i ] = & ijNodes.rbegin()->second;
304       if ( verticEdgeNodes[ i ] )
305         break;
306     }
307   }
308
309   // 3.3 set XYZ of vertices, and initialize of the rest
310   SMESHDS_Mesh* aMesh = GetMesh()->GetMeshDS();
311   for ( int id = SMESH_Block::ID_V000; id < SMESH_Block::ID_Shell; ++id ) {
312     if ( SMESH_Block::IsVertexID( id )) {
313       TopoDS_Shape V = myBlock.Shape( id );
314       SMESHDS_SubMesh* sm = aMesh->MeshElements( V );
315       const SMDS_MeshNode* n = sm->GetNodes()->next();
316       myShapeXYZ[ id ].SetCoord( n->X(), n->Y(), n->Z() );
317     }
318     else
319       myShapeXYZ[ id ].SetCoord( 0., 0., 0. );
320   }
321
322
323   // 4. Fill the rest repers
324   bool bIsUpperLayer;
325   int iBNID;
326   SMESH_Block::TShapeID aSSID, aBNSSID;
327   StdMeshers_TNode aTN;
328   //
329
330   // create top face and find UV for it's corners
331   const TopoDS_Face& TopFace = TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy1));
332   SMESHDS_Mesh* meshDS = pMesh->GetMeshDS();
333   int topfaceID = meshDS->ShapeToIndex(TopFace);
334   const TopoDS_Vertex& v001 = TopoDS::Vertex(myBlock.Shape(SMESH_Block::ID_V001));
335   SMDS_NodeIteratorPtr itn = pMesh->GetSubMeshContaining(v001)->GetSubMeshDS()->GetNodes();
336   const SMDS_MeshNode* N = itn->next();
337   gp_XY UV001 = myTool->GetNodeUV(TopFace,N);
338   const TopoDS_Vertex& v101 = TopoDS::Vertex(myBlock.Shape(SMESH_Block::ID_V101));
339   itn = pMesh->GetSubMeshContaining(v101)->GetSubMeshDS()->GetNodes();
340   N = itn->next();
341   gp_XY UV101 = myTool->GetNodeUV(TopFace,N);
342   const TopoDS_Vertex& v011 = TopoDS::Vertex(myBlock.Shape(SMESH_Block::ID_V011));
343   itn = pMesh->GetSubMeshContaining(v011)->GetSubMeshDS()->GetNodes();
344   N = itn->next();
345   gp_XY UV011 = myTool->GetNodeUV(TopFace,N);
346   const TopoDS_Vertex& v111 = TopoDS::Vertex(myBlock.Shape(SMESH_Block::ID_V111));
347   itn = pMesh->GetSubMeshContaining(v111)->GetSubMeshDS()->GetNodes();
348   N = itn->next();
349   gp_XY UV111 = myTool->GetNodeUV(TopFace,N);
350
351   for (j=0; j<myJSize; ++j) { // loop on all nodes of the base face (ID_Fxy0)
352     // base node info
353     const StdMeshers_TNode& aBN = myTNodes[j];
354     aBNSSID = (SMESH_Block::TShapeID)aBN.ShapeSupportID();
355     iBNID = aBN.BaseNodeID();
356     const gp_XYZ& aBNXYZ = aBN.NormCoord();
357     bool createNode = ( aBNSSID == SMESH_Block::ID_Fxy0 ); // if base node is inside a bottom face
358     //
359     // set XYZ on horizontal edges and get node columns of faces:
360     // 2 columns for each face, between which a base node is located
361     vector<const SMDS_MeshNode*>* nColumns[8];
362     double ratio[ NB_WALL_FACES ]; // base node position between columns [0.-1.]
363     if ( createNode ) {
364       for ( k = 0; k < NB_WALL_FACES ; ++k ) {
365         ratio[ k ] = SetHorizEdgeXYZ (aBNXYZ, wallFaceID[ k ],
366                                       nColumns[k*2], nColumns[k*2+1]);
367       }
368     }
369     //
370     // XYZ on the bottom and top faces
371     const SMDS_MeshNode* n = aBN.Node();
372     myShapeXYZ[ SMESH_Block::ID_Fxy0 ].SetCoord( n->X(), n->Y(), n->Z() );
373     myShapeXYZ[ SMESH_Block::ID_Fxy1 ].SetCoord( 0., 0., 0. );
374     //
375     // first create or find a top node, then the rest ones in a column
376     for (i=myISize-1; i>0; --i) // vertical loop, from top to bottom
377     {
378       bIsUpperLayer = (i==(myISize-1));
379       gp_XY UV_Ex01, UV_Ex11, UV_E0y1, UV_E1y1;
380       if ( createNode ) // a base node is inside a top face
381       {
382         // set XYZ on vertical edges and faces
383         for ( k = 0; k < NB_WALL_FACES ; ++k ) {
384           // XYZ on a vertical edge 
385           const SMDS_MeshNode* n = (*verticEdgeNodes[ k ]) [ i ];
386           myShapeXYZ[ verticEdgeID[ k ] ].SetCoord( n->X(), n->Y(), n->Z() );
387           // XYZ on a face (part 1 from one column)
388           n = (*nColumns[k*2]) [ i ];
389           gp_XYZ xyz( n->X(), n->Y(), n->Z() );
390           myShapeXYZ[ wallFaceID[ k ]] = ( 1. - ratio[ k ]) * xyz;
391           gp_XY tmp1;
392           if( bIsUpperLayer ) {
393             tmp1 = myTool->GetNodeUV(TopFace,n);
394             tmp1 = ( 1. - ratio[ k ]) * tmp1;
395           }
396           // XYZ on a face (part 2 from other column)
397           n = (*nColumns[k*2+1]) [ i ];
398           xyz.SetCoord( n->X(), n->Y(), n->Z() );
399           myShapeXYZ[ wallFaceID[ k ]] += ratio[ k ] * xyz;
400           if( bIsUpperLayer ) {
401             gp_XY tmp2 = myTool->GetNodeUV(TopFace,n);
402             tmp1 +=  ratio[ k ] * tmp2;
403             if( k==0 )
404               UV_Ex01 = tmp1;
405             else if( k==1 )
406               UV_Ex11 = tmp1;
407             else if( k==2 )
408               UV_E0y1 = tmp1;
409             else
410               UV_E1y1 = tmp1;
411           }
412         }
413       }
414       // fill current node info
415       //   -index in aTNodes
416       ij=i*myJSize+j; 
417       //   -normalized coordinates  
418       aX=aBNXYZ.X();  
419       aY=aBNXYZ.Y();
420       //aZ=aZL[i];
421       aZ=(double)i/(double)(myISize-1);
422       aCoords.SetCoord(aX, aY, aZ);
423       //
424       //   suporting shape ID
425       ShapeSupportID(bIsUpperLayer, aBNSSID, aSSID);
426       if (!myErrorStatus->IsOK()) {
427         MESSAGE("StdMeshers_Penta_3D::MakeNodes() ");
428         return;
429       }
430       //
431       aTN.SetShapeSupportID(aSSID);
432       aTN.SetNormCoord(aCoords);
433       aTN.SetBaseNodeID(iBNID);
434       //
435       if (aSSID!=SMESH_Block::ID_NONE){
436         // try to find the node
437         const TopoDS_Shape& aS=myBlock.Shape((int)aSSID);
438         FindNodeOnShape(aS, aCoords, i, aTN);
439       }
440       else{
441         // create node and get its id
442         CreateNode (bIsUpperLayer, aCoords, aTN);
443         //
444         if ( bIsUpperLayer ) {
445           const SMDS_MeshNode* n = aTN.Node();
446           myShapeXYZ[ SMESH_Block::ID_Fxy1 ].SetCoord( n->X(), n->Y(), n->Z() );
447           // set node on top face:
448           // find UV parameter for this node
449           //              UV_Ex11
450           //   UV011+-----+----------+UV111
451           //        |                |
452           //        |                |
453           // UV_E0y1+     +node      +UV_E1y1
454           //        |                |
455           //        |                |
456           //        |                |
457           //   UV001+-----+----------+UV101
458           //              UV_Ex01
459           gp_Pnt2d aP;
460           double u = aCoords.X(), v = aCoords.Y();
461           double u1 = ( 1. - u ), v1 = ( 1. - v );
462           aP.ChangeCoord()  = UV_Ex01 * v1;
463           aP.ChangeCoord() += UV_Ex11 * v;
464           aP.ChangeCoord() += UV_E0y1 * u1;
465           aP.ChangeCoord() += UV_E1y1 * u;
466           aP.ChangeCoord() -= UV001 * u1 * v1;
467           aP.ChangeCoord() -= UV101 * u  * v1;
468           aP.ChangeCoord() -= UV011 * u1 * v;
469           aP.ChangeCoord() -= UV111 * u  * v;
470           meshDS->SetNodeOnFace((SMDS_MeshNode*)n, topfaceID, aP.X(), aP.Y());
471         }
472       }
473       if (!myErrorStatus->IsOK()) {
474         MESSAGE("StdMeshers_Penta_3D::MakeNodes() ");
475         return;
476       }
477       //
478       myTNodes[ij]=aTN;
479     }
480   }
481 }
482
483
484 //=======================================================================
485 //function : FindNodeOnShape
486 //purpose  : 
487 //=======================================================================
488
489 void StdMeshers_Penta_3D::FindNodeOnShape(const TopoDS_Shape& aS,
490                                           const gp_XYZ&       aParams,
491                                           const int           z,
492                                           StdMeshers_TNode&   aTN)
493 {
494   double aX, aY, aZ, aD, aTol2, minD;
495   gp_Pnt aP1, aP2;
496   //
497   SMESH_Mesh* pMesh = GetMesh();
498   aTol2 = myTol3D*myTol3D;
499   minD = 1.e100;
500   SMDS_MeshNode* pNode = NULL;
501   //
502   if ( aS.ShapeType() == TopAbs_FACE ||
503        aS.ShapeType() == TopAbs_EDGE ) {
504     // find a face ID to which aTN belongs to
505     int faceID;
506     if ( aS.ShapeType() == TopAbs_FACE )
507       faceID = myBlock.ShapeID( aS );
508     else { // edge maybe vertical or top horizontal
509       gp_XYZ aCoord = aParams;
510       if ( aCoord.Z() == 1. )
511         aCoord.SetZ( 0.5 ); // move from top down
512       else
513         aCoord.SetX( 0.5 ); // move along X
514       faceID = SMESH_Block::GetShapeIDByParams( aCoord );
515     }
516     ASSERT( SMESH_Block::IsFaceID( faceID ));
517     int fIndex = SMESH_Block::ShapeIndex( faceID );
518     StdMeshers_IJNodeMap & ijNodes = myWallNodesMaps[ fIndex ];
519     // look for a base node in ijNodes
520     const SMDS_MeshNode* baseNode = pMesh->GetMeshDS()->FindNode( aTN.BaseNodeID() );
521     StdMeshers_IJNodeMap::const_iterator par_nVec = ijNodes.begin();
522     for ( ; par_nVec != ijNodes.end(); par_nVec++ )
523       if ( par_nVec->second[ 0 ] == baseNode ) {
524         pNode = (SMDS_MeshNode*)par_nVec->second.at( z );
525         aTN.SetNode(pNode);
526         return;
527       }
528   }
529   //
530   myBlock.Point(aParams, aS, aP1);
531   //
532   SMDS_NodeIteratorPtr ite=
533     pMesh->GetSubMeshContaining(aS)->GetSubMeshDS()->GetNodes();
534   while(ite->more()) {
535     const SMDS_MeshNode* aNode = ite->next();
536     if(myTool->IsMedium(aNode))
537       continue;
538     aX=aNode->X();
539     aY=aNode->Y();
540     aZ=aNode->Z();
541     aP2.SetCoord(aX, aY, aZ);
542     aD=(double)aP1.SquareDistance(aP2);
543     //printf("** D=%lf ", aD, aTol2);
544     if (aD < minD) {
545       pNode=(SMDS_MeshNode*)aNode;
546       aTN.SetNode(pNode);
547       minD = aD;
548       //printf(" Ok\n");
549       if (aD<aTol2)
550         return; 
551     }
552   }
553   //
554   //printf(" KO\n");
555   //aTN.SetNode(pNode);
556   //MESSAGE("StdMeshers_Penta_3D::FindNodeOnShape(), can not find the node");
557   //myErrorStatus=11; // can not find the node;
558 }
559
560
561 //=======================================================================
562 //function : SetHorizEdgeXYZ
563 //purpose  : 
564 //=======================================================================
565
566 double StdMeshers_Penta_3D::SetHorizEdgeXYZ(const gp_XYZ&                  aBaseNodeParams,
567                                             const int                      aFaceID,
568                                             vector<const SMDS_MeshNode*>*& aCol1,
569                                             vector<const SMDS_MeshNode*>*& aCol2)
570 {
571   // find base and top edges of the face
572   enum { BASE = 0, TOP };
573   vector< int > edgeVec; // 0-base, 1-top
574   SMESH_Block::GetFaceEdgesIDs( aFaceID, edgeVec );
575   //
576   int coord = SMESH_Block::GetCoordIndOnEdge( edgeVec[ BASE ] );
577   bool isForward = myBlock.IsForwadEdge( edgeVec[ BASE ] );
578
579   double param = aBaseNodeParams.Coord( coord );
580   if ( !isForward)
581     param = 1. - param;
582   //
583   // look for columns around param
584   StdMeshers_IJNodeMap & ijNodes =
585     myWallNodesMaps[ SMESH_Block::ShapeIndex( aFaceID )];
586   StdMeshers_IJNodeMap::iterator par_nVec_1 = ijNodes.begin();
587   while ( par_nVec_1->first < param )
588     par_nVec_1++;
589   StdMeshers_IJNodeMap::iterator par_nVec_2 = par_nVec_1;
590   //
591   double r = 0;
592   if ( par_nVec_1 != ijNodes.begin() ) {
593     par_nVec_1--;
594     r = ( param - par_nVec_1->first ) / ( par_nVec_2->first - par_nVec_1->first );
595   }
596   aCol1 = & par_nVec_1->second;
597   aCol2 = & par_nVec_2->second;
598
599   // top edge
600   if (1) {
601     // this variant is better for cases with curved edges and
602     // different nodes distribution on top and base edges
603     const SMDS_MeshNode* n1 = aCol1->back();
604     const SMDS_MeshNode* n2 = aCol2->back();
605     gp_XYZ xyz1( n1->X(), n1->Y(), n1->Z() );
606     gp_XYZ xyz2( n2->X(), n2->Y(), n2->Z() );
607     myShapeXYZ[ edgeVec[ 1 ] ] = ( 1. - r ) * xyz1 + r * xyz2;
608   }
609   else {
610     // this variant is better for other cases
611     //   SMESH_MesherHelper helper( *GetMesh() );
612     //   const TopoDS_Edge & edge = TopoDS::Edge( myBlock.Shape( edgeVec[ TOP ]));
613     //   double u1 = helper.GetNodeU( edge, n1 );
614     //   double u2 = helper.GetNodeU( edge, n2 );
615     //   double u = ( 1. - r ) * u1 + r * u2;
616     //   gp_XYZ topNodeParams;
617     //   myBlock.Block().EdgeParameters( edgeVec[ TOP ], u, topNodeParams );
618     //   myBlock.Block().EdgePoint( edgeVec[ TOP ],
619     //                              topNodeParams,
620     //                              myShapeXYZ[ edgeVec[ TOP ]]);
621   }
622
623   // base edge
624   myBlock.Block().EdgePoint( edgeVec[ BASE ],
625                              aBaseNodeParams,
626                              myShapeXYZ[ edgeVec[ BASE ]]);
627   return r;
628 }
629
630
631 //=======================================================================
632 //function : MakeVolumeMesh
633 //purpose  : 
634 //=======================================================================
635 void StdMeshers_Penta_3D::MakeVolumeMesh()
636 {
637   int i, j, ij, ik, i1, i2, aSSID; 
638   //
639   SMESH_Mesh*   pMesh = GetMesh();
640   SMESHDS_Mesh* meshDS = pMesh->GetMeshDS();
641   //
642   int shapeID = meshDS->ShapeToIndex( myShape );
643   //
644   // 1. Set Node In Volume
645   ik = myISize-1;
646   for (i=1; i<ik; ++i){
647     for (j=0; j<myJSize; ++j){
648       ij=i*myJSize+j;
649       const StdMeshers_TNode& aTN = myTNodes[ij];
650       aSSID=aTN.ShapeSupportID();
651       if (aSSID==SMESH_Block::ID_NONE) {
652         SMDS_MeshNode* aNode = (SMDS_MeshNode*)aTN.Node();
653         meshDS->SetNodeInVolume(aNode, shapeID);
654       }
655     }
656   }
657   //
658   // 2. Make pentahedrons
659   int aID0, k , aJ[3];
660   vector<const SMDS_MeshNode*> aN;
661   //
662   SMDS_ElemIteratorPtr itf, aItNodes;
663   //
664   const TopoDS_Face& aFxy0=
665     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy0));
666   SMESH_subMesh *aSubMesh0 = pMesh->GetSubMeshContaining(aFxy0);
667   SMESHDS_SubMesh *aSM0 = aSubMesh0->GetSubMeshDS();
668   //
669   itf = aSM0->GetElements();
670   while(itf->more()) {
671     const SMDS_MeshElement* pE0 = itf->next();
672     //
673     int nbFaceNodes = pE0->NbNodes();
674     if(myCreateQuadratic)
675       nbFaceNodes = nbFaceNodes/2;
676     if ( aN.size() < nbFaceNodes * 2 )
677       aN.resize( nbFaceNodes * 2 );
678     //
679     for ( k=0; k<nbFaceNodes; ++k ) {
680       const SMDS_MeshNode* pNode = pE0->GetNode(k);
681 //       if(myTool->IsMedium(pNode))
682 //         continue;
683       aID0 = pNode->GetID();
684       aJ[k] = GetIndexOnLayer(aID0);
685       if (!myErrorStatus->IsOK()) {
686         MESSAGE("StdMeshers_Penta_3D::MakeVolumeMesh");
687         return;
688       }
689     }
690     //
691     bool forward = true;
692     for (i=0; i<ik; ++i) {
693       i1=i;
694       i2=i+1;
695       for(j=0; j<nbFaceNodes; ++j) {
696         ij = i1*myJSize+aJ[j];
697         const StdMeshers_TNode& aTN1 = myTNodes[ij];
698         const SMDS_MeshNode* aN1 = aTN1.Node();
699         aN[j]=aN1;
700         //
701         ij=i2*myJSize+aJ[j];
702         const StdMeshers_TNode& aTN2 = myTNodes[ij];
703         const SMDS_MeshNode* aN2 = aTN2.Node();
704         aN[j+nbFaceNodes] = aN2;
705       }
706       // check if volume orientation will be ok
707       if ( i == 0 ) {
708         SMDS_VolumeTool vTool;
709         switch ( nbFaceNodes ) {
710         case 3: {
711           SMDS_VolumeOfNodes tmpVol (aN[0], aN[1], aN[2],
712                                      aN[3], aN[4], aN[5]);
713           vTool.Set( &tmpVol );
714           break;
715         }
716         case 4: {
717           SMDS_VolumeOfNodes tmpVol(aN[0], aN[1], aN[2], aN[3],
718                                     aN[4], aN[5], aN[6], aN[7]);
719           vTool.Set( &tmpVol );
720           break;
721         }
722         default:
723           continue;
724         }
725         forward = vTool.IsForward();
726       }
727       // add volume
728       SMDS_MeshVolume* aV = 0;
729       switch ( nbFaceNodes ) {
730       case 3:
731         if ( forward ) {
732           //aV = meshDS->AddVolume(aN[0], aN[1], aN[2],
733           //                       aN[3], aN[4], aN[5]);
734           aV = myTool->AddVolume(aN[0], aN[1], aN[2], aN[3], aN[4], aN[5]);
735         }
736         else {
737           //aV = meshDS->AddVolume(aN[0], aN[2], aN[1],
738           //                       aN[3], aN[5], aN[4]);
739           aV = myTool->AddVolume(aN[0], aN[2], aN[1], aN[3], aN[5], aN[4]);
740         }
741         break;
742       case 4:
743         if ( forward ) {
744           //aV = meshDS->AddVolume(aN[0], aN[1], aN[2], aN[3],
745           //                       aN[4], aN[5], aN[6], aN[7]);
746           aV = myTool->AddVolume(aN[0], aN[1], aN[2], aN[3],
747                                  aN[4], aN[5], aN[6], aN[7]);
748         }
749         else {
750           //aV = meshDS->AddVolume(aN[0], aN[3], aN[2], aN[1],
751           //                       aN[4], aN[7], aN[6], aN[5]);
752           aV = myTool->AddVolume(aN[0], aN[3], aN[2], aN[1],
753                                  aN[4], aN[7], aN[6], aN[5]);
754         }
755         break;
756       default:
757         continue;
758       }
759       meshDS->SetMeshElementOnShape(aV, shapeID);
760     }
761   }
762 }
763
764 //=======================================================================
765 //function : MakeMeshOnFxy1
766 //purpose  : 
767 //=======================================================================
768 void StdMeshers_Penta_3D::MakeMeshOnFxy1()
769 {
770   int aID0, aJ, aLevel, ij, aNbNodes, k;
771   //
772   SMDS_NodeIteratorPtr itn;
773   SMDS_ElemIteratorPtr itf, aItNodes;
774   SMDSAbs_ElementType aElementType;
775   //
776   const TopoDS_Face& aFxy0=
777     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy0));
778   const TopoDS_Face& aFxy1=
779     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy1));
780   //
781   SMESH_Mesh* pMesh = GetMesh();
782   SMESHDS_Mesh * meshDS = pMesh->GetMeshDS();
783   //
784   SMESH_subMesh *aSubMesh1 = pMesh->GetSubMeshContaining(aFxy1);
785   SMESH_subMesh *aSubMesh0 = pMesh->GetSubMeshContaining(aFxy0);
786   SMESHDS_SubMesh *aSM0 = aSubMesh0->GetSubMeshDS();
787   //
788   // set nodes on aFxy1
789   aLevel = myISize-1;
790   itn = aSM0->GetNodes();
791   aNbNodes = aSM0->NbNodes();
792   //printf("** aNbNodes=%d\n", aNbNodes);
793   myTool->SetSubShape( aFxy1 ); // to set medium nodes to aFxy1
794   //
795   // set elements on aFxy1
796   vector<const SMDS_MeshNode*> aNodes1;
797   //
798   itf = aSM0->GetElements();
799   while(itf->more()) {
800     const SMDS_MeshElement* pE0 = itf->next();
801     aElementType = pE0->GetType();
802     if (!aElementType==SMDSAbs_Face) {
803       continue;
804     }
805     aNbNodes = pE0->NbNodes();
806     if(myCreateQuadratic)
807       aNbNodes = aNbNodes/2;
808     if ( aNodes1.size() < aNbNodes )
809       aNodes1.resize( aNbNodes );
810     //
811     k = aNbNodes-1; // reverse a face
812     aItNodes = pE0->nodesIterator();
813     while (aItNodes->more()) {
814       const SMDS_MeshNode* pNode =
815         static_cast<const SMDS_MeshNode*> (aItNodes->next());
816       if(myTool->IsMedium(pNode))
817         continue;
818       aID0 = pNode->GetID();
819       aJ = GetIndexOnLayer(aID0);
820       if (!myErrorStatus->IsOK()) {
821         MESSAGE("StdMeshers_Penta_3D::MakeMeshOnFxy1() ");
822         return;
823       }
824       //
825       ij = aLevel*myJSize + aJ;
826       const StdMeshers_TNode& aTN1 = myTNodes[ij];
827       const SMDS_MeshNode* aN1 = aTN1.Node();
828       aNodes1[k] = aN1;
829       --k;
830     }
831     SMDS_MeshFace * face = 0;
832     switch ( aNbNodes ) {
833     case 3:
834       face = myTool->AddFace(aNodes1[0], aNodes1[1], aNodes1[2]);
835       break;
836     case 4:
837       face = myTool->AddFace(aNodes1[0], aNodes1[1], aNodes1[2], aNodes1[3]);
838       break;
839     default:
840       continue;
841     }
842     meshDS->SetMeshElementOnShape(face, aFxy1);
843   }
844   myTool->SetSubShape( myShape );
845
846   // update compute state of top face submesh
847   aSubMesh1->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
848
849   // assure that mesh on the top face will be cleaned when it is cleaned
850   // on the bottom face
851   SMESH_subMesh* volSM = pMesh->GetSubMesh( myTool->GetSubShape() );
852   volSM->SetEventListener( new SMESH_subMeshEventListener(true),
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 wich 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() ");
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() ");
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() ");
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 subshape is not compatible
1340       myErrorStatus->myComment="Wrong number of subshapes 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, 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     const SMDS_EdgePosition* pos =
1468       dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition() );
1469     if ( !pos ) {
1470       return false;
1471     }
1472     double u = ( pos->GetUParameter() - f ) / range;
1473     vector<const SMDS_MeshNode*> & nVec = theIJNodes[ u ];
1474     nVec.resize( vsize, nullNode );
1475     loadedNodes.insert( nVec[ 0 ] = node );
1476   }
1477   if ( theIJNodes.size() != hsize ) {
1478     MESSAGE( "Wrong node positions on theBaseEdge" );
1479     return false;
1480   }
1481
1482   // load nodes from e1
1483
1484   map< double, const SMDS_MeshNode*> sortedNodes; // sort by param on edge
1485   nIt = sm1->GetNodes();
1486   while ( nIt->more() ) {
1487     node = nIt->next();
1488     if(myTool->IsMedium(node))
1489       continue;
1490     const SMDS_EdgePosition* pos =
1491       dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition() );
1492     if ( !pos ) {
1493       return false;
1494     }
1495     sortedNodes.insert( make_pair( pos->GetUParameter(), node ));
1496   }
1497   loadedNodes.insert( nVecf[ vsize - 1 ] = smVft->GetNodes()->next() );
1498   map< double, const SMDS_MeshNode*>::iterator u_n = sortedNodes.begin();
1499   int row = rev1 ? vsize - 1 : 0;
1500   for ( ; u_n != sortedNodes.end(); u_n++ ) {
1501     if ( rev1 ) row--;
1502     else        row++;
1503     loadedNodes.insert( nVecf[ row ] = u_n->second );
1504   }
1505
1506   // try to load the rest nodes
1507
1508   // get all faces from theFace
1509   TIDSortedElemSet allFaces, foundFaces;
1510   SMDS_ElemIteratorPtr eIt = smFace->GetElements();
1511   while ( eIt->more() ) {
1512     const SMDS_MeshElement* e = eIt->next();
1513     if ( e->GetType() == SMDSAbs_Face )
1514       allFaces.insert( e );
1515   }
1516   // Starting from 2 neighbour nodes on theBaseEdge, look for a face
1517   // the nodes belong to, and between the nodes of the found face,
1518   // look for a not loaded node considering this node to be the next
1519   // in a column of the starting second node. Repeat, starting
1520   // from nodes next to the previous starting nodes in their columns,
1521   // and so on while a face can be found. Then go the the next pair
1522   // of nodes on theBaseEdge.
1523   StdMeshers_IJNodeMap::iterator par_nVec_1 = theIJNodes.begin();
1524   StdMeshers_IJNodeMap::iterator par_nVec_2 = par_nVec_1;
1525   // loop on columns
1526   int col = 0;
1527   for ( par_nVec_2++; par_nVec_2 != theIJNodes.end(); par_nVec_1++, par_nVec_2++ ) {
1528     col++;
1529     row = 0;
1530     const SMDS_MeshNode* n1 = par_nVec_1->second[ row ];
1531     const SMDS_MeshNode* n2 = par_nVec_2->second[ row ];
1532     const SMDS_MeshElement* face = 0;
1533     do {
1534       // look for a face by 2 nodes
1535       face = SMESH_MeshEditor::FindFaceInSet( n1, n2, allFaces, foundFaces );
1536       if ( face ) {
1537         int nbFaceNodes = face->NbNodes();
1538         if ( (!myCreateQuadratic && nbFaceNodes>4) ||
1539              (myCreateQuadratic && nbFaceNodes>8) ) {
1540           MESSAGE(" Too many nodes in a face: " << nbFaceNodes );
1541           return false;
1542         }
1543         // look for a not loaded node of the <face>
1544         bool found = false;
1545         const SMDS_MeshNode* n3 = 0; // a node defferent from n1 and n2
1546         eIt = face->nodesIterator() ;
1547         while ( !found && eIt->more() ) {
1548           node = static_cast<const SMDS_MeshNode*>( eIt->next() );
1549           if(myTool->IsMedium(node))
1550             continue;
1551           found = loadedNodes.insert( node ).second;
1552           if ( !found && node != n1 && node != n2 )
1553             n3 = node;
1554         }
1555         if ( found ) {
1556           if ( ++row > vsize - 1 ) {
1557             MESSAGE( "Too many nodes in column "<< col <<": "<< row+1);
1558             return false;
1559           }
1560           par_nVec_2->second[ row ] = node;
1561           foundFaces.insert( face );
1562           n2 = node;
1563           if ( nbFaceNodes==4 || (myCreateQuadratic && nbFaceNodes==8) ) {
1564             n1 = par_nVec_1->second[ row ];
1565           }
1566         }
1567         else if ( (nbFaceNodes==3 || (myCreateQuadratic && nbFaceNodes==6) )  &&
1568                   n3 == par_nVec_1->second[ row ] ) {
1569           n1 = n3;
1570         }
1571         else {
1572           MESSAGE( "Not quad mesh, column "<< col );
1573           return false;
1574         }
1575       }
1576     }
1577     while ( face && n1 && n2 );
1578
1579     if ( row < vsize - 1 ) {
1580       MESSAGE( "Too few nodes in column "<< col <<": "<< row+1);
1581       MESSAGE( "Base node 1: "<< par_nVec_1->second[0]);
1582       MESSAGE( "Base node 2: "<< par_nVec_2->second[0]);
1583       MESSAGE( "Current node 1: "<< n1);
1584       MESSAGE( "Current node 2: "<< n2);
1585       MESSAGE( "first base node: "<< theIJNodes.begin()->second[0]);
1586       MESSAGE( "last base node: "<< theIJNodes.rbegin()->second[0]);
1587       return false;
1588     }
1589   } // loop on columns
1590
1591   return true;
1592 }
1593
1594 //////////////////////////////////////////////////////////////////////////
1595 //
1596 //   StdMeshers_SMESHBlock
1597 //
1598 //////////////////////////////////////////////////////////////////////////
1599
1600 //=======================================================================
1601 //function : StdMeshers_SMESHBlock
1602 //purpose  : 
1603 //=======================================================================
1604 StdMeshers_SMESHBlock::StdMeshers_SMESHBlock()
1605 {
1606   myErrorStatus=1;
1607   myIsEdgeForward.resize( SMESH_Block::NbEdges(), -1 );
1608 }
1609
1610 //=======================================================================
1611 //function : IsForwadEdge
1612 //purpose  : 
1613 //=======================================================================
1614
1615 bool StdMeshers_SMESHBlock::IsForwadEdge(const int theEdgeID)
1616 {
1617   int index = myTBlock.ShapeIndex( theEdgeID );
1618   if ( !myTBlock.IsEdgeID( theEdgeID ))
1619     return false;
1620
1621   if ( myIsEdgeForward[ index ] < 0 )
1622     myIsEdgeForward[ index ] =
1623       myTBlock.IsForwardEdge( TopoDS::Edge( Shape( theEdgeID )), myShapeIDMap );
1624
1625   return myIsEdgeForward[ index ];
1626 }
1627
1628 //=======================================================================
1629 //function : ErrorStatus
1630 //purpose  : 
1631 //=======================================================================
1632 int StdMeshers_SMESHBlock::ErrorStatus() const
1633 {
1634   return myErrorStatus;
1635 }
1636
1637 //================================================================================
1638 /*!
1639  * \brief Return problem description
1640  */
1641 //================================================================================
1642
1643 SMESH_ComputeErrorPtr StdMeshers_SMESHBlock::GetError() const
1644 {
1645   SMESH_ComputeErrorPtr err = SMESH_ComputeError::New();
1646   string & text = err->myComment;
1647   switch ( myErrorStatus ) {
1648   case 2:
1649   case 3: text = "Internal error of StdMeshers_Penta_3D"; break; 
1650   case 4: text = "Can't compute normalized parameters of a point inside a block"; break;
1651   case 5: text = "Can't compute coordinates by normalized parameters inside a block"; break;
1652   case 6: text = "Can't detect block subshapes. Not a block?"; break;
1653   }
1654   if (!text.empty())
1655     err->myName = myErrorStatus;
1656   return err;
1657 }
1658
1659 //=======================================================================
1660 //function : Load
1661 //purpose  : 
1662 //=======================================================================
1663 void StdMeshers_SMESHBlock::Load(const TopoDS_Shell& theShell)
1664 {
1665   TopoDS_Vertex aV000, aV001;
1666   //
1667   Load(theShell, aV000, aV001);
1668 }
1669
1670 //=======================================================================
1671 //function : Load
1672 //purpose  : 
1673 //=======================================================================
1674 void StdMeshers_SMESHBlock::Load(const TopoDS_Shell& theShell,
1675                                  const TopoDS_Vertex& theV000,
1676                                  const TopoDS_Vertex& theV001)
1677 {
1678   myErrorStatus=0;
1679   //
1680   myShell=theShell;
1681   //
1682   bool bOk;
1683   //
1684   myShapeIDMap.Clear();  
1685   bOk = myTBlock.LoadBlockShapes(myShell, theV000, theV001, myShapeIDMap);
1686   if (!bOk) {
1687     myErrorStatus=6;
1688     return;
1689   }
1690 }
1691
1692 //=======================================================================
1693 //function : ComputeParameters
1694 //purpose  : 
1695 //=======================================================================
1696 void StdMeshers_SMESHBlock::ComputeParameters(const gp_Pnt& thePnt, 
1697                                               gp_XYZ& theXYZ)
1698 {
1699   ComputeParameters(thePnt, myShell, theXYZ);
1700 }
1701
1702 //=======================================================================
1703 //function : ComputeParameters
1704 //purpose  : 
1705 //=======================================================================
1706 void StdMeshers_SMESHBlock::ComputeParameters(const gp_Pnt& thePnt,
1707                                               const TopoDS_Shape& theShape,
1708                                               gp_XYZ& theXYZ)
1709 {
1710   myErrorStatus=0;
1711   //
1712   int aID;
1713   bool bOk;
1714   //
1715   aID = ShapeID(theShape);
1716   if (myErrorStatus) {
1717     return;
1718   }
1719   bOk = myTBlock.ComputeParameters(thePnt, theXYZ, aID);
1720   if (!bOk) {
1721     myErrorStatus=4; // problems with computation Parameters 
1722     return;
1723   }
1724 }
1725
1726 //=======================================================================
1727 //function : ComputeParameters
1728 //purpose  : 
1729 //=======================================================================
1730
1731 void StdMeshers_SMESHBlock::ComputeParameters(const double& theU,
1732                                               const TopoDS_Shape& theShape,
1733                                               gp_XYZ& theXYZ)
1734 {
1735   myErrorStatus=0;
1736   //
1737   int aID;
1738   bool bOk=false;
1739   //
1740   aID = ShapeID(theShape);
1741   if (myErrorStatus) {
1742     return;
1743   }
1744   if ( SMESH_Block::IsEdgeID( aID ))
1745     bOk = myTBlock.EdgeParameters( aID, theU, theXYZ );
1746   if (!bOk) {
1747     myErrorStatus=4; // problems with computation Parameters 
1748     return;
1749   }
1750 }
1751
1752 //=======================================================================
1753 //function : Point
1754 //purpose  : 
1755 //=======================================================================
1756 void StdMeshers_SMESHBlock::Point(const gp_XYZ& theParams, gp_Pnt& aP3D)
1757 {
1758   TopoDS_Shape aS;
1759   //
1760   Point(theParams, aS, aP3D);
1761 }
1762
1763 //=======================================================================
1764 //function : Point
1765 //purpose  : 
1766 //=======================================================================
1767 void StdMeshers_SMESHBlock::Point(const gp_XYZ& theParams,
1768                                   const TopoDS_Shape& theShape,
1769                                   gp_Pnt& aP3D)
1770 {
1771   myErrorStatus = 0;
1772   //
1773   int aID;
1774   bool bOk = false;
1775   gp_XYZ aXYZ(99.,99.,99.);
1776   aP3D.SetXYZ(aXYZ);
1777   //
1778   if (theShape.IsNull()) {
1779     bOk = myTBlock.ShellPoint(theParams, aXYZ);
1780   }
1781   //
1782   else {
1783     aID=ShapeID(theShape);
1784     if (myErrorStatus) {
1785       return;
1786     }
1787     //
1788     if (SMESH_Block::IsVertexID(aID)) {
1789       bOk = myTBlock.VertexPoint(aID, aXYZ);
1790     }
1791     else if (SMESH_Block::IsEdgeID(aID)) {
1792       bOk = myTBlock.EdgePoint(aID, theParams, aXYZ);
1793     }
1794     //
1795     else if (SMESH_Block::IsFaceID(aID)) {
1796       bOk = myTBlock.FacePoint(aID, theParams, aXYZ);
1797     }
1798   }
1799   if (!bOk) {
1800     myErrorStatus=5; // problems with point computation 
1801     return;
1802   }
1803   aP3D.SetXYZ(aXYZ);
1804 }
1805
1806 //=======================================================================
1807 //function : ShapeID
1808 //purpose  : 
1809 //=======================================================================
1810 int StdMeshers_SMESHBlock::ShapeID(const TopoDS_Shape& theShape)
1811 {
1812   myErrorStatus=0;
1813   //
1814   int aID=-1;
1815   TopoDS_Shape aSF, aSR;
1816   //
1817   aSF=theShape;
1818   aSF.Orientation(TopAbs_FORWARD);
1819   aSR=theShape;
1820   aSR.Orientation(TopAbs_REVERSED);
1821   //
1822   if (myShapeIDMap.Contains(aSF)) {
1823     aID=myShapeIDMap.FindIndex(aSF);
1824     return aID;
1825   }
1826   if (myShapeIDMap.Contains(aSR)) {
1827     aID=myShapeIDMap.FindIndex(aSR);
1828     return aID;
1829   }
1830   myErrorStatus=2; // unknown shape;
1831   return aID;
1832 }
1833
1834 //=======================================================================
1835 //function : Shape
1836 //purpose  : 
1837 //=======================================================================
1838 const TopoDS_Shape& StdMeshers_SMESHBlock::Shape(const int theID)
1839 {
1840   myErrorStatus=0;
1841   //
1842   int aNb;
1843   //
1844   aNb=myShapeIDMap.Extent();
1845   if (theID<1 || theID>aNb) {
1846     myErrorStatus=3; // ID is out of range
1847     return myEmptyShape;
1848   }
1849   //
1850   const TopoDS_Shape& aS=myShapeIDMap.FindKey(theID);
1851   return aS;
1852 }
1853
1854
1855 //=======================================================================
1856 //function : Evaluate
1857 //purpose  : 
1858 //=======================================================================
1859 bool StdMeshers_Penta_3D::Evaluate(SMESH_Mesh& aMesh, 
1860                                    const TopoDS_Shape& aShape,
1861                                    MapShapeNbElems& aResMap)
1862 {
1863   MESSAGE("StdMeshers_Penta_3D::Evaluate()");
1864
1865   // find face contains only triangles
1866   vector < SMESH_subMesh * >meshFaces;
1867   TopTools_SequenceOfShape aFaces;
1868   int NumBase = 0, i = 0;
1869   for (TopExp_Explorer exp(aShape, TopAbs_FACE); exp.More(); exp.Next()) {
1870     i++;
1871     aFaces.Append(exp.Current());
1872     SMESH_subMesh *aSubMesh = aMesh.GetSubMesh(exp.Current());
1873     meshFaces.push_back(aSubMesh);
1874     MapShapeNbElemsItr anIt = aResMap.find(meshFaces[i]);
1875     if( anIt == aResMap.end() ) {
1876       NumBase = 0;
1877       break;
1878     }
1879     std::vector<int> aVec = (*anIt).second;
1880     int nbtri = Max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
1881     int nbqua = Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1882     if( nbtri>0 && nbqua==0 ) {
1883       NumBase = i;
1884     }
1885   }
1886
1887   if(NumBase==0) {
1888     std::vector<int> aResVec(SMDSEntity_Last);
1889     for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1890     SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1891     aResMap.insert(std::make_pair(sm,aResVec));
1892     myErrorStatus->myName    = COMPERR_ALGO_FAILED;
1893     myErrorStatus->myComment = "Submesh can not be evaluated";
1894     return false;
1895   }
1896
1897   // find number of 1d elems for base face
1898   int nb1d = 0;
1899   TopTools_MapOfShape Edges1;
1900   for (TopExp_Explorer exp(aFaces.Value(NumBase), TopAbs_EDGE); exp.More(); exp.Next()) {
1901     Edges1.Add(exp.Current());
1902     SMESH_subMesh *sm = aMesh.GetSubMesh(exp.Current());
1903     if( sm ) {
1904       MapShapeNbElemsItr anIt = aResMap.find(sm);
1905       if( anIt == aResMap.end() ) continue;
1906       std::vector<int> aVec = (*anIt).second;
1907       nb1d += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
1908     }
1909   }
1910   // find face opposite to base face
1911   int OppNum = 0;
1912   for(i=1; i<=6; i++) {
1913     if(i==NumBase) continue;
1914     bool IsOpposite = true;
1915     for(TopExp_Explorer exp(aFaces.Value(i), TopAbs_EDGE); exp.More(); exp.Next()) {
1916       if( Edges1.Contains(exp.Current()) ) {
1917         IsOpposite = false;
1918         break;
1919       }
1920     }
1921     if(IsOpposite) {
1922       OppNum = i;
1923       break;
1924     }
1925   }
1926   // find number of 2d elems on side faces
1927   int nb2d = 0;
1928   for(i=1; i<=6; i++) {
1929     if( i==OppNum || i==NumBase ) continue;
1930     MapShapeNbElemsItr anIt = aResMap.find( meshFaces[i-1] );
1931     if( anIt == aResMap.end() ) continue;
1932     std::vector<int> aVec = (*anIt).second;
1933     nb2d += Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1934   }
1935
1936   MapShapeNbElemsItr anIt = aResMap.find( meshFaces[NumBase-1] );
1937   std::vector<int> aVec = (*anIt).second;
1938   int nb2d_face0 = Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1939   int nb0d_face0 = aVec[SMDSEntity_Node];
1940
1941   anIt = aResMap.find( meshFaces[OppNum-1] );
1942   for(i=SMDSEntity_Node; i<SMDSEntity_Last; i++)
1943     (*anIt).second[i] = aVec[i];
1944
1945   SMESH_MesherHelper aTool (aMesh);
1946   bool _quadraticMesh = aTool.IsQuadraticSubMesh(aShape);
1947
1948   std::vector<int> aResVec(SMDSEntity_Last);
1949   for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1950   if(_quadraticMesh) {
1951     aResVec[SMDSEntity_Quad_Penta] = nb2d_face0 * ( nb2d/nb1d );
1952     aResVec[SMDSEntity_Node] = nb0d_face0 * ( 2*nb2d/nb1d - 1 );
1953   }
1954   else {
1955     aResVec[SMDSEntity_Node] = nb0d_face0 * ( nb2d/nb1d - 1 );
1956     aResVec[SMDSEntity_Penta] = nb2d_face0 * ( nb2d/nb1d );
1957   }
1958   SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1959   aResMap.insert(std::make_pair(sm,aResVec));
1960
1961   return true;
1962 }
1963