Salome HOME
Merge from BR_Dev_For_4_0 branch (from tag mergeto_BR_QT4_Dev_07Jul08)
[modules/smesh.git] / src / SMESH_I / SMESH_MeshEditor_i.cxx
1 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
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   : SMESH_MeshEditor_i.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESH_MeshEditor_i.hxx"
30
31 #include "SMDS_MeshEdge.hxx"
32 #include "SMDS_MeshFace.hxx"
33 #include "SMDS_MeshVolume.hxx"
34 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
35 #include "SMESH_MeshEditor.hxx"
36 #include "SMESH_subMeshEventListener.hxx"
37 #include "SMESH_Gen_i.hxx"
38 #include "SMESH_Filter_i.hxx"
39 #include "SMESH_PythonDump.hxx"
40
41 #include "utilities.h"
42 #include "Utils_ExceptHandlers.hxx"
43 #include "Utils_CorbaException.hxx"
44
45 #include <BRepAdaptor_Surface.hxx>
46 #include <BRep_Tool.hxx>
47 #include <TopExp_Explorer.hxx>
48 #include <TopoDS.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <TopoDS_Face.hxx>
51 #include <gp_Ax1.hxx>
52 #include <gp_Ax2.hxx>
53 #include <gp_Vec.hxx>
54
55 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
56 #define NO_CAS_CATCH
57 #endif
58
59 #include <Standard_Failure.hxx>
60
61 #ifdef NO_CAS_CATCH
62 #include <Standard_ErrorHandler.hxx>
63 #endif
64
65 #include <sstream>
66
67 #define cast2Node(elem) static_cast<const SMDS_MeshNode*>( elem )
68
69 using namespace std;
70 using SMESH::TPythonDump;
71
72 namespace {
73
74   //=============================================================================
75   /*!
76    * \brief Mesh to apply modifications for preview purposes
77    */
78   //=============================================================================
79
80   struct TPreviewMesh: public SMESH_Mesh
81   {
82     SMDSAbs_ElementType myPreviewType; // type to show
83     //!< Constructor
84     TPreviewMesh(SMDSAbs_ElementType previewElements = SMDSAbs_All) {
85       _isShapeToMesh = _id =_studyId =_idDoc = 0;
86       _myMeshDS  = new SMESHDS_Mesh( _id, true );
87       myPreviewType = previewElements;
88     }
89     //!< Destructor
90     virtual ~TPreviewMesh() { delete _myMeshDS; }
91     //!< Copy a set of elements
92     void Copy(const TIDSortedElemSet & theElements,
93               TIDSortedElemSet&        theCopyElements,
94               SMDSAbs_ElementType      theSelectType = SMDSAbs_All,
95               SMDSAbs_ElementType      theAvoidType = SMDSAbs_All)
96     {
97       // loop on theIDsOfElements
98       TIDSortedElemSet::const_iterator eIt = theElements.begin();
99       for ( ; eIt != theElements.end(); ++eIt )
100       {
101         const SMDS_MeshElement* anElem = *eIt;
102         if ( !anElem ) continue;
103         SMDSAbs_ElementType type = anElem->GetType();
104         if ( type == theAvoidType ||
105              ( theSelectType != SMDSAbs_All && type != theSelectType ))
106           continue;
107
108         if ( const SMDS_MeshElement* anElemCopy = Copy( anElem ))
109           theCopyElements.insert( theCopyElements.end(), anElemCopy );
110       }
111     }
112     //!< Copy an element
113     SMDS_MeshElement* Copy( const SMDS_MeshElement* anElem )
114     {
115       // copy element nodes
116       int anElemNbNodes = anElem->NbNodes();
117       vector< int > anElemNodesID( anElemNbNodes ) ;
118       SMDS_ElemIteratorPtr itElemNodes = anElem->nodesIterator();
119       for ( int i = 0; itElemNodes->more(); i++)
120       {
121         const SMDS_MeshNode* anElemNode = cast2Node( itElemNodes->next() );
122         Copy( anElemNode );
123         anElemNodesID[i] = anElemNode->GetID();
124       }
125
126       // creates a corresponding element on copied nodes
127       SMDS_MeshElement* anElemCopy = 0;
128       if ( anElem->IsPoly() && anElem->GetType() == SMDSAbs_Volume )
129       {
130         const SMDS_PolyhedralVolumeOfNodes* ph =
131           dynamic_cast<const SMDS_PolyhedralVolumeOfNodes*> (anElem);
132         if ( ph )
133           anElemCopy = _myMeshDS->AddPolyhedralVolumeWithID
134             (anElemNodesID, ph->GetQuanities(),anElem->GetID());
135       }
136       else {
137         anElemCopy = ::SMESH_MeshEditor(this).AddElement( anElemNodesID,
138                                                           anElem->GetType(),
139                                                           anElem->IsPoly() );
140       }
141       return anElemCopy;
142     }
143     //!< Copy a node
144     SMDS_MeshNode* Copy( const SMDS_MeshNode* anElemNode )
145     {
146       return _myMeshDS->AddNodeWithID(anElemNode->X(), anElemNode->Y(), anElemNode->Z(), 
147                                       anElemNode->GetID());
148     }
149   };// struct TPreviewMesh
150
151   static SMESH_NodeSearcher * myNodeSearcher = 0;
152
153   //=============================================================================
154   /*!
155    * \brief Deleter of myNodeSearcher at any compute event occured
156    */
157   //=============================================================================
158
159   struct TNodeSearcherDeleter : public SMESH_subMeshEventListener
160   {
161     SMESH_Mesh* myMesh;
162     //!< Constructor
163     TNodeSearcherDeleter(): SMESH_subMeshEventListener( false ), // won't be deleted by submesh
164     myMesh(0) {}
165     //!< Delete myNodeSearcher
166     static void Delete()
167     {
168       if ( myNodeSearcher ) { delete myNodeSearcher; myNodeSearcher = 0; }
169     }
170     typedef map < int, SMESH_subMesh * > TDependsOnMap;
171     //!< The meshod called by submesh: do my main job
172     void ProcessEvent(const int, const int eventType, SMESH_subMesh* sm,
173                       SMESH_subMeshEventListenerData*,const SMESH_Hypothesis*)
174     {
175       if ( eventType == SMESH_subMesh::COMPUTE_EVENT ) {
176         Delete();
177         Unset( sm->GetFather() );
178       }
179     }
180     //!< set self on all submeshes and delete myNodeSearcher if other mesh is set
181     void Set(SMESH_Mesh* mesh)
182     {
183       if ( myMesh && myMesh != mesh ) {
184         Delete();
185         Unset( myMesh );
186       }
187       myMesh = mesh;
188       if ( SMESH_subMesh* myMainSubMesh = mesh->GetSubMeshContaining(1) ) {
189         const TDependsOnMap & subMeshes = myMainSubMesh->DependsOn();
190         TDependsOnMap::const_iterator sm;
191         for (sm = subMeshes.begin(); sm != subMeshes.end(); sm++)
192           sm->second->SetEventListener( this, 0, sm->second );
193       }
194     }
195     //!<  delete self from all submeshes
196     void Unset(SMESH_Mesh* mesh)
197     {
198       if ( SMESH_subMesh* myMainSubMesh = mesh->GetSubMeshContaining(1) ) {
199         const TDependsOnMap & subMeshes = myMainSubMesh->DependsOn();
200         TDependsOnMap::const_iterator sm;
201         for (sm = subMeshes.begin(); sm != subMeshes.end(); sm++)
202           sm->second->DeleteEventListener( this );
203       }
204     }
205   };
206
207   TCollection_AsciiString mirrorTypeName( SMESH::SMESH_MeshEditor::MirrorType theMirrorType )
208   {
209     TCollection_AsciiString typeStr;
210     switch ( theMirrorType ) {
211     case  SMESH::SMESH_MeshEditor::POINT:
212       typeStr = "SMESH.SMESH_MeshEditor.POINT";
213       break;
214     case  SMESH::SMESH_MeshEditor::AXIS:
215       typeStr = "SMESH.SMESH_MeshEditor.AXIS";
216       break;
217     default:
218       typeStr = "SMESH.SMESH_MeshEditor.PLANE";
219     }
220     return typeStr;
221   }
222 }
223
224 //=============================================================================
225 /*!
226  *
227  */
228 //=============================================================================
229
230 SMESH_MeshEditor_i::SMESH_MeshEditor_i(SMESH_Mesh_i* theMesh, bool isPreview)
231 {
232   myMesh_i = theMesh;
233   myMesh = & theMesh->GetImpl();
234   myPreviewMode = isPreview;
235 }
236
237 //================================================================================
238 /*!
239  * \brief Destructor
240  */
241 //================================================================================
242
243 SMESH_MeshEditor_i::~SMESH_MeshEditor_i()
244 {
245 }
246
247 //================================================================================
248 /*!
249  * \brief Clear members
250  */
251 //================================================================================
252
253 void SMESH_MeshEditor_i::initData()
254 {
255   if ( myPreviewMode ) {
256     myPreviewData = new SMESH::MeshPreviewStruct();
257   }
258   else {
259     myLastCreatedElems = new SMESH::long_array();
260     myLastCreatedNodes = new SMESH::long_array();
261     TNodeSearcherDeleter::Delete();
262   }
263 }
264
265 //=============================================================================
266 /*!
267  *
268  */
269 //=============================================================================
270
271 CORBA::Boolean
272   SMESH_MeshEditor_i::RemoveElements(const SMESH::long_array & IDsOfElements)
273 {
274   initData();
275
276   ::SMESH_MeshEditor anEditor( myMesh );
277   list< int > IdList;
278
279   for (int i = 0; i < IDsOfElements.length(); i++)
280     IdList.push_back( IDsOfElements[i] );
281
282   // Update Python script
283   TPythonDump() << "isDone = " << this << ".RemoveElements( " << IDsOfElements << " )";
284 #ifdef _DEBUG_
285   TPythonDump() << "print 'RemoveElements: ', isDone";
286 #endif
287   // Remove Elements
288   return anEditor.Remove( IdList, false );
289 }
290
291 //=============================================================================
292 /*!
293  *
294  */
295 //=============================================================================
296
297 CORBA::Boolean SMESH_MeshEditor_i::RemoveNodes(const SMESH::long_array & IDsOfNodes)
298 {
299   initData();
300
301   ::SMESH_MeshEditor anEditor( myMesh );
302   list< int > IdList;
303   for (int i = 0; i < IDsOfNodes.length(); i++)
304     IdList.push_back( IDsOfNodes[i] );
305
306   // Update Python script
307   TPythonDump() << "isDone = " << this << ".RemoveNodes( " << IDsOfNodes << " )";
308 #ifdef _DEBUG_
309   TPythonDump() << "print 'RemoveNodes: ', isDone";
310 #endif
311
312   return anEditor.Remove( IdList, true );
313 }
314
315 //=============================================================================
316 /*!
317  *
318  */
319 //=============================================================================
320
321 CORBA::Long SMESH_MeshEditor_i::AddEdge(const SMESH::long_array & IDsOfNodes)
322 {
323   initData();
324
325   int NbNodes = IDsOfNodes.length();
326   SMDS_MeshElement* elem = 0;
327   if (NbNodes == 2)
328   {
329     CORBA::Long index1 = IDsOfNodes[0];
330     CORBA::Long index2 = IDsOfNodes[1];
331     elem = GetMeshDS()->AddEdge(GetMeshDS()->FindNode(index1), GetMeshDS()->FindNode(index2));
332
333     // Update Python script
334     TPythonDump() << "edge = " << this << ".AddEdge([ "
335                   << index1 << ", " << index2 <<" ])";
336   }
337   if (NbNodes == 3) {
338     CORBA::Long n1 = IDsOfNodes[0];
339     CORBA::Long n2 = IDsOfNodes[1];
340     CORBA::Long n12 = IDsOfNodes[2];
341     elem = GetMeshDS()->AddEdge(GetMeshDS()->FindNode(n1),
342                                 GetMeshDS()->FindNode(n2),
343                                 GetMeshDS()->FindNode(n12));
344     // Update Python script
345     TPythonDump() << "edgeID = " << this << ".AddEdge([ "
346                   <<n1<<", "<<n2<<", "<<n12<<" ])";
347   }
348
349   if(elem)
350     return elem->GetID();
351
352   return 0;
353 }
354
355 //=============================================================================
356 /*!
357  *
358  */
359 //=============================================================================
360
361 CORBA::Long SMESH_MeshEditor_i::AddNode(CORBA::Double x,
362                                         CORBA::Double y, CORBA::Double z)
363 {
364   initData();
365
366   const SMDS_MeshNode* N = GetMeshDS()->AddNode(x, y, z);
367
368   // Update Python script
369   TPythonDump() << "nodeID = " << this << ".AddNode( "
370                 << x << ", " << y << ", " << z << " )";
371
372   return N->GetID();
373 }
374
375 //=============================================================================
376 /*!
377  *  AddFace
378  */
379 //=============================================================================
380
381 CORBA::Long SMESH_MeshEditor_i::AddFace(const SMESH::long_array & IDsOfNodes)
382 {
383   initData();
384
385   int NbNodes = IDsOfNodes.length();
386   if (NbNodes < 3)
387   {
388     return false;
389   }
390
391   std::vector<const SMDS_MeshNode*> nodes (NbNodes);
392   for (int i = 0; i < NbNodes; i++)
393     nodes[i] = GetMeshDS()->FindNode(IDsOfNodes[i]);
394
395   SMDS_MeshElement* elem = 0;
396   if (NbNodes == 3) {
397     elem = GetMeshDS()->AddFace(nodes[0], nodes[1], nodes[2]);
398   }
399   else if (NbNodes == 4) {
400     elem = GetMeshDS()->AddFace(nodes[0], nodes[1], nodes[2], nodes[3]);
401   }
402   else if (NbNodes == 6) {
403     elem = GetMeshDS()->AddFace(nodes[0], nodes[1], nodes[2], nodes[3],
404                                 nodes[4], nodes[5]);
405   }
406   else if (NbNodes == 8) {
407     elem = GetMeshDS()->AddFace(nodes[0], nodes[1], nodes[2], nodes[3],
408                                 nodes[4], nodes[5], nodes[6], nodes[7]);
409   }
410
411   // Update Python script
412   TPythonDump() << "faceID = " << this << ".AddFace( " << IDsOfNodes << " )";
413
414   if(elem)
415     return elem->GetID();
416
417   return 0;
418 }
419
420 //=============================================================================
421 /*!
422  *  AddPolygonalFace
423  */
424 //=============================================================================
425 CORBA::Long SMESH_MeshEditor_i::AddPolygonalFace
426                                    (const SMESH::long_array & IDsOfNodes)
427 {
428   initData();
429
430   int NbNodes = IDsOfNodes.length();
431   std::vector<const SMDS_MeshNode*> nodes (NbNodes);
432   for (int i = 0; i < NbNodes; i++)
433     nodes[i] = GetMeshDS()->FindNode(IDsOfNodes[i]);
434
435   const SMDS_MeshElement* elem = GetMeshDS()->AddPolygonalFace(nodes);
436   
437   // Update Python script
438   TPythonDump() <<"faceID = "<<this<<".AddPolygonalFace( "<<IDsOfNodes<<" )";
439 #ifdef _DEBUG_
440   TPythonDump() << "print 'AddPolygonalFace: ', faceID";
441 #endif
442
443   if(elem)
444     return elem->GetID();
445
446   return 0;
447 }
448
449 //=============================================================================
450 /*!
451  *
452  */
453 //=============================================================================
454
455 CORBA::Long SMESH_MeshEditor_i::AddVolume(const SMESH::long_array & IDsOfNodes)
456 {
457   initData();
458
459   int NbNodes = IDsOfNodes.length();
460   vector< const SMDS_MeshNode*> n(NbNodes);
461   for(int i=0;i<NbNodes;i++)
462     n[i]=GetMeshDS()->FindNode(IDsOfNodes[i]);
463
464   SMDS_MeshElement* elem = 0;
465   switch(NbNodes)
466   {
467   case 4 :elem = GetMeshDS()->AddVolume(n[0],n[1],n[2],n[3]); break;
468   case 5 :elem = GetMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4]); break;
469   case 6 :elem = GetMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5]); break;
470   case 8 :elem = GetMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7]); break;
471   case 10:elem = GetMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5],
472                                         n[6],n[7],n[8],n[9]);
473     break;
474   case 13:elem = GetMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5],n[6],
475                                         n[7],n[8],n[9],n[10],n[11],n[12]);
476     break;
477   case 15:elem = GetMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7],n[8],
478                                         n[9],n[10],n[11],n[12],n[13],n[14]);
479     break;
480   case 20:elem = GetMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7],
481                                         n[8],n[9],n[10],n[11],n[12],n[13],n[14],
482                                         n[15],n[16],n[17],n[18],n[19]);
483     break;
484   }
485
486   // Update Python script
487   TPythonDump() << "volID = " << this << ".AddVolume( " << IDsOfNodes << " )";
488 #ifdef _DEBUG_
489   TPythonDump() << "print 'AddVolume: ', volID";
490 #endif
491
492   if(elem)
493     return elem->GetID();
494
495   return 0;
496 }
497
498 //=============================================================================
499 /*!
500  *  AddPolyhedralVolume
501  */
502 //=============================================================================
503 CORBA::Long SMESH_MeshEditor_i::AddPolyhedralVolume
504                                    (const SMESH::long_array & IDsOfNodes,
505                                     const SMESH::long_array & Quantities)
506 {
507   initData();
508
509   int NbNodes = IDsOfNodes.length();
510   std::vector<const SMDS_MeshNode*> n (NbNodes);
511   for (int i = 0; i < NbNodes; i++)
512     n[i] = GetMeshDS()->FindNode(IDsOfNodes[i]);
513
514   int NbFaces = Quantities.length();
515   std::vector<int> q (NbFaces);
516   for (int j = 0; j < NbFaces; j++)
517     q[j] = Quantities[j];
518
519   const SMDS_MeshElement* elem = GetMeshDS()->AddPolyhedralVolume(n, q);
520
521   // Update Python script
522   TPythonDump() << "volID = " << this << ".AddPolyhedralVolume( "
523                 << IDsOfNodes << ", " << Quantities << " )";
524 #ifdef _DEBUG_
525   TPythonDump() << "print 'AddPolyhedralVolume: ', volID";
526 #endif
527
528   if(elem)
529     return elem->GetID();
530
531   return 0;
532 }
533
534 //=============================================================================
535 /*!
536  *  AddPolyhedralVolumeByFaces
537  */
538 //=============================================================================
539 CORBA::Long SMESH_MeshEditor_i::AddPolyhedralVolumeByFaces
540                                    (const SMESH::long_array & IdsOfFaces)
541 {
542   initData();
543
544   int NbFaces = IdsOfFaces.length();
545   std::vector<const SMDS_MeshNode*> poly_nodes;
546   std::vector<int> quantities (NbFaces);
547
548   for (int i = 0; i < NbFaces; i++) {
549     const SMDS_MeshElement* aFace = GetMeshDS()->FindElement(IdsOfFaces[i]);
550     quantities[i] = aFace->NbNodes();
551
552     SMDS_ElemIteratorPtr It = aFace->nodesIterator();
553     while (It->more()) {
554       poly_nodes.push_back(static_cast<const SMDS_MeshNode *>(It->next()));
555     }
556   }
557
558   const SMDS_MeshElement* elem = GetMeshDS()->AddPolyhedralVolume(poly_nodes, quantities);
559
560   // Update Python script
561   TPythonDump() << "volID = " << this << ".AddPolyhedralVolumeByFaces( "
562                 << IdsOfFaces << " )";
563 #ifdef _DEBUG_
564   TPythonDump() << "print 'AddPolyhedralVolume: ', volID";
565 #endif
566
567   if(elem)
568     return elem->GetID();
569
570   return 0;
571 }
572
573 //=============================================================================
574 /*!
575  * \brief Bind a node to a vertex
576  * \param NodeID - node ID
577  * \param VertexID - vertex ID available through GEOM_Object.GetSubShapeIndices()[0]
578  * \retval boolean - false if NodeID or VertexID is invalid
579  */
580 //=============================================================================
581
582 void SMESH_MeshEditor_i::SetNodeOnVertex(CORBA::Long NodeID, CORBA::Long VertexID)
583   throw (SALOME::SALOME_Exception)
584 {
585   Unexpect aCatch(SALOME_SalomeException);
586
587   SMESHDS_Mesh * mesh = GetMeshDS();
588   SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( mesh->FindNode(NodeID) );
589   if ( !node )
590     THROW_SALOME_CORBA_EXCEPTION("Invalid NodeID", SALOME::BAD_PARAM);
591
592   if ( mesh->MaxShapeIndex() < VertexID )
593     THROW_SALOME_CORBA_EXCEPTION("Invalid VertexID", SALOME::BAD_PARAM);
594
595   TopoDS_Shape shape = mesh->IndexToShape( VertexID );
596   if ( shape.ShapeType() != TopAbs_VERTEX )
597     THROW_SALOME_CORBA_EXCEPTION("Invalid VertexID", SALOME::BAD_PARAM);
598
599   mesh->SetNodeOnVertex( node, VertexID );
600 }
601
602 //=============================================================================
603 /*!
604  * \brief Store node position on an edge
605  * \param NodeID - node ID
606  * \param EdgeID - edge ID available through GEOM_Object.GetSubShapeIndices()[0]
607  * \param paramOnEdge - parameter on edge where the node is located
608  * \retval boolean - false if any parameter is invalid
609  */
610 //=============================================================================
611
612 void SMESH_MeshEditor_i::SetNodeOnEdge(CORBA::Long NodeID, CORBA::Long EdgeID,
613                                        CORBA::Double paramOnEdge)
614   throw (SALOME::SALOME_Exception)
615 {
616   Unexpect aCatch(SALOME_SalomeException);
617
618   SMESHDS_Mesh * mesh = GetMeshDS();
619   SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( mesh->FindNode(NodeID) );
620   if ( !node )
621     THROW_SALOME_CORBA_EXCEPTION("Invalid NodeID", SALOME::BAD_PARAM);
622
623   if ( mesh->MaxShapeIndex() < EdgeID )
624     THROW_SALOME_CORBA_EXCEPTION("Invalid EdgeID", SALOME::BAD_PARAM);
625
626   TopoDS_Shape shape = mesh->IndexToShape( EdgeID );
627   if ( shape.ShapeType() != TopAbs_EDGE )
628     THROW_SALOME_CORBA_EXCEPTION("Invalid EdgeID", SALOME::BAD_PARAM);
629
630   Standard_Real f,l;
631   BRep_Tool::Range( TopoDS::Edge( shape ), f,l);
632   if ( paramOnEdge < f || paramOnEdge > l )
633     THROW_SALOME_CORBA_EXCEPTION("Invalid paramOnEdge", SALOME::BAD_PARAM);
634
635   mesh->SetNodeOnEdge( node, EdgeID, paramOnEdge );
636 }
637
638 //=============================================================================
639 /*!
640  * \brief Store node position on a face
641  * \param NodeID - node ID
642  * \param FaceID - face ID available through GEOM_Object.GetSubShapeIndices()[0]
643  * \param u - U parameter on face where the node is located
644  * \param v - V parameter on face where the node is located
645  * \retval boolean - false if any parameter is invalid
646  */
647 //=============================================================================
648
649 void SMESH_MeshEditor_i::SetNodeOnFace(CORBA::Long NodeID, CORBA::Long FaceID,
650                                        CORBA::Double u, CORBA::Double v)
651   throw (SALOME::SALOME_Exception)
652 {
653   Unexpect aCatch(SALOME_SalomeException);
654
655   SMESHDS_Mesh * mesh = GetMeshDS();
656   SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( mesh->FindNode(NodeID) );
657   if ( !node )
658     THROW_SALOME_CORBA_EXCEPTION("Invalid NodeID", SALOME::BAD_PARAM);
659
660   if ( mesh->MaxShapeIndex() < FaceID )
661     THROW_SALOME_CORBA_EXCEPTION("Invalid FaceID", SALOME::BAD_PARAM);
662
663   TopoDS_Shape shape = mesh->IndexToShape( FaceID );
664   if ( shape.ShapeType() != TopAbs_FACE )
665     THROW_SALOME_CORBA_EXCEPTION("Invalid FaceID", SALOME::BAD_PARAM);
666
667   BRepAdaptor_Surface surf( TopoDS::Face( shape ));
668   bool isOut = ( u < surf.FirstUParameter() ||
669                  u > surf.LastUParameter()  ||
670                  v < surf.FirstVParameter() ||
671                  v > surf.LastVParameter() );
672
673   if ( isOut ) {
674 #ifdef _DEBUG_
675     cout << "FACE " << FaceID << " (" << u << "," << v << ") out of "
676          << " u( " <<  surf.FirstUParameter() 
677          << "," <<  surf.LastUParameter()  
678          << ") v( " <<  surf.FirstVParameter() 
679          << "," <<  surf.LastVParameter()
680          << ")" << endl;
681 #endif    
682     THROW_SALOME_CORBA_EXCEPTION("Invalid UV", SALOME::BAD_PARAM);
683   }
684
685   mesh->SetNodeOnFace( node, FaceID, u, v );
686 }
687
688 //=============================================================================
689 /*!
690  * \brief Bind a node to a solid
691  * \param NodeID - node ID
692  * \param SolidID - vertex ID available through GEOM_Object.GetSubShapeIndices()[0]
693  * \retval boolean - false if NodeID or SolidID is invalid
694  */
695 //=============================================================================
696
697 void SMESH_MeshEditor_i::SetNodeInVolume(CORBA::Long NodeID, CORBA::Long SolidID)
698   throw (SALOME::SALOME_Exception)
699 {
700   Unexpect aCatch(SALOME_SalomeException);
701
702   SMESHDS_Mesh * mesh = GetMeshDS();
703   SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( mesh->FindNode(NodeID) );
704   if ( !node )
705     THROW_SALOME_CORBA_EXCEPTION("Invalid NodeID", SALOME::BAD_PARAM);
706
707   if ( mesh->MaxShapeIndex() < SolidID )
708     THROW_SALOME_CORBA_EXCEPTION("Invalid SolidID", SALOME::BAD_PARAM);
709
710   TopoDS_Shape shape = mesh->IndexToShape( SolidID );
711   if ( shape.ShapeType() != TopAbs_SOLID &&
712        shape.ShapeType() != TopAbs_SHELL)
713     THROW_SALOME_CORBA_EXCEPTION("Invalid SolidID", SALOME::BAD_PARAM);
714
715   mesh->SetNodeInVolume( node, SolidID );
716 }
717
718 //=============================================================================
719 /*!
720  * \brief Bind an element to a shape
721  * \param ElementID - element ID
722  * \param ShapeID - shape ID available through GEOM_Object.GetSubShapeIndices()[0]
723  * \retval boolean - false if ElementID or ShapeID is invalid
724  */
725 //=============================================================================
726
727 void SMESH_MeshEditor_i::SetMeshElementOnShape(CORBA::Long ElementID,
728                                                CORBA::Long ShapeID)
729   throw (SALOME::SALOME_Exception)
730 {
731   Unexpect aCatch(SALOME_SalomeException);
732
733   SMESHDS_Mesh * mesh = GetMeshDS();
734   SMDS_MeshElement* elem = const_cast<SMDS_MeshElement*>(mesh->FindElement(ElementID));
735   if ( !elem )
736     THROW_SALOME_CORBA_EXCEPTION("Invalid ElementID", SALOME::BAD_PARAM);
737
738   if ( mesh->MaxShapeIndex() < ShapeID )
739     THROW_SALOME_CORBA_EXCEPTION("Invalid ShapeID", SALOME::BAD_PARAM);
740
741   TopoDS_Shape shape = mesh->IndexToShape( ShapeID );
742   if ( shape.ShapeType() != TopAbs_EDGE &&
743        shape.ShapeType() != TopAbs_FACE &&
744        shape.ShapeType() != TopAbs_SOLID &&
745        shape.ShapeType() != TopAbs_SHELL )
746     THROW_SALOME_CORBA_EXCEPTION("Invalid shape type", SALOME::BAD_PARAM);
747
748   mesh->SetMeshElementOnShape( elem, ShapeID );
749 }
750
751
752 //=============================================================================
753 /*!
754  *
755  */
756 //=============================================================================
757
758 CORBA::Boolean SMESH_MeshEditor_i::MoveNode(CORBA::Long   NodeID,
759                                             CORBA::Double x,
760                                             CORBA::Double y,
761                                             CORBA::Double z)
762 {
763   initData();
764
765   const SMDS_MeshNode * node = GetMeshDS()->FindNode( NodeID );
766   if ( !node )
767     return false;
768
769   GetMeshDS()->MoveNode(node, x, y, z);
770
771   // Update Python script
772   TPythonDump() << "isDone = " << this << ".MoveNode( "
773                 << NodeID << ", " << x << ", " << y << ", " << z << " )";
774
775   return true;
776 }
777
778 //=============================================================================
779 /*!
780  *
781  */
782 //=============================================================================
783
784 CORBA::Boolean SMESH_MeshEditor_i::InverseDiag(CORBA::Long NodeID1,
785                                                CORBA::Long NodeID2)
786 {
787   initData();
788
789   const SMDS_MeshNode * n1 = GetMeshDS()->FindNode( NodeID1 );
790   const SMDS_MeshNode * n2 = GetMeshDS()->FindNode( NodeID2 );
791   if ( !n1 || !n2 )
792     return false;
793
794   // Update Python script
795   TPythonDump() << "isDone = " << this << ".InverseDiag( "
796                 << NodeID1 << ", " << NodeID2 << " )";
797
798   ::SMESH_MeshEditor aMeshEditor( myMesh );
799   return aMeshEditor.InverseDiag ( n1, n2 );
800 }
801
802 //=============================================================================
803 /*!
804  *
805  */
806 //=============================================================================
807
808 CORBA::Boolean SMESH_MeshEditor_i::DeleteDiag(CORBA::Long NodeID1,
809                                               CORBA::Long NodeID2)
810 {
811   initData();
812
813   const SMDS_MeshNode * n1 = GetMeshDS()->FindNode( NodeID1 );
814   const SMDS_MeshNode * n2 = GetMeshDS()->FindNode( NodeID2 );
815   if ( !n1 || !n2 )
816     return false;
817
818   // Update Python script
819   TPythonDump() << "isDone = " << this << ".DeleteDiag( "
820                 << NodeID1 << ", " << NodeID2 <<  " )";
821
822   ::SMESH_MeshEditor aMeshEditor( myMesh );
823
824   bool stat = aMeshEditor.DeleteDiag ( n1, n2 );
825
826   storeResult(aMeshEditor);
827
828   return stat;
829 }
830
831 //=============================================================================
832 /*!
833  *
834  */
835 //=============================================================================
836
837 CORBA::Boolean SMESH_MeshEditor_i::Reorient(const SMESH::long_array & IDsOfElements)
838 {
839   initData();
840
841   ::SMESH_MeshEditor anEditor( myMesh );
842   for (int i = 0; i < IDsOfElements.length(); i++)
843   {
844     CORBA::Long index = IDsOfElements[i];
845     const SMDS_MeshElement * elem = GetMeshDS()->FindElement(index);
846     if ( elem )
847       anEditor.Reorient( elem );
848   }
849   // Update Python script
850   TPythonDump() << "isDone = " << this << ".Reorient( " << IDsOfElements << " )";
851
852   return true;
853 }
854
855
856 //=============================================================================
857 /*!
858  *
859  */
860 //=============================================================================
861
862 CORBA::Boolean SMESH_MeshEditor_i::ReorientObject(SMESH::SMESH_IDSource_ptr theObject)
863 {
864   initData();
865
866   SMESH::long_array_var anElementsId = theObject->GetIDs();
867   CORBA::Boolean isDone = Reorient(anElementsId);
868
869   // Clear python line, created by Reorient()
870   SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
871   aSMESHGen->RemoveLastFromPythonScript(aSMESHGen->GetCurrentStudyID());
872
873   // Update Python script
874   TPythonDump() << "isDone = " << this << ".ReorientObject( " << theObject << " )";
875
876   return isDone;
877 }
878
879 namespace
880 {
881   //================================================================================
882   /*!
883    * \brief function for conversion long_array to TIDSortedElemSet
884     * \param IDs - array of IDs
885     * \param aMesh - mesh
886     * \param aMap - collection to fill
887     * \param aType - element type
888    */
889   //================================================================================
890
891   void arrayToSet(const SMESH::long_array & IDs,
892                   const SMESHDS_Mesh*       aMesh,
893                   TIDSortedElemSet&         aMap,
894                   const SMDSAbs_ElementType aType = SMDSAbs_All )
895   { 
896     for (int i=0; i<IDs.length(); i++) {
897       CORBA::Long ind = IDs[i];
898       const SMDS_MeshElement * elem = aMesh->FindElement(ind);
899       if ( elem && ( aType == SMDSAbs_All || elem->GetType() == aType ))
900         aMap.insert( elem );
901     }
902   }
903 }
904
905 //=============================================================================
906 /*!
907  *
908  */
909 //=============================================================================
910 CORBA::Boolean SMESH_MeshEditor_i::TriToQuad (const SMESH::long_array &   IDsOfElements,
911                                               SMESH::NumericalFunctor_ptr Criterion,
912                                               CORBA::Double               MaxAngle)
913 {
914   initData();
915
916   SMESHDS_Mesh* aMesh = GetMeshDS();
917   TIDSortedElemSet faces;
918   arrayToSet(IDsOfElements, aMesh, faces, SMDSAbs_Face);
919
920   SMESH::NumericalFunctor_i* aNumericalFunctor =
921     dynamic_cast<SMESH::NumericalFunctor_i*>( SMESH_Gen_i::GetServant( Criterion ).in() );
922   SMESH::Controls::NumericalFunctorPtr aCrit;
923   if ( !aNumericalFunctor )
924     aCrit.reset( new SMESH::Controls::AspectRatio() );
925   else
926     aCrit = aNumericalFunctor->GetNumericalFunctor();
927
928   // Update Python script
929   TPythonDump() << "isDone = " << this << ".TriToQuad( "
930                 << IDsOfElements << ", " << aNumericalFunctor << ", " << MaxAngle << " )";
931 #ifdef _DEBUG_
932   TPythonDump() << "print 'TriToQuad: ', isDone";
933 #endif
934
935   ::SMESH_MeshEditor anEditor( myMesh );
936
937   bool stat = anEditor.TriToQuad( faces, aCrit, MaxAngle );
938
939   storeResult(anEditor);
940
941   return stat;
942 }
943
944
945 //=============================================================================
946 /*!
947  *
948  */
949 //=============================================================================
950 CORBA::Boolean SMESH_MeshEditor_i::TriToQuadObject (SMESH::SMESH_IDSource_ptr   theObject,
951                                                     SMESH::NumericalFunctor_ptr Criterion,
952                                                     CORBA::Double               MaxAngle)
953 {
954   initData();
955
956   SMESH::long_array_var anElementsId = theObject->GetIDs();
957   CORBA::Boolean isDone = TriToQuad(anElementsId, Criterion, MaxAngle);
958
959   // Clear python line(s), created by TriToQuad()
960   SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
961   aSMESHGen->RemoveLastFromPythonScript(aSMESHGen->GetCurrentStudyID());
962 #ifdef _DEBUG_
963   aSMESHGen->RemoveLastFromPythonScript(aSMESHGen->GetCurrentStudyID());
964 #endif
965
966   SMESH::NumericalFunctor_i* aNumericalFunctor =
967     SMESH::DownCast<SMESH::NumericalFunctor_i*>( Criterion );
968
969   // Update Python script
970   TPythonDump() << "isDone = " << this << ".TriToQuadObject("
971                 << theObject << ", " << aNumericalFunctor << ", " << MaxAngle << " )";
972 #ifdef _DEBUG_
973   TPythonDump() << "print 'TriToQuadObject: ', isDone";
974 #endif
975
976   return isDone;
977 }
978
979
980 //=============================================================================
981 /*!
982  *
983  */
984 //=============================================================================
985 CORBA::Boolean SMESH_MeshEditor_i::QuadToTri (const SMESH::long_array &   IDsOfElements,
986                                               SMESH::NumericalFunctor_ptr Criterion)
987 {
988   initData();
989
990   SMESHDS_Mesh* aMesh = GetMeshDS();
991   TIDSortedElemSet faces;
992   arrayToSet(IDsOfElements, aMesh, faces, SMDSAbs_Face);
993
994   SMESH::NumericalFunctor_i* aNumericalFunctor =
995     dynamic_cast<SMESH::NumericalFunctor_i*>( SMESH_Gen_i::GetServant( Criterion ).in() );
996   SMESH::Controls::NumericalFunctorPtr aCrit;
997   if ( !aNumericalFunctor )
998     aCrit.reset( new SMESH::Controls::AspectRatio() );
999   else
1000     aCrit = aNumericalFunctor->GetNumericalFunctor();
1001
1002
1003   // Update Python script
1004   TPythonDump() << "isDone = " << this << ".QuadToTri( " << IDsOfElements << ", " << aNumericalFunctor << " )";
1005 #ifdef _DEBUG_
1006   TPythonDump() << "print 'QuadToTri: ', isDone";
1007 #endif
1008
1009   ::SMESH_MeshEditor anEditor( myMesh );
1010   CORBA::Boolean stat = anEditor.QuadToTri( faces, aCrit );
1011
1012   storeResult(anEditor);
1013
1014   return stat;
1015 }
1016
1017
1018 //=============================================================================
1019 /*!
1020  *
1021  */
1022 //=============================================================================
1023 CORBA::Boolean SMESH_MeshEditor_i::QuadToTriObject (SMESH::SMESH_IDSource_ptr   theObject,
1024                                                     SMESH::NumericalFunctor_ptr Criterion)
1025 {
1026   initData();
1027
1028   SMESH::long_array_var anElementsId = theObject->GetIDs();
1029   CORBA::Boolean isDone = QuadToTri(anElementsId, Criterion);
1030
1031   // Clear python line(s), created by QuadToTri()
1032   SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
1033   aSMESHGen->RemoveLastFromPythonScript(aSMESHGen->GetCurrentStudyID());
1034 #ifdef _DEBUG_
1035   aSMESHGen->RemoveLastFromPythonScript(aSMESHGen->GetCurrentStudyID());
1036 #endif
1037
1038   SMESH::NumericalFunctor_i* aNumericalFunctor =
1039     SMESH::DownCast<SMESH::NumericalFunctor_i*>( Criterion );
1040
1041   // Update Python script
1042   TPythonDump() << "isDone = " << this << ".QuadToTriObject( " << theObject << ", " << aNumericalFunctor << " )";
1043 #ifdef _DEBUG_
1044   TPythonDump() << "print 'QuadToTriObject: ', isDone";
1045 #endif
1046
1047   return isDone;
1048 }
1049
1050
1051 //=============================================================================
1052 /*!
1053  *
1054  */
1055 //=============================================================================
1056 CORBA::Boolean SMESH_MeshEditor_i::SplitQuad (const SMESH::long_array & IDsOfElements,
1057                                               CORBA::Boolean            Diag13)
1058 {
1059   initData();
1060
1061   SMESHDS_Mesh* aMesh = GetMeshDS();
1062   TIDSortedElemSet faces;
1063   arrayToSet(IDsOfElements, aMesh, faces, SMDSAbs_Face);
1064
1065   // Update Python script
1066   TPythonDump() << "isDone = " << this << ".SplitQuad( "
1067                 << IDsOfElements << ", " << Diag13 << " )";
1068 #ifdef _DEBUG_
1069   TPythonDump() << "print 'SplitQuad: ', isDone";
1070 #endif
1071
1072   ::SMESH_MeshEditor anEditor( myMesh );
1073   CORBA::Boolean stat = anEditor.QuadToTri( faces, Diag13 );
1074
1075   storeResult(anEditor);
1076
1077   return stat;
1078 }
1079
1080
1081 //=============================================================================
1082 /*!
1083  *
1084  */
1085 //=============================================================================
1086 CORBA::Boolean SMESH_MeshEditor_i::SplitQuadObject (SMESH::SMESH_IDSource_ptr theObject,
1087                                                     CORBA::Boolean            Diag13)
1088 {
1089   initData();
1090
1091   SMESH::long_array_var anElementsId = theObject->GetIDs();
1092   CORBA::Boolean isDone = SplitQuad(anElementsId, Diag13);
1093
1094   // Clear python line(s), created by SplitQuad()
1095   SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
1096   aSMESHGen->RemoveLastFromPythonScript(aSMESHGen->GetCurrentStudyID());
1097 #ifdef _DEBUG_
1098   aSMESHGen->RemoveLastFromPythonScript(aSMESHGen->GetCurrentStudyID());
1099 #endif
1100
1101   // Update Python script
1102   TPythonDump() << "isDone = " << this << ".SplitQuadObject( "
1103                 << theObject << ", " << Diag13 << " )";
1104 #ifdef _DEBUG_
1105   TPythonDump() << "print 'SplitQuadObject: ', isDone";
1106 #endif
1107
1108   return isDone;
1109 }
1110
1111
1112 //=============================================================================
1113 /*!
1114  *  BestSplit
1115  */
1116 //=============================================================================
1117 CORBA::Long SMESH_MeshEditor_i::BestSplit (CORBA::Long                 IDOfQuad,
1118                                            SMESH::NumericalFunctor_ptr Criterion)
1119 {
1120   const SMDS_MeshElement* quad = GetMeshDS()->FindElement(IDOfQuad);
1121   if (quad && quad->GetType() == SMDSAbs_Face && quad->NbNodes() == 4)
1122   {
1123     SMESH::NumericalFunctor_i* aNumericalFunctor =
1124       dynamic_cast<SMESH::NumericalFunctor_i*>(SMESH_Gen_i::GetServant(Criterion).in());
1125     SMESH::Controls::NumericalFunctorPtr aCrit;
1126     if (aNumericalFunctor)
1127       aCrit = aNumericalFunctor->GetNumericalFunctor();
1128     else
1129       aCrit.reset(new SMESH::Controls::AspectRatio());
1130
1131     ::SMESH_MeshEditor anEditor (myMesh);
1132     return anEditor.BestSplit(quad, aCrit);
1133   }
1134   return -1;
1135 }
1136
1137
1138 //=======================================================================
1139 //function : Smooth
1140 //purpose  :
1141 //=======================================================================
1142
1143 CORBA::Boolean
1144   SMESH_MeshEditor_i::Smooth(const SMESH::long_array &              IDsOfElements,
1145                              const SMESH::long_array &              IDsOfFixedNodes,
1146                              CORBA::Long                            MaxNbOfIterations,
1147                              CORBA::Double                          MaxAspectRatio,
1148                              SMESH::SMESH_MeshEditor::Smooth_Method Method)
1149 {
1150   return smooth( IDsOfElements, IDsOfFixedNodes, MaxNbOfIterations,
1151                 MaxAspectRatio, Method, false );
1152 }
1153
1154
1155 //=======================================================================
1156 //function : SmoothParametric
1157 //purpose  :
1158 //=======================================================================
1159
1160 CORBA::Boolean
1161   SMESH_MeshEditor_i::SmoothParametric(const SMESH::long_array &              IDsOfElements,
1162                                        const SMESH::long_array &              IDsOfFixedNodes,
1163                                        CORBA::Long                            MaxNbOfIterations,
1164                                        CORBA::Double                          MaxAspectRatio,
1165                                        SMESH::SMESH_MeshEditor::Smooth_Method Method)
1166 {
1167   return smooth( IDsOfElements, IDsOfFixedNodes, MaxNbOfIterations,
1168                 MaxAspectRatio, Method, true );
1169 }
1170
1171
1172 //=======================================================================
1173 //function : SmoothObject
1174 //purpose  :
1175 //=======================================================================
1176
1177 CORBA::Boolean
1178   SMESH_MeshEditor_i::SmoothObject(SMESH::SMESH_IDSource_ptr              theObject,
1179                                    const SMESH::long_array &              IDsOfFixedNodes,
1180                                    CORBA::Long                            MaxNbOfIterations,
1181                                    CORBA::Double                          MaxAspectRatio,
1182                                    SMESH::SMESH_MeshEditor::Smooth_Method Method)
1183 {
1184   return smoothObject (theObject, IDsOfFixedNodes, MaxNbOfIterations,
1185                        MaxAspectRatio, Method, false);
1186 }
1187
1188
1189 //=======================================================================
1190 //function : SmoothParametricObject
1191 //purpose  :
1192 //=======================================================================
1193
1194 CORBA::Boolean
1195   SMESH_MeshEditor_i::SmoothParametricObject(SMESH::SMESH_IDSource_ptr              theObject,
1196                                    const SMESH::long_array &              IDsOfFixedNodes,
1197                                    CORBA::Long                            MaxNbOfIterations,
1198                                    CORBA::Double                          MaxAspectRatio,
1199                                    SMESH::SMESH_MeshEditor::Smooth_Method Method)
1200 {
1201   return smoothObject (theObject, IDsOfFixedNodes, MaxNbOfIterations,
1202                        MaxAspectRatio, Method, true);
1203 }
1204
1205
1206 //=============================================================================
1207 /*!
1208  *
1209  */
1210 //=============================================================================
1211
1212 CORBA::Boolean
1213   SMESH_MeshEditor_i::smooth(const SMESH::long_array &              IDsOfElements,
1214                              const SMESH::long_array &              IDsOfFixedNodes,
1215                              CORBA::Long                            MaxNbOfIterations,
1216                              CORBA::Double                          MaxAspectRatio,
1217                              SMESH::SMESH_MeshEditor::Smooth_Method Method,
1218                              bool                                   IsParametric)
1219 {
1220   initData();
1221
1222   SMESHDS_Mesh* aMesh = GetMeshDS();
1223
1224   TIDSortedElemSet elements;
1225   arrayToSet(IDsOfElements, aMesh, elements, SMDSAbs_Face);
1226
1227   set<const SMDS_MeshNode*> fixedNodes;
1228   for (int i = 0; i < IDsOfFixedNodes.length(); i++) {
1229     CORBA::Long index = IDsOfFixedNodes[i];
1230     const SMDS_MeshNode * node = aMesh->FindNode(index);
1231     if ( node )
1232       fixedNodes.insert( node );
1233   }
1234   ::SMESH_MeshEditor::SmoothMethod method = ::SMESH_MeshEditor::LAPLACIAN;
1235   if ( Method != SMESH::SMESH_MeshEditor::LAPLACIAN_SMOOTH )
1236     method = ::SMESH_MeshEditor::CENTROIDAL;
1237
1238   ::SMESH_MeshEditor anEditor( myMesh );
1239   anEditor.Smooth(elements, fixedNodes, method,
1240                   MaxNbOfIterations, MaxAspectRatio, IsParametric );
1241
1242   storeResult(anEditor);
1243
1244   // Update Python script
1245   TPythonDump() << "isDone = " << this << "."
1246                 << (IsParametric ? "SmoothParametric( " : "Smooth( ")
1247                 << IDsOfElements << ", "     << IDsOfFixedNodes << ", "
1248                 << MaxNbOfIterations << ", " << MaxAspectRatio << ", "
1249                 << "SMESH.SMESH_MeshEditor."
1250                 << ( Method == SMESH::SMESH_MeshEditor::CENTROIDAL_SMOOTH ?
1251                      "CENTROIDAL_SMOOTH )" : "LAPLACIAN_SMOOTH )");
1252 #ifdef _DEBUG_
1253   TPythonDump() << "print 'Smooth: ', isDone";
1254 #endif
1255
1256   return true;
1257 }
1258
1259
1260 //=============================================================================
1261 /*!
1262  *
1263  */
1264 //=============================================================================
1265
1266 CORBA::Boolean
1267 SMESH_MeshEditor_i::smoothObject(SMESH::SMESH_IDSource_ptr              theObject,
1268                                  const SMESH::long_array &              IDsOfFixedNodes,
1269                                  CORBA::Long                            MaxNbOfIterations,
1270                                  CORBA::Double                          MaxAspectRatio,
1271                                  SMESH::SMESH_MeshEditor::Smooth_Method Method,
1272                                  bool                                   IsParametric)
1273 {
1274   initData();
1275
1276   SMESH::long_array_var anElementsId = theObject->GetIDs();
1277   CORBA::Boolean isDone = smooth (anElementsId, IDsOfFixedNodes, MaxNbOfIterations,
1278                                   MaxAspectRatio, Method, IsParametric);
1279
1280   // Clear python line(s), created by Smooth()
1281   SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
1282   aSMESHGen->RemoveLastFromPythonScript(aSMESHGen->GetCurrentStudyID());
1283 #ifdef _DEBUG_
1284   aSMESHGen->RemoveLastFromPythonScript(aSMESHGen->GetCurrentStudyID());
1285 #endif
1286
1287   // Update Python script
1288   TPythonDump() << "isDone = " << this << "."
1289                 << (IsParametric ? "SmoothParametricObject( " : "SmoothObject( ")
1290                 << theObject << ", " << IDsOfFixedNodes << ", "
1291                 << MaxNbOfIterations << ", " << MaxAspectRatio << ", "
1292                 << "SMESH.SMESH_MeshEditor."
1293                 << ( Method == SMESH::SMESH_MeshEditor::CENTROIDAL_SMOOTH ?
1294                      "CENTROIDAL_SMOOTH )" : "LAPLACIAN_SMOOTH )");
1295 #ifdef _DEBUG_
1296   TPythonDump() << "print 'SmoothObject: ', isDone";
1297 #endif
1298
1299   return isDone;
1300 }
1301
1302
1303 //=============================================================================
1304 /*!
1305  *
1306  */
1307 //=============================================================================
1308
1309 void SMESH_MeshEditor_i::RenumberNodes()
1310 {
1311   // Update Python script
1312   TPythonDump() << this << ".RenumberNodes()";
1313
1314   GetMeshDS()->Renumber( true );
1315 }
1316
1317
1318 //=============================================================================
1319 /*!
1320  *
1321  */
1322 //=============================================================================
1323
1324 void SMESH_MeshEditor_i::RenumberElements()
1325 {
1326   // Update Python script
1327   TPythonDump() << this << ".RenumberElements()";
1328
1329   GetMeshDS()->Renumber( false );
1330 }
1331
1332 //=======================================================================
1333   /*!
1334    * \brief Return groups by their IDs
1335    */
1336 //=======================================================================
1337
1338 SMESH::ListOfGroups* SMESH_MeshEditor_i::getGroups(const std::list<int>* groupIDs)
1339 {
1340   if ( !groupIDs )
1341     return 0;
1342   myMesh_i->CreateGroupServants();
1343   return myMesh_i->GetGroups( *groupIDs );
1344 }
1345
1346 //=======================================================================
1347 //function : rotationSweep
1348 //purpose  : 
1349 //=======================================================================
1350
1351 SMESH::ListOfGroups*
1352 SMESH_MeshEditor_i::rotationSweep(const SMESH::long_array & theIDsOfElements,
1353                                   const SMESH::AxisStruct & theAxis,
1354                                   CORBA::Double             theAngleInRadians,
1355                                   CORBA::Long               theNbOfSteps,
1356                                   CORBA::Double             theTolerance,
1357                                   const bool                theMakeGroups)
1358 {
1359   initData();
1360
1361   TIDSortedElemSet inElements, copyElements;
1362   arrayToSet(theIDsOfElements, GetMeshDS(), inElements);
1363
1364   TIDSortedElemSet* workElements = & inElements;
1365   TPreviewMesh      tmpMesh( SMDSAbs_Face );
1366   SMESH_Mesh*       mesh = 0;
1367   bool              makeWalls=true;
1368   if ( myPreviewMode )
1369   {
1370     SMDSAbs_ElementType select = SMDSAbs_All, avoid = SMDSAbs_Volume;
1371     tmpMesh.Copy( inElements, copyElements, select, avoid );
1372     mesh = &tmpMesh;
1373     workElements = & copyElements;
1374     //makeWalls = false;
1375   }
1376   else
1377   {
1378     mesh = myMesh;
1379   }
1380
1381   gp_Ax1 Ax1 (gp_Pnt( theAxis.x,  theAxis.y,  theAxis.z ),
1382               gp_Vec( theAxis.vx, theAxis.vy, theAxis.vz ));
1383
1384   ::SMESH_MeshEditor anEditor( mesh );
1385   ::SMESH_MeshEditor::PGroupIDs groupIds =
1386     anEditor.RotationSweep (*workElements, Ax1, theAngleInRadians,
1387                             theNbOfSteps, theTolerance, theMakeGroups, makeWalls);
1388   storeResult(anEditor);
1389
1390   return theMakeGroups ? getGroups(groupIds.get()) : 0;
1391 }
1392
1393 //=======================================================================
1394 //function : RotationSweep
1395 //purpose  :
1396 //=======================================================================
1397
1398 void SMESH_MeshEditor_i::RotationSweep(const SMESH::long_array & theIDsOfElements,
1399                                        const SMESH::AxisStruct & theAxis,
1400                                        CORBA::Double             theAngleInRadians,
1401                                        CORBA::Long               theNbOfSteps,
1402                                        CORBA::Double             theTolerance)
1403 {
1404   if ( !myPreviewMode ) {
1405     TPythonDump() << "axis = " << theAxis;
1406     TPythonDump() << this << ".RotationSweep( "
1407                   << theIDsOfElements
1408                   << ", axis, "
1409                   << theAngleInRadians << ", "
1410                   << theNbOfSteps << ", "
1411                   << theTolerance << " )";
1412   }
1413   rotationSweep(theIDsOfElements,
1414                 theAxis,
1415                 theAngleInRadians,
1416                 theNbOfSteps,
1417                 theTolerance,
1418                 false);
1419 }
1420
1421 //=======================================================================
1422 //function : RotationSweepMakeGroups
1423 //purpose  : 
1424 //=======================================================================
1425
1426 SMESH::ListOfGroups*
1427 SMESH_MeshEditor_i::RotationSweepMakeGroups(const SMESH::long_array& theIDsOfElements,
1428                                             const SMESH::AxisStruct& theAxis,
1429                                             CORBA::Double            theAngleInRadians,
1430                                             CORBA::Long              theNbOfSteps,
1431                                             CORBA::Double            theTolerance)
1432 {
1433   if ( !myPreviewMode ) {
1434     TPythonDump() << "axis = " << theAxis;
1435     TPythonDump() << this << ".RotationSweepMakeGroups( "
1436                   << theIDsOfElements
1437                   << ", axis, "
1438                   << theAngleInRadians << ", "
1439                   << theNbOfSteps << ", "
1440                   << theTolerance << " )";
1441   }
1442   return rotationSweep(theIDsOfElements,
1443                        theAxis,
1444                        theAngleInRadians,
1445                        theNbOfSteps,
1446                        theTolerance,
1447                        true);
1448 }
1449
1450 //=======================================================================
1451 //function : RotationSweepObject
1452 //purpose  :
1453 //=======================================================================
1454
1455 void SMESH_MeshEditor_i::RotationSweepObject(SMESH::SMESH_IDSource_ptr theObject,
1456                                              const SMESH::AxisStruct & theAxis,
1457                                              CORBA::Double             theAngleInRadians,
1458                                              CORBA::Long               theNbOfSteps,
1459                                              CORBA::Double             theTolerance)
1460 {
1461   if ( !myPreviewMode ) {
1462     TPythonDump() << "axis = " << theAxis;
1463     TPythonDump() << this << ".RotationSweepObject( "
1464                   << theObject
1465                   << ", axis, "
1466                   << theAngleInRadians << ", "
1467                   << theNbOfSteps << ", "
1468                   << theTolerance << " )";
1469   }
1470   SMESH::long_array_var anElementsId = theObject->GetIDs();
1471   rotationSweep(anElementsId,
1472                 theAxis,
1473                 theAngleInRadians,
1474                 theNbOfSteps,
1475                 theTolerance,
1476                 false);
1477 }
1478
1479 //=======================================================================
1480 //function : RotationSweepObjectMakeGroups
1481 //purpose  : 
1482 //=======================================================================
1483
1484 SMESH::ListOfGroups*
1485 SMESH_MeshEditor_i::RotationSweepObjectMakeGroups(SMESH::SMESH_IDSource_ptr theObject,
1486                                                   const SMESH::AxisStruct&  theAxis,
1487                                                   CORBA::Double             theAngleInRadians,
1488                                                   CORBA::Long               theNbOfSteps,
1489                                                   CORBA::Double             theTolerance)
1490 {
1491   if ( !myPreviewMode ) {
1492     TPythonDump() << "axis = " << theAxis;
1493     TPythonDump() << this << ".RotationSweepObjectMakeGroups( "
1494                   << theObject
1495                   << ", axis, "
1496                   << theAngleInRadians << ", "
1497                   << theNbOfSteps << ", "
1498                   << theTolerance << " )";
1499   }
1500   SMESH::long_array_var anElementsId = theObject->GetIDs();
1501   return rotationSweep(anElementsId,
1502                        theAxis,
1503                        theAngleInRadians,
1504                        theNbOfSteps,
1505                        theTolerance,
1506                        true);
1507 }
1508
1509
1510 //=======================================================================
1511 //function : extrusionSweep
1512 //purpose  : 
1513 //=======================================================================
1514
1515 SMESH::ListOfGroups*
1516 SMESH_MeshEditor_i::extrusionSweep(const SMESH::long_array & theIDsOfElements,
1517                                    const SMESH::DirStruct &  theStepVector,
1518                                    CORBA::Long               theNbOfSteps,
1519                                    const bool                theMakeGroups,
1520                                    const SMDSAbs_ElementType theElementType)
1521 {
1522   initData();
1523
1524   try {   
1525 #ifdef NO_CAS_CATCH
1526     OCC_CATCH_SIGNALS;
1527 #endif
1528     TIDSortedElemSet elements;
1529     arrayToSet(theIDsOfElements, GetMeshDS(), elements, theElementType);
1530
1531     const SMESH::PointStruct * P = &theStepVector.PS;
1532     gp_Vec stepVec( P->x, P->y, P->z );
1533
1534     TElemOfElemListMap aHystory;
1535     ::SMESH_MeshEditor anEditor( myMesh );
1536     ::SMESH_MeshEditor::PGroupIDs groupIds =
1537         anEditor.ExtrusionSweep (elements, stepVec, theNbOfSteps, aHystory, theMakeGroups);
1538
1539     storeResult(anEditor);
1540
1541     return theMakeGroups ? getGroups(groupIds.get()) : 0;
1542
1543   } catch(Standard_Failure) {
1544     Handle(Standard_Failure) aFail = Standard_Failure::Caught();          
1545     INFOS( "SMESH_MeshEditor_i::ExtrusionSweep fails - "<< aFail->GetMessageString() );
1546   }
1547   return 0;
1548 }
1549
1550 //=======================================================================
1551 //function : ExtrusionSweep
1552 //purpose  :
1553 //=======================================================================
1554
1555 void SMESH_MeshEditor_i::ExtrusionSweep(const SMESH::long_array & theIDsOfElements,
1556                                         const SMESH::DirStruct &  theStepVector,
1557                                         CORBA::Long               theNbOfSteps)
1558 {
1559   extrusionSweep (theIDsOfElements, theStepVector, theNbOfSteps, false );
1560   if ( !myPreviewMode ) {
1561     TPythonDump() << "stepVector = " << theStepVector;
1562     TPythonDump() << this << ".ExtrusionSweep( "
1563                   << theIDsOfElements << ", stepVector, " << theNbOfSteps << " )";
1564   }
1565 }
1566
1567
1568 //=======================================================================
1569 //function : ExtrusionSweepObject
1570 //purpose  :
1571 //=======================================================================
1572
1573 void SMESH_MeshEditor_i::ExtrusionSweepObject(SMESH::SMESH_IDSource_ptr theObject,
1574                                               const SMESH::DirStruct &  theStepVector,
1575                                               CORBA::Long               theNbOfSteps)
1576 {
1577   SMESH::long_array_var anElementsId = theObject->GetIDs();
1578   extrusionSweep (anElementsId, theStepVector, theNbOfSteps, false );
1579   if ( !myPreviewMode ) {
1580     TPythonDump() << "stepVector = " << theStepVector;
1581     TPythonDump() << this << ".ExtrusionSweepObject( "
1582                   << theObject << ", stepVector, " << theNbOfSteps << " )";
1583   }
1584 }
1585
1586 //=======================================================================
1587 //function : ExtrusionSweepObject1D
1588 //purpose  :
1589 //=======================================================================
1590
1591 void SMESH_MeshEditor_i::ExtrusionSweepObject1D(SMESH::SMESH_IDSource_ptr theObject,
1592                                                 const SMESH::DirStruct &  theStepVector,
1593                                                 CORBA::Long               theNbOfSteps)
1594 {
1595   SMESH::long_array_var anElementsId = theObject->GetIDs();
1596   extrusionSweep (anElementsId, theStepVector, theNbOfSteps, false, SMDSAbs_Edge );
1597   if ( !myPreviewMode ) {
1598     TPythonDump() << "stepVector = " << theStepVector;
1599     TPythonDump() << this << ".ExtrusionSweepObject1D( "
1600                   << theObject << ", stepVector, " << theNbOfSteps << " )";
1601   }
1602 }
1603
1604 //=======================================================================
1605 //function : ExtrusionSweepObject2D
1606 //purpose  :
1607 //=======================================================================
1608
1609 void SMESH_MeshEditor_i::ExtrusionSweepObject2D(SMESH::SMESH_IDSource_ptr theObject,
1610                                                 const SMESH::DirStruct &  theStepVector,
1611                                                 CORBA::Long               theNbOfSteps)
1612 {
1613   SMESH::long_array_var anElementsId = theObject->GetIDs();
1614   extrusionSweep (anElementsId, theStepVector, theNbOfSteps, false, SMDSAbs_Face );
1615   if ( !myPreviewMode ) {
1616     TPythonDump() << "stepVector = " << theStepVector;
1617     TPythonDump() << this << ".ExtrusionSweepObject2D( "
1618                   << theObject << ", stepVector, " << theNbOfSteps << " )";
1619   }
1620 }
1621
1622 //=======================================================================
1623 //function : ExtrusionSweepMakeGroups
1624 //purpose  : 
1625 //=======================================================================
1626
1627 SMESH::ListOfGroups*
1628 SMESH_MeshEditor_i::ExtrusionSweepMakeGroups(const SMESH::long_array& theIDsOfElements,
1629                                              const SMESH::DirStruct&  theStepVector,
1630                                              CORBA::Long              theNbOfSteps)
1631 {
1632   if ( !myPreviewMode ) {
1633     TPythonDump() << "stepVector = " << theStepVector;
1634     TPythonDump() << this << ".ExtrusionSweepMakeGroups( "
1635                   << theIDsOfElements << ", stepVector, " << theNbOfSteps << " )";
1636   }
1637   return extrusionSweep (theIDsOfElements, theStepVector, theNbOfSteps, true );
1638 }
1639 //=======================================================================
1640 //function : ExtrusionSweepObjectMakeGroups
1641 //purpose  : 
1642 //=======================================================================
1643
1644 SMESH::ListOfGroups*
1645 SMESH_MeshEditor_i::ExtrusionSweepObjectMakeGroups(SMESH::SMESH_IDSource_ptr theObject,
1646                                                    const SMESH::DirStruct&   theStepVector,
1647                                                    CORBA::Long               theNbOfSteps)
1648 {
1649   if ( !myPreviewMode ) {
1650     TPythonDump() << "stepVector = " << theStepVector;
1651     TPythonDump() << this << ".ExtrusionSweepObjectMakeGroups( "
1652                   << theObject << ", stepVector, " << theNbOfSteps << " )";
1653   }
1654   SMESH::long_array_var anElementsId = theObject->GetIDs();
1655   return extrusionSweep (anElementsId, theStepVector, theNbOfSteps, true );
1656 }
1657
1658 //=======================================================================
1659 //function : ExtrusionSweepObject1DMakeGroups
1660 //purpose  : 
1661 //=======================================================================
1662
1663 SMESH::ListOfGroups*
1664 SMESH_MeshEditor_i::ExtrusionSweepObject1DMakeGroups(SMESH::SMESH_IDSource_ptr theObject,
1665                                                      const SMESH::DirStruct&   theStepVector,
1666                                                      CORBA::Long               theNbOfSteps)
1667 {
1668   if ( !myPreviewMode ) {
1669     TPythonDump() << "stepVector = " << theStepVector;
1670     TPythonDump() << this << ".ExtrusionSweepObject1DMakeGroups( "
1671                   << theObject << ", stepVector, " << theNbOfSteps << " )";
1672   }
1673   SMESH::long_array_var anElementsId = theObject->GetIDs();
1674   return extrusionSweep (anElementsId, theStepVector, theNbOfSteps, true, SMDSAbs_Edge );
1675 }
1676
1677 //=======================================================================
1678 //function : ExtrusionSweepObject2DMakeGroups
1679 //purpose  : 
1680 //=======================================================================
1681
1682 SMESH::ListOfGroups*
1683 SMESH_MeshEditor_i::ExtrusionSweepObject2DMakeGroups(SMESH::SMESH_IDSource_ptr theObject,
1684                                                      const SMESH::DirStruct&   theStepVector,
1685                                                      CORBA::Long               theNbOfSteps)
1686 {
1687   if ( !myPreviewMode ) {
1688     TPythonDump() << "stepVector = " << theStepVector;
1689     TPythonDump() << this << ".ExtrusionSweepObject2DMakeGroups( "
1690                   << theObject << ", stepVector, " << theNbOfSteps << " )";
1691   }
1692   SMESH::long_array_var anElementsId = theObject->GetIDs();
1693   return extrusionSweep (anElementsId, theStepVector, theNbOfSteps, true, SMDSAbs_Face );
1694 }
1695
1696
1697 //=======================================================================
1698 //function : advancedExtrusion
1699 //purpose  : 
1700 //=======================================================================
1701
1702 SMESH::ListOfGroups*
1703 SMESH_MeshEditor_i::advancedExtrusion(const SMESH::long_array & theIDsOfElements,
1704                                       const SMESH::DirStruct &  theStepVector,
1705                                       CORBA::Long               theNbOfSteps,
1706                                       CORBA::Long               theExtrFlags,
1707                                       CORBA::Double             theSewTolerance,
1708                                       const bool                theMakeGroups)
1709 {
1710   initData();
1711
1712   TIDSortedElemSet elements;
1713   arrayToSet(theIDsOfElements, GetMeshDS(), elements);
1714
1715   const SMESH::PointStruct * P = &theStepVector.PS;
1716   gp_Vec stepVec( P->x, P->y, P->z );
1717
1718   ::SMESH_MeshEditor anEditor( myMesh );
1719   TElemOfElemListMap aHystory;
1720   ::SMESH_MeshEditor::PGroupIDs groupIds =
1721       anEditor.ExtrusionSweep (elements, stepVec, theNbOfSteps, aHystory,
1722                                theMakeGroups, theExtrFlags, theSewTolerance);
1723   storeResult(anEditor);
1724
1725   return theMakeGroups ? getGroups(groupIds.get()) : 0;
1726 }
1727
1728 //=======================================================================
1729 //function : AdvancedExtrusion
1730 //purpose  :
1731 //=======================================================================
1732
1733 void SMESH_MeshEditor_i::AdvancedExtrusion(const SMESH::long_array & theIDsOfElements,
1734                                            const SMESH::DirStruct &  theStepVector,
1735                                            CORBA::Long               theNbOfSteps,
1736                                            CORBA::Long               theExtrFlags,
1737                                            CORBA::Double             theSewTolerance)
1738 {
1739   if ( !myPreviewMode ) {
1740     TPythonDump() << "stepVector = " << theStepVector;
1741     TPythonDump() << this << ".AdvancedExtrusion("
1742                   << theIDsOfElements
1743                   << ", stepVector, "
1744                   << theNbOfSteps << ","
1745                   << theExtrFlags << ", "
1746                   << theSewTolerance <<  " )";
1747   }
1748   advancedExtrusion( theIDsOfElements,
1749                      theStepVector,
1750                      theNbOfSteps,
1751                      theExtrFlags,
1752                      theSewTolerance,
1753                      false);
1754 }
1755
1756 //=======================================================================
1757 //function : AdvancedExtrusionMakeGroups
1758 //purpose  : 
1759 //=======================================================================
1760
1761 SMESH::ListOfGroups*
1762 SMESH_MeshEditor_i::AdvancedExtrusionMakeGroups(const SMESH::long_array& theIDsOfElements,
1763                                                 const SMESH::DirStruct&  theStepVector,
1764                                                 CORBA::Long              theNbOfSteps,
1765                                                 CORBA::Long              theExtrFlags,
1766                                                 CORBA::Double            theSewTolerance)
1767 {
1768   if ( !myPreviewMode ) {
1769     TPythonDump() << "stepVector = " << theStepVector;
1770     TPythonDump() << this << ".AdvancedExtrusionMakeGroups("
1771                   << theIDsOfElements
1772                   << ", stepVector, "
1773                   << theNbOfSteps << ","
1774                   << theExtrFlags << ", "
1775                   << theSewTolerance <<  " )";
1776   }
1777   return advancedExtrusion( theIDsOfElements,
1778                             theStepVector,
1779                             theNbOfSteps,
1780                             theExtrFlags,
1781                             theSewTolerance,
1782                             true);
1783 }
1784
1785
1786 //================================================================================
1787 /*!
1788  * \brief Convert extrusion error to IDL enum
1789  */
1790 //================================================================================
1791
1792 #define RETCASE(enm) case ::SMESH_MeshEditor::enm: return SMESH::SMESH_MeshEditor::enm;
1793
1794 static SMESH::SMESH_MeshEditor::Extrusion_Error convExtrError( const::SMESH_MeshEditor::Extrusion_Error e )
1795 {
1796   switch ( e ) {
1797   RETCASE( EXTR_OK );
1798   RETCASE( EXTR_NO_ELEMENTS );
1799   RETCASE( EXTR_PATH_NOT_EDGE );
1800   RETCASE( EXTR_BAD_PATH_SHAPE );
1801   RETCASE( EXTR_BAD_STARTING_NODE );
1802   RETCASE( EXTR_BAD_ANGLES_NUMBER );
1803   RETCASE( EXTR_CANT_GET_TANGENT );
1804   }
1805   return SMESH::SMESH_MeshEditor::EXTR_OK;
1806 }
1807
1808
1809 //=======================================================================
1810 //function : extrusionAlongPath
1811 //purpose  : 
1812 //=======================================================================
1813
1814 SMESH::ListOfGroups*
1815 SMESH_MeshEditor_i::extrusionAlongPath(const SMESH::long_array &   theIDsOfElements,
1816                                        SMESH::SMESH_Mesh_ptr       thePathMesh,
1817                                        GEOM::GEOM_Object_ptr       thePathShape,
1818                                        CORBA::Long                 theNodeStart,
1819                                        CORBA::Boolean              theHasAngles,
1820                                        const SMESH::double_array & theAngles,
1821                                        CORBA::Boolean              theHasRefPoint,
1822                                        const SMESH::PointStruct &  theRefPoint,
1823                                        const bool                  theMakeGroups,
1824                                        SMESH::SMESH_MeshEditor::Extrusion_Error & theError)
1825 {
1826   initData();
1827
1828   if ( thePathMesh->_is_nil() || thePathShape->_is_nil() ) {
1829     theError = SMESH::SMESH_MeshEditor::EXTR_BAD_PATH_SHAPE;
1830     return 0;
1831   }
1832   SMESH_Mesh_i* aMeshImp = SMESH::DownCast<SMESH_Mesh_i*>( thePathMesh );
1833
1834   TopoDS_Shape aShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( thePathShape );
1835   SMESH_subMesh* aSubMesh = aMeshImp->GetImpl().GetSubMesh( aShape );
1836
1837   if ( !aSubMesh || !aSubMesh->GetSubMeshDS()) {
1838     theError = SMESH::SMESH_MeshEditor::EXTR_BAD_PATH_SHAPE;
1839     return 0;
1840   }
1841
1842   SMDS_MeshNode* nodeStart = (SMDS_MeshNode*)aMeshImp->GetImpl().GetMeshDS()->FindNode(theNodeStart);
1843   if ( !nodeStart ) {
1844     theError = SMESH::SMESH_MeshEditor::EXTR_BAD_STARTING_NODE;
1845     return 0;
1846   }
1847
1848   TIDSortedElemSet elements;
1849   arrayToSet(theIDsOfElements, GetMeshDS(), elements);
1850
1851   list<double> angles;
1852   for (int i = 0; i < theAngles.length(); i++) {
1853     angles.push_back( theAngles[i] );
1854   }
1855
1856   gp_Pnt refPnt( theRefPoint.x, theRefPoint.y, theRefPoint.z );
1857
1858   int nbOldGroups = myMesh->NbGroup();
1859
1860   ::SMESH_MeshEditor anEditor( myMesh );
1861   ::SMESH_MeshEditor::Extrusion_Error error =
1862       anEditor.ExtrusionAlongTrack( elements, aSubMesh, nodeStart,
1863                                     theHasAngles, angles,
1864                                     theHasRefPoint, refPnt, theMakeGroups );
1865   storeResult(anEditor);
1866   theError = convExtrError( error );
1867
1868   if ( theMakeGroups ) {
1869     list<int> groupIDs = myMesh->GetGroupIds();
1870     list<int>::iterator newBegin = groupIDs.begin();
1871     std::advance( newBegin, nbOldGroups ); // skip old groups
1872     groupIDs.erase( groupIDs.begin(), newBegin );
1873     return getGroups( & groupIDs );
1874   }
1875   return 0;
1876 }
1877
1878 //=======================================================================
1879 //function : ExtrusionAlongPath
1880 //purpose  :
1881 //=======================================================================
1882
1883 SMESH::SMESH_MeshEditor::Extrusion_Error
1884   SMESH_MeshEditor_i::ExtrusionAlongPath(const SMESH::long_array &   theIDsOfElements,
1885                                          SMESH::SMESH_Mesh_ptr       thePathMesh,
1886                                          GEOM::GEOM_Object_ptr       thePathShape,
1887                                          CORBA::Long                 theNodeStart,
1888                                          CORBA::Boolean              theHasAngles,
1889                                          const SMESH::double_array & theAngles,
1890                                          CORBA::Boolean              theHasRefPoint,
1891                                          const SMESH::PointStruct &  theRefPoint)
1892 {
1893   if ( !myPreviewMode ) {
1894     TPythonDump() << "rotAngles = " << theAngles;
1895
1896     if ( theHasRefPoint )
1897       TPythonDump() << "refPoint = SMESH.PointStruct( "
1898                     << theRefPoint.x << ", "
1899                     << theRefPoint.y << ", "
1900                     << theRefPoint.z << " )";
1901     else
1902       TPythonDump() << "refPoint = SMESH.PointStruct( 0,0,0 )";
1903
1904     TPythonDump() << "error = " << this << ".ExtrusionAlongPath( "
1905                   << theIDsOfElements << ", "
1906                   << thePathMesh      << ", "
1907                   << thePathShape     << ", "
1908                   << theNodeStart     << ", "
1909                   << theHasAngles     << ", "
1910                   << "rotAngles"      << ", "
1911                   << theHasRefPoint   << ", refPoint )";
1912   }
1913   SMESH::SMESH_MeshEditor::Extrusion_Error anError;
1914   extrusionAlongPath( theIDsOfElements,
1915                       thePathMesh,
1916                       thePathShape,
1917                       theNodeStart,
1918                       theHasAngles,
1919                       theAngles,
1920                       theHasRefPoint,
1921                       theRefPoint,
1922                       false,
1923                       anError);
1924   return anError;
1925 }
1926
1927 //=======================================================================
1928 //function : ExtrusionAlongPathObject
1929 //purpose  :
1930 //=======================================================================
1931
1932 SMESH::SMESH_MeshEditor::Extrusion_Error
1933 SMESH_MeshEditor_i::ExtrusionAlongPathObject(SMESH::SMESH_IDSource_ptr   theObject,
1934                                              SMESH::SMESH_Mesh_ptr       thePathMesh,
1935                                              GEOM::GEOM_Object_ptr       thePathShape,
1936                                              CORBA::Long                 theNodeStart,
1937                                              CORBA::Boolean              theHasAngles,
1938                                              const SMESH::double_array & theAngles,
1939                                              CORBA::Boolean              theHasRefPoint,
1940                                              const SMESH::PointStruct &  theRefPoint)
1941 {
1942   if ( !myPreviewMode ) {
1943     TPythonDump() << "rotAngles = " << theAngles;
1944
1945     if ( theHasRefPoint )
1946       TPythonDump() << "refPoint = SMESH.PointStruct( "
1947                     << theRefPoint.x << ", "
1948                     << theRefPoint.y << ", "
1949                     << theRefPoint.z << " )";
1950     else
1951       TPythonDump() << "refPoint = SMESH.PointStruct( 0,0,0 )";
1952
1953     TPythonDump() << "error = " << this << ".ExtrusionAlongPathObject( "
1954                   << theObject        << ", "
1955                   << thePathMesh      << ", "
1956                   << thePathShape     << ", "
1957                   << theNodeStart     << ", "
1958                   << theHasAngles     << ", "
1959                   << "rotAngles"      << ", "
1960                   << theHasRefPoint   << ", refPoint )";
1961   }
1962   SMESH::SMESH_MeshEditor::Extrusion_Error anError;
1963   SMESH::long_array_var anElementsId = theObject->GetIDs();
1964   extrusionAlongPath( anElementsId,
1965                       thePathMesh,
1966                       thePathShape,
1967                       theNodeStart,
1968                       theHasAngles,
1969                       theAngles,
1970                       theHasRefPoint,
1971                       theRefPoint,
1972                       false,
1973                       anError);
1974   return anError;
1975 }
1976
1977
1978 //=======================================================================
1979 //function : ExtrusionAlongPathMakeGroups
1980 //purpose  : 
1981 //=======================================================================
1982
1983 SMESH::ListOfGroups*
1984 SMESH_MeshEditor_i::ExtrusionAlongPathMakeGroups(const SMESH::long_array&   theIDsOfElements,
1985                                                  SMESH::SMESH_Mesh_ptr      thePathMesh,
1986                                                  GEOM::GEOM_Object_ptr      thePathShape,
1987                                                  CORBA::Long                theNodeStart,
1988                                                  CORBA::Boolean             theHasAngles,
1989                                                  const SMESH::double_array& theAngles,
1990                                                  CORBA::Boolean             theHasRefPoint,
1991                                                  const SMESH::PointStruct&  theRefPoint,
1992                                                  SMESH::SMESH_MeshEditor::Extrusion_Error& Error)
1993 {
1994   if ( !myPreviewMode ) {
1995     TPythonDump() << "rotAngles = " << theAngles;
1996
1997     if ( theHasRefPoint )
1998       TPythonDump() << "refPoint = SMESH.PointStruct( "
1999                     << theRefPoint.x << ", "
2000                     << theRefPoint.y << ", "
2001                     << theRefPoint.z << " )";
2002     else
2003       TPythonDump() << "refPoint = SMESH.PointStruct( 0,0,0 )";
2004
2005     TPythonDump() << "groups = " << this << ".ExtrusionAlongPathMakeGroups( "
2006                   << theIDsOfElements << ", "
2007                   << thePathMesh      << ", "
2008                   << thePathShape     << ", "
2009                   << theNodeStart     << ", "
2010                   << theHasAngles     << ", "
2011                   << "rotAngles"      << ", "
2012                   << theHasRefPoint   << ", refPoint )";
2013   }
2014   return extrusionAlongPath( theIDsOfElements,
2015                              thePathMesh,
2016                              thePathShape,
2017                              theNodeStart,
2018                              theHasAngles,
2019                              theAngles,
2020                              theHasRefPoint,
2021                              theRefPoint,
2022                              true,
2023                              Error);
2024 }
2025
2026 //=======================================================================
2027 //function : ExtrusionAlongPathObjectMakeGroups
2028 //purpose  : 
2029 //=======================================================================
2030
2031 SMESH::ListOfGroups* SMESH_MeshEditor_i::
2032 ExtrusionAlongPathObjectMakeGroups(SMESH::SMESH_IDSource_ptr  theObject,
2033                                    SMESH::SMESH_Mesh_ptr      thePathMesh,
2034                                    GEOM::GEOM_Object_ptr      thePathShape,
2035                                    CORBA::Long                theNodeStart,
2036                                    CORBA::Boolean             theHasAngles,
2037                                    const SMESH::double_array& theAngles,
2038                                    CORBA::Boolean             theHasRefPoint,
2039                                    const SMESH::PointStruct&  theRefPoint,
2040                                    SMESH::SMESH_MeshEditor::Extrusion_Error& Error)
2041 {
2042   if ( !myPreviewMode ) {
2043     TPythonDump() << "rotAngles = " << theAngles;
2044
2045     if ( theHasRefPoint )
2046       TPythonDump() << "refPoint = SMESH.PointStruct( "
2047                     << theRefPoint.x << ", "
2048                     << theRefPoint.y << ", "
2049                     << theRefPoint.z << " )";
2050     else
2051       TPythonDump() << "refPoint = SMESH.PointStruct( 0,0,0 )";
2052
2053     TPythonDump() << "groups = " << this << ".ExtrusionAlongPathObjectMakeGroups( "
2054                   << theObject << ", "
2055                   << thePathMesh      << ", "
2056                   << thePathShape     << ", "
2057                   << theNodeStart     << ", "
2058                   << theHasAngles     << ", "
2059                   << "rotAngles"      << ", "
2060                   << theHasRefPoint   << ", refPoint )";
2061   }
2062   SMESH::long_array_var anElementsId = theObject->GetIDs();
2063   return extrusionAlongPath( anElementsId,
2064                              thePathMesh,
2065                              thePathShape,
2066                              theNodeStart,
2067                              theHasAngles,
2068                              theAngles,
2069                              theHasRefPoint,
2070                              theRefPoint,
2071                              true,
2072                              Error);
2073 }
2074
2075 //================================================================================
2076 /*!
2077  * \brief Compute rotation angles for ExtrusionAlongPath as linear variation
2078  * of given angles along path steps
2079   * \param PathMesh mesh containing a 1D sub-mesh on the edge, along 
2080   *                which proceeds the extrusion
2081   * \param PathShape is shape(edge); as the mesh can be complex, the edge 
2082   *                 is used to define the sub-mesh for the path
2083  */
2084 //================================================================================
2085
2086 SMESH::double_array*
2087 SMESH_MeshEditor_i::LinearAnglesVariation(SMESH::SMESH_Mesh_ptr       thePathMesh,
2088                                           GEOM::GEOM_Object_ptr       thePathShape,
2089                                           const SMESH::double_array & theAngles)
2090 {
2091   SMESH::double_array_var aResult = new SMESH::double_array();
2092   int nbAngles = theAngles.length();
2093   if ( nbAngles > 0 && !thePathMesh->_is_nil() && !thePathShape->_is_nil() )
2094   {
2095     SMESH_Mesh_i* aMeshImp = SMESH::DownCast<SMESH_Mesh_i*>( thePathMesh );
2096     TopoDS_Shape aShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( thePathShape );
2097     SMESH_subMesh* aSubMesh = aMeshImp->GetImpl().GetSubMesh( aShape );
2098     if ( !aSubMesh || !aSubMesh->GetSubMeshDS())
2099       return aResult._retn();
2100     int nbSteps = aSubMesh->GetSubMeshDS()->NbElements();
2101     if ( nbSteps == nbAngles )
2102     {
2103       aResult.inout() = theAngles;
2104     }
2105     else
2106     {
2107       aResult->length( nbSteps );
2108       double rAn2St = double( nbAngles ) / double( nbSteps );
2109       double angPrev = 0, angle;
2110       for ( int iSt = 0; iSt < nbSteps; ++iSt )
2111       {
2112         double angCur = rAn2St * ( iSt+1 );
2113         double angCurFloor  = floor( angCur );
2114         double angPrevFloor = floor( angPrev );
2115         if ( angPrevFloor == angCurFloor )
2116           angle = rAn2St * theAngles[ int( angCurFloor ) ];
2117         else
2118         {
2119           int iP = int( angPrevFloor );
2120           double angPrevCeil = ceil(angPrev);
2121           angle = ( angPrevCeil - angPrev ) * theAngles[ iP ];
2122           
2123           int iC = int( angCurFloor );
2124           if ( iC < nbAngles )
2125             angle += ( angCur - angCurFloor ) * theAngles[ iC ];
2126
2127           iP = int( angPrevCeil );
2128           while ( iC-- > iP )
2129             angle += theAngles[ iC ];
2130         }
2131         aResult[ iSt ] = angle;
2132         angPrev = angCur;
2133       }
2134     }
2135   }
2136   // Update Python script
2137   TPythonDump() << "rotAngles = " << theAngles;
2138   TPythonDump() << "rotAngles = " << this << ".LinearAnglesVariation( "
2139                 << thePathMesh  << ", "
2140                 << thePathShape << ", "
2141                 << "rotAngles )";
2142
2143   return aResult._retn();
2144 }
2145
2146
2147 //=======================================================================
2148 //function : mirror
2149 //purpose  : 
2150 //=======================================================================
2151
2152 SMESH::ListOfGroups*
2153 SMESH_MeshEditor_i::mirror(const SMESH::long_array &           theIDsOfElements,
2154                            const SMESH::AxisStruct &           theAxis,
2155                            SMESH::SMESH_MeshEditor::MirrorType theMirrorType,
2156                            CORBA::Boolean                      theCopy,
2157                            const bool                          theMakeGroups,
2158                            ::SMESH_Mesh*                       theTargetMesh)
2159 {
2160   initData();
2161
2162   TIDSortedElemSet elements;
2163   arrayToSet(theIDsOfElements, GetMeshDS(), elements);
2164
2165   gp_Pnt P ( theAxis.x, theAxis.y, theAxis.z );
2166   gp_Vec V ( theAxis.vx, theAxis.vy, theAxis.vz );
2167
2168   gp_Trsf aTrsf;
2169   switch ( theMirrorType ) {
2170   case  SMESH::SMESH_MeshEditor::POINT:
2171     aTrsf.SetMirror( P );
2172     break;
2173   case  SMESH::SMESH_MeshEditor::AXIS:
2174     aTrsf.SetMirror( gp_Ax1( P, V ));
2175     break;
2176   default:
2177     aTrsf.SetMirror( gp_Ax2( P, V ));
2178   }
2179
2180   ::SMESH_MeshEditor anEditor( myMesh );
2181   ::SMESH_MeshEditor::PGroupIDs groupIds =
2182       anEditor.Transform (elements, aTrsf, theCopy, theMakeGroups, theTargetMesh);
2183
2184   if(theCopy) {
2185     storeResult(anEditor);
2186   }
2187   return theMakeGroups ? getGroups(groupIds.get()) : 0;
2188 }
2189
2190 //=======================================================================
2191 //function : Mirror
2192 //purpose  :
2193 //=======================================================================
2194
2195 void SMESH_MeshEditor_i::Mirror(const SMESH::long_array &           theIDsOfElements,
2196                                 const SMESH::AxisStruct &           theAxis,
2197                                 SMESH::SMESH_MeshEditor::MirrorType theMirrorType,
2198                                 CORBA::Boolean                      theCopy)
2199 {
2200   if ( !myPreviewMode ) {
2201     TPythonDump() << this << ".Mirror( "
2202                   << theIDsOfElements << ", "
2203                   << theAxis          << ", "
2204                   << mirrorTypeName(theMirrorType) << ", "
2205                   << theCopy          << " )";
2206   }
2207   mirror(theIDsOfElements, theAxis, theMirrorType, theCopy, false);
2208 }
2209
2210
2211 //=======================================================================
2212 //function : MirrorObject
2213 //purpose  :
2214 //=======================================================================
2215
2216 void SMESH_MeshEditor_i::MirrorObject(SMESH::SMESH_IDSource_ptr           theObject,
2217                                       const SMESH::AxisStruct &           theAxis,
2218                                       SMESH::SMESH_MeshEditor::MirrorType theMirrorType,
2219                                       CORBA::Boolean                      theCopy)
2220 {
2221   if ( !myPreviewMode ) {
2222     TPythonDump() << this << ".MirrorObject( "
2223                   << theObject << ", "
2224                   << theAxis   << ", "
2225                   << mirrorTypeName(theMirrorType) << ", "
2226                   << theCopy   << " )";
2227   }
2228   SMESH::long_array_var anElementsId = theObject->GetIDs();
2229   mirror(anElementsId, theAxis, theMirrorType, theCopy, false);
2230 }
2231
2232 //=======================================================================
2233 //function : MirrorMakeGroups
2234 //purpose  : 
2235 //=======================================================================
2236
2237 SMESH::ListOfGroups*
2238 SMESH_MeshEditor_i::MirrorMakeGroups(const SMESH::long_array&            theIDsOfElements,
2239                                      const SMESH::AxisStruct&            theMirror,
2240                                      SMESH::SMESH_MeshEditor::MirrorType theMirrorType)
2241 {
2242   if ( !myPreviewMode ) {
2243     TPythonDump() << this << ".MirrorMakeGroups( "
2244                   << theIDsOfElements << ", "
2245                   << theMirror          << ", "
2246                   << mirrorTypeName(theMirrorType) << " )";
2247   }
2248   return mirror(theIDsOfElements, theMirror, theMirrorType, true, true);
2249 }
2250
2251 //=======================================================================
2252 //function : MirrorObjectMakeGroups
2253 //purpose  : 
2254 //=======================================================================
2255
2256 SMESH::ListOfGroups*
2257 SMESH_MeshEditor_i::MirrorObjectMakeGroups(SMESH::SMESH_IDSource_ptr           theObject,
2258                                            const SMESH::AxisStruct&            theMirror,
2259                                            SMESH::SMESH_MeshEditor::MirrorType theMirrorType)
2260 {
2261   if ( !myPreviewMode ) {
2262     TPythonDump() << this << ".MirrorObjectMakeGroups( "
2263                   << theObject << ", "
2264                   << theMirror   << ", "
2265                   << mirrorTypeName(theMirrorType) << " )";
2266   }
2267   SMESH::long_array_var anElementsId = theObject->GetIDs();
2268   return mirror(anElementsId, theMirror, theMirrorType, true, true);
2269 }
2270
2271 //=======================================================================
2272 //function : MirrorMakeMesh
2273 //purpose  : 
2274 //=======================================================================
2275
2276 SMESH::SMESH_Mesh_ptr
2277 SMESH_MeshEditor_i::MirrorMakeMesh(const SMESH::long_array&            theIDsOfElements,
2278                                    const SMESH::AxisStruct&            theMirror,
2279                                    SMESH::SMESH_MeshEditor::MirrorType theMirrorType,
2280                                    CORBA::Boolean                      theCopyGroups,
2281                                    const char*                         theMeshName)
2282 {
2283   TPythonDump pydump; // to prevent dump at mesh creation
2284
2285   SMESH::SMESH_Mesh_var mesh = makeMesh( theMeshName );
2286   if ( SMESH_Mesh_i* mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh ))
2287   {
2288     mirror(theIDsOfElements, theMirror, theMirrorType,
2289            false, theCopyGroups, & mesh_i->GetImpl());
2290     mesh_i->CreateGroupServants();
2291   }
2292
2293   if ( !myPreviewMode ) {
2294     pydump << mesh << " = " << this << ".MirrorMakeMesh( "
2295            << theIDsOfElements << ", "
2296            << theMirror   << ", "
2297            << mirrorTypeName(theMirrorType) << ", "
2298            << theCopyGroups << ", '"
2299            << theMeshName << "' )";
2300   }
2301   return mesh._retn();
2302 }
2303
2304 //=======================================================================
2305 //function : MirrorObjectMakeMesh
2306 //purpose  : 
2307 //=======================================================================
2308
2309 SMESH::SMESH_Mesh_ptr
2310 SMESH_MeshEditor_i::MirrorObjectMakeMesh(SMESH::SMESH_IDSource_ptr           theObject,
2311                                          const SMESH::AxisStruct&            theMirror,
2312                                          SMESH::SMESH_MeshEditor::MirrorType theMirrorType,
2313                                          CORBA::Boolean                      theCopyGroups,
2314                                          const char*                         theMeshName)
2315 {
2316   TPythonDump pydump; // to prevent dump at mesh creation
2317
2318   SMESH::SMESH_Mesh_var mesh = makeMesh( theMeshName );
2319   if ( SMESH_Mesh_i* mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh ))
2320   {
2321     SMESH::long_array_var anElementsId = theObject->GetIDs();
2322     mirror(anElementsId, theMirror, theMirrorType,
2323            false, theCopyGroups, & mesh_i->GetImpl());
2324     mesh_i->CreateGroupServants();
2325   }
2326   if ( !myPreviewMode ) {
2327     pydump << mesh << " = " << this << ".MirrorObjectMakeMesh( "
2328            << theObject << ", "
2329            << theMirror   << ", "
2330            << mirrorTypeName(theMirrorType) << ", "
2331            << theCopyGroups << ", '"
2332            << theMeshName << "' )";
2333   }
2334   return mesh._retn();
2335 }
2336
2337 //=======================================================================
2338 //function : translate
2339 //purpose  : 
2340 //=======================================================================
2341
2342 SMESH::ListOfGroups*
2343 SMESH_MeshEditor_i::translate(const SMESH::long_array & theIDsOfElements,
2344                               const SMESH::DirStruct &  theVector,
2345                               CORBA::Boolean            theCopy,
2346                               const bool                theMakeGroups,
2347                               ::SMESH_Mesh*             theTargetMesh)
2348 {
2349   initData();
2350
2351   TIDSortedElemSet elements;
2352   arrayToSet(theIDsOfElements, GetMeshDS(), elements);
2353
2354   gp_Trsf aTrsf;
2355   const SMESH::PointStruct * P = &theVector.PS;
2356   aTrsf.SetTranslation( gp_Vec( P->x, P->y, P->z ));
2357
2358   ::SMESH_MeshEditor anEditor( myMesh );
2359   ::SMESH_MeshEditor::PGroupIDs groupIds =
2360       anEditor.Transform (elements, aTrsf, theCopy, theMakeGroups, theTargetMesh);
2361
2362   if(theCopy)
2363     storeResult(anEditor);
2364
2365   return theMakeGroups ? getGroups(groupIds.get()) : 0;
2366 }
2367
2368 //=======================================================================
2369 //function : Translate
2370 //purpose  :
2371 //=======================================================================
2372
2373 void SMESH_MeshEditor_i::Translate(const SMESH::long_array & theIDsOfElements,
2374                                    const SMESH::DirStruct &  theVector,
2375                                    CORBA::Boolean            theCopy)
2376 {
2377   if ( !myPreviewMode ) {
2378     TPythonDump() << "vector = " << theVector;
2379     TPythonDump() << this << ".Translate( "
2380                   << theIDsOfElements
2381                   << ", vector, "
2382                   << theCopy << " )";
2383   }
2384   translate(theIDsOfElements,
2385             theVector,
2386             theCopy,
2387             false);
2388 }
2389
2390 //=======================================================================
2391 //function : TranslateObject
2392 //purpose  :
2393 //=======================================================================
2394
2395 void SMESH_MeshEditor_i::TranslateObject(SMESH::SMESH_IDSource_ptr theObject,
2396                                          const SMESH::DirStruct &  theVector,
2397                                          CORBA::Boolean            theCopy)
2398 {
2399   if ( !myPreviewMode ) {
2400     TPythonDump() << this << ".TranslateObject( "
2401                   << theObject
2402                   << ", vector, "
2403                   << theCopy << " )";
2404   }
2405   SMESH::long_array_var anElementsId = theObject->GetIDs();
2406   translate(anElementsId,
2407             theVector,
2408             theCopy,
2409             false);
2410 }
2411
2412 //=======================================================================
2413 //function : TranslateMakeGroups
2414 //purpose  : 
2415 //=======================================================================
2416
2417 SMESH::ListOfGroups*
2418 SMESH_MeshEditor_i::TranslateMakeGroups(const SMESH::long_array& theIDsOfElements,
2419                                         const SMESH::DirStruct&  theVector)
2420 {
2421   if ( !myPreviewMode ) {
2422     TPythonDump() << "vector = " << theVector;
2423     TPythonDump() << this << ".TranslateMakeGroups( "
2424                   << theIDsOfElements
2425                   << ", vector )";
2426   }
2427   return translate(theIDsOfElements,theVector,true,true);
2428 }
2429
2430 //=======================================================================
2431 //function : TranslateObjectMakeGroups
2432 //purpose  : 
2433 //=======================================================================
2434
2435 SMESH::ListOfGroups*
2436 SMESH_MeshEditor_i::TranslateObjectMakeGroups(SMESH::SMESH_IDSource_ptr theObject,
2437                                               const SMESH::DirStruct&   theVector)
2438 {
2439   if ( !myPreviewMode ) {
2440     TPythonDump() << "vector = " << theVector;
2441     TPythonDump() << this << ".TranslateObjectMakeGroups( "
2442                   << theObject
2443                   << ", vector )";
2444   }
2445   SMESH::long_array_var anElementsId = theObject->GetIDs();
2446   return translate(anElementsId, theVector, true, true);
2447 }
2448
2449 //=======================================================================
2450 //function : TranslateMakeMesh
2451 //purpose  : 
2452 //=======================================================================
2453
2454 SMESH::SMESH_Mesh_ptr
2455 SMESH_MeshEditor_i::TranslateMakeMesh(const SMESH::long_array& theIDsOfElements,
2456                                       const SMESH::DirStruct&  theVector,
2457                                       CORBA::Boolean           theCopyGroups,
2458                                       const char*              theMeshName)
2459 {
2460   TPythonDump pydump; // to prevent dump at mesh creation
2461   SMESH::SMESH_Mesh_var mesh = makeMesh( theMeshName );
2462
2463   if ( SMESH_Mesh_i* mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh )) {
2464     translate(theIDsOfElements, theVector,
2465               false, theCopyGroups, & mesh_i->GetImpl());
2466     mesh_i->CreateGroupServants();
2467   }
2468   if ( !myPreviewMode ) {
2469     pydump << mesh << " = " << this << ".TranslateMakeMesh( "
2470            << theIDsOfElements << ", "
2471            << theVector   << ", "
2472            << theCopyGroups << ", '"
2473            << theMeshName << "' )";
2474   }
2475   return mesh._retn();
2476 }
2477
2478 //=======================================================================
2479 //function : TranslateObjectMakeMesh
2480 //purpose  : 
2481 //=======================================================================
2482
2483 SMESH::SMESH_Mesh_ptr
2484 SMESH_MeshEditor_i::TranslateObjectMakeMesh(SMESH::SMESH_IDSource_ptr theObject,
2485                                             const SMESH::DirStruct&   theVector,
2486                                             CORBA::Boolean            theCopyGroups,
2487                                             const char*               theMeshName)
2488 {
2489   TPythonDump pydump; // to prevent dump at mesh creation
2490   SMESH::SMESH_Mesh_var mesh = makeMesh( theMeshName );
2491
2492   if ( SMESH_Mesh_i* mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh )) {
2493     SMESH::long_array_var anElementsId = theObject->GetIDs();
2494     translate(anElementsId, theVector,
2495               false, theCopyGroups, & mesh_i->GetImpl());
2496     mesh_i->CreateGroupServants();
2497   }
2498   if ( !myPreviewMode ) {
2499     pydump << mesh << " = " << this << ".TranslateObjectMakeMesh( "
2500            << theObject << ", "
2501            << theVector   << ", "
2502            << theCopyGroups << ", '"
2503            << theMeshName << "' )";
2504   }
2505   return mesh._retn();
2506 }
2507
2508 //=======================================================================
2509 //function : rotate
2510 //purpose  : 
2511 //=======================================================================
2512
2513 SMESH::ListOfGroups*
2514 SMESH_MeshEditor_i::rotate(const SMESH::long_array & theIDsOfElements,
2515                            const SMESH::AxisStruct & theAxis,
2516                            CORBA::Double             theAngle,
2517                            CORBA::Boolean            theCopy,
2518                            const bool                theMakeGroups,
2519                            ::SMESH_Mesh*             theTargetMesh)
2520 {
2521   initData();
2522
2523   TIDSortedElemSet elements;
2524   arrayToSet(theIDsOfElements, GetMeshDS(), elements);
2525
2526   gp_Pnt P ( theAxis.x, theAxis.y, theAxis.z );
2527   gp_Vec V ( theAxis.vx, theAxis.vy, theAxis.vz );
2528
2529   gp_Trsf aTrsf;
2530   aTrsf.SetRotation( gp_Ax1( P, V ), theAngle);
2531
2532   ::SMESH_MeshEditor anEditor( myMesh );
2533   ::SMESH_MeshEditor::PGroupIDs groupIds =
2534       anEditor.Transform (elements, aTrsf, theCopy, theMakeGroups, theTargetMesh);
2535
2536   if(theCopy) {
2537     storeResult(anEditor);
2538   }
2539   return theMakeGroups ? getGroups(groupIds.get()) : 0;
2540 }
2541
2542 //=======================================================================
2543 //function : Rotate
2544 //purpose  :
2545 //=======================================================================
2546
2547 void SMESH_MeshEditor_i::Rotate(const SMESH::long_array & theIDsOfElements,
2548                                 const SMESH::AxisStruct & theAxis,
2549                                 CORBA::Double             theAngle,
2550                                 CORBA::Boolean            theCopy)
2551 {
2552   if ( !myPreviewMode ) {
2553     TPythonDump() << "axis = " << theAxis;
2554     TPythonDump() << this << ".Rotate( "
2555                   << theIDsOfElements
2556                   << ", axis, "
2557                   << theAngle << ", "
2558                   << theCopy << " )";
2559   }
2560   rotate(theIDsOfElements,
2561          theAxis,
2562          theAngle,
2563          theCopy,
2564          false);
2565 }
2566
2567 //=======================================================================
2568 //function : RotateObject
2569 //purpose  :
2570 //=======================================================================
2571
2572 void SMESH_MeshEditor_i::RotateObject(SMESH::SMESH_IDSource_ptr theObject,
2573                                       const SMESH::AxisStruct & theAxis,
2574                                       CORBA::Double             theAngle,
2575                                       CORBA::Boolean            theCopy)
2576 {
2577   if ( !myPreviewMode ) {
2578     TPythonDump() << "axis = " << theAxis;
2579     TPythonDump() << this << ".RotateObject( "
2580                   << theObject
2581                   << ", axis, "
2582                   << theAngle << ", "
2583                   << theCopy << " )";
2584   }
2585   SMESH::long_array_var anElementsId = theObject->GetIDs();
2586   rotate(anElementsId,
2587          theAxis,
2588          theAngle,
2589          theCopy,
2590          false);
2591 }
2592
2593 //=======================================================================
2594 //function : RotateMakeGroups
2595 //purpose  : 
2596 //=======================================================================
2597
2598 SMESH::ListOfGroups*
2599 SMESH_MeshEditor_i::RotateMakeGroups(const SMESH::long_array& theIDsOfElements,
2600                                      const SMESH::AxisStruct& theAxis,
2601                                      CORBA::Double            theAngle)
2602 {
2603   if ( !myPreviewMode ) {
2604     TPythonDump() << "axis = " << theAxis;
2605     TPythonDump() << this << ".RotateMakeGroups( "
2606                   << theIDsOfElements
2607                   << ", axis, "
2608                   << theAngle << " )";
2609   }
2610   return rotate(theIDsOfElements,theAxis,theAngle,true,true);
2611 }
2612
2613 //=======================================================================
2614 //function : RotateObjectMakeGroups
2615 //purpose  : 
2616 //=======================================================================
2617
2618 SMESH::ListOfGroups*
2619 SMESH_MeshEditor_i::RotateObjectMakeGroups(SMESH::SMESH_IDSource_ptr theObject,
2620                                            const SMESH::AxisStruct&  theAxis,
2621                                            CORBA::Double             theAngle)
2622 {
2623   if ( !myPreviewMode ) {
2624     TPythonDump() << "axis = " << theAxis;
2625     TPythonDump() << this << ".RotateObjectMakeGroups( "
2626                   << theObject
2627                   << ", axis, "
2628                   << theAngle << " )";
2629   }
2630   SMESH::long_array_var anElementsId = theObject->GetIDs();
2631   return rotate(anElementsId,theAxis,theAngle,true,true);
2632 }
2633
2634 //=======================================================================
2635 //function : RotateMakeMesh
2636 //purpose  : 
2637 //=======================================================================
2638
2639 SMESH::SMESH_Mesh_ptr 
2640 SMESH_MeshEditor_i::RotateMakeMesh(const SMESH::long_array& theIDsOfElements,
2641                                    const SMESH::AxisStruct& theAxis,
2642                                    CORBA::Double            theAngleInRadians,
2643                                    CORBA::Boolean           theCopyGroups,
2644                                    const char*              theMeshName)
2645 {
2646   TPythonDump pydump; // to prevent dump at mesh creation
2647   SMESH::SMESH_Mesh_var mesh = makeMesh( theMeshName );
2648
2649   if ( SMESH_Mesh_i* mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh )) {
2650     rotate(theIDsOfElements, theAxis, theAngleInRadians,
2651            false, theCopyGroups, & mesh_i->GetImpl());
2652     mesh_i->CreateGroupServants();
2653   }
2654   if ( !myPreviewMode ) {
2655     pydump << mesh << " = " << this << ".RotateMakeMesh( "
2656            << theIDsOfElements << ", "
2657            << theAxis << ", "
2658            << theAngleInRadians   << ", "
2659            << theCopyGroups << ", '"
2660            << theMeshName << "' )";
2661   }
2662   return mesh._retn();
2663 }
2664
2665 //=======================================================================
2666 //function : RotateObjectMakeMesh
2667 //purpose  : 
2668 //=======================================================================
2669
2670 SMESH::SMESH_Mesh_ptr 
2671 SMESH_MeshEditor_i::RotateObjectMakeMesh(SMESH::SMESH_IDSource_ptr theObject,
2672                                          const SMESH::AxisStruct&  theAxis,
2673                                          CORBA::Double             theAngleInRadians,
2674                                          CORBA::Boolean            theCopyGroups,
2675                                          const char*               theMeshName)
2676 {
2677   TPythonDump pydump; // to prevent dump at mesh creation
2678   SMESH::SMESH_Mesh_var mesh = makeMesh( theMeshName );
2679
2680   if ( SMESH_Mesh_i* mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh )) {
2681     SMESH::long_array_var anElementsId = theObject->GetIDs();
2682     rotate(anElementsId, theAxis, theAngleInRadians,
2683            false, theCopyGroups, & mesh_i->GetImpl());
2684     mesh_i->CreateGroupServants();
2685   }
2686   if ( !myPreviewMode ) {
2687     pydump << mesh << " = " << this << ".RotateObjectMakeMesh( "
2688            << theObject << ", "
2689            << theAxis << ", "
2690            << theAngleInRadians   << ", "
2691            << theCopyGroups << ", '"
2692            << theMeshName << "' )";
2693   }
2694   return mesh._retn();
2695 }
2696
2697 //=======================================================================
2698 //function : FindCoincidentNodes
2699 //purpose  :
2700 //=======================================================================
2701
2702 void SMESH_MeshEditor_i::FindCoincidentNodes (CORBA::Double                  Tolerance,
2703                                               SMESH::array_of_long_array_out GroupsOfNodes)
2704 {
2705   initData();
2706
2707   ::SMESH_MeshEditor::TListOfListOfNodes aListOfListOfNodes;
2708   ::SMESH_MeshEditor anEditor( myMesh );
2709   set<const SMDS_MeshNode*> nodes; // no input nodes
2710   anEditor.FindCoincidentNodes( nodes, Tolerance, aListOfListOfNodes );
2711
2712   GroupsOfNodes = new SMESH::array_of_long_array;
2713   GroupsOfNodes->length( aListOfListOfNodes.size() );
2714   ::SMESH_MeshEditor::TListOfListOfNodes::iterator llIt = aListOfListOfNodes.begin();
2715   for ( CORBA::Long i = 0; llIt != aListOfListOfNodes.end(); llIt++, i++ ) {
2716     list< const SMDS_MeshNode* >& aListOfNodes = *llIt;
2717     list< const SMDS_MeshNode* >::iterator lIt = aListOfNodes.begin();;
2718     SMESH::long_array& aGroup = (*GroupsOfNodes)[ i ];
2719     aGroup.length( aListOfNodes.size() );
2720     for ( int j = 0; lIt != aListOfNodes.end(); lIt++, j++ )
2721       aGroup[ j ] = (*lIt)->GetID();
2722   }
2723   TPythonDump() << "coincident_nodes = " << this << ".FindCoincidentNodes( "
2724                 << Tolerance << " )";
2725 }
2726
2727 //=======================================================================
2728 //function : FindCoincidentNodesOnPart
2729 //purpose  :
2730 //=======================================================================
2731 void SMESH_MeshEditor_i::FindCoincidentNodesOnPart(SMESH::SMESH_IDSource_ptr      theObject,
2732                                                    CORBA::Double                  Tolerance,
2733                                                    SMESH::array_of_long_array_out GroupsOfNodes)
2734 {
2735   initData();
2736   SMESH::long_array_var aElementsId = theObject->GetIDs();
2737
2738   SMESHDS_Mesh* aMesh = GetMeshDS();
2739   set<const SMDS_MeshNode*> nodes;
2740
2741   if ( !CORBA::is_nil(SMESH::SMESH_GroupBase::_narrow(theObject)) &&
2742       SMESH::SMESH_GroupBase::_narrow(theObject)->GetType() == SMESH::NODE) {
2743     for(int i = 0; i < aElementsId->length(); i++) {
2744       CORBA::Long ind = aElementsId[i];
2745       const SMDS_MeshNode * elem = aMesh->FindNode(ind);
2746       if(elem)
2747         nodes.insert(elem);
2748     }
2749   }
2750   else {
2751     for(int i = 0; i < aElementsId->length(); i++) {
2752       CORBA::Long ind = aElementsId[i];
2753       const SMDS_MeshElement * elem = aMesh->FindElement(ind);
2754       if(elem) {
2755         SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
2756         while ( nIt->more() )
2757           nodes.insert( nodes.end(),static_cast<const SMDS_MeshNode*>(nIt->next()));
2758       }
2759     }
2760   }
2761     
2762   
2763   ::SMESH_MeshEditor::TListOfListOfNodes aListOfListOfNodes;
2764   ::SMESH_MeshEditor anEditor( myMesh );
2765   if(!nodes.empty())
2766     anEditor.FindCoincidentNodes( nodes, Tolerance, aListOfListOfNodes );
2767   
2768   GroupsOfNodes = new SMESH::array_of_long_array;
2769   GroupsOfNodes->length( aListOfListOfNodes.size() );
2770   ::SMESH_MeshEditor::TListOfListOfNodes::iterator llIt = aListOfListOfNodes.begin();
2771   for ( CORBA::Long i = 0; llIt != aListOfListOfNodes.end(); llIt++, i++ ) {
2772     list< const SMDS_MeshNode* >& aListOfNodes = *llIt;
2773     list< const SMDS_MeshNode* >::iterator lIt = aListOfNodes.begin();;
2774     SMESH::long_array& aGroup = (*GroupsOfNodes)[ i ];
2775     aGroup.length( aListOfNodes.size() );
2776     for ( int j = 0; lIt != aListOfNodes.end(); lIt++, j++ )
2777       aGroup[ j ] = (*lIt)->GetID();
2778   }
2779   TPythonDump() << "coincident_nodes_on_part = " << this << ".FindCoincidentNodesOnPart( "
2780                 <<theObject<<", "
2781                 << Tolerance << " )";
2782 }
2783
2784 //=======================================================================
2785 //function : MergeNodes
2786 //purpose  :
2787 //=======================================================================
2788
2789 void SMESH_MeshEditor_i::MergeNodes (const SMESH::array_of_long_array& GroupsOfNodes)
2790 {
2791   initData();
2792
2793   SMESHDS_Mesh* aMesh = GetMeshDS();
2794
2795   TPythonDump aTPythonDump;
2796   aTPythonDump << this << ".MergeNodes([";
2797   ::SMESH_MeshEditor::TListOfListOfNodes aListOfListOfNodes;
2798   for (int i = 0; i < GroupsOfNodes.length(); i++)
2799   {
2800     const SMESH::long_array& aNodeGroup = GroupsOfNodes[ i ];
2801     aListOfListOfNodes.push_back( list< const SMDS_MeshNode* >() );
2802     list< const SMDS_MeshNode* >& aListOfNodes = aListOfListOfNodes.back();
2803     for ( int j = 0; j < aNodeGroup.length(); j++ )
2804     {
2805       CORBA::Long index = aNodeGroup[ j ];
2806       const SMDS_MeshNode * node = aMesh->FindNode(index);
2807       if ( node )
2808         aListOfNodes.push_back( node );
2809     }
2810     if ( aListOfNodes.size() < 2 )
2811       aListOfListOfNodes.pop_back();
2812
2813     if ( i > 0 ) aTPythonDump << ", ";
2814     aTPythonDump << aNodeGroup;
2815   }
2816   ::SMESH_MeshEditor anEditor( myMesh );
2817   anEditor.MergeNodes( aListOfListOfNodes );
2818
2819   aTPythonDump <<  "])";
2820 }
2821
2822 //=======================================================================
2823 //function : FindEqualElements
2824 //purpose  :
2825 //=======================================================================
2826 void SMESH_MeshEditor_i::FindEqualElements(SMESH::SMESH_IDSource_ptr      theObject,
2827                                            SMESH::array_of_long_array_out GroupsOfElementsID)
2828 {
2829   initData();
2830   if ( !(!CORBA::is_nil(SMESH::SMESH_GroupBase::_narrow(theObject)) &&
2831          SMESH::SMESH_GroupBase::_narrow(theObject)->GetType() == SMESH::NODE) ) {
2832     typedef list<int> TListOfIDs;
2833     set<const SMDS_MeshElement*> elems;
2834     SMESH::long_array_var aElementsId = theObject->GetIDs();
2835     SMESHDS_Mesh* aMesh = GetMeshDS();
2836
2837     for(int i = 0; i < aElementsId->length(); i++) {
2838       CORBA::Long anID = aElementsId[i];
2839       const SMDS_MeshElement * elem = aMesh->FindElement(anID);
2840       if (elem) {
2841         elems.insert(elem);
2842       }
2843     }
2844
2845     ::SMESH_MeshEditor::TListOfListOfElementsID aListOfListOfElementsID;
2846     ::SMESH_MeshEditor anEditor( myMesh );
2847     anEditor.FindEqualElements( elems, aListOfListOfElementsID );
2848
2849     GroupsOfElementsID = new SMESH::array_of_long_array;
2850     GroupsOfElementsID->length( aListOfListOfElementsID.size() );
2851
2852     ::SMESH_MeshEditor::TListOfListOfElementsID::iterator arraysIt = aListOfListOfElementsID.begin();
2853     for (CORBA::Long j = 0; arraysIt != aListOfListOfElementsID.end(); ++arraysIt, ++j) {
2854       SMESH::long_array& aGroup = (*GroupsOfElementsID)[ j ];
2855       TListOfIDs& listOfIDs = *arraysIt;
2856       aGroup.length( listOfIDs.size() );
2857       TListOfIDs::iterator idIt = listOfIDs.begin();
2858       for (int k = 0; idIt != listOfIDs.end(); ++idIt, ++k ) {
2859         aGroup[ k ] = *idIt;
2860       }
2861     }
2862
2863   TPythonDump() << "equal_elements = " << this << ".FindEqualElements( "
2864                 <<theObject<<" )";
2865   }
2866 }
2867
2868 //=======================================================================
2869 //function : MergeElements
2870 //purpose  :
2871 //=======================================================================
2872
2873 void SMESH_MeshEditor_i::MergeElements(const SMESH::array_of_long_array& GroupsOfElementsID)
2874 {
2875   initData();
2876
2877   TPythonDump aTPythonDump;
2878   aTPythonDump << this << ".MergeElements( [";
2879
2880   ::SMESH_MeshEditor::TListOfListOfElementsID aListOfListOfElementsID;
2881
2882   for (int i = 0; i < GroupsOfElementsID.length(); i++) {
2883     const SMESH::long_array& anElemsIDGroup = GroupsOfElementsID[ i ];
2884     aListOfListOfElementsID.push_back( list< int >() );
2885     list< int >& aListOfElemsID = aListOfListOfElementsID.back();
2886     for ( int j = 0; j < anElemsIDGroup.length(); j++ ) {
2887       CORBA::Long id = anElemsIDGroup[ j ];
2888       aListOfElemsID.push_back( id );
2889     }
2890     if ( aListOfElemsID.size() < 2 )
2891       aListOfListOfElementsID.pop_back();
2892     if ( i > 0 ) aTPythonDump << ", ";
2893     aTPythonDump << anElemsIDGroup;
2894   }
2895
2896   ::SMESH_MeshEditor anEditor( myMesh );
2897   anEditor.MergeElements(aListOfListOfElementsID);
2898
2899   aTPythonDump << "] )";
2900 }
2901
2902 //=======================================================================
2903 //function : MergeEqualElements
2904 //purpose  :
2905 //=======================================================================
2906
2907 void SMESH_MeshEditor_i::MergeEqualElements()
2908 {
2909   initData();
2910
2911   ::SMESH_MeshEditor anEditor( myMesh );
2912   anEditor.MergeEqualElements();
2913
2914   TPythonDump() << this << ".MergeEqualElements()";
2915 }
2916
2917 //================================================================================
2918 /*!
2919  * \brief If the given ID is a valid node ID (nodeID > 0), just move this node, else
2920  * move the node closest to the point to point's location and return ID of the node
2921  */
2922 //================================================================================
2923
2924 CORBA::Long SMESH_MeshEditor_i::MoveClosestNodeToPoint(CORBA::Double x,
2925                                                        CORBA::Double y,
2926                                                        CORBA::Double z,
2927                                                        CORBA::Long   theNodeID)
2928 {
2929   // We keep myNodeSearcher until any mesh modification:
2930   // 1) initData() deletes myNodeSearcher at any edition,
2931   // 2) TNodeSearcherDeleter - at any mesh compute event and mesh change
2932
2933   initData();
2934
2935   int nodeID = theNodeID;
2936   const SMDS_MeshNode* node = GetMeshDS()->FindNode( nodeID );
2937   if ( !node )
2938   {
2939     static TNodeSearcherDeleter deleter;
2940     deleter.Set( myMesh );
2941     if ( !myNodeSearcher ) {
2942       ::SMESH_MeshEditor anEditor( myMesh );
2943       myNodeSearcher = anEditor.GetNodeSearcher();
2944     }
2945     gp_Pnt p( x,y,z );
2946     node = myNodeSearcher->FindClosestTo( p );
2947   }
2948   if ( node ) {
2949     nodeID = node->GetID();
2950     if ( myPreviewMode ) // make preview data
2951     {
2952       // in a preview mesh, make edges linked to a node
2953       TPreviewMesh tmpMesh;
2954       TIDSortedElemSet linkedNodes;
2955       ::SMESH_MeshEditor::GetLinkedNodes( node, linkedNodes );
2956       TIDSortedElemSet::iterator nIt = linkedNodes.begin();
2957       for ( ; nIt != linkedNodes.end(); ++nIt )
2958       {
2959         SMDS_MeshEdge edge( node, cast2Node( *nIt ));
2960         tmpMesh.Copy( &edge );
2961       }
2962       // move copied node
2963       node = tmpMesh.GetMeshDS()->FindNode( nodeID );
2964       if ( node )
2965         tmpMesh.GetMeshDS()->MoveNode(node, x, y, z);
2966       // fill preview data
2967       ::SMESH_MeshEditor anEditor( & tmpMesh );
2968       storeResult( anEditor );
2969     }
2970     else
2971     {
2972       GetMeshDS()->MoveNode(node, x, y, z);
2973     }
2974   }
2975
2976   if ( !myPreviewMode ) {
2977     TPythonDump() << "nodeID = " << this
2978                   << ".MoveClosestNodeToPoint( "<< x << ", " << y << ", " << z << " )";
2979   }
2980
2981   return nodeID;
2982 }
2983
2984 //=======================================================================
2985 //function : convError
2986 //purpose  :
2987 //=======================================================================
2988
2989 #define RETCASE(enm) case ::SMESH_MeshEditor::enm: return SMESH::SMESH_MeshEditor::enm;
2990
2991 static SMESH::SMESH_MeshEditor::Sew_Error convError( const::SMESH_MeshEditor::Sew_Error e )
2992 {
2993   switch ( e ) {
2994   RETCASE( SEW_OK );
2995   RETCASE( SEW_BORDER1_NOT_FOUND );
2996   RETCASE( SEW_BORDER2_NOT_FOUND );
2997   RETCASE( SEW_BOTH_BORDERS_NOT_FOUND );
2998   RETCASE( SEW_BAD_SIDE_NODES );
2999   RETCASE( SEW_VOLUMES_TO_SPLIT );
3000   RETCASE( SEW_DIFF_NB_OF_ELEMENTS );
3001   RETCASE( SEW_TOPO_DIFF_SETS_OF_ELEMENTS );
3002   RETCASE( SEW_BAD_SIDE1_NODES );
3003   RETCASE( SEW_BAD_SIDE2_NODES );
3004   }
3005   return SMESH::SMESH_MeshEditor::SEW_OK;
3006 }
3007
3008 //=======================================================================
3009 //function : SewFreeBorders
3010 //purpose  :
3011 //=======================================================================
3012
3013 SMESH::SMESH_MeshEditor::Sew_Error
3014   SMESH_MeshEditor_i::SewFreeBorders(CORBA::Long FirstNodeID1,
3015                                      CORBA::Long SecondNodeID1,
3016                                      CORBA::Long LastNodeID1,
3017                                      CORBA::Long FirstNodeID2,
3018                                      CORBA::Long SecondNodeID2,
3019                                      CORBA::Long LastNodeID2,
3020                                      CORBA::Boolean CreatePolygons,
3021                                      CORBA::Boolean CreatePolyedrs)
3022 {
3023   initData();
3024
3025   SMESHDS_Mesh* aMesh = GetMeshDS();
3026
3027   const SMDS_MeshNode* aBorderFirstNode  = aMesh->FindNode( FirstNodeID1  );
3028   const SMDS_MeshNode* aBorderSecondNode = aMesh->FindNode( SecondNodeID1 );
3029   const SMDS_MeshNode* aBorderLastNode   = aMesh->FindNode( LastNodeID1   );
3030   const SMDS_MeshNode* aSide2FirstNode   = aMesh->FindNode( FirstNodeID2  );
3031   const SMDS_MeshNode* aSide2SecondNode  = aMesh->FindNode( SecondNodeID2 );
3032   const SMDS_MeshNode* aSide2ThirdNode   = aMesh->FindNode( LastNodeID2   );
3033
3034   if (!aBorderFirstNode ||
3035       !aBorderSecondNode||
3036       !aBorderLastNode)
3037     return SMESH::SMESH_MeshEditor::SEW_BORDER1_NOT_FOUND;
3038   if (!aSide2FirstNode  ||
3039       !aSide2SecondNode ||
3040       !aSide2ThirdNode)
3041     return SMESH::SMESH_MeshEditor::SEW_BORDER2_NOT_FOUND;
3042
3043   TPythonDump() << "error = " << this << ".SewFreeBorders( "
3044                 << FirstNodeID1  << ", "
3045                 << SecondNodeID1 << ", "
3046                 << LastNodeID1   << ", "
3047                 << FirstNodeID2  << ", "
3048                 << SecondNodeID2 << ", "
3049                 << LastNodeID2   << ", "
3050                 << CreatePolygons<< ", "
3051                 << CreatePolyedrs<< " )";
3052
3053   ::SMESH_MeshEditor anEditor( myMesh );
3054   SMESH::SMESH_MeshEditor::Sew_Error error =
3055     convError( anEditor.SewFreeBorder (aBorderFirstNode,
3056                                        aBorderSecondNode,
3057                                        aBorderLastNode,
3058                                        aSide2FirstNode,
3059                                        aSide2SecondNode,
3060                                        aSide2ThirdNode,
3061                                        true,
3062                                        CreatePolygons,
3063                                        CreatePolyedrs) );
3064
3065   storeResult(anEditor);
3066
3067   return error;
3068 }
3069
3070
3071 //=======================================================================
3072 //function : SewConformFreeBorders
3073 //purpose  :
3074 //=======================================================================
3075
3076 SMESH::SMESH_MeshEditor::Sew_Error
3077 SMESH_MeshEditor_i::SewConformFreeBorders(CORBA::Long FirstNodeID1,
3078                                           CORBA::Long SecondNodeID1,
3079                                           CORBA::Long LastNodeID1,
3080                                           CORBA::Long FirstNodeID2,
3081                                           CORBA::Long SecondNodeID2)
3082 {
3083   initData();
3084
3085   SMESHDS_Mesh* aMesh = GetMeshDS();
3086
3087   const SMDS_MeshNode* aBorderFirstNode  = aMesh->FindNode( FirstNodeID1  );
3088   const SMDS_MeshNode* aBorderSecondNode = aMesh->FindNode( SecondNodeID1 );
3089   const SMDS_MeshNode* aBorderLastNode   = aMesh->FindNode( LastNodeID1   );
3090   const SMDS_MeshNode* aSide2FirstNode   = aMesh->FindNode( FirstNodeID2  );
3091   const SMDS_MeshNode* aSide2SecondNode  = aMesh->FindNode( SecondNodeID2 );
3092   const SMDS_MeshNode* aSide2ThirdNode   = 0;
3093
3094   if (!aBorderFirstNode ||
3095       !aBorderSecondNode||
3096       !aBorderLastNode )
3097     return SMESH::SMESH_MeshEditor::SEW_BORDER1_NOT_FOUND;
3098   if (!aSide2FirstNode  ||
3099       !aSide2SecondNode)
3100     return SMESH::SMESH_MeshEditor::SEW_BORDER2_NOT_FOUND;
3101
3102   TPythonDump() << "error = " << this << ".SewConformFreeBorders( "
3103                 << FirstNodeID1  << ", "
3104                 << SecondNodeID1 << ", "
3105                 << LastNodeID1   << ", "
3106                 << FirstNodeID2  << ", "
3107                 << SecondNodeID2 << " )";
3108
3109   ::SMESH_MeshEditor anEditor( myMesh );
3110   SMESH::SMESH_MeshEditor::Sew_Error error =
3111     convError( anEditor.SewFreeBorder (aBorderFirstNode,
3112                                        aBorderSecondNode,
3113                                        aBorderLastNode,
3114                                        aSide2FirstNode,
3115                                        aSide2SecondNode,
3116                                        aSide2ThirdNode,
3117                                        true,
3118                                        false, false) );
3119
3120   storeResult(anEditor);
3121
3122   return error;
3123 }
3124
3125
3126 //=======================================================================
3127 //function : SewBorderToSide
3128 //purpose  :
3129 //=======================================================================
3130
3131 SMESH::SMESH_MeshEditor::Sew_Error
3132 SMESH_MeshEditor_i::SewBorderToSide(CORBA::Long FirstNodeIDOnFreeBorder,
3133                                     CORBA::Long SecondNodeIDOnFreeBorder,
3134                                     CORBA::Long LastNodeIDOnFreeBorder,
3135                                     CORBA::Long FirstNodeIDOnSide,
3136                                     CORBA::Long LastNodeIDOnSide,
3137                                     CORBA::Boolean CreatePolygons,
3138                                     CORBA::Boolean CreatePolyedrs)
3139 {
3140   initData();
3141
3142   SMESHDS_Mesh* aMesh = GetMeshDS();
3143
3144   const SMDS_MeshNode* aBorderFirstNode  = aMesh->FindNode( FirstNodeIDOnFreeBorder  );
3145   const SMDS_MeshNode* aBorderSecondNode = aMesh->FindNode( SecondNodeIDOnFreeBorder );
3146   const SMDS_MeshNode* aBorderLastNode   = aMesh->FindNode( LastNodeIDOnFreeBorder   );
3147   const SMDS_MeshNode* aSide2FirstNode   = aMesh->FindNode( FirstNodeIDOnSide  );
3148   const SMDS_MeshNode* aSide2SecondNode  = aMesh->FindNode( LastNodeIDOnSide );
3149   const SMDS_MeshNode* aSide2ThirdNode   = 0;
3150
3151   if (!aBorderFirstNode ||
3152       !aBorderSecondNode||
3153       !aBorderLastNode  )
3154     return SMESH::SMESH_MeshEditor::SEW_BORDER1_NOT_FOUND;
3155   if (!aSide2FirstNode  ||
3156       !aSide2SecondNode)
3157     return SMESH::SMESH_MeshEditor::SEW_BAD_SIDE_NODES;
3158
3159   TPythonDump() << "error = " << this << ".SewBorderToSide( "
3160                 << FirstNodeIDOnFreeBorder  << ", "
3161                 << SecondNodeIDOnFreeBorder << ", "
3162                 << LastNodeIDOnFreeBorder   << ", "
3163                 << FirstNodeIDOnSide        << ", "
3164                 << LastNodeIDOnSide         << ", "
3165                 << CreatePolygons           << ", "
3166                 << CreatePolyedrs           << ") ";
3167
3168   ::SMESH_MeshEditor anEditor( myMesh );
3169   SMESH::SMESH_MeshEditor::Sew_Error error =
3170     convError( anEditor.SewFreeBorder (aBorderFirstNode,
3171                                        aBorderSecondNode,
3172                                        aBorderLastNode,
3173                                        aSide2FirstNode,
3174                                        aSide2SecondNode,
3175                                        aSide2ThirdNode,
3176                                        false,
3177                                        CreatePolygons,
3178                                        CreatePolyedrs) );
3179
3180   storeResult(anEditor);
3181
3182   return error;
3183 }
3184
3185
3186 //=======================================================================
3187 //function : SewSideElements
3188 //purpose  :
3189 //=======================================================================
3190
3191 SMESH::SMESH_MeshEditor::Sew_Error
3192 SMESH_MeshEditor_i::SewSideElements(const SMESH::long_array& IDsOfSide1Elements,
3193                                     const SMESH::long_array& IDsOfSide2Elements,
3194                                     CORBA::Long NodeID1OfSide1ToMerge,
3195                                     CORBA::Long NodeID1OfSide2ToMerge,
3196                                     CORBA::Long NodeID2OfSide1ToMerge,
3197                                     CORBA::Long NodeID2OfSide2ToMerge)
3198 {
3199   initData();
3200
3201   SMESHDS_Mesh* aMesh = GetMeshDS();
3202
3203   const SMDS_MeshNode* aFirstNode1ToMerge  = aMesh->FindNode( NodeID1OfSide1ToMerge );
3204   const SMDS_MeshNode* aFirstNode2ToMerge  = aMesh->FindNode( NodeID1OfSide2ToMerge );
3205   const SMDS_MeshNode* aSecondNode1ToMerge = aMesh->FindNode( NodeID2OfSide1ToMerge );
3206   const SMDS_MeshNode* aSecondNode2ToMerge = aMesh->FindNode( NodeID2OfSide2ToMerge );
3207
3208   if (!aFirstNode1ToMerge ||
3209       !aFirstNode2ToMerge )
3210     return SMESH::SMESH_MeshEditor::SEW_BAD_SIDE1_NODES;
3211   if (!aSecondNode1ToMerge||
3212       !aSecondNode2ToMerge)
3213     return SMESH::SMESH_MeshEditor::SEW_BAD_SIDE2_NODES;
3214
3215   TIDSortedElemSet aSide1Elems, aSide2Elems;
3216   arrayToSet(IDsOfSide1Elements, aMesh, aSide1Elems);
3217   arrayToSet(IDsOfSide2Elements, aMesh, aSide2Elems);
3218
3219   TPythonDump() << "error = " << this << ".SewSideElements( "
3220                 << IDsOfSide1Elements << ", "
3221                 << IDsOfSide2Elements << ", "
3222                 << NodeID1OfSide1ToMerge << ", "
3223                 << NodeID1OfSide2ToMerge << ", "
3224                 << NodeID2OfSide1ToMerge << ", "
3225                 << NodeID2OfSide2ToMerge << ")";
3226
3227   ::SMESH_MeshEditor anEditor( myMesh );
3228   SMESH::SMESH_MeshEditor::Sew_Error error =
3229     convError( anEditor.SewSideElements (aSide1Elems, aSide2Elems,
3230                                          aFirstNode1ToMerge,
3231                                          aFirstNode2ToMerge,
3232                                          aSecondNode1ToMerge,
3233                                          aSecondNode2ToMerge));
3234
3235   storeResult(anEditor);
3236
3237   return error;
3238 }
3239
3240 //================================================================================
3241 /*!
3242  * \brief Set new nodes for given element
3243   * \param ide - element id
3244   * \param newIDs - new node ids
3245   * \retval CORBA::Boolean - true if result is OK
3246  */
3247 //================================================================================
3248
3249 CORBA::Boolean SMESH_MeshEditor_i::ChangeElemNodes(CORBA::Long ide,
3250                                                    const SMESH::long_array& newIDs)
3251 {
3252   initData();
3253
3254   const SMDS_MeshElement* elem = GetMeshDS()->FindElement(ide);
3255   if(!elem) return false;
3256
3257   int nbn = newIDs.length();
3258   int i=0;
3259   vector<const SMDS_MeshNode*> aNodes(nbn);
3260   int nbn1=-1;
3261   for(; i<nbn; i++) {
3262     const SMDS_MeshNode* aNode = GetMeshDS()->FindNode(newIDs[i]);
3263     if(aNode) {
3264       nbn1++;
3265       aNodes[nbn1] = aNode;
3266     }
3267   }
3268   TPythonDump() << "isDone = " << this << ".ChangeElemNodes( "
3269                 << ide << ", " << newIDs << " )";
3270 #ifdef _DEBUG_
3271   TPythonDump() << "print 'ChangeElemNodes: ', isDone";
3272 #endif
3273
3274   return GetMeshDS()->ChangeElementNodes( elem, & aNodes[0], nbn1+1 );
3275 }
3276   
3277 //================================================================================
3278 /*!
3279  * \brief Update myLastCreated* or myPreviewData
3280   * \param anEditor - it contains last modification results
3281  */
3282 //================================================================================
3283
3284 void SMESH_MeshEditor_i::storeResult(::SMESH_MeshEditor& anEditor)
3285 {
3286   if ( myPreviewMode ) { // --- MeshPreviewStruct filling --- 
3287
3288     list<int> aNodesConnectivity;
3289     typedef map<int, int> TNodesMap;
3290     TNodesMap nodesMap;
3291
3292     TPreviewMesh * aPreviewMesh = dynamic_cast< TPreviewMesh* >( anEditor.GetMesh() );
3293     SMDSAbs_ElementType previewType = aPreviewMesh->myPreviewType;
3294
3295     SMESHDS_Mesh* aMeshDS = anEditor.GetMeshDS();
3296     int nbEdges = aMeshDS->NbEdges();
3297     int nbFaces = aMeshDS->NbFaces();
3298     int nbVolum = aMeshDS->NbVolumes();
3299     switch ( previewType ) {
3300     case SMDSAbs_Edge  : nbFaces = nbVolum = 0; break;
3301     case SMDSAbs_Face  : nbEdges = nbVolum = 0; break;
3302     case SMDSAbs_Volume: nbEdges = nbFaces = 0; break;
3303     default:;
3304     }
3305     myPreviewData->nodesXYZ.length(aMeshDS->NbNodes());
3306     myPreviewData->elementTypes.length(nbEdges + nbFaces + nbVolum);
3307     int i = 0, j = 0;
3308     SMDS_ElemIteratorPtr itMeshElems = aMeshDS->elementsIterator();
3309
3310     while ( itMeshElems->more() ) {
3311       const SMDS_MeshElement* aMeshElem = itMeshElems->next();
3312       if ( previewType != SMDSAbs_All && aMeshElem->GetType() != previewType )
3313         continue;
3314
3315       SMDS_ElemIteratorPtr itElemNodes = aMeshElem->nodesIterator();
3316       while ( itElemNodes->more() ) {
3317         const SMDS_MeshNode* aMeshNode = 
3318           static_cast<const SMDS_MeshNode*>( itElemNodes->next() );
3319         int aNodeID = aMeshNode->GetID();
3320         TNodesMap::iterator anIter = nodesMap.find(aNodeID);
3321         if ( anIter == nodesMap.end() ) {
3322           // filling the nodes coordinates
3323           myPreviewData->nodesXYZ[j].x = aMeshNode->X();
3324           myPreviewData->nodesXYZ[j].y = aMeshNode->Y();
3325           myPreviewData->nodesXYZ[j].z = aMeshNode->Z();
3326           anIter = nodesMap.insert( make_pair(aNodeID, j) ).first;
3327           j++;
3328         }
3329         aNodesConnectivity.push_back(anIter->second);
3330       }
3331
3332       // filling the elements types
3333       SMDSAbs_ElementType aType;
3334       bool isPoly;
3335       /*if (aMeshElem->GetType() == SMDSAbs_Volume) {
3336         aType = SMDSAbs_Node;
3337         isPoly = false;
3338       }
3339       else*/ {
3340         aType = aMeshElem->GetType();
3341         isPoly = aMeshElem->IsPoly();
3342       }
3343
3344       myPreviewData->elementTypes[i].SMDS_ElementType = (SMESH::ElementType) aType;
3345       myPreviewData->elementTypes[i].isPoly = isPoly;
3346       myPreviewData->elementTypes[i].nbNodesInElement = aMeshElem->NbNodes();
3347       i++;
3348
3349     }
3350     myPreviewData->nodesXYZ.length( j );
3351
3352     // filling the elements connectivities
3353     list<int>::iterator aConnIter = aNodesConnectivity.begin();
3354     myPreviewData->elementConnectivities.length(aNodesConnectivity.size());
3355     for( int i = 0; aConnIter != aNodesConnectivity.end(); aConnIter++, i++ )
3356       myPreviewData->elementConnectivities[i] = *aConnIter;
3357     
3358     return;
3359   }
3360
3361   {
3362     // add new nodes into myLastCreatedNodes
3363     const SMESH_SequenceOfElemPtr& aSeq = anEditor.GetLastCreatedNodes();
3364     myLastCreatedNodes->length(aSeq.Length());
3365     for(int i=0; i<aSeq.Length(); i++)
3366       myLastCreatedNodes[i] = aSeq.Value(i+1)->GetID();
3367   }
3368   {
3369     // add new elements into myLastCreatedElems
3370     const SMESH_SequenceOfElemPtr& aSeq = anEditor.GetLastCreatedElems();
3371     myLastCreatedElems->length(aSeq.Length());
3372     for(int i=0; i<aSeq.Length(); i++)
3373       myLastCreatedElems[i] = aSeq.Value(i+1)->GetID();
3374   }
3375 }
3376
3377 //================================================================================
3378 /*!
3379  * Return data of mesh edition preview
3380  */
3381 //================================================================================
3382
3383 SMESH::MeshPreviewStruct* SMESH_MeshEditor_i::GetPreviewData()
3384 {
3385   return myPreviewData._retn();
3386 }
3387
3388 //================================================================================
3389 /*!
3390  * \brief Returns list of it's IDs of created nodes
3391   * \retval SMESH::long_array* - list of node ID
3392  */
3393 //================================================================================
3394
3395 SMESH::long_array* SMESH_MeshEditor_i::GetLastCreatedNodes()
3396 {
3397   return myLastCreatedNodes._retn();
3398 }
3399
3400 //================================================================================
3401 /*!
3402  * \brief Returns list of it's IDs of created elements
3403   * \retval SMESH::long_array* - list of elements' ID
3404  */
3405 //================================================================================
3406
3407 SMESH::long_array* SMESH_MeshEditor_i::GetLastCreatedElems()
3408 {
3409   return myLastCreatedElems._retn();
3410 }
3411
3412 //=======================================================================
3413 //function : ConvertToQuadratic
3414 //purpose  :
3415 //=======================================================================
3416
3417 void SMESH_MeshEditor_i::ConvertToQuadratic(CORBA::Boolean theForce3d)
3418 {
3419   ::SMESH_MeshEditor anEditor( myMesh );
3420   anEditor.ConvertToQuadratic(theForce3d);
3421   TPythonDump() << this << ".ConvertToQuadratic( " << theForce3d << " )";
3422 }
3423
3424 //=======================================================================
3425 //function : ConvertFromQuadratic
3426 //purpose  : 
3427 //=======================================================================
3428
3429 CORBA::Boolean SMESH_MeshEditor_i::ConvertFromQuadratic()
3430 {
3431   ::SMESH_MeshEditor anEditor( myMesh );
3432   CORBA::Boolean isDone = anEditor.ConvertFromQuadratic();
3433   TPythonDump() << this << ".ConvertFromQuadratic()";
3434   return isDone;
3435 }
3436
3437 //=======================================================================
3438 //function : makeMesh
3439 //purpose  : create a named imported mesh 
3440 //=======================================================================
3441
3442 SMESH::SMESH_Mesh_ptr SMESH_MeshEditor_i::makeMesh(const char* theMeshName)
3443 {
3444   SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen();
3445   SMESH::SMESH_Mesh_var mesh = gen->CreateEmptyMesh();
3446   SALOMEDS::Study_var study = gen->GetCurrentStudy();
3447   SALOMEDS::SObject_var meshSO = gen->ObjectToSObject( study, mesh );
3448   gen->SetName( meshSO, theMeshName, "Mesh" );
3449
3450   SALOMEDS::StudyBuilder_var builder = study->NewBuilder();
3451   SALOMEDS::GenericAttribute_var anAttr
3452     = builder->FindOrCreateAttribute( meshSO, "AttributePixMap" );
3453   SALOMEDS::AttributePixMap::_narrow( anAttr )->SetPixMap( "ICON_SMESH_TREE_MESH_IMPORTED" );
3454
3455   return mesh._retn();
3456 }