1 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : SMESH_2D_Algo_i.hxx
25 // Author : Paul RASCLE, EDF
29 // File : SMESH_2smeshpy.cxx
30 // Created : Fri Nov 18 13:20:10 2005
31 // Author : Edward AGAPOV (eap)
33 #include "SMESH_2smeshpy.hxx"
35 #include "SMESH_Gen_i.hxx"
36 #include "utilities.h"
37 #include "SMESH_PythonDump.hxx"
38 #include "Resource_DataMapOfAsciiStringAsciiString.hxx"
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);
49 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
50 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
51 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
52 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
53 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
54 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
55 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
56 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
57 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
60 using SMESH::TPythonDump;
63 * \brief Container of commands into which the initial script is split.
64 * It also contains data coresponding to SMESH_Gen contents
66 static Handle(_pyGen) theGen;
68 static TCollection_AsciiString theEmptyString;
70 //#define DUMP_CONVERSION
72 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
73 #undef DUMP_CONVERSION
76 //================================================================================
78 * \brief Convert python script using commands of smesh.py
79 * \param theScript - Input script
80 * \retval TCollection_AsciiString - Convertion result
82 //================================================================================
84 TCollection_AsciiString
85 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
86 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
88 theGen = new _pyGen( theEntry2AccessorMethod );
90 // split theScript into separate commands
91 int from = 1, end = theScript.Length(), to;
92 while ( from < end && ( to = theScript.Location( "\n", from, end )))
95 // cut out and store a command
96 theGen->AddCommand( theScript.SubString( from, to - 1 ));
101 #ifdef DUMP_CONVERSION
102 cout << endl << " ######## RESULT ######## " << endl<< endl;
104 // reorder commands after conversion
105 list< Handle(_pyCommand) >::iterator cmd;
108 orderChanges = false;
109 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
110 if ( (*cmd)->SetDependentCmdsAfter() )
112 } while ( orderChanges );
114 // concat commands back into a script
115 TCollection_AsciiString aScript;
116 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
118 #ifdef DUMP_CONVERSION
119 cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
121 if ( !(*cmd)->IsEmpty() ) {
123 aScript += (*cmd)->GetString();
133 //================================================================================
135 * \brief _pyGen constructor
137 //================================================================================
139 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
140 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
141 myID2AccessorMethod( theEntry2AccessorMethod )
144 myHasPattern = false;
145 // make that GetID() to return TPythonDump::SMESHGenName()
146 GetCreationCmd()->GetString() += "=";
149 //================================================================================
151 * \brief Convert a command using a specific converter
152 * \param theCommand - the command to convert
154 //================================================================================
156 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
158 // store theCommand in the sequence
159 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
161 Handle(_pyCommand) aCommand = myCommands.back();
162 #ifdef DUMP_CONVERSION
163 cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
166 _pyID objID = aCommand->GetObject();
168 if ( objID.IsEmpty() )
172 if ( objID == this->GetID() ) {
173 this->Process( aCommand );
176 // SMESH_Mesh method?
177 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
178 if ( id_mesh != myMeshes.end() ) {
179 id_mesh->second->Process( aCommand );
182 // SMESH_Hypothesis method?
183 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
184 for ( ; hyp != myHypos.end(); ++hyp )
185 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
186 (*hyp)->Process( aCommand );
190 // Add access to a wrapped mesh
191 AddMeshAccessorMethod( aCommand );
193 // Add access to a wrapped algorithm
194 AddAlgoAccessorMethod( aCommand );
196 // PAL12227. PythonDump was not updated at proper time; result is
197 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
198 // TypeError: __init__() takes exactly 11 arguments (10 given)
199 char wrongCommand[] = "SMESH.Filter.Criterion(";
200 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
202 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
203 // there must be 10 arguments, 5-th arg ThresholdID is missing,
204 const int wrongNbArgs = 9, missingArg = 5;
205 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
207 for ( int i = wrongNbArgs; i > missingArg; --i )
208 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
209 tmpCmd.SetArg( missingArg, "''");
210 aCommand->GetString().Trunc( beg - 1 );
211 aCommand->GetString() += tmpCmd.GetString();
217 //================================================================================
219 * \brief Convert the command or remember it for later conversion
220 * \param theCommand - The python command calling a method of SMESH_Gen
222 //================================================================================
224 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
226 // there are methods to convert:
227 // CreateMesh( shape )
228 // CreateHypothesis( theHypType, theLibName )
229 // Compute( mesh, geom )
231 if ( theCommand->GetMethod() == "CreateMesh" )
233 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
234 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
238 // CreateHypothesis()
239 if ( theCommand->GetMethod() == "CreateHypothesis" )
241 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
245 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
246 if ( theCommand->GetMethod() == "Compute" )
248 const _pyID& meshID = theCommand->GetArg( 1 );
249 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
250 if ( id_mesh != myMeshes.end() ) {
251 theCommand->SetObject( meshID );
252 theCommand->RemoveArgs();
253 id_mesh->second->Flush();
258 // leave only one smeshgen.GetPattern() in the script
259 if ( theCommand->GetMethod() == "GetPattern" ) {
260 if ( myHasPattern ) {
267 // smeshgen.Method() --> smesh.smesh.Method()
268 theCommand->SetObject( SMESH_2smeshpy::GenName() );
271 //================================================================================
273 * \brief Convert the remembered commands
275 //================================================================================
279 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
280 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
281 if ( ! id_mesh->second.IsNull() )
282 id_mesh->second->Flush();
284 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
285 for ( ; hyp != myHypos.end(); ++hyp )
286 if ( !hyp->IsNull() ) {
288 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
289 if ( !(*hyp)->IsWrapped() )
290 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
294 //================================================================================
296 * \brief Add access method to mesh that is object or arg
297 * \param theCmd - command to add access method
298 * \retval bool - true if added
300 //================================================================================
302 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
304 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
305 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
306 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
312 //================================================================================
314 * \brief Add access method to algo that is object or arg
315 * \param theCmd - command to add access method
316 * \retval bool - true if added
318 //================================================================================
320 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
322 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
323 for ( ; hyp != myHypos.end(); ++hyp ) {
324 if ( (*hyp)->IsAlgo() &&
325 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
331 //================================================================================
333 * \brief Find hypothesis by ID (entry)
334 * \param theHypID - The hypothesis ID
335 * \retval Handle(_pyHypothesis) - The found hypothesis
337 //================================================================================
339 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
341 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
342 for ( ; hyp != myHypos.end(); ++hyp )
343 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
345 return Handle(_pyHypothesis)();
348 //================================================================================
350 * \brief Find algorithm the created algorithm
351 * \param theGeom - The shape ID the algorithm was created on
352 * \param theMesh - The mesh ID that created the algorithm
353 * \param dim - The algo dimension
354 * \retval Handle(_pyHypothesis) - The found algo
356 //================================================================================
358 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
359 const TCollection_AsciiString& theAlgoType )
361 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
362 for ( ; hyp != myHypos.end(); ++hyp )
363 if ( !hyp->IsNull() &&
365 (*hyp)->GetType() == theAlgoType &&
366 (*hyp)->GetGeom() == theGeom &&
367 (*hyp)->GetMesh() == theMesh )
372 //================================================================================
374 * \brief Change order of commands in the script
375 * \param theCmd1 - One command
376 * \param theCmd2 - Another command
378 //================================================================================
380 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
382 list< Handle(_pyCommand) >::iterator pos1, pos2;
383 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
384 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
385 myCommands.insert( pos1, theCmd2 );
386 myCommands.insert( pos2, theCmd1 );
387 myCommands.erase( pos1 );
388 myCommands.erase( pos2 );
390 int nb1 = theCmd1->GetOrderNb();
391 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
392 theCmd2->SetOrderNb( nb1 );
393 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
394 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
397 //================================================================================
399 * \brief Set one command after the other
400 * \param theCmd - Command to move
401 * \param theAfterCmd - Command ater which to insert the first one
403 //================================================================================
405 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
407 // cout << "SET\t" << theCmd->GetString() << endl << "AFTER\t" << theAfterCmd->GetString() << endl << endl;
408 list< Handle(_pyCommand) >::iterator pos;
409 pos = find( myCommands.begin(), myCommands.end(), theCmd );
410 myCommands.erase( pos );
411 pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
412 myCommands.insert( ++pos, theCmd );
415 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
416 (*pos)->SetOrderNb( i++ );
419 //================================================================================
421 * \brief Set method to access to object wrapped with python class
422 * \param theID - The wrapped object entry
423 * \param theMethod - The accessor method
425 //================================================================================
427 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
429 myID2AccessorMethod.Bind( theID, (char*) theMethod );
432 //================================================================================
434 * \brief Find out type of geom group
435 * \param grpID - The geom group entry
436 * \retval int - The type
438 //================================================================================
440 static bool sameGroupType( const _pyID& grpID,
441 const TCollection_AsciiString& theType)
443 // define group type as smesh.Mesh.Group() does
445 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
446 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
447 if ( !aSObj->_is_nil() ) {
448 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
449 if ( !aGeomObj->_is_nil() ) {
450 switch ( aGeomObj->GetShapeType() ) {
451 case GEOM::VERTEX: type = SMESH::NODE; break;
452 case GEOM::EDGE: type = SMESH::EDGE; break;
453 case GEOM::FACE: type = SMESH::FACE; break;
455 case GEOM::SHELL: type = SMESH::VOLUME; break;
456 case GEOM::COMPOUND: {
457 GEOM::GEOM_Gen_var aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
458 if ( !aGeomGen->_is_nil() ) {
459 GEOM::GEOM_IGroupOperations_var aGrpOp =
460 aGeomGen->GetIGroupOperations( study->StudyId() );
461 if ( !aGrpOp->_is_nil() ) {
462 switch ( aGrpOp->GetType( aGeomObj )) {
463 case TopAbs_VERTEX: type = SMESH::NODE; break;
464 case TopAbs_EDGE: type = SMESH::EDGE; break;
465 case TopAbs_FACE: type = SMESH::FACE; break;
466 case TopAbs_SOLID: type = SMESH::VOLUME; break;
477 MESSAGE("Type of the group " << grpID << " not found");
480 if ( theType.IsIntegerValue() )
481 return type == theType.IntegerValue();
484 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
485 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
486 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
487 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
493 //================================================================================
496 * \param theCreationCmd -
498 //================================================================================
500 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd):
501 _pyObject(theCreationCmd), myHasEditor(false)
503 // convert my creation command
504 Handle(_pyCommand) creationCmd = GetCreationCmd();
505 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
506 creationCmd->SetMethod( "Mesh" );
508 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
511 //================================================================================
513 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
514 * \param theCommand - Engine method called for this mesh
516 //================================================================================
518 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
520 // smesh.py wraps the following methods:
522 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
523 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
524 // 2. AddHypothesis(geom, hyp)
525 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
526 // 3. CreateGroupFromGEOM(type, name, grp)
527 // --> in Mesh.Group(grp, name="")
528 // 4. ExportToMED(f, opt, version)
529 // --> in Mesh.ExportToMED( f, version, opt=0 )
530 // 5. ExportMED(f, opt)
531 // --> in Mesh.ExportMED( f,opt=0 )
533 // --> in Mesh.ExportDAT( f )
535 // --> in Mesh.ExportUNV(f)
536 // 8. ExportSTL(f, ascii)
537 // --> in Mesh.ExportSTL(f, ascii=1)
539 const TCollection_AsciiString method = theCommand->GetMethod();
540 if ( method == "GetSubMesh" ) {
541 mySubmeshes.push_back( theCommand );
543 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
544 myAddHypCmds.push_back( theCommand );
546 const _pyID& hypID = theCommand->GetArg( 2 );
547 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
548 if ( !hyp.IsNull() ) {
549 myHypos.push_back( hyp );
550 if ( hyp->GetMesh().IsEmpty() )
551 hyp->SetMesh( this->GetID() );
554 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
555 _pyID grp = theCommand->GetArg( 3 );
556 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
557 theCommand->SetMethod( "Group" );
558 theCommand->RemoveArgs();
559 theCommand->SetArg( 1, grp );
562 AddMeshAccess( theCommand );
565 else if ( method == "ExportToMED" ) {//(f, opt, version)
566 // --> (f, version, opt)
567 _pyID opt = theCommand->GetArg( 2 );
568 _pyID ver = theCommand->GetArg( 3 );
569 theCommand->SetArg( 2, ver );
570 theCommand->SetArg( 3, opt );
572 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
574 const _pyID & hypID = theCommand->GetArg( 2 );
576 // check if this mesh still has corresponding addition command
577 bool hasAddCmd = false;
578 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
579 while ( cmd != myAddHypCmds.end() )
581 // AddHypothesis(geom, hyp)
582 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
585 cmd = myAddHypCmds.erase( cmd );
592 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
593 if ( ! hasAddCmd ) { // hypo addition already wrapped
594 // access to wrapped mesh
595 AddMeshAccess( theCommand );
596 // access to wrapped algo
597 if ( !hyp.IsNull() && hyp->IsAlgo() && hyp->IsWrapped() )
598 theCommand->SetArg( 2, theCommand->GetArg( 2 ) + ".GetAlgorithm()" );
600 // remove hyp from myHypos
601 myHypos.remove( hyp );
604 // leave only one " mesh_editor_<nb> = mesh.GetMeshEditor()"
605 else if ( theCommand->GetMethod() == "GetMeshEditor")
610 AddMeshAccess( theCommand );
614 // apply theCommand to the mesh wrapped by smeshpy mesh
617 AddMeshAccess( theCommand );
623 //================================================================================
625 * \brief add addition result treatement command
626 * \param addCmd - hypothesis addition command
628 //================================================================================
630 void addErrorTreatmentCmd( Handle(_pyCommand) & addCmd,
633 // addCmd: status = mesh.AddHypothesis( geom, hypo )
634 // treatement command:
635 // def TreatHypoStatus(status, hypName, geomName, isAlgo):
636 TCollection_AsciiString status = addCmd->GetResultValue();
637 if ( !status.IsEmpty() ) {
638 const _pyID& geomID = addCmd->GetArg( 1 );
639 const _pyID& hypoID = addCmd->GetArg( 2 );
640 TCollection_AsciiString cmdStr = addCmd->GetIndentation() +
641 SMESH_2smeshpy::SmeshpyName() + ".TreatHypoStatus( " + status + ", " +
642 SMESH_2smeshpy::SmeshpyName() + ".GetName(" + hypoID + "), " +
643 SMESH_2smeshpy::SmeshpyName() + ".GetName(" + geomID + "), " +
644 (char*)( isAlgo ? "True" : "False" ) + " )";
645 Handle(_pyCommand) cmd = theGen->AddCommand( cmdStr );
646 addCmd->AddDependantCmd( cmd );
651 //================================================================================
653 * \brief Convert creation and addition of all algos and hypos
655 //================================================================================
657 void _pyMesh::Flush()
659 list < Handle(_pyCommand) >::iterator cmd, cmd2;
661 // try to convert algo addition like this:
662 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
663 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
665 Handle(_pyCommand) addCmd = *cmd;
666 const _pyID& algoID = addCmd->GetArg( 2 );
667 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
668 if ( algo.IsNull() || !algo->IsAlgo() )
671 _pyID geom = addCmd->GetArg( 1 );
672 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
674 // wrapped algo is created atfer mesh creation
675 GetCreationCmd()->AddDependantCmd( addCmd );
677 if ( geom != GetGeom() ) // local algo
679 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
680 addCmd->SetArg( addCmd->GetNbArgs() + 1,
681 TCollection_AsciiString( "geom=" ) + geom );
682 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
683 for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
684 Handle(_pyCommand) subCmd = *cmd2;
685 if ( geom == subCmd->GetArg( 1 )) {
686 subCmd->SetObject( algo->GetID() );
687 subCmd->RemoveArgs();
688 addCmd->AddDependantCmd( subCmd );
693 else // ALGO was already created
695 // mesh.AddHypothesis(geom, ALGO ) --> mesh.GetMesh().AddHypothesis(geom, ALGO )
696 AddMeshAccess( addCmd );
697 // mesh.GetMesh().AddHypothesis(geom, ALGO ) ->
698 // mesh.GetMesh().AddHypothesis(geom, ALGO.GetAlgorithm() )
699 addCmd->SetArg( 2, addCmd->GetArg( 2 ) + ".GetAlgorithm()" );
700 // add addition result treatement cmd
701 addErrorTreatmentCmd( addCmd, true );
705 // try to convert hypo addition like this:
706 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
707 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
709 Handle(_pyCommand) addCmd = *cmd;
710 const _pyID& hypID = addCmd->GetArg( 2 );
711 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
712 if ( hyp.IsNull() || hyp->IsAlgo() )
714 const _pyID& geom = addCmd->GetArg( 1 );
715 // find algo created on <geom> for this mesh
716 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, this->GetID(), hyp->GetType() );
717 //_pyID algoID = algo.IsNull() ? "" : algo->GetID();
718 if ( !algo.IsNull() && hyp->Addition2Creation( addCmd, this->GetID() )) // OK
720 addCmd->SetObject( algo->GetID() );
721 algo->GetCreationCmd()->AddDependantCmd( addCmd );
725 AddMeshAccess( addCmd );
726 // add addition result treatement cmd
727 addErrorTreatmentCmd( addCmd, false );
731 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
732 for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
733 Handle(_pyCommand) subCmd = *cmd;
734 if ( subCmd->GetNbArgs() > 0 )
735 AddMeshAccess( subCmd );
737 myAddHypCmds.clear();
741 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
742 for ( ; hyp != myHypos.end(); ++hyp )
746 //================================================================================
748 * \brief _pyHypothesis constructor
749 * \param theCreationCmd -
751 //================================================================================
753 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
754 _pyObject( theCreationCmd )
756 myDim = myIsAlgo = /*myIsLocal = */myIsWrapped = myIsConverted = false;
759 //================================================================================
761 * \brief Creates algorithm or hypothesis
762 * \param theCreationCmd - The engine command creating a hypothesis
763 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
765 //================================================================================
767 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
769 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
770 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
772 Handle(_pyHypothesis) hyp, algo;
775 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
776 if ( hypTypeQuoted.IsEmpty() )
779 TCollection_AsciiString hypType =
780 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
782 algo = new _pyAlgorithm( theCreationCmd );
783 hyp = new _pyHypothesis( theCreationCmd );
785 // 1D Regular_1D ----------
786 if ( hypType == "Regular_1D" ) {
787 algo->SetDimMethodType( 1, "Segment");
789 else if ( hypType == "LocalLength" ) {
790 hyp->SetDimMethodType( 1, "LocalLength", "Regular_1D");
791 hyp->AddArgMethod( "SetLength" );
793 else if ( hypType == "NumberOfSegments" ) {
794 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
795 hyp->SetDimMethodType( 1, "NumberOfSegments", "Regular_1D");
796 hyp->AddArgMethod( "SetNumberOfSegments" );
797 hyp->AddArgMethod( "SetScaleFactor" );
799 else if ( hypType == "Arithmetic1D" ) {
800 hyp = new _pyComplexParamHypo( theCreationCmd );
801 hyp->SetDimMethodType( 1, "Arithmetic1D", "Regular_1D");
803 else if ( hypType == "StartEndLength" ) {
804 hyp = new _pyComplexParamHypo( theCreationCmd );
805 hyp->SetDimMethodType( 1, "StartEndLength", "Regular_1D");
807 else if ( hypType == "Deflection1D" ) {
808 hyp->SetDimMethodType( 1, "Deflection1D", "Regular_1D");
809 hyp->AddArgMethod( "SetDeflection" );
811 else if ( hypType == "Propagation" ) {
812 hyp->SetDimMethodType( 1, "Propagation", "Regular_1D");
814 else if ( hypType == "QuadraticMesh" ) {
815 hyp->SetDimMethodType( 1, "QuadraticMesh", "Regular_1D");
817 else if ( hypType == "AutomaticLength" ) {
818 hyp->SetDimMethodType( 1, "AutomaticLength", "Regular_1D");
819 hyp->AddArgMethod( "SetFineness");
821 // 1D Python_1D ----------
822 else if ( hypType == "Python_1D" ) {
823 algo->SetDimMethodType( 1, "Segment");
824 algo->myArgs.Append( "algo=smesh.PYTHON");
826 else if ( hypType == "PythonSplit1D" ) {
827 hyp->SetDimMethodType( 1, "PythonSplit1D", "Python_1D");
828 hyp->AddArgMethod( "SetNumberOfSegments");
829 hyp->AddArgMethod( "SetPythonLog10RatioFunction");
832 else if ( hypType == "MEFISTO_2D" ) {
833 algo->SetDimMethodType( 2, "Triangle");
835 else if ( hypType == "MaxElementArea" ) {
836 hyp->SetDimMethodType( 2, "MaxElementArea", "MEFISTO_2D");
837 hyp->AddArgMethod( "SetMaxElementArea");
839 else if ( hypType == "LengthFromEdges" ) {
840 hyp->SetDimMethodType( 2, "LengthFromEdges", "MEFISTO_2D");
842 else if ( hypType == "Quadrangle_2D" ) {
843 algo->SetDimMethodType( 2, "Quadrangle" );
845 else if ( hypType == "QuadranglePreference" ) {
846 hyp->SetDimMethodType( 2, "QuadranglePreference", "Quadrangle_2D");
849 else if ( hypType == "NETGEN_3D") {
850 algo->SetDimMethodType( 3, "Tetrahedron" );
851 algo->myArgs.Append( "algo=smesh.NETGEN" );
853 else if ( hypType == "MaxElementVolume") {
854 hyp->SetDimMethodType( 3, "MaxElementVolume", "NETGEN_3D");
855 hyp->AddArgMethod( "SetMaxElementVolume" );
857 else if ( hypType == "GHS3D_3D" ) {
858 algo->SetDimMethodType( 3, "Tetrahedron");
859 algo->myArgs.Append( "algo=smesh.GHS3D" );
861 else if ( hypType == "Hexa_3D" ) {
862 algo->SetDimMethodType( 3, "Hexahedron");
864 // Repetitive ---------
865 else if ( hypType == "Projection_1D" ) {
866 algo->SetDimMethodType( 1, "Projection1D");
868 else if ( hypType == "ProjectionSource1D" ) {
869 hyp->SetDimMethodType( 1, "SourceEdge", "Projection_1D");
870 hyp->AddArgMethod( "SetSourceEdge");
871 hyp->AddArgMethod( "SetSourceMesh");
872 hyp->AddArgMethod( "SetVertexAssociation", 2 );
874 else if ( hypType == "Projection_2D" ) {
875 algo->SetDimMethodType( 2, "Projection2D");
877 else if ( hypType == "ProjectionSource2D" ) {
878 hyp->SetDimMethodType( 2, "SourceFace", "Projection_2D");
879 hyp->AddArgMethod( "SetSourceFace");
880 hyp->AddArgMethod( "SetSourceMesh");
881 hyp->AddArgMethod( "SetVertexAssociation", 4 );
883 else if ( hypType == "Projection_3D" ) {
884 algo->SetDimMethodType( 3, "Projection3D");
886 else if ( hypType == "ProjectionSource3D" ) {
887 hyp->SetDimMethodType( 3, "SourceShape3D", "Projection_3D");
888 hyp->AddArgMethod( "SetSource3DShape");
889 hyp->AddArgMethod( "SetSourceMesh");
890 hyp->AddArgMethod( "SetVertexAssociation", 4 );
892 else if ( hypType == "Prism_3D" ) {
893 algo->SetDimMethodType( 3, "Prism");
895 else if ( hypType == "RadialPrism_3D" ) {
896 algo->SetDimMethodType( 3, "Prism");
898 else if ( hypType == "NumberOfLayers" ) {
899 hyp->SetDimMethodType( 3, "NumberOfLayers", "RadialPrism_3D");
900 hyp->AddArgMethod( "SetNumberOfLayers" );
902 else if ( hypType == "LayerDistribution" ) {
903 hyp = new _pyLayerDistributionHypo( theCreationCmd );
904 hyp->SetDimMethodType( 3, "LayerDistribution", "RadialPrism_3D");
905 // hyp->AddArgMethod( "SetSource3DShape");
906 // hyp->AddArgMethod( "SetSourceMesh");
907 // hyp->AddArgMethod( "SetVertexAssociation", 4 );
910 if ( algo->GetDim() ) {
911 algo->myType = hypType;
917 //================================================================================
919 * \brief Convert the command adding a hypothesis to mesh into a smesh command
920 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
921 * \param theAlgo - The algo that can create this hypo
922 * \retval bool - false if the command cant be converted
924 //================================================================================
926 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
927 const _pyID& theMesh)
929 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
931 if ( !IsWrappable( theMesh ))
938 // mesh.AddHypothesis(geom,hyp) --> hyp = theMesh.myCreationMethod(args)
939 theCmd->SetResultValue( GetID() );
940 theCmd->SetObject( theMesh );
941 theCmd->SetMethod( myCreationMethod );
943 theCmd->RemoveArgs();
944 for ( int i = 1; i <= myArgs.Length(); ++i ) {
945 if ( !myArgs( i ).IsEmpty() )
946 theCmd->SetArg( i, myArgs( i ));
948 theCmd->SetArg( i, "[]");
950 // set a new creation command
951 GetCreationCmd()->Clear();
952 SetCreationCmd( theCmd );
954 // clear commands setting arg values
955 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
956 for ( ; argCmd != myArgCommands.end(); ++argCmd )
961 // // set arg commands after hypo creation
962 // list<Handle(_pyCommand)>::iterator argCmd = myArgCommands.begin();
963 // for ( ; argCmd != myArgCommands.end(); ++argCmd )
964 // if ( !(*argCmd)->IsEmpty() && GetCommandNb() > (*argCmd)->GetOrderNb() )
965 // theGen->ExchangeCommands( GetCreationCmd(), *argCmd );
968 // set unknown arg commands after hypo creation
969 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
970 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
971 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
972 afterCmd->AddDependantCmd( *cmd );
978 //================================================================================
980 * \brief Remember hypothesis parameter values
981 * \param theCommand - The called hypothesis method
983 //================================================================================
985 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
990 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
991 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
992 while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
993 myArgs.Append( "[]" );
994 for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
995 myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
996 myArgCommands.push_back( theCommand );
999 nbArgs += myNbArgsByMethod( i );
1001 myUnknownCommands.push_back( theCommand );
1004 //================================================================================
1006 * \brief Finish conversion
1008 //================================================================================
1010 void _pyHypothesis::Flush()
1012 if ( IsWrapped() ) {
1015 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1016 for ( ; cmd != myArgCommands.end(); ++cmd ) {
1017 // Add access to a wrapped mesh
1018 theGen->AddMeshAccessorMethod( *cmd );
1019 // Add access to a wrapped algorithm
1020 theGen->AddAlgoAccessorMethod( *cmd );
1022 cmd = myUnknownCommands.begin();
1023 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1024 // Add access to a wrapped mesh
1025 theGen->AddMeshAccessorMethod( *cmd );
1026 // Add access to a wrapped algorithm
1027 theGen->AddAlgoAccessorMethod( *cmd );
1030 // forget previous hypothesis modifications
1031 myArgCommands.clear();
1032 myUnknownCommands.clear();
1035 //================================================================================
1037 * \brief clear creation, arg and unkown commands
1039 //================================================================================
1041 void _pyHypothesis::ClearAllCommands()
1043 GetCreationCmd()->Clear();
1044 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1045 for ( ; cmd != myArgCommands.end(); ++cmd )
1047 cmd = myUnknownCommands.begin();
1048 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1052 //================================================================================
1054 * \brief Remember hypothesis parameter values
1055 * \param theCommand - The called hypothesis method
1057 //================================================================================
1059 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1061 // ex: hyp.SetLength(start, 1)
1062 // hyp.SetLength(end, 0)
1063 ASSERT(( theCommand->GetMethod() == "SetLength" ));
1064 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1065 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1066 while ( myArgs.Length() < i )
1067 myArgs.Append( "[]" );
1068 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1069 myArgCommands.push_back( theCommand );
1072 //================================================================================
1074 * \brief Convert methods of 1D hypotheses to my own methods
1075 * \param theCommand - The called hypothesis method
1077 //================================================================================
1079 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1081 if ( theCommand->GetMethod() != "SetLayerDistribution" )
1084 _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1086 const _pyID& hyp1dID = theCommand->GetArg( 1 );
1087 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1088 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1090 else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1091 // 1D hypo is already set, so distribution changes and the old
1092 // 1D hypo is thrown away
1093 my1dHyp->ClearAllCommands();
1096 if ( my1dHyp.IsNull() )
1097 return; // something wrong :(
1099 // make a new name for 1D hyp = "HypType" + "_Distribution"
1100 if ( my1dHyp->GetCreationCmd()->GetMethod() == "CreateHypothesis" ) {
1101 // not yet converted creation cmd
1102 TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1103 TCollection_AsciiString hypType = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1104 newName = hypType + "_Distribution";
1105 my1dHyp->GetCreationCmd()->SetResultValue( newName );
1108 // already converted creation cmd
1109 newName = my1dHyp->GetCreationCmd()->GetResultValue();
1112 // as creation of 1D hyp was written later then it's edition,
1113 // we need to find all it's edition calls and process them
1114 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1115 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1116 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1117 const _pyID& objID = (*cmdIt)->GetObject();
1118 if ( objID == hyp1dID ) {
1119 my1dHyp->Process( *cmdIt );
1120 my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1121 ( *cmdIt )->SetObject( newName );
1124 if ( !myArgCommands.empty() )
1125 myArgCommands.front()->Clear();
1126 theCommand->SetArg( 1, newName );
1127 myArgCommands.push_back( theCommand );
1128 // copy hyp1d's creation method and args
1129 // myCreationMethod = hyp1d->GetCreationMethod();
1130 // myArgs = hyp1d->GetArgs();
1131 // // make them cleared at conversion
1132 // myArgCommands = hyp1d->GetArgCommands();
1134 // // to be cleared at convertion only
1135 // myArgCommands.push_back( theCommand );
1138 //================================================================================
1141 * \param theAdditionCmd -
1145 //================================================================================
1147 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1148 const _pyID& theMesh)
1150 myIsWrapped = false;
1152 if ( my1dHyp.IsNull() )
1155 // set "SetLayerDistribution()" after addition cmd
1156 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1158 _pyID geom = theAdditionCmd->GetArg( 1 );
1160 my1dHyp->SetMesh( theMesh );
1161 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1164 // clear "SetLayerDistribution()" cmd
1165 myArgCommands.front()->Clear();
1167 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1169 // find RadialPrism algo created on <geom> for theMesh
1170 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this->GetType() );
1171 if ( !algo.IsNull() ) {
1172 GetCreationCmd()->SetObject( algo->GetID() );
1173 GetCreationCmd()->SetMethod( "Get3DHypothesis" );
1174 GetCreationCmd()->RemoveArgs();
1175 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1181 //================================================================================
1185 //================================================================================
1187 void _pyLayerDistributionHypo::Flush()
1189 //my1dHyp.Nullify();
1190 //_pyHypothesis::Flush();
1193 //================================================================================
1195 * \brief additionally to Addition2Creation, clears SetDistrType() command
1196 * \param theCmd - AddHypothesis() command
1197 * \param theMesh - mesh to which a hypothesis is added
1198 * \retval bool - convertion result
1200 //================================================================================
1202 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1203 const _pyID& theMesh)
1205 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1206 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1207 bool scaleDistrType = false;
1208 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1209 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1210 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1211 if ( (*cmd)->GetArg( 1 ) == "1" ) {
1212 scaleDistrType = true;
1215 else if ( !scaleDistrType ) {
1216 // distribution type changed: remove scale factor from args
1217 myArgs.Remove( 2, myArgs.Length() );
1223 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1226 //================================================================================
1228 * \brief remove repeated commands defining distribution
1230 //================================================================================
1232 void _pyNumberOfSegmentsHyp::Flush()
1234 // find number of the last SetDistrType() command
1235 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1236 int distrTypeNb = 0;
1237 for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1238 if ( (*cmd)->GetMethod() == "SetDistrType" )
1239 distrTypeNb = (*cmd)->GetOrderNb();
1241 // clear commands before the last SetDistrType()
1242 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1243 for ( int i = 0; i < 2; ++i ) {
1244 set<TCollection_AsciiString> uniqueMethods;
1245 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1246 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1248 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1249 const TCollection_AsciiString& method = (*cmd)->GetMethod();
1250 if ( !clear || method == "SetNumberOfSegments" ) {
1251 bool isNewInSet = uniqueMethods.insert( method ).second;
1252 clear = !isNewInSet;
1261 //================================================================================
1263 * \brief _pyAlgorithm constructor
1264 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1266 //================================================================================
1268 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1269 : _pyHypothesis( theCreationCmd )
1274 //================================================================================
1276 * \brief Convert the command adding an algorithm to mesh
1277 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1278 * \param theMesh - The mesh needing this algo
1279 * \retval bool - false if the command cant be converted
1281 //================================================================================
1283 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1284 const _pyID& theMeshID)
1286 if ( IsWrappable( theMeshID )) {
1288 myGeom = theCmd->GetArg( 1 );
1290 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1291 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1292 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1299 //================================================================================
1301 * \brief Return starting position of a part of python command
1302 * \param thePartIndex - The index of command part
1303 * \retval int - Part position
1305 //================================================================================
1307 int _pyCommand::GetBegPos( int thePartIndex )
1311 if ( myBegPos.Length() < thePartIndex )
1313 return myBegPos( thePartIndex );
1316 //================================================================================
1318 * \brief Store starting position of a part of python command
1319 * \param thePartIndex - The index of command part
1320 * \param thePosition - Part position
1322 //================================================================================
1324 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1326 while ( myBegPos.Length() < thePartIndex )
1327 myBegPos.Append( UNKNOWN );
1328 myBegPos( thePartIndex ) = thePosition;
1331 //================================================================================
1333 * \brief Returns whitespace symbols at the line beginning
1334 * \retval TCollection_AsciiString - result
1336 //================================================================================
1338 TCollection_AsciiString _pyCommand::GetIndentation()
1341 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1342 GetWord( myString, end, true );
1344 end = GetBegPos( RESULT_IND );
1345 return myString.SubString( 1, end - 1 );
1348 //================================================================================
1350 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1351 * \retval const TCollection_AsciiString & - ResultValue substring
1353 //================================================================================
1355 const TCollection_AsciiString & _pyCommand::GetResultValue()
1357 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1359 int begPos = myString.Location( "=", 1, Length() );
1361 myRes = GetWord( myString, begPos, false );
1364 SetBegPos( RESULT_IND, begPos );
1369 //================================================================================
1371 * \brief Return substring of python command looking like ResVal = Object.Meth()
1372 * \retval const TCollection_AsciiString & - Object substring
1374 //================================================================================
1376 const TCollection_AsciiString & _pyCommand::GetObject()
1378 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1381 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1383 begPos = myString.Location( "=", 1, Length() ) + 1;
1385 myObj = GetWord( myString, begPos, true );
1386 SetBegPos( OBJECT_IND, begPos );
1392 //================================================================================
1394 * \brief Return substring of python command looking like ResVal = Obj.Method()
1395 * \retval const TCollection_AsciiString & - Method substring
1397 //================================================================================
1399 const TCollection_AsciiString & _pyCommand::GetMethod()
1401 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1404 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1405 bool forward = true;
1407 begPos = myString.Location( "(", 1, Length() ) - 1;
1411 myMeth = GetWord( myString, begPos, forward );
1412 SetBegPos( METHOD_IND, begPos );
1418 //================================================================================
1420 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1421 * \retval const TCollection_AsciiString & - Arg<index> substring
1423 //================================================================================
1425 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1427 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1430 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1432 begPos = myString.Location( "(", 1, Length() ) + 1;
1434 int i = 0, prevLen = 0;
1435 while ( begPos != EMPTY ) {
1437 // check if we are looking at the closing parenthesis
1438 while ( begPos <= Length() && isspace( myString.Value( begPos )))
1440 if ( begPos > Length() || myString.Value( begPos ) == ')' )
1442 myArgs.Append( GetWord( myString, begPos, true, true ));
1443 SetBegPos( ARG1_IND + i, begPos );
1444 prevLen = myArgs.Last().Length();
1446 myArgs.Remove( myArgs.Length() ); // no more args
1450 if ( myArgs.Length() < index )
1451 return theEmptyString;
1452 return myArgs( index );
1455 //================================================================================
1457 * \brief Check if char is a word part
1458 * \param c - The character to check
1459 * \retval bool - The check result
1461 //================================================================================
1463 static inline bool isWord(const char c, const bool dotIsWord)
1466 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1469 //================================================================================
1471 * \brief Looks for a word in the string and returns word's beginning
1472 * \param theString - The input string
1473 * \param theStartPos - The position to start the search, returning word's beginning
1474 * \param theForward - The search direction
1475 * \retval TCollection_AsciiString - The found word
1477 //================================================================================
1479 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1481 const bool theForward,
1482 const bool dotIsWord )
1484 int beg = theStartPos, end = theStartPos;
1485 theStartPos = EMPTY;
1486 if ( beg < 1 || beg > theString.Length() )
1487 return theEmptyString;
1489 if ( theForward ) { // search forward
1491 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1493 if ( beg > theString.Length() )
1494 return theEmptyString; // no word found
1497 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1501 else { // search backward
1503 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1506 return theEmptyString; // no word found
1508 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1513 //cout << theString << " ---- " << beg << " - " << end << endl;
1514 return theString.SubString( beg, end );
1517 //================================================================================
1519 * \brief Look for position where not space char is
1520 * \param theString - The string
1521 * \param thePos - The position to search from and which returns result
1522 * \retval bool - false if there are only space after thePos in theString
1526 //================================================================================
1528 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1530 if ( thePos < 1 || thePos > theString.Length() )
1533 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1536 return thePos <= theString.Length();
1539 //================================================================================
1541 * \brief Modify a part of the command
1542 * \param thePartIndex - The index of the part
1543 * \param thePart - The new part string
1544 * \param theOldPart - The old part
1546 //================================================================================
1548 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1549 TCollection_AsciiString& theOldPart)
1551 int pos = GetBegPos( thePartIndex );
1552 if ( pos <= Length() && theOldPart != thePart)
1554 TCollection_AsciiString seperator;
1556 pos = GetBegPos( thePartIndex + 1 );
1557 if ( pos < 1 ) return;
1558 switch ( thePartIndex ) {
1559 case RESULT_IND: seperator = " = "; break;
1560 case OBJECT_IND: seperator = "."; break;
1561 case METHOD_IND: seperator = "()"; break;
1565 myString.Remove( pos, theOldPart.Length() );
1566 if ( !seperator.IsEmpty() )
1567 myString.Insert( pos , seperator );
1568 myString.Insert( pos, thePart );
1569 // update starting positions of the following parts
1570 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1571 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1572 if ( myBegPos( i ) > 0 )
1573 myBegPos( i ) += posDelta;
1575 theOldPart = thePart;
1579 //================================================================================
1581 * \brief Set agrument
1582 * \param index - The argument index, it counts from 1
1583 * \param theArg - The argument string
1585 //================================================================================
1587 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1590 int argInd = ARG1_IND + index - 1;
1591 int pos = GetBegPos( argInd );
1592 if ( pos < 1 ) // no index-th arg exist, append inexistent args
1594 // find a closing parenthesis
1595 if ( int lastArgInd = GetNbArgs() ) {
1596 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
1597 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
1602 while ( pos > 0 && myString.Value( pos ) != ')' )
1605 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
1609 while ( myArgs.Length() < index ) {
1610 if ( myArgs.Length() )
1611 myString.Insert( pos++, "," );
1612 myArgs.Append("None");
1613 myString.Insert( pos, myArgs.Last() );
1614 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1615 pos += myArgs.Last().Length();
1618 SetPart( argInd, theArg, myArgs( index ));
1621 //================================================================================
1623 * \brief Empty arg list
1625 //================================================================================
1627 void _pyCommand::RemoveArgs()
1629 if ( int pos = myString.Location( '(', 1, Length() ))
1630 myString.Trunc( pos );
1633 if ( myBegPos.Length() >= ARG1_IND )
1634 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1637 //================================================================================
1639 * \brief Set dependent commands after this one
1641 //================================================================================
1643 bool _pyCommand::SetDependentCmdsAfter() const
1645 bool orderChanged = false;
1646 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1647 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1648 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1649 orderChanged = true;
1650 theGen->SetCommandAfter( *cmd, this );
1651 (*cmd)->SetDependentCmdsAfter();
1654 return orderChanged;
1656 //================================================================================
1658 * \brief Insert accessor method after theObjectID
1659 * \param theObjectID - id of the accessed object
1660 * \param theAcsMethod - name of the method giving access to the object
1661 * \retval bool - false if theObjectID is not found in the command string
1663 //================================================================================
1665 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
1667 if ( !theAcsMethod )
1669 // start object search from the object, i.e. ignore result
1671 int beg = GetBegPos( OBJECT_IND );
1672 if ( beg < 1 || beg > Length() )
1674 while (( beg = myString.Location( theObjectID, beg, Length() )))
1676 // check that theObjectID is not just a part of a longer ID
1677 int afterEnd = beg + theObjectID.Length();
1678 Standard_Character c = myString.Value( afterEnd );
1679 if ( !isalnum( c ) && c != ':' ) {
1681 int oldLen = Length();
1682 myString.Insert( afterEnd, (char*) theAcsMethod );
1683 myString.Insert( afterEnd, "." );
1684 // update starting positions of the parts following the modified one
1685 int posDelta = Length() - oldLen;
1686 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
1687 if ( myBegPos( i ) > afterEnd )
1688 myBegPos( i ) += posDelta;
1692 beg = afterEnd; // is a part - next search
1697 //================================================================================
1699 * \brief Return method name giving access to an interaface object wrapped by python class
1700 * \retval const char* - method name
1702 //================================================================================
1704 const char* _pyObject::AccessorMethod() const