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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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);
59 using SMESH::TPythonDump;
62 * \brief Container of commands into which the initial script is split.
63 * It also contains data coresponding to SMESH_Gen contents
65 static Handle(_pyGen) theGen;
67 static TCollection_AsciiString theEmptyString;
69 //#define DUMP_CONVERSION
71 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
72 #undef DUMP_CONVERSION
75 //================================================================================
77 * \brief Convert python script using commands of smesh.py
78 * \param theScript - Input script
79 * \retval TCollection_AsciiString - Convertion result
81 //================================================================================
83 TCollection_AsciiString
84 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
85 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
87 theGen = new _pyGen( theEntry2AccessorMethod );
89 // split theScript into separate commands
90 int from = 1, end = theScript.Length(), to;
91 while ( from < end && ( to = theScript.Location( "\n", from, end )))
94 // cut out and store a command
95 theGen->AddCommand( theScript.SubString( from, to - 1 ));
100 #ifdef DUMP_CONVERSION
101 cout << endl << " ######## RESULT ######## " << endl<< endl;
103 // reorder commands after conversion
104 list< Handle(_pyCommand) >::iterator cmd;
107 orderChanges = false;
108 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
109 if ( (*cmd)->SetDependentCmdsAfter() )
111 } while ( orderChanges );
113 // concat commands back into a script
114 TCollection_AsciiString aScript;
115 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
117 #ifdef DUMP_CONVERSION
118 cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
120 if ( !(*cmd)->IsEmpty() ) {
122 aScript += (*cmd)->GetString();
132 //================================================================================
134 * \brief _pyGen constructor
136 //================================================================================
138 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
139 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
140 myID2AccessorMethod( theEntry2AccessorMethod )
143 // make that GetID() to return TPythonDump::SMESHGenName()
144 GetCreationCmd()->GetString() += "=";
147 //================================================================================
149 * \brief Convert a command using a specific converter
150 * \param theCommand - the command to convert
151 * \retval bool - convertion result
153 //================================================================================
155 void _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
157 // store theCommand in the sequence
158 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
160 Handle(_pyCommand) aCommand = myCommands.back();
161 #ifdef DUMP_CONVERSION
162 cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
165 _pyID objID = aCommand->GetObject();
167 if ( objID.IsEmpty() )
171 if ( objID == this->GetID() ) {
172 this->Process( aCommand );
175 // SMESH_Mesh method?
176 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
177 if ( id_mesh != myMeshes.end() ) {
178 id_mesh->second->Process( aCommand );
181 // SMESH_Hypothesis method
182 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
183 for ( ; hyp != myHypos.end(); ++hyp )
184 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() )
185 (*hyp)->Process( aCommand );
187 // Mesh provides SMESH_IDSource interface used in SMESH_MeshEditor.
188 // Add access to wrapped mesh
189 if ( objID == TPythonDump::MeshEditorName() ) {
190 // in all SMESH_MeshEditor's commands, a SMESH_IDSource is the first arg
191 id_mesh = myMeshes.find( aCommand->GetArg( 1 ));
192 if ( id_mesh != myMeshes.end() )
193 aCommand->SetArg( 1 , aCommand->GetArg( 1 ) + ".GetMesh()" );
197 //================================================================================
199 * \brief Convert the command or remember it for later conversion
200 * \param theCommand - The python command calling a method of SMESH_Gen
202 //================================================================================
204 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
206 // there are methods to convert:
207 // CreateMesh( shape )
208 // CreateHypothesis( theHypType, theLibName )
209 // Compute( mesh, geom )
211 if ( theCommand->GetMethod() == "CreateMesh" )
213 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
214 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
218 // CreateHypothesis()
219 if ( theCommand->GetMethod() == "CreateHypothesis" )
221 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
225 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
226 if ( theCommand->GetMethod() == "Compute" )
228 const _pyID& meshID = theCommand->GetArg( 1 );
229 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
230 if ( id_mesh != myMeshes.end() ) {
231 theCommand->SetObject( meshID );
232 theCommand->RemoveArgs();
233 id_mesh->second->Flush();
238 // smeshgen.Method() --> smesh.smesh.Method()
239 theCommand->SetObject( SMESH_2smeshpy::GenName() );
242 //================================================================================
244 * \brief Convert the remembered commands
246 //================================================================================
250 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
251 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
252 if ( ! id_mesh->second.IsNull() )
253 id_mesh->second->Flush();
255 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
256 for ( ; hyp != myHypos.end(); ++hyp )
257 if ( !hyp->IsNull() ) {
259 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
260 if ( !(*hyp)->IsWrapped() )
261 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
265 //================================================================================
267 * \brief Find hypothesis by ID (entry)
268 * \param theHypID - The hypothesis ID
269 * \retval Handle(_pyHypothesis) - The found hypothesis
271 //================================================================================
273 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
275 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
276 for ( ; hyp != myHypos.end(); ++hyp )
277 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
279 return Handle(_pyHypothesis)();
282 //================================================================================
284 * \brief Find algorithm the created algorithm
285 * \param theGeom - The shape ID the algorithm was created on
286 * \param theMesh - The mesh ID that created the algorithm
287 * \param dim - The algo dimension
288 * \retval Handle(_pyHypothesis) - The found algo
290 //================================================================================
292 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
293 const TCollection_AsciiString& theAlgoType )
295 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
296 for ( ; hyp != myHypos.end(); ++hyp )
297 if ( !hyp->IsNull() &&
299 (*hyp)->GetType() == theAlgoType &&
300 (*hyp)->GetGeom() == theGeom &&
301 (*hyp)->GetMesh() == theMesh )
306 //================================================================================
308 * \brief Change order of commands in the script
309 * \param theCmd1 - One command
310 * \param theCmd2 - Another command
312 //================================================================================
314 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
316 list< Handle(_pyCommand) >::iterator pos1, pos2;
317 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
318 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
319 myCommands.insert( pos1, theCmd2 );
320 myCommands.insert( pos2, theCmd1 );
321 myCommands.erase( pos1 );
322 myCommands.erase( pos2 );
324 int nb1 = theCmd1->GetOrderNb();
325 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
326 theCmd2->SetOrderNb( nb1 );
327 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
328 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
331 //================================================================================
333 * \brief Set one command after the other
334 * \param theCmd - Command to move
335 * \param theAfterCmd - Command ater which to insert the first one
337 //================================================================================
339 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
341 // cout << "SET\t" << theCmd->GetString() << endl << "AFTER\t" << theAfterCmd->GetString() << endl << endl;
342 list< Handle(_pyCommand) >::iterator pos;
343 pos = find( myCommands.begin(), myCommands.end(), theCmd );
344 myCommands.erase( pos );
345 pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
346 myCommands.insert( ++pos, theCmd );
349 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
350 (*pos)->SetOrderNb( i++ );
353 //================================================================================
355 * \brief Set method to access to object wrapped with python class
356 * \param theID - The wrapped object entry
357 * \param theMethod - The accessor method
359 //================================================================================
361 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
363 myID2AccessorMethod.Bind( theID, (char*) theMethod );
366 //================================================================================
368 * \brief Find out type of geom group
369 * \param grpID - The geom group entry
370 * \retval int - The type
372 //================================================================================
374 static bool sameGroupType( const _pyID& grpID,
375 const TCollection_AsciiString& theType)
377 // define group type as smesh.Mesh.Group() does
379 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
380 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
381 if ( !aSObj->_is_nil() ) {
382 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
383 if ( !aGeomObj->_is_nil() ) {
384 switch ( aGeomObj->GetShapeType() ) {
385 case GEOM::VERTEX: type = SMESH::NODE; break;
386 case GEOM::EDGE: type = SMESH::EDGE; break;
387 case GEOM::FACE: type = SMESH::FACE; break;
389 case GEOM::SHELL: type = SMESH::VOLUME; break;
390 case GEOM::COMPOUND: {
391 GEOM::GEOM_Gen_var aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
392 if ( !aGeomGen->_is_nil() ) {
393 GEOM::GEOM_IGroupOperations_var aGrpOp =
394 aGeomGen->GetIGroupOperations( study->StudyId() );
395 if ( !aGrpOp->_is_nil() ) {
396 switch ( aGrpOp->GetType( aGeomObj )) {
397 case TopAbs_VERTEX: type = SMESH::NODE; break;
398 case TopAbs_EDGE: type = SMESH::EDGE; break;
399 case TopAbs_FACE: type = SMESH::FACE; break;
400 case TopAbs_SOLID: type = SMESH::VOLUME; break;
411 MESSAGE("Type of the group " << grpID << " not found");
414 if ( theType.IsIntegerValue() )
415 return type == theType.IntegerValue();
418 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
419 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
420 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
421 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
427 //================================================================================
430 * \param theCreationCmd -
432 //================================================================================
434 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd): _pyObject(theCreationCmd)
436 // convert my creation command
437 Handle(_pyCommand) creationCmd = GetCreationCmd();
438 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
439 creationCmd->SetMethod( "Mesh" );
441 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
444 //================================================================================
446 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
447 * \param theCommand - Engine method called for this mesh
449 //================================================================================
451 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
453 // smesh.py wraps the following methods:
455 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
456 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
457 // 2. AddHypothesis(geom, hyp)
458 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
459 // 3. CreateGroupFromGEOM(type, name, grp)
460 // --> in Mesh.Group(grp, name="")
461 // 4. ExportToMED(f, opt, version)
462 // --> in Mesh.ExportToMED( f, version, opt=0 )
463 // 5. ExportMED(f, opt)
464 // --> in Mesh.ExportMED( f,opt=0 )
466 // --> in Mesh.ExportDAT( f )
468 // --> in Mesh.ExportUNV(f)
469 // 8. ExportSTL(f, ascii)
470 // --> in Mesh.ExportSTL(f, ascii=1)
472 const TCollection_AsciiString method = theCommand->GetMethod();
473 if ( method == "GetSubMesh" ) {
474 mySubmeshes.push_back( theCommand );
476 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
477 myAddHypCmds.push_back( theCommand );
479 const _pyID& hypID = theCommand->GetArg( 2 );
480 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
481 if ( !hyp.IsNull() && hyp->GetMesh().IsEmpty() )
482 hyp->SetMesh( this->GetID() );
484 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
485 _pyID grp = theCommand->GetArg( 3 );
486 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
487 theCommand->SetMethod( "Group" );
488 theCommand->RemoveArgs();
489 theCommand->SetArg( 1, grp );
492 AddMeshAccess( theCommand );
495 else if ( method == "ExportToMED" ) {//(f, opt, version)
496 // --> (f, version, opt)
497 _pyID opt = theCommand->GetArg( 2 );
498 _pyID ver = theCommand->GetArg( 3 );
499 theCommand->SetArg( 2, ver );
500 theCommand->SetArg( 3, opt );
502 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
504 const _pyID & hypID = theCommand->GetArg( 2 );
506 // check if this mesh still has corresponding addition command
507 bool hasAddCmd = false;
508 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
509 while ( cmd != myAddHypCmds.end() )
511 // AddHypothesis(geom, hyp)
512 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both commands
515 cmd = myAddHypCmds.erase( cmd );
523 // access to wrapped mesh
524 AddMeshAccess( theCommand );
525 // access to wrapped algo
526 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
527 if ( !hyp.IsNull() && hyp->IsAlgo() && hyp->IsWrapped() )
528 theCommand->SetArg( 2, theCommand->GetArg( 2 ) + ".GetAlgorithm()" );
531 else { // apply theCommand to the mesh wrapped by smeshpy mesh
532 AddMeshAccess( theCommand );
536 //================================================================================
538 * \brief Convert creation and addition of all algos and hypos
540 //================================================================================
542 void _pyMesh::Flush()
544 list < Handle(_pyCommand) >::iterator cmd, cmd2;
546 // try to convert algo addition like this:
547 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
548 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
550 Handle(_pyCommand) addCmd = *cmd;
551 const _pyID& algoID = addCmd->GetArg( 2 );
552 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
553 if ( algo.IsNull() || !algo->IsAlgo() )
556 _pyID geom = addCmd->GetArg( 1 );
557 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
559 // wrapped algo is created atfer mesh creation
560 GetCreationCmd()->AddDependantCmd( addCmd );
562 if ( geom != GetGeom() ) // local algo
564 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
565 addCmd->SetArg( addCmd->GetNbArgs() + 1,
566 TCollection_AsciiString( "geom=" ) + geom );
567 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
568 for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
569 Handle(_pyCommand) subCmd = *cmd2;
570 if ( geom == subCmd->GetArg( 1 )) {
571 subCmd->SetObject( algo->GetID() );
572 subCmd->RemoveArgs();
573 addCmd->AddDependantCmd( subCmd );
578 else // ALGO was already created
580 // mesh.AddHypothesis(geom, ALGO ) --> mesh.GetMesh().AddHypothesis(geom, ALGO )
581 AddMeshAccess( addCmd );
582 // mesh.GetMesh().AddHypothesis(geom, ALGO ) ->
583 // mesh.GetMesh().AddHypothesis(geom, ALGO.GetAlgorithm() )
584 addCmd->SetArg( 2, addCmd->GetArg( 2 ) + ".GetAlgorithm()" );
588 // try to convert hypo addition like this:
589 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
590 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
592 Handle(_pyCommand) addCmd = *cmd;
593 const _pyID& hypID = addCmd->GetArg( 2 );
594 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
595 if ( hyp.IsNull() || hyp->IsAlgo() )
597 const _pyID& geom = addCmd->GetArg( 1 );
598 // find algo created on <geom> for this mesh
599 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, this->GetID(), hyp->GetType() );
600 //_pyID algoID = algo.IsNull() ? "" : algo->GetID();
601 if ( !algo.IsNull() && hyp->Addition2Creation( addCmd, this->GetID() )) // OK
603 addCmd->SetObject( algo->GetID() );
604 algo->GetCreationCmd()->AddDependantCmd( addCmd );
608 AddMeshAccess( addCmd );
612 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
613 for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
614 Handle(_pyCommand) subCmd = *cmd;
615 if ( subCmd->GetNbArgs() > 0 )
616 AddMeshAccess( subCmd );
618 myAddHypCmds.clear();
622 //================================================================================
624 * \brief _pyHypothesis constructor
625 * \param theCreationCmd -
627 //================================================================================
629 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
630 _pyObject( theCreationCmd )
632 myDim = myIsAlgo = /*myIsLocal = */myIsWrapped = myIsConverted = false;
635 //================================================================================
637 * \brief Creates algorithm or hypothesis
638 * \param theCreationCmd - The engine command creating a hypothesis
639 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
641 //================================================================================
643 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
645 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
646 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
648 Handle(_pyHypothesis) hyp, algo;
651 const TCollection_AsciiString & hypTypeWithQuotes = theCreationCmd->GetArg( 1 );
652 if ( hypTypeWithQuotes.IsEmpty() )
655 TCollection_AsciiString hypType =
656 hypTypeWithQuotes.SubString( 2, hypTypeWithQuotes.Length() - 1 );
658 algo = new _pyAlgorithm( theCreationCmd );
659 hyp = new _pyHypothesis( theCreationCmd );
661 // 1D Regular_1D ----------
662 if ( hypType == "Regular_1D" ) {
664 algo->myCreationMethod = "Segment";
666 else if ( hypType == "LocalLength" ) {
668 hyp->myCreationMethod = "LocalLength";
669 hyp->myType = "Regular_1D";
670 hyp->myArgMethods.Append( "SetLength" );
672 else if ( hypType == "NumberOfSegments" ) {
673 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
675 hyp->myCreationMethod = "NumberOfSegments";
676 hyp->myType = "Regular_1D";
677 hyp->myArgMethods.Append( "SetNumberOfSegments" );
678 hyp->myArgMethods.Append( "SetScaleFactor" );
680 else if ( hypType == "Arithmetic1D" ) {
681 hyp = new _pyComplexParamHypo( theCreationCmd );
683 hyp->myCreationMethod = "Arithmetic1D";
684 hyp->myType = "Regular_1D";
686 else if ( hypType == "StartEndLength" ) {
687 hyp = new _pyComplexParamHypo( theCreationCmd );
689 hyp->myCreationMethod = "StartEndLength";
690 hyp->myType = "Regular_1D";
692 else if ( hypType == "Deflection1D" ) {
694 hyp->myCreationMethod = "Deflection1D";
695 hyp->myArgMethods.Append( "SetDeflection" );
696 hyp->myType = "Regular_1D";
698 else if ( hypType == "Propagation" ) {
700 hyp->myCreationMethod = "Propagation";
701 hyp->myType = "Regular_1D";
703 else if ( hypType == "QuadraticMesh" ) {
705 hyp->myCreationMethod = "QuadraticMesh";
706 hyp->myType = "Regular_1D";
708 else if ( hypType == "AutomaticLength" ) {
710 hyp->myCreationMethod = "AutomaticLength";
711 hyp->myType = "Regular_1D";
712 hyp->myArgMethods.Append( "SetFineness");
714 // 1D Python_1D ----------
715 else if ( hypType == "Python_1D" ) {
717 algo->myCreationMethod = "Segment";
718 algo->myArgs.Append( "algo=smesh.PYTHON");
720 else if ( hypType == "PythonSplit1D" ) {
722 hyp->myCreationMethod = "PythonSplit1D";
723 hyp->myType = "Python_1D";
724 hyp->myArgMethods.Append( "SetNumberOfSegments");
725 hyp->myArgMethods.Append( "SetPythonLog10RatioFunction");
728 else if ( hypType == "MEFISTO_2D" ) {
730 algo->myCreationMethod = "Triangle";
732 else if ( hypType == "MaxElementArea" ) {
734 hyp->myCreationMethod = "MaxElementArea";
735 hyp->myType = "MEFISTO_2D";
736 hyp->myArgMethods.Append( "SetMaxElementArea");
738 else if ( hypType == "LengthFromEdges" ) {
740 hyp->myCreationMethod = "LengthFromEdges";
741 hyp->myType = "MEFISTO_2D";
743 else if ( hypType == "Quadrangle_2D" ) {
745 algo->myCreationMethod = "Quadrangle";
747 else if ( hypType == "QuadranglePreference" ) {
749 hyp->myCreationMethod = "QuadranglePreference";
750 hyp->myType = "Quadrangle_2D";
753 else if ( hypType == "NETGEN_3D") {
755 algo->myCreationMethod = "Tetrahedron";
756 algo->myArgs.Append( "algo=smesh.NETGEN" );
758 else if ( hypType == "MaxElementVolume") {
760 hyp->myCreationMethod = "MaxElementVolume";
761 hyp->myType = "NETGEN_3D";
762 hyp->myArgMethods.Append( "SetMaxElementVolume" );
764 else if ( hypType == "GHS3D_3D" ) {
766 algo->myCreationMethod = "Tetrahedron";
767 algo->myArgs.Append( "algo=smesh.GHS3D" );
769 else if ( hypType == "Hexa_3D" ) {
771 algo->myCreationMethod = "Hexahedron";
774 if ( algo->GetDim() ) {
775 algo->myType = hypType;
781 //================================================================================
783 * \brief Convert the command adding a hypothesis to mesh into a smesh command
784 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
785 * \param theAlgo - The algo that can create this hypo
786 * \retval bool - false if the command cant be converted
788 //================================================================================
790 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
791 const _pyID& theMesh)
793 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
795 if ( !IsWrappable( theMesh ))
802 // mesh.AddHypothesis(geom,hyp) --> hyp = theMesh.myCreationMethod(args)
803 theCmd->SetResultValue( GetID() );
804 theCmd->SetObject( theMesh );
805 theCmd->SetMethod( myCreationMethod );
807 theCmd->RemoveArgs();
808 for ( int i = 1; i <= myArgs.Length(); ++i ) {
809 if ( !myArgs( i ).IsEmpty() )
810 theCmd->SetArg( i, myArgs( i ));
812 theCmd->SetArg( i, "[]");
814 // set a new creation command
815 GetCreationCmd()->Clear();
816 SetCreationCmd( theCmd );
818 // clear commands setting arg values
819 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
820 for ( ; argCmd != myArgCommands.end(); ++argCmd )
825 // // set arg commands after hypo creation
826 // list<Handle(_pyCommand)>::iterator argCmd = myArgCommands.begin();
827 // for ( ; argCmd != myArgCommands.end(); ++argCmd )
828 // if ( !(*argCmd)->IsEmpty() && GetCommandNb() > (*argCmd)->GetOrderNb() )
829 // theGen->ExchangeCommands( GetCreationCmd(), *argCmd );
832 // set unknown arg commands after hypo creation
833 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
834 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
835 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
836 afterCmd->AddDependantCmd( *cmd );
838 myArgCommands.clear();
839 myUnknownCommands.clear();
844 //================================================================================
846 * \brief Remember hypothesis parameter values
847 * \param theCommand - The called hypothesis method
849 //================================================================================
851 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
855 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
856 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
857 while ( myArgs.Length() < i )
858 myArgs.Append( "[]" );
859 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
860 myArgCommands.push_back( theCommand );
864 myUnknownCommands.push_back( theCommand );
867 //================================================================================
869 * \brief Finish conversion
871 //================================================================================
873 void _pyHypothesis::Flush()
875 // if ( IsWrapped() )
876 // GetCreationCmd()->Clear();
879 //================================================================================
881 * \brief Remember hypothesis parameter values
882 * \param theCommand - The called hypothesis method
884 //================================================================================
886 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
888 // ex: hyp.SetLength(start, 1)
889 // hyp.SetLength(end, 0)
890 ASSERT(( theCommand->GetMethod() == "SetLength" ));
891 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
892 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
893 while ( myArgs.Length() < i )
894 myArgs.Append( "[]" );
895 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
896 myArgCommands.push_back( theCommand );
899 //================================================================================
901 * \brief additionally to Addition2Creation, clears SetDistrType() command
902 * \param theCmd - AddHypothesis() command
903 * \param theMesh - mesh to which a hypothesis is added
904 * \retval bool - convertion result
906 //================================================================================
908 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
909 const _pyID& theMesh)
911 if ( IsWrappable( theMesh ) && myArgs.Length() > 0 ) {
912 list<Handle(_pyCommand)> aNewCommandsList;
913 list<TCollection_AsciiString> aNewList;
914 list<TCollection_AsciiString>::iterator aNewListIter;
916 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
917 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
918 aNewListIter = find(aNewList.begin(),aNewList.end(),(*cmd)->GetMethod());
919 if(aNewListIter == aNewList.end()){
920 aNewList.push_front((*cmd)->GetMethod());
921 aNewCommandsList.push_front((*cmd));
926 myUnknownCommands = aNewCommandsList;
928 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
931 //================================================================================
933 * \brief _pyAlgorithm constructor
934 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
936 //================================================================================
938 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
939 : _pyHypothesis( theCreationCmd )
944 //================================================================================
946 * \brief Convert the command adding an algorithm to mesh
947 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
948 * \param theMesh - The mesh needing this algo
949 * \retval bool - false if the command cant be converted
951 //================================================================================
953 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
954 const _pyID& theMeshID)
956 if ( IsWrappable( theMeshID )) {
958 myGeom = theCmd->GetArg( 1 );
960 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
961 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
962 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
969 //================================================================================
971 * \brief Return starting position of a part of python command
972 * \param thePartIndex - The index of command part
973 * \retval int - Part position
975 //================================================================================
977 int _pyCommand::GetBegPos( int thePartIndex )
981 if ( myBegPos.Length() < thePartIndex )
983 return myBegPos( thePartIndex );
986 //================================================================================
988 * \brief Store starting position of a part of python command
989 * \param thePartIndex - The index of command part
990 * \param thePosition - Part position
992 //================================================================================
994 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
996 while ( myBegPos.Length() < thePartIndex )
997 myBegPos.Append( UNKNOWN );
998 myBegPos( thePartIndex ) = thePosition;
1001 //================================================================================
1003 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1004 * \retval const TCollection_AsciiString & - ResultValue substring
1006 //================================================================================
1008 const TCollection_AsciiString & _pyCommand::GetResultValue()
1010 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1012 int begPos = myString.Location( "=", 1, Length() );
1014 myRes = GetWord( myString, begPos, false );
1017 SetBegPos( RESULT_IND, begPos );
1022 //================================================================================
1024 * \brief Return substring of python command looking like ResVal = Object.Meth()
1025 * \retval const TCollection_AsciiString & - Object substring
1027 //================================================================================
1029 const TCollection_AsciiString & _pyCommand::GetObject()
1031 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1034 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1036 begPos = myString.Location( "=", 1, Length() ) + 1;
1038 myObj = GetWord( myString, begPos, true );
1039 SetBegPos( OBJECT_IND, begPos );
1045 //================================================================================
1047 * \brief Return substring of python command looking like ResVal = Obj.Method()
1048 * \retval const TCollection_AsciiString & - Method substring
1050 //================================================================================
1052 const TCollection_AsciiString & _pyCommand::GetMethod()
1054 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1057 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1058 bool forward = true;
1060 begPos = myString.Location( "(", 1, Length() ) - 1;
1064 myMeth = GetWord( myString, begPos, forward );
1065 SetBegPos( METHOD_IND, begPos );
1071 //================================================================================
1073 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1074 * \retval const TCollection_AsciiString & - Arg<index> substring
1076 //================================================================================
1078 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1080 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1083 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1085 begPos = myString.Location( "(", 1, Length() ) + 1;
1087 int i = 0, prevLen = 0;
1088 while ( begPos != EMPTY ) {
1090 // check if we are looking at the closing parenthesis
1091 while ( begPos <= Length() && isspace( myString.Value( begPos )))
1093 if ( begPos > Length() || myString.Value( begPos ) == ')' )
1095 myArgs.Append( GetWord( myString, begPos, true, true ));
1096 SetBegPos( ARG1_IND + i, begPos );
1097 prevLen = myArgs.Last().Length();
1099 myArgs.Remove( myArgs.Length() ); // no more args
1103 if ( myArgs.Length() < index )
1104 return theEmptyString;
1105 return myArgs( index );
1108 //================================================================================
1110 * \brief Check if char is a word part
1111 * \param c - The character to check
1112 * \retval bool - The check result
1114 //================================================================================
1116 static inline bool isWord(const char c, const bool dotIsWord)
1119 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1122 //================================================================================
1124 * \brief Looks for a word in the string and returns word's beginning
1125 * \param theString - The input string
1126 * \param theStartPos - The position to start the search, returning word's beginning
1127 * \param theForward - The search direction
1128 * \retval TCollection_AsciiString - The found word
1130 //================================================================================
1132 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1134 const bool theForward,
1135 const bool dotIsWord )
1137 int beg = theStartPos, end = theStartPos;
1138 theStartPos = EMPTY;
1139 if ( beg < 1 || beg > theString.Length() )
1140 return theEmptyString;
1142 if ( theForward ) { // search forward
1144 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1146 if ( beg > theString.Length() )
1147 return theEmptyString; // no word found
1150 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1154 else { // search backward
1156 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1159 return theEmptyString; // no word found
1161 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1166 //cout << theString << " ---- " << beg << " - " << end << endl;
1167 return theString.SubString( beg, end );
1170 //================================================================================
1172 * \brief Look for position where not space char is
1173 * \param theString - The string
1174 * \param thePos - The position to search from and which returns result
1175 * \retval bool - false if there are only space after thePos in theString
1179 //================================================================================
1181 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1183 if ( thePos < 1 || thePos > theString.Length() )
1186 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1189 return thePos <= theString.Length();
1192 //================================================================================
1194 * \brief Modify a part of the command
1195 * \param thePartIndex - The index of the part
1196 * \param thePart - The new part string
1197 * \param theOldPart - The old part
1199 //================================================================================
1201 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1202 TCollection_AsciiString& theOldPart)
1204 int pos = GetBegPos( thePartIndex );
1205 if ( pos <= Length() && theOldPart != thePart)
1207 TCollection_AsciiString seperator;
1209 pos = GetBegPos( thePartIndex + 1 );
1210 if ( pos < 1 ) return;
1211 switch ( thePartIndex ) {
1212 case RESULT_IND: seperator = " = "; break;
1213 case OBJECT_IND: seperator = "."; break;
1214 case METHOD_IND: seperator = "()"; break;
1218 myString.Remove( pos, theOldPart.Length() );
1219 if ( !seperator.IsEmpty() )
1220 myString.Insert( pos , seperator );
1221 myString.Insert( pos, thePart );
1222 // update starting positions of the following parts
1223 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1224 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1225 if ( myBegPos( i ) > 0 )
1226 myBegPos( i ) += posDelta;
1228 theOldPart = thePart;
1232 //================================================================================
1234 * \brief Set agrument
1235 * \param index - The argument index, it counts from 1
1236 * \param theArg - The argument string
1238 //================================================================================
1240 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1243 int argInd = ARG1_IND + index - 1;
1244 int pos = GetBegPos( argInd );
1245 if ( pos < 1 ) // no index-th arg exist, append inexistent args
1247 // find a closing parenthesis
1249 while ( pos > 0 && myString.Value( pos ) != ')' )
1251 if ( pos == 0 ) { // no parentheses at all
1255 while ( myArgs.Length() < index ) {
1256 if ( myArgs.Length() )
1257 myString.Insert( pos++, "," );
1258 myArgs.Append("None");
1259 myString.Insert( pos, myArgs.Last() );
1260 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1261 pos += myArgs.Last().Length();
1264 SetPart( argInd, theArg, myArgs( index ));
1267 //================================================================================
1269 * \brief Empty arg list
1271 //================================================================================
1273 void _pyCommand::RemoveArgs()
1275 if ( int pos = myString.Location( '(', 1, Length() ))
1276 myString.Trunc( pos );
1279 if ( myBegPos.Length() >= ARG1_IND )
1280 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1283 //================================================================================
1285 * \brief Set dependent commands after this one
1287 //================================================================================
1289 bool _pyCommand::SetDependentCmdsAfter() const
1291 bool orderChanged = false;
1292 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1293 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1294 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1295 orderChanged = true;
1296 theGen->SetCommandAfter( *cmd, this );
1297 (*cmd)->SetDependentCmdsAfter();
1300 return orderChanged;