Salome HOME
PAL10467. Add "Quadrangle Preference" hypothesis for "Quadrangle(Mapping)" algo
[modules/smesh.git] / src / SMESH_I / SMESH_2smeshpy.cxx
1 // File      : SMESH_2smeshpy.cxx
2 // Created   : Fri Nov 18 13:20:10 2005
3 // Author    : Edward AGAPOV (eap)
4
5 #include "SMESH_2smeshpy.hxx"
6
7 #include "SMESH_Gen_i.hxx"
8 #include "utilities.h"
9 #include "SMESH_PythonDump.hxx"
10 #include "Resource_DataMapOfAsciiStringAsciiString.hxx"
11
12 IMPLEMENT_STANDARD_HANDLE (_pyObject          ,Standard_Transient);
13 IMPLEMENT_STANDARD_HANDLE (_pyCommand         ,Standard_Transient);
14 IMPLEMENT_STANDARD_HANDLE (_pyGen             ,_pyObject);
15 IMPLEMENT_STANDARD_HANDLE (_pyMesh            ,_pyObject);
16 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis      ,_pyObject);
17 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm       ,_pyHypothesis);
18 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
19 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
20
21 IMPLEMENT_STANDARD_RTTIEXT(_pyObject          ,Standard_Transient);
22 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand         ,Standard_Transient);
23 IMPLEMENT_STANDARD_RTTIEXT(_pyGen             ,_pyObject);
24 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh            ,_pyObject);
25 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis      ,_pyObject);
26 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm       ,_pyHypothesis);
27 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
28 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
29
30 using namespace std;
31 using SMESH::TPythonDump;
32
33 /*!
34  * \brief Container of commands into which the initial script is split.
35  *        It also contains data coresponding to SMESH_Gen contents
36  */
37 static Handle(_pyGen) theGen;
38
39 static TCollection_AsciiString theEmptyString;
40
41 //#define DUMP_CONVERSION
42
43 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
44 #undef DUMP_CONVERSION
45 #endif
46
47 //================================================================================
48 /*!
49  * \brief Convert python script using commands of smesh.py
50   * \param theScript - Input script
51   * \retval TCollection_AsciiString - Convertion result
52  */
53 //================================================================================
54
55 TCollection_AsciiString
56 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
57                               Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
58 {
59   theGen = new _pyGen( theEntry2AccessorMethod );
60
61   // split theScript into separate commands
62   int from = 1, end = theScript.Length(), to;
63   while ( from < end && ( to = theScript.Location( "\n", from, end )))
64   {
65     if ( to != from )
66       // cut out and store a command
67       theGen->AddCommand( theScript.SubString( from, to - 1 ));
68     from = to + 1;
69   }
70   // finish conversion
71   theGen->Flush();
72 #ifdef DUMP_CONVERSION
73   cout << endl << " ######## RESULT ######## " << endl<< endl;
74 #endif
75   // concat commands back into a script
76   TCollection_AsciiString aScript;
77   list< Handle(_pyCommand) >::iterator cmd = theGen->GetCommands().begin();
78   for ( ; cmd != theGen->GetCommands().end(); ++cmd ) {
79 #ifdef DUMP_CONVERSION
80     cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
81 #endif
82     if ( !(*cmd)->IsEmpty() ) {
83       aScript += "\n";
84       aScript += (*cmd)->GetString();
85     }
86   }
87   aScript += "\n";
88
89   theGen.Nullify();
90
91   return aScript;
92 }
93
94 //================================================================================
95 /*!
96  * \brief _pyGen constructor
97  */
98 //================================================================================
99
100 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
101   : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
102     myID2AccessorMethod( theEntry2AccessorMethod )
103 {
104   myNbCommands = 0;
105   // make that GetID() to return TPythonDump::SMESHGenName()
106   GetCreationCmd()->GetString() += "=";
107 }
108
109 //================================================================================
110 /*!
111  * \brief Convert a command using a specific converter
112   * \param theCommand - the command to convert
113   * \retval bool - convertion result
114  */
115 //================================================================================
116
117 void _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
118 {
119   // store theCommand in the sequence
120   myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
121
122   Handle(_pyCommand) aCommand = myCommands.back();
123 #ifdef DUMP_CONVERSION
124   cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
125 #endif
126
127   _pyID objID = aCommand->GetObject();
128
129   if ( objID.IsEmpty() )
130     return;
131
132   // SMESH_Gen method?
133   if ( objID == this->GetID() ) {
134     this->Process( aCommand );
135     return;
136   }
137   // SMESH_Mesh method?
138   map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
139   if ( id_mesh != myMeshes.end() ) {
140     id_mesh->second->Process( aCommand );
141     return;
142   }
143   // SMESH_Hypothesis method
144   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
145   for ( ; hyp != myHypos.end(); ++hyp )
146     if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() )
147       (*hyp)->Process( aCommand );
148
149   // Mesh provides SMESH_IDSource interface used in SMESH_MeshEditor.
150   // Add access to wrapped mesh
151   if ( objID == TPythonDump::MeshEditorName() ) {
152     // in all SMESH_MeshEditor's commands, a SMESH_IDSource is the first arg
153     id_mesh = myMeshes.find( aCommand->GetArg( 1 ));
154     if ( id_mesh != myMeshes.end() )
155       aCommand->SetArg( 1 , aCommand->GetArg( 1 ) + ".GetMesh()" );
156   }
157 }
158
159 //================================================================================
160 /*!
161  * \brief Convert the command or remember it for later conversion 
162   * \param theCommand - The python command calling a method of SMESH_Gen
163  */
164 //================================================================================
165
166 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
167 {
168   // there are methods to convert:
169   // CreateMesh( shape )
170   // CreateHypothesis( theHypType, theLibName )
171   // Compute( mesh, geom )
172
173   if ( theCommand->GetMethod() == "CreateMesh" )
174   {
175     Handle(_pyMesh) mesh = new _pyMesh( theCommand );
176     myMeshes.insert( make_pair( mesh->GetID(), mesh ));
177     return;
178   }
179
180   // CreateHypothesis()
181   if ( theCommand->GetMethod() == "CreateHypothesis" )
182   {
183     myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
184     return;
185   }
186
187   // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
188   if ( theCommand->GetMethod() == "Compute" )
189   {
190     const _pyID& meshID = theCommand->GetArg( 1 );
191     map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
192     if ( id_mesh != myMeshes.end() ) {
193       theCommand->SetObject( meshID );
194       theCommand->RemoveArgs();
195       id_mesh->second->Flush();
196       return;
197     }
198   }
199
200   // smeshgen.Method() --> smesh.smesh.Method()
201   theCommand->SetObject( SMESH_2smeshpy::GenName() );
202 }
203
204 //================================================================================
205 /*!
206  * \brief Convert the remembered commands
207  */
208 //================================================================================
209
210 void _pyGen::Flush()
211 {
212   map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
213   for ( ; id_mesh != myMeshes.end(); ++id_mesh )
214     if ( ! id_mesh->second.IsNull() )
215       id_mesh->second->Flush();
216
217   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
218   for ( ; hyp != myHypos.end(); ++hyp )
219     if ( !hyp->IsNull() ) {
220       (*hyp)->Flush();
221       // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
222       if ( !(*hyp)->GetCreationCmd()->IsEmpty() )
223         (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
224     }
225 }
226
227 //================================================================================
228 /*!
229  * \brief Find hypothesis by ID (entry)
230   * \param theHypID - The hypothesis ID
231   * \retval Handle(_pyHypothesis) - The found hypothesis
232  */
233 //================================================================================
234
235 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
236 {
237   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
238   for ( ; hyp != myHypos.end(); ++hyp )
239     if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
240       return *hyp;
241   return Handle(_pyHypothesis)();
242 }
243
244 //================================================================================
245 /*!
246  * \brief Find algorithm the created algorithm
247   * \param theGeom - The shape ID the algorithm was created on
248   * \param theMesh - The mesh ID that created the algorithm
249   * \param dim - The algo dimension
250   * \retval Handle(_pyHypothesis) - The found algo
251  */
252 //================================================================================
253
254 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
255                                       const TCollection_AsciiString& theAlgoType )
256 {
257   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
258   for ( ; hyp != myHypos.end(); ++hyp )
259     if ( !hyp->IsNull() &&
260          (*hyp)->IsAlgo() &&
261          (*hyp)->GetType() == theAlgoType &&
262          (*hyp)->GetGeom() == theGeom &&
263          (*hyp)->GetMesh() == theMesh )
264       return *hyp;
265   return 0;
266 }
267
268 //================================================================================
269 /*!
270  * \brief Change order of commands in the script
271   * \param theCmd1 - One command
272   * \param theCmd2 - Another command
273  */
274 //================================================================================
275
276 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
277 {
278   list< Handle(_pyCommand) >::iterator pos1, pos2;
279   pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
280   pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
281   myCommands.insert( pos1, theCmd2 );
282   myCommands.insert( pos2, theCmd1 );
283   myCommands.erase( pos1 );
284   myCommands.erase( pos2 );
285
286   int nb1 = theCmd1->GetOrderNb();
287   theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
288   theCmd2->SetOrderNb( nb1 );
289 }
290 //================================================================================
291 /*!
292  * \brief Set one command after the other
293   * \param theCmd - Command to move
294   * \param theAfterCmd - Command ater which to insert the first one
295  */
296 //================================================================================
297
298 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
299 {
300   list< Handle(_pyCommand) >::iterator pos;
301   pos = find( myCommands.begin(), myCommands.end(), theCmd );
302   myCommands.erase( pos );
303   pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
304   myCommands.insert( ++pos, theCmd );
305
306   int i = 1;
307   for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
308     (*pos)->SetOrderNb( i++ );
309 }
310
311 //================================================================================
312 /*!
313  * \brief Set method to access to object wrapped with python class
314   * \param theID - The wrapped object entry
315   * \param theMethod - The accessor method
316  */
317 //================================================================================
318
319 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
320 {
321   myID2AccessorMethod.Bind( theID, (char*) theMethod );
322 }
323
324 //================================================================================
325 /*!
326  * \brief Find out type of geom group
327   * \param grpID - The geom group entry
328   * \retval int - The type
329  */
330 //================================================================================
331
332 static bool sameGroupType( const _pyID&                   grpID,
333                            const TCollection_AsciiString& theType)
334 {
335   // define group type as smesh.Mesh.Group() does
336   int type = -1;
337   SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
338   SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
339   if ( !aSObj->_is_nil() ) {
340     GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
341     if ( !aGeomObj->_is_nil() ) {
342       switch ( aGeomObj->GetShapeType() ) {
343       case GEOM::VERTEX: type = SMESH::NODE; break;
344       case GEOM::EDGE:   type = SMESH::EDGE; break;
345       case GEOM::FACE:   type = SMESH::FACE; break;
346       case GEOM::SOLID:
347       case GEOM::SHELL:  type = SMESH::VOLUME; break;
348       case GEOM::COMPOUND: {
349         GEOM::GEOM_Gen_var aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
350         if ( !aGeomGen->_is_nil() ) {
351           GEOM::GEOM_IGroupOperations_var aGrpOp =
352             aGeomGen->GetIGroupOperations( study->StudyId() );
353           if ( !aGrpOp->_is_nil() ) {
354             switch ( aGrpOp->GetType( aGeomObj )) {
355             case TopAbs_VERTEX: type = SMESH::NODE; break;
356             case TopAbs_EDGE:   type = SMESH::EDGE; break;
357             case TopAbs_FACE:   type = SMESH::FACE; break;
358             case TopAbs_SOLID:  type = SMESH::VOLUME; break;
359             default:;
360             }
361           }
362         }
363       }
364       default:;
365       }
366     }
367   }
368   if ( type < 0 ) {
369     MESSAGE("Type of the group " << grpID << " not found");
370     return false;
371   }
372   if ( theType.IsIntegerValue() )
373     return type == theType.IntegerValue();
374
375   switch ( type ) {
376   case SMESH::NODE:   return theType.Location( "NODE", 1, theType.Length() );
377   case SMESH::EDGE:   return theType.Location( "EDGE", 1, theType.Length() );
378   case SMESH::FACE:   return theType.Location( "FACE", 1, theType.Length() );
379   case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
380   default:;
381   }
382   return false;
383 }
384
385 //================================================================================
386 /*!
387  * \brief 
388   * \param theCreationCmd - 
389  */
390 //================================================================================
391
392 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd): _pyObject(theCreationCmd)
393 {
394   // convert my creation command
395   Handle(_pyCommand) creationCmd = GetCreationCmd();
396   creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
397   creationCmd->SetMethod( "Mesh" );
398
399   theGen->SetAccessorMethod( GetID(), "GetMesh()" );
400 }
401
402 //================================================================================
403 /*!
404             case brief:
405             default:
406   * \param theCommand - Engine method called for this mesh
407  */
408 //================================================================================
409
410 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
411 {
412   // smesh.py wraps the following methods:
413   //
414   // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
415   //     --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
416   // 2. AddHypothesis(geom, hyp)
417   //     --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
418   // 3. CreateGroupFromGEOM(type, name, grp)
419   //     --> in Mesh.Group(grp, name="")
420   // 4. ExportToMED(f, opt, version)
421   //     --> in Mesh.ExportToMED( f, version, opt=0 )
422   // 5. ExportMED(f, opt)
423   //     --> in Mesh.ExportMED( f,opt=0 )
424   // 6. ExportDAT(f)
425   //     --> in Mesh.ExportDAT( f )
426   // 7. ExportUNV(f)
427   //     --> in Mesh.ExportUNV(f)
428   // 8. ExportSTL(f, ascii)
429   //     --> in Mesh.ExportSTL(f, ascii=1)
430
431   const TCollection_AsciiString method = theCommand->GetMethod();
432   if ( method == "GetSubMesh" ) {
433     mySubmeshes.push_back( theCommand );
434   }
435   else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
436     myAddHypCmds.push_back( theCommand );
437     // set mesh to hypo
438     const _pyID& hypID = theCommand->GetArg( 2 );
439     Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
440     if ( !hyp.IsNull() && hyp->GetMesh().IsEmpty() )
441       hyp->SetMesh( this->GetID() );
442   }
443   else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
444     _pyID grp = theCommand->GetArg( 3 );
445     if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
446       theCommand->SetMethod( "Group" );
447       theCommand->RemoveArgs();
448       theCommand->SetArg( 1, grp );
449     }
450     else {
451       AddMeshAccess( theCommand );
452     }
453   }
454   else if ( method == "ExportToMED" ) {//(f, opt, version)
455     // --> (f, version, opt)
456     _pyID opt = theCommand->GetArg( 2 );
457     _pyID ver = theCommand->GetArg( 3 );
458     theCommand->SetArg( 2, ver );
459     theCommand->SetArg( 3, opt );
460   }
461   else if ( method == "RemoveHypothesis" ) // (geom, hyp)
462   {
463     const _pyID & hypID = theCommand->GetArg( 2 );
464
465     // check if this mesh still has corresponding addition command
466     bool hasAddCmd = false;
467     list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
468     while ( cmd != myAddHypCmds.end() )
469     {
470       // AddHypothesis(geom, hyp)
471       if ( hypID == (*cmd)->GetArg( 2 )) { // erase both commands
472         theCommand->Clear();
473         (*cmd)->Clear();
474         cmd = myAddHypCmds.erase( cmd );
475         hasAddCmd = true;
476       }
477       else {
478         ++cmd;
479       }
480     }
481     if ( ! hasAddCmd ) {
482       // access to wrapped mesh
483       AddMeshAccess( theCommand );
484       // access to wrapped algo
485       Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
486       if ( !hyp.IsNull() && hyp->IsAlgo() && hyp->IsWrapped() )
487         theCommand->SetArg( 2, theCommand->GetArg( 2 ) + ".GetAlgorithm()" );
488     }
489   }
490   else { // apply theCommand to the mesh wrapped by smeshpy mesh
491     AddMeshAccess( theCommand );
492   }
493 }
494
495 //================================================================================
496 /*!
497  * \brief Convert creation and addition of all algos and hypos
498  */
499 //================================================================================
500
501 void _pyMesh::Flush()
502 {
503   list < Handle(_pyCommand) >::iterator cmd, cmd2;
504   map< _pyID, Handle(_pyCommand) > algo2additionCmd;
505
506   // try to convert algo addition like this:
507   // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
508   for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
509   {
510     Handle(_pyCommand) addCmd = *cmd;
511     const _pyID& algoID = addCmd->GetArg( 2 );
512     Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
513     if ( algo.IsNull() || !algo->IsAlgo() )
514       continue;
515     // try to convert
516     _pyID geom = addCmd->GetArg( 1 );
517     if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
518     {
519       algo2additionCmd[ algo->GetID() ] = addCmd;
520
521       if ( geom != GetGeom() ) // local algo
522       {
523         // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
524         addCmd->SetArg( addCmd->GetNbArgs() + 1,
525                         TCollection_AsciiString( "geom=" ) + geom );
526         // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
527         for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
528           Handle(_pyCommand) subCmd = *cmd2;
529           if ( geom == subCmd->GetArg( 1 )) {
530             subCmd->SetObject( algo->GetID() );
531             subCmd->RemoveArgs();
532             if ( addCmd->GetOrderNb() > subCmd->GetOrderNb() )
533               theGen->SetCommandAfter( subCmd, addCmd );
534           }
535         }
536       }
537     }
538     else // ALGO was already created
539     {
540       // mesh.AddHypothesis(geom, ALGO ) --> mesh.GetMesh().AddHypothesis(geom, ALGO )
541       AddMeshAccess( addCmd );
542       // mesh.GetMesh().AddHypothesis(geom, ALGO ) ->
543       // mesh.GetMesh().AddHypothesis(geom, ALGO.GetAlgorithm() )
544       addCmd->SetArg( 2, addCmd->GetArg( 2 ) + ".GetAlgorithm()" );
545     }
546   }
547
548   // try to convert hypo addition like this:
549   // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
550   for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
551   {
552     Handle(_pyCommand) addCmd = *cmd;
553     const _pyID& hypID = addCmd->GetArg( 2 );
554     Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
555     if ( hyp.IsNull() || hyp->IsAlgo() )
556       continue;
557     const _pyID& geom = addCmd->GetArg( 1 );
558     // find algo created on <geom> for this mesh
559     Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, this->GetID(), hyp->GetType() );
560     //_pyID algoID = algo.IsNull() ? "" : algo->GetID();
561     if ( !algo.IsNull() && hyp->Addition2Creation( addCmd, this->GetID() )) // OK
562     {
563       addCmd->SetObject( algo->GetID() );
564       Handle(_pyCommand) algoCmd = algo2additionCmd[ algo->GetID() ];
565       if ( !algoCmd.IsNull() && algoCmd->GetOrderNb() > addCmd->GetOrderNb() )
566         // algo was created later than hyp
567         theGen->ExchangeCommands( algoCmd, addCmd );
568     }
569     else
570     {
571       AddMeshAccess( addCmd );
572     }
573   }
574
575   // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
576   for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
577     Handle(_pyCommand) subCmd = *cmd;
578     if ( subCmd->GetNbArgs() > 0 )
579       AddMeshAccess( subCmd );
580   }
581   myAddHypCmds.clear();
582   mySubmeshes.clear();
583 }
584
585 //================================================================================
586 /*!
587  * \brief _pyHypothesis constructor
588   * \param theCreationCmd - 
589  */
590 //================================================================================
591
592 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
593   _pyObject( theCreationCmd )
594 {
595   myDim = myIsAlgo = /*myIsLocal = */myIsWrapped = myIsConverted = false;
596 }
597
598 //================================================================================
599 /*!
600  * \brief Creates algorithm or hypothesis
601   * \param theCreationCmd - The engine command creating a hypothesis
602   * \retval Handle(_pyHypothesis) - Result _pyHypothesis
603  */
604 //================================================================================
605
606 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
607 {
608   // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
609   ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
610
611   Handle(_pyHypothesis) hyp, algo;
612
613   // "theHypType"
614   const TCollection_AsciiString & hypTypeWithQuotes = theCreationCmd->GetArg( 1 );
615   if ( hypTypeWithQuotes.IsEmpty() )
616     return hyp;
617   // theHypType
618   TCollection_AsciiString  hypType =
619     hypTypeWithQuotes.SubString( 2, hypTypeWithQuotes.Length() - 1 );
620
621   algo = new _pyAlgorithm( theCreationCmd );
622   hyp  = new _pyHypothesis( theCreationCmd );
623
624   // 1D Regular_1D ----------
625   if ( hypType == "Regular_1D" ) {
626     algo->myDim = 1;
627     algo->myCreationMethod = "Segment";
628   }
629   else if ( hypType == "LocalLength" ) {
630     hyp->myDim = 1;
631     hyp->myCreationMethod = "LocalLength";
632     hyp->myType = "Regular_1D";
633     hyp->myArgMethods.Append( "SetLength" );
634   }
635   else if ( hypType == "NumberOfSegments" ) {
636     hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
637     hyp->myDim = 1;
638     hyp->myCreationMethod = "NumberOfSegments";
639     hyp->myType = "Regular_1D";
640     hyp->myArgMethods.Append( "SetNumberOfSegments" );
641     hyp->myArgMethods.Append( "SetScaleFactor" );
642   }
643   else if ( hypType == "Arithmetic1D" ) {
644     hyp = new _pyComplexParamHypo( theCreationCmd );
645     hyp->myDim = 1;
646     hyp->myCreationMethod = "Arithmetic1D";
647     hyp->myType = "Regular_1D";
648   }
649   else if ( hypType == "StartEndLength" ) {
650     hyp = new _pyComplexParamHypo( theCreationCmd );
651     hyp->myDim = 1;
652     hyp->myCreationMethod = "StartEndLength";
653     hyp->myType = "Regular_1D";
654   }
655   else if ( hypType == "Deflection1D" ) {
656     hyp->myDim = 1;
657     hyp->myCreationMethod = "Deflection1D";
658     hyp->myArgMethods.Append( "SetDeflection" );
659     hyp->myType = "Regular_1D";
660   }
661   else if ( hypType == "Propagation" ) {
662     hyp->myDim = 1;
663     hyp->myCreationMethod = "Propagation";
664     hyp->myType = "Regular_1D";
665   }
666   else if ( hypType == "AutomaticLength" ) {
667     hyp->myDim = 1;
668     hyp->myCreationMethod = "AutomaticLength";
669     hyp->myType = "Regular_1D";
670   }
671   // 1D Python_1D ----------
672   else if ( hypType == "Python_1D" ) {
673     algo->myDim = 1;
674     algo->myCreationMethod = "Segment";
675     algo->myArgs.Append( "algo=smesh.PYTHON");
676   }
677   else if ( hypType == "PythonSplit1D" ) {
678     hyp->myDim = 1;
679     hyp->myCreationMethod = "PythonSplit1D";
680     hyp->myType = "Python_1D";
681     hyp->myArgMethods.Append( "SetNumberOfSegments");
682     hyp->myArgMethods.Append( "SetPythonLog10RatioFunction");
683   }
684   // 2D ----------
685   else if ( hypType == "MEFISTO_2D" ) {
686     algo->myDim = 2;
687     algo->myCreationMethod = "Triangle";
688   }
689   else if ( hypType == "MaxElementArea" ) {
690     hyp->myDim = 2;
691     hyp->myCreationMethod = "MaxElementArea";
692     hyp->myType = "MEFISTO_2D";
693     hyp->myArgMethods.Append( "SetMaxElementArea");
694   }
695   else if ( hypType == "LengthFromEdges" ) {
696     hyp->myDim = 2;
697     hyp->myCreationMethod = "LengthFromEdges";
698     hyp->myType = "MEFISTO_2D";
699   }
700   else if ( hypType == "Quadrangle_2D" ) {
701     algo->myDim = 2;
702     algo->myCreationMethod = "Quadrangle";
703   }
704   else if ( hypType == "QuadranglePreference" ) {
705     hyp->myDim = 2;
706     hyp->myCreationMethod = "QuadranglePreference";
707     hyp->myType = "MEFISTO_2D";
708   }
709   // 3D ----------
710   else if ( hypType == "NETGEN_3D") {
711     algo->myDim = 3;
712     algo->myCreationMethod = "Tetrahedron";
713     algo->myArgs.Append( "algo=smesh.NETGEN" );
714   }
715   else if ( hypType == "MaxElementVolume") {
716     hyp->myDim = 3;
717     hyp->myCreationMethod = "MaxElementVolume";
718     hyp->myType = "NETGEN_3D";
719     hyp->myArgMethods.Append( "SetMaxElementVolume" );
720   }
721   else if ( hypType == "GHS3D_3D" ) {
722     algo->myDim = 3;
723     algo->myCreationMethod = "Tetrahedron";
724     algo->myArgs.Append( "algo=smesh.GHS3D" );
725   }
726   else if ( hypType == "Hexa_3D" ) {
727     algo->myDim = 3;
728     algo->myCreationMethod = "Hexahedron";
729   }
730
731   if ( algo->GetDim() ) {
732     algo->myType = hypType;
733     return algo;
734   }
735   return hyp;
736 }
737
738 //================================================================================
739 /*!
740  * \brief Convert the command adding a hypothesis to mesh into a smesh command
741   * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
742   * \param theAlgo - The algo that can create this hypo
743   * \retval bool - false if the command cant be converted
744  */
745 //================================================================================
746
747 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
748                                        const _pyID&              theMesh)
749 {
750   ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
751
752   if ( !IsWrappable( theMesh ))
753     return false;
754
755   myIsWrapped = true;
756
757   if ( myIsWrapped )
758   {
759     // mesh.AddHypothesis(geom,hyp) --> hyp = theMesh.myCreationMethod(args)
760     theCmd->SetResultValue( GetID() );
761     theCmd->SetObject( theMesh );
762     theCmd->SetMethod( myCreationMethod );
763     // set args
764     theCmd->RemoveArgs();
765     for ( int i = 1; i <= myArgs.Length(); ++i ) {
766       if ( !myArgs( i ).IsEmpty() )
767         theCmd->SetArg( i, myArgs( i ));
768       else
769         theCmd->SetArg( i, "[]");
770     }
771
772     // clear commands setting arg values
773     list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
774     for ( ; argCmd != myArgCommands.end(); ++argCmd )
775       (*argCmd)->Clear();
776   }
777   else
778   {
779 //     // set arg commands after hypo creation
780 //     list<Handle(_pyCommand)>::iterator argCmd = myArgCommands.begin();
781 //     for ( ; argCmd != myArgCommands.end(); ++argCmd )
782 //       if ( !(*argCmd)->IsEmpty() && GetCommandNb() > (*argCmd)->GetOrderNb() )
783 //         theGen->ExchangeCommands( GetCreationCmd(), *argCmd );
784   }
785
786   // set unknown arg commands after hypo creation
787   Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
788   list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
789   for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
790     if ( !(*cmd)->IsEmpty() && afterCmd->GetOrderNb() > (*cmd)->GetOrderNb() ) {
791       theGen->SetCommandAfter( *cmd, afterCmd );
792       afterCmd = *cmd;
793     }
794   }
795   myArgCommands.clear();
796   myUnknownCommands.clear();
797
798   return myIsWrapped;
799 }
800
801 //================================================================================
802 /*!
803  * \brief Remember hypothesis parameter values
804  * \param theCommand - The called hypothesis method
805  */
806 //================================================================================
807
808 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
809 {
810   ASSERT( !myIsAlgo );
811   // set args
812   for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
813     if ( myArgMethods( i ) == theCommand->GetMethod() ) {
814       while ( myArgs.Length() < i )
815         myArgs.Append( "[]" );
816       myArgs( i ) = theCommand->GetArg( 1 ); // arg value
817       myArgCommands.push_back( theCommand );
818       return;
819     }
820   }
821   myUnknownCommands.push_back( theCommand );
822 }
823
824 //================================================================================
825 /*!
826  * \brief Finish conversion
827  */
828 //================================================================================
829
830 void _pyHypothesis::Flush()
831 {
832   if ( IsWrapped() )
833     GetCreationCmd()->Clear();
834 }
835
836 //================================================================================
837 /*!
838  * \brief Remember hypothesis parameter values
839   * \param theCommand - The called hypothesis method
840  */
841 //================================================================================
842
843 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
844 {
845   // ex: hyp.SetLength(start, 1)
846   //     hyp.SetLength(end,   0)
847   ASSERT(( theCommand->GetMethod() == "SetLength" ));
848   ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
849   int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
850   while ( myArgs.Length() < i )
851     myArgs.Append( "[]" );
852   myArgs( i ) = theCommand->GetArg( 1 ); // arg value
853   myArgCommands.push_back( theCommand );
854 }
855
856 //================================================================================
857 /*!
858  * \brief additionally to Addition2Creation, clears SetDistrType() command
859   * \param theCmd - AddHypothesis() command
860   * \param theMesh - mesh to which a hypothesis is added
861   * \retval bool - convertion result
862  */
863 //================================================================================
864
865 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
866                                                 const _pyID&              theMesh)
867 {
868   if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
869     list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
870     for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
871       // clear SetDistrType()
872       if ( (*cmd)->GetString().Location( "SetDistrType", 1, (*cmd)->Length() ))
873         (*cmd)->Clear();
874     }
875   }
876   return _pyHypothesis::Addition2Creation( theCmd, theMesh );
877 }
878
879 //================================================================================
880 /*!
881  * \brief _pyAlgorithm constructor
882   * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
883  */
884 //================================================================================
885
886 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
887   : _pyHypothesis( theCreationCmd )
888 {
889   myIsAlgo = true;
890 }
891
892 //================================================================================
893 /*!
894  * \brief Convert the command adding an algorithm to mesh
895   * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
896   * \param theMesh - The mesh needing this algo 
897   * \retval bool - false if the command cant be converted
898  */
899 //================================================================================
900   
901 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
902                                       const _pyID&              theMeshID)
903 {
904   if ( IsWrappable( theMeshID )) {
905
906     myGeom = theCmd->GetArg( 1 );
907
908     // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
909     if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
910       theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
911       return true;
912     }
913   }
914   return false;
915 }
916
917 //================================================================================
918 /*!
919  * \brief Return starting position of a part of python command
920   * \param thePartIndex - The index of command part
921   * \retval int - Part position
922  */
923 //================================================================================
924
925 int _pyCommand::GetBegPos( int thePartIndex )
926 {
927   if ( IsEmpty() )
928     return EMPTY;
929   if ( myBegPos.Length() < thePartIndex )
930     return UNKNOWN;
931   return myBegPos( thePartIndex );
932 }
933
934 //================================================================================
935 /*!
936  * \brief Store starting position of a part of python command
937   * \param thePartIndex - The index of command part
938   * \param thePosition - Part position
939  */
940 //================================================================================
941
942 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
943 {
944   while ( myBegPos.Length() < thePartIndex )
945     myBegPos.Append( UNKNOWN );
946   myBegPos( thePartIndex ) = thePosition;
947 }
948
949 //================================================================================
950 /*!
951  * \brief Return substring of python command looking like ResultValue = Obj.Meth()
952   * \retval const TCollection_AsciiString & - ResultValue substring
953  */
954 //================================================================================
955
956 const TCollection_AsciiString & _pyCommand::GetResultValue()
957 {
958   if ( GetBegPos( RESULT_IND ) == UNKNOWN )
959   {
960     int begPos = myString.Location( "=", 1, Length() );
961     if ( begPos )
962       myRes = GetWord( myString, begPos, false );
963     else
964       begPos = EMPTY;
965     SetBegPos( RESULT_IND, begPos );
966   }
967   return myRes;
968 }
969
970 //================================================================================
971 /*!
972  * \brief Return substring of python command looking like ResVal = Object.Meth()
973   * \retval const TCollection_AsciiString & - Object substring
974  */
975 //================================================================================
976
977 const TCollection_AsciiString & _pyCommand::GetObject()
978 {
979   if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
980   {
981     // beginning
982     int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
983     if ( begPos < 1 )
984       begPos = myString.Location( "=", 1, Length() ) + 1;
985     // store
986     myObj = GetWord( myString, begPos, true );
987     SetBegPos( OBJECT_IND, begPos );
988   }
989   //SCRUTE(myObj);
990   return myObj;
991 }
992
993 //================================================================================
994 /*!
995  * \brief Return substring of python command looking like ResVal = Obj.Method()
996   * \retval const TCollection_AsciiString & - Method substring
997  */
998 //================================================================================
999
1000 const TCollection_AsciiString & _pyCommand::GetMethod()
1001 {
1002   if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1003   {
1004     // beginning
1005     int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1006     bool forward = true;
1007     if ( begPos < 1 ) {
1008       begPos = myString.Location( "(", 1, Length() ) - 1;
1009       forward = false;
1010     }
1011     // store
1012     myMeth = GetWord( myString, begPos, forward );
1013     SetBegPos( METHOD_IND, begPos );
1014   }
1015   //SCRUTE(myMeth);
1016   return myMeth;
1017 }
1018
1019 //================================================================================
1020 /*!
1021  * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1022   * \retval const TCollection_AsciiString & - Arg<index> substring
1023  */
1024 //================================================================================
1025
1026 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1027 {
1028   if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1029   {
1030     // find all args
1031     int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1032     if ( begPos < 1 )
1033       begPos = myString.Location( "(", 1, Length() ) + 1;
1034
1035     int i = 0, prevLen = 0;
1036     while ( begPos != EMPTY ) {
1037       begPos += prevLen;
1038       // check if we are looking at the closing parenthesis
1039       while ( begPos <= Length() && isspace( myString.Value( begPos )))
1040         ++begPos;
1041       if ( begPos > Length() || myString.Value( begPos ) == ')' )
1042         break;
1043       myArgs.Append( GetWord( myString, begPos, true, true ));
1044       SetBegPos( ARG1_IND + i, begPos );
1045       prevLen = myArgs.Last().Length();
1046       if ( prevLen == 0 )
1047         myArgs.Remove( myArgs.Length() ); // no more args
1048       i++;
1049     }
1050   }
1051   if ( myArgs.Length() < index )
1052     return theEmptyString;
1053   return myArgs( index );
1054 }
1055
1056 //================================================================================
1057 /*!
1058  * \brief Check if char is a word part
1059   * \param c - The character to check
1060   * \retval bool - The check result
1061  */
1062 //================================================================================
1063
1064 static inline bool isWord(const char c, const bool dotIsWord)
1065 {
1066   return
1067     !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1068 }
1069
1070 //================================================================================
1071 /*!
1072  * \brief Looks for a word in the string and returns word's beginning
1073   * \param theString - The input string
1074   * \param theStartPos - The position to start the search, returning word's beginning
1075   * \param theForward - The search direction
1076   * \retval TCollection_AsciiString - The found word
1077  */
1078 //================================================================================
1079
1080 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1081                                             int &      theStartPos,
1082                                             const bool theForward,
1083                                             const bool dotIsWord )
1084 {
1085   int beg = theStartPos, end = theStartPos;
1086   theStartPos = EMPTY;
1087   if ( beg < 1 || beg > theString.Length() )
1088     return theEmptyString;
1089
1090   if ( theForward ) { // search forward
1091     // beg
1092     while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1093       ++beg;
1094     if ( beg > theString.Length() )
1095       return theEmptyString; // no word found
1096     // end
1097     end = beg + 1;
1098     while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1099       ++end;
1100     --end;
1101   }
1102   else {  // search backward
1103     // end
1104     while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1105       --end;
1106     if ( end == 0 )
1107       return theEmptyString; // no word found
1108     beg = end - 1;
1109     while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1110       --beg;
1111     ++beg;
1112   }
1113   theStartPos = beg;
1114   //cout << theString << " ---- " << beg << " - " << end << endl;
1115   return theString.SubString( beg, end );
1116 }
1117
1118 //================================================================================
1119 /*!
1120  * \brief Look for position where not space char is
1121   * \param theString - The string 
1122   * \param thePos - The position to search from and which returns result
1123   * \retval bool - false if there are only space after thePos in theString
1124  * 
1125  * 
1126  */
1127 //================================================================================
1128
1129 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1130 {
1131   if ( thePos < 1 || thePos > theString.Length() )
1132     return false;
1133
1134   while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1135     ++thePos;
1136
1137   return thePos <= theString.Length();
1138 }
1139
1140 //================================================================================
1141 /*!
1142  * \brief Modify a part of the command
1143   * \param thePartIndex - The index of the part
1144   * \param thePart - The new part string
1145   * \param theOldPart - The old part
1146  */
1147 //================================================================================
1148
1149 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1150                         TCollection_AsciiString& theOldPart)
1151 {
1152   int pos = GetBegPos( thePartIndex );
1153   if ( pos <= Length() && theOldPart != thePart)
1154   {
1155     TCollection_AsciiString seperator;
1156     if ( pos < 1 ) {
1157       pos = GetBegPos( thePartIndex + 1 );
1158       if ( pos < 1 ) return;
1159       switch ( thePartIndex ) {
1160       case RESULT_IND: seperator = " = "; break;
1161       case OBJECT_IND: seperator = "."; break;
1162       case METHOD_IND: seperator = "()"; break;
1163       default:;
1164       }
1165     }      
1166     myString.Remove( pos, theOldPart.Length() );
1167     if ( !seperator.IsEmpty() )
1168       myString.Insert( pos , seperator );
1169     myString.Insert( pos, thePart );
1170     // update starting positions of the following parts
1171     int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1172     for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1173       if ( myBegPos( i ) > 0 )
1174         myBegPos( i ) += posDelta;
1175     }
1176     theOldPart = thePart;
1177   }
1178 }
1179
1180 //================================================================================
1181 /*!
1182  * \brief Set agrument
1183   * \param index - The argument index, it counts from 1
1184   * \param theArg - The argument string
1185  */
1186 //================================================================================
1187
1188 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1189 {
1190   FindAllArgs();
1191   int argInd = ARG1_IND + index - 1;
1192   int pos = GetBegPos( argInd );
1193   if ( pos < 1 ) // no index-th arg exist, append inexistent args
1194   {
1195     // find a closing parenthesis
1196     pos = Length();
1197     while ( pos > 0 && myString.Value( pos ) != ')' )
1198       --pos;
1199     if ( pos == 0 ) { // no parentheses at all
1200       myString += "()";
1201       pos = Length();
1202     }
1203     while ( myArgs.Length() < index ) {
1204       if ( myArgs.Length() )
1205         myString.Insert( pos++, "," );
1206       myArgs.Append("None");
1207       myString.Insert( pos, myArgs.Last() );
1208       SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1209       pos += myArgs.Last().Length();
1210     }
1211   }
1212   SetPart( argInd, theArg, myArgs( index ));
1213 }
1214
1215 //================================================================================
1216 /*!
1217  * \brief Empty arg list
1218  */
1219 //================================================================================
1220
1221 void _pyCommand::RemoveArgs()
1222 {
1223   if ( int pos = myString.Location( '(', 1, Length() ))
1224     myString.Trunc( pos );
1225   myString += ")";
1226   myArgs.Clear();
1227   if ( myBegPos.Length() >= ARG1_IND )
1228     myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1229 }