Salome HOME
Merge from V6_3_BR 06/06/2011
[modules/smesh.git] / src / StdMeshers / StdMeshers_Import_1D.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH SMESH : implementaion of SMESH idl descriptions
24 //  File   : StdMeshers_Import_1D.cxx
25 //  Module : SMESH
26 //
27 #include "StdMeshers_Import_1D.hxx"
28 #include "StdMeshers_ImportSource.hxx"
29
30 #include "SMDS_MeshElement.hxx"
31 #include "SMDS_MeshNode.hxx"
32 #include "SMESHDS_Group.hxx"
33 #include "SMESHDS_Mesh.hxx"
34 #include "SMESH_Comment.hxx"
35 #include "SMESH_Gen.hxx"
36 #include "SMESH_Group.hxx"
37 #include "SMESH_HypoFilter.hxx"
38 #include "SMESH_Mesh.hxx"
39 #include "SMESH_MesherHelper.hxx"
40 #include "SMESH_subMesh.hxx"
41 #include "SMESH_subMeshEventListener.hxx"
42
43 #include "Utils_SALOME_Exception.hxx"
44 #include "utilities.h"
45
46 #include <BRep_Builder.hxx>
47 #include <BRep_Tool.hxx>
48 #include <TopExp.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <TopoDS.hxx>
51 #include <TopoDS_Compound.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Vertex.hxx>
54
55 using namespace std;
56
57 //=============================================================================
58 /*!
59  * Creates StdMeshers_Import_1D
60  */
61 //=============================================================================
62
63 StdMeshers_Import_1D::StdMeshers_Import_1D(int hypId, int studyId, SMESH_Gen * gen)
64   :SMESH_1D_Algo(hypId, studyId, gen), _sourceHyp(0)
65 {
66   MESSAGE("StdMeshers_Import_1D::StdMeshers_Import_1D");
67   _name = "Import_1D";
68   _shapeType = (1 << TopAbs_EDGE);
69
70   _compatibleHypothesis.push_back("ImportSource1D");
71 }
72
73 //=============================================================================
74 /*!
75  * Check presence of a hypothesis
76  */
77 //=============================================================================
78
79 bool StdMeshers_Import_1D::CheckHypothesis
80                          (SMESH_Mesh&                          aMesh,
81                           const TopoDS_Shape&                  aShape,
82                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
83 {
84   _sourceHyp = 0;
85
86   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
87   if ( hyps.size() == 0 )
88   {
89     aStatus = SMESH_Hypothesis::HYP_MISSING;
90     return false;  // can't work with no hypothesis
91   }
92
93   if ( hyps.size() > 1 )
94   {
95     aStatus = SMESH_Hypothesis::HYP_ALREADY_EXIST;
96     return false;
97   }
98
99   const SMESHDS_Hypothesis *theHyp = hyps.front();
100
101   string hypName = theHyp->GetName();
102
103   if (hypName == _compatibleHypothesis.front())
104   {
105     _sourceHyp = (StdMeshers_ImportSource1D *)theHyp;
106     aStatus = SMESH_Hypothesis::HYP_OK;
107     return true;
108   }
109
110   aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
111   return true;
112 }
113
114 //================================================================================
115 namespace // INTERNAL STUFF
116 //================================================================================
117 {
118   int getSubmeshIDForCopiedMesh(const SMESHDS_Mesh* srcMeshDS, SMESH_Mesh* tgtMesh);
119
120   enum _ListenerDataType
121     {
122       WAIT_HYP_MODIF=1, // data indicating awaiting for valid parameters of src hyp
123       LISTEN_SRC_MESH, // data storing submesh depending on source mesh state
124       SRC_HYP // data storing ImportSource hyp
125     };
126   //================================================================================
127   /*!
128    * \brief _ListenerData holding ImportSource hyp holding in its turn
129    *  imported groups
130    */
131   struct _ListenerData : public SMESH_subMeshEventListenerData
132   {
133     const StdMeshers_ImportSource1D* _srcHyp;
134     _ListenerData(const StdMeshers_ImportSource1D* h, _ListenerDataType type=SRC_HYP):
135       SMESH_subMeshEventListenerData(/*isDeletable=*/true), _srcHyp(h)
136     {
137       myType = type; 
138     }
139   };
140   //================================================================================
141   /*!
142    * \brief Comparator of sub-meshes
143    */
144   struct _SubLess
145   {
146     bool operator()(const SMESH_subMesh* sm1, const SMESH_subMesh* sm2 ) const
147     {
148       if ( sm1 == sm2 ) return false;
149       if ( !sm1 || !sm2 ) return sm1 < sm2;
150       const TopoDS_Shape& s1 = sm1->GetSubShape();
151       const TopoDS_Shape& s2 = sm2->GetSubShape();
152       TopAbs_ShapeEnum t1 = s1.IsNull() ? TopAbs_SHAPE : s1.ShapeType();
153       TopAbs_ShapeEnum t2 = s2.IsNull() ? TopAbs_SHAPE : s2.ShapeType();
154       if ( t1 == t2)
155         return (sm1 < sm2);
156       return t1 < t2; // to have: face < edge
157     }
158   };
159   //================================================================================
160   /*!
161    * \brief Container of data dedicated to one source mesh
162    */
163   struct _ImportData
164   {
165     const SMESH_Mesh* _srcMesh;
166     StdMeshers_Import_1D::TNodeNodeMap _n2n;
167     StdMeshers_Import_1D::TElemElemMap _e2e;
168
169     set< SMESH_subMesh*, _SubLess > _subM; // submeshes relating to this srcMesh
170     set< SMESH_subMesh*, _SubLess > _copyMeshSubM; // submeshes requesting mesh copying
171     set< SMESH_subMesh*, _SubLess > _copyGroupSubM; // submeshes requesting mesh copying
172     set< SMESH_subMesh*, _SubLess > _computedSubM;
173
174     SMESHDS_SubMesh*     _importMeshSubDS; // submesh storing a copy of _srcMesh
175     int                  _importMeshSubID; // id of _importMeshSubDS
176
177     _ImportData(const SMESH_Mesh* srcMesh=0):
178       _srcMesh(srcMesh), _importMeshSubDS(0),_importMeshSubID(-1) {}
179
180     void removeImportedMesh( SMESHDS_Mesh* meshDS )
181     {
182       if ( !_importMeshSubDS ) return;
183       SMDS_ElemIteratorPtr eIt = _importMeshSubDS->GetElements();
184       while ( eIt->more() )
185         meshDS->RemoveFreeElement( eIt->next(), _importMeshSubDS, /*fromGroups=*/false );
186       SMDS_NodeIteratorPtr nIt = _importMeshSubDS->GetNodes();
187       while ( nIt->more() )
188         meshDS->RemoveFreeNode( nIt->next(), _importMeshSubDS, /*fromGroups=*/false );
189       _n2n.clear();
190       _e2e.clear();
191     }
192     void removeGroups( SMESH_subMesh* subM, const StdMeshers_ImportSource1D* srcHyp )
193     {
194       if ( !srcHyp ) return;
195       SMESH_Mesh*           tgtMesh = subM->GetFather();
196       const SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
197       const SMESHDS_Mesh* srcMeshDS = _srcMesh->GetMeshDS();
198       vector<SMESH_Group*>*  groups =
199         const_cast<StdMeshers_ImportSource1D*>(srcHyp)->GetResultGroups(*srcMeshDS,*tgtMeshDS);
200       if ( groups )
201       {
202         for ( unsigned i = 0; i < groups->size(); ++i )
203           tgtMesh->RemoveGroup( groups->at(i)->GetGroupDS()->GetID() );
204         groups->clear();
205       }
206     }
207     void trackHypParams( SMESH_subMesh* sm, const StdMeshers_ImportSource1D* srcHyp )
208     {
209       if ( !srcHyp ) return;
210       bool toCopyMesh, toCopyGroups;
211       srcHyp->GetCopySourceMesh(toCopyMesh, toCopyGroups);
212
213       if ( toCopyMesh )_copyMeshSubM.insert( sm );
214       else             _copyMeshSubM.erase( sm );
215
216       if ( toCopyGroups ) _copyGroupSubM.insert( sm );
217       else                _copyGroupSubM.erase( sm );
218     }
219     void addComputed( SMESH_subMesh* sm )
220     {
221       SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
222                                                                /*complexShapeFirst=*/true);
223       while ( smIt->more() )
224       {
225         sm = smIt->next();
226         switch ( sm->GetSubShape().ShapeType() )
227         {
228         case TopAbs_EDGE:
229         case TopAbs_FACE:
230           _subM.insert( sm );
231           if ( !sm->IsEmpty() )
232             _computedSubM.insert( sm );
233         case TopAbs_VERTEX:
234           break;
235         default:;
236         }
237       }
238     }
239   };
240   //================================================================================
241   /*!
242    * Listener notified on events relating to imported submesh
243    */
244   class _Listener : public SMESH_subMeshEventListener
245   {
246     typedef map< SMESH_Mesh*, list< _ImportData > > TMesh2ImpData;
247     TMesh2ImpData _tgtMesh2ImportData;
248
249     _Listener():SMESH_subMeshEventListener(/*isDeletable=*/false){}
250
251   public:
252     // return poiter to a static listener
253     static _Listener* get() { static _Listener theListener; return &theListener; }
254
255     static _ImportData* getImportData(const SMESH_Mesh* srcMesh, SMESH_Mesh* tgtMesh);
256
257     static void storeImportSubmesh(SMESH_subMesh*                   importSub,
258                                    const SMESH_Mesh*                srcMesh,
259                                    const StdMeshers_ImportSource1D* srcHyp);
260
261     virtual void ProcessEvent(const int                       event,
262                               const int                       eventType,
263                               SMESH_subMesh*                  subMesh,
264                               SMESH_subMeshEventListenerData* data,
265                               const SMESH_Hypothesis*         hyp);
266     void removeSubmesh( SMESH_subMesh* sm, _ListenerData* data );
267     void clearSubmesh ( SMESH_subMesh* sm, _ListenerData* data, bool clearAllSub );
268
269     // mark sm as missing src hyp with valid groups
270     static void waitHypModification(SMESH_subMesh* sm)
271     {
272       sm->SetEventListener
273         (get(), SMESH_subMeshEventListenerData::MakeData( sm, WAIT_HYP_MODIF ), sm);
274     }
275   };
276   //--------------------------------------------------------------------------------
277   /*!
278    * \brief Find or create ImportData for given meshes
279    */
280   _ImportData* _Listener::getImportData(const SMESH_Mesh* srcMesh,
281                                         SMESH_Mesh*       tgtMesh)
282   {
283     list< _ImportData >& dList = get()->_tgtMesh2ImportData[tgtMesh];
284     list< _ImportData >::iterator d = dList.begin();
285     for ( ; d != dList.end(); ++d )
286       if ( d->_srcMesh == srcMesh )
287         return &*d;
288     dList.push_back(_ImportData(srcMesh));
289     return &dList.back();
290   }
291
292   //--------------------------------------------------------------------------------
293   /*!
294    * \brief Remember an imported sub-mesh and set needed even listeners
295    *  \param importSub - submesh computed by Import algo
296    *  \param srcMesh - source mesh
297    *  \param srcHyp - ImportSource hypothesis
298    */
299   void _Listener::storeImportSubmesh(SMESH_subMesh*                   importSub,
300                                      const SMESH_Mesh*                srcMesh,
301                                      const StdMeshers_ImportSource1D* srcHyp)
302   {
303     // set listener to hear events of the submesh computed by "Import" algo
304     importSub->SetEventListener( get(), new _ListenerData(srcHyp), importSub );
305
306     // set a listener to hear events of the source mesh
307     SMESH_subMesh* smToNotify = importSub;
308     SMESH_subMesh* smToListen = srcMesh->GetSubMeshContaining(1);
309     SMESH_subMeshEventListenerData* data = new _ListenerData(srcHyp, LISTEN_SRC_MESH);
310     data->mySubMeshes.push_back( smToNotify );
311     importSub->SetEventListener( get(), data, smToListen );
312
313     // remeber the submesh importSub and its sub-submeshes
314     _ImportData* iData = _Listener::getImportData( srcMesh, importSub->GetFather());
315     iData->trackHypParams( importSub, srcHyp );
316     iData->addComputed( importSub );
317     if ( !iData->_copyMeshSubM.empty() && iData->_importMeshSubID < 1 )
318     {
319       SMESH_Mesh* tgtMesh = importSub->GetFather();
320       iData->_importMeshSubID = getSubmeshIDForCopiedMesh( srcMesh->GetMeshDS(),tgtMesh);
321       iData->_importMeshSubDS = tgtMesh->GetMeshDS()->NewSubMesh( iData->_importMeshSubID );
322     }
323   }
324   //--------------------------------------------------------------------------------
325   /*!
326    * \brief Remove imported mesh and/or groups if needed
327    *  \param sm - submesh loosing Import algo
328    *  \param data - data holding imported groups
329    */
330   void _Listener::removeSubmesh( SMESH_subMesh* sm, _ListenerData* data )
331   {
332     list< _ImportData > &  dList = _tgtMesh2ImportData[ sm->GetFather() ];
333     list< _ImportData >::iterator d = dList.begin();
334     for ( ; d != dList.end(); ++d )
335       if ( (*d)._subM.erase( sm ))
336       {
337         d->_computedSubM.erase( sm );
338         bool rmMesh   = d->_copyMeshSubM.erase( sm ) && d->_copyMeshSubM.empty();
339         bool rmGroups = (d->_copyGroupSubM.erase( sm ) && d->_copyGroupSubM.empty()) || rmMesh;
340         if ( rmMesh )
341           d->removeImportedMesh( sm->GetFather()->GetMeshDS() );
342         if ( rmGroups && data )
343           d->removeGroups( sm, data->_srcHyp );
344       }
345   }
346   //--------------------------------------------------------------------------------
347   /*!
348    * \brief Clear submeshes and remove imported mesh and/or groups if necessary
349    *  \param sm - cleared submesh
350    *  \param data - data holding imported groups
351    */
352   void _Listener::clearSubmesh(SMESH_subMesh* sm, _ListenerData* data, bool clearAllSub)
353   {
354     list< _ImportData > &  dList = _tgtMesh2ImportData[ sm->GetFather() ];
355     list< _ImportData >::iterator d = dList.begin();
356     for ( ; d != dList.end(); ++d )
357     {
358       if ( !d->_subM.count( sm )) continue;
359       if ( (*d)._computedSubM.erase( sm ) )
360       {
361         bool copyMesh = !d->_copyMeshSubM.empty();
362         if ( copyMesh || clearAllSub )
363         {
364           // remove imported mesh and groups
365           d->removeImportedMesh( sm->GetFather()->GetMeshDS() );
366
367           if ( data )
368             d->removeGroups( sm, data->_srcHyp );
369
370           // clear the rest submeshes
371           if ( !d->_computedSubM.empty() )
372           {
373             set< SMESH_subMesh*, _SubLess> subs;
374             subs.swap( d->_computedSubM ); // avoid recursion via events
375             while ( !subs.empty() )
376             {
377               SMESH_subMesh* subM = *subs.begin(); subs.erase( subs.begin() );
378               _ListenerData* hypData = (_ListenerData*) subM->GetEventListenerData( get() );
379               if ( hypData )
380                 d->removeGroups( sm, hypData->_srcHyp );
381
382               subM->ComputeStateEngine( SMESH_subMesh::CLEAN );
383               if ( subM->GetSubShape().ShapeType() == TopAbs_FACE )
384                 subM->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
385             }
386           }
387         }
388         sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
389         if ( sm->GetSubShape().ShapeType() == TopAbs_FACE )
390           sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
391       }
392       if ( data )
393         d->trackHypParams( sm, data->_srcHyp );
394       d->_n2n.clear();
395       d->_e2e.clear();
396     }
397   }
398   //--------------------------------------------------------------------------------
399   /*!
400    * \brief Remove imported mesh and/or groups
401    */
402   void _Listener::ProcessEvent(const int                       event,
403                                const int                       eventType,
404                                SMESH_subMesh*                  subMesh,
405                                SMESH_subMeshEventListenerData* data,
406                                const SMESH_Hypothesis*         /*hyp*/)
407   {
408     if ( data && data->myType == WAIT_HYP_MODIF )
409     {
410       // event of Import submesh
411       if ( SMESH_subMesh::MODIF_HYP  == event &&
412            SMESH_subMesh::ALGO_EVENT == eventType )
413       {
414         // re-call SetEventListener() to take into account valid parameters
415         // of ImportSource hypothesis
416         SMESH_Gen* gen = subMesh->GetFather()->GetGen();
417         if ( SMESH_Algo* algo = gen->GetAlgo(*subMesh->GetFather(), subMesh->GetSubShape()))
418           algo->SetEventListener( subMesh );
419       }
420     }
421     else if ( data && data->myType == LISTEN_SRC_MESH )
422     {
423       // event of source mesh
424       if ( SMESH_subMesh::COMPUTE_EVENT == eventType )
425       {
426         switch ( event ) {
427         case SMESH_subMesh::CLEAN:
428           // source mesh cleaned -> clean target mesh
429           clearSubmesh( data->mySubMeshes.front(), (_ListenerData*) data, /*all=*/true );
430           break;
431         case SMESH_subMesh::SUBMESH_COMPUTED: {
432           // source mesh computed -> reset FAILED state of Import submeshes to
433           // READY_TO_COMPUTE
434           SMESH_Mesh* srcMesh = subMesh->GetFather();
435           if ( srcMesh->NbEdges() > 0 || srcMesh->NbFaces() > 0 )
436           {
437             SMESH_Mesh* m = data->mySubMeshes.front()->GetFather();
438             if ( SMESH_subMesh* sm1 = m->GetSubMeshContaining(1))
439             {
440               sm1->ComputeStateEngine(SMESH_subMesh::SUBMESH_COMPUTED );
441               sm1->ComputeSubMeshStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
442             }
443           }
444           break;
445         }
446         default:;
447         }
448       }
449     }
450     else // event of Import submesh
451     {
452       // find out what happens: import hyp modified or removed
453       bool removeImport = false, modifHyp = false;
454       if ( SMESH_subMesh::ALGO_EVENT == eventType )
455         modifHyp = true;
456       if ( subMesh->GetAlgoState() != SMESH_subMesh::HYP_OK )
457       {
458         removeImport = true;
459       }
460       else if ( SMESH_subMesh::REMOVE_ALGO == event ||
461                 SMESH_subMesh::REMOVE_FATHER_ALGO == event )
462       {
463         SMESH_Gen* gen = subMesh->GetFather()->GetGen();
464         SMESH_Algo* algo = gen->GetAlgo(*subMesh->GetFather(),subMesh->GetSubShape() );
465         removeImport = ( strncmp( "Import", algo->GetName(), 6 ) != 0 );
466       }
467
468       if ( removeImport )
469       {
470         // treate removal of Import algo from subMesh
471         removeSubmesh( subMesh, (_ListenerData*) data );
472       }
473       else if ( modifHyp )
474       {
475         // treate modification of ImportSource hypothesis
476         clearSubmesh( subMesh, (_ListenerData*) data, /*all=*/false );
477       }
478       else if ( SMESH_subMesh::CHECK_COMPUTE_STATE == event &&
479                 SMESH_subMesh::COMPUTE_EVENT       == eventType )
480       {
481         // check compute state of all submeshes impoting from same src mesh;
482         // this is to take into account 1D computed submeshes hidden by 2D import algo;
483         // else source mesh is not copied as _subM.size != _computedSubM.size()
484         list< _ImportData > &  dList = _tgtMesh2ImportData[ subMesh->GetFather() ];
485         list< _ImportData >::iterator d = dList.begin();
486         for ( ; d != dList.end(); ++d )
487           if ( d->_subM.count( subMesh ))
488           {
489             set<SMESH_subMesh*>::iterator smIt = d->_subM.begin();
490             for( ; smIt != d->_subM.end(); ++smIt )
491               if ( (*smIt)->IsMeshComputed() )
492                 d->_computedSubM.insert( *smIt);
493           }
494       }
495     }
496   }
497
498   //================================================================================
499   /*!
500    * \brief Return an ID of submesh to store nodes and elements of a copied mesh
501    */
502   //================================================================================
503
504   int getSubmeshIDForCopiedMesh(const SMESHDS_Mesh* srcMeshDS,
505                                 SMESH_Mesh*         tgtMesh)
506   {
507     // To get SMESH_subMesh corresponding to srcMeshDS we need to have a shape
508     // for which SMESHDS_Mesh::IsGroupOfSubShapes() returns true.
509     // And this shape must be different from subshapes of the main shape.
510     // So we create a compound containing
511     // 1) some sub-shapes of SMESH_Mesh::PseudoShape() corresponding to
512     //    srcMeshDS->GetPersistentId()
513     // 2) the 1-st vertex of the main shape to assure
514     //    SMESHDS_Mesh::IsGroupOfSubShapes(shape)==true
515     TopoDS_Shape shapeForSrcMesh;
516     TopTools_IndexedMapOfShape pseudoSubShapes;
517     TopExp::MapShapes( SMESH_Mesh::PseudoShape(), pseudoSubShapes );
518
519     // index of pseudoSubShapes corresponding to srcMeshDS
520     int subIndex = srcMeshDS->GetPersistentId() % pseudoSubShapes.Extent();
521     int nbSubShapes = 1 + srcMeshDS->GetPersistentId() / pseudoSubShapes.Extent();
522
523     // try to find already present shapeForSrcMesh
524     SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
525     for ( int i = tgtMeshDS->MaxShapeIndex(); i > 0 && shapeForSrcMesh.IsNull(); --i )
526     {
527       const TopoDS_Shape& s = tgtMeshDS->IndexToShape(i);
528       if ( s.ShapeType() != TopAbs_COMPOUND ) break;
529       TopoDS_Iterator sSubIt( s );
530       for ( int iSub = 0; iSub < nbSubShapes && sSubIt.More(); ++iSub, sSubIt.Next() )
531         if ( pseudoSubShapes( subIndex+iSub ).IsSame( sSubIt.Value()))
532           if ( iSub+1 == nbSubShapes )
533           {
534             shapeForSrcMesh = s;
535             break;
536           }
537     }
538     if ( shapeForSrcMesh.IsNull() )
539     {
540       // make a new shapeForSrcMesh
541       BRep_Builder aBuilder;
542       TopoDS_Compound comp;
543       aBuilder.MakeCompound( comp );
544       shapeForSrcMesh = comp;
545       for ( int iSub = 0; iSub < nbSubShapes; ++iSub )
546         aBuilder.Add( comp, pseudoSubShapes( subIndex+iSub ));
547       TopExp_Explorer vExp( tgtMeshDS->ShapeToMesh(), TopAbs_VERTEX );
548       aBuilder.Add( comp, vExp.Current() );
549     }
550     SMESH_subMesh* sm = tgtMesh->GetSubMesh( shapeForSrcMesh );
551     SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
552     if ( !smDS )
553       smDS = tgtMeshDS->NewSubMesh( sm->GetId() );
554
555     // make ordinary submesh from a complex one
556     if ( smDS->IsComplexSubmesh() )
557     {
558       list< const SMESHDS_SubMesh* > subSM;
559       SMESHDS_SubMeshIteratorPtr smIt = smDS->GetSubMeshIterator();
560       while ( smIt->more() ) subSM.push_back( smIt->next() );
561       list< const SMESHDS_SubMesh* >::iterator sub = subSM.begin();
562       for ( ; sub != subSM.end(); ++sub)
563         smDS->RemoveSubMesh( *sub );
564     }
565     return sm->GetId();
566   }
567
568   //================================================================================
569   /*!
570    * \brief Return a submesh to store nodes and elements of a copied mesh
571    * and set event listeners in order to clear
572    * imported mesh and groups as soon as submesh state requires it
573    */
574   //================================================================================
575
576   SMESHDS_SubMesh* getSubmeshForCopiedMesh(const SMESH_Mesh*                    srcMesh,
577                                            SMESH_Mesh*                          tgtMesh,
578                                            const TopoDS_Shape&                  tgtShape,
579                                            StdMeshers_Import_1D::TNodeNodeMap*& n2n,
580                                            StdMeshers_Import_1D::TElemElemMap*& e2e,
581                                            bool &                               toCopyGroups)
582   {
583     StdMeshers_Import_1D::getMaps( srcMesh, tgtMesh, n2n,e2e );
584
585     _ImportData* iData = _Listener::getImportData(srcMesh,tgtMesh);
586
587     SMESH_subMesh* importedSM = tgtMesh->GetSubMesh( tgtShape );
588     iData->addComputed( importedSM );
589     if ( iData->_computedSubM.size() != iData->_subM.size() )
590       return 0; // not all submeshes computed yet
591
592     toCopyGroups = !iData->_copyGroupSubM.empty();
593
594     if ( !iData->_copyMeshSubM.empty())
595     {
596       // make submesh to store a copied mesh
597       int smID = getSubmeshIDForCopiedMesh( srcMesh->GetMeshDS(), tgtMesh );
598       SMESHDS_SubMesh* subDS = tgtMesh->GetMeshDS()->NewSubMesh( smID );
599
600       iData->_importMeshSubID = smID;
601       iData->_importMeshSubDS = subDS;
602       return subDS;
603     }
604     return 0;
605   }
606
607 } // namespace
608
609
610 //=============================================================================
611 /*!
612  * Import elements from the other mesh 
613  */
614 //=============================================================================
615
616 bool StdMeshers_Import_1D::Compute(SMESH_Mesh & theMesh, const TopoDS_Shape & theShape)
617 {
618   if ( !_sourceHyp ) return false;
619
620   const vector<SMESH_Group*>& srcGroups = _sourceHyp->GetGroups();
621   if ( srcGroups.empty() )
622     return error("Invalid source groups");
623
624   SMESH_MesherHelper helper(theMesh);
625   helper.SetSubShape(theShape);
626   SMESHDS_Mesh* tgtMesh = theMesh.GetMeshDS();
627
628   const TopoDS_Edge& geomEdge = TopoDS::Edge( theShape );
629   const double edgeTol = BRep_Tool::Tolerance( geomEdge );
630   const int shapeID = tgtMesh->ShapeToIndex( geomEdge );
631
632   set<int> subShapeIDs;
633   subShapeIDs.insert( shapeID );
634
635   // get nodes on vertices
636   list < SMESH_TNodeXYZ > vertexNodes; 
637  list < SMESH_TNodeXYZ >::iterator vNIt;
638   TopExp_Explorer vExp( theShape, TopAbs_VERTEX );
639   for ( ; vExp.More(); vExp.Next() )
640   {
641     const TopoDS_Vertex& v = TopoDS::Vertex( vExp.Current() );
642     if ( !subShapeIDs.insert( tgtMesh->ShapeToIndex( v )).second )
643       continue; // closed edge
644     const SMDS_MeshNode* n = SMESH_Algo::VertexNode( v, tgtMesh );
645     if ( !n )
646     {
647       _gen->Compute(theMesh,v,/*anUpward=*/true);
648       n = SMESH_Algo::VertexNode( v, tgtMesh );
649       if ( !n ) return false; // very strange
650     }
651     vertexNodes.push_back( SMESH_TNodeXYZ( n ));
652   }
653
654   // import edges from groups
655   TNodeNodeMap* n2n;
656   TElemElemMap* e2e;
657   for ( int iG = 0; iG < srcGroups.size(); ++iG )
658   {
659     const SMESHDS_GroupBase* srcGroup = srcGroups[iG]->GetGroupDS();
660
661     const int meshID = srcGroup->GetMesh()->GetPersistentId();
662     const SMESH_Mesh* srcMesh = GetMeshByPersistentID( meshID );
663     if ( !srcMesh ) continue;
664     getMaps( srcMesh, &theMesh, n2n, e2e );
665
666     SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
667     vector<const SMDS_MeshNode*> newNodes;
668     SMDS_MeshNode *tmpNode = helper.AddNode(0,0,0);
669     double u;
670     while ( srcElems->more() ) // loop on group contents
671     {
672       const SMDS_MeshElement* edge = srcElems->next();
673       // find or create nodes of a new edge
674       newNodes.resize( edge->NbNodes() );
675       newNodes.back() = 0;
676       SMDS_MeshElement::iterator node = edge->begin_nodes();
677       for ( unsigned i = 0; i < newNodes.size(); ++i, ++node )
678       {
679         TNodeNodeMap::iterator n2nIt = n2n->insert( make_pair( *node, (SMDS_MeshNode*)0 )).first;
680         if ( n2nIt->second )
681         {
682           if ( !subShapeIDs.count( n2nIt->second->getshapeId() ))
683             break;
684         }
685         else
686         {
687           // find an existing vertex node
688           for ( vNIt = vertexNodes.begin(); vNIt != vertexNodes.end(); ++vNIt)
689             if ( vNIt->SquareDistance( *node ) < 10 * edgeTol * edgeTol)
690             {
691               (*n2nIt).second = vNIt->_node;
692               vertexNodes.erase( vNIt );
693               break;
694             }
695         }
696         if ( !n2nIt->second )
697         {
698           // find out if node lies on theShape
699           tmpNode->setXYZ( (*node)->X(), (*node)->Y(), (*node)->Z());
700           if ( helper.CheckNodeU( geomEdge, tmpNode, u, 10 * edgeTol, /*force=*/true ))
701           {
702             SMDS_MeshNode* newNode = tgtMesh->AddNode( (*node)->X(), (*node)->Y(), (*node)->Z());
703             n2nIt->second = newNode;
704             tgtMesh->SetNodeOnEdge( newNode, shapeID, u );
705           }
706         }
707         if ( !(newNodes[i] = n2nIt->second ))
708           break;
709       }
710       if ( !newNodes.back() )
711         continue; // not all nodes of edge lie on theShape
712
713       // make a new edge
714       SMDS_MeshElement * newEdge;
715       if ( newNodes.size() == 3 )
716         newEdge = tgtMesh->AddEdge( newNodes[0], newNodes[1], newNodes[2] );
717       else
718         newEdge = tgtMesh->AddEdge( newNodes[0], newNodes[1]);
719       tgtMesh->SetMeshElementOnShape( newEdge, shapeID );
720       e2e->insert( make_pair( edge, newEdge ));
721     }
722     helper.GetMeshDS()->RemoveNode(tmpNode);
723   }
724   if ( n2n->empty())
725     return error("Empty source groups");
726
727   // check if the whole geom edge is covered by imported segments;
728   // the check consist in passing by segments from one vetrex node to another
729   bool isEdgeMeshed = false;
730   if ( SMESHDS_SubMesh* tgtSM = tgtMesh->MeshElements( theShape ))
731   {
732     const TopoDS_Vertex& v = ( vExp.ReInit(), TopoDS::Vertex( vExp.Current() ));
733     const SMDS_MeshNode* n = SMESH_Algo::VertexNode( v, tgtMesh );
734     const SMDS_MeshElement* seg = 0;
735     SMDS_ElemIteratorPtr segIt = n->GetInverseElementIterator(SMDSAbs_Edge);
736     while ( segIt->more() && !seg )
737       if ( !tgtSM->Contains( seg = segIt->next()))
738         seg = 0;
739     int nbPassedSegs = 0;
740     while ( seg )
741     {
742       ++nbPassedSegs;
743       const SMDS_MeshNode* n2 = seg->GetNode(0);
744       n = ( n2 == n ? seg->GetNode(1) : n2 );
745       if ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX )
746         break;
747       const SMDS_MeshElement* seg2 = 0;
748       segIt = n->GetInverseElementIterator(SMDSAbs_Edge);
749       while ( segIt->more() && !seg2 )
750         if ( seg == ( seg2 = segIt->next()))
751           seg2 = 0;
752       seg = seg2;
753     }
754     if (nbPassedSegs > 0 && tgtSM->NbElements() > nbPassedSegs )
755       return error( "Source elements overlap one another");
756
757     isEdgeMeshed = ( tgtSM->NbElements() == nbPassedSegs &&
758                      n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX );
759   }
760   if ( !isEdgeMeshed )
761     return error( "Source elements don't cover totally the geometrical edge" );
762
763   // copy meshes
764   vector<SMESH_Mesh*> srcMeshes = _sourceHyp->GetSourceMeshes();
765   for ( unsigned i = 0; i < srcMeshes.size(); ++i )
766     importMesh( srcMeshes[i], theMesh, _sourceHyp, theShape );
767
768   return true;
769 }
770
771 //================================================================================
772 /*!
773  * \brief Copy mesh and groups
774  */
775 //================================================================================
776
777 void StdMeshers_Import_1D::importMesh(const SMESH_Mesh*          srcMesh,
778                                       SMESH_Mesh &               tgtMesh,
779                                       StdMeshers_ImportSource1D* srcHyp,
780                                       const TopoDS_Shape&        tgtShape)
781 {
782   // get submesh to store the imported mesh
783   TNodeNodeMap* n2n;
784   TElemElemMap* e2e;
785   bool toCopyGroups;
786   SMESHDS_SubMesh* tgtSubMesh =
787     getSubmeshForCopiedMesh( srcMesh, &tgtMesh, tgtShape, n2n, e2e, toCopyGroups );
788   if ( !tgtSubMesh || tgtSubMesh->NbNodes() + tgtSubMesh->NbElements() > 0 )
789     return; // not to copy srcMeshDS twice
790
791   SMESHDS_Mesh* tgtMeshDS = tgtMesh.GetMeshDS();
792   SMESH_MeshEditor additor( &tgtMesh );
793
794   // 1. Copy mesh
795
796   vector<const SMDS_MeshNode*> newNodes;
797   const SMESHDS_Mesh* srcMeshDS = srcMesh->GetMeshDS();
798   SMDS_ElemIteratorPtr eIt = srcMeshDS->elementsIterator();
799   while ( eIt->more() )
800   {
801     const SMDS_MeshElement* elem = eIt->next();
802     TElemElemMap::iterator e2eIt = e2e->insert( make_pair( elem, (SMDS_MeshElement*)0 )).first;
803     if ( e2eIt->second ) continue; // already copied by Compute()
804     newNodes.resize( elem->NbNodes() );
805     SMDS_MeshElement::iterator node = elem->begin_nodes();
806     for ( unsigned i = 0; i < newNodes.size(); ++i, ++node )
807     {
808       TNodeNodeMap::iterator n2nIt = n2n->insert( make_pair( *node, (SMDS_MeshNode*)0 )).first;
809       if ( !n2nIt->second )
810       {
811         (*n2nIt).second = tgtMeshDS->AddNode( (*node)->X(), (*node)->Y(), (*node)->Z());
812         tgtSubMesh->AddNode( n2nIt->second );
813       }
814       newNodes[i] = n2nIt->second;
815     }
816     const SMDS_MeshElement* newElem =
817       tgtMeshDS->FindElement( newNodes, elem->GetType(), /*noMedium=*/false );
818     if ( !newElem )
819     {
820       newElem = additor.AddElement( newNodes, elem->GetType(), elem->IsPoly());
821       tgtSubMesh->AddElement( newElem );
822     }
823     if ( toCopyGroups )
824       (*e2eIt).second = newElem;
825   }
826   // copy free nodes
827   if ( srcMeshDS->NbNodes() > n2n->size() )
828   {
829     SMDS_NodeIteratorPtr nIt = srcMeshDS->nodesIterator();
830     while( nIt->more() )
831     {
832       const SMDS_MeshNode* node = nIt->next();
833       if ( node->NbInverseElements() == 0 )
834       {
835         const SMDS_MeshNode* newNode = tgtMeshDS->AddNode( node->X(), node->Y(), node->Z());
836         n2n->insert( make_pair( node, newNode ));
837         tgtSubMesh->AddNode( newNode );
838       }
839     }
840   }
841
842   // 2. Copy groups
843
844   vector<SMESH_Group*> resultGroups;
845   if ( toCopyGroups )
846   {
847     // collect names of existing groups to assure uniqueness of group names within a type
848     map< SMDSAbs_ElementType, set<string> > namesByType;
849     SMESH_Mesh::GroupIteratorPtr groupIt = tgtMesh.GetGroups();
850     while ( groupIt->more() )
851     {
852       SMESH_Group* tgtGroup = groupIt->next();
853       namesByType[ tgtGroup->GetGroupDS()->GetType() ].insert( tgtGroup->GetName() );
854     }
855     if (srcMesh)
856     {
857       SMESH_Mesh::GroupIteratorPtr groupIt = srcMesh->GetGroups();
858       while ( groupIt->more() )
859       {
860         SMESH_Group* srcGroup = groupIt->next();
861         SMESHDS_GroupBase* srcGroupDS = srcGroup->GetGroupDS();
862         string name = srcGroup->GetName();
863         int nb = 1;
864         while ( !namesByType[ srcGroupDS->GetType() ].insert( name ).second )
865           name = SMESH_Comment(srcGroup->GetName()) << "_imported_" << nb++;
866         SMESH_Group* newGroup = tgtMesh.AddGroup( srcGroupDS->GetType(), name.c_str(), nb );
867         SMESHDS_Group* newGroupDS = (SMESHDS_Group*)newGroup->GetGroupDS();
868         resultGroups.push_back( newGroup );
869
870         eIt = srcGroupDS->GetElements();
871         if ( srcGroupDS->GetType() == SMDSAbs_Node )
872           while (eIt->more())
873           {
874             TNodeNodeMap::iterator n2nIt = n2n->find((const SMDS_MeshNode*) eIt->next() );
875             if ( n2nIt != n2n->end() && n2nIt->second )
876               newGroupDS->SMDSGroup().Add((*n2nIt).second );
877           }
878         else
879           while (eIt->more())
880           {
881             TElemElemMap::iterator e2eIt = e2e->find( eIt->next() );
882             if ( e2eIt != e2e->end() && e2eIt->second )
883               newGroupDS->SMDSGroup().Add((*e2eIt).second );
884           }
885       }
886     }
887   }
888   n2n->clear();
889   e2e->clear();
890
891   // Remember created groups in order to remove them as soon as the srcHyp is
892   // modified or something other similar happens. Store them in a hypothesis
893   // as it stores its values anyway
894   srcHyp->StoreResultGroups( resultGroups, *srcMeshDS, *tgtMeshDS );
895 }
896
897 //=============================================================================
898 /*!
899  * \brief Set needed event listeners and create a submesh for a copied mesh
900  *
901  * This method is called only if a submesh has HYP_OK algo_state.
902  */
903 //=============================================================================
904
905 void StdMeshers_Import_1D::setEventListener(SMESH_subMesh*             subMesh, 
906                                             StdMeshers_ImportSource1D* sourceHyp)
907 {
908   if ( sourceHyp )
909   {
910     vector<SMESH_Mesh*> srcMeshes = sourceHyp->GetSourceMeshes();
911     if ( srcMeshes.empty() )
912       _Listener::waitHypModification( subMesh );
913     for ( unsigned i = 0; i < srcMeshes.size(); ++i )
914       // set a listener to remove the imported mesh and groups
915       _Listener::storeImportSubmesh( subMesh, srcMeshes[i], sourceHyp );
916   }
917 }
918 void StdMeshers_Import_1D::SetEventListener(SMESH_subMesh* subMesh)
919 {
920   if ( !_sourceHyp )
921   {
922     const TopoDS_Shape& tgtShape = subMesh->GetSubShape();
923     SMESH_Mesh*         tgtMesh  = subMesh->GetFather();
924     Hypothesis_Status aStatus;
925     CheckHypothesis( *tgtMesh, tgtShape, aStatus );
926   }
927   setEventListener( subMesh, _sourceHyp );
928 }
929
930 void StdMeshers_Import_1D::SubmeshRestored(SMESH_subMesh* subMesh)
931 {
932   SetEventListener(subMesh);
933 }
934
935 //=============================================================================
936 /*!
937  * Predict nb of mesh entities created by Compute()
938  */
939 //=============================================================================
940
941 bool StdMeshers_Import_1D::Evaluate(SMESH_Mesh &         theMesh,
942                                     const TopoDS_Shape & theShape,
943                                     MapShapeNbElems&     aResMap)
944 {
945   if ( !_sourceHyp ) return false;
946
947   const vector<SMESH_Group*>& srcGroups = _sourceHyp->GetGroups();
948   if ( srcGroups.empty() )
949     return error("Invalid source groups");
950
951   vector<int> aVec(SMDSEntity_Last,0);
952
953   bool toCopyMesh, toCopyGroups;
954   _sourceHyp->GetCopySourceMesh(toCopyMesh, toCopyGroups);
955   if ( toCopyMesh ) // the whole mesh is copied
956   {
957     vector<SMESH_Mesh*> srcMeshes = _sourceHyp->GetSourceMeshes();
958     for ( unsigned i = 0; i < srcMeshes.size(); ++i )
959     {
960       SMESH_subMesh* sm = getSubMeshOfCopiedMesh( theMesh, *srcMeshes[i]);
961       if ( !sm || aResMap.count( sm )) continue; // already counted
962       aVec.assign( SMDSEntity_Last, 0);
963       const SMDS_MeshInfo& aMeshInfo = srcMeshes[i]->GetMeshDS()->GetMeshInfo();
964       for (int i = 0; i < SMDSEntity_Last; i++)
965         aVec[i] = aMeshInfo.NbEntities((SMDSAbs_EntityType)i);
966     }
967   }
968   else
969   {
970     SMESH_MesherHelper helper(theMesh);
971
972     const TopoDS_Edge& geomEdge = TopoDS::Edge( theShape );
973     const double edgeTol = helper.MaxTolerance( geomEdge );
974
975     // take into account nodes on vertices
976     TopExp_Explorer vExp( theShape, TopAbs_VERTEX );
977     for ( ; vExp.More(); vExp.Next() )
978       theMesh.GetSubMesh( vExp.Current())->Evaluate( aResMap );
979
980     // count edges imported from groups
981     int nbEdges = 0, nbQuadEdges = 0;
982     for ( int iG = 0; iG < srcGroups.size(); ++iG )
983     {
984       const SMESHDS_GroupBase* srcGroup = srcGroups[iG]->GetGroupDS();
985       SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
986       SMDS_MeshNode *tmpNode = helper.AddNode(0,0,0);
987       while ( srcElems->more() ) // loop on group contents
988       {
989         const SMDS_MeshElement* edge = srcElems->next();
990         // find out if edge is located on geomEdge by projecting
991         // a middle of edge to geomEdge
992         SMESH_TNodeXYZ p1( edge->GetNode(0));
993         SMESH_TNodeXYZ p2( edge->GetNode(1));
994         gp_XYZ middle = ( p1 + p2 ) / 2.;
995         tmpNode->setXYZ( middle.X(), middle.Y(), middle.Z());
996         double u = 0;
997         if ( helper.CheckNodeU( geomEdge, tmpNode, u, 10 * edgeTol, /*force=*/true ))
998           ++( edge->IsQuadratic() ? nbQuadEdges : nbEdges);
999       }
1000       helper.GetMeshDS()->RemoveNode(tmpNode);
1001     }
1002
1003     int nbNodes = nbEdges + 2 * nbQuadEdges - 1;
1004
1005     aVec[SMDSEntity_Node     ] = nbNodes;
1006     aVec[SMDSEntity_Edge     ] = nbEdges;
1007     aVec[SMDSEntity_Quad_Edge] = nbQuadEdges;
1008   }
1009
1010   SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1011   aResMap.insert(make_pair(sm,aVec));
1012
1013   return true;
1014 }
1015
1016 //================================================================================
1017 /*!
1018  * \brief Return node-node and element-element maps for import of geiven source mesh
1019  */
1020 //================================================================================
1021
1022 void StdMeshers_Import_1D::getMaps(const SMESH_Mesh* srcMesh,
1023                                    SMESH_Mesh*       tgtMesh,
1024                                    TNodeNodeMap*&    n2n,
1025                                    TElemElemMap*&    e2e)
1026 {
1027   _ImportData* iData = _Listener::getImportData(srcMesh,tgtMesh);
1028   n2n = &iData->_n2n;
1029   e2e = &iData->_e2e;
1030   if ( iData->_copyMeshSubM.empty() )
1031   {
1032     n2n->clear();
1033     e2e->clear();
1034   }
1035 }
1036
1037 //================================================================================
1038 /*!
1039  * \brief Return submesh corresponding to the copied mesh
1040  */
1041 //================================================================================
1042
1043 SMESH_subMesh* StdMeshers_Import_1D::getSubMeshOfCopiedMesh( SMESH_Mesh& tgtMesh,
1044                                                              SMESH_Mesh& srcMesh )
1045 {
1046   _ImportData* iData = _Listener::getImportData(&srcMesh,&tgtMesh);
1047   if ( iData->_copyMeshSubM.empty() ) return 0;
1048   SMESH_subMesh* sm = tgtMesh.GetSubMeshContaining( iData->_importMeshSubID );
1049   return sm;
1050 }
1051