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);
58 IMPLEMENT_STANDARD_RTTIEXT(_pySegmentLengthAroundVertexHyp,_pyHypothesis);
61 using SMESH::TPythonDump;
64 * \brief Container of commands into which the initial script is split.
65 * It also contains data coresponding to SMESH_Gen contents
67 static Handle(_pyGen) theGen;
69 static TCollection_AsciiString theEmptyString;
71 //#define DUMP_CONVERSION
73 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
74 #undef DUMP_CONVERSION
77 //================================================================================
79 * \brief Convert python script using commands of smesh.py
80 * \param theScript - Input script
81 * \retval TCollection_AsciiString - Convertion result
83 //================================================================================
85 TCollection_AsciiString
86 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
87 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
89 theGen = new _pyGen( theEntry2AccessorMethod );
91 // split theScript into separate commands
92 int from = 1, end = theScript.Length(), to;
93 while ( from < end && ( to = theScript.Location( "\n", from, end )))
96 // cut out and store a command
97 theGen->AddCommand( theScript.SubString( from, to - 1 ));
102 #ifdef DUMP_CONVERSION
103 cout << endl << " ######## RESULT ######## " << endl<< endl;
105 // reorder commands after conversion
106 list< Handle(_pyCommand) >::iterator cmd;
109 orderChanges = false;
110 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
111 if ( (*cmd)->SetDependentCmdsAfter() )
113 } while ( orderChanges );
115 // concat commands back into a script
116 TCollection_AsciiString aScript;
117 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
119 #ifdef DUMP_CONVERSION
120 cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
122 if ( !(*cmd)->IsEmpty() ) {
124 aScript += (*cmd)->GetString();
134 //================================================================================
136 * \brief _pyGen constructor
138 //================================================================================
140 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
141 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
142 myID2AccessorMethod( theEntry2AccessorMethod )
145 myHasPattern = false;
146 // make that GetID() to return TPythonDump::SMESHGenName()
147 GetCreationCmd()->GetString() += "=";
150 //================================================================================
152 * \brief Convert a command using a specific converter
153 * \param theCommand - the command to convert
155 //================================================================================
157 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
159 // store theCommand in the sequence
160 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
162 Handle(_pyCommand) aCommand = myCommands.back();
163 #ifdef DUMP_CONVERSION
164 cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
167 _pyID objID = aCommand->GetObject();
169 if ( objID.IsEmpty() )
173 if ( objID == this->GetID() ) {
174 this->Process( aCommand );
177 // SMESH_Mesh method?
178 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
179 if ( id_mesh != myMeshes.end() ) {
180 id_mesh->second->Process( aCommand );
183 // SMESH_Hypothesis method?
184 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
185 for ( ; hyp != myHypos.end(); ++hyp )
186 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
187 (*hyp)->Process( aCommand );
191 // Add access to a wrapped mesh
192 AddMeshAccessorMethod( aCommand );
194 // Add access to a wrapped algorithm
195 AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
197 // PAL12227. PythonDump was not updated at proper time; result is
198 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
199 // TypeError: __init__() takes exactly 11 arguments (10 given)
200 char wrongCommand[] = "SMESH.Filter.Criterion(";
201 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
203 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
204 // there must be 10 arguments, 5-th arg ThresholdID is missing,
205 const int wrongNbArgs = 9, missingArg = 5;
206 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
208 for ( int i = wrongNbArgs; i > missingArg; --i )
209 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
210 tmpCmd.SetArg( missingArg, "''");
211 aCommand->GetString().Trunc( beg - 1 );
212 aCommand->GetString() += tmpCmd.GetString();
218 //================================================================================
220 * \brief Convert the command or remember it for later conversion
221 * \param theCommand - The python command calling a method of SMESH_Gen
223 //================================================================================
225 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
227 // there are methods to convert:
228 // CreateMesh( shape )
229 // CreateHypothesis( theHypType, theLibName )
230 // Compute( mesh, geom )
232 if ( theCommand->GetMethod() == "CreateMesh" )
234 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
235 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
239 // CreateHypothesis()
240 if ( theCommand->GetMethod() == "CreateHypothesis" )
242 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
246 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
247 if ( theCommand->GetMethod() == "Compute" )
249 const _pyID& meshID = theCommand->GetArg( 1 );
250 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
251 if ( id_mesh != myMeshes.end() ) {
252 theCommand->SetObject( meshID );
253 theCommand->RemoveArgs();
254 id_mesh->second->Flush();
259 // leave only one smeshgen.GetPattern() in the script
260 if ( theCommand->GetMethod() == "GetPattern" ) {
261 if ( myHasPattern ) {
268 // smeshgen.Method() --> smesh.smesh.Method()
269 theCommand->SetObject( SMESH_2smeshpy::GenName() );
272 //================================================================================
274 * \brief Convert the remembered commands
276 //================================================================================
280 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
281 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
282 if ( ! id_mesh->second.IsNull() )
283 id_mesh->second->Flush();
285 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
286 for ( ; hyp != myHypos.end(); ++hyp )
287 if ( !hyp->IsNull() ) {
289 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
290 if ( !(*hyp)->IsWrapped() )
291 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
295 //================================================================================
297 * \brief Add access method to mesh that is an object or an argument
298 * \param theCmd - command to add access method
299 * \retval bool - true if added
301 //================================================================================
303 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
305 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
306 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
307 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
313 //================================================================================
315 * \brief Add access method to algo that is an object or an argument
316 * \param theCmd - command to add access method
317 * \retval bool - true if added
319 //================================================================================
321 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
323 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
324 for ( ; hyp != myHypos.end(); ++hyp ) {
325 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
326 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
332 //================================================================================
334 * \brief Find hypothesis by ID (entry)
335 * \param theHypID - The hypothesis ID
336 * \retval Handle(_pyHypothesis) - The found hypothesis
338 //================================================================================
340 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
342 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
343 for ( ; hyp != myHypos.end(); ++hyp )
344 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
346 return Handle(_pyHypothesis)();
349 //================================================================================
351 * \brief Find algorithm the created algorithm
352 * \param theGeom - The shape ID the algorithm was created on
353 * \param theMesh - The mesh ID that created the algorithm
354 * \param dim - The algo dimension
355 * \retval Handle(_pyHypothesis) - The found algo
357 //================================================================================
359 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
360 const TCollection_AsciiString& theAlgoType )
362 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
363 for ( ; hyp != myHypos.end(); ++hyp )
364 if ( !hyp->IsNull() &&
366 (*hyp)->GetType() == theAlgoType &&
367 (*hyp)->GetGeom() == theGeom &&
368 (*hyp)->GetMesh() == theMesh )
373 //================================================================================
375 * \brief Change order of commands in the script
376 * \param theCmd1 - One command
377 * \param theCmd2 - Another command
379 //================================================================================
381 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
383 list< Handle(_pyCommand) >::iterator pos1, pos2;
384 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
385 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
386 myCommands.insert( pos1, theCmd2 );
387 myCommands.insert( pos2, theCmd1 );
388 myCommands.erase( pos1 );
389 myCommands.erase( pos2 );
391 int nb1 = theCmd1->GetOrderNb();
392 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
393 theCmd2->SetOrderNb( nb1 );
394 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
395 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
398 //================================================================================
400 * \brief Set one command after the other
401 * \param theCmd - Command to move
402 * \param theAfterCmd - Command ater which to insert the first one
404 //================================================================================
406 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
409 //cout << "SET\t" << theAfterCmd->GetString() << endl << "BEFORE\t" << theCmd->GetString() << endl<<endl;
411 list< Handle(_pyCommand) >::iterator pos;
412 pos = find( myCommands.begin(), myCommands.end(), theCmd );
413 myCommands.erase( pos );
414 pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
415 myCommands.insert( ++pos, theCmd );
418 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
419 (*pos)->SetOrderNb( i++ );
422 //================================================================================
424 * \brief Set method to access to object wrapped with python class
425 * \param theID - The wrapped object entry
426 * \param theMethod - The accessor method
428 //================================================================================
430 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
432 myID2AccessorMethod.Bind( theID, (char*) theMethod );
435 //================================================================================
437 * \brief Find out type of geom group
438 * \param grpID - The geom group entry
439 * \retval int - The type
441 //================================================================================
443 static bool sameGroupType( const _pyID& grpID,
444 const TCollection_AsciiString& theType)
446 // define group type as smesh.Mesh.Group() does
448 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
449 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
450 if ( !aSObj->_is_nil() ) {
451 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
452 if ( !aGeomObj->_is_nil() ) {
453 switch ( aGeomObj->GetShapeType() ) {
454 case GEOM::VERTEX: type = SMESH::NODE; break;
455 case GEOM::EDGE: type = SMESH::EDGE; break;
456 case GEOM::FACE: type = SMESH::FACE; break;
458 case GEOM::SHELL: type = SMESH::VOLUME; break;
459 case GEOM::COMPOUND: {
460 GEOM::GEOM_Gen_var aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
461 if ( !aGeomGen->_is_nil() ) {
462 GEOM::GEOM_IGroupOperations_var aGrpOp =
463 aGeomGen->GetIGroupOperations( study->StudyId() );
464 if ( !aGrpOp->_is_nil() ) {
465 switch ( aGrpOp->GetType( aGeomObj )) {
466 case TopAbs_VERTEX: type = SMESH::NODE; break;
467 case TopAbs_EDGE: type = SMESH::EDGE; break;
468 case TopAbs_FACE: type = SMESH::FACE; break;
469 case TopAbs_SOLID: type = SMESH::VOLUME; break;
480 MESSAGE("Type of the group " << grpID << " not found");
483 if ( theType.IsIntegerValue() )
484 return type == theType.IntegerValue();
487 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
488 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
489 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
490 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
496 //================================================================================
499 * \param theCreationCmd -
501 //================================================================================
503 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd):
504 _pyObject(theCreationCmd), myHasEditor(false)
506 // convert my creation command
507 Handle(_pyCommand) creationCmd = GetCreationCmd();
508 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
509 creationCmd->SetMethod( "Mesh" );
511 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
514 //================================================================================
516 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
517 * \param theCommand - Engine method called for this mesh
519 //================================================================================
521 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
523 // smesh.py wraps the following methods:
525 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
526 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
527 // 2. AddHypothesis(geom, hyp)
528 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
529 // 3. CreateGroupFromGEOM(type, name, grp)
530 // --> in Mesh.Group(grp, name="")
531 // 4. ExportToMED(f, opt, version)
532 // --> in Mesh.ExportToMED( f, version, opt=0 )
533 // 5. ExportMED(f, opt)
534 // --> in Mesh.ExportMED( f,opt=0 )
536 // --> in Mesh.ExportDAT( f )
538 // --> in Mesh.ExportUNV(f)
539 // 8. ExportSTL(f, ascii)
540 // --> in Mesh.ExportSTL(f, ascii=1)
542 const TCollection_AsciiString method = theCommand->GetMethod();
543 if ( method == "GetSubMesh" ) {
544 mySubmeshes.push_back( theCommand );
546 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
547 myAddHypCmds.push_back( theCommand );
549 const _pyID& hypID = theCommand->GetArg( 2 );
550 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
551 if ( !hyp.IsNull() ) {
552 myHypos.push_back( hyp );
553 if ( hyp->GetMesh().IsEmpty() )
554 hyp->SetMesh( this->GetID() );
557 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
558 _pyID grp = theCommand->GetArg( 3 );
559 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
560 theCommand->SetMethod( "Group" );
561 theCommand->RemoveArgs();
562 theCommand->SetArg( 1, grp );
565 AddMeshAccess( theCommand );
568 else if ( method == "ExportToMED" ) {//(f, opt, version)
569 // --> (f, version, opt)
570 _pyID opt = theCommand->GetArg( 2 );
571 _pyID ver = theCommand->GetArg( 3 );
572 theCommand->SetArg( 2, ver );
573 theCommand->SetArg( 3, opt );
575 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
577 const _pyID & hypID = theCommand->GetArg( 2 );
579 // check if this mesh still has corresponding addition command
580 bool hasAddCmd = false;
581 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
582 while ( cmd != myAddHypCmds.end() )
584 // AddHypothesis(geom, hyp)
585 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
588 cmd = myAddHypCmds.erase( cmd );
595 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
596 if ( ! hasAddCmd ) { // hypo addition already wrapped
597 // access to wrapped mesh
598 AddMeshAccess( theCommand );
599 // access to wrapped algo
600 if ( !hyp.IsNull() && hyp->IsAlgo() && hyp->IsWrapped() )
601 theCommand->SetArg( 2, theCommand->GetArg( 2 ) + ".GetAlgorithm()" );
603 // remove hyp from myHypos
604 myHypos.remove( hyp );
607 // leave only one " mesh_editor_<nb> = mesh.GetMeshEditor()"
608 else if ( theCommand->GetMethod() == "GetMeshEditor")
613 AddMeshAccess( theCommand );
617 // apply theCommand to the mesh wrapped by smeshpy mesh
620 AddMeshAccess( theCommand );
626 //================================================================================
628 * \brief add addition result treatement command
629 * \param addCmd - hypothesis addition command
631 //================================================================================
633 void addErrorTreatmentCmd( Handle(_pyCommand) & addCmd,
636 return; // TO DEBUD - TreatHypoStatus() is not placed right after addCmd
637 // addCmd: status = mesh.AddHypothesis( geom, hypo )
638 // treatement command:
639 // def TreatHypoStatus(status, hypName, geomName, isAlgo):
640 TCollection_AsciiString status = addCmd->GetResultValue();
641 if ( !status.IsEmpty() ) {
642 const _pyID& geomID = addCmd->GetArg( 1 );
643 const _pyID& hypoID = addCmd->GetArg( 2 );
644 TCollection_AsciiString cmdStr = addCmd->GetIndentation() +
645 SMESH_2smeshpy::SmeshpyName() + ".TreatHypoStatus( " + status + ", " +
646 SMESH_2smeshpy::SmeshpyName() + ".GetName(" + hypoID + "), " +
647 SMESH_2smeshpy::SmeshpyName() + ".GetName(" + geomID + "), " +
648 (char*)( isAlgo ? "True" : "False" ) + " )";
649 Handle(_pyCommand) cmd = theGen->AddCommand( cmdStr );
650 addCmd->AddDependantCmd( cmd, true );
655 //================================================================================
657 * \brief Convert creation and addition of all algos and hypos
659 //================================================================================
661 void _pyMesh::Flush()
663 list < Handle(_pyCommand) >::iterator cmd, cmd2;
665 // try to convert algo addition like this:
666 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
667 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
669 Handle(_pyCommand) addCmd = *cmd;
670 const _pyID& algoID = addCmd->GetArg( 2 );
671 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
672 if ( algo.IsNull() || !algo->IsAlgo() )
675 _pyID geom = addCmd->GetArg( 1 );
676 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
678 // wrapped algo is created atfer mesh creation
679 GetCreationCmd()->AddDependantCmd( addCmd );
681 if ( geom != GetGeom() ) // local algo
683 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
684 addCmd->SetArg( addCmd->GetNbArgs() + 1,
685 TCollection_AsciiString( "geom=" ) + geom );
686 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
687 for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
688 Handle(_pyCommand) subCmd = *cmd2;
689 if ( geom == subCmd->GetArg( 1 )) {
690 subCmd->SetObject( algo->GetID() );
691 subCmd->RemoveArgs();
692 addCmd->AddDependantCmd( subCmd );
697 else // ALGO was already created
699 // mesh.AddHypothesis(geom, ALGO ) --> mesh.GetMesh().AddHypothesis(geom, ALGO )
700 AddMeshAccess( addCmd );
701 // mesh.GetMesh().AddHypothesis(geom, ALGO ) ->
702 // mesh.GetMesh().AddHypothesis(geom, ALGO.GetAlgorithm() )
703 addCmd->SetArg( 2, addCmd->GetArg( 2 ) + ".GetAlgorithm()" );
704 // add addition result treatement cmd
705 addErrorTreatmentCmd( addCmd, true );
709 // try to convert hypo addition like this:
710 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
711 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
713 Handle(_pyCommand) addCmd = *cmd;
714 const _pyID& hypID = addCmd->GetArg( 2 );
715 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
716 if ( hyp.IsNull() || hyp->IsAlgo() )
718 const _pyID& geom = addCmd->GetArg( 1 );
719 // find algo created on <geom> for this mesh
720 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, this->GetID(), hyp->GetType() );
721 //_pyID algoID = algo.IsNull() ? "" : algo->GetID();
722 if ( !algo.IsNull() && hyp->Addition2Creation( addCmd, this->GetID() )) // OK
724 addCmd->SetObject( algo->GetID() );
725 algo->GetCreationCmd()->AddDependantCmd( addCmd );
729 AddMeshAccess( addCmd );
730 // add addition result treatement cmd
731 addErrorTreatmentCmd( addCmd, false );
735 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
736 for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
737 Handle(_pyCommand) subCmd = *cmd;
738 if ( subCmd->GetNbArgs() > 0 )
739 AddMeshAccess( subCmd );
741 myAddHypCmds.clear();
745 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
746 for ( ; hyp != myHypos.end(); ++hyp )
750 //================================================================================
752 * \brief _pyHypothesis constructor
753 * \param theCreationCmd -
755 //================================================================================
757 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
758 _pyObject( theCreationCmd )
760 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
763 //================================================================================
765 * \brief Creates algorithm or hypothesis
766 * \param theCreationCmd - The engine command creating a hypothesis
767 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
769 //================================================================================
771 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
773 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
774 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
776 Handle(_pyHypothesis) hyp, algo;
779 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
780 if ( hypTypeQuoted.IsEmpty() )
783 TCollection_AsciiString hypType =
784 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
786 algo = new _pyAlgorithm( theCreationCmd );
787 hyp = new _pyHypothesis( theCreationCmd );
789 // 1D Regular_1D ----------
790 if ( hypType == "Regular_1D" ) {
791 // set mesh's method creating algo,
792 // i.e. convertion result will be "regular1d = Mesh.Segment()"
793 algo->SetConvMethodAndType("Segment");
795 else if ( hypType == "LocalLength" ) {
796 // set algo's method creating hyp, and algo type
797 hyp->SetConvMethodAndType( "LocalLength", "Regular_1D");
798 // set method whose 1 arg will become the 1-st arg of hyp creation command
799 // i.e. convertion result will be "locallength = regular1d.LocalLength(<arg of SetLength()>)"
800 hyp->AddArgMethod( "SetLength" );
802 else if ( hypType == "NumberOfSegments" ) {
803 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
804 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
805 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
806 hyp->AddArgMethod( "SetNumberOfSegments" );
807 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
808 hyp->AddArgMethod( "SetScaleFactor" );
810 else if ( hypType == "Arithmetic1D" ) {
811 hyp = new _pyComplexParamHypo( theCreationCmd );
812 hyp->SetConvMethodAndType( "Arithmetic1D", "Regular_1D");
814 else if ( hypType == "StartEndLength" ) {
815 hyp = new _pyComplexParamHypo( theCreationCmd );
816 hyp->SetConvMethodAndType( "StartEndLength", "Regular_1D");
818 else if ( hypType == "Deflection1D" ) {
819 hyp->SetConvMethodAndType( "Deflection1D", "Regular_1D");
820 hyp->AddArgMethod( "SetDeflection" );
822 else if ( hypType == "Propagation" ) {
823 hyp->SetConvMethodAndType( "Propagation", "Regular_1D");
825 else if ( hypType == "QuadraticMesh" ) {
826 hyp->SetConvMethodAndType( "QuadraticMesh", "Regular_1D");
828 else if ( hypType == "AutomaticLength" ) {
829 hyp->SetConvMethodAndType( "AutomaticLength", "Regular_1D");
830 hyp->AddArgMethod( "SetFineness");
832 else if ( hypType == "SegmentLengthAroundVertex" ) {
833 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
834 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
835 hyp->AddArgMethod( "SetLength" );
837 // 1D Python_1D ----------
838 else if ( hypType == "Python_1D" ) {
839 algo->SetConvMethodAndType( "Segment");
840 algo->myArgs.Append( "algo=smesh.PYTHON");
842 else if ( hypType == "PythonSplit1D" ) {
843 hyp->SetConvMethodAndType( "PythonSplit1D", "Python_1D");
844 hyp->AddArgMethod( "SetNumberOfSegments");
845 hyp->AddArgMethod( "SetPythonLog10RatioFunction");
848 else if ( hypType == "MEFISTO_2D" ) {
849 algo->SetConvMethodAndType( "Triangle");
851 else if ( hypType == "MaxElementArea" ) {
852 hyp->SetConvMethodAndType( "MaxElementArea", "MEFISTO_2D");
853 hyp->AddArgMethod( "SetMaxElementArea");
855 else if ( hypType == "LengthFromEdges" ) {
856 hyp->SetConvMethodAndType( "LengthFromEdges", "MEFISTO_2D");
858 else if ( hypType == "Quadrangle_2D" ) {
859 algo->SetConvMethodAndType( "Quadrangle" );
861 else if ( hypType == "QuadranglePreference" ) {
862 hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
865 else if ( hypType == "NETGEN_3D") {
866 algo->SetConvMethodAndType( "Tetrahedron" );
867 algo->myArgs.Append( "algo=smesh.NETGEN" );
869 else if ( hypType == "MaxElementVolume") {
870 hyp->SetConvMethodAndType( "MaxElementVolume", "NETGEN_3D");
871 hyp->AddArgMethod( "SetMaxElementVolume" );
873 else if ( hypType == "GHS3D_3D" ) {
874 algo->SetConvMethodAndType( "Tetrahedron");
875 algo->myArgs.Append( "algo=smesh.GHS3D" );
877 else if ( hypType == "Hexa_3D" ) {
878 algo->SetConvMethodAndType( "Hexahedron");
880 // Repetitive ---------
881 else if ( hypType == "Projection_1D" ) {
882 algo->SetConvMethodAndType( "Projection1D");
884 else if ( hypType == "ProjectionSource1D" ) {
885 hyp->SetConvMethodAndType( "SourceEdge", "Projection_1D");
886 hyp->AddArgMethod( "SetSourceEdge");
887 hyp->AddArgMethod( "SetSourceMesh");
888 // 2 args of SetVertexAssociation() will become the 3-th and 4-th args of hyp creation command
889 hyp->AddArgMethod( "SetVertexAssociation", 2 );
891 else if ( hypType == "Projection_2D" ) {
892 algo->SetConvMethodAndType( "Projection2D");
894 else if ( hypType == "ProjectionSource2D" ) {
895 hyp->SetConvMethodAndType( "SourceFace", "Projection_2D");
896 hyp->AddArgMethod( "SetSourceFace");
897 hyp->AddArgMethod( "SetSourceMesh");
898 hyp->AddArgMethod( "SetVertexAssociation", 4 );
900 else if ( hypType == "Projection_3D" ) {
901 algo->SetConvMethodAndType( "Projection3D");
903 else if ( hypType == "ProjectionSource3D" ) {
904 hyp->SetConvMethodAndType( "SourceShape3D", "Projection_3D");
905 hyp->AddArgMethod( "SetSource3DShape");
906 hyp->AddArgMethod( "SetSourceMesh");
907 hyp->AddArgMethod( "SetVertexAssociation", 4 );
909 else if ( hypType == "Prism_3D" ) {
910 algo->SetConvMethodAndType( "Prism");
912 else if ( hypType == "RadialPrism_3D" ) {
913 algo->SetConvMethodAndType( "Prism");
915 else if ( hypType == "NumberOfLayers" ) {
916 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialPrism_3D");
917 hyp->AddArgMethod( "SetNumberOfLayers" );
919 else if ( hypType == "LayerDistribution" ) {
920 hyp = new _pyLayerDistributionHypo( theCreationCmd );
921 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
924 if ( !algo->GetCreationMethod().IsEmpty() ) {
925 algo->myType = hypType;
931 //================================================================================
933 * \brief Convert the command adding a hypothesis to mesh into a smesh command
934 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
935 * \param theAlgo - The algo that can create this hypo
936 * \retval bool - false if the command cant be converted
938 //================================================================================
940 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
941 const _pyID& theMesh)
943 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
945 if ( !IsWrappable( theMesh ))
952 // mesh.AddHypothesis(geom,hyp) --> hyp = theMesh.myCreationMethod(args)
953 theCmd->SetResultValue( GetID() );
954 theCmd->SetObject( theMesh );
955 theCmd->SetMethod( myCreationMethod );
957 theCmd->RemoveArgs();
958 for ( int i = 1; i <= myArgs.Length(); ++i ) {
959 if ( !myArgs( i ).IsEmpty() )
960 theCmd->SetArg( i, myArgs( i ));
962 theCmd->SetArg( i, "[]");
964 // set a new creation command
965 GetCreationCmd()->Clear();
966 SetCreationCmd( theCmd );
968 // clear commands setting arg values
969 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
970 for ( ; argCmd != myArgCommands.end(); ++argCmd )
975 // // set arg commands after hypo creation
976 // list<Handle(_pyCommand)>::iterator argCmd = myArgCommands.begin();
977 // for ( ; argCmd != myArgCommands.end(); ++argCmd )
978 // if ( !(*argCmd)->IsEmpty() && GetCommandNb() > (*argCmd)->GetOrderNb() )
979 // theGen->ExchangeCommands( GetCreationCmd(), *argCmd );
982 // set unknown arg commands after hypo creation
983 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
984 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
985 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
986 afterCmd->AddDependantCmd( *cmd );
992 //================================================================================
994 * \brief Remember hypothesis parameter values
995 * \param theCommand - The called hypothesis method
997 //================================================================================
999 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
1001 ASSERT( !myIsAlgo );
1004 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
1005 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
1006 while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
1007 myArgs.Append( "[]" );
1008 for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
1009 myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
1010 myArgCommands.push_back( theCommand );
1013 nbArgs += myNbArgsByMethod( i );
1015 myUnknownCommands.push_back( theCommand );
1018 //================================================================================
1020 * \brief Finish conversion
1022 //================================================================================
1024 void _pyHypothesis::Flush()
1026 if ( IsWrapped() ) {
1029 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1030 for ( ; cmd != myArgCommands.end(); ++cmd ) {
1031 // Add access to a wrapped mesh
1032 theGen->AddMeshAccessorMethod( *cmd );
1033 // Add access to a wrapped algorithm
1034 theGen->AddAlgoAccessorMethod( *cmd );
1036 cmd = myUnknownCommands.begin();
1037 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1038 // Add access to a wrapped mesh
1039 theGen->AddMeshAccessorMethod( *cmd );
1040 // Add access to a wrapped algorithm
1041 theGen->AddAlgoAccessorMethod( *cmd );
1044 // forget previous hypothesis modifications
1045 myArgCommands.clear();
1046 myUnknownCommands.clear();
1049 //================================================================================
1051 * \brief clear creation, arg and unkown commands
1053 //================================================================================
1055 void _pyHypothesis::ClearAllCommands()
1057 GetCreationCmd()->Clear();
1058 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1059 for ( ; cmd != myArgCommands.end(); ++cmd )
1061 cmd = myUnknownCommands.begin();
1062 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1066 //================================================================================
1068 * \brief Remember hypothesis parameter values
1069 * \param theCommand - The called hypothesis method
1071 //================================================================================
1073 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1075 // ex: hyp.SetLength(start, 1)
1076 // hyp.SetLength(end, 0)
1077 ASSERT(( theCommand->GetMethod() == "SetLength" ));
1078 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1079 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1080 while ( myArgs.Length() < i )
1081 myArgs.Append( "[]" );
1082 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1083 myArgCommands.push_back( theCommand );
1086 //================================================================================
1088 * \brief Convert methods of 1D hypotheses to my own methods
1089 * \param theCommand - The called hypothesis method
1091 //================================================================================
1093 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1095 if ( theCommand->GetMethod() != "SetLayerDistribution" )
1098 _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1100 const _pyID& hyp1dID = theCommand->GetArg( 1 );
1101 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1102 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1104 else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1105 // 1D hypo is already set, so distribution changes and the old
1106 // 1D hypo is thrown away
1107 my1dHyp->ClearAllCommands();
1110 if ( my1dHyp.IsNull() )
1111 return; // something wrong :(
1113 // make a new name for 1D hyp = "HypType" + "_Distribution"
1114 if ( my1dHyp->GetCreationCmd()->GetMethod() == "CreateHypothesis" ) {
1115 // not yet converted creation cmd
1116 TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1117 TCollection_AsciiString hypType = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1118 newName = hypType + "_Distribution";
1119 my1dHyp->GetCreationCmd()->SetResultValue( newName );
1122 // already converted creation cmd
1123 newName = my1dHyp->GetCreationCmd()->GetResultValue();
1126 // as creation of 1D hyp was written later then it's edition,
1127 // we need to find all it's edition calls and process them
1128 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1129 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1130 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1131 const _pyID& objID = (*cmdIt)->GetObject();
1132 if ( objID == hyp1dID ) {
1133 my1dHyp->Process( *cmdIt );
1134 my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1135 ( *cmdIt )->SetObject( newName );
1138 if ( !myArgCommands.empty() )
1139 myArgCommands.front()->Clear();
1140 theCommand->SetArg( 1, newName );
1141 myArgCommands.push_back( theCommand );
1142 // copy hyp1d's creation method and args
1143 // myCreationMethod = hyp1d->GetCreationMethod();
1144 // myArgs = hyp1d->GetArgs();
1145 // // make them cleared at conversion
1146 // myArgCommands = hyp1d->GetArgCommands();
1148 // // to be cleared at convertion only
1149 // myArgCommands.push_back( theCommand );
1152 //================================================================================
1155 * \param theAdditionCmd -
1159 //================================================================================
1161 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1162 const _pyID& theMesh)
1164 myIsWrapped = false;
1166 if ( my1dHyp.IsNull() )
1169 // set "SetLayerDistribution()" after addition cmd
1170 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1172 _pyID geom = theAdditionCmd->GetArg( 1 );
1174 my1dHyp->SetMesh( theMesh );
1175 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1178 // clear "SetLayerDistribution()" cmd
1179 myArgCommands.front()->Clear();
1181 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1183 // find RadialPrism algo created on <geom> for theMesh
1184 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this->GetType() );
1185 if ( !algo.IsNull() ) {
1186 GetCreationCmd()->SetObject( algo->GetID() );
1187 GetCreationCmd()->SetMethod( "Get3DHypothesis" );
1188 GetCreationCmd()->RemoveArgs();
1189 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1195 //================================================================================
1199 //================================================================================
1201 void _pyLayerDistributionHypo::Flush()
1203 //my1dHyp.Nullify();
1204 //_pyHypothesis::Flush();
1207 //================================================================================
1209 * \brief additionally to Addition2Creation, clears SetDistrType() command
1210 * \param theCmd - AddHypothesis() command
1211 * \param theMesh - mesh to which a hypothesis is added
1212 * \retval bool - convertion result
1214 //================================================================================
1216 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1217 const _pyID& theMesh)
1219 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1220 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1221 bool scaleDistrType = false;
1222 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1223 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1224 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1225 if ( (*cmd)->GetArg( 1 ) == "1" ) {
1226 scaleDistrType = true;
1229 else if ( !scaleDistrType ) {
1230 // distribution type changed: remove scale factor from args
1231 myArgs.Remove( 2, myArgs.Length() );
1237 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1240 //================================================================================
1242 * \brief remove repeated commands defining distribution
1244 //================================================================================
1246 void _pyNumberOfSegmentsHyp::Flush()
1248 // find number of the last SetDistrType() command
1249 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1250 int distrTypeNb = 0;
1251 for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1252 if ( (*cmd)->GetMethod() == "SetDistrType" )
1253 distrTypeNb = (*cmd)->GetOrderNb();
1255 // clear commands before the last SetDistrType()
1256 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1257 for ( int i = 0; i < 2; ++i ) {
1258 set<TCollection_AsciiString> uniqueMethods;
1259 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1260 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1262 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1263 const TCollection_AsciiString& method = (*cmd)->GetMethod();
1264 if ( !clear || method == "SetNumberOfSegments" ) {
1265 bool isNewInSet = uniqueMethods.insert( method ).second;
1266 clear = !isNewInSet;
1275 //================================================================================
1277 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
1278 * into regular1D.LengthNearVertex( length, vertex )
1279 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
1280 * \param theMesh - The mesh needing this hypo
1281 * \retval bool - false if the command cant be converted
1283 //================================================================================
1285 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1286 const _pyID& theMeshID)
1288 if ( IsWrappable( theMeshID )) {
1290 _pyID vertex = theCmd->GetArg( 1 );
1292 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) --> theMeshID.LengthNearVertex( length )
1293 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1295 theCmd->SetArg( 2, vertex );
1302 //================================================================================
1304 * \brief _pyAlgorithm constructor
1305 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1307 //================================================================================
1309 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1310 : _pyHypothesis( theCreationCmd )
1315 //================================================================================
1317 * \brief Convert the command adding an algorithm to mesh
1318 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1319 * \param theMesh - The mesh needing this algo
1320 * \retval bool - false if the command cant be converted
1322 //================================================================================
1324 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1325 const _pyID& theMeshID)
1327 if ( IsWrappable( theMeshID )) {
1329 myGeom = theCmd->GetArg( 1 );
1331 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1332 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1333 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1340 //================================================================================
1342 * \brief Return starting position of a part of python command
1343 * \param thePartIndex - The index of command part
1344 * \retval int - Part position
1346 //================================================================================
1348 int _pyCommand::GetBegPos( int thePartIndex )
1352 if ( myBegPos.Length() < thePartIndex )
1354 return myBegPos( thePartIndex );
1357 //================================================================================
1359 * \brief Store starting position of a part of python command
1360 * \param thePartIndex - The index of command part
1361 * \param thePosition - Part position
1363 //================================================================================
1365 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1367 while ( myBegPos.Length() < thePartIndex )
1368 myBegPos.Append( UNKNOWN );
1369 myBegPos( thePartIndex ) = thePosition;
1372 //================================================================================
1374 * \brief Returns whitespace symbols at the line beginning
1375 * \retval TCollection_AsciiString - result
1377 //================================================================================
1379 TCollection_AsciiString _pyCommand::GetIndentation()
1382 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1383 GetWord( myString, end, true );
1385 end = GetBegPos( RESULT_IND );
1386 return myString.SubString( 1, end - 1 );
1389 //================================================================================
1391 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1392 * \retval const TCollection_AsciiString & - ResultValue substring
1394 //================================================================================
1396 const TCollection_AsciiString & _pyCommand::GetResultValue()
1398 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1400 int begPos = myString.Location( "=", 1, Length() );
1402 myRes = GetWord( myString, begPos, false );
1405 SetBegPos( RESULT_IND, begPos );
1410 //================================================================================
1412 * \brief Return substring of python command looking like ResVal = Object.Meth()
1413 * \retval const TCollection_AsciiString & - Object substring
1415 //================================================================================
1417 const TCollection_AsciiString & _pyCommand::GetObject()
1419 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1422 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1424 begPos = myString.Location( "=", 1, Length() ) + 1;
1426 myObj = GetWord( myString, begPos, true );
1427 SetBegPos( OBJECT_IND, begPos );
1433 //================================================================================
1435 * \brief Return substring of python command looking like ResVal = Obj.Method()
1436 * \retval const TCollection_AsciiString & - Method substring
1438 //================================================================================
1440 const TCollection_AsciiString & _pyCommand::GetMethod()
1442 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1445 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1446 bool forward = true;
1448 begPos = myString.Location( "(", 1, Length() ) - 1;
1452 myMeth = GetWord( myString, begPos, forward );
1453 SetBegPos( METHOD_IND, begPos );
1459 //================================================================================
1461 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1462 * \retval const TCollection_AsciiString & - Arg<index> substring
1464 //================================================================================
1466 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1468 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1471 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1473 begPos = myString.Location( "(", 1, Length() ) + 1;
1475 int i = 0, prevLen = 0;
1476 while ( begPos != EMPTY ) {
1478 // check if we are looking at the closing parenthesis
1479 while ( begPos <= Length() && isspace( myString.Value( begPos )))
1481 if ( begPos > Length() || myString.Value( begPos ) == ')' )
1483 myArgs.Append( GetWord( myString, begPos, true, true ));
1484 SetBegPos( ARG1_IND + i, begPos );
1485 prevLen = myArgs.Last().Length();
1487 myArgs.Remove( myArgs.Length() ); // no more args
1491 if ( myArgs.Length() < index )
1492 return theEmptyString;
1493 return myArgs( index );
1496 //================================================================================
1498 * \brief Check if char is a word part
1499 * \param c - The character to check
1500 * \retval bool - The check result
1502 //================================================================================
1504 static inline bool isWord(const char c, const bool dotIsWord)
1507 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1510 //================================================================================
1512 * \brief Looks for a word in the string and returns word's beginning
1513 * \param theString - The input string
1514 * \param theStartPos - The position to start the search, returning word's beginning
1515 * \param theForward - The search direction
1516 * \retval TCollection_AsciiString - The found word
1518 //================================================================================
1520 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1522 const bool theForward,
1523 const bool dotIsWord )
1525 int beg = theStartPos, end = theStartPos;
1526 theStartPos = EMPTY;
1527 if ( beg < 1 || beg > theString.Length() )
1528 return theEmptyString;
1530 if ( theForward ) { // search forward
1532 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1534 if ( beg > theString.Length() )
1535 return theEmptyString; // no word found
1538 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1542 else { // search backward
1544 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1547 return theEmptyString; // no word found
1549 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1554 //cout << theString << " ---- " << beg << " - " << end << endl;
1555 return theString.SubString( beg, end );
1558 //================================================================================
1560 * \brief Look for position where not space char is
1561 * \param theString - The string
1562 * \param thePos - The position to search from and which returns result
1563 * \retval bool - false if there are only space after thePos in theString
1567 //================================================================================
1569 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1571 if ( thePos < 1 || thePos > theString.Length() )
1574 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1577 return thePos <= theString.Length();
1580 //================================================================================
1582 * \brief Modify a part of the command
1583 * \param thePartIndex - The index of the part
1584 * \param thePart - The new part string
1585 * \param theOldPart - The old part
1587 //================================================================================
1589 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1590 TCollection_AsciiString& theOldPart)
1592 int pos = GetBegPos( thePartIndex );
1593 if ( pos <= Length() && theOldPart != thePart)
1595 TCollection_AsciiString seperator;
1597 pos = GetBegPos( thePartIndex + 1 );
1598 if ( pos < 1 ) return;
1599 switch ( thePartIndex ) {
1600 case RESULT_IND: seperator = " = "; break;
1601 case OBJECT_IND: seperator = "."; break;
1602 case METHOD_IND: seperator = "()"; break;
1606 myString.Remove( pos, theOldPart.Length() );
1607 if ( !seperator.IsEmpty() )
1608 myString.Insert( pos , seperator );
1609 myString.Insert( pos, thePart );
1610 // update starting positions of the following parts
1611 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1612 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1613 if ( myBegPos( i ) > 0 )
1614 myBegPos( i ) += posDelta;
1616 theOldPart = thePart;
1620 //================================================================================
1622 * \brief Set agrument
1623 * \param index - The argument index, it counts from 1
1624 * \param theArg - The argument string
1626 //================================================================================
1628 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1631 int argInd = ARG1_IND + index - 1;
1632 int pos = GetBegPos( argInd );
1633 if ( pos < 1 ) // no index-th arg exist, append inexistent args
1635 // find a closing parenthesis
1636 if ( int lastArgInd = GetNbArgs() ) {
1637 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
1638 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
1643 while ( pos > 0 && myString.Value( pos ) != ')' )
1646 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
1650 while ( myArgs.Length() < index ) {
1651 if ( myArgs.Length() )
1652 myString.Insert( pos++, "," );
1653 myArgs.Append("None");
1654 myString.Insert( pos, myArgs.Last() );
1655 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1656 pos += myArgs.Last().Length();
1659 SetPart( argInd, theArg, myArgs( index ));
1662 //================================================================================
1664 * \brief Empty arg list
1666 //================================================================================
1668 void _pyCommand::RemoveArgs()
1670 if ( int pos = myString.Location( '(', 1, Length() ))
1671 myString.Trunc( pos );
1674 if ( myBegPos.Length() >= ARG1_IND )
1675 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1678 //================================================================================
1680 * \brief Set dependent commands after this one
1682 //================================================================================
1684 bool _pyCommand::SetDependentCmdsAfter() const
1686 bool orderChanged = false;
1687 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1688 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1689 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1690 orderChanged = true;
1691 theGen->SetCommandAfter( *cmd, this );
1692 (*cmd)->SetDependentCmdsAfter();
1695 return orderChanged;
1697 //================================================================================
1699 * \brief Insert accessor method after theObjectID
1700 * \param theObjectID - id of the accessed object
1701 * \param theAcsMethod - name of the method giving access to the object
1702 * \retval bool - false if theObjectID is not found in the command string
1704 //================================================================================
1706 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
1708 if ( !theAcsMethod )
1710 // start object search from the object, i.e. ignore result
1712 int beg = GetBegPos( OBJECT_IND );
1713 if ( beg < 1 || beg > Length() )
1715 while (( beg = myString.Location( theObjectID, beg, Length() )))
1717 // check that theObjectID is not just a part of a longer ID
1718 int afterEnd = beg + theObjectID.Length();
1719 Standard_Character c = myString.Value( afterEnd );
1720 if ( !isalnum( c ) && c != ':' ) {
1721 // check if accessor method already present
1723 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
1725 int oldLen = Length();
1726 myString.Insert( afterEnd, (char*) theAcsMethod );
1727 myString.Insert( afterEnd, "." );
1728 // update starting positions of the parts following the modified one
1729 int posDelta = Length() - oldLen;
1730 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
1731 if ( myBegPos( i ) > afterEnd )
1732 myBegPos( i ) += posDelta;
1737 beg = afterEnd; // is a part - next search
1742 //================================================================================
1744 * \brief Return method name giving access to an interaface object wrapped by python class
1745 * \retval const char* - method name
1747 //================================================================================
1749 const char* _pyObject::AccessorMethod() const