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