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