Salome HOME
b06a856246e62d79206cb5f9c2d3d1332e5dde9d
[modules/smesh.git] / src / SMESH_I / SMESH_2smeshpy.cxx
1 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : SMESH_2D_Algo_i.hxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
29 // File      : SMESH_2smeshpy.cxx
30 // Created   : Fri Nov 18 13:20:10 2005
31 // Author    : Edward AGAPOV (eap)
32
33 #include "SMESH_2smeshpy.hxx"
34
35 #include "SMESH_Gen_i.hxx"
36 #include "utilities.h"
37 #include "SMESH_PythonDump.hxx"
38 #include "Resource_DataMapOfAsciiStringAsciiString.hxx"
39
40 IMPLEMENT_STANDARD_HANDLE (_pyObject          ,Standard_Transient);
41 IMPLEMENT_STANDARD_HANDLE (_pyCommand         ,Standard_Transient);
42 IMPLEMENT_STANDARD_HANDLE (_pyGen             ,_pyObject);
43 IMPLEMENT_STANDARD_HANDLE (_pyMesh            ,_pyObject);
44 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis      ,_pyObject);
45 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm       ,_pyHypothesis);
46 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
47 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
48
49 IMPLEMENT_STANDARD_RTTIEXT(_pyObject          ,Standard_Transient);
50 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand         ,Standard_Transient);
51 IMPLEMENT_STANDARD_RTTIEXT(_pyGen             ,_pyObject);
52 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh            ,_pyObject);
53 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis      ,_pyObject);
54 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm       ,_pyHypothesis);
55 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
56 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
57 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
58
59 using namespace std;
60 using SMESH::TPythonDump;
61
62 /*!
63  * \brief Container of commands into which the initial script is split.
64  *        It also contains data coresponding to SMESH_Gen contents
65  */
66 static Handle(_pyGen) theGen;
67
68 static TCollection_AsciiString theEmptyString;
69
70 //#define DUMP_CONVERSION
71
72 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
73 #undef DUMP_CONVERSION
74 #endif
75
76 //================================================================================
77 /*!
78  * \brief Convert python script using commands of smesh.py
79   * \param theScript - Input script
80   * \retval TCollection_AsciiString - Convertion result
81  */
82 //================================================================================
83
84 TCollection_AsciiString
85 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
86                               Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
87 {
88   theGen = new _pyGen( theEntry2AccessorMethod );
89
90   // split theScript into separate commands
91   int from = 1, end = theScript.Length(), to;
92   while ( from < end && ( to = theScript.Location( "\n", from, end )))
93   {
94     if ( to != from )
95       // cut out and store a command
96       theGen->AddCommand( theScript.SubString( from, to - 1 ));
97     from = to + 1;
98   }
99   // finish conversion
100   theGen->Flush();
101 #ifdef DUMP_CONVERSION
102   cout << endl << " ######## RESULT ######## " << endl<< endl;
103 #endif
104   // reorder commands after conversion
105   list< Handle(_pyCommand) >::iterator cmd;
106   bool orderChanges;
107   do {
108     orderChanges = false;
109     for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
110       if ( (*cmd)->SetDependentCmdsAfter() )
111         orderChanges = true;
112   } while ( orderChanges );
113   
114   // concat commands back into a script
115   TCollection_AsciiString aScript;
116   for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
117   {
118 #ifdef DUMP_CONVERSION
119     cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
120 #endif
121     if ( !(*cmd)->IsEmpty() ) {
122       aScript += "\n";
123       aScript += (*cmd)->GetString();
124     }
125   }
126   aScript += "\n";
127
128   theGen.Nullify();
129
130   return aScript;
131 }
132
133 //================================================================================
134 /*!
135  * \brief _pyGen constructor
136  */
137 //================================================================================
138
139 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
140   : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
141     myID2AccessorMethod( theEntry2AccessorMethod )
142 {
143   myNbCommands = 0;
144   myHasPattern = false;
145   // make that GetID() to return TPythonDump::SMESHGenName()
146   GetCreationCmd()->GetString() += "=";
147 }
148
149 //================================================================================
150 /*!
151  * \brief Convert a command using a specific converter
152   * \param theCommand - the command to convert
153  */
154 //================================================================================
155
156 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
157 {
158   // store theCommand in the sequence
159   myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
160
161   Handle(_pyCommand) aCommand = myCommands.back();
162 #ifdef DUMP_CONVERSION
163   cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
164 #endif
165
166   _pyID objID = aCommand->GetObject();
167
168   if ( objID.IsEmpty() )
169     return aCommand;
170
171   // SMESH_Gen method?
172   if ( objID == this->GetID() ) {
173     this->Process( aCommand );
174     return aCommand;
175   }
176   // SMESH_Mesh method?
177   map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
178   if ( id_mesh != myMeshes.end() ) {
179     id_mesh->second->Process( aCommand );
180     return aCommand;
181   }
182   // SMESH_Hypothesis method?
183   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
184   for ( ; hyp != myHypos.end(); ++hyp )
185     if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
186       (*hyp)->Process( aCommand );
187       return aCommand;
188     }
189
190   // Add access to a wrapped mesh
191   AddMeshAccessorMethod( aCommand );
192
193   // Add access to a wrapped algorithm
194   AddAlgoAccessorMethod( aCommand );
195
196   // PAL12227. PythonDump was not updated at proper time; result is
197   //     aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
198   // TypeError: __init__() takes exactly 11 arguments (10 given)
199   char wrongCommand[] = "SMESH.Filter.Criterion(";
200   if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
201   {
202     _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
203     // there must be 10 arguments, 5-th arg ThresholdID is missing,
204     const int wrongNbArgs = 9, missingArg = 5;
205     if ( tmpCmd.GetNbArgs() == wrongNbArgs )
206     {
207       for ( int i = wrongNbArgs; i > missingArg; --i )
208         tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
209       tmpCmd.SetArg(  missingArg, "''");
210       aCommand->GetString().Trunc( beg - 1 );
211       aCommand->GetString() += tmpCmd.GetString();
212     }
213   }
214   return aCommand;
215 }
216
217 //================================================================================
218 /*!
219  * \brief Convert the command or remember it for later conversion 
220   * \param theCommand - The python command calling a method of SMESH_Gen
221  */
222 //================================================================================
223
224 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
225 {
226   // there are methods to convert:
227   // CreateMesh( shape )
228   // CreateHypothesis( theHypType, theLibName )
229   // Compute( mesh, geom )
230
231   if ( theCommand->GetMethod() == "CreateMesh" )
232   {
233     Handle(_pyMesh) mesh = new _pyMesh( theCommand );
234     myMeshes.insert( make_pair( mesh->GetID(), mesh ));
235     return;
236   }
237
238   // CreateHypothesis()
239   if ( theCommand->GetMethod() == "CreateHypothesis" )
240   {
241     myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
242     return;
243   }
244
245   // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
246   if ( theCommand->GetMethod() == "Compute" )
247   {
248     const _pyID& meshID = theCommand->GetArg( 1 );
249     map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
250     if ( id_mesh != myMeshes.end() ) {
251       theCommand->SetObject( meshID );
252       theCommand->RemoveArgs();
253       id_mesh->second->Flush();
254       return;
255     }
256   }
257
258   // leave only one smeshgen.GetPattern() in the script
259   if ( theCommand->GetMethod() == "GetPattern" ) {
260     if ( myHasPattern ) {
261       theCommand->Clear();
262       return;
263     }
264     myHasPattern = true;
265   }
266
267   // smeshgen.Method() --> smesh.smesh.Method()
268   theCommand->SetObject( SMESH_2smeshpy::GenName() );
269 }
270
271 //================================================================================
272 /*!
273  * \brief Convert the remembered commands
274  */
275 //================================================================================
276
277 void _pyGen::Flush()
278 {
279   map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
280   for ( ; id_mesh != myMeshes.end(); ++id_mesh )
281     if ( ! id_mesh->second.IsNull() )
282       id_mesh->second->Flush();
283
284   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
285   for ( ; hyp != myHypos.end(); ++hyp )
286     if ( !hyp->IsNull() ) {
287       (*hyp)->Flush();
288       // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
289       if ( !(*hyp)->IsWrapped() )
290         (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
291     }
292 }
293
294 //================================================================================
295 /*!
296  * \brief Add access method to mesh that is object or arg
297   * \param theCmd - command to add access method
298   * \retval bool - true if added
299  */
300 //================================================================================
301
302 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
303 {
304   map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
305   for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
306     if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
307       return true;
308   }
309   return false;
310 }
311
312 //================================================================================
313 /*!
314  * \brief Add access method to algo that is object or arg
315   * \param theCmd - command to add access method
316   * \retval bool - true if added
317  */
318 //================================================================================
319
320 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
321 {
322   list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
323   for ( ; hyp != myHypos.end(); ++hyp ) {
324     if ( (*hyp)->IsAlgo() &&
325          theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
326       return true;
327   }
328   return false;
329 }
330
331 //================================================================================
332 /*!
333  * \brief Find hypothesis by ID (entry)
334   * \param theHypID - The hypothesis ID
335   * \retval Handle(_pyHypothesis) - The found hypothesis
336  */
337 //================================================================================
338
339 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
340 {
341   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
342   for ( ; hyp != myHypos.end(); ++hyp )
343     if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
344       return *hyp;
345   return Handle(_pyHypothesis)();
346 }
347
348 //================================================================================
349 /*!
350  * \brief Find algorithm the created algorithm
351   * \param theGeom - The shape ID the algorithm was created on
352   * \param theMesh - The mesh ID that created the algorithm
353   * \param dim - The algo dimension
354   * \retval Handle(_pyHypothesis) - The found algo
355  */
356 //================================================================================
357
358 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
359                                       const TCollection_AsciiString& theAlgoType )
360 {
361   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
362   for ( ; hyp != myHypos.end(); ++hyp )
363     if ( !hyp->IsNull() &&
364          (*hyp)->IsAlgo() &&
365          (*hyp)->GetType() == theAlgoType &&
366          (*hyp)->GetGeom() == theGeom &&
367          (*hyp)->GetMesh() == theMesh )
368       return *hyp;
369   return 0;
370 }
371
372 //================================================================================
373 /*!
374  * \brief Change order of commands in the script
375   * \param theCmd1 - One command
376   * \param theCmd2 - Another command
377  */
378 //================================================================================
379
380 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
381 {
382   list< Handle(_pyCommand) >::iterator pos1, pos2;
383   pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
384   pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
385   myCommands.insert( pos1, theCmd2 );
386   myCommands.insert( pos2, theCmd1 );
387   myCommands.erase( pos1 );
388   myCommands.erase( pos2 );
389
390   int nb1 = theCmd1->GetOrderNb();
391   theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
392   theCmd2->SetOrderNb( nb1 );
393 //   cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
394 //        << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
395 }
396
397 //================================================================================
398 /*!
399  * \brief Set one command after the other
400   * \param theCmd - Command to move
401   * \param theAfterCmd - Command ater which to insert the first one
402  */
403 //================================================================================
404
405 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
406 {
407 //   cout << "SET\t" << theCmd->GetString() << endl << "AFTER\t" << theAfterCmd->GetString() << endl << endl;
408   list< Handle(_pyCommand) >::iterator pos;
409   pos = find( myCommands.begin(), myCommands.end(), theCmd );
410   myCommands.erase( pos );
411   pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
412   myCommands.insert( ++pos, theCmd );
413
414   int i = 1;
415   for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
416     (*pos)->SetOrderNb( i++ );
417 }
418
419 //================================================================================
420 /*!
421  * \brief Set method to access to object wrapped with python class
422   * \param theID - The wrapped object entry
423   * \param theMethod - The accessor method
424  */
425 //================================================================================
426
427 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
428 {
429   myID2AccessorMethod.Bind( theID, (char*) theMethod );
430 }
431
432 //================================================================================
433 /*!
434  * \brief Find out type of geom group
435   * \param grpID - The geom group entry
436   * \retval int - The type
437  */
438 //================================================================================
439
440 static bool sameGroupType( const _pyID&                   grpID,
441                            const TCollection_AsciiString& theType)
442 {
443   // define group type as smesh.Mesh.Group() does
444   int type = -1;
445   SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
446   SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
447   if ( !aSObj->_is_nil() ) {
448     GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
449     if ( !aGeomObj->_is_nil() ) {
450       switch ( aGeomObj->GetShapeType() ) {
451       case GEOM::VERTEX: type = SMESH::NODE; break;
452       case GEOM::EDGE:   type = SMESH::EDGE; break;
453       case GEOM::FACE:   type = SMESH::FACE; break;
454       case GEOM::SOLID:
455       case GEOM::SHELL:  type = SMESH::VOLUME; break;
456       case GEOM::COMPOUND: {
457         GEOM::GEOM_Gen_var aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
458         if ( !aGeomGen->_is_nil() ) {
459           GEOM::GEOM_IGroupOperations_var aGrpOp =
460             aGeomGen->GetIGroupOperations( study->StudyId() );
461           if ( !aGrpOp->_is_nil() ) {
462             switch ( aGrpOp->GetType( aGeomObj )) {
463             case TopAbs_VERTEX: type = SMESH::NODE; break;
464             case TopAbs_EDGE:   type = SMESH::EDGE; break;
465             case TopAbs_FACE:   type = SMESH::FACE; break;
466             case TopAbs_SOLID:  type = SMESH::VOLUME; break;
467             default:;
468             }
469           }
470         }
471       }
472       default:;
473       }
474     }
475   }
476   if ( type < 0 ) {
477     MESSAGE("Type of the group " << grpID << " not found");
478     return false;
479   }
480   if ( theType.IsIntegerValue() )
481     return type == theType.IntegerValue();
482
483   switch ( type ) {
484   case SMESH::NODE:   return theType.Location( "NODE", 1, theType.Length() );
485   case SMESH::EDGE:   return theType.Location( "EDGE", 1, theType.Length() );
486   case SMESH::FACE:   return theType.Location( "FACE", 1, theType.Length() );
487   case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
488   default:;
489   }
490   return false;
491 }
492
493 //================================================================================
494 /*!
495  * \brief 
496   * \param theCreationCmd - 
497  */
498 //================================================================================
499
500 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd):
501   _pyObject(theCreationCmd), myHasEditor(false)
502 {
503   // convert my creation command
504   Handle(_pyCommand) creationCmd = GetCreationCmd();
505   creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
506   creationCmd->SetMethod( "Mesh" );
507
508   theGen->SetAccessorMethod( GetID(), "GetMesh()" );
509 }
510
511 //================================================================================
512 /*!
513  * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
514   * \param theCommand - Engine method called for this mesh
515  */
516 //================================================================================
517
518 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
519 {
520   // smesh.py wraps the following methods:
521   //
522   // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
523   //     --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
524   // 2. AddHypothesis(geom, hyp)
525   //     --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
526   // 3. CreateGroupFromGEOM(type, name, grp)
527   //     --> in Mesh.Group(grp, name="")
528   // 4. ExportToMED(f, opt, version)
529   //     --> in Mesh.ExportToMED( f, version, opt=0 )
530   // 5. ExportMED(f, opt)
531   //     --> in Mesh.ExportMED( f,opt=0 )
532   // 6. ExportDAT(f)
533   //     --> in Mesh.ExportDAT( f )
534   // 7. ExportUNV(f)
535   //     --> in Mesh.ExportUNV(f)
536   // 8. ExportSTL(f, ascii)
537   //     --> in Mesh.ExportSTL(f, ascii=1)
538
539   const TCollection_AsciiString method = theCommand->GetMethod();
540   if ( method == "GetSubMesh" ) {
541     mySubmeshes.push_back( theCommand );
542   }
543   else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
544     myAddHypCmds.push_back( theCommand );
545     // set mesh to hypo
546     const _pyID& hypID = theCommand->GetArg( 2 );
547     Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
548     if ( !hyp.IsNull() ) {
549       myHypos.push_back( hyp );
550       if ( hyp->GetMesh().IsEmpty() )
551         hyp->SetMesh( this->GetID() );
552     }
553   }
554   else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
555     _pyID grp = theCommand->GetArg( 3 );
556     if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
557       theCommand->SetMethod( "Group" );
558       theCommand->RemoveArgs();
559       theCommand->SetArg( 1, grp );
560     }
561     else {
562       AddMeshAccess( theCommand );
563     }
564   }
565   else if ( method == "ExportToMED" ) {//(f, opt, version)
566     // --> (f, version, opt)
567     _pyID opt = theCommand->GetArg( 2 );
568     _pyID ver = theCommand->GetArg( 3 );
569     theCommand->SetArg( 2, ver );
570     theCommand->SetArg( 3, opt );
571   }
572   else if ( method == "RemoveHypothesis" ) // (geom, hyp)
573   {
574     const _pyID & hypID = theCommand->GetArg( 2 );
575
576     // check if this mesh still has corresponding addition command
577     bool hasAddCmd = false;
578     list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
579     while ( cmd != myAddHypCmds.end() )
580     {
581       // AddHypothesis(geom, hyp)
582       if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
583         theCommand->Clear();
584         (*cmd)->Clear();
585         cmd = myAddHypCmds.erase( cmd );
586         hasAddCmd = true;
587       }
588       else {
589         ++cmd;
590       }
591     }
592     Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
593     if ( ! hasAddCmd ) { // hypo addition already wrapped
594       // access to wrapped mesh
595       AddMeshAccess( theCommand );
596       // access to wrapped algo
597       if ( !hyp.IsNull() && hyp->IsAlgo() && hyp->IsWrapped() )
598         theCommand->SetArg( 2, theCommand->GetArg( 2 ) + ".GetAlgorithm()" );
599     }
600     // remove hyp from myHypos
601     myHypos.remove( hyp );
602   }
603
604   // leave only one "  mesh_editor_<nb> = mesh.GetMeshEditor()"
605   else if ( theCommand->GetMethod() == "GetMeshEditor")
606   {
607     if ( myHasEditor )
608       theCommand->Clear();
609     else
610       AddMeshAccess( theCommand );
611     myHasEditor = true;
612   }
613
614   // apply theCommand to the mesh wrapped by smeshpy mesh
615   else
616   {
617     AddMeshAccess( theCommand );
618   }
619 }
620
621 namespace {
622
623   //================================================================================
624   /*!
625    * \brief add addition result treatement command
626     * \param addCmd - hypothesis addition command
627    */
628   //================================================================================
629
630   void addErrorTreatmentCmd( Handle(_pyCommand) & addCmd,
631                              const bool           isAlgo)
632   {
633     // addCmd: status = mesh.AddHypothesis( geom, hypo )
634     // treatement command:
635     //    def TreatHypoStatus(status, hypName, geomName, isAlgo):
636     TCollection_AsciiString status = addCmd->GetResultValue();
637     if ( !status.IsEmpty() ) {
638       const _pyID& geomID = addCmd->GetArg( 1 );
639       const _pyID& hypoID = addCmd->GetArg( 2 );
640       TCollection_AsciiString cmdStr = addCmd->GetIndentation() +
641         SMESH_2smeshpy::SmeshpyName() + ".TreatHypoStatus( " + status + ", " +
642         SMESH_2smeshpy::SmeshpyName() + ".GetName(" + hypoID + "), " +
643         SMESH_2smeshpy::SmeshpyName() + ".GetName(" + geomID + "), " +
644         (char*)( isAlgo ? "True" : "False" ) + " )";
645       Handle(_pyCommand) cmd = theGen->AddCommand( cmdStr );
646       addCmd->AddDependantCmd( cmd );
647     }
648   }
649 }
650
651 //================================================================================
652 /*!
653  * \brief Convert creation and addition of all algos and hypos
654  */
655 //================================================================================
656
657 void _pyMesh::Flush()
658 {
659   list < Handle(_pyCommand) >::iterator cmd, cmd2;
660
661   // try to convert algo addition like this:
662   // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
663   for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
664   {
665     Handle(_pyCommand) addCmd = *cmd;
666     const _pyID& algoID = addCmd->GetArg( 2 );
667     Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
668     if ( algo.IsNull() || !algo->IsAlgo() )
669       continue;
670     // try to convert
671     _pyID geom = addCmd->GetArg( 1 );
672     if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
673     {
674       // wrapped algo is created atfer mesh creation
675       GetCreationCmd()->AddDependantCmd( addCmd );
676
677       if ( geom != GetGeom() ) // local algo
678       {
679         // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
680         addCmd->SetArg( addCmd->GetNbArgs() + 1,
681                         TCollection_AsciiString( "geom=" ) + geom );
682         // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
683         for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
684           Handle(_pyCommand) subCmd = *cmd2;
685           if ( geom == subCmd->GetArg( 1 )) {
686             subCmd->SetObject( algo->GetID() );
687             subCmd->RemoveArgs();
688             addCmd->AddDependantCmd( subCmd );
689           }
690         }
691       }
692     }
693     else // ALGO was already created
694     {
695       // mesh.AddHypothesis(geom, ALGO ) --> mesh.GetMesh().AddHypothesis(geom, ALGO )
696       AddMeshAccess( addCmd );
697       // mesh.GetMesh().AddHypothesis(geom, ALGO ) ->
698       // mesh.GetMesh().AddHypothesis(geom, ALGO.GetAlgorithm() )
699       addCmd->SetArg( 2, addCmd->GetArg( 2 ) + ".GetAlgorithm()" );
700       // add addition result treatement cmd
701       addErrorTreatmentCmd( addCmd, true );
702     }
703   }
704
705   // try to convert hypo addition like this:
706   // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
707   for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
708   {
709     Handle(_pyCommand) addCmd = *cmd;
710     const _pyID& hypID = addCmd->GetArg( 2 );
711     Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
712     if ( hyp.IsNull() || hyp->IsAlgo() )
713       continue;
714     const _pyID& geom = addCmd->GetArg( 1 );
715     // find algo created on <geom> for this mesh
716     Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, this->GetID(), hyp->GetType() );
717     //_pyID algoID = algo.IsNull() ? "" : algo->GetID();
718     if ( !algo.IsNull() && hyp->Addition2Creation( addCmd, this->GetID() )) // OK
719     {
720       addCmd->SetObject( algo->GetID() );
721       algo->GetCreationCmd()->AddDependantCmd( addCmd );
722     }
723     else
724     {
725       AddMeshAccess( addCmd );
726       // add addition result treatement cmd
727       addErrorTreatmentCmd( addCmd, false );
728     }
729   }
730
731   // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
732   for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
733     Handle(_pyCommand) subCmd = *cmd;
734     if ( subCmd->GetNbArgs() > 0 )
735       AddMeshAccess( subCmd );
736   }
737   myAddHypCmds.clear();
738   mySubmeshes.clear();
739
740   // flush hypotheses
741   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
742   for ( ; hyp != myHypos.end(); ++hyp )
743     (*hyp)->Flush();
744 }
745
746 //================================================================================
747 /*!
748  * \brief _pyHypothesis constructor
749   * \param theCreationCmd - 
750  */
751 //================================================================================
752
753 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
754   _pyObject( theCreationCmd )
755 {
756   myDim = myIsAlgo = /*myIsLocal = */myIsWrapped = myIsConverted = false;
757 }
758
759 //================================================================================
760 /*!
761  * \brief Creates algorithm or hypothesis
762   * \param theCreationCmd - The engine command creating a hypothesis
763   * \retval Handle(_pyHypothesis) - Result _pyHypothesis
764  */
765 //================================================================================
766
767 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
768 {
769   // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
770   ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
771
772   Handle(_pyHypothesis) hyp, algo;
773
774   // "theHypType"
775   const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
776   if ( hypTypeQuoted.IsEmpty() )
777     return hyp;
778   // theHypType
779   TCollection_AsciiString  hypType =
780     hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
781
782   algo = new _pyAlgorithm( theCreationCmd );
783   hyp  = new _pyHypothesis( theCreationCmd );
784
785   // 1D Regular_1D ----------
786   if ( hypType == "Regular_1D" ) {
787     algo->SetDimMethodType( 1, "Segment");
788   }
789   else if ( hypType == "LocalLength" ) {
790     hyp->SetDimMethodType( 1, "LocalLength", "Regular_1D");
791     hyp->AddArgMethod( "SetLength" );
792   }
793   else if ( hypType == "NumberOfSegments" ) {
794     hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
795     hyp->SetDimMethodType( 1, "NumberOfSegments", "Regular_1D");
796     hyp->AddArgMethod( "SetNumberOfSegments" );
797     hyp->AddArgMethod( "SetScaleFactor" );
798   }
799   else if ( hypType == "Arithmetic1D" ) {
800     hyp = new _pyComplexParamHypo( theCreationCmd );
801     hyp->SetDimMethodType( 1, "Arithmetic1D", "Regular_1D");
802   }
803   else if ( hypType == "StartEndLength" ) {
804     hyp = new _pyComplexParamHypo( theCreationCmd );
805     hyp->SetDimMethodType( 1, "StartEndLength", "Regular_1D");
806   }
807   else if ( hypType == "Deflection1D" ) {
808     hyp->SetDimMethodType( 1, "Deflection1D", "Regular_1D");
809     hyp->AddArgMethod( "SetDeflection" );
810   }
811   else if ( hypType == "Propagation" ) {
812     hyp->SetDimMethodType( 1, "Propagation", "Regular_1D");
813   }
814   else if ( hypType == "QuadraticMesh" ) {
815     hyp->SetDimMethodType( 1, "QuadraticMesh", "Regular_1D");
816   }
817   else if ( hypType == "AutomaticLength" ) {
818     hyp->SetDimMethodType( 1, "AutomaticLength", "Regular_1D");
819     hyp->AddArgMethod( "SetFineness");
820   }
821   // 1D Python_1D ----------
822   else if ( hypType == "Python_1D" ) {
823     algo->SetDimMethodType( 1, "Segment");
824     algo->myArgs.Append( "algo=smesh.PYTHON");
825   }
826   else if ( hypType == "PythonSplit1D" ) {
827     hyp->SetDimMethodType( 1, "PythonSplit1D", "Python_1D");
828     hyp->AddArgMethod( "SetNumberOfSegments");
829     hyp->AddArgMethod( "SetPythonLog10RatioFunction");
830   }
831   // 2D ----------
832   else if ( hypType == "MEFISTO_2D" ) {
833     algo->SetDimMethodType( 2, "Triangle");
834   }
835   else if ( hypType == "MaxElementArea" ) {
836     hyp->SetDimMethodType( 2, "MaxElementArea", "MEFISTO_2D");
837     hyp->AddArgMethod( "SetMaxElementArea");
838   }
839   else if ( hypType == "LengthFromEdges" ) {
840     hyp->SetDimMethodType( 2, "LengthFromEdges", "MEFISTO_2D");
841   }
842   else if ( hypType == "Quadrangle_2D" ) {
843     algo->SetDimMethodType( 2, "Quadrangle" );
844   }
845   else if ( hypType == "QuadranglePreference" ) {
846     hyp->SetDimMethodType( 2, "QuadranglePreference", "Quadrangle_2D");
847   }
848   // 3D ----------
849   else if ( hypType == "NETGEN_3D") {
850     algo->SetDimMethodType( 3, "Tetrahedron" );
851     algo->myArgs.Append( "algo=smesh.NETGEN" );
852   }
853   else if ( hypType == "MaxElementVolume") {
854     hyp->SetDimMethodType( 3, "MaxElementVolume", "NETGEN_3D");
855     hyp->AddArgMethod( "SetMaxElementVolume" );
856   }
857   else if ( hypType == "GHS3D_3D" ) {
858     algo->SetDimMethodType( 3, "Tetrahedron");
859     algo->myArgs.Append( "algo=smesh.GHS3D" );
860   }
861   else if ( hypType == "Hexa_3D" ) {
862     algo->SetDimMethodType( 3, "Hexahedron");
863   }
864   // Repetitive ---------
865   else if ( hypType == "Projection_1D" ) {
866     algo->SetDimMethodType( 1, "Projection1D");
867   }
868   else if ( hypType == "ProjectionSource1D" ) {
869     hyp->SetDimMethodType( 1, "SourceEdge", "Projection_1D");
870     hyp->AddArgMethod( "SetSourceEdge");
871     hyp->AddArgMethod( "SetSourceMesh");
872     hyp->AddArgMethod( "SetVertexAssociation", 2 );
873   }
874   else if ( hypType == "Projection_2D" ) {
875     algo->SetDimMethodType( 2, "Projection2D");
876   }
877   else if ( hypType == "ProjectionSource2D" ) {
878     hyp->SetDimMethodType( 2, "SourceFace", "Projection_2D");
879     hyp->AddArgMethod( "SetSourceFace");
880     hyp->AddArgMethod( "SetSourceMesh");
881     hyp->AddArgMethod( "SetVertexAssociation", 4 );
882   }
883   else if ( hypType == "Projection_3D" ) {
884     algo->SetDimMethodType( 3, "Projection3D");
885   }
886   else if ( hypType == "ProjectionSource3D" ) {
887     hyp->SetDimMethodType( 3, "SourceShape3D", "Projection_3D");
888     hyp->AddArgMethod( "SetSource3DShape");
889     hyp->AddArgMethod( "SetSourceMesh");
890     hyp->AddArgMethod( "SetVertexAssociation", 4 );
891   }
892   else if ( hypType == "Prism_3D" ) {
893     algo->SetDimMethodType( 3, "Prism");
894   }
895   else if ( hypType == "RadialPrism_3D" ) {
896     algo->SetDimMethodType( 3, "Prism");
897   }
898   else if ( hypType == "NumberOfLayers" ) {
899     hyp->SetDimMethodType( 3, "NumberOfLayers", "RadialPrism_3D");
900     hyp->AddArgMethod( "SetNumberOfLayers" );
901   }
902   else if ( hypType == "LayerDistribution" ) {
903     hyp = new _pyLayerDistributionHypo( theCreationCmd );
904     hyp->SetDimMethodType( 3, "LayerDistribution", "RadialPrism_3D");
905 //     hyp->AddArgMethod( "SetSource3DShape");
906 //     hyp->AddArgMethod( "SetSourceMesh");
907 //     hyp->AddArgMethod( "SetVertexAssociation", 4 );
908   }
909
910   if ( algo->GetDim() ) {
911     algo->myType = hypType;
912     return algo;
913   }
914   return hyp;
915 }
916
917 //================================================================================
918 /*!
919  * \brief Convert the command adding a hypothesis to mesh into a smesh command
920   * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
921   * \param theAlgo - The algo that can create this hypo
922   * \retval bool - false if the command cant be converted
923  */
924 //================================================================================
925
926 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
927                                        const _pyID&              theMesh)
928 {
929   ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
930
931   if ( !IsWrappable( theMesh ))
932     return false;
933
934   myIsWrapped = true;
935
936   if ( myIsWrapped )
937   {
938     // mesh.AddHypothesis(geom,hyp) --> hyp = theMesh.myCreationMethod(args)
939     theCmd->SetResultValue( GetID() );
940     theCmd->SetObject( theMesh );
941     theCmd->SetMethod( myCreationMethod );
942     // set args
943     theCmd->RemoveArgs();
944     for ( int i = 1; i <= myArgs.Length(); ++i ) {
945       if ( !myArgs( i ).IsEmpty() )
946         theCmd->SetArg( i, myArgs( i ));
947       else
948         theCmd->SetArg( i, "[]");
949     }
950     // set a new creation command
951     GetCreationCmd()->Clear();
952     SetCreationCmd( theCmd );
953
954     // clear commands setting arg values
955     list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
956     for ( ; argCmd != myArgCommands.end(); ++argCmd )
957       (*argCmd)->Clear();
958   }
959   else
960   {
961 //     // set arg commands after hypo creation
962 //     list<Handle(_pyCommand)>::iterator argCmd = myArgCommands.begin();
963 //     for ( ; argCmd != myArgCommands.end(); ++argCmd )
964 //       if ( !(*argCmd)->IsEmpty() && GetCommandNb() > (*argCmd)->GetOrderNb() )
965 //         theGen->ExchangeCommands( GetCreationCmd(), *argCmd );
966   }
967
968   // set unknown arg commands after hypo creation
969   Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
970   list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
971   for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
972     afterCmd->AddDependantCmd( *cmd );
973   }
974
975   return myIsWrapped;
976 }
977
978 //================================================================================
979 /*!
980  * \brief Remember hypothesis parameter values
981  * \param theCommand - The called hypothesis method
982  */
983 //================================================================================
984
985 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
986 {
987   ASSERT( !myIsAlgo );
988   // set args
989   int nbArgs = 0;
990   for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
991     if ( myArgMethods( i ) == theCommand->GetMethod() ) {
992       while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
993         myArgs.Append( "[]" );
994       for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
995         myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
996       myArgCommands.push_back( theCommand );
997       return;
998     }
999     nbArgs += myNbArgsByMethod( i );
1000   }
1001   myUnknownCommands.push_back( theCommand );
1002 }
1003
1004 //================================================================================
1005 /*!
1006  * \brief Finish conversion
1007  */
1008 //================================================================================
1009
1010 void _pyHypothesis::Flush()
1011 {
1012   if ( IsWrapped() ) {
1013   }
1014   else {
1015     list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1016     for ( ; cmd != myArgCommands.end(); ++cmd ) {
1017       // Add access to a wrapped mesh
1018       theGen->AddMeshAccessorMethod( *cmd );
1019       // Add access to a wrapped algorithm
1020       theGen->AddAlgoAccessorMethod( *cmd );
1021     }
1022     cmd = myUnknownCommands.begin();
1023     for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1024       // Add access to a wrapped mesh
1025       theGen->AddMeshAccessorMethod( *cmd );
1026       // Add access to a wrapped algorithm
1027       theGen->AddAlgoAccessorMethod( *cmd );
1028     }
1029   }
1030   // forget previous hypothesis modifications
1031   myArgCommands.clear();
1032   myUnknownCommands.clear();
1033 }
1034
1035 //================================================================================
1036 /*!
1037  * \brief clear creation, arg and unkown commands
1038  */
1039 //================================================================================
1040
1041 void _pyHypothesis::ClearAllCommands()
1042 {
1043   GetCreationCmd()->Clear();
1044   list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1045   for ( ; cmd != myArgCommands.end(); ++cmd )
1046     ( *cmd )->Clear();
1047   cmd = myUnknownCommands.begin();
1048   for ( ; cmd != myUnknownCommands.end(); ++cmd )
1049     ( *cmd )->Clear();
1050 }
1051
1052 //================================================================================
1053 /*!
1054  * \brief Remember hypothesis parameter values
1055   * \param theCommand - The called hypothesis method
1056  */
1057 //================================================================================
1058
1059 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1060 {
1061   // ex: hyp.SetLength(start, 1)
1062   //     hyp.SetLength(end,   0)
1063   ASSERT(( theCommand->GetMethod() == "SetLength" ));
1064   ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1065   int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1066   while ( myArgs.Length() < i )
1067     myArgs.Append( "[]" );
1068   myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1069   myArgCommands.push_back( theCommand );
1070 }
1071
1072 //================================================================================
1073 /*!
1074  * \brief Convert methods of 1D hypotheses to my own methods
1075   * \param theCommand - The called hypothesis method
1076  */
1077 //================================================================================
1078
1079 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1080 {
1081   if ( theCommand->GetMethod() != "SetLayerDistribution" )
1082     return;
1083
1084   _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1085
1086   const _pyID& hyp1dID = theCommand->GetArg( 1 );
1087   Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1088   if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1089     hyp1d = my1dHyp;
1090   else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1091     // 1D hypo is already set, so distribution changes and the old
1092     // 1D hypo is thrown away
1093     my1dHyp->ClearAllCommands();
1094   }
1095   my1dHyp = hyp1d;
1096   if ( my1dHyp.IsNull() )
1097     return; // something wrong :(
1098
1099   // make a new name for 1D hyp = "HypType" + "_Distribution"
1100   if ( my1dHyp->GetCreationCmd()->GetMethod() == "CreateHypothesis" ) {
1101     // not yet converted creation cmd
1102     TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1103     TCollection_AsciiString hypType = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1104     newName = hypType + "_Distribution";
1105     my1dHyp->GetCreationCmd()->SetResultValue( newName );
1106   }
1107   else {
1108     // already converted creation cmd
1109     newName = my1dHyp->GetCreationCmd()->GetResultValue();
1110   }
1111
1112   // as creation of 1D hyp was written later then it's edition,
1113   // we need to find all it's edition calls and process them
1114   list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1115   list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1116   for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1117     const _pyID& objID = (*cmdIt)->GetObject();
1118     if ( objID == hyp1dID ) {
1119       my1dHyp->Process( *cmdIt );
1120       my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1121       ( *cmdIt )->SetObject( newName );
1122     }
1123   }
1124   if ( !myArgCommands.empty() )
1125     myArgCommands.front()->Clear();
1126   theCommand->SetArg( 1, newName );
1127   myArgCommands.push_back( theCommand );
1128   // copy hyp1d's creation method and args
1129 //   myCreationMethod = hyp1d->GetCreationMethod();
1130 //   myArgs           = hyp1d->GetArgs();
1131 //   // make them cleared at conversion
1132 //   myArgCommands = hyp1d->GetArgCommands();
1133
1134 //   // to be cleared at convertion only
1135 //   myArgCommands.push_back( theCommand );
1136 }
1137
1138 //================================================================================
1139 /*!
1140  * \brief 
1141   * \param theAdditionCmd - 
1142   * \param theMesh - 
1143   * \retval bool - 
1144  */
1145 //================================================================================
1146
1147 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1148                                                   const _pyID&              theMesh)
1149 {
1150   myIsWrapped = false;
1151
1152   if ( my1dHyp.IsNull() )
1153     return false;
1154
1155   // set "SetLayerDistribution()" after addition cmd
1156   theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1157
1158   _pyID geom = theAdditionCmd->GetArg( 1 );
1159
1160   my1dHyp->SetMesh( theMesh );
1161   if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1162     return false;
1163
1164   // clear "SetLayerDistribution()" cmd
1165   myArgCommands.front()->Clear();
1166
1167   // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1168
1169   // find RadialPrism algo created on <geom> for theMesh
1170   Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this->GetType() );
1171   if ( !algo.IsNull() ) {
1172     GetCreationCmd()->SetObject( algo->GetID() );
1173     GetCreationCmd()->SetMethod( "Get3DHypothesis" );
1174     GetCreationCmd()->RemoveArgs();
1175     theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1176     myIsWrapped = true;
1177   }
1178   return myIsWrapped;
1179 }
1180
1181 //================================================================================
1182 /*!
1183  * \brief 
1184  */
1185 //================================================================================
1186
1187 void _pyLayerDistributionHypo::Flush()
1188 {
1189   //my1dHyp.Nullify();
1190   //_pyHypothesis::Flush();
1191 }
1192
1193 //================================================================================
1194 /*!
1195  * \brief additionally to Addition2Creation, clears SetDistrType() command
1196   * \param theCmd - AddHypothesis() command
1197   * \param theMesh - mesh to which a hypothesis is added
1198   * \retval bool - convertion result
1199  */
1200 //================================================================================
1201
1202 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1203                                                 const _pyID&              theMesh)
1204 {
1205   if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1206     // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1207     bool scaleDistrType = false;
1208     list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1209     for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1210       if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1211         if ( (*cmd)->GetArg( 1 ) == "1" ) {
1212           scaleDistrType = true;
1213           (*cmd)->Clear();
1214         }
1215         else if ( !scaleDistrType ) {
1216           // distribution type changed: remove scale factor from args
1217           myArgs.Remove( 2, myArgs.Length() );
1218           break;
1219         }
1220       }
1221     }
1222   }
1223   return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1224 }
1225
1226 //================================================================================
1227 /*!
1228  * \brief remove repeated commands defining distribution
1229  */
1230 //================================================================================
1231
1232 void _pyNumberOfSegmentsHyp::Flush()
1233 {
1234   // find number of the last SetDistrType() command
1235   list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1236   int distrTypeNb = 0;
1237   for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1238     if ( (*cmd)->GetMethod() == "SetDistrType" )
1239       distrTypeNb = (*cmd)->GetOrderNb();
1240
1241   // clear commands before the last SetDistrType()
1242   list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1243   for ( int i = 0; i < 2; ++i ) {
1244     set<TCollection_AsciiString> uniqueMethods;
1245     list<Handle(_pyCommand)> & cmdList = *cmds[i];
1246     for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1247     {
1248       bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1249       const TCollection_AsciiString& method = (*cmd)->GetMethod();
1250       if ( !clear || method == "SetNumberOfSegments" ) {
1251         bool isNewInSet = uniqueMethods.insert( method ).second;
1252         clear = !isNewInSet;
1253       }
1254       if ( clear )
1255         (*cmd)->Clear();
1256     }
1257     cmdList.clear();
1258   }
1259 }
1260
1261 //================================================================================
1262 /*!
1263  * \brief _pyAlgorithm constructor
1264  * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1265  */
1266 //================================================================================
1267
1268 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1269   : _pyHypothesis( theCreationCmd )
1270 {
1271   myIsAlgo = true;
1272 }
1273
1274 //================================================================================
1275 /*!
1276  * \brief Convert the command adding an algorithm to mesh
1277   * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1278   * \param theMesh - The mesh needing this algo 
1279   * \retval bool - false if the command cant be converted
1280  */
1281 //================================================================================
1282   
1283 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1284                                       const _pyID&              theMeshID)
1285 {
1286   if ( IsWrappable( theMeshID )) {
1287
1288     myGeom = theCmd->GetArg( 1 );
1289
1290     // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1291     if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1292       theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1293       return true;
1294     }
1295   }
1296   return false;
1297 }
1298
1299 //================================================================================
1300 /*!
1301  * \brief Return starting position of a part of python command
1302   * \param thePartIndex - The index of command part
1303   * \retval int - Part position
1304  */
1305 //================================================================================
1306
1307 int _pyCommand::GetBegPos( int thePartIndex )
1308 {
1309   if ( IsEmpty() )
1310     return EMPTY;
1311   if ( myBegPos.Length() < thePartIndex )
1312     return UNKNOWN;
1313   return myBegPos( thePartIndex );
1314 }
1315
1316 //================================================================================
1317 /*!
1318  * \brief Store starting position of a part of python command
1319   * \param thePartIndex - The index of command part
1320   * \param thePosition - Part position
1321  */
1322 //================================================================================
1323
1324 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1325 {
1326   while ( myBegPos.Length() < thePartIndex )
1327     myBegPos.Append( UNKNOWN );
1328   myBegPos( thePartIndex ) = thePosition;
1329 }
1330
1331 //================================================================================
1332 /*!
1333  * \brief Returns whitespace symbols at the line beginning
1334   * \retval TCollection_AsciiString - result
1335  */
1336 //================================================================================
1337
1338 TCollection_AsciiString _pyCommand::GetIndentation()
1339 {
1340   int end = 1;
1341   if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1342     GetWord( myString, end, true );
1343   else
1344     end = GetBegPos( RESULT_IND );
1345   return myString.SubString( 1, end - 1 );
1346 }
1347
1348 //================================================================================
1349 /*!
1350  * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1351   * \retval const TCollection_AsciiString & - ResultValue substring
1352  */
1353 //================================================================================
1354
1355 const TCollection_AsciiString & _pyCommand::GetResultValue()
1356 {
1357   if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1358   {
1359     int begPos = myString.Location( "=", 1, Length() );
1360     if ( begPos )
1361       myRes = GetWord( myString, begPos, false );
1362     else
1363       begPos = EMPTY;
1364     SetBegPos( RESULT_IND, begPos );
1365   }
1366   return myRes;
1367 }
1368
1369 //================================================================================
1370 /*!
1371  * \brief Return substring of python command looking like ResVal = Object.Meth()
1372   * \retval const TCollection_AsciiString & - Object substring
1373  */
1374 //================================================================================
1375
1376 const TCollection_AsciiString & _pyCommand::GetObject()
1377 {
1378   if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1379   {
1380     // beginning
1381     int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1382     if ( begPos < 1 )
1383       begPos = myString.Location( "=", 1, Length() ) + 1;
1384     // store
1385     myObj = GetWord( myString, begPos, true );
1386     SetBegPos( OBJECT_IND, begPos );
1387   }
1388   //SCRUTE(myObj);
1389   return myObj;
1390 }
1391
1392 //================================================================================
1393 /*!
1394  * \brief Return substring of python command looking like ResVal = Obj.Method()
1395   * \retval const TCollection_AsciiString & - Method substring
1396  */
1397 //================================================================================
1398
1399 const TCollection_AsciiString & _pyCommand::GetMethod()
1400 {
1401   if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1402   {
1403     // beginning
1404     int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1405     bool forward = true;
1406     if ( begPos < 1 ) {
1407       begPos = myString.Location( "(", 1, Length() ) - 1;
1408       forward = false;
1409     }
1410     // store
1411     myMeth = GetWord( myString, begPos, forward );
1412     SetBegPos( METHOD_IND, begPos );
1413   }
1414   //SCRUTE(myMeth);
1415   return myMeth;
1416 }
1417
1418 //================================================================================
1419 /*!
1420  * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1421   * \retval const TCollection_AsciiString & - Arg<index> substring
1422  */
1423 //================================================================================
1424
1425 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1426 {
1427   if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1428   {
1429     // find all args
1430     int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1431     if ( begPos < 1 )
1432       begPos = myString.Location( "(", 1, Length() ) + 1;
1433
1434     int i = 0, prevLen = 0;
1435     while ( begPos != EMPTY ) {
1436       begPos += prevLen;
1437       // check if we are looking at the closing parenthesis
1438       while ( begPos <= Length() && isspace( myString.Value( begPos )))
1439         ++begPos;
1440       if ( begPos > Length() || myString.Value( begPos ) == ')' )
1441         break;
1442       myArgs.Append( GetWord( myString, begPos, true, true ));
1443       SetBegPos( ARG1_IND + i, begPos );
1444       prevLen = myArgs.Last().Length();
1445       if ( prevLen == 0 )
1446         myArgs.Remove( myArgs.Length() ); // no more args
1447       i++;
1448     }
1449   }
1450   if ( myArgs.Length() < index )
1451     return theEmptyString;
1452   return myArgs( index );
1453 }
1454
1455 //================================================================================
1456 /*!
1457  * \brief Check if char is a word part
1458   * \param c - The character to check
1459   * \retval bool - The check result
1460  */
1461 //================================================================================
1462
1463 static inline bool isWord(const char c, const bool dotIsWord)
1464 {
1465   return
1466     !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1467 }
1468
1469 //================================================================================
1470 /*!
1471  * \brief Looks for a word in the string and returns word's beginning
1472   * \param theString - The input string
1473   * \param theStartPos - The position to start the search, returning word's beginning
1474   * \param theForward - The search direction
1475   * \retval TCollection_AsciiString - The found word
1476  */
1477 //================================================================================
1478
1479 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1480                                             int &      theStartPos,
1481                                             const bool theForward,
1482                                             const bool dotIsWord )
1483 {
1484   int beg = theStartPos, end = theStartPos;
1485   theStartPos = EMPTY;
1486   if ( beg < 1 || beg > theString.Length() )
1487     return theEmptyString;
1488
1489   if ( theForward ) { // search forward
1490     // beg
1491     while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1492       ++beg;
1493     if ( beg > theString.Length() )
1494       return theEmptyString; // no word found
1495     // end
1496     end = beg + 1;
1497     while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1498       ++end;
1499     --end;
1500   }
1501   else {  // search backward
1502     // end
1503     while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1504       --end;
1505     if ( end == 0 )
1506       return theEmptyString; // no word found
1507     beg = end - 1;
1508     while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1509       --beg;
1510     ++beg;
1511   }
1512   theStartPos = beg;
1513   //cout << theString << " ---- " << beg << " - " << end << endl;
1514   return theString.SubString( beg, end );
1515 }
1516
1517 //================================================================================
1518 /*!
1519  * \brief Look for position where not space char is
1520   * \param theString - The string 
1521   * \param thePos - The position to search from and which returns result
1522   * \retval bool - false if there are only space after thePos in theString
1523  * 
1524  * 
1525  */
1526 //================================================================================
1527
1528 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1529 {
1530   if ( thePos < 1 || thePos > theString.Length() )
1531     return false;
1532
1533   while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1534     ++thePos;
1535
1536   return thePos <= theString.Length();
1537 }
1538
1539 //================================================================================
1540 /*!
1541  * \brief Modify a part of the command
1542   * \param thePartIndex - The index of the part
1543   * \param thePart - The new part string
1544   * \param theOldPart - The old part
1545  */
1546 //================================================================================
1547
1548 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1549                         TCollection_AsciiString& theOldPart)
1550 {
1551   int pos = GetBegPos( thePartIndex );
1552   if ( pos <= Length() && theOldPart != thePart)
1553   {
1554     TCollection_AsciiString seperator;
1555     if ( pos < 1 ) {
1556       pos = GetBegPos( thePartIndex + 1 );
1557       if ( pos < 1 ) return;
1558       switch ( thePartIndex ) {
1559       case RESULT_IND: seperator = " = "; break;
1560       case OBJECT_IND: seperator = "."; break;
1561       case METHOD_IND: seperator = "()"; break;
1562       default:;
1563       }
1564     }      
1565     myString.Remove( pos, theOldPart.Length() );
1566     if ( !seperator.IsEmpty() )
1567       myString.Insert( pos , seperator );
1568     myString.Insert( pos, thePart );
1569     // update starting positions of the following parts
1570     int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1571     for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1572       if ( myBegPos( i ) > 0 )
1573         myBegPos( i ) += posDelta;
1574     }
1575     theOldPart = thePart;
1576   }
1577 }
1578
1579 //================================================================================
1580 /*!
1581  * \brief Set agrument
1582   * \param index - The argument index, it counts from 1
1583   * \param theArg - The argument string
1584  */
1585 //================================================================================
1586
1587 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1588 {
1589   FindAllArgs();
1590   int argInd = ARG1_IND + index - 1;
1591   int pos = GetBegPos( argInd );
1592   if ( pos < 1 ) // no index-th arg exist, append inexistent args
1593   {
1594     // find a closing parenthesis
1595     if ( int lastArgInd = GetNbArgs() ) {
1596       pos = GetBegPos( ARG1_IND + lastArgInd  - 1 ) + GetArg( lastArgInd ).Length();
1597       while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
1598         ++pos;
1599     }
1600     else {
1601       pos = Length();
1602       while ( pos > 0 && myString.Value( pos ) != ')' )
1603         --pos;
1604     }
1605     if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
1606       myString += "()";
1607       pos = Length();
1608     }
1609     while ( myArgs.Length() < index ) {
1610       if ( myArgs.Length() )
1611         myString.Insert( pos++, "," );
1612       myArgs.Append("None");
1613       myString.Insert( pos, myArgs.Last() );
1614       SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1615       pos += myArgs.Last().Length();
1616     }
1617   }
1618   SetPart( argInd, theArg, myArgs( index ));
1619 }
1620
1621 //================================================================================
1622 /*!
1623  * \brief Empty arg list
1624  */
1625 //================================================================================
1626
1627 void _pyCommand::RemoveArgs()
1628 {
1629   if ( int pos = myString.Location( '(', 1, Length() ))
1630     myString.Trunc( pos );
1631   myString += ")";
1632   myArgs.Clear();
1633   if ( myBegPos.Length() >= ARG1_IND )
1634     myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1635 }
1636
1637 //================================================================================
1638 /*!
1639  * \brief Set dependent commands after this one
1640  */
1641 //================================================================================
1642
1643 bool _pyCommand::SetDependentCmdsAfter() const
1644 {
1645   bool orderChanged = false;
1646   list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1647   for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1648     if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1649       orderChanged = true;
1650       theGen->SetCommandAfter( *cmd, this );
1651       (*cmd)->SetDependentCmdsAfter();
1652     }
1653   }
1654   return orderChanged;
1655 }
1656 //================================================================================
1657 /*!
1658  * \brief Insert accessor method after theObjectID
1659   * \param theObjectID - id of the accessed object
1660   * \param theAcsMethod - name of the method giving access to the object
1661   * \retval bool - false if theObjectID is not found in the command string
1662  */
1663 //================================================================================
1664
1665 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
1666 {
1667   if ( !theAcsMethod )
1668     return false;
1669   // start object search from the object, i.e. ignore result
1670   GetObject();
1671   int beg = GetBegPos( OBJECT_IND );
1672   if ( beg < 1 || beg > Length() )
1673     return false;
1674   while (( beg = myString.Location( theObjectID, beg, Length() )))
1675   {
1676     // check that theObjectID is not just a part of a longer ID
1677     int afterEnd = beg + theObjectID.Length();
1678     Standard_Character c = myString.Value( afterEnd );
1679     if ( !isalnum( c ) && c != ':' ) {
1680       // insertion
1681       int oldLen = Length();
1682       myString.Insert( afterEnd, (char*) theAcsMethod );
1683       myString.Insert( afterEnd, "." );
1684       // update starting positions of the parts following the modified one
1685       int posDelta = Length() - oldLen;
1686       for ( int i = 1; i <= myBegPos.Length(); ++i ) {
1687         if ( myBegPos( i ) > afterEnd )
1688           myBegPos( i ) += posDelta;
1689       }
1690       return true;
1691     }
1692     beg = afterEnd; // is a part - next search
1693   }
1694   return false;
1695 }
1696
1697 //================================================================================
1698 /*!
1699  * \brief Return method name giving access to an interaface object wrapped by python class
1700   * \retval const char* - method name
1701  */
1702 //================================================================================
1703
1704 const char* _pyObject::AccessorMethod() const
1705 {
1706   return 0;
1707 }