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