Salome HOME
206c73da4378217e110f4d8fdac1e46b12cfd7d3
[modules/smesh.git] / src / SMESH_I / SMESH_MeshEditor_i.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SMESH_MeshEditor_i.cxx
23 //  Author : Nicolas REJNERI
24 //  Module : SMESH
25
26 #ifdef WIN32
27 #define NOMINMAX
28 #endif
29
30 // A macro used in SMESH_TryCatch.hxx,
31 // it re-raises a CORBA SALOME exception thrown by SMESH_MeshEditor_i and caught by SMESH_CATCH
32 #define SMY_OWN_CATCH \
33   catch ( SALOME::SALOME_Exception & e ) { throw e; }
34
35 #include "SMESH_MeshEditor_i.hxx"
36
37 #include "SMDS_EdgePosition.hxx"
38 #include "SMDS_ElemIterator.hxx"
39 #include "SMDS_FacePosition.hxx"
40 #include "SMDS_IteratorOnIterators.hxx"
41 #include "SMDS_LinearEdge.hxx"
42 #include "SMDS_Mesh0DElement.hxx"
43 #include "SMDS_MeshFace.hxx"
44 #include "SMDS_MeshVolume.hxx"
45 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
46 #include "SMDS_SetIterator.hxx"
47 #include "SMDS_VolumeTool.hxx"
48 #include "SMESHDS_Group.hxx"
49 #include "SMESHDS_GroupOnGeom.hxx"
50 #include "SMESH_ControlsDef.hxx"
51 #include "SMESH_Filter_i.hxx"
52 #include "SMESH_Gen_i.hxx"
53 #include "SMESH_Group.hxx"
54 #include "SMESH_Group_i.hxx"
55 #include "SMESH_MeshAlgos.hxx"
56 #include "SMESH_MeshPartDS.hxx"
57 #include "SMESH_MesherHelper.hxx"
58 #include "SMESH_PythonDump.hxx"
59 #include "SMESH_subMeshEventListener.hxx"
60 #include "SMESH_subMesh_i.hxx"
61
62 #include <utilities.h>
63 #include <Utils_ExceptHandlers.hxx>
64 #include <Utils_CorbaException.hxx>
65 #include <SALOMEDS_wrap.hxx>
66 #include <SALOME_GenericObj_i.hh>
67 #include <Basics_OCCTVersion.hxx>
68
69 #include <BRepAdaptor_Surface.hxx>
70 #include <BRep_Tool.hxx>
71 #include <TopExp_Explorer.hxx>
72 #include <TopoDS.hxx>
73 #include <TopoDS_Edge.hxx>
74 #include <TopoDS_Face.hxx>
75 #include <gp_Ax1.hxx>
76 #include <gp_Ax2.hxx>
77 #include <gp_Vec.hxx>
78
79 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
80 #define NO_CAS_CATCH
81 #endif
82
83 #include <Standard_Failure.hxx>
84
85 #ifdef NO_CAS_CATCH
86 #include <Standard_ErrorHandler.hxx>
87 #endif
88
89 #include <sstream>
90 #include <limits>
91
92 #include "SMESH_TryCatch.hxx" // include after OCCT headers!
93
94 #define cast2Node(elem) static_cast<const SMDS_MeshNode*>( elem )
95
96 using namespace std;
97 using SMESH::TPythonDump;
98 using SMESH::TVar;
99
100 namespace MeshEditor_I {
101
102   //=============================================================================
103   /*!
104    * \brief Mesh to apply modifications for preview purposes
105    */
106   //=============================================================================
107
108   struct TPreviewMesh: public SMESH_Mesh
109   {
110     SMDSAbs_ElementType myPreviewType; // type to show
111     //!< Constructor
112     TPreviewMesh(SMDSAbs_ElementType previewElements = SMDSAbs_All) {
113       _isShapeToMesh = (_id =_studyId = 0);
114       _myMeshDS  = new SMESHDS_Mesh( _id, true );
115       myPreviewType = previewElements;
116     }
117     //!< Copy a set of elements
118     void Copy(const TIDSortedElemSet & theElements,
119               TIDSortedElemSet&        theCopyElements,
120               SMDSAbs_ElementType      theSelectType = SMDSAbs_All,
121               SMDSAbs_ElementType      theAvoidType = SMDSAbs_All)
122     {
123       // loop on theIDsOfElements
124       TIDSortedElemSet::const_iterator eIt = theElements.begin();
125       for ( ; eIt != theElements.end(); ++eIt )
126       {
127         const SMDS_MeshElement* anElem = *eIt;
128         if ( !anElem ) continue;
129         SMDSAbs_ElementType type = anElem->GetType();
130         if ( type == theAvoidType ||
131              ( theSelectType != SMDSAbs_All && type != theSelectType ))
132           continue;
133         const SMDS_MeshElement* anElemCopy;
134         if ( type == SMDSAbs_Node)
135           anElemCopy = Copy( cast2Node(anElem) );
136         else
137           anElemCopy = Copy( anElem );
138         if ( anElemCopy )
139           theCopyElements.insert( theCopyElements.end(), anElemCopy );
140       }
141     }
142     //!< Copy an element
143     SMDS_MeshElement* Copy( const SMDS_MeshElement* anElem )
144     {
145       // copy element nodes
146       int anElemNbNodes = anElem->NbNodes();
147       vector< int > anElemNodesID( anElemNbNodes ) ;
148       SMDS_ElemIteratorPtr itElemNodes = anElem->nodesIterator();
149       for ( int i = 0; itElemNodes->more(); i++)
150       {
151         const SMDS_MeshNode* anElemNode = cast2Node( itElemNodes->next() );
152         Copy( anElemNode );
153         anElemNodesID[i] = anElemNode->GetID();
154       }
155
156       // creates a corresponding element on copied nodes
157       ::SMESH_MeshEditor::ElemFeatures elemType;
158       elemType.Init( anElem, /*basicOnly=*/false );
159       elemType.SetID( anElem->GetID() );
160       SMDS_MeshElement* anElemCopy =
161         ::SMESH_MeshEditor(this).AddElement( anElemNodesID, elemType );
162       return anElemCopy;
163     }
164     //!< Copy a node
165     SMDS_MeshNode* Copy( const SMDS_MeshNode* anElemNode )
166     {
167       return _myMeshDS->AddNodeWithID(anElemNode->X(), anElemNode->Y(), anElemNode->Z(),
168                                       anElemNode->GetID());
169     }
170     void RemoveAll()
171     {
172       GetMeshDS()->ClearMesh();
173     }
174     void Remove( SMDSAbs_ElementType type )
175     {
176       SMDS_ElemIteratorPtr eIt = GetMeshDS()->elementsIterator( type );
177       while ( eIt->more() )
178         GetMeshDS()->RemoveFreeElement( eIt->next(), /*sm=*/0, /*fromGroups=*/false );
179     }
180   };// struct TPreviewMesh
181
182   static SMESH_NodeSearcher *    theNodeSearcher    = 0;
183   static SMESH_ElementSearcher * theElementSearcher = 0;
184
185   //=============================================================================
186   /*!
187    * \brief Deleter of theNodeSearcher at any compute event occured
188    */
189   //=============================================================================
190
191   struct TSearchersDeleter : public SMESH_subMeshEventListener
192   {
193     SMESH_Mesh* myMesh;
194     string      myMeshPartIOR;
195     //!< Constructor
196     TSearchersDeleter(): SMESH_subMeshEventListener( false, // won't be deleted by submesh
197                                                      "SMESH_MeshEditor_i::TSearchersDeleter"),
198                          myMesh(0) {}
199     //!< Delete theNodeSearcher
200     static void Delete()
201     {
202       if ( theNodeSearcher )    delete theNodeSearcher;    theNodeSearcher    = 0;
203       if ( theElementSearcher ) delete theElementSearcher; theElementSearcher = 0;
204     }
205     typedef map < int, SMESH_subMesh * > TDependsOnMap;
206     //!< The meshod called by submesh: do my main job
207     void ProcessEvent(const int, const int eventType, SMESH_subMesh* sm,
208                       SMESH_subMeshEventListenerData*,const SMESH_Hypothesis*)
209     {
210       if ( eventType == SMESH_subMesh::COMPUTE_EVENT ) {
211         Delete();
212         Unset( sm->GetFather() );
213       }
214     }
215     //!< set self on all submeshes and delete theNodeSearcher if other mesh is set
216     void Set(SMESH_Mesh* mesh, const string& meshPartIOR = string())
217     {
218       if ( myMesh != mesh || myMeshPartIOR != meshPartIOR)
219       {
220         if ( myMesh ) {
221           Delete();
222           Unset( myMesh );
223         }
224         myMesh = mesh;
225         myMeshPartIOR = meshPartIOR;
226         SMESH_subMesh* sm = mesh->GetSubMesh( mesh->GetShapeToMesh() );
227         SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator( /*includeSelf=*/true );
228         while ( smIt->more() )
229         {
230           sm = smIt->next();
231           sm->SetEventListener( this, 0, sm );
232         }
233       }
234     }
235     //!<  delete self from all submeshes
236     void Unset(SMESH_Mesh* mesh)
237     {
238       if ( SMESH_subMesh* sm = mesh->GetSubMeshContaining(1) ) {
239         SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator( /*includeSelf=*/true );
240         while ( smIt->more() )
241           smIt->next()->DeleteEventListener( this );
242       }
243       myMesh = 0;
244     }
245
246   } theSearchersDeleter;
247
248   TCollection_AsciiString mirrorTypeName( SMESH::SMESH_MeshEditor::MirrorType theMirrorType )
249   {
250     TCollection_AsciiString typeStr;
251     switch ( theMirrorType ) {
252     case  SMESH::SMESH_MeshEditor::POINT:
253       typeStr = "SMESH.SMESH_MeshEditor.POINT";
254       break;
255     case  SMESH::SMESH_MeshEditor::AXIS:
256       typeStr = "SMESH.SMESH_MeshEditor.AXIS";
257       break;
258     default:
259       typeStr = "SMESH.SMESH_MeshEditor.PLANE";
260     }
261     return typeStr;
262   }
263   //================================================================================
264   /*!
265    * \brief function for conversion of long_array to TIDSortedElemSet
266    * \param IDs - array of IDs
267    * \param aMesh - mesh
268    * \param aMap - collection to fill
269    * \param aType - element type
270    */
271   //================================================================================
272
273   void arrayToSet(const SMESH::long_array & IDs,
274                   const SMESHDS_Mesh*       aMesh,
275                   TIDSortedElemSet&         aMap,
276                   const SMDSAbs_ElementType aType = SMDSAbs_All,
277                   SMDS_MeshElement::Filter* aFilter = NULL)
278   {
279     SMDS_MeshElement::NonNullFilter filter1;
280     SMDS_MeshElement::TypeFilter    filter2( aType );
281
282     if ( aFilter == NULL )
283       aFilter = ( aType == SMDSAbs_All ) ? (SMDS_MeshElement::Filter*) &filter1 : (SMDS_MeshElement::Filter*) &filter2;
284     
285     SMDS_MeshElement::Filter & filter = *aFilter;
286
287     if ( aType == SMDSAbs_Node )
288       for (int i=0; i<IDs.length(); i++) {
289         const SMDS_MeshElement * elem = aMesh->FindNode( IDs[i] );
290         if ( filter( elem ))
291           aMap.insert( aMap.end(), elem );
292       }
293     else
294       for (int i=0; i<IDs.length(); i++) {
295         const SMDS_MeshElement * elem = aMesh->FindElement( IDs[i] );
296         if ( filter( elem ))
297           aMap.insert( aMap.end(), elem );
298       }
299   }
300
301   //================================================================================
302   /*!
303    * \brief Retrieve nodes from SMESH_IDSource
304    */
305   //================================================================================
306
307   void idSourceToNodeSet(SMESH::SMESH_IDSource_ptr  theObject,
308                          const SMESHDS_Mesh*        theMeshDS,
309                          TIDSortedNodeSet&          theNodeSet)
310
311   {
312     if ( CORBA::is_nil( theObject ) )
313       return;
314     SMESH::array_of_ElementType_var types = theObject->GetTypes();
315     SMESH::long_array_var     aElementsId = theObject->GetIDs();
316     if ( types->length() == 1 && types[0] == SMESH::NODE)
317     {
318       for(int i = 0; i < aElementsId->length(); i++)
319         if ( const SMDS_MeshNode * n = theMeshDS->FindNode( aElementsId[i] ))
320           theNodeSet.insert( theNodeSet.end(), n);
321     }
322     else if ( SMESH::DownCast<SMESH_Mesh_i*>( theObject ))
323     {
324       SMDS_NodeIteratorPtr nIt = theMeshDS->nodesIterator();
325       while ( nIt->more( ))
326         if( const SMDS_MeshElement * elem = nIt->next() )
327           theNodeSet.insert( elem->begin_nodes(), elem->end_nodes());
328     }
329     else
330     {
331       for(int i = 0; i < aElementsId->length(); i++)
332         if( const SMDS_MeshElement * elem = theMeshDS->FindElement( aElementsId[i] ))
333           theNodeSet.insert( elem->begin_nodes(), elem->end_nodes());
334     }
335   }
336
337   //================================================================================
338   /*!
339    * \brief Returns elements connected to the given elements
340    */
341   //================================================================================
342
343   void getElementsAround(const TIDSortedElemSet& theElements,
344                          const SMESHDS_Mesh*     theMeshDS,
345                          TIDSortedElemSet&       theElementsAround)
346   {
347     if ( theElements.empty() ) return;
348
349     SMDSAbs_ElementType elemType    = (*theElements.begin())->GetType();
350     bool sameElemType = ( elemType == (*theElements.rbegin())->GetType() );
351     if ( sameElemType &&
352          theMeshDS->GetMeshInfo().NbElements( elemType ) == theElements.size() )
353       return; // all the elements are in theElements
354
355     if ( !sameElemType )
356       elemType = SMDSAbs_All;
357
358     vector<bool> isNodeChecked( theMeshDS->NbNodes(), false );
359
360     TIDSortedElemSet::const_iterator elemIt = theElements.begin();
361     for ( ; elemIt != theElements.end(); ++elemIt )
362     {
363       const SMDS_MeshElement* e = *elemIt;
364       int i = e->NbCornerNodes();
365       while ( --i != -1 )
366       {
367         const SMDS_MeshNode* n = e->GetNode( i );
368         if ( !isNodeChecked[ n->GetID() ])
369         {
370           isNodeChecked[ n->GetID() ] = true;
371           SMDS_ElemIteratorPtr invIt = n->GetInverseElementIterator(elemType);
372           while ( invIt->more() )
373           {
374             const SMDS_MeshElement* elemAround = invIt->next();
375             if ( !theElements.count( elemAround ))
376               theElementsAround.insert( elemAround );
377           }
378         }
379       }
380     }
381   }
382
383   //================================================================================
384   /*!
385    * \brief Return a string used to detect change of mesh part on which theElementSearcher
386    * is going to be used
387    */
388   //================================================================================
389
390   string getPartIOR( SMESH::SMESH_IDSource_ptr theMeshPart, SMESH::ElementType type)
391   {
392     string partIOR = SMESH_Gen_i::GetORB()->object_to_string( theMeshPart );
393     if ( SMESH_Group_i* group_i = SMESH::DownCast<SMESH_Group_i*>( theMeshPart ))
394       // take into account passible group modification
395       partIOR += SMESH_Comment( ((SMESHDS_Group*)group_i->GetGroupDS())->SMDSGroup().Tic() );
396     partIOR += SMESH_Comment( type );
397     return partIOR;
398   }
399
400 } // namespace MeshEditor_I
401
402 using namespace MeshEditor_I;
403
404 //=============================================================================
405 /*!
406  *
407  */
408 //=============================================================================
409
410 SMESH_MeshEditor_i::SMESH_MeshEditor_i(SMESH_Mesh_i* theMesh, bool isPreview):
411   myMesh_i( theMesh ),
412   myMesh( &theMesh->GetImpl() ),
413   myEditor( myMesh ),
414   myIsPreviewMode ( isPreview ),
415   myPreviewMesh( 0 ),
416   myPreviewEditor( 0 )
417 {
418 }
419
420 //================================================================================
421 /*!
422  * \brief Destructor
423  */
424 //================================================================================
425
426 SMESH_MeshEditor_i::~SMESH_MeshEditor_i()
427 {
428   PortableServer::POA_var poa = SMESH_Gen_i::GetPOA();
429   PortableServer::ObjectId_var anObjectId = poa->servant_to_id(this);
430   poa->deactivate_object(anObjectId.in());
431
432   //deleteAuxIDSources();
433   delete myPreviewMesh;   myPreviewMesh = 0;
434   delete myPreviewEditor; myPreviewEditor = 0;
435 }
436
437 //================================================================================
438 /*!
439  * \brief Returns the mesh
440  */
441 //================================================================================
442
443 SMESH::SMESH_Mesh_ptr SMESH_MeshEditor_i::GetMesh()
444 {
445   return myMesh_i->_this();
446 }
447
448 //================================================================================
449 /*!
450  * \brief Clear members
451  */
452 //================================================================================
453
454 void SMESH_MeshEditor_i::initData(bool deleteSearchers)
455 {
456   if ( myIsPreviewMode ) {
457     if ( myPreviewMesh ) myPreviewMesh->RemoveAll();
458   }
459   else {
460     if ( deleteSearchers )
461       TSearchersDeleter::Delete();
462   }
463   getEditor().GetError().reset();
464   getEditor().ClearLastCreated();
465 }
466
467 //================================================================================
468 /*!
469  * \brief Increment mesh modif time and optionally record that the performed
470  *        modification may influence futher mesh re-compute.
471  *  \param [in] isReComputeSafe - true if the modification does not influence
472  *              futher mesh re-compute
473  */
474 //================================================================================
475
476 void SMESH_MeshEditor_i::declareMeshModified( bool isReComputeSafe )
477 {
478   myMesh->GetMeshDS()->Modified();
479   if ( !isReComputeSafe )
480     myMesh->SetIsModified( true );
481 }
482
483 //================================================================================
484 /*!
485  * \brief Return either myEditor or myPreviewEditor depending on myIsPreviewMode.
486  *        WARNING: in preview mode call getPreviewMesh() before getEditor()!
487  */
488 //================================================================================
489
490 ::SMESH_MeshEditor& SMESH_MeshEditor_i::getEditor()
491 {
492   if ( myIsPreviewMode && !myPreviewEditor ) {
493     if ( !myPreviewMesh ) getPreviewMesh();
494     myPreviewEditor = new ::SMESH_MeshEditor( myPreviewMesh );
495   }
496   return myIsPreviewMode ? *myPreviewEditor : myEditor;
497 }
498
499 //================================================================================
500 /*!
501  * \brief Initialize and return myPreviewMesh
502  *  \param previewElements - type of elements to show in preview
503  *
504  *  WARNING: call it once par a method!
505  */
506 //================================================================================
507
508 TPreviewMesh * SMESH_MeshEditor_i::getPreviewMesh(SMDSAbs_ElementType previewElements)
509 {
510   if ( !myPreviewMesh || myPreviewMesh->myPreviewType != previewElements )
511   {
512     delete myPreviewEditor;
513     myPreviewEditor = 0;
514     delete myPreviewMesh;
515     myPreviewMesh = new TPreviewMesh( previewElements );
516   }
517   myPreviewMesh->Clear();
518   return myPreviewMesh;
519 }
520
521 //================================================================================
522 /*!
523  * Return data of mesh edition preview
524  */
525 //================================================================================
526
527 SMESH::MeshPreviewStruct* SMESH_MeshEditor_i::GetPreviewData()
528   throw (SALOME::SALOME_Exception)
529 {
530   SMESH_TRY;
531   const bool hasBadElems = ( getEditor().GetError() && getEditor().GetError()->HasBadElems() );
532
533   if ( myIsPreviewMode || hasBadElems ) { // --- MeshPreviewStruct filling ---
534
535     list<int> aNodesConnectivity;
536     typedef map<int, int> TNodesMap;
537     TNodesMap nodesMap;
538
539     SMESHDS_Mesh* aMeshDS;
540     std::auto_ptr< SMESH_MeshPartDS > aMeshPartDS;
541     if ( hasBadElems ) {
542       aMeshPartDS.reset( new SMESH_MeshPartDS( getEditor().GetError()->myBadElements ));
543       aMeshDS = aMeshPartDS.get();
544     }
545     else {
546       aMeshDS = getEditor().GetMeshDS();
547     }
548     myPreviewData = new SMESH::MeshPreviewStruct();
549     myPreviewData->nodesXYZ.length(aMeshDS->NbNodes());
550
551
552     SMDSAbs_ElementType previewType = SMDSAbs_All;
553     if ( !hasBadElems )
554       if (TPreviewMesh * aPreviewMesh = dynamic_cast< TPreviewMesh* >( getEditor().GetMesh() )) {
555         previewType = aPreviewMesh->myPreviewType;
556         switch ( previewType ) {
557         case SMDSAbs_Edge  : break;
558         case SMDSAbs_Face  : break;
559         case SMDSAbs_Volume: break;
560         default:;
561           if ( aMeshDS->GetMeshInfo().NbElements() == 0 ) previewType = SMDSAbs_Node;
562         }
563       }
564
565     myPreviewData->elementTypes.length( aMeshDS->GetMeshInfo().NbElements( previewType ));
566     int i = 0, j = 0;
567     SMDS_ElemIteratorPtr itMeshElems = aMeshDS->elementsIterator(previewType);
568
569     while ( itMeshElems->more() ) {
570       const SMDS_MeshElement* aMeshElem = itMeshElems->next();
571       SMDS_NodeIteratorPtr itElemNodes = 
572         (( aMeshElem->GetEntityType() == SMDSEntity_Quad_Polygon ) ?
573          aMeshElem->interlacedNodesIterator() :
574          aMeshElem->nodeIterator() );
575       while ( itElemNodes->more() ) {
576         const SMDS_MeshNode* aMeshNode = itElemNodes->next();
577         int aNodeID = aMeshNode->GetID();
578         TNodesMap::iterator anIter = nodesMap.find(aNodeID);
579         if ( anIter == nodesMap.end() ) {
580           // filling the nodes coordinates
581           myPreviewData->nodesXYZ[j].x = aMeshNode->X();
582           myPreviewData->nodesXYZ[j].y = aMeshNode->Y();
583           myPreviewData->nodesXYZ[j].z = aMeshNode->Z();
584           anIter = nodesMap.insert( make_pair(aNodeID, j) ).first;
585           j++;
586         }
587         aNodesConnectivity.push_back(anIter->second);
588       }
589
590       // filling the elements types
591       SMDSAbs_ElementType aType = aMeshElem->GetType();
592       bool               isPoly = aMeshElem->IsPoly();
593       myPreviewData->elementTypes[i].SMDS_ElementType = (SMESH::ElementType) aType;
594       myPreviewData->elementTypes[i].isPoly           = isPoly;
595       myPreviewData->elementTypes[i].nbNodesInElement = aMeshElem->NbNodes();
596       i++;
597     }
598     myPreviewData->nodesXYZ.length( j );
599
600     // filling the elements connectivities
601     list<int>::iterator aConnIter = aNodesConnectivity.begin();
602     myPreviewData->elementConnectivities.length(aNodesConnectivity.size());
603     for( int i = 0; aConnIter != aNodesConnectivity.end(); aConnIter++, i++ )
604       myPreviewData->elementConnectivities[i] = *aConnIter;
605   }
606   return myPreviewData._retn();
607
608   SMESH_CATCH( SMESH::throwCorbaException );
609   return 0;
610 }
611
612 //================================================================================
613 /*!
614  * \brief Returns list of it's IDs of created nodes
615  * \retval SMESH::long_array* - list of node ID
616  */
617 //================================================================================
618
619 SMESH::long_array* SMESH_MeshEditor_i::GetLastCreatedNodes()
620   throw (SALOME::SALOME_Exception)
621 {
622   SMESH_TRY;
623   SMESH::long_array_var myLastCreatedNodes = new SMESH::long_array();
624
625   const SMESH_SequenceOfElemPtr& aSeq = getEditor().GetLastCreatedNodes();
626   myLastCreatedNodes->length( aSeq.Length() );
627   for (int i = 1; i <= aSeq.Length(); i++)
628     myLastCreatedNodes[i-1] = aSeq.Value(i)->GetID();
629
630   return myLastCreatedNodes._retn();
631   SMESH_CATCH( SMESH::throwCorbaException );
632   return 0;
633 }
634
635 //================================================================================
636 /*!
637  * \brief Returns list of it's IDs of created elements
638  * \retval SMESH::long_array* - list of elements' ID
639  */
640 //================================================================================
641
642 SMESH::long_array* SMESH_MeshEditor_i::GetLastCreatedElems()
643   throw (SALOME::SALOME_Exception)
644 {
645   SMESH_TRY;
646   SMESH::long_array_var myLastCreatedElems = new SMESH::long_array();
647
648   const SMESH_SequenceOfElemPtr& aSeq = getEditor().GetLastCreatedElems();
649   myLastCreatedElems->length( aSeq.Length() );
650   for ( int i = 1; i <= aSeq.Length(); i++ )
651     myLastCreatedElems[i-1] = aSeq.Value(i)->GetID();
652
653   return myLastCreatedElems._retn();
654   SMESH_CATCH( SMESH::throwCorbaException );
655   return 0;
656 }
657
658 //=======================================================================
659 //function : ClearLastCreated
660 //purpose  : Clears sequences of last created elements and nodes 
661 //=======================================================================
662
663 void SMESH_MeshEditor_i::ClearLastCreated() throw (SALOME::SALOME_Exception)
664 {
665   SMESH_TRY;
666   getEditor().ClearLastCreated();
667   SMESH_CATCH( SMESH::throwCorbaException );
668 }
669
670 //=======================================================================
671 /*
672  * Returns description of an error/warning occured during the last operation
673  * WARNING: ComputeError.code >= 100 and no corresponding enum in IDL API
674  */
675 //=======================================================================
676
677 SMESH::ComputeError* SMESH_MeshEditor_i::GetLastError()
678   throw (SALOME::SALOME_Exception)
679 {
680   SMESH_TRY;
681   SMESH::ComputeError_var errOut = new SMESH::ComputeError;
682   SMESH_ComputeErrorPtr&  errIn  = getEditor().GetError();
683   if ( errIn && !errIn->IsOK() )
684   {
685     errOut->code       = -( errIn->myName < 0 ? errIn->myName + 1: errIn->myName ); // -1 -> 0
686     errOut->comment    = errIn->myComment.c_str();
687     errOut->subShapeID = -1;
688     errOut->hasBadMesh = !errIn->myBadElements.empty();
689   }
690   else
691   {
692     errOut->code       = 0;
693     errOut->subShapeID = -1;
694     errOut->hasBadMesh = false;
695   }
696
697   return errOut._retn();
698   SMESH_CATCH( SMESH::throwCorbaException );
699   return 0;
700 }
701
702 //=======================================================================
703 //function : MakeIDSource
704 //purpose  : Wrap a sequence of ids in a SMESH_IDSource.
705 //           Call UnRegister() as you fininsh using it!!
706 //=======================================================================
707
708 struct SMESH_MeshEditor_i::_IDSource : public virtual POA_SMESH::SMESH_IDSource,
709                                        public virtual SALOME::GenericObj_i
710 {
711   SMESH::long_array     _ids;
712   SMESH::ElementType    _type;
713   SMESH::SMESH_Mesh_ptr _mesh;
714   SMESH::long_array* GetIDs()      { return new SMESH::long_array( _ids ); }
715   SMESH::long_array* GetMeshInfo() { return 0; }
716   SMESH::long_array* GetNbElementsByType()
717   {
718     SMESH::long_array_var aRes = new SMESH::long_array();
719     aRes->length(SMESH::NB_ELEMENT_TYPES);
720     for (int i = 0; i < SMESH::NB_ELEMENT_TYPES; i++)
721       aRes[ i ] = ( i == _type ) ? _ids.length() : 0;
722     return aRes._retn();  
723   }
724   SMESH::SMESH_Mesh_ptr GetMesh()  { return SMESH::SMESH_Mesh::_duplicate( _mesh ); }
725   bool IsMeshInfoCorrect()         { return true; }
726   SMESH::array_of_ElementType* GetTypes()
727   {
728     SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
729     if ( _ids.length() > 0 ) {
730       types->length( 1 );
731       types[0] = _type;
732     }
733     return types._retn();
734   }
735   SALOMEDS::TMPFile* GetVtkUgStream()
736   {
737     SALOMEDS::TMPFile_var SeqFile;
738     return SeqFile._retn();
739   }
740 };
741
742 SMESH::SMESH_IDSource_ptr SMESH_MeshEditor_i::MakeIDSource(const SMESH::long_array& ids,
743                                                            SMESH::ElementType       type)
744 {
745   _IDSource* idSrc = new _IDSource;
746   idSrc->_mesh = myMesh_i->_this();
747   idSrc->_ids  = ids;
748   idSrc->_type = type;
749   if ( type == SMESH::ALL && ids.length() > 0 )
750     idSrc->_type = myMesh_i->GetElementType( ids[0], true );
751
752   SMESH::SMESH_IDSource_var anIDSourceVar = idSrc->_this();
753
754   return anIDSourceVar._retn();
755 }
756
757 bool SMESH_MeshEditor_i::IsTemporaryIDSource( SMESH::SMESH_IDSource_ptr& idSource )
758 {
759   return SMESH::DownCast<SMESH_MeshEditor_i::_IDSource*>( idSource );
760 }
761
762 CORBA::Long* SMESH_MeshEditor_i::GetTemporaryIDs( SMESH::SMESH_IDSource_ptr& idSource,
763                                                   int&                       nbIds)
764 {
765   if ( _IDSource* tmpIdSource = SMESH::DownCast<SMESH_MeshEditor_i::_IDSource*>( idSource ))
766   {
767     nbIds = (int) tmpIdSource->_ids.length();
768     return & tmpIdSource->_ids[0];
769   }
770   nbIds = 0;
771   return 0;
772 }
773
774 // void SMESH_MeshEditor_i::deleteAuxIDSources()
775 // {
776 //   std::list< _IDSource* >::iterator idSrcIt = myAuxIDSources.begin();
777 //   for ( ; idSrcIt != myAuxIDSources.end(); ++idSrcIt )
778 //     delete *idSrcIt;
779 //   myAuxIDSources.clear();
780 // }
781
782 //=============================================================================
783 /*!
784  *
785  */
786 //=============================================================================
787
788 CORBA::Boolean
789 SMESH_MeshEditor_i::RemoveElements(const SMESH::long_array & IDsOfElements)
790   throw (SALOME::SALOME_Exception)
791 {
792   SMESH_TRY;
793   initData();
794
795   list< int > IdList;
796
797   for (int i = 0; i < IDsOfElements.length(); i++)
798     IdList.push_back( IDsOfElements[i] );
799
800   // Update Python script
801   TPythonDump() << "isDone = " << this << ".RemoveElements( " << IDsOfElements << " )";
802
803   // Remove Elements
804   bool ret = getEditor().Remove( IdList, false );
805
806   declareMeshModified( /*isReComputeSafe=*/ IDsOfElements.length() == 0 ); // issue 0020693
807   return ret;
808
809   SMESH_CATCH( SMESH::throwCorbaException );
810   return 0;
811 }
812
813 //=============================================================================
814 /*!
815  *
816  */
817 //=============================================================================
818
819 CORBA::Boolean SMESH_MeshEditor_i::RemoveNodes(const SMESH::long_array & IDsOfNodes)
820   throw (SALOME::SALOME_Exception)
821 {
822   SMESH_TRY;
823   initData();
824
825   list< int > IdList;
826   for (int i = 0; i < IDsOfNodes.length(); i++)
827     IdList.push_back( IDsOfNodes[i] );
828
829   // Update Python script
830   TPythonDump() << "isDone = " << this << ".RemoveNodes( " << IDsOfNodes << " )";
831
832   bool ret = getEditor().Remove( IdList, true );
833
834   declareMeshModified( /*isReComputeSafe=*/ !ret ); // issue 0020693
835   return ret;
836
837   SMESH_CATCH( SMESH::throwCorbaException );
838   return 0;
839 }
840
841 //=============================================================================
842 /*!
843  *
844  */
845 //=============================================================================
846
847 CORBA::Long SMESH_MeshEditor_i::RemoveOrphanNodes()
848   throw (SALOME::SALOME_Exception)
849 {
850   SMESH_TRY;
851   initData();
852
853   // Update Python script
854   TPythonDump() << "nbRemoved = " << this << ".RemoveOrphanNodes()";
855
856   // Create filter to find all orphan nodes
857   SMESH::Controls::Filter::TIdSequence seq;
858   SMESH::Controls::PredicatePtr predicate( new SMESH::Controls::FreeNodes() );
859   SMESH::Controls::Filter::GetElementsId( getMeshDS(), predicate, seq );
860
861   // remove orphan nodes (if there are any)
862   list< int > IdList;
863   for ( int i = 0; i < seq.size(); i++ )
864     IdList.push_back( seq[i] );
865
866   int nbNodesBefore = myMesh->NbNodes();
867   getEditor().Remove( IdList, true );
868   int nbNodesAfter = myMesh->NbNodes();
869
870   declareMeshModified( /*isReComputeSafe=*/ IdList.size() == 0 ); // issue 0020693
871   return nbNodesBefore - nbNodesAfter;
872
873   SMESH_CATCH( SMESH::throwCorbaException );
874   return 0;
875 }
876
877 //=============================================================================
878 /*!
879  * Add a new node.
880  */
881 //=============================================================================
882
883 CORBA::Long SMESH_MeshEditor_i::AddNode(CORBA::Double x,CORBA::Double y, CORBA::Double z)
884   throw (SALOME::SALOME_Exception)
885 {
886   SMESH_TRY;
887   initData();
888
889   const SMDS_MeshNode* N = getMeshDS()->AddNode(x, y, z);
890
891   // Update Python script
892   TPythonDump() << "nodeID = " << this << ".AddNode( "
893                 << TVar( x ) << ", " << TVar( y ) << ", " << TVar( z )<< " )";
894
895   declareMeshModified( /*isReComputeSafe=*/false );
896   return N->GetID();
897
898   SMESH_CATCH( SMESH::throwCorbaException );
899   return 0;
900 }
901
902 //=============================================================================
903 /*!
904  * Create 0D element on the given node.
905  */
906 //=============================================================================
907
908 CORBA::Long SMESH_MeshEditor_i::Add0DElement(CORBA::Long IDOfNode)
909   throw (SALOME::SALOME_Exception)
910 {
911   SMESH_TRY;
912   initData();
913
914   const SMDS_MeshNode* aNode = getMeshDS()->FindNode(IDOfNode);
915   SMDS_MeshElement* elem = getMeshDS()->Add0DElement(aNode);
916
917   // Update Python script
918   TPythonDump() << "elem0d = " << this << ".Add0DElement( " << IDOfNode <<" )";
919
920   declareMeshModified( /*isReComputeSafe=*/false );
921
922   return elem ? elem->GetID() : 0;
923
924   SMESH_CATCH( SMESH::throwCorbaException );
925   return 0;
926 }
927
928 //=============================================================================
929 /*!
930  * Create a ball element on the given node.
931  */
932 //=============================================================================
933
934 CORBA::Long SMESH_MeshEditor_i::AddBall(CORBA::Long IDOfNode, CORBA::Double diameter)
935   throw (SALOME::SALOME_Exception)
936 {
937   SMESH_TRY;
938   initData();
939
940   if ( diameter < std::numeric_limits<double>::min() )
941     THROW_SALOME_CORBA_EXCEPTION("Invalid diameter", SALOME::BAD_PARAM);
942
943   const SMDS_MeshNode* aNode = getMeshDS()->FindNode(IDOfNode);
944   SMDS_MeshElement* elem = getMeshDS()->AddBall(aNode, diameter);
945
946   // Update Python script
947   TPythonDump() << "ballElem = "
948                 << this << ".AddBall( " << IDOfNode << ", " << diameter <<" )";
949
950   declareMeshModified( /*isReComputeSafe=*/false );
951   return elem ? elem->GetID() : 0;
952
953   SMESH_CATCH( SMESH::throwCorbaException );
954   return 0;
955 }
956
957 //=============================================================================
958 /*!
959  * Create an edge, either linear and quadratic (this is determed
960  *  by number of given nodes, two or three)
961  */
962 //=============================================================================
963
964 CORBA::Long SMESH_MeshEditor_i::AddEdge(const SMESH::long_array & IDsOfNodes)
965   throw (SALOME::SALOME_Exception)
966 {
967   SMESH_TRY;
968   initData();
969
970   int NbNodes = IDsOfNodes.length();
971   SMDS_MeshElement* elem = 0;
972   if (NbNodes == 2)
973   {
974     CORBA::Long index1 = IDsOfNodes[0];
975     CORBA::Long index2 = IDsOfNodes[1];
976     elem = getMeshDS()->AddEdge( getMeshDS()->FindNode(index1),
977                                  getMeshDS()->FindNode(index2));
978
979     // Update Python script
980     TPythonDump() << "edge = " << this << ".AddEdge([ "
981                   << index1 << ", " << index2 <<" ])";
982   }
983   if (NbNodes == 3) {
984     CORBA::Long n1 = IDsOfNodes[0];
985     CORBA::Long n2 = IDsOfNodes[1];
986     CORBA::Long n12 = IDsOfNodes[2];
987     elem = getMeshDS()->AddEdge( getMeshDS()->FindNode(n1),
988                                  getMeshDS()->FindNode(n2),
989                                  getMeshDS()->FindNode(n12));
990     // Update Python script
991     TPythonDump() << "edgeID = " << this << ".AddEdge([ "
992                   <<n1<<", "<<n2<<", "<<n12<<" ])";
993   }
994
995   declareMeshModified( /*isReComputeSafe=*/false );
996   return elem ? elem->GetID() : 0;
997
998   SMESH_CATCH( SMESH::throwCorbaException );
999   return 0;
1000 }
1001
1002 //=============================================================================
1003 /*!
1004  *  AddFace
1005  */
1006 //=============================================================================
1007
1008 CORBA::Long SMESH_MeshEditor_i::AddFace(const SMESH::long_array & IDsOfNodes)
1009   throw (SALOME::SALOME_Exception)
1010 {
1011   SMESH_TRY;
1012   initData();
1013
1014   int NbNodes = IDsOfNodes.length();
1015   if (NbNodes < 3)
1016   {
1017     return 0;
1018   }
1019
1020   std::vector<const SMDS_MeshNode*> nodes (NbNodes);
1021   for (int i = 0; i < NbNodes; i++)
1022     nodes[i] = getMeshDS()->FindNode(IDsOfNodes[i]);
1023
1024   SMDS_MeshElement* elem = 0;
1025   switch (NbNodes) {
1026   case 3: elem = getMeshDS()->AddFace(nodes[0], nodes[1], nodes[2]); break;
1027   case 4: elem = getMeshDS()->AddFace(nodes[0], nodes[1], nodes[2], nodes[3]); break;
1028   case 6: elem = getMeshDS()->AddFace(nodes[0], nodes[1], nodes[2], nodes[3],
1029                                       nodes[4], nodes[5]); break;
1030   case 7: elem = getMeshDS()->AddFace(nodes[0], nodes[1], nodes[2], nodes[3],
1031                                       nodes[4], nodes[5], nodes[6]); break;
1032   case 8: elem = getMeshDS()->AddFace(nodes[0], nodes[1], nodes[2], nodes[3],
1033                                       nodes[4], nodes[5], nodes[6], nodes[7]); break;
1034   case 9: elem = getMeshDS()->AddFace(nodes[0], nodes[1], nodes[2], nodes[3],
1035                                       nodes[4], nodes[5], nodes[6], nodes[7],
1036                                       nodes[8] ); break;
1037   default: elem = getMeshDS()->AddPolygonalFace(nodes);
1038   }
1039
1040   // Update Python script
1041   TPythonDump() << "faceID = " << this << ".AddFace( " << IDsOfNodes << " )";
1042
1043   declareMeshModified( /*isReComputeSafe=*/false );
1044
1045   return elem ? elem->GetID() : 0;
1046
1047   SMESH_CATCH( SMESH::throwCorbaException );
1048   return 0;
1049 }
1050
1051 //=============================================================================
1052 /*!
1053  *  AddPolygonalFace
1054  */
1055 //=============================================================================
1056
1057 CORBA::Long SMESH_MeshEditor_i::AddPolygonalFace (const SMESH::long_array & IDsOfNodes)
1058   throw (SALOME::SALOME_Exception)
1059 {
1060   SMESH_TRY;
1061   initData();
1062
1063   int NbNodes = IDsOfNodes.length();
1064   std::vector<const SMDS_MeshNode*> nodes (NbNodes);
1065   for (int i = 0; i < NbNodes; i++)
1066     if ( ! ( nodes[i] = getMeshDS()->FindNode( IDsOfNodes[i] )))
1067       return 0;
1068
1069   const SMDS_MeshElement* elem = getMeshDS()->AddPolygonalFace(nodes);
1070
1071   // Update Python script
1072   TPythonDump() <<"faceID = "<<this<<".AddPolygonalFace( "<<IDsOfNodes<<" )";
1073
1074   declareMeshModified( /*isReComputeSafe=*/false );
1075   return elem ? elem->GetID() : 0;
1076
1077   SMESH_CATCH( SMESH::throwCorbaException );
1078   return 0;
1079 }
1080
1081 //=============================================================================
1082 /*!
1083  *  AddQuadPolygonalFace
1084  */
1085 //=============================================================================
1086
1087 CORBA::Long SMESH_MeshEditor_i::AddQuadPolygonalFace (const SMESH::long_array & IDsOfNodes)
1088   throw (SALOME::SALOME_Exception)
1089 {
1090   SMESH_TRY;
1091   initData();
1092
1093   int NbNodes = IDsOfNodes.length();
1094   std::vector<const SMDS_MeshNode*> nodes (NbNodes);
1095   for (int i = 0; i < NbNodes; i++)
1096     nodes[i] = getMeshDS()->FindNode(IDsOfNodes[i]);
1097
1098   const SMDS_MeshElement* elem = getMeshDS()->AddQuadPolygonalFace(nodes);
1099
1100   // Update Python script
1101   TPythonDump() <<"faceID = "<<this<<".AddPolygonalFace( "<<IDsOfNodes<<" )";
1102
1103   declareMeshModified( /*isReComputeSafe=*/false );
1104   return elem ? elem->GetID() : 0;
1105
1106   SMESH_CATCH( SMESH::throwCorbaException );
1107   return 0;
1108 }
1109
1110 //=============================================================================
1111 /*!
1112  * Create volume, either linear and quadratic (this is determed
1113  *  by number of given nodes)
1114  */
1115 //=============================================================================
1116
1117 CORBA::Long SMESH_MeshEditor_i::AddVolume(const SMESH::long_array & IDsOfNodes)
1118   throw (SALOME::SALOME_Exception)
1119 {
1120   SMESH_TRY;
1121   initData();
1122
1123   int NbNodes = IDsOfNodes.length();
1124   vector< const SMDS_MeshNode*> n(NbNodes);
1125   for(int i=0;i<NbNodes;i++)
1126     n[i]= getMeshDS()->FindNode(IDsOfNodes[i]);
1127
1128   SMDS_MeshElement* elem = 0;
1129   switch(NbNodes)
1130   {
1131   case 4 :elem = getMeshDS()->AddVolume(n[0],n[1],n[2],n[3]); break;
1132   case 5 :elem = getMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4]); break;
1133   case 6 :elem = getMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5]); break;
1134   case 8 :elem = getMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7]); break;
1135   case 10:elem = getMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5],
1136                                         n[6],n[7],n[8],n[9]);
1137     break;
1138   case 12:elem = getMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5],
1139                                         n[6],n[7],n[8],n[9],n[10],n[11]);
1140     break;
1141   case 13:elem = getMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5],n[6],
1142                                         n[7],n[8],n[9],n[10],n[11],n[12]);
1143     break;
1144   case 15:elem = getMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7],n[8],
1145                                         n[9],n[10],n[11],n[12],n[13],n[14]);
1146     break;
1147   case 20:elem = getMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7],
1148                                         n[8],n[9],n[10],n[11],n[12],n[13],n[14],
1149                                         n[15],n[16],n[17],n[18],n[19]);
1150     break;
1151   case 27:elem = getMeshDS()->AddVolume(n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7],
1152                                         n[8],n[9],n[10],n[11],n[12],n[13],n[14],
1153                                         n[15],n[16],n[17],n[18],n[19],
1154                                         n[20],n[21],n[22],n[23],n[24],n[25],n[26]);
1155     break;
1156   }
1157
1158   // Update Python script
1159   TPythonDump() << "volID = " << this << ".AddVolume( " << IDsOfNodes << " )";
1160
1161   declareMeshModified( /*isReComputeSafe=*/false );
1162   return elem ? elem->GetID() : 0;
1163
1164   SMESH_CATCH( SMESH::throwCorbaException );
1165   return 0;
1166 }
1167
1168 //=============================================================================
1169 /*!
1170  *  AddPolyhedralVolume
1171  */
1172 //=============================================================================
1173 CORBA::Long SMESH_MeshEditor_i::AddPolyhedralVolume (const SMESH::long_array & IDsOfNodes,
1174                                                      const SMESH::long_array & Quantities)
1175   throw (SALOME::SALOME_Exception)
1176 {
1177   SMESH_TRY;
1178   initData();
1179
1180   int NbNodes = IDsOfNodes.length();
1181   std::vector<const SMDS_MeshNode*> n (NbNodes);
1182   for (int i = 0; i < NbNodes; i++)
1183     {
1184       const SMDS_MeshNode* aNode = getMeshDS()->FindNode(IDsOfNodes[i]);
1185       if (!aNode) return 0;
1186       n[i] = aNode;
1187     }
1188
1189   int NbFaces = Quantities.length();
1190   std::vector<int> q (NbFaces);
1191   for (int j = 0; j < NbFaces; j++)
1192     q[j] = Quantities[j];
1193
1194   const SMDS_MeshElement* elem = getMeshDS()->AddPolyhedralVolume(n, q);
1195
1196   // Update Python script
1197   TPythonDump() << "volID = " << this << ".AddPolyhedralVolume( "
1198                 << IDsOfNodes << ", " << Quantities << " )";
1199
1200   declareMeshModified( /*isReComputeSafe=*/false );
1201   return elem ? elem->GetID() : 0;
1202
1203   SMESH_CATCH( SMESH::throwCorbaException );
1204   return 0;
1205 }
1206
1207 //=============================================================================
1208 /*!
1209  *  AddPolyhedralVolumeByFaces
1210  */
1211 //=============================================================================
1212
1213 CORBA::Long SMESH_MeshEditor_i::AddPolyhedralVolumeByFaces (const SMESH::long_array & IdsOfFaces)
1214   throw (SALOME::SALOME_Exception)
1215 {
1216   SMESH_TRY;
1217   initData();
1218
1219   int NbFaces = IdsOfFaces.length();
1220   std::vector<const SMDS_MeshNode*> poly_nodes;
1221   std::vector<int> quantities (NbFaces);
1222
1223   for (int i = 0; i < NbFaces; i++) {
1224     const SMDS_MeshElement* aFace = getMeshDS()->FindElement(IdsOfFaces[i]);
1225     quantities[i] = aFace->NbNodes();
1226
1227     SMDS_ElemIteratorPtr It = aFace->nodesIterator();
1228     while (It->more()) {
1229       poly_nodes.push_back(static_cast<const SMDS_MeshNode *>(It->next()));
1230     }
1231   }
1232
1233   const SMDS_MeshElement* elem = getMeshDS()->AddPolyhedralVolume(poly_nodes, quantities);
1234
1235   // Update Python script
1236   TPythonDump() << "volID = " << this << ".AddPolyhedralVolumeByFaces( "
1237                 << IdsOfFaces << " )";
1238
1239   declareMeshModified( /*isReComputeSafe=*/false );
1240   return elem ? elem->GetID() : 0;
1241
1242   SMESH_CATCH( SMESH::throwCorbaException );
1243   return 0;
1244 }
1245
1246 //=============================================================================
1247 //
1248 // \brief Create 0D elements on all nodes of the given object except those 
1249 //        nodes on which a 0D element already exists.
1250 //  \param theObject object on whose nodes 0D elements will be created.
1251 //  \param theGroupName optional name of a group to add 0D elements created
1252 //         and/or found on nodes of \a theObject.
1253 //  \return an object (a new group or a temporary SMESH_IDSource) holding
1254 //          ids of new and/or found 0D elements.
1255 //
1256 //=============================================================================
1257
1258 SMESH::SMESH_IDSource_ptr
1259 SMESH_MeshEditor_i::Create0DElementsOnAllNodes(SMESH::SMESH_IDSource_ptr theObject,
1260                                                const char*               theGroupName)
1261   throw (SALOME::SALOME_Exception)
1262 {
1263   SMESH_TRY;
1264   initData();
1265
1266   SMESH::SMESH_IDSource_var result;
1267   TPythonDump pyDump;
1268
1269   TIDSortedElemSet elements, elems0D;
1270   if ( idSourceToSet( theObject, getMeshDS(), elements, SMDSAbs_All, /*emptyIfIsMesh=*/1))
1271     getEditor().Create0DElementsOnAllNodes( elements, elems0D );
1272
1273   SMESH::long_array_var newElems = new SMESH::long_array;
1274   newElems->length( elems0D.size() );
1275   TIDSortedElemSet::iterator eIt = elems0D.begin();
1276   for ( size_t i = 0; i < elems0D.size(); ++i, ++eIt )
1277     newElems[ i ] = (*eIt)->GetID();
1278
1279   SMESH::SMESH_GroupBase_var groupToFill;
1280   if ( theGroupName && strlen( theGroupName ))
1281   {
1282     // Get existing group named theGroupName
1283     SMESH::ListOfGroups_var groups = myMesh_i->GetGroups();
1284     for (int i = 0, nbGroups = groups->length(); i < nbGroups; i++ ) {
1285       SMESH::SMESH_GroupBase_var group = groups[i];
1286       if ( !group->_is_nil() ) {
1287         CORBA::String_var name = group->GetName();
1288         if ( strcmp( name.in(), theGroupName ) == 0 && group->GetType() == SMESH::ELEM0D ) {
1289           groupToFill = group;
1290           break;
1291         }
1292       }
1293     }
1294     if ( groupToFill->_is_nil() )
1295       groupToFill = myMesh_i->CreateGroup( SMESH::ELEM0D, theGroupName );
1296     else if ( !SMESH::DownCast< SMESH_Group_i* > ( groupToFill ))
1297       groupToFill = myMesh_i->ConvertToStandalone( groupToFill );
1298   }
1299
1300   if ( SMESH_Group_i* group_i = SMESH::DownCast< SMESH_Group_i* > ( groupToFill ))
1301   {
1302     group_i->Add( newElems );
1303     result = SMESH::SMESH_IDSource::_narrow( groupToFill );
1304     pyDump << groupToFill;
1305   }
1306   else
1307   {
1308     result = MakeIDSource( newElems, SMESH::ELEM0D );
1309     pyDump << "elem0DIDs";
1310   }
1311
1312   pyDump << " = " << this << ".Create0DElementsOnAllNodes( "
1313          << theObject << ", '" << theGroupName << "' )";
1314
1315   return result._retn();
1316
1317   SMESH_CATCH( SMESH::throwCorbaException );
1318   return 0;
1319 }
1320
1321 //=============================================================================
1322 /*!
1323  * \brief Bind a node to a vertex
1324  * \param NodeID - node ID
1325  * \param VertexID - vertex ID available through GEOM_Object.GetSubShapeIndices()[0]
1326  * \retval boolean - false if NodeID or VertexID is invalid
1327  */
1328 //=============================================================================
1329
1330 void SMESH_MeshEditor_i::SetNodeOnVertex(CORBA::Long NodeID, CORBA::Long VertexID)
1331   throw (SALOME::SALOME_Exception)
1332 {
1333   SMESH_TRY;
1334
1335   SMESHDS_Mesh * mesh = getMeshDS();
1336   SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( mesh->FindNode(NodeID) );
1337   if ( !node )
1338     THROW_SALOME_CORBA_EXCEPTION("Invalid NodeID", SALOME::BAD_PARAM);
1339
1340   if ( mesh->MaxShapeIndex() < VertexID )
1341     THROW_SALOME_CORBA_EXCEPTION("Invalid VertexID", SALOME::BAD_PARAM);
1342
1343   TopoDS_Shape shape = mesh->IndexToShape( VertexID );
1344   if ( shape.ShapeType() != TopAbs_VERTEX )
1345     THROW_SALOME_CORBA_EXCEPTION("Invalid VertexID", SALOME::BAD_PARAM);
1346
1347   mesh->SetNodeOnVertex( node, VertexID );
1348
1349   myMesh->SetIsModified( true );
1350
1351   SMESH_CATCH( SMESH::throwCorbaException );
1352 }
1353
1354 //=============================================================================
1355 /*!
1356  * \brief Store node position on an edge
1357  * \param NodeID - node ID
1358  * \param EdgeID - edge ID available through GEOM_Object.GetSubShapeIndices()[0]
1359  * \param paramOnEdge - parameter on edge where the node is located
1360  * \retval boolean - false if any parameter is invalid
1361  */
1362 //=============================================================================
1363
1364 void SMESH_MeshEditor_i::SetNodeOnEdge(CORBA::Long NodeID, CORBA::Long EdgeID,
1365                                        CORBA::Double paramOnEdge)
1366   throw (SALOME::SALOME_Exception)
1367 {
1368   SMESH_TRY;
1369
1370   SMESHDS_Mesh * mesh = getMeshDS();
1371   SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( mesh->FindNode(NodeID) );
1372   if ( !node )
1373     THROW_SALOME_CORBA_EXCEPTION("Invalid NodeID", SALOME::BAD_PARAM);
1374
1375   if ( mesh->MaxShapeIndex() < EdgeID )
1376     THROW_SALOME_CORBA_EXCEPTION("Invalid EdgeID", SALOME::BAD_PARAM);
1377
1378   TopoDS_Shape shape = mesh->IndexToShape( EdgeID );
1379   if ( shape.ShapeType() != TopAbs_EDGE )
1380     THROW_SALOME_CORBA_EXCEPTION("Invalid EdgeID", SALOME::BAD_PARAM);
1381
1382   Standard_Real f,l;
1383   BRep_Tool::Range( TopoDS::Edge( shape ), f,l);
1384   if ( paramOnEdge < f || paramOnEdge > l )
1385     THROW_SALOME_CORBA_EXCEPTION("Invalid paramOnEdge", SALOME::BAD_PARAM);
1386
1387   mesh->SetNodeOnEdge( node, EdgeID, paramOnEdge );
1388
1389   myMesh->SetIsModified( true );
1390
1391   SMESH_CATCH( SMESH::throwCorbaException );
1392 }
1393
1394 //=============================================================================
1395 /*!
1396  * \brief Store node position on a face
1397  * \param NodeID - node ID
1398  * \param FaceID - face ID available through GEOM_Object.GetSubShapeIndices()[0]
1399  * \param u - U parameter on face where the node is located
1400  * \param v - V parameter on face where the node is located
1401  * \retval boolean - false if any parameter is invalid
1402  */
1403 //=============================================================================
1404
1405 void SMESH_MeshEditor_i::SetNodeOnFace(CORBA::Long NodeID, CORBA::Long FaceID,
1406                                        CORBA::Double u, CORBA::Double v)
1407   throw (SALOME::SALOME_Exception)
1408 {
1409   SMESH_TRY;
1410   SMESHDS_Mesh * mesh = getMeshDS();
1411   SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( mesh->FindNode(NodeID) );
1412   if ( !node )
1413     THROW_SALOME_CORBA_EXCEPTION("Invalid NodeID", SALOME::BAD_PARAM);
1414
1415   if ( mesh->MaxShapeIndex() < FaceID )
1416     THROW_SALOME_CORBA_EXCEPTION("Invalid FaceID", SALOME::BAD_PARAM);
1417
1418   TopoDS_Shape shape = mesh->IndexToShape( FaceID );
1419   if ( shape.ShapeType() != TopAbs_FACE )
1420     THROW_SALOME_CORBA_EXCEPTION("Invalid FaceID", SALOME::BAD_PARAM);
1421
1422   BRepAdaptor_Surface surf( TopoDS::Face( shape ));
1423   bool isOut = ( u < surf.FirstUParameter() ||
1424                  u > surf.LastUParameter()  ||
1425                  v < surf.FirstVParameter() ||
1426                  v > surf.LastVParameter() );
1427
1428   if ( isOut ) {
1429 #ifdef _DEBUG_
1430     MESSAGE ( "FACE " << FaceID << " (" << u << "," << v << ") out of "
1431               << " u( " <<  surf.FirstUParameter()
1432               << "," <<  surf.LastUParameter()
1433               << ") v( " <<  surf.FirstVParameter()
1434               << "," <<  surf.LastVParameter() << ")" );
1435 #endif
1436     THROW_SALOME_CORBA_EXCEPTION("Invalid UV", SALOME::BAD_PARAM);
1437   }
1438
1439   mesh->SetNodeOnFace( node, FaceID, u, v );
1440   myMesh->SetIsModified( true );
1441
1442   SMESH_CATCH( SMESH::throwCorbaException );
1443 }
1444
1445 //=============================================================================
1446 /*!
1447  * \brief Bind a node to a solid
1448  * \param NodeID - node ID
1449  * \param SolidID - vertex ID available through GEOM_Object.GetSubShapeIndices()[0]
1450  * \retval boolean - false if NodeID or SolidID is invalid
1451  */
1452 //=============================================================================
1453
1454 void SMESH_MeshEditor_i::SetNodeInVolume(CORBA::Long NodeID, CORBA::Long SolidID)
1455   throw (SALOME::SALOME_Exception)
1456 {
1457   SMESH_TRY;
1458   SMESHDS_Mesh * mesh = getMeshDS();
1459   SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( mesh->FindNode(NodeID) );
1460   if ( !node )
1461     THROW_SALOME_CORBA_EXCEPTION("Invalid NodeID", SALOME::BAD_PARAM);
1462
1463   if ( mesh->MaxShapeIndex() < SolidID )
1464     THROW_SALOME_CORBA_EXCEPTION("Invalid SolidID", SALOME::BAD_PARAM);
1465
1466   TopoDS_Shape shape = mesh->IndexToShape( SolidID );
1467   if ( shape.ShapeType() != TopAbs_SOLID &&
1468        shape.ShapeType() != TopAbs_SHELL)
1469     THROW_SALOME_CORBA_EXCEPTION("Invalid SolidID", SALOME::BAD_PARAM);
1470
1471   mesh->SetNodeInVolume( node, SolidID );
1472
1473   SMESH_CATCH( SMESH::throwCorbaException );
1474 }
1475
1476 //=============================================================================
1477 /*!
1478  * \brief Bind an element to a shape
1479  * \param ElementID - element ID
1480  * \param ShapeID - shape ID available through GEOM_Object.GetSubShapeIndices()[0]
1481  */
1482 //=============================================================================
1483
1484 void SMESH_MeshEditor_i::SetMeshElementOnShape(CORBA::Long ElementID,
1485                                                CORBA::Long ShapeID)
1486   throw (SALOME::SALOME_Exception)
1487 {
1488   SMESH_TRY;
1489   SMESHDS_Mesh * mesh = getMeshDS();
1490   SMDS_MeshElement* elem = const_cast<SMDS_MeshElement*>(mesh->FindElement(ElementID));
1491   if ( !elem )
1492     THROW_SALOME_CORBA_EXCEPTION("Invalid ElementID", SALOME::BAD_PARAM);
1493
1494   if ( mesh->MaxShapeIndex() < ShapeID || ShapeID < 1 )
1495     THROW_SALOME_CORBA_EXCEPTION("Invalid ShapeID", SALOME::BAD_PARAM);
1496
1497   TopoDS_Shape shape = mesh->IndexToShape( ShapeID );
1498   if ( shape.ShapeType() != TopAbs_EDGE &&
1499        shape.ShapeType() != TopAbs_FACE &&
1500        shape.ShapeType() != TopAbs_SOLID &&
1501        shape.ShapeType() != TopAbs_SHELL )
1502     THROW_SALOME_CORBA_EXCEPTION("Invalid shape type", SALOME::BAD_PARAM);
1503
1504   mesh->SetMeshElementOnShape( elem, ShapeID );
1505
1506   myMesh->SetIsModified( true );
1507
1508   SMESH_CATCH( SMESH::throwCorbaException );
1509 }
1510
1511 //=============================================================================
1512 /*!
1513  *
1514  */
1515 //=============================================================================
1516
1517 CORBA::Boolean SMESH_MeshEditor_i::InverseDiag(CORBA::Long NodeID1,
1518                                                CORBA::Long NodeID2)
1519   throw (SALOME::SALOME_Exception)
1520 {
1521   SMESH_TRY;
1522   initData();
1523
1524   const SMDS_MeshNode * n1 = getMeshDS()->FindNode( NodeID1 );
1525   const SMDS_MeshNode * n2 = getMeshDS()->FindNode( NodeID2 );
1526   if ( !n1 || !n2 )
1527     return false;
1528
1529   // Update Python script
1530   TPythonDump() << "isDone = " << this << ".InverseDiag( "
1531                 << NodeID1 << ", " << NodeID2 << " )";
1532
1533   int ret =  getEditor().InverseDiag ( n1, n2 );
1534
1535   declareMeshModified( /*isReComputeSafe=*/false );
1536   return ret;
1537
1538   SMESH_CATCH( SMESH::throwCorbaException );
1539   return 0;
1540 }
1541
1542 //=============================================================================
1543 /*!
1544  *
1545  */
1546 //=============================================================================
1547
1548 CORBA::Boolean SMESH_MeshEditor_i::DeleteDiag(CORBA::Long NodeID1,
1549                                               CORBA::Long NodeID2)
1550   throw (SALOME::SALOME_Exception)
1551 {
1552   SMESH_TRY;
1553   initData();
1554
1555   const SMDS_MeshNode * n1 = getMeshDS()->FindNode( NodeID1 );
1556   const SMDS_MeshNode * n2 = getMeshDS()->FindNode( NodeID2 );
1557   if ( !n1 || !n2 )
1558     return false;
1559
1560   // Update Python script
1561   TPythonDump() << "isDone = " << this << ".DeleteDiag( "
1562                 << NodeID1 << ", " << NodeID2 <<  " )";
1563
1564
1565   bool stat = getEditor().DeleteDiag ( n1, n2 );
1566
1567   declareMeshModified( /*isReComputeSafe=*/!stat );
1568
1569   return stat;
1570
1571   SMESH_CATCH( SMESH::throwCorbaException );
1572   return 0;
1573 }
1574
1575 //=============================================================================
1576 /*!
1577  *
1578  */
1579 //=============================================================================
1580
1581 CORBA::Boolean SMESH_MeshEditor_i::Reorient(const SMESH::long_array & IDsOfElements)
1582   throw (SALOME::SALOME_Exception)
1583 {
1584   SMESH_TRY;
1585   initData();
1586
1587   for (int i = 0; i < IDsOfElements.length(); i++)
1588   {
1589     CORBA::Long index = IDsOfElements[i];
1590     const SMDS_MeshElement * elem = getMeshDS()->FindElement(index);
1591     if ( elem )
1592       getEditor().Reorient( elem );
1593   }
1594   // Update Python script
1595   TPythonDump() << "isDone = " << this << ".Reorient( " << IDsOfElements << " )";
1596
1597   declareMeshModified( /*isReComputeSafe=*/ IDsOfElements.length() == 0 );
1598   return true;
1599
1600   SMESH_CATCH( SMESH::throwCorbaException );
1601   return 0;
1602 }
1603
1604 //=============================================================================
1605 /*!
1606  *
1607  */
1608 //=============================================================================
1609
1610 CORBA::Boolean SMESH_MeshEditor_i::ReorientObject(SMESH::SMESH_IDSource_ptr theObject)
1611   throw (SALOME::SALOME_Exception)
1612 {
1613   SMESH_TRY;
1614   initData();
1615
1616   TPythonDump aTPythonDump; // suppress dump in Reorient()
1617
1618   prepareIdSource( theObject );
1619
1620   SMESH::long_array_var anElementsId = theObject->GetIDs();
1621   CORBA::Boolean isDone = Reorient(anElementsId);
1622
1623   // Update Python script
1624   aTPythonDump << "isDone = " << this << ".ReorientObject( " << theObject << " )";
1625
1626   declareMeshModified( /*isReComputeSafe=*/ anElementsId->length() == 0 );
1627   return isDone;
1628
1629   SMESH_CATCH( SMESH::throwCorbaException );
1630   return 0;
1631 }
1632
1633 //=======================================================================
1634 //function : Reorient2D
1635 //purpose  : Reorient faces contained in \a the2Dgroup.
1636 //           the2Dgroup   - the mesh or its part to reorient
1637 //           theDirection - desired direction of normal of \a theFace
1638 //           theFace      - ID of face whose orientation is checked.
1639 //           It can be < 1 then \a thePoint is used to find a face.
1640 //           thePoint     - is used to find a face if \a theFace < 1.
1641 //           return number of reoriented elements.
1642 //=======================================================================
1643
1644 CORBA::Long SMESH_MeshEditor_i::Reorient2D(SMESH::SMESH_IDSource_ptr the2Dgroup,
1645                                            const SMESH::DirStruct&   theDirection,
1646                                            CORBA::Long               theFace,
1647                                            const SMESH::PointStruct& thePoint)
1648   throw (SALOME::SALOME_Exception)
1649 {
1650   SMESH_TRY;
1651   initData(/*deleteSearchers=*/false);
1652
1653   TIDSortedElemSet elements;
1654   IDSource_Error error;
1655   idSourceToSet( the2Dgroup, getMeshDS(), elements, SMDSAbs_Face, /*emptyIfIsMesh=*/1, &error );
1656   if ( error == IDSource_EMPTY )
1657     return 0;
1658   if ( error == IDSource_INVALID )
1659     THROW_SALOME_CORBA_EXCEPTION("No faces in given group", SALOME::BAD_PARAM);
1660
1661
1662   const SMDS_MeshElement* face = 0;
1663   if ( theFace > 0 )
1664   {
1665     face = getMeshDS()->FindElement( theFace );
1666     if ( !face )
1667       THROW_SALOME_CORBA_EXCEPTION("Inexistent face given", SALOME::BAD_PARAM);
1668     if ( face->GetType() != SMDSAbs_Face )
1669       THROW_SALOME_CORBA_EXCEPTION("Wrong element type", SALOME::BAD_PARAM);
1670   }
1671   else
1672   {
1673     // create theElementSearcher if needed
1674     theSearchersDeleter.Set( myMesh, getPartIOR( the2Dgroup, SMESH::FACE ));
1675     if ( !theElementSearcher )
1676     {
1677       if ( elements.empty() ) // search in the whole mesh
1678       {
1679         if ( myMesh->NbFaces() == 0 )
1680           THROW_SALOME_CORBA_EXCEPTION("No faces in the mesh", SALOME::BAD_PARAM);
1681
1682         theElementSearcher = SMESH_MeshAlgos::GetElementSearcher( *getMeshDS() );
1683       }
1684       else
1685       {
1686         typedef SMDS_SetIterator<const SMDS_MeshElement*, TIDSortedElemSet::const_iterator > TIter;
1687         SMDS_ElemIteratorPtr elemsIt( new TIter( elements.begin(), elements.end() ));
1688
1689         theElementSearcher = SMESH_MeshAlgos::GetElementSearcher( *getMeshDS(), elemsIt);
1690       }
1691     }
1692     // find a face
1693     gp_Pnt p( thePoint.x, thePoint.y, thePoint.z );
1694     face = theElementSearcher->FindClosestTo( p, SMDSAbs_Face );
1695
1696     if ( !face )
1697       THROW_SALOME_CORBA_EXCEPTION("No face found by point", SALOME::INTERNAL_ERROR );
1698     if ( !elements.empty() && !elements.count( face ))
1699       THROW_SALOME_CORBA_EXCEPTION("Found face is not in the group", SALOME::BAD_PARAM );
1700   }
1701
1702   const SMESH::PointStruct * P = &theDirection.PS;
1703   gp_Vec dirVec( P->x, P->y, P->z );
1704   if ( dirVec.Magnitude() < std::numeric_limits< double >::min() )
1705     THROW_SALOME_CORBA_EXCEPTION("Zero size vector", SALOME::BAD_PARAM);
1706
1707   int nbReori = getEditor().Reorient2D( elements, dirVec, face );
1708
1709   if ( nbReori ) {
1710     declareMeshModified( /*isReComputeSafe=*/false );
1711   }
1712   TPythonDump() << this << ".Reorient2D( "
1713                 << the2Dgroup << ", "
1714                 << theDirection << ", "
1715                 << theFace << ", "
1716                 << thePoint << " )";
1717
1718   return nbReori;
1719
1720   SMESH_CATCH( SMESH::throwCorbaException );
1721   return 0;
1722 }
1723
1724 //=======================================================================
1725 //function : Reorient2DBy3D
1726 //purpose  : Reorient faces basing on orientation of adjacent volumes.
1727 //=======================================================================
1728
1729 CORBA::Long SMESH_MeshEditor_i::Reorient2DBy3D(const SMESH::ListOfIDSources& faceGroups,
1730                                                SMESH::SMESH_IDSource_ptr     volumeGroup,
1731                                                CORBA::Boolean                outsideNormal)
1732   throw (SALOME::SALOME_Exception)
1733 {
1734   SMESH_TRY;
1735   initData();
1736
1737   TIDSortedElemSet volumes;
1738   IDSource_Error volsError;
1739   idSourceToSet( volumeGroup, getMeshDS(), volumes, SMDSAbs_Volume, /*emptyIfMesh=*/1, &volsError);
1740
1741   int nbReori = 0;
1742   for ( size_t i = 0; i < faceGroups.length(); ++i )
1743   {
1744     SMESH::SMESH_IDSource_ptr faceGrp = faceGroups[i].in();
1745
1746     TIDSortedElemSet faces;
1747     IDSource_Error error;
1748     idSourceToSet( faceGrp, getMeshDS(), faces, SMDSAbs_Face, /*emptyIfIsMesh=*/1, &error );
1749     if ( error == IDSource_INVALID && faceGroups.length() == 1 )
1750       THROW_SALOME_CORBA_EXCEPTION("No faces in a given object", SALOME::BAD_PARAM);
1751     if ( error == IDSource_OK && volsError != IDSource_OK )
1752       THROW_SALOME_CORBA_EXCEPTION("No volumes in a given object", SALOME::BAD_PARAM);
1753
1754     nbReori += getEditor().Reorient2DBy3D( faces, volumes, outsideNormal );
1755
1756     if ( error != IDSource_EMPTY && faces.empty() ) // all faces in the mesh treated
1757       break;
1758   }
1759
1760   if ( nbReori ) {
1761     declareMeshModified( /*isReComputeSafe=*/false );
1762   }
1763   TPythonDump() << this << ".Reorient2DBy3D( "
1764                 << faceGroups << ", "
1765                 << volumeGroup << ", "
1766                 << outsideNormal << " )";
1767
1768   return nbReori;
1769
1770   SMESH_CATCH( SMESH::throwCorbaException );
1771   return 0;
1772 }
1773
1774 //=============================================================================
1775 /*!
1776  * \brief Fuse neighbour triangles into quadrangles.
1777  */
1778 //=============================================================================
1779
1780 CORBA::Boolean SMESH_MeshEditor_i::TriToQuad (const SMESH::long_array &   IDsOfElements,
1781                                               SMESH::NumericalFunctor_ptr Criterion,
1782                                               CORBA::Double               MaxAngle)
1783   throw (SALOME::SALOME_Exception)
1784 {
1785   SMESH_TRY;
1786   initData();
1787
1788   SMESHDS_Mesh* aMesh = getMeshDS();
1789   TIDSortedElemSet faces,copyFaces;
1790   SMDS_MeshElement::GeomFilter triaFilter(SMDSGeom_TRIANGLE);
1791   arrayToSet(IDsOfElements, aMesh, faces, SMDSAbs_Face, & triaFilter);
1792   TIDSortedElemSet* workElements = & faces;
1793
1794   if ( myIsPreviewMode ) {
1795     SMDSAbs_ElementType select =  SMDSAbs_Face;
1796     getPreviewMesh( SMDSAbs_Face )->Copy( faces, copyFaces, select );
1797     workElements = & copyFaces;
1798   }
1799
1800   SMESH::NumericalFunctor_i* aNumericalFunctor =
1801     dynamic_cast<SMESH::NumericalFunctor_i*>( SMESH_Gen_i::GetServant( Criterion ).in() );
1802   SMESH::Controls::NumericalFunctorPtr aCrit;
1803   if ( !aNumericalFunctor )
1804     aCrit.reset( new SMESH::Controls::MaxElementLength2D() );
1805   else
1806     aCrit = aNumericalFunctor->GetNumericalFunctor();
1807
1808   if ( !myIsPreviewMode ) {
1809     // Update Python script
1810     TPythonDump() << "isDone = " << this << ".TriToQuad( "
1811                   << IDsOfElements << ", " << aNumericalFunctor << ", " << TVar( MaxAngle ) << " )";
1812   }
1813
1814   bool stat = getEditor().TriToQuad( *workElements, aCrit, MaxAngle );
1815
1816   declareMeshModified( /*isReComputeSafe=*/!stat );
1817   return stat;
1818
1819   SMESH_CATCH( SMESH::throwCorbaException );
1820   return 0;
1821 }
1822
1823 //=============================================================================
1824 /*!
1825  * \brief Fuse neighbour triangles into quadrangles.
1826  */
1827 //=============================================================================
1828
1829 CORBA::Boolean SMESH_MeshEditor_i::TriToQuadObject (SMESH::SMESH_IDSource_ptr   theObject,
1830                                                     SMESH::NumericalFunctor_ptr Criterion,
1831                                                     CORBA::Double               MaxAngle)
1832   throw (SALOME::SALOME_Exception)
1833 {
1834   SMESH_TRY;
1835   initData();
1836
1837   TPythonDump aTPythonDump;  // suppress dump in TriToQuad()
1838
1839   prepareIdSource( theObject );
1840   SMESH::long_array_var anElementsId = theObject->GetIDs();
1841   CORBA::Boolean isDone = TriToQuad(anElementsId, Criterion, MaxAngle);
1842
1843   if ( !myIsPreviewMode ) {
1844     SMESH::NumericalFunctor_i* aNumericalFunctor =
1845       SMESH::DownCast<SMESH::NumericalFunctor_i*>( Criterion );
1846
1847     // Update Python script
1848     aTPythonDump << "isDone = " << this << ".TriToQuadObject("
1849                  << theObject << ", " << aNumericalFunctor << ", " << TVar( MaxAngle ) << " )";
1850   }
1851
1852   return isDone;
1853
1854   SMESH_CATCH( SMESH::throwCorbaException );
1855   return 0;
1856 }
1857
1858 //=============================================================================
1859 /*!
1860  * \brief Split quadrangles into triangles.
1861  */
1862 //=============================================================================
1863
1864 CORBA::Boolean SMESH_MeshEditor_i::QuadToTri (const SMESH::long_array &   IDsOfElements,
1865                                               SMESH::NumericalFunctor_ptr Criterion)
1866   throw (SALOME::SALOME_Exception)
1867 {
1868   SMESH_TRY;
1869   initData();
1870
1871   SMESHDS_Mesh* aMesh = getMeshDS();
1872   TIDSortedElemSet faces;
1873   arrayToSet(IDsOfElements, aMesh, faces, SMDSAbs_Face);
1874
1875   SMESH::NumericalFunctor_i* aNumericalFunctor =
1876     dynamic_cast<SMESH::NumericalFunctor_i*>( SMESH_Gen_i::GetServant( Criterion ).in() );
1877   SMESH::Controls::NumericalFunctorPtr aCrit;
1878   if ( !aNumericalFunctor )
1879     aCrit.reset( new SMESH::Controls::AspectRatio() );
1880   else
1881     aCrit = aNumericalFunctor->GetNumericalFunctor();
1882
1883
1884   // Update Python script
1885   TPythonDump() << "isDone = " << this << ".QuadToTri( " << IDsOfElements << ", " << aNumericalFunctor << " )";
1886
1887   CORBA::Boolean stat = getEditor().QuadToTri( faces, aCrit );
1888
1889   declareMeshModified( /*isReComputeSafe=*/false );
1890   return stat;
1891
1892   SMESH_CATCH( SMESH::throwCorbaException );
1893   return 0;
1894 }
1895
1896 //=============================================================================
1897 /*!
1898  * \brief Split quadrangles into triangles.
1899  */
1900 //=============================================================================
1901
1902 CORBA::Boolean SMESH_MeshEditor_i::QuadToTriObject (SMESH::SMESH_IDSource_ptr   theObject,
1903                                                     SMESH::NumericalFunctor_ptr Criterion)
1904   throw (SALOME::SALOME_Exception)
1905 {
1906   SMESH_TRY;
1907   initData();
1908
1909   TPythonDump aTPythonDump;  // suppress dump in QuadToTri()
1910
1911   prepareIdSource( theObject );
1912   SMESH::long_array_var anElementsId = theObject->GetIDs();
1913   CORBA::Boolean isDone = QuadToTri(anElementsId, Criterion);
1914
1915   SMESH::NumericalFunctor_i* aNumericalFunctor =
1916     SMESH::DownCast<SMESH::NumericalFunctor_i*>( Criterion );
1917
1918   // Update Python script
1919   aTPythonDump << "isDone = " << this << ".QuadToTriObject( " << theObject << ", " << aNumericalFunctor << " )";
1920
1921   declareMeshModified( /*isReComputeSafe=*/false );
1922   return isDone;
1923
1924   SMESH_CATCH( SMESH::throwCorbaException );
1925   return 0;
1926 }
1927
1928 //================================================================================
1929 /*!
1930  * \brief Split each of quadrangles into 4 triangles.
1931  *  \param [in] theObject - theQuads Container of quadrangles to split.
1932  */
1933 //================================================================================
1934
1935 void SMESH_MeshEditor_i::QuadTo4Tri (SMESH::SMESH_IDSource_ptr theObject)
1936   throw (SALOME::SALOME_Exception)
1937 {
1938   SMESH_TRY;
1939   initData();
1940
1941   TIDSortedElemSet faces;
1942   if ( !idSourceToSet( theObject, getMeshDS(), faces, SMDSAbs_Face, /*emptyIfIsMesh=*/true ) &&
1943        faces.empty() )
1944     THROW_SALOME_CORBA_EXCEPTION("No faces given", SALOME::BAD_PARAM);
1945
1946   getEditor().QuadTo4Tri( faces );
1947   TPythonDump() << this << ".QuadTo4Tri( " << theObject << " )";
1948
1949   SMESH_CATCH( SMESH::throwCorbaException );
1950 }
1951
1952 //=============================================================================
1953 /*!
1954  * \brief Split quadrangles into triangles.
1955  */
1956 //=============================================================================
1957
1958 CORBA::Boolean SMESH_MeshEditor_i::SplitQuad (const SMESH::long_array & IDsOfElements,
1959                                               CORBA::Boolean            Diag13)
1960   throw (SALOME::SALOME_Exception)
1961 {
1962   SMESH_TRY;
1963   initData();
1964
1965   SMESHDS_Mesh* aMesh = getMeshDS();
1966   TIDSortedElemSet faces;
1967   arrayToSet(IDsOfElements, aMesh, faces, SMDSAbs_Face);
1968
1969   // Update Python script
1970   TPythonDump() << "isDone = " << this << ".SplitQuad( "
1971                 << IDsOfElements << ", " << Diag13 << " )";
1972
1973   CORBA::Boolean stat = getEditor().QuadToTri( faces, Diag13 );
1974
1975   declareMeshModified( /*isReComputeSafe=*/ !stat );
1976   return stat;
1977
1978   SMESH_CATCH( SMESH::throwCorbaException );
1979   return 0;
1980 }
1981
1982 //=============================================================================
1983 /*!
1984  * \brief Split quadrangles into triangles.
1985  */
1986 //=============================================================================
1987
1988 CORBA::Boolean SMESH_MeshEditor_i::SplitQuadObject (SMESH::SMESH_IDSource_ptr theObject,
1989                                                     CORBA::Boolean            Diag13)
1990   throw (SALOME::SALOME_Exception)
1991 {
1992   SMESH_TRY;
1993   initData();
1994
1995   TPythonDump aTPythonDump;  // suppress dump in SplitQuad()
1996
1997   prepareIdSource( theObject );
1998   SMESH::long_array_var anElementsId = theObject->GetIDs();
1999   CORBA::Boolean isDone = SplitQuad(anElementsId, Diag13);
2000
2001   // Update Python script
2002   aTPythonDump << "isDone = " << this << ".SplitQuadObject( "
2003                << theObject << ", " << Diag13 << " )";
2004
2005   declareMeshModified( /*isReComputeSafe=*/!isDone );
2006   return isDone;
2007
2008   SMESH_CATCH( SMESH::throwCorbaException );
2009   return 0;
2010 }
2011
2012
2013 //=============================================================================
2014 /*!
2015  * Find better splitting of the given quadrangle.
2016  *  \param IDOfQuad  ID of the quadrangle to be splitted.
2017  *  \param Criterion A criterion to choose a diagonal for splitting.
2018  *  \return 1 if 1-3 diagonal is better, 2 if 2-4
2019  *          diagonal is better, 0 if error occurs.
2020  */
2021 //=============================================================================
2022
2023 CORBA::Long SMESH_MeshEditor_i::BestSplit (CORBA::Long                 IDOfQuad,
2024                                            SMESH::NumericalFunctor_ptr Criterion)
2025   throw (SALOME::SALOME_Exception)
2026 {
2027   SMESH_TRY;
2028   initData();
2029
2030   const SMDS_MeshElement* quad = getMeshDS()->FindElement(IDOfQuad);
2031   if (quad && quad->GetType() == SMDSAbs_Face && quad->NbNodes() == 4)
2032   {
2033     SMESH::NumericalFunctor_i* aNumericalFunctor =
2034       dynamic_cast<SMESH::NumericalFunctor_i*>(SMESH_Gen_i::GetServant(Criterion).in());
2035     SMESH::Controls::NumericalFunctorPtr aCrit;
2036     if (aNumericalFunctor)
2037       aCrit = aNumericalFunctor->GetNumericalFunctor();
2038     else
2039       aCrit.reset(new SMESH::Controls::AspectRatio());
2040
2041     int id = getEditor().BestSplit(quad, aCrit);
2042     declareMeshModified( /*isReComputeSafe=*/ id < 1 );
2043     return id;
2044   }
2045
2046   SMESH_CATCH( SMESH::throwCorbaException );
2047   return 0;
2048 }
2049
2050 //================================================================================
2051 /*!
2052  * \brief Split volumic elements into tetrahedrons
2053  */
2054 //================================================================================
2055
2056 void SMESH_MeshEditor_i::SplitVolumesIntoTetra (SMESH::SMESH_IDSource_ptr elems,
2057                                                 CORBA::Short              methodFlags)
2058   throw (SALOME::SALOME_Exception)
2059 {
2060   SMESH_TRY;
2061   initData();
2062
2063   ::SMESH_MeshEditor::TFacetOfElem elemSet;
2064   const int noneFacet = -1;
2065   SMDS_ElemIteratorPtr volIt = myMesh_i->GetElements( elems, SMESH::VOLUME );
2066   while( volIt->more() )
2067     elemSet.insert( elemSet.end(), make_pair( volIt->next(), noneFacet ));
2068
2069   getEditor().SplitVolumes( elemSet, int( methodFlags ));
2070   declareMeshModified( /*isReComputeSafe=*/true ); // it does not influence Compute()
2071
2072   TPythonDump() << this << ".SplitVolumesIntoTetra( "
2073                 << elems << ", " << methodFlags << " )";
2074
2075   SMESH_CATCH( SMESH::throwCorbaException );
2076 }
2077
2078 //================================================================================
2079 /*!
2080  * \brief Split hexahedra into triangular prisms
2081  *  \param elems - elements to split
2082  *  \param facetToSplitNormal - normal used to find a facet of hexahedron
2083  *         to split into triangles
2084  *  \param methodFlags - flags passing splitting method:
2085  *         1 - split the hexahedron into 2 prisms
2086  *         2 - split the hexahedron into 4 prisms
2087  */
2088 //================================================================================
2089
2090 void SMESH_MeshEditor_i::SplitHexahedraIntoPrisms( SMESH::SMESH_IDSource_ptr  elems,
2091                                                    const SMESH::PointStruct & startHexPoint,
2092                                                    const SMESH::DirStruct&    facetToSplitNormal,
2093                                                    CORBA::Short               methodFlags,
2094                                                    CORBA::Boolean             allDomains)
2095   throw (SALOME::SALOME_Exception)
2096 {
2097   SMESH_TRY;
2098   initData();
2099   prepareIdSource( elems );
2100
2101   gp_Ax1 facetNorm( gp_Pnt( startHexPoint.x,
2102                             startHexPoint.y,
2103                             startHexPoint.z ),
2104                     gp_Dir( facetToSplitNormal.PS.x,
2105                             facetToSplitNormal.PS.y,
2106                             facetToSplitNormal.PS.z ));
2107   TIDSortedElemSet elemSet;
2108   SMESH::long_array_var anElementsId = elems->GetIDs();
2109   SMDS_MeshElement::GeomFilter filter( SMDSGeom_HEXA );
2110   arrayToSet( anElementsId, getMeshDS(), elemSet, SMDSAbs_Volume, &filter );
2111
2112   ::SMESH_MeshEditor::TFacetOfElem elemFacets;
2113   while ( !elemSet.empty() )
2114   {
2115     getEditor().GetHexaFacetsToSplit( elemSet, facetNorm, elemFacets );
2116     if ( !allDomains )
2117       break;
2118
2119     ::SMESH_MeshEditor::TFacetOfElem::iterator ef = elemFacets.begin();
2120     for ( ; ef != elemFacets.end(); ++ef )
2121       elemSet.erase( ef->first );
2122   }
2123
2124   if ( methodFlags == 2 )
2125     methodFlags = int( ::SMESH_MeshEditor::HEXA_TO_4_PRISMS );
2126   else
2127     methodFlags = int( ::SMESH_MeshEditor::HEXA_TO_2_PRISMS );
2128
2129   getEditor().SplitVolumes( elemFacets, int( methodFlags ));
2130   declareMeshModified( /*isReComputeSafe=*/true ); // it does not influence Compute()
2131
2132   TPythonDump() << this << ".SplitHexahedraIntoPrisms( "
2133                 << elems << ", "
2134                 << startHexPoint << ", "
2135                 << facetToSplitNormal<< ", "
2136                 << methodFlags<< ", "
2137                 << allDomains << " )";
2138
2139   SMESH_CATCH( SMESH::throwCorbaException );
2140 }
2141
2142 //================================================================================
2143 /*!
2144  * \brief Split bi-quadratic elements into linear ones without creation of additional nodes:
2145  *   - bi-quadratic triangle will be split into 3 linear quadrangles;
2146  *   - bi-quadratic quadrangle will be split into 4 linear quadrangles;
2147  *   - tri-quadratic hexahedron will be split into 8 linear hexahedra.
2148  *   Quadratic elements of lower dimension  adjacent to the split bi-quadratic element
2149  *   will be split in order to keep the mesh conformal.
2150  *  \param elems - elements to split
2151  */
2152 //================================================================================
2153
2154 void SMESH_MeshEditor_i::SplitBiQuadraticIntoLinear(const SMESH::ListOfIDSources& theElems)
2155   throw (SALOME::SALOME_Exception)
2156 {
2157   SMESH_TRY;
2158   initData();
2159
2160   TIDSortedElemSet elemSet;
2161   for ( size_t i = 0; i < theElems.length(); ++i )
2162   {
2163     SMESH::SMESH_IDSource_ptr elems = theElems[i].in();
2164     SMESH::SMESH_Mesh_var      mesh = elems->GetMesh();
2165     if ( mesh->GetId() != myMesh_i->GetId() )
2166       THROW_SALOME_CORBA_EXCEPTION("Wrong mesh of IDSource", SALOME::BAD_PARAM);
2167
2168     idSourceToSet( elems, getMeshDS(), elemSet, SMDSAbs_All );
2169   }
2170   getEditor().SplitBiQuadraticIntoLinear( elemSet );
2171
2172   declareMeshModified( /*isReComputeSafe=*/true ); // it does not influence Compute()
2173
2174   TPythonDump() << this << ".SplitBiQuadraticIntoLinear( "
2175                 << theElems << " )";
2176
2177   SMESH_CATCH( SMESH::throwCorbaException );
2178 }
2179
2180 //=======================================================================
2181 //function : Smooth
2182 //purpose  :
2183 //=======================================================================
2184
2185 CORBA::Boolean
2186 SMESH_MeshEditor_i::Smooth(const SMESH::long_array &              IDsOfElements,
2187                            const SMESH::long_array &              IDsOfFixedNodes,
2188                            CORBA::Long                            MaxNbOfIterations,
2189                            CORBA::Double                          MaxAspectRatio,
2190                            SMESH::SMESH_MeshEditor::Smooth_Method Method)
2191   throw (SALOME::SALOME_Exception)
2192 {
2193   return smooth( IDsOfElements, IDsOfFixedNodes, MaxNbOfIterations,
2194                  MaxAspectRatio, Method, false );
2195 }
2196
2197
2198 //=======================================================================
2199 //function : SmoothParametric
2200 //purpose  :
2201 //=======================================================================
2202
2203 CORBA::Boolean
2204 SMESH_MeshEditor_i::SmoothParametric(const SMESH::long_array &              IDsOfElements,
2205                                      const SMESH::long_array &              IDsOfFixedNodes,
2206                                      CORBA::Long                            MaxNbOfIterations,
2207                                      CORBA::Double                          MaxAspectRatio,
2208                                      SMESH::SMESH_MeshEditor::Smooth_Method Method)
2209   throw (SALOME::SALOME_Exception)
2210 {
2211   return smooth( IDsOfElements, IDsOfFixedNodes, MaxNbOfIterations,
2212                  MaxAspectRatio, Method, true );
2213 }
2214
2215
2216 //=======================================================================
2217 //function : SmoothObject
2218 //purpose  :
2219 //=======================================================================
2220
2221 CORBA::Boolean
2222 SMESH_MeshEditor_i::SmoothObject(SMESH::SMESH_IDSource_ptr              theObject,
2223                                  const SMESH::long_array &              IDsOfFixedNodes,
2224                                  CORBA::Long                            MaxNbOfIterations,
2225                                  CORBA::Double                          MaxAspectRatio,
2226                                  SMESH::SMESH_MeshEditor::Smooth_Method Method)
2227   throw (SALOME::SALOME_Exception)
2228 {
2229   return smoothObject (theObject, IDsOfFixedNodes, MaxNbOfIterations,
2230                        MaxAspectRatio, Method, false);
2231 }
2232
2233
2234 //=======================================================================
2235 //function : SmoothParametricObject
2236 //purpose  :
2237 //=======================================================================
2238
2239 CORBA::Boolean
2240 SMESH_MeshEditor_i::SmoothParametricObject(SMESH::SMESH_IDSource_ptr              theObject,
2241                                            const SMESH::long_array &              IDsOfFixedNodes,
2242                                            CORBA::Long                            MaxNbOfIterations,
2243                                            CORBA::Double                          MaxAspectRatio,
2244                                            SMESH::SMESH_MeshEditor::Smooth_Method Method)
2245   throw (SALOME::SALOME_Exception)
2246 {
2247   return smoothObject (theObject, IDsOfFixedNodes, MaxNbOfIterations,
2248                        MaxAspectRatio, Method, true);
2249 }
2250
2251
2252 //=============================================================================
2253 /*!
2254  *
2255  */
2256 //=============================================================================
2257
2258 CORBA::Boolean
2259 SMESH_MeshEditor_i::smooth(const SMESH::long_array &              IDsOfElements,
2260                            const SMESH::long_array &              IDsOfFixedNodes,
2261                            CORBA::Long                            MaxNbOfIterations,
2262                            CORBA::Double                          MaxAspectRatio,
2263                            SMESH::SMESH_MeshEditor::Smooth_Method Method,
2264                            bool                                   IsParametric)
2265   throw (SALOME::SALOME_Exception)
2266 {
2267   SMESH_TRY;
2268   initData();
2269
2270   SMESHDS_Mesh* aMesh = getMeshDS();
2271
2272   TIDSortedElemSet elements;
2273   arrayToSet(IDsOfElements, aMesh, elements, SMDSAbs_Face);
2274
2275   set<const SMDS_MeshNode*> fixedNodes;
2276   for (int i = 0; i < IDsOfFixedNodes.length(); i++) {
2277     CORBA::Long index = IDsOfFixedNodes[i];
2278     const SMDS_MeshNode * node = aMesh->FindNode(index);
2279     if ( node )
2280       fixedNodes.insert( node );
2281   }
2282   ::SMESH_MeshEditor::SmoothMethod method = ::SMESH_MeshEditor::LAPLACIAN;
2283   if ( Method != SMESH::SMESH_MeshEditor::LAPLACIAN_SMOOTH )
2284     method = ::SMESH_MeshEditor::CENTROIDAL;
2285
2286   getEditor().Smooth(elements, fixedNodes, method,
2287                   MaxNbOfIterations, MaxAspectRatio, IsParametric );
2288
2289   declareMeshModified( /*isReComputeSafe=*/true ); // does not prevent re-compute
2290
2291   // Update Python script
2292   TPythonDump() << "isDone = " << this << "."
2293                 << (IsParametric ? "SmoothParametric( " : "Smooth( ")
2294                 << IDsOfElements << ", "     << IDsOfFixedNodes << ", "
2295                 << TVar( MaxNbOfIterations ) << ", " << TVar( MaxAspectRatio ) << ", "
2296                 << "SMESH.SMESH_MeshEditor."
2297                 << ( Method == SMESH::SMESH_MeshEditor::CENTROIDAL_SMOOTH ?
2298                      "CENTROIDAL_SMOOTH )" : "LAPLACIAN_SMOOTH )");
2299
2300   return true;
2301
2302   SMESH_CATCH( SMESH::throwCorbaException );
2303   return 0;
2304 }
2305
2306 //=============================================================================
2307 /*!
2308  *
2309  */
2310 //=============================================================================
2311
2312 CORBA::Boolean
2313 SMESH_MeshEditor_i::smoothObject(SMESH::SMESH_IDSource_ptr              theObject,
2314                                  const SMESH::long_array &              IDsOfFixedNodes,
2315                                  CORBA::Long                            MaxNbOfIterations,
2316                                  CORBA::Double                          MaxAspectRatio,
2317                                  SMESH::SMESH_MeshEditor::Smooth_Method Method,
2318                                  bool                                   IsParametric)
2319   throw (SALOME::SALOME_Exception)
2320 {
2321   SMESH_TRY;
2322   initData();
2323
2324   TPythonDump aTPythonDump;  // suppress dump in smooth()
2325
2326   prepareIdSource( theObject );
2327   SMESH::long_array_var anElementsId = theObject->GetIDs();
2328   CORBA::Boolean isDone = smooth (anElementsId, IDsOfFixedNodes, MaxNbOfIterations,
2329                                   MaxAspectRatio, Method, IsParametric);
2330
2331   // Update Python script
2332   aTPythonDump << "isDone = " << this << "."
2333                << (IsParametric ? "SmoothParametricObject( " : "SmoothObject( ")
2334                << theObject << ", " << IDsOfFixedNodes << ", "
2335                << TVar( MaxNbOfIterations ) << ", " << TVar( MaxAspectRatio ) << ", "
2336                << "SMESH.SMESH_MeshEditor."
2337                << ( Method == SMESH::SMESH_MeshEditor::CENTROIDAL_SMOOTH ?
2338                     "CENTROIDAL_SMOOTH )" : "LAPLACIAN_SMOOTH )");
2339
2340   return isDone;
2341
2342   SMESH_CATCH( SMESH::throwCorbaException );
2343   return 0;
2344 }
2345
2346 //=============================================================================
2347 /*!
2348  *
2349  */
2350 //=============================================================================
2351
2352 void SMESH_MeshEditor_i::RenumberNodes()
2353   throw (SALOME::SALOME_Exception)
2354 {
2355   SMESH_TRY;
2356   // Update Python script
2357   TPythonDump() << this << ".RenumberNodes()";
2358
2359   getMeshDS()->Renumber( true );
2360
2361   SMESH_CATCH( SMESH::throwCorbaException );
2362 }
2363
2364 //=============================================================================
2365 /*!
2366  *
2367  */
2368 //=============================================================================
2369
2370 void SMESH_MeshEditor_i::RenumberElements()
2371   throw (SALOME::SALOME_Exception)
2372 {
2373   SMESH_TRY;
2374   // Update Python script
2375   TPythonDump() << this << ".RenumberElements()";
2376
2377   getMeshDS()->Renumber( false );
2378
2379   SMESH_CATCH( SMESH::throwCorbaException );
2380 }
2381
2382 //=======================================================================
2383 /*!
2384  * \brief Return groups by their IDs
2385  */
2386 //=======================================================================
2387
2388 SMESH::ListOfGroups* SMESH_MeshEditor_i::getGroups(const std::list<int>* groupIDs)
2389   throw (SALOME::SALOME_Exception)
2390 {
2391   SMESH_TRY;
2392   if ( !groupIDs )
2393     return 0;
2394   myMesh_i->CreateGroupServants();
2395   return myMesh_i->GetGroups( *groupIDs );
2396
2397   SMESH_CATCH( SMESH::throwCorbaException );
2398   return 0;
2399 }
2400
2401 //=======================================================================
2402 //function : RotationSweepObjects
2403 //purpose  :
2404 //=======================================================================
2405
2406 SMESH::ListOfGroups*
2407 SMESH_MeshEditor_i::RotationSweepObjects(const SMESH::ListOfIDSources & theNodes,
2408                                          const SMESH::ListOfIDSources & theEdges,
2409                                          const SMESH::ListOfIDSources & theFaces,
2410                                          const SMESH::AxisStruct &      theAxis,
2411                                          CORBA::Double                  theAngleInRadians,
2412                                          CORBA::Long                    theNbOfSteps,
2413                                          CORBA::Double                  theTolerance,
2414                                          const bool                     theMakeGroups)
2415   throw (SALOME::SALOME_Exception)
2416 {
2417   SMESH_TRY;
2418   initData();
2419
2420   TIDSortedElemSet elemsNodes[2];
2421   for ( int i = 0, nb = theNodes.length(); i < nb; ++i ) {
2422     SMDS_ElemIteratorPtr nIt = myMesh_i->GetElements( theNodes[i], SMESH::NODE );
2423     while ( nIt->more() ) elemsNodes[1].insert( nIt->next() );
2424   }
2425   for ( int i = 0, nb = theEdges.length(); i < nb; ++i )
2426     idSourceToSet( theEdges[i], getMeshDS(), elemsNodes[0], SMDSAbs_Edge );
2427   for ( int i = 0, nb = theFaces.length(); i < nb; ++i )
2428     idSourceToSet( theFaces[i], getMeshDS(), elemsNodes[0], SMDSAbs_Face );
2429
2430   TIDSortedElemSet* workElements = & elemsNodes[0], copyElements[2];
2431   bool              makeWalls=true;
2432   if ( myIsPreviewMode )
2433   {
2434     SMDSAbs_ElementType select = SMDSAbs_All, avoid = SMDSAbs_Volume;
2435     TPreviewMesh * tmpMesh = getPreviewMesh();
2436     tmpMesh->Copy( elemsNodes[0], copyElements[0], select, avoid );
2437     tmpMesh->Copy( elemsNodes[1], copyElements[1], select, avoid );
2438     workElements = & copyElements[0];
2439     //makeWalls = false; -- faces are needed for preview
2440   }
2441
2442   TPythonDump aPythonDump; // it is here to prevent dump of getGroups()
2443
2444   gp_Ax1 Ax1 (gp_Pnt( theAxis.x,  theAxis.y,  theAxis.z ),
2445               gp_Vec( theAxis.vx, theAxis.vy, theAxis.vz ));
2446
2447   ::SMESH_MeshEditor::PGroupIDs groupIds =
2448       getEditor().RotationSweep (workElements, Ax1, theAngleInRadians,
2449                                  theNbOfSteps, theTolerance, theMakeGroups, makeWalls);
2450
2451   SMESH::ListOfGroups * aGroups = theMakeGroups ? getGroups( groupIds.get()) : 0;
2452
2453   declareMeshModified( /*isReComputeSafe=*/true ); // does not influence Compute()
2454
2455   if ( !myIsPreviewMode )
2456   {
2457     dumpGroupsList( aPythonDump, aGroups );
2458     aPythonDump << this<< ".RotationSweepObjects( "
2459                 << theNodes                  << ", "
2460                 << theEdges                  << ", "
2461                 << theFaces                  << ", "
2462                 << theAxis                   << ", "
2463                 << TVar( theAngleInRadians ) << ", "
2464                 << TVar( theNbOfSteps      ) << ", "
2465                 << TVar( theTolerance      ) << ", "
2466                 << theMakeGroups             << " )";
2467   }
2468   else
2469   {
2470     getPreviewMesh()->Remove( SMDSAbs_Volume );
2471   }
2472
2473   return aGroups ? aGroups : new SMESH::ListOfGroups;
2474
2475   SMESH_CATCH( SMESH::throwCorbaException );
2476   return 0;
2477 }
2478
2479 namespace MeshEditor_I
2480 {
2481   /*!
2482    * \brief Structure used to pass extrusion parameters to ::SMESH_MeshEditor
2483    */
2484   struct ExtrusionParams : public ::SMESH_MeshEditor::ExtrusParam
2485   {
2486     bool myIsExtrusionByNormal;
2487
2488     static int makeFlags( CORBA::Boolean MakeGroups,
2489                           CORBA::Boolean ByAverageNormal = false,
2490                           CORBA::Boolean UseInputElemsOnly = false,
2491                           CORBA::Long    Flags = 0,
2492                           CORBA::Boolean MakeBoundary = true )
2493     {
2494       if ( MakeGroups       ) Flags |= ::SMESH_MeshEditor::EXTRUSION_FLAG_GROUPS;
2495       if ( ByAverageNormal  ) Flags |= ::SMESH_MeshEditor::EXTRUSION_FLAG_BY_AVG_NORMAL;
2496       if ( UseInputElemsOnly) Flags |= ::SMESH_MeshEditor::EXTRUSION_FLAG_USE_INPUT_ELEMS_ONLY;
2497       if ( MakeBoundary     ) Flags |= ::SMESH_MeshEditor::EXTRUSION_FLAG_BOUNDARY;
2498       return Flags;
2499     }
2500     // standard params
2501     ExtrusionParams(const SMESH::DirStruct &  theDir,
2502                     CORBA::Long               theNbOfSteps,
2503                     CORBA::Boolean            theMakeGroups):
2504       ::SMESH_MeshEditor::ExtrusParam ( gp_Vec( theDir.PS.x,
2505                                                 theDir.PS.y,
2506                                                 theDir.PS.z ),
2507                                         theNbOfSteps,
2508                                         makeFlags( theMakeGroups )),
2509       myIsExtrusionByNormal( false )
2510     {
2511     }
2512     // advanced params
2513     ExtrusionParams(const SMESH::DirStruct &  theDir,
2514                     CORBA::Long               theNbOfSteps,
2515                     CORBA::Boolean            theMakeGroups,
2516                     CORBA::Long               theExtrFlags,
2517                     CORBA::Double             theSewTolerance):
2518       ::SMESH_MeshEditor::ExtrusParam ( gp_Vec( theDir.PS.x,
2519                                                 theDir.PS.y,
2520                                                 theDir.PS.z ),
2521                                         theNbOfSteps,
2522                                         makeFlags( theMakeGroups, false, false,
2523                                                    theExtrFlags, false ),
2524                                         theSewTolerance ),
2525       myIsExtrusionByNormal( false )
2526     {
2527     }
2528     // params for extrusion by normal
2529     ExtrusionParams(CORBA::Double  theStepSize,
2530                     CORBA::Long    theNbOfSteps,
2531                     CORBA::Short   theDim,
2532                     CORBA::Boolean theByAverageNormal,
2533                     CORBA::Boolean theUseInputElemsOnly,
2534                     CORBA::Boolean theMakeGroups ):
2535       ::SMESH_MeshEditor::ExtrusParam ( theStepSize, 
2536                                         theNbOfSteps,
2537                                         makeFlags( theMakeGroups,
2538                                                    theByAverageNormal, theUseInputElemsOnly ),
2539                                         theDim),
2540       myIsExtrusionByNormal( true )
2541     {
2542     }
2543
2544     void SetNoGroups()
2545     {
2546       Flags() &= ~(::SMESH_MeshEditor::EXTRUSION_FLAG_GROUPS);
2547     }
2548   };
2549 }
2550
2551 //=======================================================================
2552 /*!
2553  * \brief Generate dim+1 elements by extrusion of elements along vector
2554  *  \param [in] edges - edges to extrude: a list including groups, sub-meshes or a mesh
2555  *  \param [in] faces - faces to extrude: a list including groups, sub-meshes or a mesh
2556  *  \param [in] nodes - nodes to extrude: a list including groups, sub-meshes or a mesh
2557  *  \param [in] stepVector - vector giving direction and distance of an extrusion step
2558  *  \param [in] nbOfSteps - number of elements to generate from one element
2559  *  \param [in] toMakeGroups - if true, new elements will be included into new groups
2560  *              corresponding to groups the input elements included in.
2561  *  \return ListOfGroups - new groups craeted if \a toMakeGroups is true
2562  */
2563 //=======================================================================
2564
2565 SMESH::ListOfGroups*
2566 SMESH_MeshEditor_i::ExtrusionSweepObjects(const SMESH::ListOfIDSources & theNodes,
2567                                           const SMESH::ListOfIDSources & theEdges,
2568                                           const SMESH::ListOfIDSources & theFaces,
2569                                           const SMESH::DirStruct &       theStepVector,
2570                                           CORBA::Long                    theNbOfSteps,
2571                                           CORBA::Boolean                 theToMakeGroups)
2572   throw (SALOME::SALOME_Exception)
2573 {
2574   SMESH_TRY;
2575   initData();
2576
2577   ExtrusionParams params( theStepVector, theNbOfSteps, theToMakeGroups );
2578
2579   TIDSortedElemSet elemsNodes[2];
2580   for ( int i = 0, nb = theNodes.length(); i < nb; ++i ) {
2581     SMDS_ElemIteratorPtr nIt = myMesh_i->GetElements( theNodes[i], SMESH::NODE );
2582     while ( nIt->more() ) elemsNodes[1].insert( nIt->next() );
2583   }
2584   for ( int i = 0, nb = theEdges.length(); i < nb; ++i )
2585     idSourceToSet( theEdges[i], getMeshDS(), elemsNodes[0], SMDSAbs_Edge );
2586   for ( int i = 0, nb = theFaces.length(); i < nb; ++i )
2587     idSourceToSet( theFaces[i], getMeshDS(), elemsNodes[0], SMDSAbs_Face );
2588
2589   TIDSortedElemSet* workElements = & elemsNodes[0], copyElements[2];
2590   SMDSAbs_ElementType previewType = SMDSAbs_All; //SMDSAbs_Face;
2591   if ( myIsPreviewMode )
2592   {
2593     // if ( (*elemsNodes.begin())->GetType() == SMDSAbs_Node )
2594     //   previewType = SMDSAbs_Edge;
2595
2596     SMDSAbs_ElementType select = SMDSAbs_All, avoid = SMDSAbs_Volume;
2597     TPreviewMesh * tmpMesh = getPreviewMesh( previewType );
2598     tmpMesh->Copy( elemsNodes[0], copyElements[0], select, avoid );
2599     tmpMesh->Copy( elemsNodes[1], copyElements[1], select, avoid );
2600     workElements = & copyElements[0];
2601
2602     params.SetNoGroups();
2603   }
2604   TPythonDump aPythonDump; // it is here to prevent dump of getGroups()
2605
2606   ::SMESH_MeshEditor::TTElemOfElemListMap aHistory;
2607   ::SMESH_MeshEditor::PGroupIDs groupIds =
2608       getEditor().ExtrusionSweep( workElements, params, aHistory );
2609
2610   SMESH::ListOfGroups * aGroups = theToMakeGroups ? getGroups( groupIds.get()) : 0;
2611
2612   declareMeshModified( /*isReComputeSafe=*/true ); // does not influence Compute()
2613
2614   if ( !myIsPreviewMode )
2615   {
2616     dumpGroupsList( aPythonDump, aGroups );
2617     aPythonDump << this<< ".ExtrusionSweepObjects( "
2618                 << theNodes             << ", "
2619                 << theEdges             << ", "
2620                 << theFaces             << ", "
2621                 << theStepVector        << ", "
2622                 << TVar( theNbOfSteps ) << ", "
2623                 << theToMakeGroups      << " )";
2624   }
2625   else
2626   {
2627     getPreviewMesh( previewType )->Remove( SMDSAbs_Volume );
2628   }
2629
2630   return aGroups ? aGroups : new SMESH::ListOfGroups;
2631
2632   SMESH_CATCH( SMESH::throwCorbaException );
2633   return 0;
2634 }
2635
2636 //=======================================================================
2637 //function : ExtrusionByNormal
2638 //purpose  :
2639 //=======================================================================
2640
2641 SMESH::ListOfGroups*
2642 SMESH_MeshEditor_i::ExtrusionByNormal(const SMESH::ListOfIDSources& objects,
2643                                       CORBA::Double                 stepSize,
2644                                       CORBA::Long                   nbOfSteps,
2645                                       CORBA::Boolean                byAverageNormal,
2646                                       CORBA::Boolean                useInputElemsOnly,
2647                                       CORBA::Boolean                makeGroups,
2648                                       CORBA::Short                  dim)
2649   throw (SALOME::SALOME_Exception)
2650 {
2651   SMESH_TRY;
2652   initData();
2653
2654   TPythonDump aPythonDump; // it is here to prevent dump of GetGroups()
2655
2656   ExtrusionParams params( stepSize, nbOfSteps, dim,
2657                           byAverageNormal, useInputElemsOnly, makeGroups );
2658
2659   SMDSAbs_ElementType elemType = ( dim == 1 ? SMDSAbs_Edge : SMDSAbs_Face );
2660   if ( objects.length() > 0 && !SMESH::DownCast<SMESH_Mesh_i*>( objects[0] ))
2661   {
2662     SMESH::array_of_ElementType_var elemTypes = objects[0]->GetTypes();
2663     if (( elemTypes->length() == 1 ) &&
2664         ( elemTypes[0] == SMESH::EDGE || elemTypes[0] == SMESH::FACE ))
2665       elemType = ( SMDSAbs_ElementType ) elemTypes[0];
2666   }
2667
2668   TIDSortedElemSet elemsNodes[2];
2669   for ( int i = 0, nb = objects.length(); i < nb; ++i )
2670     idSourceToSet( objects[i], getMeshDS(), elemsNodes[0], elemType );
2671
2672   TIDSortedElemSet* workElements = & elemsNodes[0], copyElements[2];
2673   SMDSAbs_ElementType previewType = SMDSAbs_Face;
2674   if ( myIsPreviewMode )
2675   {
2676     SMDSAbs_ElementType select = SMDSAbs_All, avoid = SMDSAbs_Volume;
2677     TPreviewMesh * tmpMesh = getPreviewMesh( previewType );
2678     tmpMesh->Copy( elemsNodes[0], copyElements[0], select, avoid );
2679     workElements = & copyElements[0];
2680
2681     params.SetNoGroups();
2682   }
2683
2684   ::SMESH_MeshEditor::TTElemOfElemListMap aHistory;
2685   ::SMESH_MeshEditor::PGroupIDs groupIds =
2686       getEditor().ExtrusionSweep( workElements, params, aHistory );
2687
2688   SMESH::ListOfGroups * aGroups = makeGroups ? getGroups( groupIds.get()) : 0;
2689
2690   if (!myIsPreviewMode) {
2691     dumpGroupsList(aPythonDump, aGroups);
2692     aPythonDump << this << ".ExtrusionByNormal( " << objects
2693                 << ", " << TVar( stepSize )
2694                 << ", " << TVar( nbOfSteps )
2695                 << ", " << byAverageNormal
2696                 << ", " << useInputElemsOnly
2697                 << ", " << makeGroups
2698                 << ", " << dim
2699                 << " )";
2700   }
2701   else
2702   {
2703     getPreviewMesh( previewType )->Remove( SMDSAbs_Volume );
2704   }
2705
2706   declareMeshModified( /*isReComputeSafe=*/true ); // does not influence Compute()
2707
2708   return aGroups ? aGroups : new SMESH::ListOfGroups;
2709
2710   SMESH_CATCH( SMESH::throwCorbaException );
2711   return 0;
2712 }
2713
2714 //=======================================================================
2715 //function : AdvancedExtrusion
2716 //purpose  :
2717 //=======================================================================
2718
2719 SMESH::ListOfGroups*
2720 SMESH_MeshEditor_i::AdvancedExtrusion(const SMESH::long_array & theIDsOfElements,
2721                                       const SMESH::DirStruct &  theStepVector,
2722                                       CORBA::Long               theNbOfSteps,
2723                                       CORBA::Long               theExtrFlags,
2724                                       CORBA::Double             theSewTolerance,
2725                                       CORBA::Boolean            theMakeGroups)
2726   throw (SALOME::SALOME_Exception)
2727 {
2728   SMESH_TRY;
2729   initData();
2730
2731   TPythonDump aPythonDump; // it is here to prevent dump of getGroups()
2732
2733   ExtrusionParams params( theStepVector, theNbOfSteps, theMakeGroups,
2734                           theExtrFlags, theSewTolerance );
2735
2736   TIDSortedElemSet elemsNodes[2];
2737   arrayToSet( theIDsOfElements, getMeshDS(), elemsNodes[0] );
2738
2739   ::SMESH_MeshEditor::TTElemOfElemListMap aHistory;
2740   ::SMESH_MeshEditor::PGroupIDs groupIds =
2741       getEditor().ExtrusionSweep( elemsNodes, params, aHistory );
2742
2743   SMESH::ListOfGroups * aGroups = theMakeGroups ? getGroups( groupIds.get()) : 0;
2744
2745   declareMeshModified( /*isReComputeSafe=*/true ); // does not influence Compute()
2746
2747   if ( !myIsPreviewMode ) {
2748     dumpGroupsList(aPythonDump, aGroups);
2749     aPythonDump << this << ".AdvancedExtrusion( "
2750                 << theIDsOfElements << ", "
2751                 << theStepVector << ", "
2752                 << theNbOfSteps << ", "
2753                 << theExtrFlags << ", "
2754                 << theSewTolerance << ", "
2755                 << theMakeGroups << " )";
2756   }
2757   else
2758   {
2759     getPreviewMesh()->Remove( SMDSAbs_Volume );
2760   }
2761
2762   return aGroups ? aGroups : new SMESH::ListOfGroups;
2763
2764   SMESH_CATCH( SMESH::throwCorbaException );
2765   return 0;
2766 }
2767
2768 //================================================================================
2769 /*!
2770  * \brief Convert extrusion error to IDL enum
2771  */
2772 //================================================================================
2773
2774 namespace
2775 {
2776 #define RETCASE(enm) case ::SMESH_MeshEditor::enm: return SMESH::SMESH_MeshEditor::enm;
2777
2778   SMESH::SMESH_MeshEditor::Extrusion_Error convExtrError( ::SMESH_MeshEditor::Extrusion_Error e )
2779   {
2780     switch ( e ) {
2781       RETCASE( EXTR_OK );
2782       RETCASE( EXTR_NO_ELEMENTS );
2783       RETCASE( EXTR_PATH_NOT_EDGE );
2784       RETCASE( EXTR_BAD_PATH_SHAPE );
2785       RETCASE( EXTR_BAD_STARTING_NODE );
2786       RETCASE( EXTR_BAD_ANGLES_NUMBER );
2787       RETCASE( EXTR_CANT_GET_TANGENT );
2788     }
2789     return SMESH::SMESH_MeshEditor::EXTR_OK;
2790   }
2791 }
2792
2793 //=======================================================================
2794 //function : extrusionAlongPath
2795 //purpose  :
2796 //=======================================================================
2797 SMESH::ListOfGroups*
2798 SMESH_MeshEditor_i::ExtrusionAlongPathObjects(const SMESH::ListOfIDSources & theNodes,
2799                                               const SMESH::ListOfIDSources & theEdges,
2800                                               const SMESH::ListOfIDSources & theFaces,
2801                                               SMESH::SMESH_IDSource_ptr      thePathMesh,
2802                                               GEOM::GEOM_Object_ptr          thePathShape,
2803                                               CORBA::Long                    theNodeStart,
2804                                               CORBA::Boolean                 theHasAngles,
2805                                               const SMESH::double_array &    theAngles,
2806                                               CORBA::Boolean                 theLinearVariation,
2807                                               CORBA::Boolean                 theHasRefPoint,
2808                                               const SMESH::PointStruct &     theRefPoint,
2809                                               bool                           theMakeGroups,
2810                                               SMESH::SMESH_MeshEditor::Extrusion_Error& theError)
2811   throw (SALOME::SALOME_Exception)
2812 {
2813   SMESH_TRY;
2814   initData();
2815
2816   SMESH::ListOfGroups_var aGroups = new SMESH::ListOfGroups;
2817
2818   theError = SMESH::SMESH_MeshEditor::EXTR_BAD_PATH_SHAPE;
2819   if ( thePathMesh->_is_nil() )
2820     return aGroups._retn();
2821
2822   // get a sub-mesh
2823   SMESH_subMesh* aSubMesh = 0;
2824   SMESH_Mesh_i* aMeshImp = SMESH::DownCast<SMESH_Mesh_i*>( thePathMesh );
2825   if ( thePathShape->_is_nil() )
2826   {
2827     // thePathMesh should be either a sub-mesh or a mesh with 1D elements only
2828     if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( thePathMesh ))
2829     {
2830       SMESH::SMESH_Mesh_var mesh = thePathMesh->GetMesh();
2831       aMeshImp = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
2832       if ( !aMeshImp ) return aGroups._retn();
2833       aSubMesh = aMeshImp->GetImpl().GetSubMeshContaining( sm->GetId() );
2834       if ( !aSubMesh ) return aGroups._retn();
2835     }
2836     else if ( !aMeshImp ||
2837               aMeshImp->NbEdges() != aMeshImp->NbElements() )
2838     {
2839       return aGroups._retn();
2840     }
2841   }
2842   else
2843   {
2844     if ( !aMeshImp ) return aGroups._retn();
2845     TopoDS_Shape aShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( thePathShape );
2846     aSubMesh = aMeshImp->GetImpl().GetSubMesh( aShape );
2847     if ( !aSubMesh || !aSubMesh->GetSubMeshDS() )
2848       return aGroups._retn();
2849   }
2850
2851   SMDS_MeshNode* nodeStart =
2852     (SMDS_MeshNode*)aMeshImp->GetImpl().GetMeshDS()->FindNode(theNodeStart);
2853   if ( !nodeStart ) {
2854     theError = SMESH::SMESH_MeshEditor::EXTR_BAD_STARTING_NODE;
2855     return aGroups._retn();
2856   }
2857
2858   TIDSortedElemSet elemsNodes[2];
2859   for ( int i = 0, nb = theNodes.length(); i < nb; ++i ) {
2860     SMDS_ElemIteratorPtr nIt = myMesh_i->GetElements( theNodes[i], SMESH::NODE );
2861     while ( nIt->more() ) elemsNodes[1].insert( nIt->next() );
2862   }
2863   for ( int i = 0, nb = theEdges.length(); i < nb; ++i )
2864     idSourceToSet( theEdges[i], getMeshDS(), elemsNodes[0], SMDSAbs_Edge );
2865   for ( int i = 0, nb = theFaces.length(); i < nb; ++i )
2866     idSourceToSet( theFaces[i], getMeshDS(), elemsNodes[0], SMDSAbs_Face );
2867
2868   list<double> angles;
2869   for (int i = 0; i < theAngles.length(); i++) {
2870     angles.push_back( theAngles[i] );
2871   }
2872
2873   gp_Pnt refPnt( theRefPoint.x, theRefPoint.y, theRefPoint.z );
2874
2875   int nbOldGroups = myMesh->NbGroup();
2876
2877   TIDSortedElemSet* workElements = & elemsNodes[0], copyElements[2];
2878   if ( myIsPreviewMode )
2879   {
2880     SMDSAbs_ElementType select = SMDSAbs_All, avoid = SMDSAbs_Volume;
2881     TPreviewMesh * tmpMesh = getPreviewMesh();
2882     tmpMesh->Copy( elemsNodes[0], copyElements[0], select, avoid );
2883     tmpMesh->Copy( elemsNodes[1], copyElements[1], select, avoid );
2884     workElements = & copyElements[0];
2885     theMakeGroups = false;
2886   }
2887
2888   ::SMESH_MeshEditor::Extrusion_Error error;
2889   if ( !aSubMesh )
2890     error = getEditor().ExtrusionAlongTrack( workElements, &(aMeshImp->GetImpl()), nodeStart,
2891                                              theHasAngles, angles, theLinearVariation,
2892                                              theHasRefPoint, refPnt, theMakeGroups );
2893   else
2894     error = getEditor().ExtrusionAlongTrack( workElements, aSubMesh, nodeStart,
2895                                              theHasAngles, angles, theLinearVariation,
2896                                              theHasRefPoint, refPnt, theMakeGroups );
2897
2898   declareMeshModified( /*isReComputeSafe=*/true );
2899   theError = convExtrError( error );
2900
2901   TPythonDump aPythonDump; // it is here to prevent dump of getGroups()
2902   if ( theMakeGroups ) {
2903     list<int> groupIDs = myMesh->GetGroupIds();
2904     list<int>::iterator newBegin = groupIDs.begin();
2905     std::advance( newBegin, nbOldGroups ); // skip old groups
2906     groupIDs.erase( groupIDs.begin(), newBegin );
2907     aGroups = getGroups( & groupIDs );
2908     if ( ! &aGroups.in() ) aGroups = new SMESH::ListOfGroups;
2909   }
2910
2911   if ( !myIsPreviewMode ) {
2912     aPythonDump << "(" << aGroups << ", error) = "
2913                 << this << ".ExtrusionAlongPathObjects( "
2914                 << theNodes            << ", "
2915                 << theEdges            << ", "
2916                 << theFaces            << ", "
2917                 << thePathMesh         << ", "
2918                 << thePathShape        << ", "
2919                 << theNodeStart        << ", "
2920                 << theHasAngles        << ", "
2921                 << theAngles           << ", "
2922                 << theLinearVariation  << ", "
2923                 << theHasRefPoint      << ", "
2924                 << "SMESH.PointStruct( "
2925                 << ( theHasRefPoint ? theRefPoint.x : 0 ) << ", "
2926                 << ( theHasRefPoint ? theRefPoint.y : 0 ) << ", "
2927                 << ( theHasRefPoint ? theRefPoint.z : 0 ) << " ), "
2928                 << theMakeGroups       << " )";
2929   }
2930   else
2931   {
2932     getPreviewMesh()->Remove( SMDSAbs_Volume );
2933   }
2934
2935   return aGroups._retn();
2936
2937   SMESH_CATCH( SMESH::throwCorbaException );
2938   return 0;
2939 }
2940
2941 //================================================================================
2942 /*!
2943  * \brief Compute rotation angles for ExtrusionAlongPath as linear variation
2944  * of given angles along path steps
2945  * \param PathMesh mesh containing a 1D sub-mesh on the edge, along
2946  *                which proceeds the extrusion
2947  * \param PathShape is shape(edge); as the mesh can be complex, the edge
2948  *                 is used to define the sub-mesh for the path
2949  */
2950 //================================================================================
2951
2952 SMESH::double_array*
2953 SMESH_MeshEditor_i::LinearAnglesVariation(SMESH::SMESH_Mesh_ptr       thePathMesh,
2954                                           GEOM::GEOM_Object_ptr       thePathShape,
2955                                           const SMESH::double_array & theAngles)
2956 {
2957   SMESH::double_array_var aResult = new SMESH::double_array();
2958   int nbAngles = theAngles.length();
2959   if ( nbAngles > 0 && !thePathMesh->_is_nil() && !thePathShape->_is_nil() )
2960   {
2961     SMESH_Mesh_i* aMeshImp = SMESH::DownCast<SMESH_Mesh_i*>( thePathMesh );
2962     TopoDS_Shape aShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( thePathShape );
2963     SMESH_subMesh* aSubMesh = aMeshImp->GetImpl().GetSubMesh( aShape );
2964     if ( !aSubMesh || !aSubMesh->GetSubMeshDS())
2965       return aResult._retn();
2966     int nbSteps = aSubMesh->GetSubMeshDS()->NbElements();
2967     if ( nbSteps == nbAngles )
2968     {
2969       aResult.inout() = theAngles;
2970     }
2971     else
2972     {
2973       aResult->length( nbSteps );
2974       double rAn2St = double( nbAngles ) / double( nbSteps );
2975       double angPrev = 0, angle;
2976       for ( int iSt = 0; iSt < nbSteps; ++iSt )
2977       {
2978         double angCur = rAn2St * ( iSt+1 );
2979         double angCurFloor  = floor( angCur );
2980         double angPrevFloor = floor( angPrev );
2981         if ( angPrevFloor == angCurFloor )
2982           angle = rAn2St * theAngles[ int( angCurFloor ) ];
2983         else
2984         {
2985           int iP = int( angPrevFloor );
2986           double angPrevCeil = ceil(angPrev);
2987           angle = ( angPrevCeil - angPrev ) * theAngles[ iP ];
2988
2989           int iC = int( angCurFloor );
2990           if ( iC < nbAngles )
2991             angle += ( angCur - angCurFloor ) * theAngles[ iC ];
2992
2993           iP = int( angPrevCeil );
2994           while ( iC-- > iP )
2995             angle += theAngles[ iC ];
2996         }
2997         aResult[ iSt ] = angle;
2998         angPrev = angCur;
2999       }
3000     }
3001   }
3002   // Update Python script
3003   TPythonDump() << "rotAngles = " << theAngles;
3004   TPythonDump() << "rotAngles = " << this << ".LinearAnglesVariation( "
3005                 << thePathMesh  << ", "
3006                 << thePathShape << ", "
3007                 << "rotAngles )";
3008
3009   return aResult._retn();
3010 }
3011
3012 //=======================================================================
3013 //function : mirror
3014 //purpose  :
3015 //=======================================================================
3016
3017 SMESH::ListOfGroups*
3018 SMESH_MeshEditor_i::mirror(TIDSortedElemSet &                  theElements,
3019                            const SMESH::AxisStruct &           theAxis,
3020                            SMESH::SMESH_MeshEditor::MirrorType theMirrorType,
3021                            CORBA::Boolean                      theCopy,
3022                            bool                                theMakeGroups,
3023                            ::SMESH_Mesh*                       theTargetMesh)
3024   throw (SALOME::SALOME_Exception)
3025 {
3026   SMESH_TRY;
3027   initData();
3028
3029   gp_Pnt P ( theAxis.x, theAxis.y, theAxis.z );
3030   gp_Vec V ( theAxis.vx, theAxis.vy, theAxis.vz );
3031
3032   if ( theTargetMesh )
3033     theCopy = false;
3034
3035   gp_Trsf aTrsf;
3036   switch ( theMirrorType ) {
3037   case  SMESH::SMESH_MeshEditor::POINT:
3038     aTrsf.SetMirror( P );
3039     break;
3040   case  SMESH::SMESH_MeshEditor::AXIS:
3041     aTrsf.SetMirror( gp_Ax1( P, V ));
3042     break;
3043   default:
3044     aTrsf.SetMirror( gp_Ax2( P, V ));
3045   }
3046
3047   TIDSortedElemSet  copyElements;
3048   TIDSortedElemSet* workElements = & theElements;
3049
3050   if ( myIsPreviewMode )
3051   {
3052     TPreviewMesh * tmpMesh = getPreviewMesh();
3053     tmpMesh->Copy( theElements, copyElements);
3054     if ( !theCopy && !theTargetMesh )
3055     {
3056       TIDSortedElemSet elemsAround, elemsAroundCopy;
3057       getElementsAround( theElements, getMeshDS(), elemsAround );
3058       tmpMesh->Copy( elemsAround, elemsAroundCopy);
3059     }
3060     workElements = & copyElements;
3061     theMakeGroups = false;
3062   }
3063
3064   ::SMESH_MeshEditor::PGroupIDs groupIds =
3065       getEditor().Transform (*workElements, aTrsf, theCopy, theMakeGroups, theTargetMesh);
3066
3067   if ( theCopy && !myIsPreviewMode)
3068   {
3069     if ( theTargetMesh )
3070     {
3071       theTargetMesh->GetMeshDS()->Modified();
3072     }
3073     else
3074     {
3075       declareMeshModified( /*isReComputeSafe=*/false );
3076     }
3077   }
3078   return theMakeGroups ? getGroups(groupIds.get()) : 0;
3079
3080   SMESH_CATCH( SMESH::throwCorbaException );
3081   return 0;
3082 }
3083
3084 //=======================================================================
3085 //function : Mirror
3086 //purpose  :
3087 //=======================================================================
3088
3089 void SMESH_MeshEditor_i::Mirror(const SMESH::long_array &           theIDsOfElements,
3090                                 const SMESH::AxisStruct &           theAxis,
3091                                 SMESH::SMESH_MeshEditor::MirrorType theMirrorType,
3092                                 CORBA::Boolean                      theCopy)
3093   throw (SALOME::SALOME_Exception)
3094 {
3095   if ( !myIsPreviewMode ) {
3096     TPythonDump() << this << ".Mirror( "
3097                   << theIDsOfElements              << ", "
3098                   << theAxis                       << ", "
3099                   << mirrorTypeName(theMirrorType) << ", "
3100                   << theCopy                       << " )";
3101   }
3102   if ( theIDsOfElements.length() > 0 )
3103   {
3104     TIDSortedElemSet elements;
3105     arrayToSet(theIDsOfElements, getMeshDS(), elements);
3106     mirror(elements, theAxis, theMirrorType, theCopy, false);
3107   }
3108 }
3109
3110
3111 //=======================================================================
3112 //function : MirrorObject
3113 //purpose  :
3114 //=======================================================================
3115
3116 void SMESH_MeshEditor_i::MirrorObject(SMESH::SMESH_IDSource_ptr           theObject,
3117                                       const SMESH::AxisStruct &           theAxis,
3118                                       SMESH::SMESH_MeshEditor::MirrorType theMirrorType,
3119                                       CORBA::Boolean                      theCopy)
3120   throw (SALOME::SALOME_Exception)
3121 {
3122   if ( !myIsPreviewMode ) {
3123     TPythonDump() << this << ".MirrorObject( "
3124                   << theObject                     << ", "
3125                   << theAxis                       << ", "
3126                   << mirrorTypeName(theMirrorType) << ", "
3127                   << theCopy                       << " )";
3128   }
3129   TIDSortedElemSet elements;
3130
3131   bool emptyIfIsMesh = myIsPreviewMode ? false : true;
3132
3133   if (idSourceToSet(theObject, getMeshDS(), elements, SMDSAbs_All, emptyIfIsMesh))
3134     mirror(elements, theAxis, theMirrorType, theCopy, false);
3135 }
3136
3137 //=======================================================================
3138 //function : MirrorMakeGroups
3139 //purpose  :
3140 //=======================================================================
3141
3142 SMESH::ListOfGroups*
3143 SMESH_MeshEditor_i::MirrorMakeGroups(const SMESH::long_array&            theIDsOfElements,
3144                                      const SMESH::AxisStruct&            theMirror,
3145                                      SMESH::SMESH_MeshEditor::MirrorType theMirrorType)
3146   throw (SALOME::SALOME_Exception)
3147 {
3148   TPythonDump aPythonDump; // it is here to prevent dump of GetGroups()
3149
3150   SMESH::ListOfGroups * aGroups = 0;
3151   if ( theIDsOfElements.length() > 0 )
3152   {
3153     TIDSortedElemSet elements;
3154     arrayToSet(theIDsOfElements, getMeshDS(), elements);
3155     aGroups = mirror(elements, theMirror, theMirrorType, true, true);
3156   }
3157   if (!myIsPreviewMode) {
3158     dumpGroupsList(aPythonDump, aGroups);
3159     aPythonDump << this << ".MirrorMakeGroups( "
3160                 << theIDsOfElements              << ", "
3161                 << theMirror                     << ", "
3162                 << mirrorTypeName(theMirrorType) << " )";
3163   }
3164   return aGroups;
3165 }
3166
3167 //=======================================================================
3168 //function : MirrorObjectMakeGroups
3169 //purpose  :
3170 //=======================================================================
3171
3172 SMESH::ListOfGroups*
3173 SMESH_MeshEditor_i::MirrorObjectMakeGroups(SMESH::SMESH_IDSource_ptr           theObject,
3174                                            const SMESH::AxisStruct&            theMirror,
3175                                            SMESH::SMESH_MeshEditor::MirrorType theMirrorType)
3176   throw (SALOME::SALOME_Exception)
3177 {
3178   TPythonDump aPythonDump; // it is here to prevent dump of GetGroups()
3179
3180   SMESH::ListOfGroups * aGroups = 0;
3181   TIDSortedElemSet elements;
3182   if ( idSourceToSet(theObject, getMeshDS(), elements, SMDSAbs_All, /*emptyIfIsMesh=*/1))
3183     aGroups = mirror(elements, theMirror, theMirrorType, true, true);
3184
3185   if (!myIsPreviewMode)
3186   {
3187     dumpGroupsList(aPythonDump,aGroups);
3188     aPythonDump << this << ".MirrorObjectMakeGroups( "
3189                 << theObject                     << ", "
3190                 << theMirror                     << ", "
3191                 << mirrorTypeName(theMirrorType) << " )";
3192   }
3193   return aGroups;
3194 }
3195
3196 //=======================================================================
3197 //function : MirrorMakeMesh
3198 //purpose  :
3199 //=======================================================================
3200
3201 SMESH::SMESH_Mesh_ptr
3202 SMESH_MeshEditor_i::MirrorMakeMesh(const SMESH::long_array&            theIDsOfElements,
3203                                    const SMESH::AxisStruct&            theMirror,
3204                                    SMESH::SMESH_MeshEditor::MirrorType theMirrorType,
3205                                    CORBA::Boolean                      theCopyGroups,
3206                                    const char*                         theMeshName)
3207   throw (SALOME::SALOME_Exception)
3208 {
3209   SMESH_Mesh_i* mesh_i;
3210   SMESH::SMESH_Mesh_var mesh;
3211   { // open new scope to dump "MakeMesh" command
3212     // and then "GetGroups" using SMESH_Mesh::GetGroups()
3213
3214     TPythonDump pydump; // to prevent dump at mesh creation
3215
3216     mesh = makeMesh( theMeshName );
3217     mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
3218     if (mesh_i && theIDsOfElements.length() > 0 )
3219     {
3220       TIDSortedElemSet elements;
3221       arrayToSet(theIDsOfElements, getMeshDS(), elements);
3222       mirror(elements, theMirror, theMirrorType,
3223              false, theCopyGroups, & mesh_i->GetImpl());
3224       mesh_i->CreateGroupServants();
3225     }
3226
3227     if (!myIsPreviewMode) {
3228       pydump << mesh << " = " << this << ".MirrorMakeMesh( "
3229              << theIDsOfElements              << ", "
3230              << theMirror                     << ", "
3231              << mirrorTypeName(theMirrorType) << ", "
3232              << theCopyGroups                 << ", '"
3233              << theMeshName                   << "' )";
3234     }
3235   }
3236
3237   //dump "GetGroups"
3238   if (!myIsPreviewMode && mesh_i)
3239     mesh_i->GetGroups();
3240
3241   return mesh._retn();
3242 }
3243
3244 //=======================================================================
3245 //function : MirrorObjectMakeMesh
3246 //purpose  :
3247 //=======================================================================
3248
3249 SMESH::SMESH_Mesh_ptr
3250 SMESH_MeshEditor_i::MirrorObjectMakeMesh(SMESH::SMESH_IDSource_ptr           theObject,
3251                                          const SMESH::AxisStruct&            theMirror,
3252                                          SMESH::SMESH_MeshEditor::MirrorType theMirrorType,
3253                                          CORBA::Boolean                      theCopyGroups,
3254                                          const char*                         theMeshName)
3255   throw (SALOME::SALOME_Exception)
3256 {
3257   SMESH_Mesh_i* mesh_i;
3258   SMESH::SMESH_Mesh_var mesh;
3259   { // open new scope to dump "MakeMesh" command
3260     // and then "GetGroups" using SMESH_Mesh::GetGroups()
3261
3262     TPythonDump pydump; // to prevent dump at mesh creation
3263
3264     mesh = makeMesh( theMeshName );
3265     mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
3266     TIDSortedElemSet elements;
3267     if ( mesh_i &&
3268          idSourceToSet(theObject, getMeshDS(), elements, SMDSAbs_All, /*emptyIfIsMesh=*/1))
3269     {
3270       mirror(elements, theMirror, theMirrorType,
3271              false, theCopyGroups, & mesh_i->GetImpl());
3272       mesh_i->CreateGroupServants();
3273     }
3274     if (!myIsPreviewMode) {
3275       pydump << mesh << " = " << this << ".MirrorObjectMakeMesh( "
3276              << theObject                     << ", "
3277              << theMirror                     << ", "
3278              << mirrorTypeName(theMirrorType) << ", "
3279              << theCopyGroups                 << ", '"
3280              << theMeshName                   << "' )";
3281     }
3282   }
3283
3284   //dump "GetGroups"
3285   if (!myIsPreviewMode && mesh_i)
3286     mesh_i->GetGroups();
3287
3288   return mesh._retn();
3289 }
3290
3291 //=======================================================================
3292 //function : translate
3293 //purpose  :
3294 //=======================================================================
3295
3296 SMESH::ListOfGroups*
3297 SMESH_MeshEditor_i::translate(TIDSortedElemSet        & theElements,
3298                               const SMESH::DirStruct &  theVector,
3299                               CORBA::Boolean            theCopy,
3300                               bool                      theMakeGroups,
3301                               ::SMESH_Mesh*             theTargetMesh)
3302   throw (SALOME::SALOME_Exception)
3303 {
3304   SMESH_TRY;
3305   initData();
3306
3307   if ( theTargetMesh )
3308     theCopy = false;
3309
3310   gp_Trsf aTrsf;
3311   const SMESH::PointStruct * P = &theVector.PS;
3312   aTrsf.SetTranslation( gp_Vec( P->x, P->y, P->z ));
3313
3314   TIDSortedElemSet  copyElements;
3315   TIDSortedElemSet* workElements = &theElements;
3316
3317   if ( myIsPreviewMode )
3318   {
3319     TPreviewMesh * tmpMesh = getPreviewMesh();
3320     tmpMesh->Copy( theElements, copyElements);
3321     if ( !theCopy && !theTargetMesh )
3322     {
3323       TIDSortedElemSet elemsAround, elemsAroundCopy;
3324       getElementsAround( theElements, getMeshDS(), elemsAround );
3325       tmpMesh->Copy( elemsAround, elemsAroundCopy);
3326     }
3327     workElements = & copyElements;
3328     theMakeGroups = false;
3329   }
3330
3331   ::SMESH_MeshEditor::PGroupIDs groupIds =
3332       getEditor().Transform (*workElements, aTrsf, theCopy, theMakeGroups, theTargetMesh);
3333
3334   if ( theCopy && !myIsPreviewMode )
3335   {
3336     if ( theTargetMesh )
3337     {
3338       theTargetMesh->GetMeshDS()->Modified();
3339     }
3340     else
3341     {
3342       declareMeshModified( /*isReComputeSafe=*/false );
3343     }
3344   }
3345
3346   return theMakeGroups ? getGroups(groupIds.get()) : 0;
3347
3348   SMESH_CATCH( SMESH::throwCorbaException );
3349   return 0;
3350 }
3351
3352 //=======================================================================
3353 //function : Translate
3354 //purpose  :
3355 //=======================================================================
3356
3357 void SMESH_MeshEditor_i::Translate(const SMESH::long_array & theIDsOfElements,
3358                                    const SMESH::DirStruct &  theVector,
3359                                    CORBA::Boolean            theCopy)
3360   throw (SALOME::SALOME_Exception)
3361 {
3362   if (!myIsPreviewMode) {
3363     TPythonDump() << this << ".Translate( "
3364                   << theIDsOfElements << ", "
3365                   << theVector        << ", "
3366                   << theCopy          << " )";
3367   }
3368   if (theIDsOfElements.length()) {
3369     TIDSortedElemSet elements;
3370     arrayToSet(theIDsOfElements, getMeshDS(), elements);
3371     translate(elements, theVector, theCopy, false);
3372   }
3373 }
3374
3375 //=======================================================================
3376 //function : TranslateObject
3377 //purpose  :
3378 //=======================================================================
3379
3380 void SMESH_MeshEditor_i::TranslateObject(SMESH::SMESH_IDSource_ptr theObject,
3381                                          const SMESH::DirStruct &  theVector,
3382                                          CORBA::Boolean            theCopy)
3383   throw (SALOME::SALOME_Exception)
3384 {
3385   if (!myIsPreviewMode) {
3386     TPythonDump() << this << ".TranslateObject( "
3387                   << theObject << ", "
3388                   << theVector << ", "
3389                   << theCopy   << " )";
3390   }
3391   TIDSortedElemSet elements;
3392
3393   bool emptyIfIsMesh = myIsPreviewMode ? false : true;
3394
3395   if (idSourceToSet(theObject, getMeshDS(), elements, SMDSAbs_All, emptyIfIsMesh))
3396     translate(elements, theVector, theCopy, false);
3397 }
3398
3399 //=======================================================================
3400 //function : TranslateMakeGroups
3401 //purpose  :
3402 //=======================================================================
3403
3404 SMESH::ListOfGroups*
3405 SMESH_MeshEditor_i::TranslateMakeGroups(const SMESH::long_array& theIDsOfElements,
3406                                         const SMESH::DirStruct&  theVector)
3407   throw (SALOME::SALOME_Exception)
3408 {
3409   TPythonDump aPythonDump; // it is here to prevent dump of GetGroups()
3410
3411   SMESH::ListOfGroups * aGroups = 0;
3412   if (theIDsOfElements.length()) {
3413     TIDSortedElemSet elements;
3414     arrayToSet(theIDsOfElements, getMeshDS(), elements);
3415     aGroups = translate(elements,theVector,true,true);
3416   }
3417   if (!myIsPreviewMode) {
3418     dumpGroupsList(aPythonDump, aGroups);
3419     aPythonDump << this << ".TranslateMakeGroups( "
3420                 << theIDsOfElements << ", "
3421                 << theVector        << " )";
3422   }
3423   return aGroups;
3424 }
3425
3426 //=======================================================================
3427 //function : TranslateObjectMakeGroups
3428 //purpose  :
3429 //=======================================================================
3430
3431 SMESH::ListOfGroups*
3432 SMESH_MeshEditor_i::TranslateObjectMakeGroups(SMESH::SMESH_IDSource_ptr theObject,
3433                                               const SMESH::DirStruct&   theVector)
3434   throw (SALOME::SALOME_Exception)
3435 {
3436   TPythonDump aPythonDump; // it is here to prevent dump of GetGroups()
3437
3438   SMESH::ListOfGroups * aGroups = 0;
3439   TIDSortedElemSet elements;
3440   if (idSourceToSet(theObject, getMeshDS(), elements, SMDSAbs_All, /*emptyIfIsMesh=*/1))
3441     aGroups = translate(elements, theVector, true, true);
3442
3443   if (!myIsPreviewMode) {
3444     dumpGroupsList(aPythonDump, aGroups);
3445     aPythonDump << this << ".TranslateObjectMakeGroups( "
3446                 << theObject << ", "
3447                 << theVector << " )";
3448   }
3449   return aGroups;
3450 }
3451
3452 //=======================================================================
3453 //function : TranslateMakeMesh
3454 //purpose  :
3455 //=======================================================================
3456
3457 SMESH::SMESH_Mesh_ptr
3458 SMESH_MeshEditor_i::TranslateMakeMesh(const SMESH::long_array& theIDsOfElements,
3459                                       const SMESH::DirStruct&  theVector,
3460                                       CORBA::Boolean           theCopyGroups,
3461                                       const char*              theMeshName)
3462   throw (SALOME::SALOME_Exception)
3463 {
3464   SMESH_Mesh_i* mesh_i;
3465   SMESH::SMESH_Mesh_var mesh;
3466
3467   { // open new scope to dump "MakeMesh" command
3468     // and then "GetGroups" using SMESH_Mesh::GetGroups()
3469
3470     TPythonDump pydump; // to prevent dump at mesh creation
3471
3472     mesh = makeMesh( theMeshName );
3473     mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
3474
3475     if ( mesh_i && theIDsOfElements.length() )
3476     {
3477       TIDSortedElemSet elements;
3478       arrayToSet(theIDsOfElements, getMeshDS(), elements);
3479       translate(elements, theVector, false, theCopyGroups, & mesh_i->GetImpl());
3480       mesh_i->CreateGroupServants();
3481     }
3482
3483     if ( !myIsPreviewMode ) {
3484       pydump << mesh << " = " << this << ".TranslateMakeMesh( "
3485              << theIDsOfElements << ", "
3486              << theVector        << ", "
3487              << theCopyGroups    << ", '"
3488              << theMeshName      << "' )";
3489     }
3490   }
3491
3492   //dump "GetGroups"
3493   if (!myIsPreviewMode && mesh_i)
3494     mesh_i->GetGroups();
3495
3496   return mesh._retn();
3497 }
3498
3499 //=======================================================================
3500 //function : TranslateObjectMakeMesh
3501 //purpose  :
3502 //=======================================================================
3503
3504 SMESH::SMESH_Mesh_ptr
3505 SMESH_MeshEditor_i::TranslateObjectMakeMesh(SMESH::SMESH_IDSource_ptr theObject,
3506                                             const SMESH::DirStruct&   theVector,
3507                                             CORBA::Boolean            theCopyGroups,
3508                                             const char*               theMeshName)
3509   throw (SALOME::SALOME_Exception)
3510 {
3511   SMESH_TRY;
3512   SMESH_Mesh_i* mesh_i;
3513   SMESH::SMESH_Mesh_var mesh;
3514   { // open new scope to dump "MakeMesh" command
3515     // and then "GetGroups" using SMESH_Mesh::GetGroups()
3516
3517     TPythonDump pydump; // to prevent dump at mesh creation
3518     mesh = makeMesh( theMeshName );
3519     mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
3520
3521     TIDSortedElemSet elements;
3522     if ( mesh_i &&
3523          idSourceToSet(theObject, getMeshDS(), elements, SMDSAbs_All, /*emptyIfIsMesh=*/1))
3524     {
3525       translate(elements, theVector,false, theCopyGroups, & mesh_i->GetImpl());
3526       mesh_i->CreateGroupServants();
3527     }
3528     if ( !myIsPreviewMode ) {
3529       pydump << mesh << " = " << this << ".TranslateObjectMakeMesh( "
3530              << theObject     << ", "
3531              << theVector     << ", "
3532              << theCopyGroups << ", '"
3533              << theMeshName   << "' )";
3534     }
3535   }
3536
3537   // dump "GetGroups"
3538   if (!myIsPreviewMode && mesh_i)
3539     mesh_i->GetGroups();
3540
3541   return mesh._retn();
3542
3543   SMESH_CATCH( SMESH::throwCorbaException );
3544   return 0;
3545 }
3546
3547 //=======================================================================
3548 //function : rotate
3549 //purpose  :
3550 //=======================================================================
3551
3552 SMESH::ListOfGroups*
3553 SMESH_MeshEditor_i::rotate(TIDSortedElemSet &        theElements,
3554                            const SMESH::AxisStruct & theAxis,
3555                            CORBA::Double             theAngle,
3556                            CORBA::Boolean            theCopy,
3557                            bool                      theMakeGroups,
3558                            ::SMESH_Mesh*             theTargetMesh)
3559   throw (SALOME::SALOME_Exception)
3560 {
3561   SMESH_TRY;
3562   initData();
3563
3564   if ( theTargetMesh )
3565     theCopy = false;
3566
3567   gp_Pnt P ( theAxis.x, theAxis.y, theAxis.z );
3568   gp_Vec V ( theAxis.vx, theAxis.vy, theAxis.vz );
3569
3570   gp_Trsf aTrsf;
3571   aTrsf.SetRotation( gp_Ax1( P, V ), theAngle);
3572
3573   TIDSortedElemSet  copyElements;
3574   TIDSortedElemSet* workElements = &theElements;
3575   if ( myIsPreviewMode ) {
3576     TPreviewMesh * tmpMesh = getPreviewMesh();
3577     tmpMesh->Copy( theElements, copyElements );
3578     if ( !theCopy && !theTargetMesh )
3579     {
3580       TIDSortedElemSet elemsAround, elemsAroundCopy;
3581       getElementsAround( theElements, getMeshDS(), elemsAround );
3582       tmpMesh->Copy( elemsAround, elemsAroundCopy);
3583     }
3584     workElements = &copyElements;
3585     theMakeGroups = false;
3586   }
3587
3588   ::SMESH_MeshEditor::PGroupIDs groupIds =
3589       getEditor().Transform (*workElements, aTrsf, theCopy, theMakeGroups, theTargetMesh);
3590
3591   if ( theCopy && !myIsPreviewMode)
3592   {
3593     if ( theTargetMesh ) theTargetMesh->GetMeshDS()->Modified();
3594     else                 declareMeshModified( /*isReComputeSafe=*/false );
3595   }
3596
3597   return theMakeGroups ? getGroups(groupIds.get()) : 0;
3598
3599   SMESH_CATCH( SMESH::throwCorbaException );
3600   return 0;
3601 }
3602
3603 //=======================================================================
3604 //function : Rotate
3605 //purpose  :
3606 //=======================================================================
3607
3608 void SMESH_MeshEditor_i::Rotate(const SMESH::long_array & theIDsOfElements,
3609                                 const SMESH::AxisStruct & theAxis,
3610                                 CORBA::Double             theAngle,
3611                                 CORBA::Boolean            theCopy)
3612   throw (SALOME::SALOME_Exception)
3613 {
3614   if (!myIsPreviewMode) {
3615     TPythonDump() << this << ".Rotate( "
3616                   << theIDsOfElements << ", "
3617                   << theAxis          << ", "
3618                   << TVar( theAngle ) << ", "
3619                   << theCopy          << " )";
3620   }
3621   if (theIDsOfElements.length() > 0)
3622   {
3623     TIDSortedElemSet elements;
3624     arrayToSet(theIDsOfElements, getMeshDS(), elements);
3625     rotate(elements,theAxis,theAngle,theCopy,false);
3626   }
3627 }
3628
3629 //=======================================================================
3630 //function : RotateObject
3631 //purpose  :
3632 //=======================================================================
3633
3634 void SMESH_MeshEditor_i::RotateObject(SMESH::SMESH_IDSource_ptr theObject,
3635                                       const SMESH::AxisStruct & theAxis,
3636                                       CORBA::Double             theAngle,
3637                                       CORBA::Boolean            theCopy)
3638   throw (SALOME::SALOME_Exception)
3639 {
3640   if ( !myIsPreviewMode ) {
3641     TPythonDump() << this << ".RotateObject( "
3642                   << theObject        << ", "
3643                   << theAxis          << ", "
3644                   << TVar( theAngle ) << ", "
3645                   << theCopy          << " )";
3646   }
3647   TIDSortedElemSet elements;
3648   bool emptyIfIsMesh = myIsPreviewMode ? false : true;
3649   if (idSourceToSet(theObject, getMeshDS(), elements, SMDSAbs_All, emptyIfIsMesh))
3650     rotate(elements,theAxis,theAngle,theCopy,false);
3651 }
3652
3653 //=======================================================================
3654 //function : RotateMakeGroups
3655 //purpose  :
3656 //=======================================================================
3657
3658 SMESH::ListOfGroups*
3659 SMESH_MeshEditor_i::RotateMakeGroups(const SMESH::long_array& theIDsOfElements,
3660                                      const SMESH::AxisStruct& theAxis,
3661                                      CORBA::Double            theAngle)
3662   throw (SALOME::SALOME_Exception)
3663 {
3664   TPythonDump aPythonDump; // it is here to prevent dump of GetGroups()
3665
3666   SMESH::ListOfGroups * aGroups = 0;
3667   if (theIDsOfElements.length() > 0)
3668   {
3669     TIDSortedElemSet elements;
3670     arrayToSet(theIDsOfElements, getMeshDS(), elements);
3671     aGroups = rotate(elements,theAxis,theAngle,true,true);
3672   }
3673   if (!myIsPreviewMode) {
3674     dumpGroupsList(aPythonDump, aGroups);
3675     aPythonDump << this << ".RotateMakeGroups( "
3676                 << theIDsOfElements << ", "
3677                 << theAxis          << ", "
3678                 << TVar( theAngle ) << " )";
3679   }
3680   return aGroups;
3681 }
3682
3683 //=======================================================================
3684 //function : RotateObjectMakeGroups
3685 //purpose  :
3686 //=======================================================================
3687
3688 SMESH::ListOfGroups*
3689 SMESH_MeshEditor_i::RotateObjectMakeGroups(SMESH::SMESH_IDSource_ptr theObject,
3690                                            const SMESH::AxisStruct&  theAxis,
3691                                            CORBA::Double             theAngle)
3692   throw (SALOME::SALOME_Exception)
3693 {
3694   TPythonDump aPythonDump; // it is here to prevent dump of GetGroups()
3695
3696   SMESH::ListOfGroups * aGroups = 0;
3697   TIDSortedElemSet elements;
3698   if (idSourceToSet(theObject, getMeshDS(), elements, SMDSAbs_All, /*emptyIfIsMesh=*/1))
3699     aGroups = rotate(elements, theAxis, theAngle, true, true);
3700
3701   if (!myIsPreviewMode) {
3702     dumpGroupsList(aPythonDump, aGroups);
3703     aPythonDump << this << ".RotateObjectMakeGroups( "
3704                 << theObject        << ", "
3705                 << theAxis          << ", "
3706                 << TVar( theAngle ) << " )";
3707   }
3708   return aGroups;
3709 }
3710
3711 //=======================================================================
3712 //function : RotateMakeMesh
3713 //purpose  :
3714 //=======================================================================
3715
3716 SMESH::SMESH_Mesh_ptr
3717 SMESH_MeshEditor_i::RotateMakeMesh(const SMESH::long_array& theIDsOfElements,
3718                                    const SMESH::AxisStruct& theAxis,
3719                                    CORBA::Double            theAngleInRadians,
3720                                    CORBA::Boolean           theCopyGroups,
3721                                    const char*              theMeshName)
3722   throw (SALOME::SALOME_Exception)
3723 {
3724   SMESH_TRY;
3725   SMESH::SMESH_Mesh_var mesh;
3726   SMESH_Mesh_i* mesh_i;
3727
3728   { // open new scope to dump "MakeMesh" command
3729     // and then "GetGroups" using SMESH_Mesh::GetGroups()
3730
3731     TPythonDump pydump; // to prevent dump at mesh creation
3732
3733     mesh = makeMesh( theMeshName );
3734     mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
3735
3736     if ( mesh_i && theIDsOfElements.length() > 0 )
3737     {
3738       TIDSortedElemSet elements;
3739       arrayToSet(theIDsOfElements, getMeshDS(), elements);
3740       rotate(elements, theAxis, theAngleInRadians,
3741              false, theCopyGroups, & mesh_i->GetImpl());
3742       mesh_i->CreateGroupServants();
3743     }
3744     if ( !myIsPreviewMode ) {
3745       pydump << mesh << " = " << this << ".RotateMakeMesh( "
3746              << theIDsOfElements          << ", "
3747              << theAxis                   << ", "
3748              << TVar( theAngleInRadians ) << ", "
3749              << theCopyGroups             << ", '"
3750              << theMeshName               << "' )";
3751     }
3752   }
3753
3754   // dump "GetGroups"
3755   if (!myIsPreviewMode && mesh_i && theIDsOfElements.length() > 0 )
3756     mesh_i->GetGroups();
3757
3758   return mesh._retn();
3759
3760   SMESH_CATCH( SMESH::throwCorbaException );
3761   return 0;
3762 }
3763
3764 //=======================================================================
3765 //function : RotateObjectMakeMesh
3766 //purpose  :
3767 //=======================================================================
3768
3769 SMESH::SMESH_Mesh_ptr
3770 SMESH_MeshEditor_i::RotateObjectMakeMesh(SMESH::SMESH_IDSource_ptr theObject,
3771                                          const SMESH::AxisStruct&  theAxis,
3772                                          CORBA::Double             theAngleInRadians,
3773                                          CORBA::Boolean            theCopyGroups,
3774                                          const char*               theMeshName)
3775   throw (SALOME::SALOME_Exception)
3776 {
3777   SMESH_TRY;
3778   SMESH::SMESH_Mesh_var mesh;
3779   SMESH_Mesh_i* mesh_i;
3780
3781   {// open new scope to dump "MakeMesh" command
3782    // and then "GetGroups" using SMESH_Mesh::GetGroups()
3783
3784     TPythonDump pydump; // to prevent dump at mesh creation
3785     mesh = makeMesh( theMeshName );
3786     mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
3787
3788     TIDSortedElemSet elements;
3789     if (mesh_i &&
3790         idSourceToSet(theObject, getMeshDS(), elements, SMDSAbs_All, /*emptyIfIsMesh=*/1))
3791     {
3792       rotate(elements, theAxis, theAngleInRadians,
3793              false, theCopyGroups, & mesh_i->GetImpl());
3794       mesh_i->CreateGroupServants();
3795     }
3796     if ( !myIsPreviewMode ) {
3797       pydump << mesh << " = " << this << ".RotateObjectMakeMesh( "
3798              << theObject                 << ", "
3799              << theAxis                   << ", "
3800              << TVar( theAngleInRadians ) << ", "
3801              << theCopyGroups             << ", '"
3802              << theMeshName               << "' )";
3803     }
3804   }
3805
3806   // dump "GetGroups"
3807   if (!myIsPreviewMode && mesh_i)
3808     mesh_i->GetGroups();
3809
3810   return mesh._retn();
3811
3812   SMESH_CATCH( SMESH::throwCorbaException );
3813   return 0;
3814 }
3815
3816 //=======================================================================
3817 //function : scale
3818 //purpose  :
3819 //=======================================================================
3820
3821 SMESH::ListOfGroups*
3822 SMESH_MeshEditor_i::scale(SMESH::SMESH_IDSource_ptr  theObject,
3823                           const SMESH::PointStruct&  thePoint,
3824                           const SMESH::double_array& theScaleFact,
3825                           CORBA::Boolean             theCopy,
3826                           bool                       theMakeGroups,
3827                           ::SMESH_Mesh*              theTargetMesh)
3828   throw (SALOME::SALOME_Exception)
3829 {
3830   SMESH_TRY;
3831   initData();
3832   if ( theScaleFact.length() < 1 )
3833     THROW_SALOME_CORBA_EXCEPTION("Scale factor not given", SALOME::BAD_PARAM);
3834   if ( theScaleFact.length() == 2 )
3835     THROW_SALOME_CORBA_EXCEPTION("Invalid nb of scale factors : 2", SALOME::BAD_PARAM);
3836
3837   if ( theTargetMesh )
3838     theCopy = false;
3839
3840   TIDSortedElemSet elements;
3841   bool emptyIfIsMesh = myIsPreviewMode ? false : true;
3842   if ( !idSourceToSet(theObject, getMeshDS(), elements, SMDSAbs_All, emptyIfIsMesh))
3843     return 0;
3844
3845   double S[3] = {
3846     theScaleFact[0],
3847     (theScaleFact.length() == 1) ? theScaleFact[0] : theScaleFact[1],
3848     (theScaleFact.length() == 1) ? theScaleFact[0] : theScaleFact[2],
3849   };
3850   gp_Trsf aTrsf;
3851
3852 #if OCC_VERSION_LARGE > 0x06070100
3853   // fight against orthogonalization
3854   // aTrsf.SetValues( S[0], 0,    0,    thePoint.x * (1-S[0]),
3855   //                  0,    S[1], 0,    thePoint.y * (1-S[1]),
3856   //                  0,    0,    S[2], thePoint.z * (1-S[2]) );
3857   aTrsf.SetScale( gp::Origin(), 1.0 ); // set form which is used to make group names
3858   gp_XYZ & loc = ( gp_XYZ& ) aTrsf.TranslationPart();
3859   gp_Mat & M   = ( gp_Mat& ) aTrsf.HVectorialPart();
3860   loc.SetCoord( thePoint.x * (1-S[0]),
3861                 thePoint.y * (1-S[1]),
3862                 thePoint.z * (1-S[2]));
3863   M.SetDiagonal( S[0], S[1], S[2] );
3864
3865 #else
3866   double tol = std::numeric_limits<double>::max();
3867   aTrsf.SetValues( S[0], 0,    0,    thePoint.x * (1-S[0]),
3868                    0,    S[1], 0,    thePoint.y * (1-S[1]),
3869                    0,    0,    S[2], thePoint.z * (1-S[2]),   tol, tol);
3870 #endif
3871
3872   TIDSortedElemSet  copyElements;
3873   TIDSortedElemSet* workElements = &elements;
3874   if ( myIsPreviewMode )
3875   {
3876     TPreviewMesh * tmpMesh = getPreviewMesh();
3877     tmpMesh->Copy( elements, copyElements);
3878     if ( !theCopy && !theTargetMesh )
3879     {
3880       TIDSortedElemSet elemsAround, elemsAroundCopy;
3881       getElementsAround( elements, getMeshDS(), elemsAround );
3882       tmpMesh->Copy( elemsAround, elemsAroundCopy);
3883     }
3884     workElements = & copyElements;
3885     theMakeGroups = false;
3886   }
3887
3888   ::SMESH_MeshEditor::PGroupIDs groupIds =
3889       getEditor().Transform (*workElements, aTrsf, theCopy, theMakeGroups, theTargetMesh);
3890
3891   if ( theCopy && !myIsPreviewMode )
3892   {
3893     if ( theTargetMesh ) theTargetMesh->GetMeshDS()->Modified();
3894     else                 declareMeshModified( /*isReComputeSafe=*/false );
3895   }
3896   return theMakeGroups ? getGroups(groupIds.get()) : 0;
3897
3898   SMESH_CATCH( SMESH::throwCorbaException );
3899   return 0;
3900 }
3901
3902 //=======================================================================
3903 //function : Scale
3904 //purpose  :
3905 //=======================================================================
3906
3907 void SMESH_MeshEditor_i::Scale(SMESH::SMESH_IDSource_ptr  theObject,
3908                                const SMESH::PointStruct&  thePoint,
3909                                const SMESH::double_array& theScaleFact,
3910                                CORBA::Boolean             theCopy)
3911   throw (SALOME::SALOME_Exception)
3912 {
3913   if ( !myIsPreviewMode ) {
3914     TPythonDump() << this << ".Scale( "
3915                   << theObject            << ", "
3916                   << thePoint             << ", "
3917                   << TVar( theScaleFact ) << ", "
3918                   << theCopy              << " )";
3919   }
3920   scale(theObject, thePoint, theScaleFact, theCopy, false);
3921 }
3922
3923
3924 //=======================================================================
3925 //function : ScaleMakeGroups
3926 //purpose  :
3927 //=======================================================================
3928
3929 SMESH::ListOfGroups*
3930 SMESH_MeshEditor_i::ScaleMakeGroups(SMESH::SMESH_IDSource_ptr  theObject,
3931                                     const SMESH::PointStruct&  thePoint,
3932                                     const SMESH::double_array& theScaleFact)
3933   throw (SALOME::SALOME_Exception)
3934 {
3935   TPythonDump aPythonDump; // it is here to prevent dump of GetGroups()
3936
3937   SMESH::ListOfGroups * aGroups = scale(theObject, thePoint, theScaleFact, true, true);
3938   if (!myIsPreviewMode) {
3939     dumpGroupsList(aPythonDump, aGroups);
3940     aPythonDump << this << ".Scale("
3941                 << theObject            << ","
3942                 << thePoint             << ","
3943                 << TVar( theScaleFact ) << ",True,True)";
3944   }
3945   return aGroups;
3946 }
3947
3948
3949 //=======================================================================
3950 //function : ScaleMakeMesh
3951 //purpose  :
3952 //=======================================================================
3953
3954 SMESH::SMESH_Mesh_ptr
3955 SMESH_MeshEditor_i::ScaleMakeMesh(SMESH::SMESH_IDSource_ptr  theObject,
3956                                   const SMESH::PointStruct&  thePoint,
3957                                   const SMESH::double_array& theScaleFact,
3958                                   CORBA::Boolean             theCopyGroups,
3959                                   const char*                theMeshName)
3960   throw (SALOME::SALOME_Exception)
3961 {
3962   SMESH_Mesh_i* mesh_i;
3963   SMESH::SMESH_Mesh_var mesh;
3964   { // open new scope to dump "MakeMesh" command
3965     // and then "GetGroups" using SMESH_Mesh::GetGroups()
3966
3967     TPythonDump pydump; // to prevent dump at mesh creation
3968     mesh = makeMesh( theMeshName );
3969     mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
3970
3971     if ( mesh_i )
3972     {
3973       scale(theObject, thePoint, theScaleFact,false, theCopyGroups, & mesh_i->GetImpl());
3974       mesh_i->CreateGroupServants();
3975     }
3976     if ( !myIsPreviewMode )
3977       pydump << mesh << " = " << this << ".ScaleMakeMesh( "
3978              << theObject            << ", "
3979              << thePoint             << ", "
3980              << TVar( theScaleFact ) << ", "
3981              << theCopyGroups        << ", '"
3982              << theMeshName          << "' )";
3983   }
3984
3985   // dump "GetGroups"
3986   if (!myIsPreviewMode && mesh_i)
3987     mesh_i->GetGroups();
3988
3989   return mesh._retn();
3990 }
3991
3992
3993 //=======================================================================
3994 //function : findCoincidentNodes
3995 //purpose  :
3996 //=======================================================================
3997
3998 void SMESH_MeshEditor_i::
3999 findCoincidentNodes (TIDSortedNodeSet &             Nodes,
4000                      CORBA::Double                  Tolerance,
4001                      SMESH::array_of_long_array_out GroupsOfNodes,
4002                      CORBA::Boolean                 SeparateCornersAndMedium)
4003 {
4004   ::SMESH_MeshEditor::TListOfListOfNodes aListOfListOfNodes;
4005   getEditor().FindCoincidentNodes( Nodes, Tolerance, aListOfListOfNodes, SeparateCornersAndMedium );
4006
4007   GroupsOfNodes = new SMESH::array_of_long_array;
4008   GroupsOfNodes->length( aListOfListOfNodes.size() );
4009   ::SMESH_MeshEditor::TListOfListOfNodes::iterator llIt = aListOfListOfNodes.begin();
4010   for ( CORBA::Long i = 0; llIt != aListOfListOfNodes.end(); llIt++, i++ )
4011   {
4012     list< const SMDS_MeshNode* >& aListOfNodes = *llIt;
4013     list< const SMDS_MeshNode* >::iterator lIt = aListOfNodes.begin();;
4014     SMESH::long_array& aGroup = (*GroupsOfNodes)[ i ];
4015     aGroup.length( aListOfNodes.size() );
4016     for ( int j = 0; lIt != aListOfNodes.end(); lIt++, j++ )
4017       aGroup[ j ] = (*lIt)->GetID();
4018   }
4019 }
4020
4021 //=======================================================================
4022 //function : FindCoincidentNodes
4023 //purpose  :
4024 //=======================================================================
4025
4026 void SMESH_MeshEditor_i::
4027 FindCoincidentNodes (CORBA::Double                  Tolerance,
4028                      SMESH::array_of_long_array_out GroupsOfNodes,
4029                      CORBA::Boolean                 SeparateCornersAndMedium)
4030   throw (SALOME::SALOME_Exception)
4031 {
4032   SMESH_TRY;
4033   initData();
4034
4035   TIDSortedNodeSet nodes; // no input nodes
4036   findCoincidentNodes( nodes, Tolerance, GroupsOfNodes, SeparateCornersAndMedium );
4037
4038   TPythonDump() << "coincident_nodes = " << this << ".FindCoincidentNodes( "
4039                 << Tolerance << ", "
4040                 << SeparateCornersAndMedium << " )";
4041
4042   SMESH_CATCH( SMESH::throwCorbaException );
4043 }
4044
4045 //=======================================================================
4046 //function : FindCoincidentNodesOnPart
4047 //purpose  :
4048 //=======================================================================
4049
4050 void SMESH_MeshEditor_i::
4051 FindCoincidentNodesOnPart(SMESH::SMESH_IDSource_ptr      theObject,
4052                           CORBA::Double                  Tolerance,
4053                           SMESH::array_of_long_array_out GroupsOfNodes,
4054                           CORBA::Boolean                 SeparateCornersAndMedium)
4055   throw (SALOME::SALOME_Exception)
4056 {
4057   SMESH_TRY;
4058   initData();
4059
4060   TIDSortedNodeSet nodes;
4061   idSourceToNodeSet( theObject, getMeshDS(), nodes );
4062
4063   findCoincidentNodes( nodes, Tolerance, GroupsOfNodes, SeparateCornersAndMedium );
4064
4065   TPythonDump() << "coincident_nodes_on_part = " << this << ".FindCoincidentNodesOnPart( "
4066                 << theObject <<", "
4067                 << Tolerance << ", "
4068                 << SeparateCornersAndMedium << " )";
4069
4070   SMESH_CATCH( SMESH::throwCorbaException );
4071 }
4072
4073 //================================================================================
4074 /*!
4075  * \brief Finds nodes coinsident with Tolerance within Object excluding nodes within
4076  *        ExceptSubMeshOrGroups
4077  */
4078 //================================================================================
4079
4080 void SMESH_MeshEditor_i::
4081 FindCoincidentNodesOnPartBut(SMESH::SMESH_IDSource_ptr      theObject,
4082                              CORBA::Double                  theTolerance,
4083                              SMESH::array_of_long_array_out theGroupsOfNodes,
4084                              const SMESH::ListOfIDSources&  theExceptSubMeshOrGroups,
4085                              CORBA::Boolean                 theSeparateCornersAndMedium)
4086   throw (SALOME::SALOME_Exception)
4087 {
4088   SMESH_TRY;
4089   initData();
4090
4091   TIDSortedNodeSet nodes;
4092   idSourceToNodeSet( theObject, getMeshDS(), nodes );
4093
4094   for ( int i = 0; i < theExceptSubMeshOrGroups.length(); ++i )
4095   {
4096     SMDS_ElemIteratorPtr nodeIt = myMesh_i->GetElements( theExceptSubMeshOrGroups[i],
4097                                                          SMESH::NODE );
4098     while ( nodeIt->more() )
4099       nodes.erase( cast2Node( nodeIt->next() ));
4100   }
4101   findCoincidentNodes( nodes, theTolerance, theGroupsOfNodes, theSeparateCornersAndMedium );
4102
4103   TPythonDump() << "coincident_nodes_on_part = " << this << ".FindCoincidentNodesOnPartBut( "
4104                 << theObject<<", "
4105                 << theTolerance << ", "
4106                 << theExceptSubMeshOrGroups << ", "
4107                 << theSeparateCornersAndMedium << " )";
4108
4109   SMESH_CATCH( SMESH::throwCorbaException );
4110 }
4111
4112 //=======================================================================
4113 //function : MergeNodes
4114 //purpose  :
4115 //=======================================================================
4116
4117 void SMESH_MeshEditor_i::MergeNodes (const SMESH::array_of_long_array& GroupsOfNodes,
4118                                      const SMESH::ListOfIDSources&     NodesToKeep)
4119   throw (SALOME::SALOME_Exception)
4120 {
4121   SMESH_TRY;
4122   initData();
4123
4124   SMESHDS_Mesh* aMesh = getMeshDS();
4125
4126   TPythonDump aTPythonDump;
4127   aTPythonDump << this << ".MergeNodes([";
4128
4129   TIDSortedNodeSet setOfNodesToKeep;
4130   for ( int i = 0; i < NodesToKeep.length(); ++i )
4131   {
4132     prepareIdSource( NodesToKeep[i] );
4133     SMDS_ElemIteratorPtr nodeIt = myMesh_i->GetElements( NodesToKeep[i], SMESH::NODE );
4134     while ( nodeIt->more() )
4135       setOfNodesToKeep.insert( setOfNodesToKeep.end(), cast2Node( nodeIt->next() ));
4136   }
4137
4138   ::SMESH_MeshEditor::TListOfListOfNodes aListOfListOfNodes;
4139   for (int i = 0; i < GroupsOfNodes.length(); i++)
4140   {
4141     const SMESH::long_array& aNodeGroup = GroupsOfNodes[ i ];
4142     aListOfListOfNodes.push_back( list< const SMDS_MeshNode* >() );
4143     list< const SMDS_MeshNode* >& aListOfNodes = aListOfListOfNodes.back();
4144     for ( int j = 0; j < aNodeGroup.length(); j++ )
4145     {
4146       CORBA::Long index = aNodeGroup[ j ];
4147       if ( const SMDS_MeshNode * node = aMesh->FindNode( index ))
4148       {
4149         if ( setOfNodesToKeep.count( node ))
4150           aListOfNodes.push_front( node );
4151         else
4152           aListOfNodes.push_back( node );
4153       }
4154     }
4155     if ( aListOfNodes.size() < 2 )
4156       aListOfListOfNodes.pop_back();
4157
4158     if ( i > 0 ) aTPythonDump << ", ";
4159     aTPythonDump << aNodeGroup;
4160   }
4161
4162   getEditor().MergeNodes( aListOfListOfNodes );
4163
4164   aTPythonDump << "], " << NodesToKeep << ")";
4165
4166   declareMeshModified( /*isReComputeSafe=*/false );
4167
4168   SMESH_CATCH( SMESH::throwCorbaException );
4169 }
4170
4171 //=======================================================================
4172 //function : FindEqualElements
4173 //purpose  :
4174 //=======================================================================
4175
4176 void SMESH_MeshEditor_i::FindEqualElements(SMESH::SMESH_IDSource_ptr      theObject,
4177                                            SMESH::array_of_long_array_out GroupsOfElementsID)
4178   throw (SALOME::SALOME_Exception)
4179 {
4180   SMESH_TRY;
4181   initData();
4182
4183   SMESH::SMESH_GroupBase_var group = SMESH::SMESH_GroupBase::_narrow(theObject);
4184   if ( !(!group->_is_nil() && group->GetType() == SMESH::NODE) )
4185   {
4186     TIDSortedElemSet elems;
4187     idSourceToSet( theObject, getMeshDS(), elems, SMDSAbs_All, /*emptyIfIsMesh=*/true);
4188
4189     ::SMESH_MeshEditor::TListOfListOfElementsID aListOfListOfElementsID;
4190     getEditor().FindEqualElements( elems, aListOfListOfElementsID );
4191
4192     GroupsOfElementsID = new SMESH::array_of_long_array;
4193     GroupsOfElementsID->length( aListOfListOfElementsID.size() );
4194
4195     ::SMESH_MeshEditor::TListOfListOfElementsID::iterator arraysIt =
4196         aListOfListOfElementsID.begin();
4197     for (CORBA::Long j = 0; arraysIt != aListOfListOfElementsID.end(); ++arraysIt, ++j)
4198     {
4199       SMESH::long_array& aGroup = (*GroupsOfElementsID)[ j ];
4200       list<int>&      listOfIDs = *arraysIt;
4201       aGroup.length( listOfIDs.size() );
4202       list<int>::iterator idIt = listOfIDs.begin();
4203       for (int k = 0; idIt != listOfIDs.end(); ++idIt, ++k )
4204         aGroup[ k ] = *idIt;
4205     }
4206
4207     TPythonDump() << "equal_elements = " << this << ".FindEqualElements( "
4208                   <<theObject<<" )";
4209   }
4210
4211   SMESH_CATCH( SMESH::throwCorbaException );
4212 }
4213
4214 //=======================================================================
4215 //function : MergeElements
4216 //purpose  :
4217 //=======================================================================
4218
4219 void SMESH_MeshEditor_i::MergeElements(const SMESH::array_of_long_array& GroupsOfElementsID)
4220   throw (SALOME::SALOME_Exception)
4221 {
4222   SMESH_TRY;
4223   initData();
4224
4225   TPythonDump aTPythonDump;
4226   aTPythonDump << this << ".MergeElements( [";
4227
4228   ::SMESH_MeshEditor::TListOfListOfElementsID aListOfListOfElementsID;
4229
4230   for (int i = 0; i < GroupsOfElementsID.length(); i++) {
4231     const SMESH::long_array& anElemsIDGroup = GroupsOfElementsID[ i ];
4232     aListOfListOfElementsID.push_back( list< int >() );
4233     list< int >& aListOfElemsID = aListOfListOfElementsID.back();
4234     for ( int j = 0; j < anElemsIDGroup.length(); j++ ) {
4235       CORBA::Long id = anElemsIDGroup[ j ];
4236       aListOfElemsID.push_back( id );
4237     }
4238     if ( aListOfElemsID.size() < 2 )
4239       aListOfListOfElementsID.pop_back();
4240     if ( i > 0 ) aTPythonDump << ", ";
4241     aTPythonDump << anElemsIDGroup;
4242   }
4243
4244   getEditor().MergeElements(aListOfListOfElementsID);
4245
4246   declareMeshModified( /*isReComputeSafe=*/true );
4247
4248   aTPythonDump << "] )";
4249
4250   SMESH_CATCH( SMESH::throwCorbaException );
4251 }
4252
4253 //=======================================================================
4254 //function : MergeEqualElements
4255 //purpose  :
4256 //=======================================================================
4257
4258 void SMESH_MeshEditor_i::MergeEqualElements()
4259   throw (SALOME::SALOME_Exception)
4260 {
4261   SMESH_TRY;
4262   initData();
4263
4264   getEditor().MergeEqualElements();
4265
4266   declareMeshModified( /*isReComputeSafe=*/true );
4267
4268   TPythonDump() << this << ".MergeEqualElements()";
4269
4270   SMESH_CATCH( SMESH::throwCorbaException );
4271 }
4272
4273 //=============================================================================
4274 /*!
4275  * Move the node to a given point
4276  */
4277 //=============================================================================
4278
4279 CORBA::Boolean SMESH_MeshEditor_i::MoveNode(CORBA::Long   NodeID,
4280                                             CORBA::Double x,
4281                                             CORBA::Double y,
4282                                             CORBA::Double z)
4283   throw (SALOME::SALOME_Exception)
4284 {
4285   SMESH_TRY;
4286   initData(/*deleteSearchers=*/false);
4287
4288   const SMDS_MeshNode * node = getMeshDS()->FindNode( NodeID );
4289   if ( !node )
4290     return false;
4291
4292   if ( theNodeSearcher )
4293     theSearchersDeleter.Set( myMesh ); // remove theNodeSearcher if mesh is other
4294
4295   if ( myIsPreviewMode ) // make preview data
4296   {
4297     // in a preview mesh, make edges linked to a node
4298     TPreviewMesh& tmpMesh = *getPreviewMesh();
4299     TIDSortedElemSet linkedNodes;
4300     ::SMESH_MeshEditor::GetLinkedNodes( node, linkedNodes );
4301     TIDSortedElemSet::iterator nIt = linkedNodes.begin();
4302     SMDS_MeshNode *nodeCpy1 = tmpMesh.Copy(node);
4303     for ( ; nIt != linkedNodes.end(); ++nIt )
4304     {
4305       SMDS_MeshNode *nodeCpy2 = tmpMesh.Copy ( cast2Node( *nIt ));
4306       tmpMesh.GetMeshDS()->AddEdge(nodeCpy1, nodeCpy2);
4307     }
4308     // move copied node
4309     if ( nodeCpy1 )
4310       tmpMesh.GetMeshDS()->MoveNode(nodeCpy1, x, y, z);
4311     // fill preview data
4312   }
4313   else if ( theNodeSearcher ) // move node and update theNodeSearcher data accordingly
4314     theNodeSearcher->MoveNode(node, gp_Pnt( x,y,z ));
4315   else
4316     getMeshDS()->MoveNode(node, x, y, z);
4317
4318   if ( !myIsPreviewMode )
4319   {
4320     // Update Python script
4321     TPythonDump() << "isDone = " << this << ".MoveNode( "
4322                   << NodeID << ", " << TVar(x) << ", " << TVar(y) << ", " << TVar(z) << " )";
4323     declareMeshModified( /*isReComputeSafe=*/false );
4324   }
4325
4326   SMESH_CATCH( SMESH::throwCorbaException );
4327
4328   return true;
4329 }
4330
4331 //================================================================================
4332 /*!
4333  * \brief Return ID of node closest to a given point
4334  */
4335 //================================================================================
4336
4337 CORBA::Long SMESH_MeshEditor_i::FindNodeClosestTo(CORBA::Double x,
4338                                                   CORBA::Double y,
4339                                                   CORBA::Double z)
4340   throw (SALOME::SALOME_Exception)
4341 {
4342   SMESH_TRY;
4343   theSearchersDeleter.Set( myMesh ); // remove theNodeSearcher if mesh is other
4344
4345   if ( !theNodeSearcher ) {
4346     theNodeSearcher = SMESH_MeshAlgos::GetNodeSearcher( *getMeshDS() );
4347   }
4348   gp_Pnt p( x,y,z );
4349   if ( const SMDS_MeshNode* node = theNodeSearcher->FindClosestTo( p ))
4350     return node->GetID();
4351
4352   SMESH_CATCH( SMESH::throwCorbaException );
4353   return 0;
4354 }
4355
4356 //================================================================================
4357 /*!
4358  * \brief If the given ID is a valid node ID (nodeID > 0), just move this node, else
4359  * move the node closest to the point to point's location and return ID of the node
4360  */
4361 //================================================================================
4362
4363 CORBA::Long SMESH_MeshEditor_i::MoveClosestNodeToPoint(CORBA::Double x,
4364                                                        CORBA::Double y,
4365                                                        CORBA::Double z,
4366                                                        CORBA::Long   theNodeID)
4367   throw (SALOME::SALOME_Exception)
4368 {
4369   SMESH_TRY;
4370   // We keep theNodeSearcher until any mesh modification:
4371   // 1) initData() deletes theNodeSearcher at any edition,
4372   // 2) TSearchersDeleter - at any mesh compute event and mesh change
4373
4374   initData(/*deleteSearchers=*/false);
4375
4376   theSearchersDeleter.Set( myMesh ); // remove theNodeSearcher if mesh is other
4377
4378   int nodeID = theNodeID;
4379   const SMDS_MeshNode* node = getMeshDS()->FindNode( nodeID );
4380   if ( !node ) // preview moving node
4381   {
4382     if ( !theNodeSearcher ) {
4383       theNodeSearcher = SMESH_MeshAlgos::GetNodeSearcher( *getMeshDS() );
4384     }
4385     gp_Pnt p( x,y,z );
4386     node = theNodeSearcher->FindClosestTo( p );
4387   }
4388   if ( node ) {
4389     nodeID = node->GetID();
4390     if ( myIsPreviewMode ) // make preview data
4391     {
4392       // in a preview mesh, make edges linked to a node
4393       TPreviewMesh tmpMesh = *getPreviewMesh();
4394       TIDSortedElemSet linkedNodes;
4395       ::SMESH_MeshEditor::GetLinkedNodes( node, linkedNodes );
4396       TIDSortedElemSet::iterator nIt = linkedNodes.begin();
4397       for ( ; nIt != linkedNodes.end(); ++nIt )
4398       {
4399         SMDS_LinearEdge edge( node, cast2Node( *nIt ));
4400         tmpMesh.Copy( &edge );
4401       }
4402       // move copied node
4403       node = tmpMesh.GetMeshDS()->FindNode( nodeID );
4404       if ( node )
4405         tmpMesh.GetMeshDS()->MoveNode(node, x, y, z);
4406       // fill preview data
4407     }
4408     else if ( theNodeSearcher ) // move node and update theNodeSearcher data accordingly
4409     {
4410       theNodeSearcher->MoveNode(node, gp_Pnt( x,y,z ));
4411     }
4412     else
4413     {
4414       getMeshDS()->MoveNode(node, x, y, z);
4415     }
4416   }
4417
4418   if ( !myIsPreviewMode )
4419   {
4420     TPythonDump() << "nodeID = " << this
4421                   << ".MoveClosestNodeToPoint( "<< x << ", " << y << ", " << z
4422                   << ", " << nodeID << " )";
4423
4424     declareMeshModified( /*isReComputeSafe=*/false );
4425   }
4426
4427   return nodeID;
4428
4429   SMESH_CATCH( SMESH::throwCorbaException );
4430   return 0;
4431 }
4432
4433 //=======================================================================
4434 /*!
4435  * Return elements of given type where the given point is IN or ON.
4436  *
4437  * 'ALL' type means elements of any type excluding nodes
4438  */
4439 //=======================================================================
4440
4441 SMESH::long_array* SMESH_MeshEditor_i::FindElementsByPoint(CORBA::Double      x,
4442                                                            CORBA::Double      y,
4443                                                            CORBA::Double      z,
4444                                                            SMESH::ElementType type)
4445   throw (SALOME::SALOME_Exception)
4446 {
4447   SMESH_TRY;
4448   SMESH::long_array_var res = new SMESH::long_array;
4449   vector< const SMDS_MeshElement* > foundElems;
4450
4451   theSearchersDeleter.Set( myMesh );
4452   if ( !theElementSearcher ) {
4453     theElementSearcher = SMESH_MeshAlgos::GetElementSearcher( *getMeshDS() );
4454   }
4455   theElementSearcher->FindElementsByPoint( gp_Pnt( x,y,z ),
4456                                            SMDSAbs_ElementType( type ),
4457                                            foundElems);
4458   res->length( foundElems.size() );
4459   for ( int i = 0; i < foundElems.size(); ++i )
4460     res[i] = foundElems[i]->GetID();
4461
4462   return res._retn();
4463
4464   SMESH_CATCH( SMESH::throwCorbaException );
4465   return 0;
4466 }
4467
4468 //=======================================================================
4469 //function : FindAmongElementsByPoint
4470 //purpose  : Searching among the given elements, return elements of given type 
4471 //           where the given point is IN or ON.
4472 //           'ALL' type means elements of any type excluding nodes
4473 //=======================================================================
4474
4475 SMESH::long_array*
4476 SMESH_MeshEditor_i::FindAmongElementsByPoint(SMESH::SMESH_IDSource_ptr elementIDs,
4477                                              CORBA::Double             x,
4478                                              CORBA::Double             y,
4479                                              CORBA::Double             z,
4480                                              SMESH::ElementType        type)
4481   throw (SALOME::SALOME_Exception)
4482 {
4483   SMESH_TRY;
4484   SMESH::long_array_var res = new SMESH::long_array;
4485   
4486   SMESH::array_of_ElementType_var types = elementIDs->GetTypes();
4487   if ( types->length() == 1 && // a part contains only nodes or 0D elements
4488        ( types[0] == SMESH::NODE || types[0] == SMESH::ELEM0D || types[0] == SMESH::BALL) &&
4489        type != types[0] ) // but search of elements of dim > 0
4490     return res._retn();
4491
4492   if ( SMESH::DownCast<SMESH_Mesh_i*>( elementIDs )) // elementIDs is the whole mesh 
4493     return FindElementsByPoint( x,y,z, type );
4494
4495   TIDSortedElemSet elements; // elems should live until FindElementsByPoint() finishes
4496
4497   theSearchersDeleter.Set( myMesh, getPartIOR( elementIDs, type ));
4498   if ( !theElementSearcher )
4499   {
4500     // create a searcher from elementIDs
4501     SMESH::SMESH_Mesh_var mesh = elementIDs->GetMesh();
4502     SMESHDS_Mesh* meshDS = SMESH::DownCast<SMESH_Mesh_i*>( mesh )->GetImpl().GetMeshDS();
4503
4504     if ( !idSourceToSet( elementIDs, meshDS, elements,
4505                          SMDSAbs_ElementType(type), /*emptyIfIsMesh=*/true))
4506       return res._retn();
4507
4508     typedef SMDS_SetIterator<const SMDS_MeshElement*, TIDSortedElemSet::const_iterator > TIter;
4509     SMDS_ElemIteratorPtr elemsIt( new TIter( elements.begin(), elements.end() ));
4510
4511     theElementSearcher = SMESH_MeshAlgos::GetElementSearcher( *getMeshDS(), elemsIt );
4512   }
4513
4514   vector< const SMDS_MeshElement* > foundElems;
4515
4516   theElementSearcher->FindElementsByPoint( gp_Pnt( x,y,z ),
4517                                            SMDSAbs_ElementType( type ),
4518                                            foundElems);
4519   res->length( foundElems.size() );
4520   for ( int i = 0; i < foundElems.size(); ++i )
4521     res[i] = foundElems[i]->GetID();
4522
4523   return res._retn();
4524
4525   SMESH_CATCH( SMESH::throwCorbaException );
4526   return 0;
4527 }
4528
4529 //=======================================================================
4530 //function : GetPointState
4531 //purpose  : Return point state in a closed 2D mesh in terms of TopAbs_State enumeration.
4532 //           TopAbs_UNKNOWN state means that either mesh is wrong or the analysis fails.
4533 //=======================================================================
4534
4535 CORBA::Short SMESH_MeshEditor_i::GetPointState(CORBA::Double x,
4536                                                CORBA::Double y,
4537                                                CORBA::Double z)
4538   throw (SALOME::SALOME_Exception)
4539 {
4540   SMESH_TRY;
4541   theSearchersDeleter.Set( myMesh );
4542   if ( !theElementSearcher ) {
4543     theElementSearcher = SMESH_MeshAlgos::GetElementSearcher( *getMeshDS() );
4544   }
4545   return CORBA::Short( theElementSearcher->GetPointState( gp_Pnt( x,y,z )));
4546
4547   SMESH_CATCH( SMESH::throwCorbaException );
4548   return 0;
4549 }
4550
4551 //=======================================================================
4552 //function : convError
4553 //purpose  :
4554 //=======================================================================
4555
4556 #define RETCASE(enm) case ::SMESH_MeshEditor::enm: return SMESH::SMESH_MeshEditor::enm;
4557
4558 static SMESH::SMESH_MeshEditor::Sew_Error convError( const::SMESH_MeshEditor::Sew_Error e )
4559 {
4560   switch ( e ) {
4561     RETCASE( SEW_OK );
4562     RETCASE( SEW_BORDER1_NOT_FOUND );
4563     RETCASE( SEW_BORDER2_NOT_FOUND );
4564     RETCASE( SEW_BOTH_BORDERS_NOT_FOUND );
4565     RETCASE( SEW_BAD_SIDE_NODES );
4566     RETCASE( SEW_VOLUMES_TO_SPLIT );
4567     RETCASE( SEW_DIFF_NB_OF_ELEMENTS );
4568     RETCASE( SEW_TOPO_DIFF_SETS_OF_ELEMENTS );
4569     RETCASE( SEW_BAD_SIDE1_NODES );
4570     RETCASE( SEW_BAD_SIDE2_NODES );
4571   }
4572   return SMESH::SMESH_MeshEditor::SEW_OK;
4573 }
4574
4575 //=======================================================================
4576 //function : SewFreeBorders
4577 //purpose  :
4578 //=======================================================================
4579
4580 SMESH::SMESH_MeshEditor::Sew_Error
4581 SMESH_MeshEditor_i::SewFreeBorders(CORBA::Long FirstNodeID1,
4582                                    CORBA::Long SecondNodeID1,
4583                                    CORBA::Long LastNodeID1,
4584                                    CORBA::Long FirstNodeID2,
4585                                    CORBA::Long SecondNodeID2,
4586                                    CORBA::Long LastNodeID2,
4587                                    CORBA::Boolean CreatePolygons,
4588                                    CORBA::Boolean CreatePolyedrs)
4589   throw (SALOME::SALOME_Exception)
4590 {
4591   SMESH_TRY;
4592   initData();
4593
4594   SMESHDS_Mesh* aMesh = getMeshDS();
4595
4596   const SMDS_MeshNode* aBorderFirstNode  = aMesh->FindNode( FirstNodeID1  );
4597   const SMDS_MeshNode* aBorderSecondNode = aMesh->FindNode( SecondNodeID1 );
4598   const SMDS_MeshNode* aBorderLastNode   = aMesh->FindNode( LastNodeID1   );
4599   const SMDS_MeshNode* aSide2FirstNode   = aMesh->FindNode( FirstNodeID2  );
4600   const SMDS_MeshNode* aSide2SecondNode  = aMesh->FindNode( SecondNodeID2 );
4601   const SMDS_MeshNode* aSide2ThirdNode   = aMesh->FindNode( LastNodeID2   );
4602
4603   if (!aBorderFirstNode ||
4604       !aBorderSecondNode||
4605       !aBorderLastNode)
4606     return SMESH::SMESH_MeshEditor::SEW_BORDER1_NOT_FOUND;
4607   if (!aSide2FirstNode  ||
4608       !aSide2SecondNode ||
4609       !aSide2ThirdNode)
4610     return SMESH::SMESH_MeshEditor::SEW_BORDER2_NOT_FOUND;
4611
4612   TPythonDump() << "error = " << this << ".SewFreeBorders( "
4613                 << FirstNodeID1  << ", "
4614                 << SecondNodeID1 << ", "
4615                 << LastNodeID1   << ", "
4616                 << FirstNodeID2  << ", "
4617                 << SecondNodeID2 << ", "
4618                 << LastNodeID2   << ", "
4619                 << CreatePolygons<< ", "
4620                 << CreatePolyedrs<< " )";
4621
4622   SMESH::SMESH_MeshEditor::Sew_Error error =
4623     convError( getEditor().SewFreeBorder (aBorderFirstNode,
4624                                        aBorderSecondNode,
4625                                        aBorderLastNode,
4626                                        aSide2FirstNode,
4627                                        aSide2SecondNode,
4628                                        aSide2ThirdNode,
4629                                        true,
4630                                        CreatePolygons,
4631                                        CreatePolyedrs) );
4632
4633
4634   declareMeshModified( /*isReComputeSafe=*/false );
4635   return error;
4636
4637   SMESH_CATCH( SMESH::throwCorbaException );
4638   return SMESH::SMESH_MeshEditor::Sew_Error(0);
4639 }
4640
4641
4642 //=======================================================================
4643 //function : SewConformFreeBorders
4644 //purpose  :
4645 //=======================================================================
4646
4647 SMESH::SMESH_MeshEditor::Sew_Error
4648 SMESH_MeshEditor_i::SewConformFreeBorders(CORBA::Long FirstNodeID1,
4649                                           CORBA::Long SecondNodeID1,
4650                                           CORBA::Long LastNodeID1,
4651                                           CORBA::Long FirstNodeID2,
4652                                           CORBA::Long SecondNodeID2)
4653   throw (SALOME::SALOME_Exception)
4654 {
4655   SMESH_TRY;
4656   initData();
4657
4658   SMESHDS_Mesh* aMesh = getMeshDS();
4659
4660   const SMDS_MeshNode* aBorderFirstNode  = aMesh->FindNode( FirstNodeID1  );
4661   const SMDS_MeshNode* aBorderSecondNode = aMesh->FindNode( SecondNodeID1 );
4662   const SMDS_MeshNode* aBorderLastNode   = aMesh->FindNode( LastNodeID1   );
4663   const SMDS_MeshNode* aSide2FirstNode   = aMesh->FindNode( FirstNodeID2  );
4664   const SMDS_MeshNode* aSide2SecondNode  = aMesh->FindNode( SecondNodeID2 );
4665   const SMDS_MeshNode* aSide2ThirdNode   = 0;
4666
4667   if (!aBorderFirstNode ||
4668       !aBorderSecondNode||
4669       !aBorderLastNode )
4670     return SMESH::SMESH_MeshEditor::SEW_BORDER1_NOT_FOUND;
4671   if (!aSide2FirstNode  ||
4672       !aSide2SecondNode)
4673     return SMESH::SMESH_MeshEditor::SEW_BORDER2_NOT_FOUND;
4674
4675   TPythonDump() << "error = " << this << ".SewConformFreeBorders( "
4676                 << FirstNodeID1  << ", "
4677                 << SecondNodeID1 << ", "
4678                 << LastNodeID1   << ", "
4679                 << FirstNodeID2  << ", "
4680                 << SecondNodeID2 << " )";
4681
4682   SMESH::SMESH_MeshEditor::Sew_Error error =
4683     convError( getEditor().SewFreeBorder (aBorderFirstNode,
4684                                        aBorderSecondNode,
4685                                        aBorderLastNode,
4686                                        aSide2FirstNode,
4687                                        aSide2SecondNode,
4688                                        aSide2ThirdNode,
4689                                        true,
4690                                        false, false) );
4691
4692   declareMeshModified( /*isReComputeSafe=*/false );
4693   return error;
4694
4695   SMESH_CATCH( SMESH::throwCorbaException );
4696   return SMESH::SMESH_MeshEditor::Sew_Error(0);
4697 }
4698
4699
4700 //=======================================================================
4701 //function : SewBorderToSide
4702 //purpose  :
4703 //=======================================================================
4704
4705 SMESH::SMESH_MeshEditor::Sew_Error
4706 SMESH_MeshEditor_i::SewBorderToSide(CORBA::Long FirstNodeIDOnFreeBorder,
4707                                     CORBA::Long SecondNodeIDOnFreeBorder,
4708                                     CORBA::Long LastNodeIDOnFreeBorder,
4709                                     CORBA::Long FirstNodeIDOnSide,
4710                                     CORBA::Long LastNodeIDOnSide,
4711                                     CORBA::Boolean CreatePolygons,
4712                                     CORBA::Boolean CreatePolyedrs)
4713   throw (SALOME::SALOME_Exception)
4714 {
4715   SMESH_TRY;
4716   initData();
4717
4718   SMESHDS_Mesh* aMesh = getMeshDS();
4719
4720   const SMDS_MeshNode* aBorderFirstNode  = aMesh->FindNode( FirstNodeIDOnFreeBorder  );
4721   const SMDS_MeshNode* aBorderSecondNode = aMesh->FindNode( SecondNodeIDOnFreeBorder );
4722   const SMDS_MeshNode* aBorderLastNode   = aMesh->FindNode( LastNodeIDOnFreeBorder   );
4723   const SMDS_MeshNode* aSide2FirstNode   = aMesh->FindNode( FirstNodeIDOnSide  );
4724   const SMDS_MeshNode* aSide2SecondNode  = aMesh->FindNode( LastNodeIDOnSide );
4725   const SMDS_MeshNode* aSide2ThirdNode   = 0;
4726
4727   if (!aBorderFirstNode ||
4728       !aBorderSecondNode||
4729       !aBorderLastNode  )
4730     return SMESH::SMESH_MeshEditor::SEW_BORDER1_NOT_FOUND;
4731   if (!aSide2FirstNode  ||
4732       !aSide2SecondNode)
4733     return SMESH::SMESH_MeshEditor::SEW_BAD_SIDE_NODES;
4734
4735   TPythonDump() << "error = " << this << ".SewBorderToSide( "
4736                 << FirstNodeIDOnFreeBorder  << ", "
4737                 << SecondNodeIDOnFreeBorder << ", "
4738                 << LastNodeIDOnFreeBorder   << ", "
4739                 << FirstNodeIDOnSide        << ", "
4740                 << LastNodeIDOnSide         << ", "
4741                 << CreatePolygons           << ", "
4742                 << CreatePolyedrs           << ") ";
4743
4744   SMESH::SMESH_MeshEditor::Sew_Error error =
4745     convError( getEditor().SewFreeBorder (aBorderFirstNode,
4746                                        aBorderSecondNode,
4747                                        aBorderLastNode,
4748                                        aSide2FirstNode,
4749                                        aSide2SecondNode,
4750                                        aSide2ThirdNode,
4751                                        false,
4752                                        CreatePolygons,
4753                                        CreatePolyedrs) );
4754
4755   declareMeshModified( /*isReComputeSafe=*/false );
4756   return error;
4757
4758   SMESH_CATCH( SMESH::throwCorbaException );
4759   return SMESH::SMESH_MeshEditor::Sew_Error(0);
4760 }
4761
4762
4763 //=======================================================================
4764 //function : SewSideElements
4765 //purpose  :
4766 //=======================================================================
4767
4768 SMESH::SMESH_MeshEditor::Sew_Error
4769 SMESH_MeshEditor_i::SewSideElements(const SMESH::long_array& IDsOfSide1Elements,
4770                                     const SMESH::long_array& IDsOfSide2Elements,
4771                                     CORBA::Long NodeID1OfSide1ToMerge,
4772                                     CORBA::Long NodeID1OfSide2ToMerge,
4773                                     CORBA::Long NodeID2OfSide1ToMerge,
4774                                     CORBA::Long NodeID2OfSide2ToMerge)
4775   throw (SALOME::SALOME_Exception)
4776 {
4777   SMESH_TRY;
4778   initData();
4779
4780   SMESHDS_Mesh* aMesh = getMeshDS();
4781
4782   const SMDS_MeshNode* aFirstNode1ToMerge  = aMesh->FindNode( NodeID1OfSide1ToMerge );
4783   const SMDS_MeshNode* aFirstNode2ToMerge  = aMesh->FindNode( NodeID1OfSide2ToMerge );
4784   const SMDS_MeshNode* aSecondNode1ToMerge = aMesh->FindNode( NodeID2OfSide1ToMerge );
4785   const SMDS_MeshNode* aSecondNode2ToMerge = aMesh->FindNode( NodeID2OfSide2ToMerge );
4786
4787   if (!aFirstNode1ToMerge ||
4788       !aFirstNode2ToMerge )
4789     return SMESH::SMESH_MeshEditor::SEW_BAD_SIDE1_NODES;
4790   if (!aSecondNode1ToMerge||
4791       !aSecondNode2ToMerge)
4792     return SMESH::SMESH_MeshEditor::SEW_BAD_SIDE2_NODES;
4793
4794   TIDSortedElemSet aSide1Elems, aSide2Elems;
4795   arrayToSet(IDsOfSide1Elements, aMesh, aSide1Elems);
4796   arrayToSet(IDsOfSide2Elements, aMesh, aSide2Elems);
4797
4798   TPythonDump() << "error = " << this << ".SewSideElements( "
4799                 << IDsOfSide1Elements << ", "
4800                 << IDsOfSide2Elements << ", "
4801                 << NodeID1OfSide1ToMerge << ", "
4802                 << NodeID1OfSide2ToMerge << ", "
4803                 << NodeID2OfSide1ToMerge << ", "
4804                 << NodeID2OfSide2ToMerge << ")";
4805
4806   SMESH::SMESH_MeshEditor::Sew_Error error =
4807     convError( getEditor().SewSideElements (aSide1Elems, aSide2Elems,
4808                                          aFirstNode1ToMerge,
4809                                          aFirstNode2ToMerge,
4810                                          aSecondNode1ToMerge,
4811                                          aSecondNode2ToMerge));
4812
4813   declareMeshModified( /*isReComputeSafe=*/false );
4814   return error;
4815
4816   SMESH_CATCH( SMESH::throwCorbaException );
4817   return SMESH::SMESH_MeshEditor::Sew_Error(0);
4818 }
4819
4820 //================================================================================
4821 /*!
4822  * \brief Set new nodes for given element
4823  * \param ide - element id
4824  * \param newIDs - new node ids
4825  * \retval CORBA::Boolean - true if result is OK
4826  */
4827 //================================================================================
4828
4829 CORBA::Boolean SMESH_MeshEditor_i::ChangeElemNodes(CORBA::Long ide,
4830                                                    const SMESH::long_array& newIDs)
4831   throw (SALOME::SALOME_Exception)
4832 {
4833   SMESH_TRY;
4834   initData();
4835
4836   const SMDS_MeshElement* elem = getMeshDS()->FindElement(ide);
4837   if(!elem) return false;
4838
4839   int nbn = newIDs.length();
4840   int i=0;
4841   vector<const SMDS_MeshNode*> aNodes(nbn);
4842   int nbn1=-1;
4843   for(; i<nbn; i++) {
4844     const SMDS_MeshNode* aNode = getMeshDS()->FindNode(newIDs[i]);
4845     if(aNode) {
4846       nbn1++;
4847       aNodes[nbn1] = aNode;
4848     }
4849   }
4850   TPythonDump() << "isDone = " << this << ".ChangeElemNodes( "
4851                 << ide << ", " << newIDs << " )";
4852
4853   MESSAGE("ChangeElementNodes");
4854   bool res = getMeshDS()->ChangeElementNodes( elem, & aNodes[0], nbn1+1 );
4855
4856   declareMeshModified( /*isReComputeSafe=*/ !res );
4857
4858   return res;
4859
4860   SMESH_CATCH( SMESH::throwCorbaException );
4861   return 0;
4862 }
4863
4864 //=======================================================================
4865 /*!
4866  * \brief Makes a part of the mesh quadratic or bi-quadratic
4867  */
4868 //=======================================================================
4869
4870 void SMESH_MeshEditor_i::convertToQuadratic(CORBA::Boolean            theForce3d,
4871                                             CORBA::Boolean            theToBiQuad,
4872                                             SMESH::SMESH_IDSource_ptr theObject)
4873   throw (SALOME::SALOME_Exception)
4874 {
4875   SMESH_TRY;
4876   initData();
4877
4878   TIDSortedElemSet elems;
4879   bool elemsOK;
4880   if ( !( elemsOK = CORBA::is_nil( theObject )))
4881   {
4882     elemsOK =  idSourceToSet( theObject, getMeshDS(), elems,
4883                               SMDSAbs_All, /*emptyIfIsMesh=*/true );
4884   }
4885   if ( elemsOK )
4886   {
4887     if ( !elems.empty() && (*elems.begin())->GetType() == SMDSAbs_Node )
4888       THROW_SALOME_CORBA_EXCEPTION("Group of nodes is not allowed", SALOME::BAD_PARAM);
4889
4890     if ( elems.empty() ) getEditor().ConvertToQuadratic(theForce3d, theToBiQuad);
4891     else                 getEditor().ConvertToQuadratic(theForce3d, elems, theToBiQuad);
4892
4893     declareMeshModified( /*isReComputeSafe=*/false );
4894   }
4895
4896   SMESH_CATCH( SMESH::throwCorbaException );
4897 }
4898
4899 //=======================================================================
4900 //function : ConvertFromQuadratic
4901 //purpose  :
4902 //=======================================================================
4903
4904 CORBA::Boolean SMESH_MeshEditor_i::ConvertFromQuadratic()
4905   throw (SALOME::SALOME_Exception)
4906 {
4907   SMESH_TRY;
4908   initData();
4909
4910   CORBA::Boolean isDone = getEditor().ConvertFromQuadratic();
4911   TPythonDump() << this << ".ConvertFromQuadratic()";
4912   declareMeshModified( /*isReComputeSafe=*/!isDone );
4913   return isDone;
4914
4915   SMESH_CATCH( SMESH::throwCorbaException );
4916   return false;
4917 }
4918
4919 //=======================================================================
4920 //function : ConvertToQuadratic
4921 //purpose  :
4922 //=======================================================================
4923
4924 void SMESH_MeshEditor_i::ConvertToQuadratic(CORBA::Boolean theForce3d)
4925   throw (SALOME::SALOME_Exception)
4926 {
4927   convertToQuadratic( theForce3d, false );
4928   TPythonDump() << this << ".ConvertToQuadratic("<<theForce3d<<")";
4929 }
4930
4931 //================================================================================
4932 /*!
4933  * \brief Makes a part of the mesh quadratic
4934  */
4935 //================================================================================
4936
4937 void SMESH_MeshEditor_i::ConvertToQuadraticObject(CORBA::Boolean            theForce3d,
4938                                                   SMESH::SMESH_IDSource_ptr theObject)
4939   throw (SALOME::SALOME_Exception)
4940 {
4941   convertToQuadratic( theForce3d, false, theObject );
4942   TPythonDump() << this << ".ConvertToQuadraticObject("<<theForce3d<<", "<<theObject<<")";
4943 }
4944
4945 //================================================================================
4946 /*!
4947  * \brief Makes a part of the mesh bi-quadratic
4948  */
4949 //================================================================================
4950
4951 void SMESH_MeshEditor_i::ConvertToBiQuadratic(CORBA::Boolean            theForce3d,
4952                                               SMESH::SMESH_IDSource_ptr theObject)
4953   throw (SALOME::SALOME_Exception)
4954 {
4955   convertToQuadratic( theForce3d, true, theObject );
4956   TPythonDump() << this << ".ConvertToBiQuadratic("<<theForce3d<<", "<<theObject<<")";
4957 }
4958
4959 //================================================================================
4960 /*!
4961  * \brief Makes a part of the mesh linear
4962  */
4963 //================================================================================
4964
4965 void SMESH_MeshEditor_i::ConvertFromQuadraticObject(SMESH::SMESH_IDSource_ptr theObject)
4966   throw (SALOME::SALOME_Exception)
4967 {
4968   SMESH_TRY;
4969   initData();
4970
4971   TPythonDump pyDump;
4972
4973   TIDSortedElemSet elems;
4974   if ( idSourceToSet( theObject, getMeshDS(), elems, SMDSAbs_All, /*emptyIfIsMesh=*/true ))
4975   {
4976     if ( elems.empty() )
4977     {
4978       ConvertFromQuadratic();
4979     }
4980     else if ( (*elems.begin())->GetType() == SMDSAbs_Node )
4981     {
4982       THROW_SALOME_CORBA_EXCEPTION("Group of nodes is not allowed", SALOME::BAD_PARAM);
4983     }
4984     else
4985     {
4986       getEditor().ConvertFromQuadratic(elems);
4987     }
4988   }
4989   declareMeshModified( /*isReComputeSafe=*/false );
4990
4991   pyDump << this << ".ConvertFromQuadraticObject( "<<theObject<<" )";
4992
4993   SMESH_CATCH( SMESH::throwCorbaException );
4994 }
4995
4996 //=======================================================================
4997 //function : makeMesh
4998 //purpose  : create a named imported mesh
4999 //=======================================================================
5000
5001 SMESH::SMESH_Mesh_ptr SMESH_MeshEditor_i::makeMesh(const char* theMeshName)
5002 {
5003   SMESH_Gen_i*              gen = SMESH_Gen_i::GetSMESHGen();
5004   SMESH::SMESH_Mesh_var    mesh = gen->CreateEmptyMesh();
5005   SALOMEDS::Study_var     study = gen->GetCurrentStudy();
5006   SALOMEDS::SObject_wrap meshSO = gen->ObjectToSObject( study, mesh );
5007   gen->SetName( meshSO, theMeshName, "Mesh" );
5008   gen->SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED");
5009
5010   return mesh._retn();
5011 }
5012
5013 //=======================================================================
5014 //function : dumpGroupsList
5015 //purpose  :
5016 //=======================================================================
5017
5018 void SMESH_MeshEditor_i::dumpGroupsList(TPythonDump &               theDumpPython,
5019                                         const SMESH::ListOfGroups * theGroupList)
5020 {
5021   bool isDumpGroupList = ( theGroupList && theGroupList->length() > 0 );
5022   if ( isDumpGroupList )
5023     theDumpPython << theGroupList << " = ";
5024 }
5025
5026 //================================================================================
5027 /*!
5028   \brief Generates the unique group name.
5029   \param thePrefix name prefix
5030   \return unique name
5031 */
5032 //================================================================================
5033
5034 string SMESH_MeshEditor_i::generateGroupName(const string& thePrefix)
5035 {
5036   SMESH::ListOfGroups_var groups = myMesh_i->GetGroups();
5037   set<string> groupNames;
5038
5039   // Get existing group names
5040   for (int i = 0, nbGroups = groups->length(); i < nbGroups; i++ ) {
5041     SMESH::SMESH_GroupBase_var aGroup = groups[i];
5042     if (CORBA::is_nil(aGroup))
5043       continue;
5044
5045     CORBA::String_var name = aGroup->GetName();
5046     groupNames.insert( name.in() );
5047   }
5048
5049   // Find new name
5050   string name = thePrefix;
5051   int index = 0;
5052
5053   while (!groupNames.insert(name).second)
5054     name = SMESH_Comment( thePrefix ) << "_" << index++;
5055
5056   return name;
5057 }
5058
5059 //================================================================================
5060 /*!
5061  * \brief Prepare SMESH_IDSource for work
5062  */
5063 //================================================================================
5064
5065 void SMESH_MeshEditor_i::prepareIdSource(SMESH::SMESH_IDSource_ptr theObject)
5066 {
5067   if ( SMESH::Filter_i* filter = SMESH::DownCast<SMESH::Filter_i*>( theObject ))
5068   {
5069     SMESH::SMESH_Mesh_var mesh = myMesh_i->_this();
5070     filter->SetMesh( mesh );
5071   }
5072 }
5073 //================================================================================
5074 /*!
5075  * \brief Retrieve elements of given type from SMESH_IDSource
5076  */
5077 //================================================================================
5078
5079 bool SMESH_MeshEditor_i::idSourceToSet(SMESH::SMESH_IDSource_ptr  theIDSource,
5080                                        const SMESHDS_Mesh*        theMeshDS,
5081                                        TIDSortedElemSet&          theElemSet,
5082                                        const SMDSAbs_ElementType  theType,
5083                                        const bool                 emptyIfIsMesh,
5084                                        IDSource_Error*            error)
5085
5086 {
5087   if ( error ) *error = IDSource_OK;
5088
5089   if ( CORBA::is_nil( theIDSource ) )
5090   {
5091     if ( error ) *error = IDSource_INVALID;
5092     return false;
5093   }
5094   if ( emptyIfIsMesh && SMESH::DownCast<SMESH_Mesh_i*>( theIDSource ))
5095   {
5096     if ( error && getMeshDS()->GetMeshInfo().NbElements( theType ) == 0 )
5097       *error = IDSource_EMPTY;
5098     return true;
5099   }
5100   prepareIdSource( theIDSource );
5101   SMESH::long_array_var anIDs = theIDSource->GetIDs();
5102   if ( anIDs->length() == 0 )
5103   {
5104     if ( error ) *error = IDSource_EMPTY;
5105     return false;
5106   }
5107   SMESH::array_of_ElementType_var types = theIDSource->GetTypes();
5108   if ( types->length() == 1 && types[0] == SMESH::NODE ) // group of nodes
5109   {
5110     if ( theType == SMDSAbs_All || theType == SMDSAbs_Node )
5111     {
5112       arrayToSet( anIDs, getMeshDS(), theElemSet, SMDSAbs_Node );
5113     }
5114     else
5115     {
5116       if ( error ) *error = IDSource_INVALID;
5117       return false;
5118     }
5119   }
5120   else
5121   {
5122     arrayToSet( anIDs, getMeshDS(), theElemSet, theType);
5123     if ( bool(anIDs->length()) != bool(theElemSet.size()))
5124     {
5125       if ( error ) *error = IDSource_INVALID;
5126       return false;
5127     }
5128   }
5129   return true;
5130 }
5131
5132 //================================================================================
5133 /*!
5134  * \brief Duplicates given elements, i.e. creates new elements based on the
5135  *        same nodes as the given ones.
5136  * \param theElements - container of elements to duplicate.
5137  * \param theGroupName - a name of group to contain the generated elements.
5138  *                    If a group with such a name already exists, the new elements
5139  *                    are added to the existng group, else a new group is created.
5140  *                    If \a theGroupName is empty, new elements are not added 
5141  *                    in any group.
5142  * \return a group where the new elements are added. NULL if theGroupName == "".
5143  * \sa DoubleNode()
5144  */
5145 //================================================================================
5146
5147 SMESH::SMESH_Group_ptr
5148 SMESH_MeshEditor_i::DoubleElements(SMESH::SMESH_IDSource_ptr theElements,
5149                                    const char*               theGroupName)
5150   throw (SALOME::SALOME_Exception)
5151 {
5152   SMESH::SMESH_Group_var newGroup;
5153
5154   SMESH_TRY;
5155   initData();
5156
5157   TPythonDump pyDump;
5158
5159   TIDSortedElemSet elems;
5160   if ( idSourceToSet( theElements, getMeshDS(), elems, SMDSAbs_All, /*emptyIfIsMesh=*/true))
5161   {
5162     getEditor().DoubleElements( elems );
5163
5164     if ( strlen( theGroupName ) && !getEditor().GetLastCreatedElems().IsEmpty() )
5165     {
5166       // group type
5167       SMESH::ElementType type =
5168         SMESH::ElementType( getEditor().GetLastCreatedElems().Value(1)->GetType() );
5169       // find existing group
5170       SMESH::ListOfGroups_var groups = myMesh_i->GetGroups();
5171       for ( size_t i = 0; i < groups->length(); ++i )
5172         if ( groups[i]->GetType() == type )
5173         {
5174           CORBA::String_var name = groups[i]->GetName();
5175           if ( strcmp( name, theGroupName ) == 0 ) {
5176             newGroup = SMESH::SMESH_Group::_narrow( groups[i] );
5177             break;
5178           }
5179         }
5180       // create a new group
5181       if ( newGroup->_is_nil() )
5182         newGroup = myMesh_i->CreateGroup( type, theGroupName );
5183       // fill newGroup
5184       if ( SMESH_Group_i* group_i = SMESH::DownCast< SMESH_Group_i* >( newGroup ))
5185       {
5186         SMESHDS_Group* groupDS = static_cast< SMESHDS_Group* >( group_i->GetGroupDS() );
5187         const SMESH_SequenceOfElemPtr& aSeq = getEditor().GetLastCreatedElems();
5188         for ( int i = 1; i <= aSeq.Length(); i++ )
5189           groupDS->SMDSGroup().Add( aSeq(i) );
5190       }
5191     }
5192   }
5193   // python dump
5194   if ( !newGroup->_is_nil() )
5195     pyDump << newGroup << " = ";
5196   pyDump << this << ".DoubleElements( "
5197          << theElements << ", " << "'" << theGroupName <<"')";
5198
5199   SMESH_CATCH( SMESH::throwCorbaException );
5200
5201   return newGroup._retn();
5202 }
5203
5204 //================================================================================
5205 /*!
5206   \brief Creates a hole in a mesh by doubling the nodes of some particular elements
5207   \param theNodes - identifiers of nodes to be doubled
5208   \param theModifiedElems - identifiers of elements to be updated by the new (doubled)
5209          nodes. If list of element identifiers is empty then nodes are doubled but
5210          they not assigned to elements
5211   \return TRUE if operation has been completed successfully, FALSE otherwise
5212   \sa DoubleNode(), DoubleNodeGroup(), DoubleNodeGroups()
5213 */
5214 //================================================================================
5215
5216 CORBA::Boolean SMESH_MeshEditor_i::DoubleNodes( const SMESH::long_array& theNodes,
5217                                                 const SMESH::long_array& theModifiedElems )
5218   throw (SALOME::SALOME_Exception)
5219 {
5220   SMESH_TRY;
5221   initData();
5222
5223   list< int > aListOfNodes;
5224   int i, n;
5225   for ( i = 0, n = theNodes.length(); i < n; i++ )
5226     aListOfNodes.push_back( theNodes[ i ] );
5227
5228   list< int > aListOfElems;
5229   for ( i = 0, n = theModifiedElems.length(); i < n; i++ )
5230     aListOfElems.push_back( theModifiedElems[ i ] );
5231
5232   bool aResult = getEditor().DoubleNodes( aListOfNodes, aListOfElems );
5233
5234   declareMeshModified( /*isReComputeSafe=*/ !aResult );
5235
5236   // Update Python script
5237   TPythonDump() << this << ".DoubleNodes( " << theNodes << ", "<< theModifiedElems << " )";
5238
5239   return aResult;
5240
5241   SMESH_CATCH( SMESH::throwCorbaException );
5242   return 0;
5243 }
5244
5245 //================================================================================
5246 /*!
5247   \brief Creates a hole in a mesh by doubling the nodes of some particular elements
5248   This method provided for convenience works as DoubleNodes() described above.
5249   \param theNodeId - identifier of node to be doubled.
5250   \param theModifiedElems - identifiers of elements to be updated.
5251   \return TRUE if operation has been completed successfully, FALSE otherwise
5252   \sa DoubleNodes(), DoubleNodeGroup(), DoubleNodeGroups()
5253 */
5254 //================================================================================
5255
5256 CORBA::Boolean SMESH_MeshEditor_i::DoubleNode( CORBA::Long              theNodeId,
5257                                                const SMESH::long_array& theModifiedElems )
5258   throw (SALOME::SALOME_Exception)
5259 {
5260   SMESH_TRY;
5261   SMESH::long_array_var aNodes = new SMESH::long_array;
5262   aNodes->length( 1 );
5263   aNodes[ 0 ] = theNodeId;
5264
5265   TPythonDump pyDump; // suppress dump by the next line
5266
5267   CORBA::Boolean done = DoubleNodes( aNodes, theModifiedElems );
5268
5269   pyDump << this << ".DoubleNode( " << theNodeId << ", " << theModifiedElems << " )";
5270
5271   return done;
5272
5273   SMESH_CATCH( SMESH::throwCorbaException );
5274   return 0;
5275 }
5276
5277 //================================================================================
5278 /*!
5279   \brief Creates a hole in a mesh by doubling the nodes of some particular elements
5280   This method provided for convenience works as DoubleNodes() described above.
5281   \param theNodes - group of nodes to be doubled.
5282   \param theModifiedElems - group of elements to be updated.
5283   \return TRUE if operation has been completed successfully, FALSE otherwise
5284   \sa DoubleNode(), DoubleNodes(), DoubleNodeGroups()
5285 */
5286 //================================================================================
5287
5288 CORBA::Boolean SMESH_MeshEditor_i::DoubleNodeGroup(SMESH::SMESH_GroupBase_ptr theNodes,
5289                                                    SMESH::SMESH_GroupBase_ptr theModifiedElems )
5290   throw (SALOME::SALOME_Exception)
5291 {
5292   SMESH_TRY;
5293   if ( CORBA::is_nil( theNodes ) && theNodes->GetType() != SMESH::NODE )
5294     return false;
5295
5296   SMESH::long_array_var aNodes = theNodes->GetListOfID();
5297   SMESH::long_array_var aModifiedElems;
5298   if ( !CORBA::is_nil( theModifiedElems ) )
5299     aModifiedElems = theModifiedElems->GetListOfID();
5300   else
5301   {
5302     aModifiedElems = new SMESH::long_array;
5303     aModifiedElems->length( 0 );
5304   }
5305
5306   TPythonDump pyDump; // suppress dump by the next line
5307
5308   bool done = DoubleNodes( aNodes, aModifiedElems );
5309
5310   pyDump << this << ".DoubleNodeGroup( " << theNodes << ", " << theModifiedElems << " )";
5311
5312   return done;
5313
5314   SMESH_CATCH( SMESH::throwCorbaException );
5315   return 0;
5316 }
5317
5318 //================================================================================
5319 /*!
5320  * \brief Creates a hole in a mesh by doubling the nodes of some particular elements.
5321  * Works as DoubleNodeGroup(), but returns a new group with newly created nodes.
5322  * \param theNodes - group of nodes to be doubled.
5323  * \param theModifiedElems - group of elements to be updated.
5324  * \return a new group with newly created nodes
5325  * \sa DoubleNodeGroup()
5326  */
5327 //================================================================================
5328
5329 SMESH::SMESH_Group_ptr
5330 SMESH_MeshEditor_i::DoubleNodeGroupNew( SMESH::SMESH_GroupBase_ptr theNodes,
5331                                         SMESH::SMESH_GroupBase_ptr theModifiedElems )
5332   throw (SALOME::SALOME_Exception)
5333 {
5334   SMESH_TRY;
5335   SMESH::SMESH_Group_var aNewGroup;
5336
5337   if ( CORBA::is_nil( theNodes ) && theNodes->GetType() != SMESH::NODE )
5338     return aNewGroup._retn();
5339
5340   // Duplicate nodes
5341   SMESH::long_array_var aNodes = theNodes->GetListOfID();
5342   SMESH::long_array_var aModifiedElems;
5343   if ( !CORBA::is_nil( theModifiedElems ) )
5344     aModifiedElems = theModifiedElems->GetListOfID();
5345   else {
5346     aModifiedElems = new SMESH::long_array;
5347     aModifiedElems->length( 0 );
5348   }
5349
5350   TPythonDump pyDump; // suppress dump by the next line
5351
5352   bool aResult = DoubleNodes( aNodes, aModifiedElems );
5353   if ( aResult )
5354   {
5355     // Create group with newly created nodes
5356     SMESH::long_array_var anIds = GetLastCreatedNodes();
5357     if (anIds->length() > 0) {
5358       string anUnindexedName (theNodes->GetName());
5359       string aNewName = generateGroupName(anUnindexedName + "_double");
5360       aNewGroup = myMesh_i->CreateGroup(SMESH::NODE, aNewName.c_str());
5361       aNewGroup->Add(anIds);
5362       pyDump << aNewGroup << " = ";
5363     }
5364   }
5365
5366   pyDump << this << ".DoubleNodeGroupNew( " << theNodes << ", "
5367          << theModifiedElems << " )";
5368
5369   return aNewGroup._retn();
5370
5371   SMESH_CATCH( SMESH::throwCorbaException );
5372   return 0;
5373 }
5374
5375 //================================================================================
5376 /*!
5377   \brief Creates a hole in a mesh by doubling the nodes of some particular elements
5378   This method provided for convenience works as DoubleNodes() described above.
5379   \param theNodes - list of groups of nodes to be doubled
5380   \param theModifiedElems - list of groups of elements to be updated.
5381   \return TRUE if operation has been completed successfully, FALSE otherwise
5382   \sa DoubleNode(), DoubleNodeGroup(), DoubleNodes()
5383 */
5384 //================================================================================
5385
5386 CORBA::Boolean SMESH_MeshEditor_i::DoubleNodeGroups(const SMESH::ListOfGroups& theNodes,
5387                                                     const SMESH::ListOfGroups& theModifiedElems )
5388   throw (SALOME::SALOME_Exception)
5389 {
5390   SMESH_TRY;
5391   initData();
5392
5393   std::list< int > aNodes;
5394   int i, n, j, m;
5395   for ( i = 0, n = theNodes.length(); i < n; i++ )
5396   {
5397     SMESH::SMESH_GroupBase_var aGrp = theNodes[ i ];
5398     if ( !CORBA::is_nil( aGrp ) && aGrp->GetType() == SMESH::NODE )
5399     {
5400       SMESH::long_array_var aCurr = aGrp->GetListOfID();
5401       for ( j = 0, m = aCurr->length(); j < m; j++ )
5402         aNodes.push_back( aCurr[ j ] );
5403     }
5404   }
5405
5406   std::list< int > anElems;
5407   for ( i = 0, n = theModifiedElems.length(); i < n; i++ )
5408   {
5409     SMESH::SMESH_GroupBase_var aGrp = theModifiedElems[ i ];
5410     if ( !CORBA::is_nil( aGrp ) && aGrp->GetType() != SMESH::NODE )
5411     {
5412       SMESH::long_array_var aCurr = aGrp->GetListOfID();
5413       for ( j = 0, m = aCurr->length(); j < m; j++ )
5414         anElems.push_back( aCurr[ j ] );
5415     }
5416   }
5417
5418   bool aResult = getEditor().DoubleNodes( aNodes, anElems );
5419
5420   declareMeshModified( /*isReComputeSafe=*/false );
5421
5422   TPythonDump() << this << ".DoubleNodeGroups( " << theNodes << ", " << theModifiedElems << " )";
5423
5424   return aResult;
5425
5426   SMESH_CATCH( SMESH::throwCorbaException );
5427   return 0;
5428 }
5429
5430 //================================================================================
5431 /*!
5432  * \brief Creates a hole in a mesh by doubling the nodes of some particular elements.
5433  * Works as DoubleNodeGroups(), but returns a new group with newly created nodes.
5434  * \param theNodes - group of nodes to be doubled.
5435  * \param theModifiedElems - group of elements to be updated.
5436  * \return a new group with newly created nodes
5437  * \sa DoubleNodeGroups()
5438  */
5439 //================================================================================
5440
5441 SMESH::SMESH_Group_ptr
5442 SMESH_MeshEditor_i::DoubleNodeGroupsNew( const SMESH::ListOfGroups& theNodes,
5443                                          const SMESH::ListOfGroups& theModifiedElems )
5444   throw (SALOME::SALOME_Exception)
5445 {
5446   SMESH::SMESH_Group_var aNewGroup;
5447
5448   TPythonDump pyDump; // suppress dump by the next line
5449
5450   bool aResult = DoubleNodeGroups( theNodes, theModifiedElems );
5451
5452   if ( aResult )
5453   {
5454     // Create group with newly created nodes
5455     SMESH::long_array_var anIds = GetLastCreatedNodes();
5456     if (anIds->length() > 0) {
5457       string anUnindexedName (theNodes[0]->GetName());
5458       string aNewName = generateGroupName(anUnindexedName + "_double");
5459       aNewGroup = myMesh_i->CreateGroup(SMESH::NODE, aNewName.c_str());
5460       aNewGroup->Add(anIds);
5461       pyDump << aNewGroup << " = ";
5462     }
5463   }
5464
5465   pyDump << this << ".DoubleNodeGroupsNew( " << theNodes << ", "
5466          << theModifiedElems << " )";
5467
5468   return aNewGroup._retn();
5469 }
5470
5471
5472 //================================================================================
5473 /*!
5474   \brief Creates a hole in a mesh by doubling the nodes of some particular elements
5475   \param theElems - the list of elements (edges or faces) to be replicated
5476   The nodes for duplication could be found from these elements
5477   \param theNodesNot - list of nodes to NOT replicate
5478   \param theAffectedElems - the list of elements (cells and edges) to which the
5479   replicated nodes should be associated to.
5480   \return TRUE if operation has been completed successfully, FALSE otherwise
5481   \sa DoubleNodeGroup(), DoubleNodeGroups()
5482 */
5483 //================================================================================
5484
5485 CORBA::Boolean SMESH_MeshEditor_i::DoubleNodeElem( const SMESH::long_array& theElems,
5486                                                    const SMESH::long_array& theNodesNot,
5487                                                    const SMESH::long_array& theAffectedElems )
5488   throw (SALOME::SALOME_Exception)
5489 {
5490   SMESH_TRY;
5491   initData();
5492
5493   SMESHDS_Mesh* aMeshDS = getMeshDS();
5494   TIDSortedElemSet anElems, aNodes, anAffected;
5495   arrayToSet(theElems, aMeshDS, anElems, SMDSAbs_All);
5496   arrayToSet(theNodesNot, aMeshDS, aNodes, SMDSAbs_Node);
5497   arrayToSet(theAffectedElems, aMeshDS, anAffected, SMDSAbs_All);
5498
5499   bool aResult = getEditor().DoubleNodes( anElems, aNodes, anAffected );
5500
5501   // Update Python script
5502   TPythonDump() << this << ".DoubleNodeElem( " << theElems << ", "
5503                 << theNodesNot << ", " << theAffectedElems << " )";
5504
5505   declareMeshModified( /*isReComputeSafe=*/false );
5506   return aResult;
5507
5508   SMESH_CATCH( SMESH::throwCorbaException );
5509   return 0;
5510 }
5511
5512 //================================================================================
5513 /*!
5514   \brief Creates a hole in a mesh by doubling the nodes of some particular elements
5515   \param theElems - the list of elements (edges or faces) to be replicated
5516   The nodes for duplication could be found from these elements
5517   \param theNodesNot - list of nodes to NOT replicate
5518   \param theShape - shape to detect affected elements (element which geometric center
5519   located on or inside shape).
5520   The replicated nodes should be associated to affected elements.
5521   \return TRUE if operation has been completed successfully, FALSE otherwise
5522   \sa DoubleNodeGroupInRegion(), DoubleNodeGroupsInRegion()
5523 */
5524 //================================================================================
5525
5526 CORBA::Boolean SMESH_MeshEditor_i::DoubleNodeElemInRegion ( const SMESH::long_array& theElems,
5527                                                             const SMESH::long_array& theNodesNot,
5528                                                             GEOM::GEOM_Object_ptr    theShape )
5529   throw (SALOME::SALOME_Exception)
5530 {
5531   SMESH_TRY;
5532   initData();
5533
5534
5535   SMESHDS_Mesh* aMeshDS = getMeshDS();
5536   TIDSortedElemSet anElems, aNodes;
5537   arrayToSet(theElems, aMeshDS, anElems, SMDSAbs_All);
5538   arrayToSet(theNodesNot, aMeshDS, aNodes, SMDSAbs_Node);
5539
5540   TopoDS_Shape aShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( theShape );
5541   bool aResult = getEditor().DoubleNodesInRegion( anElems, aNodes, aShape );
5542
5543   // Update Python script
5544   TPythonDump() << "isDone = " << this << ".DoubleNodeElemInRegion( " << theElems << ", "
5545                 << theNodesNot << ", " << theShape << " )";
5546
5547   declareMeshModified( /*isReComputeSafe=*/false );
5548   return aResult;
5549
5550   SMESH_CATCH( SMESH::throwCorbaException );
5551   return 0;
5552 }
5553
5554 //================================================================================
5555 /*!
5556   \brief Creates a hole in a mesh by doubling the nodes of some particular elements
5557   \param theElems - group of of elements (edges or faces) to be replicated
5558   \param theNodesNot - group of nodes not to replicated
5559   \param theAffectedElems - group of elements to which the replicated nodes
5560   should be associated to.
5561   \return TRUE if operation has been completed successfully, FALSE otherwise
5562   \sa DoubleNodes(), DoubleNodeGroups()
5563 */
5564 //================================================================================
5565
5566 CORBA::Boolean
5567 SMESH_MeshEditor_i::DoubleNodeElemGroup(SMESH::SMESH_GroupBase_ptr theElems,
5568                                         SMESH::SMESH_GroupBase_ptr theNodesNot,
5569                                         SMESH::SMESH_GroupBase_ptr theAffectedElems)
5570   throw (SALOME::SALOME_Exception)
5571 {
5572   SMESH_TRY;
5573   if ( CORBA::is_nil( theElems ) && theElems->GetType() == SMESH::NODE )
5574     return false;
5575
5576   initData();
5577
5578
5579   SMESHDS_Mesh* aMeshDS = getMeshDS();
5580   TIDSortedElemSet anElems, aNodes, anAffected;
5581   idSourceToSet( theElems, aMeshDS, anElems, SMDSAbs_All );
5582   idSourceToSet( theNodesNot, aMeshDS, aNodes, SMDSAbs_Node );
5583   idSourceToSet( theAffectedElems, aMeshDS, anAffected, SMDSAbs_All );
5584
5585   bool aResult = getEditor().DoubleNodes( anElems, aNodes, anAffected );
5586
5587   // Update Python script
5588   TPythonDump() << "isDone = " << this << ".DoubleNodeElemGroup( " << theElems << ", "
5589                 << theNodesNot << ", " << theAffectedElems << " )";
5590
5591   declareMeshModified( /*isReComputeSafe=*/false );
5592   return aResult;
5593
5594   SMESH_CATCH( SMESH::throwCorbaException );
5595   return 0;
5596 }
5597
5598 //================================================================================
5599 /*!
5600  * \brief Creates a hole in a mesh by doubling the nodes of some particular elements
5601  * Works as DoubleNodeElemGroup(), but returns a new group with newly created elements.
5602  * \param theElems - group of of elements (edges or faces) to be replicated
5603  * \param theNodesNot - group of nodes not to replicated
5604  * \param theAffectedElems - group of elements to which the replicated nodes
5605  *        should be associated to.
5606  * \return a new group with newly created elements
5607  * \sa DoubleNodeElemGroup()
5608  */
5609 //================================================================================
5610
5611 SMESH::SMESH_Group_ptr
5612 SMESH_MeshEditor_i::DoubleNodeElemGroupNew(SMESH::SMESH_GroupBase_ptr theElems,
5613                                            SMESH::SMESH_GroupBase_ptr theNodesNot,
5614                                            SMESH::SMESH_GroupBase_ptr theAffectedElems)
5615   throw (SALOME::SALOME_Exception)
5616 {
5617   TPythonDump pyDump;
5618   SMESH::ListOfGroups_var twoGroups = DoubleNodeElemGroup2New( theElems,
5619                                                                theNodesNot,
5620                                                                theAffectedElems,
5621                                                                true, false );
5622   SMESH::SMESH_GroupBase_var baseGroup = twoGroups[0].in();
5623   SMESH::SMESH_Group_var     elemGroup = SMESH::SMESH_Group::_narrow( baseGroup );
5624
5625   pyDump << elemGroup << " = " << this << ".DoubleNodeElemGroupNew( "
5626          << theElems         << ", "
5627          << theNodesNot      << ", "
5628          << theAffectedElems << " )";
5629
5630   return elemGroup._retn();
5631 }
5632
5633 //================================================================================
5634 /*!
5635  * \brief Creates a hole in a mesh by doubling the nodes of some particular elements
5636  * Works as DoubleNodeElemGroup(), but returns a new group with newly created elements.
5637  * \param theElems - group of of elements (edges or faces) to be replicated
5638  * \param theNodesNot - group of nodes not to replicated
5639  * \param theAffectedElems - group of elements to which the replicated nodes
5640  *        should be associated to.
5641  * \return a new group with newly created elements
5642  * \sa DoubleNodeElemGroup()
5643  */
5644 //================================================================================
5645
5646 SMESH::ListOfGroups*
5647 SMESH_MeshEditor_i::DoubleNodeElemGroup2New(SMESH::SMESH_GroupBase_ptr theElems,
5648                                             SMESH::SMESH_GroupBase_ptr theNodesNot,
5649                                             SMESH::SMESH_GroupBase_ptr theAffectedElems,
5650                                             CORBA::Boolean             theElemGroupNeeded,
5651                                             CORBA::Boolean             theNodeGroupNeeded)
5652   throw (SALOME::SALOME_Exception)
5653 {
5654   SMESH_TRY;
5655   SMESH::SMESH_Group_var aNewElemGroup, aNewNodeGroup;
5656   SMESH::ListOfGroups_var aTwoGroups = new SMESH::ListOfGroups();
5657   aTwoGroups->length( 2 );
5658
5659   if ( CORBA::is_nil( theElems ) && theElems->GetType() == SMESH::NODE )
5660     return aTwoGroups._retn();
5661
5662   initData();
5663
5664
5665   SMESHDS_Mesh* aMeshDS = getMeshDS();
5666   TIDSortedElemSet anElems, aNodes, anAffected;
5667   idSourceToSet( theElems, aMeshDS, anElems, SMDSAbs_All );
5668   idSourceToSet( theNodesNot, aMeshDS, aNodes, SMDSAbs_Node );
5669   idSourceToSet( theAffectedElems, aMeshDS, anAffected, SMDSAbs_All );
5670
5671
5672   bool aResult = getEditor().DoubleNodes( anElems, aNodes, anAffected );
5673
5674   declareMeshModified( /*isReComputeSafe=*/ !aResult );
5675
5676   TPythonDump pyDump;
5677
5678   if ( aResult )
5679   {
5680     // Create group with newly created elements
5681     CORBA::String_var elemGroupName = theElems->GetName();
5682     string aNewName = generateGroupName( string(elemGroupName.in()) + "_double");
5683     if ( !getEditor().GetLastCreatedElems().IsEmpty() && theElemGroupNeeded )
5684     {
5685       SMESH::long_array_var anIds = GetLastCreatedElems();
5686       SMESH::ElementType aGroupType = myMesh_i->GetElementType(anIds[0], true);
5687       aNewElemGroup = myMesh_i->CreateGroup(aGroupType, aNewName.c_str());
5688       aNewElemGroup->Add(anIds);
5689     }
5690     if ( !getEditor().GetLastCreatedNodes().IsEmpty() && theNodeGroupNeeded )
5691     {
5692       SMESH::long_array_var anIds = GetLastCreatedNodes();
5693       aNewNodeGroup = myMesh_i->CreateGroup(SMESH::NODE, aNewName.c_str());
5694       aNewNodeGroup->Add(anIds);
5695     }
5696   }
5697
5698   // Update Python script
5699
5700   pyDump << "[ ";
5701   if ( aNewElemGroup->_is_nil() ) pyDump << "nothing, ";
5702   else                            pyDump << aNewElemGroup << ", ";
5703   if ( aNewNodeGroup->_is_nil() ) pyDump << "nothing ] = ";
5704   else                            pyDump << aNewNodeGroup << " ] = ";
5705
5706   pyDump << this << ".DoubleNodeElemGroup2New( " << theElems << ", "
5707          << theNodesNot        << ", "
5708          << theAffectedElems   << ", "
5709          << theElemGroupNeeded << ", "
5710          << theNodeGroupNeeded <<" )";
5711
5712   aTwoGroups[0] = aNewElemGroup._retn();
5713   aTwoGroups[1] = aNewNodeGroup._retn();
5714   return aTwoGroups._retn();
5715
5716   SMESH_CATCH( SMESH::throwCorbaException );
5717   return 0;
5718 }
5719
5720 //================================================================================
5721 /*!
5722   \brief Creates a hole in a mesh by doubling the nodes of some particular elements
5723   \param theElems - group of of elements (edges or faces) to be replicated
5724   \param theNodesNot - group of nodes not to replicated
5725   \param theShape - shape to detect affected elements (element which geometric center
5726   located on or inside shape).
5727   The replicated nodes should be associated to affected elements.
5728   \return TRUE if operation has been completed successfully, FALSE otherwise
5729   \sa DoubleNodesInRegion(), DoubleNodeGroupsInRegion()
5730 */
5731 //================================================================================
5732
5733 CORBA::Boolean
5734 SMESH_MeshEditor_i::DoubleNodeElemGroupInRegion(SMESH::SMESH_GroupBase_ptr theElems,
5735                                                 SMESH::SMESH_GroupBase_ptr theNodesNot,
5736                                                 GEOM::GEOM_Object_ptr      theShape )
5737   throw (SALOME::SALOME_Exception)
5738 {
5739   SMESH_TRY;
5740   if ( CORBA::is_nil( theElems ) && theElems->GetType() == SMESH::NODE )
5741     return false;
5742
5743   initData();
5744
5745
5746   SMESHDS_Mesh* aMeshDS = getMeshDS();
5747   TIDSortedElemSet anElems, aNodes, anAffected;
5748   idSourceToSet( theElems, aMeshDS, anElems, SMDSAbs_All );
5749   idSourceToSet( theNodesNot, aMeshDS, aNodes, SMDSAbs_Node );
5750
5751   TopoDS_Shape aShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( theShape );
5752   bool aResult = getEditor().DoubleNodesInRegion( anElems, aNodes, aShape );
5753
5754
5755   declareMeshModified( /*isReComputeSafe=*/ !aResult );
5756
5757   // Update Python script
5758   TPythonDump() << "isDone = " << this << ".DoubleNodeElemGroupInRegion( " << theElems << ", "
5759                 << theNodesNot << ", " << theShape << " )";
5760   return aResult;
5761
5762   SMESH_CATCH( SMESH::throwCorbaException );
5763   return 0;
5764 }
5765
5766 //================================================================================
5767 /*!
5768  * \brief Re-load elements from a list of groups into a TIDSortedElemSet
5769  *  \param [in] theGrpList - groups
5770  *  \param [in] theMeshDS -  mesh
5771  *  \param [out] theElemSet - set of elements
5772  *  \param [in] theIsNodeGrp - is \a theGrpList includes goups of nodes
5773  */
5774 //================================================================================
5775
5776 static void listOfGroupToSet(const SMESH::ListOfGroups& theGrpList,
5777                              SMESHDS_Mesh*              theMeshDS,
5778                              TIDSortedElemSet&          theElemSet,
5779                              const bool                 theIsNodeGrp)
5780 {
5781   for ( int i = 0, n = theGrpList.length(); i < n; i++ )
5782   {
5783     SMESH::SMESH_GroupBase_var aGrp = theGrpList[ i ];
5784     if ( !CORBA::is_nil( aGrp ) && (theIsNodeGrp ? aGrp->GetType() == SMESH::NODE
5785                                     : aGrp->GetType() != SMESH::NODE ) )
5786     {
5787       SMESH::long_array_var anIDs = aGrp->GetIDs();
5788       arrayToSet( anIDs, theMeshDS, theElemSet, theIsNodeGrp ? SMDSAbs_Node : SMDSAbs_All );
5789     }
5790   }
5791 }
5792
5793 //================================================================================
5794 /*!
5795   \brief Creates a hole in a mesh by doubling the nodes of some particular elements.
5796   This method provided for convenience works as DoubleNodes() described above.
5797   \param theElems - list of groups of elements (edges or faces) to be replicated
5798   \param theNodesNot - list of groups of nodes not to replicated
5799   \param theAffectedElems - group of elements to which the replicated nodes
5800   should be associated to.
5801   \return TRUE if operation has been completed successfully, FALSE otherwise
5802   \sa DoubleNodeGroup(), DoubleNodes(), DoubleNodeElemGroupsNew()
5803 */
5804 //================================================================================
5805
5806 CORBA::Boolean
5807 SMESH_MeshEditor_i::DoubleNodeElemGroups(const SMESH::ListOfGroups& theElems,
5808                                          const SMESH::ListOfGroups& theNodesNot,
5809                                          const SMESH::ListOfGroups& theAffectedElems)
5810   throw (SALOME::SALOME_Exception)
5811 {
5812   SMESH_TRY;
5813   initData();
5814
5815
5816   SMESHDS_Mesh* aMeshDS = getMeshDS();
5817   TIDSortedElemSet anElems, aNodes, anAffected;
5818   listOfGroupToSet(theElems, aMeshDS, anElems, false );
5819   listOfGroupToSet(theNodesNot, aMeshDS, aNodes, true );
5820   listOfGroupToSet(theAffectedElems, aMeshDS, anAffected, false );
5821
5822   bool aResult = getEditor().DoubleNodes( anElems, aNodes, anAffected );
5823
5824   // Update Python script
5825   TPythonDump() << "isDone = " << this << ".DoubleNodeElemGroups( " << &theElems << ", "
5826                 << &theNodesNot << ", " << &theAffectedElems << " )";
5827
5828   declareMeshModified( /*isReComputeSafe=*/false );
5829   return aResult;
5830
5831   SMESH_CATCH( SMESH::throwCorbaException );
5832   return 0;
5833 }
5834
5835 //================================================================================
5836 /*!
5837  * \brief Creates a hole in a mesh by doubling the nodes of some particular elements
5838  * Works as DoubleNodeElemGroups(), but returns a new group with newly created elements.
5839   \param theElems - list of groups of elements (edges or faces) to be replicated
5840   \param theNodesNot - list of groups of nodes not to replicated
5841   \param theAffectedElems - group of elements to which the replicated nodes
5842   should be associated to.
5843  * \return a new group with newly created elements
5844  * \sa DoubleNodeElemGroups()
5845  */
5846 //================================================================================
5847
5848 SMESH::SMESH_Group_ptr
5849 SMESH_MeshEditor_i::DoubleNodeElemGroupsNew(const SMESH::ListOfGroups& theElems,
5850                                             const SMESH::ListOfGroups& theNodesNot,
5851                                             const SMESH::ListOfGroups& theAffectedElems)
5852   throw (SALOME::SALOME_Exception)
5853 {
5854   TPythonDump pyDump;
5855   SMESH::ListOfGroups_var twoGroups = DoubleNodeElemGroups2New( theElems,
5856                                                                 theNodesNot,
5857                                                                 theAffectedElems,
5858                                                                 true, false );
5859   SMESH::SMESH_GroupBase_var baseGroup = twoGroups[0].in();
5860   SMESH::SMESH_Group_var     elemGroup = SMESH::SMESH_Group::_narrow( baseGroup );
5861
5862   pyDump << elemGroup << " = " << this << ".DoubleNodeElemGroupsNew( "
5863          << theElems         << ", "
5864          << theNodesNot      << ", "
5865          << theAffectedElems << " )";
5866
5867   return elemGroup._retn();
5868 }
5869
5870 //================================================================================
5871 /*!
5872  * \brief Creates a hole in a mesh by doubling the nodes of some particular elements
5873  * Works as DoubleNodeElemGroups(), but returns a new group with newly created elements.
5874   \param theElems - list of groups of elements (edges or faces) to be replicated
5875   \param theNodesNot - list of groups of nodes not to replicated
5876   \param theAffectedElems - group of elements to which the replicated nodes
5877   should be associated to.
5878  * \return a new group with newly created elements
5879  * \sa DoubleNodeElemGroups()
5880  */
5881 //================================================================================
5882
5883 SMESH::ListOfGroups*
5884 SMESH_MeshEditor_i::DoubleNodeElemGroups2New(const SMESH::ListOfGroups& theElems,
5885                                              const SMESH::ListOfGroups& theNodesNot,
5886                                              const SMESH::ListOfGroups& theAffectedElems,
5887                                              CORBA::Boolean             theElemGroupNeeded,
5888                                              CORBA::Boolean             theNodeGroupNeeded)
5889   throw (SALOME::SALOME_Exception)
5890 {
5891   SMESH_TRY;
5892   SMESH::SMESH_Group_var aNewElemGroup, aNewNodeGroup;
5893   SMESH::ListOfGroups_var aTwoGroups = new SMESH::ListOfGroups();
5894   aTwoGroups->length( 2 );
5895   
5896   initData();
5897
5898
5899   SMESHDS_Mesh* aMeshDS = getMeshDS();
5900   TIDSortedElemSet anElems, aNodes, anAffected;
5901   listOfGroupToSet(theElems, aMeshDS, anElems, false );
5902   listOfGroupToSet(theNodesNot, aMeshDS, aNodes, true );
5903   listOfGroupToSet(theAffectedElems, aMeshDS, anAffected, false );
5904
5905   bool aResult = getEditor().DoubleNodes( anElems, aNodes, anAffected );
5906
5907   declareMeshModified( /*isReComputeSafe=*/ !aResult );
5908
5909   TPythonDump pyDump;
5910   if ( aResult )
5911   {
5912     // Create group with newly created elements
5913     CORBA::String_var elemGroupName = theElems[0]->GetName();
5914     string aNewName = generateGroupName( string(elemGroupName.in()) + "_double");
5915     if ( !getEditor().GetLastCreatedElems().IsEmpty() && theElemGroupNeeded )
5916     {
5917       SMESH::long_array_var anIds = GetLastCreatedElems();
5918       SMESH::ElementType aGroupType = myMesh_i->GetElementType(anIds[0], true);
5919       aNewElemGroup = myMesh_i->CreateGroup(aGroupType, aNewName.c_str());
5920       aNewElemGroup->Add(anIds);
5921     }
5922     if ( !getEditor().GetLastCreatedNodes().IsEmpty() && theNodeGroupNeeded )
5923     {
5924       SMESH::long_array_var anIds = GetLastCreatedNodes();
5925       aNewNodeGroup = myMesh_i->CreateGroup(SMESH::NODE, aNewName.c_str());
5926       aNewNodeGroup->Add(anIds);
5927     }
5928   }
5929
5930   // Update Python script
5931
5932   pyDump << "[ ";
5933   if ( aNewElemGroup->_is_nil() ) pyDump << "nothing, ";
5934   else                            pyDump << aNewElemGroup << ", ";
5935   if ( aNewNodeGroup->_is_nil() ) pyDump << "nothing ] = ";
5936   else                            pyDump << aNewNodeGroup << " ] = ";
5937
5938   pyDump << this << ".DoubleNodeElemGroups2New( " << &theElems << ", "
5939          << &theNodesNot       << ", "
5940          << &theAffectedElems  << ", "
5941          << theElemGroupNeeded << ", "
5942          << theNodeGroupNeeded << " )";
5943
5944   aTwoGroups[0] = aNewElemGroup._retn();
5945   aTwoGroups[1] = aNewNodeGroup._retn();
5946   return aTwoGroups._retn();
5947
5948   SMESH_CATCH( SMESH::throwCorbaException );
5949   return 0;
5950 }
5951
5952 //================================================================================
5953 /*!
5954   \brief Creates a hole in a mesh by doubling the nodes of some particular elements
5955   This method provided for convenience works as DoubleNodes() described above.
5956   \param theElems - list of groups of elements (edges or faces) to be replicated
5957   \param theNodesNot - list of groups of nodes not to replicated
5958   \param theShape - shape to detect affected elements (element which geometric center
5959   located on or inside shape).
5960   The replicated nodes should be associated to affected elements.
5961   \return TRUE if operation has been completed successfully, FALSE otherwise
5962   \sa DoubleNodeGroupInRegion(), DoubleNodesInRegion()
5963 */
5964 //================================================================================
5965
5966 CORBA::Boolean
5967 SMESH_MeshEditor_i::DoubleNodeElemGroupsInRegion(const SMESH::ListOfGroups& theElems,
5968                                                  const SMESH::ListOfGroups& theNodesNot,
5969                                                  GEOM::GEOM_Object_ptr      theShape )
5970   throw (SALOME::SALOME_Exception)
5971 {
5972   SMESH_TRY;
5973   initData();
5974
5975
5976   SMESHDS_Mesh* aMeshDS = getMeshDS();
5977   TIDSortedElemSet anElems, aNodes;
5978   listOfGroupToSet(theElems, aMeshDS, anElems,false );
5979   listOfGroupToSet(theNodesNot, aMeshDS, aNodes, true );
5980
5981   TopoDS_Shape aShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( theShape );
5982   bool aResult = getEditor().DoubleNodesInRegion( anElems, aNodes, aShape );
5983
5984   // Update Python script
5985   TPythonDump() << "isDone = " << this << ".DoubleNodeElemGroupsInRegion( " << &theElems << ", "
5986                 << &theNodesNot << ", " << theShape << " )";
5987
5988   declareMeshModified( /*isReComputeSafe=*/ !aResult );
5989   return aResult;
5990
5991   SMESH_CATCH( SMESH::throwCorbaException );
5992   return 0;
5993 }
5994
5995 //================================================================================
5996 /*!
5997   \brief Identify the elements that will be affected by node duplication (actual
5998          duplication is not performed.
5999   This method is the first step of DoubleNodeElemGroupsInRegion.
6000   \param theElems - list of groups of elements (edges or faces) to be replicated
6001   \param theNodesNot - list of groups of nodes not to replicated
6002   \param theShape - shape to detect affected elements (element which geometric center
6003          located on or inside shape).
6004          The replicated nodes should be associated to affected elements.
6005   \return groups of affected elements
6006   \sa DoubleNodeElemGroupsInRegion()
6007 */
6008 //================================================================================
6009 SMESH::ListOfGroups*
6010 SMESH_MeshEditor_i::AffectedElemGroupsInRegion( const SMESH::ListOfGroups& theElems,
6011                                                 const SMESH::ListOfGroups& theNodesNot,
6012                                                 GEOM::GEOM_Object_ptr      theShape )
6013   throw (SALOME::SALOME_Exception)
6014 {
6015   SMESH_TRY;
6016   MESSAGE("AffectedElemGroupsInRegion");
6017   SMESH::ListOfGroups_var aListOfGroups = new SMESH::ListOfGroups();
6018   bool isEdgeGroup = false;
6019   bool isFaceGroup = false;
6020   bool isVolumeGroup = false;
6021   SMESH::SMESH_Group_var aNewEdgeGroup = myMesh_i->CreateGroup(SMESH::EDGE, "affectedEdges");
6022   SMESH::SMESH_Group_var aNewFaceGroup = myMesh_i->CreateGroup(SMESH::FACE, "affectedFaces");
6023   SMESH::SMESH_Group_var aNewVolumeGroup = myMesh_i->CreateGroup(SMESH::VOLUME, "affectedVolumes");
6024
6025   initData();
6026
6027   ::SMESH_MeshEditor aMeshEditor(myMesh);
6028
6029   SMESHDS_Mesh* aMeshDS = getMeshDS();
6030   TIDSortedElemSet anElems, aNodes;
6031   listOfGroupToSet(theElems, aMeshDS, anElems, false);
6032   listOfGroupToSet(theNodesNot, aMeshDS, aNodes, true);
6033
6034   TopoDS_Shape aShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape(theShape);
6035   TIDSortedElemSet anAffected;
6036   bool aResult = aMeshEditor.AffectedElemGroupsInRegion(anElems, aNodes, aShape, anAffected);
6037
6038
6039   declareMeshModified( /*isReComputeSafe=*/ !aResult );
6040
6041   TPythonDump pyDump;
6042   if (aResult)
6043   {
6044     int lg = anAffected.size();
6045     MESSAGE("lg="<< lg);
6046     SMESH::long_array_var volumeIds = new SMESH::long_array;
6047     volumeIds->length(lg);
6048     SMESH::long_array_var faceIds = new SMESH::long_array;
6049     faceIds->length(lg);
6050     SMESH::long_array_var edgeIds = new SMESH::long_array;
6051     edgeIds->length(lg);
6052     int ivol = 0;
6053     int iface = 0;
6054     int iedge = 0;
6055
6056     TIDSortedElemSet::const_iterator eIt = anAffected.begin();
6057     for (; eIt != anAffected.end(); ++eIt)
6058     {
6059       const SMDS_MeshElement* anElem = *eIt;
6060       if (!anElem)
6061         continue;
6062       int elemId = anElem->GetID();
6063       if (myMesh->GetElementType(elemId, true) == SMDSAbs_Volume)
6064         volumeIds[ivol++] = elemId;
6065       else if (myMesh->GetElementType(elemId, true) == SMDSAbs_Face)
6066         faceIds[iface++] = elemId;
6067       else if (myMesh->GetElementType(elemId, true) == SMDSAbs_Edge)
6068         edgeIds[iedge++] = elemId;
6069     }
6070     volumeIds->length(ivol);
6071     faceIds->length(iface);
6072     edgeIds->length(iedge);
6073
6074     aNewVolumeGroup->Add(volumeIds);
6075     aNewFaceGroup->Add(faceIds);
6076     aNewEdgeGroup->Add(edgeIds);
6077     isVolumeGroup = (aNewVolumeGroup->Size() > 0);
6078     isFaceGroup = (aNewFaceGroup->Size() > 0);
6079     isEdgeGroup = (aNewEdgeGroup->Size() > 0);
6080   }
6081
6082   int nbGroups = 0;
6083   if (isEdgeGroup)   nbGroups++;
6084   if (isFaceGroup)   nbGroups++;
6085   if (isVolumeGroup) nbGroups++;
6086   aListOfGroups->length(nbGroups);
6087
6088   int i = 0;
6089   if (isEdgeGroup)   aListOfGroups[i++] = aNewEdgeGroup._retn();
6090   if (isFaceGroup)   aListOfGroups[i++] = aNewFaceGroup._retn();
6091   if (isVolumeGroup) aListOfGroups[i++] = aNewVolumeGroup._retn();
6092
6093   // Update Python script
6094
6095   pyDump << "[ ";
6096   if (isEdgeGroup)   pyDump << aNewEdgeGroup << ", ";
6097   if (isFaceGroup)   pyDump << aNewFaceGroup << ", ";
6098   if (isVolumeGroup) pyDump << aNewVolumeGroup << ", ";
6099   pyDump << "] = ";
6100   pyDump << this << ".AffectedElemGroupsInRegion( "
6101          << &theElems << ", " << &theNodesNot << ", " << theShape << " )";
6102
6103   return aListOfGroups._retn();
6104
6105   SMESH_CATCH( SMESH::throwCorbaException );
6106   return 0;
6107 }
6108
6109 //================================================================================
6110 /*!
6111   \brief Generated skin mesh (containing 2D cells) from 3D mesh
6112    The created 2D mesh elements based on nodes of free faces of boundary volumes
6113   \return TRUE if operation has been completed successfully, FALSE otherwise
6114 */
6115 //================================================================================
6116
6117 CORBA::Boolean SMESH_MeshEditor_i::Make2DMeshFrom3D()
6118   throw (SALOME::SALOME_Exception)
6119 {
6120   SMESH_TRY;
6121   initData();
6122
6123   bool aResult = getEditor().Make2DMeshFrom3D();
6124
6125   TPythonDump() << "isDone = " << this << ".Make2DMeshFrom3D()";
6126
6127   declareMeshModified( /*isReComputeSafe=*/ !aResult );
6128   return aResult;
6129
6130   SMESH_CATCH( SMESH::throwCorbaException );
6131   return false;
6132 }
6133
6134 //================================================================================
6135 /*!
6136  * \brief Double nodes on shared faces between groups of volumes and create flat elements on demand.
6137  * The list of groups must contain at least two groups. The groups have to be disjoint:
6138  * no common element into two different groups.
6139  * The nodes of the internal faces at the boundaries of the groups are doubled.
6140  * Optionally, the internal faces are replaced by flat elements.
6141  * Triangles are transformed into prisms, and quadrangles into hexahedrons.
6142  * The flat elements are stored in groups of volumes.
6143  * These groups are named according to the position of the group in the list:
6144  * the group j_n_p is the group of the flat elements that are built between the group #n and the group #p in the list.
6145  * If there is no shared faces between the group #n and the group #p in the list, the group j_n_p is not created.
6146  * All the flat elements are gathered into the group named "joints3D" (or "joints2D" in 2D situation).
6147  * The flat element of the multiple junctions between the simple junction are stored in a group named "jointsMultiples".
6148  * \param theDomains - list of groups of volumes
6149  * \param createJointElems - if TRUE, create the elements
6150  * \param onAllBoundaries - if TRUE, the nodes and elements are also created on
6151  *        the boundary between \a theDomains and the rest mesh
6152  * \return TRUE if operation has been completed successfully, FALSE otherwise
6153  */
6154 //================================================================================
6155
6156 CORBA::Boolean
6157 SMESH_MeshEditor_i::DoubleNodesOnGroupBoundaries( const SMESH::ListOfGroups& theDomains,
6158                                                   CORBA::Boolean             createJointElems,
6159                                                   CORBA::Boolean             onAllBoundaries )
6160   throw (SALOME::SALOME_Exception)
6161 {
6162   bool isOK = false;
6163
6164   SMESH_TRY;
6165   initData();
6166
6167   SMESHDS_Mesh* aMeshDS = getMeshDS();
6168
6169   // MESSAGE("theDomains.length = "<<theDomains.length());
6170   if ( theDomains.length() <= 1 && !onAllBoundaries )
6171     THROW_SALOME_CORBA_EXCEPTION("At least 2 groups are required.", SALOME::BAD_PARAM);
6172
6173   vector<TIDSortedElemSet> domains;
6174   domains.resize( theDomains.length() );
6175
6176   for ( int i = 0, n = theDomains.length(); i < n; i++ )
6177   {
6178     SMESH::SMESH_GroupBase_var aGrp = theDomains[ i ];
6179     if ( !CORBA::is_nil( aGrp ) /*&& ( aGrp->GetType() != SMESH::NODE )*/ )
6180     {
6181 //      if ( aGrp->GetType() != SMESH::VOLUME )
6182 //        THROW_SALOME_CORBA_EXCEPTION("Not a volume group", SALOME::BAD_PARAM);
6183       SMESH::long_array_var anIDs = aGrp->GetIDs();
6184       arrayToSet( anIDs, aMeshDS, domains[ i ], SMDSAbs_All );
6185     }
6186   }
6187
6188   isOK = getEditor().DoubleNodesOnGroupBoundaries( domains, createJointElems, onAllBoundaries );
6189   // TODO publish the groups of flat elements in study
6190
6191   declareMeshModified( /*isReComputeSafe=*/ !isOK );
6192
6193   // Update Python script
6194   TPythonDump() << "isDone = " << this << ".DoubleNodesOnGroupBoundaries( " << &theDomains
6195                 << ", " << createJointElems << ", " << onAllBoundaries << " )";
6196
6197   SMESH_CATCH( SMESH::throwCorbaException );
6198
6199   myMesh_i->CreateGroupServants(); // publish created groups if any
6200
6201   return isOK;
6202 }
6203
6204 //================================================================================
6205 /*!
6206  * \brief Double nodes on some external faces and create flat elements.
6207  * Flat elements are mainly used by some types of mechanic calculations.
6208  *
6209  * Each group of the list must be constituted of faces.
6210  * Triangles are transformed in prisms, and quadrangles in hexahedrons.
6211  * @param theGroupsOfFaces - list of groups of faces
6212  * @return TRUE if operation has been completed successfully, FALSE otherwise
6213  */
6214 //================================================================================
6215
6216 CORBA::Boolean
6217 SMESH_MeshEditor_i::CreateFlatElementsOnFacesGroups( const SMESH::ListOfGroups& theGroupsOfFaces )
6218   throw (SALOME::SALOME_Exception)
6219 {
6220   SMESH_TRY;
6221   initData();
6222
6223   SMESHDS_Mesh* aMeshDS = getMeshDS();
6224
6225   vector<TIDSortedElemSet> faceGroups;
6226   faceGroups.clear();
6227
6228   for ( int i = 0, n = theGroupsOfFaces.length(); i < n; i++ )
6229   {
6230     SMESH::SMESH_GroupBase_var aGrp = theGroupsOfFaces[ i ];
6231     if ( !CORBA::is_nil( aGrp ) && ( aGrp->GetType() != SMESH::NODE ) )
6232     {
6233       TIDSortedElemSet faceGroup;
6234       faceGroup.clear();
6235       faceGroups.push_back(faceGroup);
6236       SMESH::long_array_var anIDs = aGrp->GetIDs();
6237       arrayToSet( anIDs, aMeshDS, faceGroups[ i ], SMDSAbs_All );
6238     }
6239   }
6240
6241   bool aResult = getEditor().CreateFlatElementsOnFacesGroups( faceGroups );
6242   // TODO publish the groups of flat elements in study
6243
6244   declareMeshModified( /*isReComputeSafe=*/ !aResult );
6245
6246   // Update Python script
6247   TPythonDump() << this << ".CreateFlatElementsOnFacesGroups( " << &theGroupsOfFaces << " )";
6248   return aResult;
6249
6250   SMESH_CATCH( SMESH::throwCorbaException );
6251   return false;
6252 }
6253
6254 //================================================================================
6255 /*!
6256  *  \brief Identify all the elements around a geom shape, get the faces delimiting
6257  *         the hole.
6258  *
6259  *  Build groups of volume to remove, groups of faces to replace on the skin of the
6260  *  object, groups of faces to remove inside the object, (idem edges).
6261  *  Build ordered list of nodes at the border of each group of faces to replace
6262  *  (to be used to build a geom subshape).
6263  */
6264 //================================================================================
6265
6266 void SMESH_MeshEditor_i::CreateHoleSkin(CORBA::Double                  radius,
6267                                         GEOM::GEOM_Object_ptr          theShape,
6268                                         const char*                    groupName,
6269                                         const SMESH::double_array&     theNodesCoords,
6270                                         SMESH::array_of_long_array_out GroupsOfNodes)
6271   throw (SALOME::SALOME_Exception)
6272 {
6273   SMESH_TRY;
6274
6275   initData();
6276   std::vector<std::vector<int> > aListOfListOfNodes;
6277   ::SMESH_MeshEditor aMeshEditor( myMesh );
6278
6279   theSearchersDeleter.Set( myMesh ); // remove theNodeSearcher if mesh is other
6280   if ( !theNodeSearcher )
6281     theNodeSearcher = SMESH_MeshAlgos::GetNodeSearcher( *getMeshDS() );
6282
6283   vector<double> nodesCoords;
6284   for (int i = 0; i < theNodesCoords.length(); i++)
6285   {
6286     nodesCoords.push_back( theNodesCoords[i] );
6287   }
6288
6289   TopoDS_Shape aShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( theShape );
6290   aMeshEditor.CreateHoleSkin(radius, aShape, theNodeSearcher, groupName,
6291                              nodesCoords, aListOfListOfNodes);
6292
6293   GroupsOfNodes = new SMESH::array_of_long_array;
6294   GroupsOfNodes->length( aListOfListOfNodes.size() );
6295   std::vector<std::vector<int> >::iterator llIt = aListOfListOfNodes.begin();
6296   for ( CORBA::Long i = 0; llIt != aListOfListOfNodes.end(); llIt++, i++ )
6297   {
6298     vector<int>& aListOfNodes = *llIt;
6299     vector<int>::iterator lIt = aListOfNodes.begin();;
6300     SMESH::long_array& aGroup = (*GroupsOfNodes)[ i ];
6301     aGroup.length( aListOfNodes.size() );
6302     for ( int j = 0; lIt != aListOfNodes.end(); lIt++, j++ )
6303       aGroup[ j ] = (*lIt);
6304   }
6305   TPythonDump() << "lists_nodes = " << this << ".CreateHoleSkin( "
6306                 << radius << ", "
6307                 << theShape
6308                 << ", '" << groupName << "', "
6309                 << theNodesCoords << " )";
6310
6311   SMESH_CATCH( SMESH::throwCorbaException );
6312 }
6313
6314 // issue 20749 ===================================================================
6315 /*!
6316  * \brief Creates missing boundary elements
6317  *  \param elements - elements whose boundary is to be checked
6318  *  \param dimension - defines type of boundary elements to create
6319  *  \param groupName - a name of group to store created boundary elements in,
6320  *                     "" means not to create the group
6321  *  \param meshName - a name of new mesh to store created boundary elements in,
6322  *                     "" means not to create the new mesh
6323  *  \param toCopyElements - if true, the checked elements will be copied into the new mesh
6324  *  \param toCopyExistingBondary - if true, not only new but also pre-existing
6325  *                                boundary elements will be copied into the new mesh
6326  *  \param group - returns the create group, if any
6327  *  \retval SMESH::SMESH_Mesh - the mesh where elements were added to
6328  */
6329 // ================================================================================
6330
6331 SMESH::SMESH_Mesh_ptr
6332 SMESH_MeshEditor_i::MakeBoundaryMesh(SMESH::SMESH_IDSource_ptr idSource,
6333                                      SMESH::Bnd_Dimension      dim,
6334                                      const char*               groupName,
6335                                      const char*               meshName,
6336                                      CORBA::Boolean            toCopyElements,
6337                                      CORBA::Boolean            toCopyExistingBondary,
6338                                      SMESH::SMESH_Group_out    group)
6339   throw (SALOME::SALOME_Exception)
6340 {
6341   SMESH_TRY;
6342   initData();
6343
6344   if ( dim > SMESH::BND_1DFROM2D )
6345     THROW_SALOME_CORBA_EXCEPTION("Invalid boundary dimension", SALOME::BAD_PARAM);
6346
6347   SMESHDS_Mesh* aMeshDS = getMeshDS();
6348
6349   SMESH::SMESH_Mesh_var mesh_var;
6350   SMESH::SMESH_Group_var group_var;
6351
6352   TPythonDump pyDump;
6353
6354   TIDSortedElemSet elements;
6355   SMDSAbs_ElementType elemType = (dim == SMESH::BND_1DFROM2D) ? SMDSAbs_Face : SMDSAbs_Volume;
6356   if ( idSourceToSet( idSource, aMeshDS, elements, elemType,/*emptyIfIsMesh=*/true ))
6357   {
6358     // mesh to fill in
6359     mesh_var =
6360       strlen(meshName) ? makeMesh(meshName) : SMESH::SMESH_Mesh::_duplicate(myMesh_i->_this());
6361     SMESH_Mesh_i* mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh_var );
6362     // other mesh
6363     SMESH_Mesh* smesh_mesh = (mesh_i==myMesh_i) ? (SMESH_Mesh*)0 : &mesh_i->GetImpl();
6364
6365     // group of new boundary elements
6366     SMESH_Group* smesh_group = 0;
6367     if ( strlen(groupName) )
6368     {
6369       group_var = mesh_i->CreateGroup( SMESH::ElementType(int(elemType)-1),groupName);
6370       if ( SMESH_GroupBase_i* group_i = SMESH::DownCast<SMESH_GroupBase_i*>( group_var ))
6371         smesh_group = group_i->GetSmeshGroup();
6372     }
6373
6374     // do it
6375     getEditor().MakeBoundaryMesh( elements,
6376                                   ::SMESH_MeshEditor::Bnd_Dimension(dim),
6377                                   smesh_group,
6378                                   smesh_mesh,
6379                                   toCopyElements,
6380                                   toCopyExistingBondary);
6381
6382     if ( smesh_mesh )
6383       smesh_mesh->GetMeshDS()->Modified();
6384   }
6385
6386   const char* dimName[] = { "BND_2DFROM3D", "BND_1DFROM3D", "BND_1DFROM2D" };
6387
6388   // result of MakeBoundaryMesh() is a tuple (mesh, group)
6389   if ( mesh_var->_is_nil() )
6390     pyDump << myMesh_i->_this() << ", ";
6391   else
6392     pyDump << mesh_var << ", ";
6393   if ( group_var->_is_nil() )
6394     pyDump << "_NoneGroup = "; // assignment to None is forbiden
6395   else
6396     pyDump << group_var << " = ";
6397   pyDump << this << ".MakeBoundaryMesh( "
6398          << idSource << ", "
6399          << "SMESH." << dimName[int(dim)] << ", "
6400          << "'" << groupName << "', "
6401          << "'" << meshName<< "', "
6402          << toCopyElements << ", "
6403          << toCopyExistingBondary << ")";
6404
6405   group = group_var._retn();
6406   return mesh_var._retn();
6407
6408   SMESH_CATCH( SMESH::throwCorbaException );
6409   return SMESH::SMESH_Mesh::_nil();
6410 }
6411
6412 //================================================================================
6413 /*!
6414  * \brief Creates missing boundary elements
6415  *  \param dimension - defines type of boundary elements to create
6416  *  \param groupName - a name of group to store all boundary elements in,
6417  *    "" means not to create the group
6418  *  \param meshName - a name of a new mesh, which is a copy of the initial 
6419  *    mesh + created boundary elements; "" means not to create the new mesh
6420  *  \param toCopyAll - if true, the whole initial mesh will be copied into
6421  *    the new mesh else only boundary elements will be copied into the new mesh
6422  *  \param groups - optional groups of elements to make boundary around
6423  *  \param mesh - returns the mesh where elements were added to
6424  *  \param group - returns the created group, if any
6425  *  \retval long - number of added boundary elements
6426  */
6427 //================================================================================
6428
6429 CORBA::Long SMESH_MeshEditor_i::MakeBoundaryElements(SMESH::Bnd_Dimension dim,
6430                                                      const char* groupName,
6431                                                      const char* meshName,
6432                                                      CORBA::Boolean toCopyAll,
6433                                                      const SMESH::ListOfIDSources& groups,
6434                                                      SMESH::SMESH_Mesh_out mesh,
6435                                                      SMESH::SMESH_Group_out group)
6436   throw (SALOME::SALOME_Exception)
6437 {
6438   SMESH_TRY;
6439   initData();
6440
6441   if ( dim > SMESH::BND_1DFROM2D )
6442     THROW_SALOME_CORBA_EXCEPTION("Invalid boundary dimension", SALOME::BAD_PARAM);
6443
6444   // separate groups belonging to this and other mesh
6445   SMESH::ListOfIDSources_var groupsOfThisMesh  = new SMESH::ListOfIDSources;
6446   SMESH::ListOfIDSources_var groupsOfOtherMesh = new SMESH::ListOfIDSources;
6447   groupsOfThisMesh ->length( groups.length() );
6448   groupsOfOtherMesh->length( groups.length() );
6449   int nbGroups = 0, nbGroupsOfOtherMesh = 0;
6450   for ( int i = 0; i < groups.length(); ++i )
6451   {
6452     SMESH::SMESH_Mesh_var m = groups[i]->GetMesh();
6453     if ( myMesh_i != SMESH::DownCast<SMESH_Mesh_i*>( m ))
6454       groupsOfOtherMesh[ nbGroupsOfOtherMesh++ ] = groups[i];
6455     else
6456       groupsOfThisMesh[ nbGroups++ ] = groups[i];
6457     if ( SMESH::DownCast<SMESH_Mesh_i*>( groups[i] ))
6458       THROW_SALOME_CORBA_EXCEPTION("expect a group but recieve a mesh", SALOME::BAD_PARAM);
6459   }
6460   groupsOfThisMesh->length( nbGroups );
6461   groupsOfOtherMesh->length( nbGroupsOfOtherMesh );
6462
6463   int nbAdded = 0;
6464   TPythonDump pyDump;
6465
6466   if ( nbGroupsOfOtherMesh > 0 )
6467   {
6468     // process groups belonging to another mesh
6469     SMESH::SMESH_Mesh_var    otherMesh = groupsOfOtherMesh[0]->GetMesh();
6470     SMESH::SMESH_MeshEditor_var editor = otherMesh->GetMeshEditor();
6471     nbAdded += editor->MakeBoundaryElements( dim, groupName, meshName, toCopyAll,
6472                                              groupsOfOtherMesh, mesh, group );
6473   }
6474
6475   SMESH::SMESH_Mesh_var mesh_var;
6476   SMESH::SMESH_Group_var group_var;
6477
6478   // get mesh to fill
6479   mesh_var = SMESH::SMESH_Mesh::_duplicate( myMesh_i->_this() );
6480   const bool toCopyMesh = ( strlen( meshName ) > 0 );
6481   if ( toCopyMesh )
6482   {
6483     if ( toCopyAll )
6484       mesh_var = SMESH_Gen_i::GetSMESHGen()->CopyMesh(mesh_var,
6485                                                       meshName,
6486                                                       /*toCopyGroups=*/false,
6487                                                       /*toKeepIDs=*/true);
6488     else
6489       mesh_var = makeMesh(meshName);
6490   }
6491   SMESH_Mesh_i* mesh_i = SMESH::DownCast<SMESH_Mesh_i*>( mesh_var );
6492   SMESH_Mesh*  tgtMesh = &mesh_i->GetImpl();
6493
6494   // source mesh
6495   SMESH_Mesh*     srcMesh = ( toCopyMesh && !toCopyAll ) ? myMesh : tgtMesh;
6496   SMESHDS_Mesh* srcMeshDS = srcMesh->GetMeshDS();
6497
6498   // group of boundary elements
6499   SMESH_Group* smesh_group = 0;
6500   SMDSAbs_ElementType elemType = (dim == SMESH::BND_2DFROM3D) ? SMDSAbs_Volume : SMDSAbs_Face;
6501   if ( strlen(groupName) )
6502   {
6503     SMESH::ElementType groupType = SMESH::ElementType( int(elemType)-1 );
6504     group_var = mesh_i->CreateGroup( groupType, groupName );
6505     if ( SMESH_GroupBase_i* group_i = SMESH::DownCast<SMESH_GroupBase_i*>( group_var ))
6506       smesh_group = group_i->GetSmeshGroup();
6507   }
6508
6509   TIDSortedElemSet elements;
6510
6511   if ( groups.length() > 0 )
6512   {
6513     for ( int i = 0; i < nbGroups; ++i )
6514     {
6515       elements.clear();
6516       if ( idSourceToSet( groupsOfThisMesh[i], srcMeshDS, elements, elemType,/*emptyIfIsMesh=*/0 ))
6517       {
6518         SMESH::Bnd_Dimension bdim = 
6519           ( elemType == SMDSAbs_Volume ) ? SMESH::BND_2DFROM3D : SMESH::BND_1DFROM2D;
6520         nbAdded += getEditor().MakeBoundaryMesh( elements,
6521                                                  ::SMESH_MeshEditor::Bnd_Dimension(bdim),
6522                                                  smesh_group,
6523                                                  tgtMesh,
6524                                                  /*toCopyElements=*/false,
6525                                                  /*toCopyExistingBondary=*/srcMesh != tgtMesh,
6526                                                  /*toAddExistingBondary=*/true,
6527                                                  /*aroundElements=*/true);
6528       }
6529     }
6530   }
6531   else
6532   {
6533     nbAdded += getEditor().MakeBoundaryMesh( elements,
6534                                              ::SMESH_MeshEditor::Bnd_Dimension(dim),
6535                                              smesh_group,
6536                                              tgtMesh,
6537                                              /*toCopyElements=*/false,
6538                                              /*toCopyExistingBondary=*/srcMesh != tgtMesh,
6539                                              /*toAddExistingBondary=*/true);
6540   }
6541   tgtMesh->GetMeshDS()->Modified();
6542
6543   const char* dimName[] = { "BND_2DFROM3D", "BND_1DFROM3D", "BND_1DFROM2D" };
6544
6545   // result of MakeBoundaryElements() is a tuple (nb, mesh, group)
6546   pyDump << "nbAdded, ";
6547   if ( mesh_var->_is_nil() )
6548     pyDump << myMesh_i->_this() << ", ";
6549   else
6550     pyDump << mesh_var << ", ";
6551   if ( group_var->_is_nil() )
6552     pyDump << "_NoneGroup = "; // assignment to None is forbiden
6553   else
6554     pyDump << group_var << " = ";
6555   pyDump << this << ".MakeBoundaryElements( "
6556          << "SMESH." << dimName[int(dim)] << ", "
6557          << "'" << groupName << "', "
6558          << "'" << meshName<< "', "
6559          << toCopyAll << ", "
6560          << groups << ")";
6561
6562   mesh  = mesh_var._retn();
6563   group = group_var._retn();
6564   return nbAdded;
6565
6566   SMESH_CATCH( SMESH::throwCorbaException );
6567   return 0;
6568 }