Salome HOME
Fix compilation problems on Mandriva64 and Mandriva
[modules/smesh.git] / src / StdMeshers / StdMeshers_Prism_3D.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 // File      : StdMeshers_Prism_3D.cxx
25 // Module    : SMESH
26 // Created   : Fri Oct 20 11:37:07 2006
27 // Author    : Edward AGAPOV (eap)
28
29
30 #include "StdMeshers_Prism_3D.hxx"
31
32 #include "StdMeshers_ProjectionUtils.hxx"
33 #include "SMESH_MeshEditor.hxx"
34 #include "SMDS_VolumeTool.hxx"
35 #include "SMDS_VolumeOfNodes.hxx"
36 #include "SMDS_EdgePosition.hxx"
37 #include "SMESH_Comment.hxx"
38
39 #include "utilities.h"
40
41 #include <BRepAdaptor_Curve.hxx>
42 #include <BRepAdaptor_Curve2d.hxx>
43 #include <BRep_Builder.hxx>
44 #include <BRep_Tool.hxx>
45 #include <Geom2dAdaptor_Curve.hxx>
46 #include <Geom2d_Line.hxx>
47 #include <TopTools_ListIteratorOfListOfShape.hxx>
48
49 using namespace std;
50
51 #define RETURN_BAD_RESULT(msg) { MESSAGE(msg); return false; }
52 #define gpXYZ(n) gp_XYZ(n->X(),n->Y(),n->Z())
53 #define SHOWYXZ(msg, xyz) // {\
54 // gp_Pnt p (xyz); \
55 // cout << msg << " ("<< p.X() << "; " <<p.Y() << "; " <<p.Z() << ") " <<endl;\
56 // }
57
58 typedef StdMeshers_ProjectionUtils TAssocTool;
59 typedef SMESH_Comment              TCom;
60
61 enum { ID_BOT_FACE = SMESH_Block::ID_Fxy0,
62        ID_TOP_FACE = SMESH_Block::ID_Fxy1,
63        BOTTOM_EDGE = 0, TOP_EDGE, V0_EDGE, V1_EDGE, // edge IDs in face
64        NB_WALL_FACES = 4 }; //
65
66 namespace {
67
68   //================================================================================
69   /*!
70    * \brief Return iterator pointing to node column for the given parameter
71    * \param columnsMap - node column map
72    * \param parameter - parameter
73    * \retval TParam2ColumnMap::iterator - result
74    *
75    * it returns closest left column
76    */
77   //================================================================================
78
79   TParam2ColumnIt getColumn( const TParam2ColumnMap* columnsMap,
80                              const double            parameter )
81   {
82     TParam2ColumnIt u_col = columnsMap->upper_bound( parameter );
83     if ( u_col != columnsMap->begin() )
84       --u_col;
85     return u_col; // return left column
86   }
87
88   //================================================================================
89   /*!
90    * \brief Return nodes around given parameter and a ratio
91    * \param column - node column
92    * \param param - parameter
93    * \param node1 - lower node
94    * \param node2 - upper node
95    * \retval double - ratio
96    */
97   //================================================================================
98
99   double getRAndNodes( const TNodeColumn*     column,
100                        const double           param,
101                        const SMDS_MeshNode* & node1,
102                        const SMDS_MeshNode* & node2)
103   {
104     if ( param == 1.0 || column->size() == 1) {
105       node1 = node2 = column->back();
106       return 0;
107     }
108
109     int i = int( param * ( column->size() - 1 ));
110     double u0 = double( i )/ double( column->size() - 1 );
111     double r = ( param - u0 ) * ( column->size() - 1 );
112
113     node1 = (*column)[ i ];
114     node2 = (*column)[ i + 1];
115     return r;
116   }
117
118   //================================================================================
119   /*!
120    * \brief Compute boundary parameters of face parts
121     * \param nbParts - nb of parts to split columns into
122     * \param columnsMap - node columns of the face to split
123     * \param params - computed parameters
124    */
125   //================================================================================
126
127   void splitParams( const int               nbParts,
128                     const TParam2ColumnMap* columnsMap,
129                     vector< double > &      params)
130   {
131     params.clear();
132     params.reserve( nbParts + 1 );
133     TParam2ColumnIt last_par_col = --columnsMap->end();
134     double par = columnsMap->begin()->first; // 0.
135     double parLast = last_par_col->first;
136     params.push_back( par );
137     for ( int i = 0; i < nbParts - 1; ++ i )
138     {
139       double partSize = ( parLast - par ) / double ( nbParts - i );
140       TParam2ColumnIt par_col = getColumn( columnsMap, par + partSize );
141       if ( par_col->first == par ) {
142         ++par_col;
143         if ( par_col == last_par_col ) {
144           while ( i < nbParts - 1 )
145             params.push_back( par + partSize * i++ );
146           break;
147         }
148       }
149       par = par_col->first;
150       params.push_back( par );
151     }
152     params.push_back( parLast ); // 1.
153   }
154 }
155
156 //=======================================================================
157 //function : StdMeshers_Prism_3D
158 //purpose  : 
159 //=======================================================================
160
161 StdMeshers_Prism_3D::StdMeshers_Prism_3D(int hypId, int studyId, SMESH_Gen* gen)
162   :SMESH_3D_Algo(hypId, studyId, gen)
163 {
164   _name = "Prism_3D";
165   _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);       // 1 bit per shape type
166   myProjectTriangles = false;
167 }
168
169 //================================================================================
170 /*!
171  * \brief Destructor
172  */
173 //================================================================================
174
175 StdMeshers_Prism_3D::~StdMeshers_Prism_3D()
176 {}
177
178 //=======================================================================
179 //function : CheckHypothesis
180 //purpose  : 
181 //=======================================================================
182
183 bool StdMeshers_Prism_3D::CheckHypothesis(SMESH_Mesh&                          aMesh,
184                                           const TopoDS_Shape&                  aShape,
185                                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
186 {
187   // Check shape geometry
188
189   aStatus = SMESH_Hypothesis::HYP_BAD_GEOMETRY;
190
191   // find not quadrangle faces
192   list< TopoDS_Shape > notQuadFaces;
193   int nbEdge, nbWire, nbFace = 0;
194   TopExp_Explorer exp( aShape, TopAbs_FACE );
195   for ( ; exp.More(); exp.Next() ) {
196     ++nbFace;
197     const TopoDS_Shape& face = exp.Current();
198     nbEdge = TAssocTool::Count( face, TopAbs_EDGE, 0 );
199     nbWire = TAssocTool::Count( face, TopAbs_WIRE, 0 );
200     if (  nbEdge!= 4 || nbWire!= 1 ) {
201       if ( !notQuadFaces.empty() ) {
202         if ( TAssocTool::Count( notQuadFaces.back(), TopAbs_EDGE, 0 ) != nbEdge ||
203              TAssocTool::Count( notQuadFaces.back(), TopAbs_WIRE, 0 ) != nbWire )
204           RETURN_BAD_RESULT("Different not quad faces");
205       }
206       notQuadFaces.push_back( face );
207     }
208   }
209   if ( !notQuadFaces.empty() )
210   {
211     if ( notQuadFaces.size() != 2 )
212       RETURN_BAD_RESULT("Bad nb not quad faces: " << notQuadFaces.size());
213
214     // check total nb faces
215     nbEdge = TAssocTool::Count( notQuadFaces.back(), TopAbs_EDGE, 0 );
216     if ( nbFace != nbEdge + 2 )
217       RETURN_BAD_RESULT("Bad nb of faces: " << nbFace << " but must be " << nbEdge + 2);
218   }
219
220   // no hypothesis
221   aStatus = SMESH_Hypothesis::HYP_OK;
222   return true;
223 }
224
225 //=======================================================================
226 //function : Compute
227 //purpose  : 
228 //=======================================================================
229
230 bool StdMeshers_Prism_3D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
231 {
232   SMESH_MesherHelper helper( theMesh );
233   myHelper = &helper;
234
235   myHelper->IsQuadraticSubMesh( theShape );
236
237   // Analyse mesh and geomerty to find block subshapes and submeshes
238   if ( !myBlock.Init( myHelper, theShape ))
239     return error( myBlock.GetError());
240
241   SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
242
243   int volumeID = meshDS->ShapeToIndex( theShape );
244
245
246   // To compute coordinates of a node inside a block, it is necessary to know
247   // 1. normalized parameters of the node by which
248   // 2. coordinates of node projections on all block sub-shapes are computed
249
250   // So we fill projections on vertices at once as they are same for all nodes
251   myShapeXYZ.resize( myBlock.NbSubShapes() );
252   for ( int iV = SMESH_Block::ID_FirstV; iV < SMESH_Block::ID_FirstE; ++iV ) {
253     myBlock.VertexPoint( iV, myShapeXYZ[ iV ]);
254     SHOWYXZ("V point " <<iV << " ", myShapeXYZ[ iV ]);
255   }
256
257   // Projections on the top and bottom faces are taken from nodes existing
258   // on these faces; find correspondence between bottom and top nodes
259   myBotToColumnMap.clear();
260   if ( !assocOrProjBottom2Top() ) // it also fill myBotToColumnMap
261     return false;
262
263
264   // Create nodes inside the block
265
266   // loop on nodes inside the bottom face
267   TNode2ColumnMap::iterator bot_column = myBotToColumnMap.begin();
268   for ( ; bot_column != myBotToColumnMap.end(); ++bot_column )
269   {
270     const TNode& tBotNode = bot_column->first; // bottom TNode
271     if ( tBotNode.GetPositionType() != SMDS_TOP_FACE )
272       continue; // node is not inside face 
273
274     // column nodes; middle part of the column are zero pointers
275     TNodeColumn& column = bot_column->second;
276
277     // bottom node parameters and coords
278     gp_XYZ botParams          = tBotNode.GetParams();
279     myShapeXYZ[ ID_BOT_FACE ] = tBotNode.GetCoords();
280
281     // compute top node parameters
282     gp_XYZ topParams;
283     myShapeXYZ[ ID_TOP_FACE ] = gpXYZ( column.back() );
284     gp_Pnt topCoords = myShapeXYZ[ ID_TOP_FACE ];
285     if ( !myBlock.ComputeParameters( topCoords, topParams, ID_TOP_FACE ))
286       return error(dfltErr(),TCom("Can't compute normalized parameters ")
287                    << "for node " << column.back()->GetID()
288                    << " on the face #"<< column.back()->GetPosition()->GetShapeId() );
289
290     // vertical loop
291     TNodeColumn::iterator columnNodes = column.begin();
292     for ( int z = 0; columnNodes != column.end(); ++columnNodes, ++z)
293     {
294       const SMDS_MeshNode* & node = *columnNodes;
295       if ( node ) continue; // skip bottom or top node
296
297       // params of a node to create
298       double rz = (double) z / (double) ( column.size() - 1 );
299       gp_XYZ params = botParams * ( 1 - rz ) + topParams * rz;
300
301       // set coords on all faces and nodes
302       const int nbSideFaces = 4;
303       int sideFaceIDs[nbSideFaces] = { SMESH_Block::ID_Fx0z,
304                                        SMESH_Block::ID_Fx1z,
305                                        SMESH_Block::ID_F0yz,
306                                        SMESH_Block::ID_F1yz };
307       for ( int iF = 0; iF < nbSideFaces; ++iF )
308         if ( !setFaceAndEdgesXYZ( sideFaceIDs[ iF ], params, z ))
309           return false;
310
311       // compute coords for a new node
312       gp_XYZ coords;
313       if ( !SMESH_Block::ShellPoint( params, myShapeXYZ, coords ))
314         return error(dfltErr(),"Can't compute coordinates by normalized parameters");
315
316       // create a node
317       node = meshDS->AddNode( coords.X(), coords.Y(), coords.Z() );
318       meshDS->SetNodeInVolume( node, volumeID );
319     }
320   } // loop on bottom nodes
321
322
323   // Create volumes
324
325   SMESHDS_SubMesh* smDS = myBlock.SubMeshDS( ID_BOT_FACE );
326   if ( !smDS ) return error(COMPERR_BAD_INPUT_MESH, "Null submesh");
327
328   // loop on bottom mesh faces
329   SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
330   while ( faceIt->more() )
331   {
332     const SMDS_MeshElement* face = faceIt->next();
333     if ( !face || face->GetType() != SMDSAbs_Face )
334       continue;
335     int nbNodes = face->NbNodes();
336     if ( face->IsQuadratic() )
337       nbNodes /= 2;
338
339     // find node columns for each node
340     vector< const TNodeColumn* > columns( nbNodes );
341     for ( int i = 0; i < nbNodes; ++i )
342     {
343       const SMDS_MeshNode* n = face->GetNode( i );
344       if ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
345         bot_column = myBotToColumnMap.find( n );
346         if ( bot_column == myBotToColumnMap.end() )
347           return error(dfltErr(),TCom("No nodes found above node ") << n->GetID() );
348         columns[ i ] = & bot_column->second;
349       }
350       else {
351         columns[ i ] = myBlock.GetNodeColumn( n );
352         if ( !columns[ i ] )
353           return error(dfltErr(),TCom("No side nodes found above node ") << n->GetID() );
354       }
355     }
356     // create prisms
357     AddPrisms( columns, myHelper );
358
359   } // loop on bottom mesh faces
360         
361   return true;
362 }
363
364 //================================================================================
365 /*!
366  * \brief Create prisms
367  * \param columns - columns of nodes generated from nodes of a mesh face
368  * \param helper - helper initialized by mesh and shape to add prisms to
369  */
370 //================================================================================
371
372 void StdMeshers_Prism_3D::AddPrisms( vector<const TNodeColumn*> & columns,
373                                      SMESH_MesherHelper*          helper)
374 {
375   SMESHDS_Mesh * meshDS = helper->GetMeshDS();
376   int shapeID = helper->GetSubShapeID();
377
378   int nbNodes = columns.size();
379
380   // vertical loop on columns
381   for ( int z = 1; z < columns[0]->size(); ++z)
382   {
383     SMDS_MeshElement* vol = 0;
384     SMDS_VolumeTool vTool;
385     switch ( nbNodes ) {
386
387     case 3: {
388       const SMDS_MeshNode* botNodes[3] = { (*columns[0])[z-1],
389                                            (*columns[1])[z-1],
390                                            (*columns[2])[z-1] };
391       const SMDS_MeshNode* topNodes[3] = { (*columns[0])[z],
392                                            (*columns[1])[z],
393                                            (*columns[2])[z] };
394       // assure good orientation
395       SMDS_VolumeOfNodes tmpVol ( botNodes[0], botNodes[1], botNodes[2],
396                                   topNodes[0], topNodes[1], topNodes[2]);
397       vTool.Set( &tmpVol );
398       if ( vTool.IsForward() )
399         vol = helper->AddVolume( botNodes[0], botNodes[1], botNodes[2],
400                                  topNodes[0], topNodes[1], topNodes[2]);
401       else
402         vol = helper->AddVolume( topNodes[0], topNodes[1], topNodes[2],
403                                  botNodes[0], botNodes[1], botNodes[2]);
404       break;
405       }
406     case 4: {
407       const SMDS_MeshNode* botNodes[4] = { (*columns[0])[z-1], (*columns[1])[z-1],
408                                            (*columns[2])[z-1], (*columns[3])[z-1] };
409       const SMDS_MeshNode* topNodes[4] = { (*columns[0])[z], (*columns[1])[z],
410                                            (*columns[2])[z], (*columns[3])[z] };
411       // assure good orientation
412       SMDS_VolumeOfNodes tmpVol ( botNodes[0], botNodes[1], botNodes[2], botNodes[3],
413                                   topNodes[0], topNodes[1], topNodes[2], topNodes[3]);
414       vTool.Set( &tmpVol );
415       if ( vTool.IsForward() )
416         vol = helper->AddVolume( botNodes[0], botNodes[1], botNodes[2], botNodes[3],
417                                  topNodes[0], topNodes[1], topNodes[2], topNodes[3]);
418       else
419         vol = helper->AddVolume( topNodes[0], topNodes[1], topNodes[2], topNodes[3],
420                                  botNodes[0], botNodes[1], botNodes[2], botNodes[3]);
421       break;
422       }
423     default:
424       // polyhedron
425       vector<const SMDS_MeshNode*> nodes( 2*nbNodes + 4*nbNodes);
426       vector<int> quantities( 2 + nbNodes, 4 );
427       quantities[0] = quantities[1] = nbNodes;
428       columns.resize( nbNodes + 1 );
429       columns[ nbNodes ] = columns[ 0 ];
430       for ( int i = 0; i < nbNodes; ++i ) {
431         nodes[ i         ] = (*columns[ i ])[z-1]; // bottom
432         nodes[ i+nbNodes ] = (*columns[ i ])[z  ]; // top
433         // side
434         int di = 2*nbNodes + 4*i - 1;
435         nodes[ di   ] = (*columns[i  ])[z-1];
436         nodes[ di+1 ] = (*columns[i+1])[z-1];
437         nodes[ di+2 ] = (*columns[i+1])[z  ];
438         nodes[ di+3 ] = (*columns[i  ])[z  ];
439       }
440       vol = meshDS->AddPolyhedralVolume( nodes, quantities );
441     }
442     if ( vol && shapeID > 0 )
443       meshDS->SetMeshElementOnShape( vol, shapeID );
444   }
445 }
446
447 //================================================================================
448 /*!
449  * \brief Find correspondence between bottom and top nodes
450  *  If elements on the bottom and top faces are topologically different,
451  *  and projection is possible and allowed, perform the projection
452  *  \retval bool - is a success or not
453  */
454 //================================================================================
455
456 bool StdMeshers_Prism_3D::assocOrProjBottom2Top()
457 {
458   SMESH_subMesh * botSM = myBlock.SubMesh( ID_BOT_FACE );
459   SMESH_subMesh * topSM = myBlock.SubMesh( ID_TOP_FACE );
460
461   SMESHDS_SubMesh * botSMDS = botSM->GetSubMeshDS();
462   SMESHDS_SubMesh * topSMDS = topSM->GetSubMeshDS();
463
464   if ( !botSMDS || botSMDS->NbElements() == 0 )
465     return error(dfltErr(),TCom("No elememts on face #") << botSM->GetId());
466
467   bool needProject = false;
468   if ( !topSMDS || 
469        botSMDS->NbElements() != topSMDS->NbElements() ||
470        botSMDS->NbNodes()    != topSMDS->NbNodes())
471   {
472     if ( myBlock.HasNotQuadElemOnTop() )
473       return error(dfltErr(),TCom("Mesh on faces #") << botSM->GetId()
474                    <<" and #"<< topSM->GetId() << " seems different" );
475     needProject = true;
476   }
477
478   if ( 0/*needProject && !myProjectTriangles*/ )
479     return error(dfltErr(),TCom("Mesh on faces #") << botSM->GetId()
480                  <<" and #"<< topSM->GetId() << " seems different" );
481   ///RETURN_BAD_RESULT("Need to project but not allowed");
482
483   if ( needProject )
484   {
485     return projectBottomToTop();
486   }
487
488   TopoDS_Face botFace = TopoDS::Face( myBlock.Shape( ID_BOT_FACE ));
489   TopoDS_Face topFace = TopoDS::Face( myBlock.Shape( ID_TOP_FACE ));
490   // associate top and bottom faces
491   TAssocTool::TShapeShapeMap shape2ShapeMap;
492   if ( !TAssocTool::FindSubShapeAssociation( botFace, myBlock.Mesh(),
493                                              topFace, myBlock.Mesh(),
494                                              shape2ShapeMap) )
495     return error(dfltErr(),TCom("Topology of faces #") << botSM->GetId()
496                  <<" and #"<< topSM->GetId() << " seems different" );
497
498   // Find matching nodes of top and bottom faces
499   TNodeNodeMap n2nMap;
500   if ( ! TAssocTool::FindMatchingNodesOnFaces( botFace, myBlock.Mesh(),
501                                                topFace, myBlock.Mesh(),
502                                                shape2ShapeMap, n2nMap ))
503     return error(dfltErr(),TCom("Mesh on faces #") << botSM->GetId()
504                  <<" and #"<< topSM->GetId() << " seems different" );
505
506   // Fill myBotToColumnMap
507
508   int zSize = myBlock.VerticalSize();
509   TNodeNodeMap::iterator bN_tN = n2nMap.begin();
510   for ( ; bN_tN != n2nMap.end(); ++bN_tN )
511   {
512     const SMDS_MeshNode* botNode = bN_tN->first;
513     const SMDS_MeshNode* topNode = bN_tN->second;
514     if ( botNode->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE )
515       continue; // wall columns are contained in myBlock
516     // compute bottom node params
517     TNode bN( botNode );
518     if ( !myBlock.ComputeParameters( bN.GetCoords(), bN.ChangeParams(), ID_BOT_FACE ))
519       return error(dfltErr(),TCom("Can't compute normalized parameters ")
520                    << "for node " << botNode->GetID() << " on the face #"<< botSM->GetId() );
521     // create node column
522     TNode2ColumnMap::iterator bN_col = 
523       myBotToColumnMap.insert( make_pair ( bN, TNodeColumn() )).first;
524     TNodeColumn & column = bN_col->second;
525     column.resize( zSize );
526     column.front() = botNode;
527     column.back()  = topNode;
528   }
529   return true;
530 }
531
532 //================================================================================
533 /*!
534  * \brief Remove quadrangles from the top face and
535  * create triangles there by projection from the bottom
536  * \retval bool - a success or not
537  */
538 //================================================================================
539
540 bool StdMeshers_Prism_3D::projectBottomToTop()
541 {
542   SMESH_subMesh * botSM = myBlock.SubMesh( ID_BOT_FACE );
543   SMESH_subMesh * topSM = myBlock.SubMesh( ID_TOP_FACE );
544
545   SMESHDS_SubMesh * botSMDS = botSM->GetSubMeshDS();
546   SMESHDS_SubMesh * topSMDS = topSM->GetSubMeshDS();
547
548   if ( topSMDS )
549     topSM->ComputeStateEngine( SMESH_subMesh::CLEAN );
550
551   SMESHDS_Mesh* meshDS = myBlock.MeshDS();
552   int shapeID = myHelper->GetSubShapeID();
553   int topFaceID = meshDS->ShapeToIndex( topSM->GetSubShape() );
554
555   // Fill myBotToColumnMap
556
557   int zSize = myBlock.VerticalSize();
558   SMDS_NodeIteratorPtr nIt = botSMDS->GetNodes();
559   while ( nIt->more() )
560   {
561     const SMDS_MeshNode* botNode = nIt->next();
562     if ( botNode->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE )
563       continue; // strange
564     // compute bottom node params
565     TNode bN( botNode );
566     if ( !myBlock.ComputeParameters( bN.GetCoords(), bN.ChangeParams(), ID_BOT_FACE ))
567       return error(dfltErr(),TCom("Can't compute normalized parameters ")
568                    << "for node " << botNode->GetID() << " on the face #"<< botSM->GetId() );
569     // compute top node coords
570     gp_XYZ topXYZ; gp_XY topUV;
571     if ( !myBlock.FacePoint( ID_TOP_FACE, bN.GetParams(), topXYZ ) ||
572          !myBlock.FaceUV   ( ID_TOP_FACE, bN.GetParams(), topUV ))
573       return error(dfltErr(),TCom("Can't compute coordinates ")
574                    << "by normalized parameters on the face #"<< topSM->GetId() );
575     SMDS_MeshNode * topNode = meshDS->AddNode( topXYZ.X(),topXYZ.Y(),topXYZ.Z() );
576     meshDS->SetNodeOnFace( topNode, topFaceID, topUV.X(), topUV.Y() );
577     // create node column
578     TNode2ColumnMap::iterator bN_col = 
579       myBotToColumnMap.insert( make_pair ( bN, TNodeColumn() )).first;
580     TNodeColumn & column = bN_col->second;
581     column.resize( zSize );
582     column.front() = botNode;
583     column.back()  = topNode;
584   }
585
586   // Create top faces
587
588   // loop on bottom mesh faces
589   SMDS_ElemIteratorPtr faceIt = botSMDS->GetElements();
590   while ( faceIt->more() )
591   {
592     const SMDS_MeshElement* face = faceIt->next();
593     if ( !face || face->GetType() != SMDSAbs_Face )
594       continue;
595     int nbNodes = face->NbNodes();
596     if ( face->IsQuadratic() )
597       nbNodes /= 2;
598
599     // find top node in columns for each bottom node
600     vector< const SMDS_MeshNode* > nodes( nbNodes );
601     for ( int i = 0; i < nbNodes; ++i )
602     {
603       const SMDS_MeshNode* n = face->GetNode( nbNodes - i - 1 );
604       if ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
605         TNode2ColumnMap::iterator bot_column = myBotToColumnMap.find( n );
606         if ( bot_column == myBotToColumnMap.end() )
607           return error(dfltErr(),TCom("No nodes found above node ") << n->GetID() );
608         nodes[ i ] = bot_column->second.back();
609       }
610       else {
611         const TNodeColumn* column = myBlock.GetNodeColumn( n );
612         if ( !column )
613           return error(dfltErr(),TCom("No side nodes found above node ") << n->GetID() );
614         nodes[ i ] = column->back();
615       }
616     }
617     // create a face, with reversed orientation
618     SMDS_MeshElement* newFace = 0;
619     switch ( nbNodes ) {
620
621     case 3: {
622       newFace = myHelper->AddFace(nodes[0], nodes[1], nodes[2]);
623       break;
624       }
625     case 4: {
626       newFace = myHelper->AddFace( nodes[0], nodes[1], nodes[2], nodes[3] );
627       break;
628       }
629     default:
630       newFace = meshDS->AddPolygonalFace( nodes );
631     }
632     if ( newFace && shapeID > 0 )
633       meshDS->SetMeshElementOnShape( newFace, shapeID );
634   }
635
636   return true;
637 }
638
639 //================================================================================
640 /*!
641  * \brief Set projection coordinates of a node to a face and it's subshapes
642  * \param faceID - the face given by in-block ID
643  * \param params - node normalized parameters
644  * \retval bool - is a success
645  */
646 //================================================================================
647
648 bool StdMeshers_Prism_3D::setFaceAndEdgesXYZ( const int faceID, const gp_XYZ& params, int z )
649 {
650   // find base and top edges of the face
651   enum { BASE = 0, TOP, LEFT, RIGHT };
652   vector< int > edgeVec; // 0-base, 1-top
653   SMESH_Block::GetFaceEdgesIDs( faceID, edgeVec );
654
655   myBlock.EdgePoint( edgeVec[ BASE ], params, myShapeXYZ[ edgeVec[ BASE ]]);
656   myBlock.EdgePoint( edgeVec[ TOP ], params, myShapeXYZ[ edgeVec[ TOP ]]);
657
658   SHOWYXZ("\nparams ", params);
659   SHOWYXZ("TOP is "<<edgeVec[ TOP], myShapeXYZ[ edgeVec[ TOP]]);
660   SHOWYXZ("BASE is "<<edgeVec[ BASE], myShapeXYZ[ edgeVec[ BASE]]);
661
662   if ( faceID == SMESH_Block::ID_Fx0z || faceID == SMESH_Block::ID_Fx1z )
663   {
664     myBlock.EdgePoint( edgeVec[ LEFT ], params, myShapeXYZ[ edgeVec[ LEFT ]]);
665     myBlock.EdgePoint( edgeVec[ RIGHT ], params, myShapeXYZ[ edgeVec[ RIGHT ]]);
666
667     SHOWYXZ("VER "<<edgeVec[ LEFT], myShapeXYZ[ edgeVec[ LEFT]]);
668     SHOWYXZ("VER "<<edgeVec[ RIGHT], myShapeXYZ[ edgeVec[ RIGHT]]);
669   }
670   myBlock.FacePoint( faceID, params, myShapeXYZ[ faceID ]);
671   SHOWYXZ("FacePoint "<<faceID, myShapeXYZ[ faceID]);
672
673   return true;
674 }
675
676 //================================================================================
677 /*!
678  * \brief Constructor. Initialization is needed
679  */
680 //================================================================================
681
682 StdMeshers_PrismAsBlock::StdMeshers_PrismAsBlock()
683 {
684   mySide = 0;
685 }
686
687 StdMeshers_PrismAsBlock::~StdMeshers_PrismAsBlock()
688 {
689   if ( mySide ) {
690     delete mySide; mySide = 0;
691   }
692 }
693
694 //================================================================================
695 /*!
696  * \brief Initialization.
697  * \param helper - helper loaded with mesh and 3D shape
698  * \param shape3D - a closed shell or solid
699  * \retval bool - false if a mesh or a shape are KO
700  */
701 //================================================================================
702
703 bool StdMeshers_PrismAsBlock::Init(SMESH_MesherHelper* helper,
704                                    const TopoDS_Shape& shape3D)
705 {
706   if ( mySide ) {
707     delete mySide; mySide = 0;
708   }
709   vector< TSideFace* > sideFaces( NB_WALL_FACES, 0 );
710   vector< pair< double, double> > params ( NB_WALL_FACES );
711   mySide = new TSideFace( sideFaces, params );
712
713   myHelper = helper;
714   SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
715
716   SMESH_Block::init();
717   myShapeIDMap.Clear();
718   myShapeIndex2ColumnMap.clear();
719   
720   int wallFaceIds[ NB_WALL_FACES ] = { // to walk around a block
721     SMESH_Block::ID_Fx0z, SMESH_Block::ID_F1yz,
722     SMESH_Block::ID_Fx1z, SMESH_Block::ID_F0yz
723   };
724
725   myError = SMESH_ComputeError::New();
726
727   // -------------------------------------------------------------
728   // Look for top and bottom faces: not quadrangle ones or meshed
729   // with not quadrangle elements
730   // -------------------------------------------------------------
731
732   list< SMESH_subMesh* > notQuadGeomSubMesh;
733   list< SMESH_subMesh* > notQuadElemSubMesh;
734   int nbFaces = 0;
735   //
736   SMESH_subMesh* mainSubMesh = myHelper->GetMesh()->GetSubMeshContaining( shape3D );
737   if ( !mainSubMesh ) return error(COMPERR_BAD_INPUT_MESH,"Null submesh of shape3D");
738
739   // analyse face submeshes
740   SMESH_subMeshIteratorPtr smIt = mainSubMesh->getDependsOnIterator(false,false);
741   while ( smIt->more() )
742   {
743     SMESH_subMesh* sm = smIt->next();
744     const TopoDS_Shape& face = sm->GetSubShape();
745     if ( face.ShapeType() != TopAbs_FACE )
746       continue;
747     nbFaces++;
748
749     // is quadrangle face?
750     list< TopoDS_Edge > orderedEdges;
751     list< int >         nbEdgesInWires;
752     TopoDS_Vertex       V000;
753     int nbWires = GetOrderedEdges( TopoDS::Face( face ),
754                                    V000, orderedEdges, nbEdgesInWires );
755     if ( nbWires != 1 || nbEdgesInWires.front() != 4 )
756       notQuadGeomSubMesh.push_back( sm );
757
758     // look for not quadrangle mesh elements
759     if ( SMESHDS_SubMesh* smDS = sm->GetSubMeshDS() ) {
760       bool hasNotQuad = false;
761       SMDS_ElemIteratorPtr eIt = smDS->GetElements();
762       while ( eIt->more() && !hasNotQuad ) {
763         const SMDS_MeshElement* elem = eIt->next();
764         if ( elem->GetType() == SMDSAbs_Face ) {
765           int nbNodes = elem->NbNodes();
766           if ( elem->IsQuadratic() )
767             nbNodes /= 2;
768           hasNotQuad = ( nbNodes != 4 );
769         }
770       }
771       if ( hasNotQuad )
772         notQuadElemSubMesh.push_back( sm );
773     }
774     else {
775       return error(COMPERR_BAD_INPUT_MESH,TCom("Not meshed face #")<<sm->GetId());
776     }
777     // check if a quadrangle face is meshed with a quadranglar grid
778     if ( notQuadGeomSubMesh.back() != sm &&
779          notQuadElemSubMesh.back() != sm )
780     {
781       // count nb edges on face sides
782       vector< int > nbEdges;
783       nbEdges.reserve( nbEdgesInWires.front() );
784       for ( list< TopoDS_Edge >::iterator edge = orderedEdges.begin();
785             edge != orderedEdges.end(); ++edge )
786       {
787         if ( SMESHDS_SubMesh* smDS = meshDS->MeshElements( *edge ))
788           nbEdges.push_back ( smDS->NbElements() );
789         else
790           nbEdges.push_back ( 0 );
791       }
792       int nbQuads = sm->GetSubMeshDS()->NbElements();
793       if ( nbEdges[0] *  nbEdges[1] != nbQuads ||
794            nbEdges[0] != nbEdges[2] ||
795            nbEdges[1] != nbEdges[3] )
796         notQuadElemSubMesh.push_back( sm );
797     }
798   }
799
800   // ----------------------------------------------------------------------
801   // Analyse faces mesh and topology: choose the bottom submesh.
802   // If there are not quadrangle geom faces, they are top and bottom ones.
803   // Not quadrangle geom faces must be only on top and bottom.
804   // ----------------------------------------------------------------------
805
806   SMESH_subMesh * botSM = 0;
807   SMESH_subMesh * topSM = 0;
808
809   int nbNotQuad       = notQuadGeomSubMesh.size();
810   int nbNotQuadMeshed = notQuadElemSubMesh.size();
811   bool hasNotQuad = ( nbNotQuad || nbNotQuadMeshed );
812
813   // detect bad cases
814   if ( nbNotQuad > 0 && nbNotQuad != 2 )
815     return error(COMPERR_BAD_SHAPE,
816                  TCom("More than 2 not quadrilateral faces")
817                  <<nbNotQuad);
818   if ( nbNotQuadMeshed > 2 )
819     return error(COMPERR_BAD_INPUT_MESH,
820                  TCom("More then 2 faces meshed with not quadrangle elements")
821                  <<nbNotQuadMeshed);
822
823   // get found submeshes
824   if ( hasNotQuad )
825   {
826     if ( nbNotQuadMeshed > 0 ) botSM = notQuadElemSubMesh.front();
827     else                       botSM = notQuadGeomSubMesh.front();
828     if ( nbNotQuadMeshed > 1 ) topSM = notQuadElemSubMesh.back();
829     else if ( nbNotQuad  > 1 ) topSM = notQuadGeomSubMesh.back();
830   }
831   // detect other bad cases
832   if ( nbNotQuad == 2 && nbNotQuadMeshed > 0 ) {
833     bool ok = false;
834     if ( nbNotQuadMeshed == 1 )
835       ok = ( find( notQuadGeomSubMesh.begin(),
836                    notQuadGeomSubMesh.end(), botSM ) != notQuadGeomSubMesh.end() );
837     else
838       ok = ( notQuadGeomSubMesh == notQuadElemSubMesh );
839     if ( !ok )
840       return error(COMPERR_BAD_INPUT_MESH, "Side face meshed with not quadrangle elements");
841   }
842
843   myNotQuadOnTop = ( nbNotQuadMeshed > 1 );
844  
845   // ----------------------------------------------------------
846
847   if ( nbNotQuad == 0 ) // Standard block of 6 quadrangle faces ?
848   {
849     // SMESH_Block will perform geometry analysis, we need just to find 2
850     // connected vertices on top and bottom
851
852     TopoDS_Vertex Vbot, Vtop;
853     if ( nbNotQuadMeshed > 0 ) // Look for vertices
854     {
855       TopTools_IndexedMapOfShape edgeMap;
856       TopExp::MapShapes( botSM->GetSubShape(), TopAbs_EDGE, edgeMap );
857       // vertex 1 is any vertex of the bottom face
858       Vbot = TopExp::FirstVertex( TopoDS::Edge( edgeMap( 1 )));
859       // vertex 2 is end vertex of edge sharing Vbot and not belonging to the bottom face
860       TopTools_ListIteratorOfListOfShape ancestIt = Mesh()->GetAncestors( Vbot );
861       for ( ; Vtop.IsNull() && ancestIt.More(); ancestIt.Next() )
862       {
863         const TopoDS_Shape & ancestor = ancestIt.Value();
864         if ( ancestor.ShapeType() == TopAbs_EDGE && !edgeMap.FindIndex( ancestor ))
865         {
866           TopoDS_Vertex V1, V2;
867           TopExp::Vertices( TopoDS::Edge( ancestor ), V1, V2);
868           if      ( Vbot.IsSame ( V1 )) Vtop = V2;
869           else if ( Vbot.IsSame ( V2 )) Vtop = V1;
870           // check that Vtop belongs to shape3D
871           TopExp_Explorer exp( shape3D, TopAbs_VERTEX );
872           for ( ; exp.More(); exp.Next() )
873             if ( Vtop.IsSame( exp.Current() ))
874               break;
875           if ( !exp.More() )
876             Vtop.Nullify();
877         }
878       }
879     }
880     // get shell from shape3D
881     TopoDS_Shell shell;
882     TopExp_Explorer exp( shape3D, TopAbs_SHELL );
883     int nbShell = 0;
884     for ( ; exp.More(); exp.Next(), ++nbShell )
885       shell = TopoDS::Shell( exp.Current() );
886 //     if ( nbShell != 1 )
887 //       RETURN_BAD_RESULT("There must be 1 shell in the block");
888
889     // Load geometry in SMESH_Block
890     if ( !SMESH_Block::FindBlockShapes( shell, Vbot, Vtop, myShapeIDMap )) {
891       if ( !hasNotQuad )
892         return error(COMPERR_BAD_SHAPE, "Can't detect top and bottom of a prism");
893     }
894     else {
895       if ( !botSM ) botSM = Mesh()->GetSubMeshContaining( myShapeIDMap( ID_BOT_FACE ));
896       if ( !topSM ) topSM = Mesh()->GetSubMeshContaining( myShapeIDMap( ID_TOP_FACE ));
897     }
898
899   } // end  Standard block of 6 quadrangle faces
900   // --------------------------------------------------------
901
902   // Here the top and bottom faces are found
903   if ( nbNotQuadMeshed == 2 ) // roughly check correspondence of horiz meshes
904   {
905 //     SMESHDS_SubMesh* topSMDS = topSM->GetSubMeshDS();
906 //     SMESHDS_SubMesh* botSMDS = botSM->GetSubMeshDS();
907 //     if ( topSMDS->NbNodes() != botSMDS->NbNodes() ||
908 //          topSMDS->NbElements() != botSMDS->NbElements() )
909 //       RETURN_BAD_RESULT("Top mesh doesn't correspond to bottom one");
910   }
911
912   // ---------------------------------------------------------
913   // If there are not quadrangle geom faces, we emulate
914   // a block of 6 quadrangle faces.
915   // Load SMESH_Block with faces and edges geometry
916   // ---------------------------------------------------------
917
918   
919   // find vertex 000 - the one with smallest coordinates (for easy DEBUG :-)
920   TopoDS_Vertex V000;
921   double minVal = DBL_MAX, minX, val;
922   for ( TopExp_Explorer exp( botSM->GetSubShape(), TopAbs_VERTEX );
923         exp.More(); exp.Next() )
924   {
925     const TopoDS_Vertex& v = TopoDS::Vertex( exp.Current() );
926     gp_Pnt P = BRep_Tool::Pnt( v );
927     val = P.X() + P.Y() + P.Z();
928     if ( val < minVal || ( val == minVal && P.X() < minX )) {
929       V000 = v;
930       minVal = val;
931       minX = P.X();
932     }
933   }
934
935   // Get ordered bottom edges
936   list< TopoDS_Edge > orderedEdges;
937   list< int >         nbVertexInWires;
938   SMESH_Block::GetOrderedEdges( TopoDS::Face( botSM->GetSubShape().Reversed() ),
939                                 V000, orderedEdges, nbVertexInWires );
940 //   if ( nbVertexInWires.size() != 1 )
941 //     RETURN_BAD_RESULT("Wrong prism geometry");
942
943   // Get Wall faces corresponding to the ordered bottom edges
944   list< TopoDS_Face > wallFaces;
945   if ( !GetWallFaces( Mesh(), shape3D, botSM->GetSubShape(), orderedEdges, wallFaces))
946     return error(COMPERR_BAD_SHAPE, "Can't find side faces");
947
948   // Find columns of wall nodes and calculate edges' lengths
949   // --------------------------------------------------------
950
951   myParam2ColumnMaps.clear();
952   myParam2ColumnMaps.resize( orderedEdges.size() ); // total nb edges
953
954   int iE, nbEdges = nbVertexInWires.front(); // nb outer edges
955   vector< double > edgeLength( nbEdges );
956   map< double, int > len2edgeMap;
957
958   list< TopoDS_Edge >::iterator edgeIt = orderedEdges.begin();
959   list< TopoDS_Face >::iterator faceIt = wallFaces.begin();
960   for ( iE = 0; iE < nbEdges; ++edgeIt, ++faceIt )
961   {
962     TParam2ColumnMap & faceColumns = myParam2ColumnMaps[ iE ];
963     if ( !myHelper->LoadNodeColumns( faceColumns, *faceIt, *edgeIt, meshDS ))
964       return error(COMPERR_BAD_INPUT_MESH, TCom("Can't find regular quadrangle mesh ")
965                    << "on a side face #" << MeshDS()->ShapeToIndex( *faceIt ));
966
967     SHOWYXZ("\np1 F "<<iE, gpXYZ(faceColumns.begin()->second.front() ));
968     SHOWYXZ("p2 F "<<iE, gpXYZ(faceColumns.rbegin()->second.front() ));
969     SHOWYXZ("V First "<<iE, BRep_Tool::Pnt( TopExp::FirstVertex(*edgeIt,true )));
970
971     edgeLength[ iE ] = SMESH_Algo::EdgeLength( *edgeIt );
972
973     if ( nbEdges < NB_WALL_FACES ) // fill map used to split faces
974     {
975       SMESHDS_SubMesh* smDS = meshDS->MeshElements( *edgeIt);
976       if ( !smDS )
977         return error(COMPERR_BAD_INPUT_MESH, TCom("Null submesh on the edge #")
978                      << MeshDS()->ShapeToIndex( *edgeIt ));
979       // assure length uniqueness
980       edgeLength[ iE ] *= smDS->NbNodes() + edgeLength[ iE ] / ( 1000 + iE );
981       len2edgeMap[ edgeLength[ iE ]] = iE;
982     }
983     ++iE;
984   }
985   // Load columns of internal edges (forming holes)
986   // and fill map ShapeIndex to TParam2ColumnMap for them
987   for ( ; edgeIt != orderedEdges.end() ; ++edgeIt, ++faceIt )
988   {
989     TParam2ColumnMap & faceColumns = myParam2ColumnMaps[ iE ];
990     if ( !myHelper->LoadNodeColumns( faceColumns, *faceIt, *edgeIt, meshDS ))
991       return error(COMPERR_BAD_INPUT_MESH, TCom("Can't find regular quadrangle mesh ")
992                    << "on a side face #" << MeshDS()->ShapeToIndex( *faceIt ));
993     // edge columns
994     int id = MeshDS()->ShapeToIndex( *edgeIt );
995     bool isForward = true; // meaningless for intenal wires
996     myShapeIndex2ColumnMap[ id ] = make_pair( & faceColumns, isForward );
997     // columns for vertices
998     // 1
999     const SMDS_MeshNode* n0 = faceColumns.begin()->second.front();
1000     id = n0->GetPosition()->GetShapeId();
1001     myShapeIndex2ColumnMap[ id ] = make_pair( & faceColumns, isForward );
1002     // 2
1003     const SMDS_MeshNode* n1 = faceColumns.rbegin()->second.front();
1004     id = n1->GetPosition()->GetShapeId();
1005     myShapeIndex2ColumnMap[ id ] = make_pair( & faceColumns, isForward );
1006 //     SHOWYXZ("\np1 F "<<iE, gpXYZ(faceColumns.begin()->second.front() ));
1007 //     SHOWYXZ("p2 F "<<iE, gpXYZ(faceColumns.rbegin()->second.front() ));
1008 //     SHOWYXZ("V First "<<iE, BRep_Tool::Pnt( TopExp::FirstVertex(*edgeIt,true )));
1009     ++iE;
1010   }
1011
1012   // Create 4 wall faces of a block
1013   // -------------------------------
1014
1015   if ( nbEdges <= NB_WALL_FACES ) // ************* Split faces if necessary
1016   {
1017     map< int, int > iE2nbSplit;
1018     if ( nbEdges != NB_WALL_FACES ) // define how to split
1019     {
1020       if ( len2edgeMap.size() != nbEdges )
1021         RETURN_BAD_RESULT("Uniqueness of edge lengths not assured");
1022       map< double, int >::reverse_iterator maxLen_i = len2edgeMap.rbegin();
1023       map< double, int >::reverse_iterator midLen_i = ++len2edgeMap.rbegin();
1024       double maxLen = maxLen_i->first;
1025       double midLen = ( len2edgeMap.size() == 1 ) ? 0 : midLen_i->first;
1026       switch ( nbEdges ) {
1027       case 1: // 0-th edge is split into 4 parts
1028         iE2nbSplit.insert( make_pair( 0, 4 )); break;
1029       case 2: // either the longest edge is split into 3 parts, or both edges into halves
1030         if ( maxLen / 3 > midLen / 2 ) {
1031           iE2nbSplit.insert( make_pair( maxLen_i->second, 3 ));
1032         }
1033         else {
1034           iE2nbSplit.insert( make_pair( maxLen_i->second, 2 ));
1035           iE2nbSplit.insert( make_pair( midLen_i->second, 2 ));
1036         }
1037         break;
1038       case 3:
1039         // split longest into halves
1040         iE2nbSplit.insert( make_pair( maxLen_i->second, 2 ));
1041       }
1042     }
1043     // Create TSideFace's
1044     faceIt = wallFaces.begin();
1045     edgeIt = orderedEdges.begin();
1046     int iSide = 0;
1047     for ( iE = 0; iE < nbEdges; ++edgeIt, ++faceIt )
1048     {
1049      // split?
1050       map< int, int >::iterator i_nb = iE2nbSplit.find( iE );
1051       if ( i_nb != iE2nbSplit.end() ) {
1052         // split!
1053         int nbSplit = i_nb->second;
1054         vector< double > params;
1055         splitParams( nbSplit, &myParam2ColumnMaps[ iE ], params );
1056         bool isForward = ( edgeIt->Orientation() == TopAbs_FORWARD );
1057         for ( int i = 0; i < nbSplit; ++i ) {
1058           double f = ( isForward ? params[ i ] : params[ nbSplit - i-1 ]);
1059           double l = ( isForward ? params[ i+1 ] : params[ nbSplit - i ]);
1060           TSideFace* comp = new TSideFace( myHelper, wallFaceIds[ iSide ],
1061                                            *faceIt, *edgeIt,
1062                                            &myParam2ColumnMaps[ iE ], f, l );
1063           mySide->SetComponent( iSide++, comp );
1064         }
1065       }
1066       else {
1067         TSideFace* comp = new TSideFace( myHelper, wallFaceIds[ iSide ],
1068                                          *faceIt, *edgeIt,
1069                                          &myParam2ColumnMaps[ iE ]);
1070         mySide->SetComponent( iSide++, comp );
1071       }
1072       ++iE;
1073     }
1074   }
1075   else { // **************************** Unite faces
1076
1077     // unite first faces
1078     int nbExraFaces = nbEdges - 3;
1079     int iSide = 0, iE;
1080     double u0 = 0, sumLen = 0;
1081     for ( iE = 0; iE < nbExraFaces; ++iE )
1082       sumLen += edgeLength[ iE ];
1083
1084     vector< TSideFace* > components( nbExraFaces );
1085     vector< pair< double, double> > params( nbExraFaces );
1086     faceIt = wallFaces.begin();
1087     edgeIt = orderedEdges.begin();
1088     for ( iE = 0; iE < nbExraFaces; ++edgeIt, ++faceIt )
1089     {
1090       components[ iE ] = new TSideFace( myHelper, wallFaceIds[ iSide ],
1091                                         *faceIt, *edgeIt,
1092                                         &myParam2ColumnMaps[ iE ]);
1093       double u1 = u0 + edgeLength[ iE ] / sumLen;
1094       params[ iE ] = make_pair( u0 , u1 );
1095       u0 = u1;
1096       ++iE;
1097     }
1098     mySide->SetComponent( iSide++, new TSideFace( components, params ));
1099
1100     // fill the rest faces
1101     for ( ; iE < nbEdges; ++faceIt, ++edgeIt )
1102     {
1103       TSideFace* comp = new TSideFace( myHelper, wallFaceIds[ iSide ],
1104                                        *faceIt, *edgeIt,
1105                                        &myParam2ColumnMaps[ iE ]);
1106       mySide->SetComponent( iSide++, comp );
1107       ++iE;
1108     }
1109   }
1110
1111
1112   // Fill geometry fields of SMESH_Block
1113   // ------------------------------------
1114
1115   TopoDS_Face botF = TopoDS::Face( botSM->GetSubShape() );
1116   TopoDS_Face topF = TopoDS::Face( topSM->GetSubShape() );
1117
1118   vector< int > botEdgeIdVec;
1119   SMESH_Block::GetFaceEdgesIDs( ID_BOT_FACE, botEdgeIdVec );
1120
1121   bool isForward[NB_WALL_FACES] = { true, true, true, true };
1122   Adaptor2d_Curve2d* botPcurves[NB_WALL_FACES];
1123   Adaptor2d_Curve2d* topPcurves[NB_WALL_FACES];
1124
1125   for ( int iF = 0; iF < NB_WALL_FACES; ++iF )
1126   {
1127     TSideFace * sideFace = mySide->GetComponent( iF );
1128     if ( !sideFace )
1129       RETURN_BAD_RESULT("NULL TSideFace");
1130     int fID = sideFace->FaceID();
1131
1132     // fill myShapeIDMap
1133     if ( sideFace->InsertSubShapes( myShapeIDMap ) != 8 &&
1134          !sideFace->IsComplex())
1135       MESSAGE( ": Warning : InsertSubShapes() < 8 on side " << iF );
1136
1137     // side faces geometry
1138     Adaptor2d_Curve2d* pcurves[NB_WALL_FACES];
1139     if ( !sideFace->GetPCurves( pcurves ))
1140       RETURN_BAD_RESULT("TSideFace::GetPCurves() failed");
1141
1142     SMESH_Block::TFace& tFace = myFace[ fID - ID_FirstF ];
1143     tFace.Set( fID, sideFace->Surface(), pcurves, isForward );
1144
1145     SHOWYXZ( endl<<"F "<< iF << " id " << fID << " FRW " << sideFace->IsForward(), );
1146     // edges 3D geometry
1147     vector< int > edgeIdVec;
1148     SMESH_Block::GetFaceEdgesIDs( fID, edgeIdVec );
1149     for ( int isMax = 0; isMax < 2; ++isMax ) {
1150       {
1151         int eID = edgeIdVec[ isMax ];
1152         SMESH_Block::TEdge& tEdge = myEdge[ eID - ID_FirstE ];
1153         tEdge.Set( eID, sideFace->HorizCurve(isMax), true);
1154         SHOWYXZ(eID<<" HOR"<<isMax<<"(0)", sideFace->HorizCurve(isMax)->Value(0));
1155         SHOWYXZ(eID<<" HOR"<<isMax<<"(1)", sideFace->HorizCurve(isMax)->Value(1));
1156       }
1157       {
1158         int eID = edgeIdVec[ isMax+2 ];
1159         SMESH_Block::TEdge& tEdge = myEdge[ eID - ID_FirstE  ];
1160         tEdge.Set( eID, sideFace->VertiCurve(isMax), true);
1161         SHOWYXZ(eID<<" VER"<<isMax<<"(0)", sideFace->VertiCurve(isMax)->Value(0));
1162         SHOWYXZ(eID<<" VER"<<isMax<<"(1)", sideFace->VertiCurve(isMax)->Value(1));
1163
1164         // corner points
1165         vector< int > vertexIdVec;
1166         SMESH_Block::GetEdgeVertexIDs( eID, vertexIdVec );
1167         myPnt[ vertexIdVec[0] - ID_FirstV ] = tEdge.GetCurve()->Value(0).XYZ();
1168         myPnt[ vertexIdVec[1] - ID_FirstV ] = tEdge.GetCurve()->Value(1).XYZ();
1169       }
1170     }
1171     // pcurves on horizontal faces
1172     for ( iE = 0; iE < NB_WALL_FACES; ++iE ) {
1173       if ( edgeIdVec[ BOTTOM_EDGE ] == botEdgeIdVec[ iE ] ) {
1174         botPcurves[ iE ] = sideFace->HorizPCurve( false, botF );
1175         topPcurves[ iE ] = sideFace->HorizPCurve( true,  topF );
1176         break;
1177       }
1178     }
1179   }
1180   // horizontal faces geometry
1181   {
1182     SMESH_Block::TFace& tFace = myFace[ ID_BOT_FACE - ID_FirstF ];
1183     tFace.Set( ID_BOT_FACE, new BRepAdaptor_Surface( botF ), botPcurves, isForward );
1184     SMESH_Block::Insert( botF, ID_BOT_FACE, myShapeIDMap );
1185   }
1186   {
1187     SMESH_Block::TFace& tFace = myFace[ ID_TOP_FACE - ID_FirstF ];
1188     tFace.Set( ID_TOP_FACE, new BRepAdaptor_Surface( topF ), topPcurves, isForward );
1189     SMESH_Block::Insert( topF, ID_TOP_FACE, myShapeIDMap );
1190   }
1191
1192   // Fill map ShapeIndex to TParam2ColumnMap
1193   // ----------------------------------------
1194
1195   list< TSideFace* > fList;
1196   list< TSideFace* >::iterator fListIt;
1197   fList.push_back( mySide );
1198   for ( fListIt = fList.begin(); fListIt != fList.end(); ++fListIt)
1199   {
1200     int nb = (*fListIt)->NbComponents();
1201     for ( int i = 0; i < nb; ++i ) {
1202       if ( TSideFace* comp = (*fListIt)->GetComponent( i ))
1203         fList.push_back( comp );
1204     }
1205     if ( TParam2ColumnMap* cols = (*fListIt)->GetColumns()) {
1206       // columns for a base edge
1207       int id = MeshDS()->ShapeToIndex( (*fListIt)->BaseEdge() );
1208       bool isForward = (*fListIt)->IsForward();
1209       myShapeIndex2ColumnMap[ id ] = make_pair( cols, isForward );
1210
1211       // columns for vertices
1212       const SMDS_MeshNode* n0 = cols->begin()->second.front();
1213       id = n0->GetPosition()->GetShapeId();
1214       myShapeIndex2ColumnMap[ id ] = make_pair( cols, isForward );
1215
1216       const SMDS_MeshNode* n1 = cols->rbegin()->second.front();
1217       id = n1->GetPosition()->GetShapeId();
1218       myShapeIndex2ColumnMap[ id ] = make_pair( cols, !isForward );
1219     }
1220   }
1221
1222 //   gp_XYZ testPar(0.25, 0.25, 0), testCoord;
1223 //   if ( !FacePoint( ID_BOT_FACE, testPar, testCoord ))
1224 //     RETURN_BAD_RESULT("TEST FacePoint() FAILED");
1225 //   SHOWYXZ("IN TEST PARAM" , testPar);
1226 //   SHOWYXZ("OUT TEST CORD" , testCoord);
1227 //   if ( !ComputeParameters( testCoord, testPar , ID_BOT_FACE))
1228 //     RETURN_BAD_RESULT("TEST ComputeParameters() FAILED");
1229 //   SHOWYXZ("OUT TEST PARAM" , testPar);
1230
1231   return true;
1232 }
1233
1234 //================================================================================
1235 /*!
1236  * \brief Return pointer to column of nodes
1237  * \param node - bottom node from which the returned column goes up
1238  * \retval const TNodeColumn* - the found column
1239  */
1240 //================================================================================
1241
1242 const TNodeColumn* StdMeshers_PrismAsBlock::GetNodeColumn(const SMDS_MeshNode* node) const
1243 {
1244   int sID = node->GetPosition()->GetShapeId();
1245
1246   map<int, pair< TParam2ColumnMap*, bool > >::const_iterator col_frw =
1247     myShapeIndex2ColumnMap.find( sID );
1248   if ( col_frw != myShapeIndex2ColumnMap.end() ) {
1249     const TParam2ColumnMap* cols = col_frw->second.first;
1250     TParam2ColumnIt u_col = cols->begin();
1251     for ( ; u_col != cols->end(); ++u_col )
1252       if ( u_col->second[ 0 ] == node )
1253         return & u_col->second;
1254   }
1255   return 0;
1256 }
1257
1258 //================================================================================
1259 /*!
1260  * \brief Check curve orientation of a bootom edge
1261   * \param meshDS - mesh DS
1262   * \param columnsMap - node columns map of side face
1263   * \param bottomEdge - the bootom edge
1264   * \param sideFaceID - side face in-block ID
1265   * \retval bool - true if orientation coinside with in-block froward orientation
1266  */
1267 //================================================================================
1268
1269 bool StdMeshers_PrismAsBlock::IsForwardEdge(SMESHDS_Mesh*           meshDS,
1270                                             const TParam2ColumnMap& columnsMap,
1271                                             const TopoDS_Edge &     bottomEdge,
1272                                             const int               sideFaceID)
1273 {
1274   bool isForward = false;
1275   if ( TAssocTool::IsClosedEdge( bottomEdge ))
1276   {
1277     isForward = ( bottomEdge.Orientation() == TopAbs_FORWARD );
1278   }
1279   else
1280   {
1281     const TNodeColumn& firstCol = columnsMap.begin()->second;
1282     const SMDS_MeshNode* bottomNode = firstCol[0];
1283     TopoDS_Shape firstVertex = SMESH_MesherHelper::GetSubShapeByNode( bottomNode, meshDS );
1284     isForward = ( firstVertex.IsSame( TopExp::FirstVertex( bottomEdge, true )));
1285   }
1286   // on 2 of 4 sides first vertex is end
1287   if ( sideFaceID == ID_Fx1z || sideFaceID == ID_F0yz )
1288     isForward = !isForward;
1289   return isForward;
1290 }
1291
1292 //================================================================================
1293   /*!
1294    * \brief Find wall faces by bottom edges
1295     * \param mesh - the mesh
1296     * \param mainShape - the prism
1297     * \param bottomFace - the bottom face
1298     * \param bottomEdges - edges bounding the bottom face
1299     * \param wallFaces - faces list to fill in
1300    */
1301 //================================================================================
1302
1303 bool StdMeshers_PrismAsBlock::GetWallFaces( SMESH_Mesh*                     mesh,
1304                                             const TopoDS_Shape &            mainShape,
1305                                             const TopoDS_Shape &            bottomFace,
1306                                             const std::list< TopoDS_Edge >& bottomEdges,
1307                                             std::list< TopoDS_Face >&       wallFaces)
1308 {
1309   wallFaces.clear();
1310
1311   TopTools_IndexedMapOfShape faceMap;
1312   TopExp::MapShapes( mainShape, TopAbs_FACE, faceMap );
1313
1314   list< TopoDS_Edge >::const_iterator edge = bottomEdges.begin();
1315   for ( ; edge != bottomEdges.end(); ++edge )
1316   {
1317     TopTools_ListIteratorOfListOfShape ancestIt = mesh->GetAncestors( *edge );
1318     for ( ; ancestIt.More(); ancestIt.Next() )
1319     {
1320       const TopoDS_Shape& ancestor = ancestIt.Value();
1321       if ( ancestor.ShapeType() == TopAbs_FACE && // face
1322            !bottomFace.IsSame( ancestor ) &&      // not bottom
1323            faceMap.FindIndex( ancestor ))         // belongs to the prism
1324       {
1325         wallFaces.push_back( TopoDS::Face( ancestor ));
1326         break;
1327       }
1328     }
1329   }
1330   return ( wallFaces.size() == bottomEdges.size() );
1331 }
1332
1333 //================================================================================
1334 /*!
1335  * \brief Constructor
1336   * \param faceID - in-block ID
1337   * \param face - geom face
1338   * \param columnsMap - map of node columns
1339   * \param first - first normalized param
1340   * \param last - last normalized param
1341  */
1342 //================================================================================
1343
1344 StdMeshers_PrismAsBlock::TSideFace::TSideFace(SMESH_MesherHelper* helper,
1345                                               const int           faceID,
1346                                               const TopoDS_Face&  face,
1347                                               const TopoDS_Edge&  baseEdge,
1348                                               TParam2ColumnMap*   columnsMap,
1349                                               const double        first,
1350                                               const double        last):
1351   myID( faceID ),
1352   myParamToColumnMap( columnsMap ),
1353   myBaseEdge( baseEdge ),
1354   myHelper( helper )
1355 {
1356   mySurface.Initialize( face );
1357   myParams.resize( 1 );
1358   myParams[ 0 ] = make_pair( first, last );
1359   myIsForward = StdMeshers_PrismAsBlock::IsForwardEdge( myHelper->GetMeshDS(),
1360                                                         *myParamToColumnMap,
1361                                                         myBaseEdge, myID );
1362 }
1363
1364 //================================================================================
1365 /*!
1366  * \brief Constructor of complex side face
1367  */
1368 //================================================================================
1369
1370 StdMeshers_PrismAsBlock::TSideFace::
1371 TSideFace(const vector< TSideFace* >&             components,
1372           const vector< pair< double, double> > & params)
1373   :myID( components[0] ? components[0]->myID : 0 ),
1374    myParamToColumnMap( 0 ),
1375    myParams( params ),
1376    myIsForward( true ),
1377    myComponents( components ),
1378    myHelper( components[0] ? components[0]->myHelper : 0 )
1379 {}
1380 //================================================================================
1381 /*!
1382  * \brief Copy constructor
1383   * \param other - other side
1384  */
1385 //================================================================================
1386
1387 StdMeshers_PrismAsBlock::TSideFace::TSideFace( const TSideFace& other )
1388 {
1389   myID               = other.myID;
1390   mySurface          = other.mySurface;
1391   myBaseEdge         = other.myBaseEdge;
1392   myParams           = other.myParams;
1393   myIsForward        = other.myIsForward;
1394   myHelper           = other.myHelper;
1395   myParamToColumnMap = other.myParamToColumnMap;
1396
1397   myComponents.resize( other.myComponents.size());
1398   for (int i = 0 ; i < myComponents.size(); ++i )
1399     myComponents[ i ] = new TSideFace( *other.myComponents[ i ]);
1400 }
1401
1402 //================================================================================
1403 /*!
1404  * \brief Deletes myComponents
1405  */
1406 //================================================================================
1407
1408 StdMeshers_PrismAsBlock::TSideFace::~TSideFace()
1409 {
1410   for (int i = 0 ; i < myComponents.size(); ++i )
1411     if ( myComponents[ i ] )
1412       delete myComponents[ i ];
1413 }
1414
1415 //================================================================================
1416 /*!
1417  * \brief Return geometry of the vertical curve
1418   * \param isMax - true means curve located closer to (1,1,1) block point
1419   * \retval Adaptor3d_Curve* - curve adaptor
1420  */
1421 //================================================================================
1422
1423 Adaptor3d_Curve* StdMeshers_PrismAsBlock::TSideFace::VertiCurve(const bool isMax) const
1424 {
1425   if ( !myComponents.empty() ) {
1426     if ( isMax )
1427       return myComponents.back()->VertiCurve(isMax);
1428     else
1429       return myComponents.front()->VertiCurve(isMax);
1430   }
1431   double f = myParams[0].first, l = myParams[0].second;
1432   if ( !myIsForward ) std::swap( f, l );
1433   return new TVerticalEdgeAdaptor( myParamToColumnMap, isMax ? l : f );
1434 }
1435
1436 //================================================================================
1437 /*!
1438  * \brief Return geometry of the top or bottom curve
1439   * \param isTop - 
1440   * \retval Adaptor3d_Curve* - 
1441  */
1442 //================================================================================
1443
1444 Adaptor3d_Curve* StdMeshers_PrismAsBlock::TSideFace::HorizCurve(const bool isTop) const
1445 {
1446   return new THorizontalEdgeAdaptor( this, isTop );
1447 }
1448
1449 //================================================================================
1450 /*!
1451  * \brief Return pcurves
1452   * \param pcurv - array of 4 pcurves
1453   * \retval bool - is a success
1454  */
1455 //================================================================================
1456
1457 bool StdMeshers_PrismAsBlock::TSideFace::GetPCurves(Adaptor2d_Curve2d* pcurv[4]) const
1458 {
1459   int iEdge[ 4 ] = { BOTTOM_EDGE, TOP_EDGE, V0_EDGE, V1_EDGE };
1460
1461   for ( int i = 0 ; i < 4 ; ++i ) {
1462     Handle(Geom2d_Line) line;
1463     switch ( iEdge[ i ] ) {
1464     case TOP_EDGE:
1465       line = new Geom2d_Line( gp_Pnt2d( 0, 1 ), gp::DX2d() ); break;
1466     case BOTTOM_EDGE:
1467       line = new Geom2d_Line( gp::Origin2d(), gp::DX2d() ); break;
1468     case V0_EDGE:
1469       line = new Geom2d_Line( gp::Origin2d(), gp::DY2d() ); break;
1470     case V1_EDGE:
1471       line = new Geom2d_Line( gp_Pnt2d( 1, 0 ), gp::DY2d() ); break;
1472     }
1473     pcurv[ i ] = new Geom2dAdaptor_Curve( line, 0, 1 );
1474   }
1475   return true;
1476 }
1477
1478 //================================================================================
1479 /*!
1480  * \brief Returns geometry of pcurve on a horizontal face
1481   * \param isTop - is top or bottom face
1482   * \param horFace - a horizontal face
1483   * \retval Adaptor2d_Curve2d* - curve adaptor
1484  */
1485 //================================================================================
1486
1487 Adaptor2d_Curve2d*
1488 StdMeshers_PrismAsBlock::TSideFace::HorizPCurve(const bool         isTop,
1489                                                 const TopoDS_Face& horFace) const
1490 {
1491   return new TPCurveOnHorFaceAdaptor( this, isTop, horFace );
1492 }
1493
1494 //================================================================================
1495 /*!
1496  * \brief Return a component corresponding to parameter
1497   * \param U - parameter along a horizontal size
1498   * \param localU - parameter along a horizontal size of a component
1499   * \retval TSideFace* - found component
1500  */
1501 //================================================================================
1502
1503 StdMeshers_PrismAsBlock::TSideFace*
1504 StdMeshers_PrismAsBlock::TSideFace::GetComponent(const double U,double & localU) const
1505 {
1506   localU = U;
1507   if ( myComponents.empty() )
1508     return const_cast<TSideFace*>( this );
1509
1510   int i;
1511   for ( i = 0; i < myComponents.size(); ++i )
1512     if ( U < myParams[ i ].second )
1513       break;
1514   if ( i >= myComponents.size() )
1515     i = myComponents.size() - 1;
1516
1517   double f = myParams[ i ].first, l = myParams[ i ].second;
1518   localU = ( U - f ) / ( l - f );
1519   return myComponents[ i ];
1520 }
1521
1522 //================================================================================
1523 /*!
1524  * \brief Find node columns for a parameter
1525   * \param U - parameter along a horizontal edge
1526   * \param col1 - the 1st found column
1527   * \param col2 - the 2nd found column
1528   * \retval r - normalized position of U between the found columns
1529  */
1530 //================================================================================
1531
1532 double StdMeshers_PrismAsBlock::TSideFace::GetColumns(const double      U,
1533                                                       TParam2ColumnIt & col1,
1534                                                       TParam2ColumnIt & col2) const
1535 {
1536   double u = U, r = 0;
1537   if ( !myComponents.empty() ) {
1538     TSideFace * comp = GetComponent(U,u);
1539     return comp->GetColumns( u, col1, col2 );
1540   }
1541
1542   if ( !myIsForward )
1543     u = 1 - u;
1544   double f = myParams[0].first, l = myParams[0].second;
1545   u = f + u * ( l - f );
1546
1547   col1 = col2 = getColumn( myParamToColumnMap, u );
1548   if ( ++col2 == myParamToColumnMap->end() ) {
1549     --col2;
1550     r = 0.5;
1551   }
1552   else {
1553 //     if ( !myIsForward )
1554 //       std::swap( col1, col2 );
1555     double uf = col1->first;
1556     double ul = col2->first;
1557     r = ( u - uf ) / ( ul - uf );
1558   }
1559   return r;
1560 }
1561
1562 //================================================================================
1563 /*!
1564  * \brief Return coordinates by normalized params
1565   * \param U - horizontal param
1566   * \param V - vertical param
1567   * \retval gp_Pnt - result point
1568  */
1569 //================================================================================
1570
1571 gp_Pnt StdMeshers_PrismAsBlock::TSideFace::Value(const Standard_Real U,
1572                                                  const Standard_Real V) const
1573 {
1574   double u;
1575   if ( !myComponents.empty() ) {
1576     TSideFace * comp = GetComponent(U,u);
1577     return comp->Value( u, V );
1578   }
1579
1580   TParam2ColumnIt u_col1, u_col2;
1581   double vR, hR = GetColumns( U, u_col1, u_col2 );
1582
1583   const SMDS_MeshNode* n1 = 0;
1584   const SMDS_MeshNode* n2 = 0;
1585   const SMDS_MeshNode* n3 = 0;
1586   const SMDS_MeshNode* n4 = 0;
1587   gp_XYZ pnt;
1588
1589   vR = getRAndNodes( & u_col1->second, V, n1, n2 );
1590   vR = getRAndNodes( & u_col2->second, V, n3, n4 );
1591   
1592   gp_XY uv1 = myHelper->GetNodeUV( mySurface.Face(), n1, n4);
1593   gp_XY uv2 = myHelper->GetNodeUV( mySurface.Face(), n2, n3);
1594   gp_XY uv12 = uv1 * ( 1 - vR ) + uv2 * vR;
1595
1596   gp_XY uv3 = myHelper->GetNodeUV( mySurface.Face(), n3, n2);
1597   gp_XY uv4 = myHelper->GetNodeUV( mySurface.Face(), n4, n1);
1598   gp_XY uv34 = uv3 * ( 1 - vR ) + uv4 * vR;
1599
1600   gp_XY uv = uv12 * ( 1 - hR ) + uv34 * hR;
1601   
1602   return mySurface.Value( uv.X(), uv.Y() );
1603 }
1604
1605
1606 //================================================================================
1607 /*!
1608  * \brief Return boundary edge
1609   * \param edge - edge index
1610   * \retval TopoDS_Edge - found edge
1611  */
1612 //================================================================================
1613
1614 TopoDS_Edge StdMeshers_PrismAsBlock::TSideFace::GetEdge(const int iEdge) const
1615 {
1616   if ( !myComponents.empty() ) {
1617     switch ( iEdge ) {
1618     case V0_EDGE : return myComponents.front()->GetEdge( iEdge );
1619     case V1_EDGE : return myComponents.back() ->GetEdge( iEdge );
1620     default: return TopoDS_Edge();
1621     }
1622   }
1623   TopoDS_Shape edge;
1624   const SMDS_MeshNode* node = 0;
1625   SMESHDS_Mesh * meshDS = myHelper->GetMesh()->GetMeshDS();
1626   TNodeColumn* column;
1627
1628   switch ( iEdge ) {
1629   case TOP_EDGE:
1630   case BOTTOM_EDGE:
1631     column = & (( ++myParamToColumnMap->begin())->second );
1632     node = ( iEdge == TOP_EDGE ) ? column->back() : column->front();
1633     edge = myHelper->GetSubShapeByNode ( node, meshDS );
1634     if ( edge.ShapeType() == TopAbs_VERTEX ) {
1635       column = & ( myParamToColumnMap->begin()->second );
1636       node = ( iEdge == TOP_EDGE ) ? column->back() : column->front();
1637     }
1638     break;
1639   case V0_EDGE:
1640   case V1_EDGE: {
1641     bool back = ( iEdge == V1_EDGE );
1642     if ( !myIsForward ) back = !back;
1643     if ( back )
1644       column = & ( myParamToColumnMap->rbegin()->second );
1645     else
1646       column = & ( myParamToColumnMap->begin()->second );
1647     if ( column->size() > 0 )
1648       edge = myHelper->GetSubShapeByNode( (*column)[ 1 ], meshDS );
1649     if ( edge.IsNull() || edge.ShapeType() == TopAbs_VERTEX )
1650       node = column->front();
1651     break;
1652   }
1653   default:;
1654   }
1655   if ( !edge.IsNull() && edge.ShapeType() == TopAbs_EDGE )
1656     return TopoDS::Edge( edge );
1657
1658   // find edge by 2 vertices
1659   TopoDS_Shape V1 = edge;
1660   TopoDS_Shape V2 = myHelper->GetSubShapeByNode( node, meshDS );
1661   if ( V2.ShapeType() == TopAbs_VERTEX && !V2.IsSame( V1 ))
1662   {
1663     TopTools_ListIteratorOfListOfShape ancestIt =
1664       myHelper->GetMesh()->GetAncestors( V1 );
1665     for ( ; ancestIt.More(); ancestIt.Next() )
1666     {
1667       const TopoDS_Shape & ancestor = ancestIt.Value();
1668       if ( ancestor.ShapeType() == TopAbs_EDGE )
1669         for ( TopExp_Explorer e( ancestor, TopAbs_VERTEX ); e.More(); e.Next() )
1670           if ( V2.IsSame( e.Current() ))
1671             return TopoDS::Edge( ancestor );
1672     }
1673   }
1674   return TopoDS_Edge();
1675 }
1676
1677 //================================================================================
1678 /*!
1679  * \brief Fill block subshapes
1680   * \param shapeMap - map to fill in
1681   * \retval int - nb inserted subshapes
1682  */
1683 //================================================================================
1684
1685 int StdMeshers_PrismAsBlock::TSideFace::InsertSubShapes(TBlockShapes& shapeMap) const
1686 {
1687   int nbInserted = 0;
1688
1689   // Insert edges
1690   vector< int > edgeIdVec;
1691   SMESH_Block::GetFaceEdgesIDs( myID, edgeIdVec );
1692
1693   for ( int i = BOTTOM_EDGE; i <=V1_EDGE ; ++i ) {
1694     TopoDS_Edge e = GetEdge( i );
1695     if ( !e.IsNull() ) {
1696       nbInserted += SMESH_Block::Insert( e, edgeIdVec[ i ], shapeMap);
1697     }
1698   }
1699
1700   // Insert corner vertices
1701
1702   TParam2ColumnIt col1, col2 ;
1703   vector< int > vertIdVec;
1704
1705   // from V0 column
1706   SMESH_Block::GetEdgeVertexIDs( edgeIdVec[ V0_EDGE ], vertIdVec);
1707   GetColumns(0, col1, col2 );
1708   const SMDS_MeshNode* node0 = col1->second.front();
1709   const SMDS_MeshNode* node1 = col1->second.back();
1710   TopoDS_Shape v0 = myHelper->GetSubShapeByNode( node0, myHelper->GetMeshDS());
1711   TopoDS_Shape v1 = myHelper->GetSubShapeByNode( node1, myHelper->GetMeshDS());
1712   if ( v0.ShapeType() == TopAbs_VERTEX ) {
1713     nbInserted += SMESH_Block::Insert( v0, vertIdVec[ 0 ], shapeMap);
1714   }
1715   if ( v1.ShapeType() == TopAbs_VERTEX ) {
1716     nbInserted += SMESH_Block::Insert( v1, vertIdVec[ 1 ], shapeMap);
1717   }
1718   
1719   // from V1 column
1720   SMESH_Block::GetEdgeVertexIDs( edgeIdVec[ V1_EDGE ], vertIdVec);
1721   GetColumns(1, col1, col2 );
1722   node0 = col2->second.front();
1723   node1 = col2->second.back();
1724   v0 = myHelper->GetSubShapeByNode( node0, myHelper->GetMeshDS());
1725   v1 = myHelper->GetSubShapeByNode( node1, myHelper->GetMeshDS());
1726   if ( v0.ShapeType() == TopAbs_VERTEX ) {
1727     nbInserted += SMESH_Block::Insert( v0, vertIdVec[ 0 ], shapeMap);
1728   }
1729   if ( v1.ShapeType() == TopAbs_VERTEX ) {
1730     nbInserted += SMESH_Block::Insert( v1, vertIdVec[ 1 ], shapeMap);
1731   }
1732
1733 //   TopoDS_Vertex V0, V1, Vcom;
1734 //   TopExp::Vertices( myBaseEdge, V0, V1, true );
1735 //   if ( !myIsForward ) std::swap( V0, V1 );
1736
1737 //   // bottom vertex IDs
1738 //   SMESH_Block::GetEdgeVertexIDs( edgeIdVec[ _u0 ], vertIdVec);
1739 //   SMESH_Block::Insert( V0, vertIdVec[ 0 ], shapeMap);
1740 //   SMESH_Block::Insert( V1, vertIdVec[ 1 ], shapeMap);
1741
1742 //   TopoDS_Edge sideEdge = GetEdge( V0_EDGE );
1743 //   if ( sideEdge.IsNull() || !TopExp::CommonVertex( botEdge, sideEdge, Vcom ))
1744 //     return false;
1745
1746 //   // insert one side edge
1747 //   int edgeID;
1748 //   if ( Vcom.IsSame( V0 )) edgeID = edgeIdVec[ _v0 ];
1749 //   else                    edgeID = edgeIdVec[ _v1 ];
1750 //   SMESH_Block::Insert( sideEdge, edgeID, shapeMap);
1751
1752 //   // top vertex of the side edge
1753 //   SMESH_Block::GetEdgeVertexIDs( edgeID, vertIdVec);
1754 //   TopoDS_Vertex Vtop = TopExp::FirstVertex( sideEdge );
1755 //   if ( Vcom.IsSame( Vtop ))
1756 //     Vtop = TopExp::LastVertex( sideEdge );
1757 //   SMESH_Block::Insert( Vtop, vertIdVec[ 1 ], shapeMap);
1758
1759 //   // other side edge
1760 //   sideEdge = GetEdge( V1_EDGE );
1761 //   if ( sideEdge.IsNull() )
1762 //     return false;
1763 //   if ( edgeID = edgeIdVec[ _v1 ]) edgeID = edgeIdVec[ _v0 ];
1764 //   else                            edgeID = edgeIdVec[ _v1 ];
1765 //   SMESH_Block::Insert( sideEdge, edgeID, shapeMap);
1766   
1767 //   // top edge
1768 //   TopoDS_Edge topEdge = GetEdge( TOP_EDGE );
1769 //   SMESH_Block::Insert( topEdge, edgeIdVec[ _u1 ], shapeMap);
1770
1771 //   // top vertex of the other side edge
1772 //   if ( !TopExp::CommonVertex( topEdge, sideEdge, Vcom ))
1773 //     return false;
1774 //   SMESH_Block::GetEdgeVertexIDs( edgeID, vertIdVec );
1775 //   SMESH_Block::Insert( Vcom, vertIdVec[ 1 ], shapeMap);
1776
1777   return nbInserted;
1778 }
1779
1780 //================================================================================
1781 /*!
1782  * \brief Creates TVerticalEdgeAdaptor 
1783   * \param columnsMap - node column map
1784   * \param parameter - normalized parameter
1785  */
1786 //================================================================================
1787
1788 StdMeshers_PrismAsBlock::TVerticalEdgeAdaptor::
1789 TVerticalEdgeAdaptor( const TParam2ColumnMap* columnsMap, const double parameter)
1790 {
1791   myNodeColumn = & getColumn( columnsMap, parameter )->second;
1792 }
1793
1794 //================================================================================
1795 /*!
1796  * \brief Return coordinates for the given normalized parameter
1797   * \param U - normalized parameter
1798   * \retval gp_Pnt - coordinates
1799  */
1800 //================================================================================
1801
1802 gp_Pnt StdMeshers_PrismAsBlock::TVerticalEdgeAdaptor::Value(const Standard_Real U) const
1803 {
1804   const SMDS_MeshNode* n1;
1805   const SMDS_MeshNode* n2;
1806   double r = getRAndNodes( myNodeColumn, U, n1, n2 );
1807   return gpXYZ(n1) * ( 1 - r ) + gpXYZ(n2) * r;
1808 }
1809
1810 //================================================================================
1811 /*!
1812  * \brief Return coordinates for the given normalized parameter
1813   * \param U - normalized parameter
1814   * \retval gp_Pnt - coordinates
1815  */
1816 //================================================================================
1817
1818 gp_Pnt StdMeshers_PrismAsBlock::THorizontalEdgeAdaptor::Value(const Standard_Real U) const
1819 {
1820   return mySide->TSideFace::Value( U, myV );
1821 }
1822
1823 //================================================================================
1824 /*!
1825  * \brief Return UV on pcurve for the given normalized parameter
1826   * \param U - normalized parameter
1827   * \retval gp_Pnt - coordinates
1828  */
1829 //================================================================================
1830
1831 gp_Pnt2d StdMeshers_PrismAsBlock::TPCurveOnHorFaceAdaptor::Value(const Standard_Real U) const
1832 {
1833   TParam2ColumnIt u_col1, u_col2;
1834   double r = mySide->GetColumns( U, u_col1, u_col2 );
1835   gp_XY uv1 = mySide->GetNodeUV( myFace, u_col1->second[ myZ ]);
1836   gp_XY uv2 = mySide->GetNodeUV( myFace, u_col2->second[ myZ ]);
1837   return uv1 * ( 1 - r ) + uv2 * r;
1838 }