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 myHasPattern = false;
144 // make that GetID() to return TPythonDump::SMESHGenName()
145 GetCreationCmd()->GetString() += "=";
148 //================================================================================
150 * \brief Convert a command using a specific converter
151 * \param theCommand - the command to convert
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 );
189 // Add access to a wrapped mesh
190 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh ) {
191 if ( aCommand->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
195 // Add access to a wrapped algorithm
196 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp ) {
197 if ( (*hyp)->IsAlgo() &&
198 aCommand->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
202 // PAL12227. PythonDump was not updated at proper time; result is
203 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
204 // TypeError: __init__() takes exactly 11 arguments (10 given)
205 char wrongCommand[] = "SMESH.Filter.Criterion(";
206 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
208 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
209 // there must be 10 arguments, 5-th arg ThresholdID is missing,
210 const int wrongNbArgs = 9, missingArg = 5;
211 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
213 for ( int i = wrongNbArgs; i > missingArg; --i )
214 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
215 tmpCmd.SetArg( missingArg, "''");
216 aCommand->GetString().Trunc( beg - 1 );
217 aCommand->GetString() += tmpCmd.GetString();
222 //================================================================================
224 * \brief Convert the command or remember it for later conversion
225 * \param theCommand - The python command calling a method of SMESH_Gen
227 //================================================================================
229 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
231 // there are methods to convert:
232 // CreateMesh( shape )
233 // CreateHypothesis( theHypType, theLibName )
234 // Compute( mesh, geom )
236 if ( theCommand->GetMethod() == "CreateMesh" )
238 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
239 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
243 // CreateHypothesis()
244 if ( theCommand->GetMethod() == "CreateHypothesis" )
246 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
250 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
251 if ( theCommand->GetMethod() == "Compute" )
253 const _pyID& meshID = theCommand->GetArg( 1 );
254 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
255 if ( id_mesh != myMeshes.end() ) {
256 theCommand->SetObject( meshID );
257 theCommand->RemoveArgs();
258 id_mesh->second->Flush();
263 // leave only one smeshgen.GetPattern() in the script
264 if ( theCommand->GetMethod() == "GetPattern" ) {
265 if ( myHasPattern ) {
272 // smeshgen.Method() --> smesh.smesh.Method()
273 theCommand->SetObject( SMESH_2smeshpy::GenName() );
276 //================================================================================
278 * \brief Convert the remembered commands
280 //================================================================================
284 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
285 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
286 if ( ! id_mesh->second.IsNull() )
287 id_mesh->second->Flush();
289 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
290 for ( ; hyp != myHypos.end(); ++hyp )
291 if ( !hyp->IsNull() ) {
293 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
294 if ( !(*hyp)->IsWrapped() )
295 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
299 //================================================================================
301 * \brief Find hypothesis by ID (entry)
302 * \param theHypID - The hypothesis ID
303 * \retval Handle(_pyHypothesis) - The found hypothesis
305 //================================================================================
307 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
309 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
310 for ( ; hyp != myHypos.end(); ++hyp )
311 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
313 return Handle(_pyHypothesis)();
316 //================================================================================
318 * \brief Find algorithm the created algorithm
319 * \param theGeom - The shape ID the algorithm was created on
320 * \param theMesh - The mesh ID that created the algorithm
321 * \param dim - The algo dimension
322 * \retval Handle(_pyHypothesis) - The found algo
324 //================================================================================
326 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
327 const TCollection_AsciiString& theAlgoType )
329 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
330 for ( ; hyp != myHypos.end(); ++hyp )
331 if ( !hyp->IsNull() &&
333 (*hyp)->GetType() == theAlgoType &&
334 (*hyp)->GetGeom() == theGeom &&
335 (*hyp)->GetMesh() == theMesh )
340 //================================================================================
342 * \brief Change order of commands in the script
343 * \param theCmd1 - One command
344 * \param theCmd2 - Another command
346 //================================================================================
348 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
350 list< Handle(_pyCommand) >::iterator pos1, pos2;
351 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
352 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
353 myCommands.insert( pos1, theCmd2 );
354 myCommands.insert( pos2, theCmd1 );
355 myCommands.erase( pos1 );
356 myCommands.erase( pos2 );
358 int nb1 = theCmd1->GetOrderNb();
359 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
360 theCmd2->SetOrderNb( nb1 );
361 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
362 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
365 //================================================================================
367 * \brief Set one command after the other
368 * \param theCmd - Command to move
369 * \param theAfterCmd - Command ater which to insert the first one
371 //================================================================================
373 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
375 // cout << "SET\t" << theCmd->GetString() << endl << "AFTER\t" << theAfterCmd->GetString() << endl << endl;
376 list< Handle(_pyCommand) >::iterator pos;
377 pos = find( myCommands.begin(), myCommands.end(), theCmd );
378 myCommands.erase( pos );
379 pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
380 myCommands.insert( ++pos, theCmd );
383 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
384 (*pos)->SetOrderNb( i++ );
387 //================================================================================
389 * \brief Set method to access to object wrapped with python class
390 * \param theID - The wrapped object entry
391 * \param theMethod - The accessor method
393 //================================================================================
395 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
397 myID2AccessorMethod.Bind( theID, (char*) theMethod );
400 //================================================================================
402 * \brief Find out type of geom group
403 * \param grpID - The geom group entry
404 * \retval int - The type
406 //================================================================================
408 static bool sameGroupType( const _pyID& grpID,
409 const TCollection_AsciiString& theType)
411 // define group type as smesh.Mesh.Group() does
413 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
414 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
415 if ( !aSObj->_is_nil() ) {
416 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
417 if ( !aGeomObj->_is_nil() ) {
418 switch ( aGeomObj->GetShapeType() ) {
419 case GEOM::VERTEX: type = SMESH::NODE; break;
420 case GEOM::EDGE: type = SMESH::EDGE; break;
421 case GEOM::FACE: type = SMESH::FACE; break;
423 case GEOM::SHELL: type = SMESH::VOLUME; break;
424 case GEOM::COMPOUND: {
425 GEOM::GEOM_Gen_var aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
426 if ( !aGeomGen->_is_nil() ) {
427 GEOM::GEOM_IGroupOperations_var aGrpOp =
428 aGeomGen->GetIGroupOperations( study->StudyId() );
429 if ( !aGrpOp->_is_nil() ) {
430 switch ( aGrpOp->GetType( aGeomObj )) {
431 case TopAbs_VERTEX: type = SMESH::NODE; break;
432 case TopAbs_EDGE: type = SMESH::EDGE; break;
433 case TopAbs_FACE: type = SMESH::FACE; break;
434 case TopAbs_SOLID: type = SMESH::VOLUME; break;
445 MESSAGE("Type of the group " << grpID << " not found");
448 if ( theType.IsIntegerValue() )
449 return type == theType.IntegerValue();
452 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
453 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
454 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
455 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
461 //================================================================================
464 * \param theCreationCmd -
466 //================================================================================
468 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd):
469 _pyObject(theCreationCmd), myHasEditor(false)
471 // convert my creation command
472 Handle(_pyCommand) creationCmd = GetCreationCmd();
473 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
474 creationCmd->SetMethod( "Mesh" );
476 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
479 //================================================================================
481 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
482 * \param theCommand - Engine method called for this mesh
484 //================================================================================
486 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
488 // smesh.py wraps the following methods:
490 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
491 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
492 // 2. AddHypothesis(geom, hyp)
493 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
494 // 3. CreateGroupFromGEOM(type, name, grp)
495 // --> in Mesh.Group(grp, name="")
496 // 4. ExportToMED(f, opt, version)
497 // --> in Mesh.ExportToMED( f, version, opt=0 )
498 // 5. ExportMED(f, opt)
499 // --> in Mesh.ExportMED( f,opt=0 )
501 // --> in Mesh.ExportDAT( f )
503 // --> in Mesh.ExportUNV(f)
504 // 8. ExportSTL(f, ascii)
505 // --> in Mesh.ExportSTL(f, ascii=1)
507 const TCollection_AsciiString method = theCommand->GetMethod();
508 if ( method == "GetSubMesh" ) {
509 mySubmeshes.push_back( theCommand );
511 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
512 myAddHypCmds.push_back( theCommand );
514 const _pyID& hypID = theCommand->GetArg( 2 );
515 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
516 if ( !hyp.IsNull() ) {
517 myHypos.push_back( hyp );
518 if ( hyp->GetMesh().IsEmpty() )
519 hyp->SetMesh( this->GetID() );
522 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
523 _pyID grp = theCommand->GetArg( 3 );
524 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
525 theCommand->SetMethod( "Group" );
526 theCommand->RemoveArgs();
527 theCommand->SetArg( 1, grp );
530 AddMeshAccess( theCommand );
533 else if ( method == "ExportToMED" ) {//(f, opt, version)
534 // --> (f, version, opt)
535 _pyID opt = theCommand->GetArg( 2 );
536 _pyID ver = theCommand->GetArg( 3 );
537 theCommand->SetArg( 2, ver );
538 theCommand->SetArg( 3, opt );
540 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
542 const _pyID & hypID = theCommand->GetArg( 2 );
544 // check if this mesh still has corresponding addition command
545 bool hasAddCmd = false;
546 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
547 while ( cmd != myAddHypCmds.end() )
549 // AddHypothesis(geom, hyp)
550 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
553 cmd = myAddHypCmds.erase( cmd );
560 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
561 if ( ! hasAddCmd ) { // hypo addition already wrapped
562 // access to wrapped mesh
563 AddMeshAccess( theCommand );
564 // access to wrapped algo
565 if ( !hyp.IsNull() && hyp->IsAlgo() && hyp->IsWrapped() )
566 theCommand->SetArg( 2, theCommand->GetArg( 2 ) + ".GetAlgorithm()" );
568 // remove hyp from myHypos
569 myHypos.remove( hyp );
572 // leave only one " mesh_editor_<nb> = mesh.GetMeshEditor()"
573 else if ( theCommand->GetMethod() == "GetMeshEditor")
578 AddMeshAccess( theCommand );
582 // apply theCommand to the mesh wrapped by smeshpy mesh
585 AddMeshAccess( theCommand );
589 //================================================================================
591 * \brief Convert creation and addition of all algos and hypos
593 //================================================================================
595 void _pyMesh::Flush()
597 list < Handle(_pyCommand) >::iterator cmd, cmd2;
599 // try to convert algo addition like this:
600 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
601 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
603 Handle(_pyCommand) addCmd = *cmd;
604 const _pyID& algoID = addCmd->GetArg( 2 );
605 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
606 if ( algo.IsNull() || !algo->IsAlgo() )
609 _pyID geom = addCmd->GetArg( 1 );
610 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
612 // wrapped algo is created atfer mesh creation
613 GetCreationCmd()->AddDependantCmd( addCmd );
615 if ( geom != GetGeom() ) // local algo
617 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
618 addCmd->SetArg( addCmd->GetNbArgs() + 1,
619 TCollection_AsciiString( "geom=" ) + geom );
620 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
621 for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
622 Handle(_pyCommand) subCmd = *cmd2;
623 if ( geom == subCmd->GetArg( 1 )) {
624 subCmd->SetObject( algo->GetID() );
625 subCmd->RemoveArgs();
626 addCmd->AddDependantCmd( subCmd );
631 else // ALGO was already created
633 // mesh.AddHypothesis(geom, ALGO ) --> mesh.GetMesh().AddHypothesis(geom, ALGO )
634 AddMeshAccess( addCmd );
635 // mesh.GetMesh().AddHypothesis(geom, ALGO ) ->
636 // mesh.GetMesh().AddHypothesis(geom, ALGO.GetAlgorithm() )
637 addCmd->SetArg( 2, addCmd->GetArg( 2 ) + ".GetAlgorithm()" );
641 // try to convert hypo addition like this:
642 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
643 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
645 Handle(_pyCommand) addCmd = *cmd;
646 const _pyID& hypID = addCmd->GetArg( 2 );
647 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
648 if ( hyp.IsNull() || hyp->IsAlgo() )
650 const _pyID& geom = addCmd->GetArg( 1 );
651 // find algo created on <geom> for this mesh
652 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, this->GetID(), hyp->GetType() );
653 //_pyID algoID = algo.IsNull() ? "" : algo->GetID();
654 if ( !algo.IsNull() && hyp->Addition2Creation( addCmd, this->GetID() )) // OK
656 addCmd->SetObject( algo->GetID() );
657 algo->GetCreationCmd()->AddDependantCmd( addCmd );
661 AddMeshAccess( addCmd );
665 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
666 for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
667 Handle(_pyCommand) subCmd = *cmd;
668 if ( subCmd->GetNbArgs() > 0 )
669 AddMeshAccess( subCmd );
671 myAddHypCmds.clear();
675 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
676 for ( ; hyp != myHypos.end(); ++hyp )
680 //================================================================================
682 * \brief _pyHypothesis constructor
683 * \param theCreationCmd -
685 //================================================================================
687 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
688 _pyObject( theCreationCmd )
690 myDim = myIsAlgo = /*myIsLocal = */myIsWrapped = myIsConverted = false;
693 //================================================================================
695 * \brief Creates algorithm or hypothesis
696 * \param theCreationCmd - The engine command creating a hypothesis
697 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
699 //================================================================================
701 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
703 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
704 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
706 Handle(_pyHypothesis) hyp, algo;
709 const TCollection_AsciiString & hypTypeWithQuotes = theCreationCmd->GetArg( 1 );
710 if ( hypTypeWithQuotes.IsEmpty() )
713 TCollection_AsciiString hypType =
714 hypTypeWithQuotes.SubString( 2, hypTypeWithQuotes.Length() - 1 );
716 algo = new _pyAlgorithm( theCreationCmd );
717 hyp = new _pyHypothesis( theCreationCmd );
719 // 1D Regular_1D ----------
720 if ( hypType == "Regular_1D" ) {
722 algo->myCreationMethod = "Segment";
724 else if ( hypType == "LocalLength" ) {
726 hyp->myCreationMethod = "LocalLength";
727 hyp->myType = "Regular_1D";
728 hyp->myArgMethods.Append( "SetLength" );
730 else if ( hypType == "NumberOfSegments" ) {
731 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
733 hyp->myCreationMethod = "NumberOfSegments";
734 hyp->myType = "Regular_1D";
735 hyp->myArgMethods.Append( "SetNumberOfSegments" );
736 hyp->myArgMethods.Append( "SetScaleFactor" );
738 else if ( hypType == "Arithmetic1D" ) {
739 hyp = new _pyComplexParamHypo( theCreationCmd );
741 hyp->myCreationMethod = "Arithmetic1D";
742 hyp->myType = "Regular_1D";
744 else if ( hypType == "StartEndLength" ) {
745 hyp = new _pyComplexParamHypo( theCreationCmd );
747 hyp->myCreationMethod = "StartEndLength";
748 hyp->myType = "Regular_1D";
750 else if ( hypType == "Deflection1D" ) {
752 hyp->myCreationMethod = "Deflection1D";
753 hyp->myArgMethods.Append( "SetDeflection" );
754 hyp->myType = "Regular_1D";
756 else if ( hypType == "Propagation" ) {
758 hyp->myCreationMethod = "Propagation";
759 hyp->myType = "Regular_1D";
761 else if ( hypType == "QuadraticMesh" ) {
763 hyp->myCreationMethod = "QuadraticMesh";
764 hyp->myType = "Regular_1D";
766 else if ( hypType == "AutomaticLength" ) {
768 hyp->myCreationMethod = "AutomaticLength";
769 hyp->myType = "Regular_1D";
770 hyp->myArgMethods.Append( "SetFineness");
772 // 1D Python_1D ----------
773 else if ( hypType == "Python_1D" ) {
775 algo->myCreationMethod = "Segment";
776 algo->myArgs.Append( "algo=smesh.PYTHON");
778 else if ( hypType == "PythonSplit1D" ) {
780 hyp->myCreationMethod = "PythonSplit1D";
781 hyp->myType = "Python_1D";
782 hyp->myArgMethods.Append( "SetNumberOfSegments");
783 hyp->myArgMethods.Append( "SetPythonLog10RatioFunction");
786 else if ( hypType == "MEFISTO_2D" ) {
788 algo->myCreationMethod = "Triangle";
790 else if ( hypType == "MaxElementArea" ) {
792 hyp->myCreationMethod = "MaxElementArea";
793 hyp->myType = "MEFISTO_2D";
794 hyp->myArgMethods.Append( "SetMaxElementArea");
796 else if ( hypType == "LengthFromEdges" ) {
798 hyp->myCreationMethod = "LengthFromEdges";
799 hyp->myType = "MEFISTO_2D";
801 else if ( hypType == "Quadrangle_2D" ) {
803 algo->myCreationMethod = "Quadrangle";
805 else if ( hypType == "QuadranglePreference" ) {
807 hyp->myCreationMethod = "QuadranglePreference";
808 hyp->myType = "Quadrangle_2D";
811 else if ( hypType == "NETGEN_3D") {
813 algo->myCreationMethod = "Tetrahedron";
814 algo->myArgs.Append( "algo=smesh.NETGEN" );
816 else if ( hypType == "MaxElementVolume") {
818 hyp->myCreationMethod = "MaxElementVolume";
819 hyp->myType = "NETGEN_3D";
820 hyp->myArgMethods.Append( "SetMaxElementVolume" );
822 else if ( hypType == "GHS3D_3D" ) {
824 algo->myCreationMethod = "Tetrahedron";
825 algo->myArgs.Append( "algo=smesh.GHS3D" );
827 else if ( hypType == "Hexa_3D" ) {
829 algo->myCreationMethod = "Hexahedron";
832 if ( algo->GetDim() ) {
833 algo->myType = hypType;
839 //================================================================================
841 * \brief Convert the command adding a hypothesis to mesh into a smesh command
842 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
843 * \param theAlgo - The algo that can create this hypo
844 * \retval bool - false if the command cant be converted
846 //================================================================================
848 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
849 const _pyID& theMesh)
851 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
853 if ( !IsWrappable( theMesh ))
860 // mesh.AddHypothesis(geom,hyp) --> hyp = theMesh.myCreationMethod(args)
861 theCmd->SetResultValue( GetID() );
862 theCmd->SetObject( theMesh );
863 theCmd->SetMethod( myCreationMethod );
865 theCmd->RemoveArgs();
866 for ( int i = 1; i <= myArgs.Length(); ++i ) {
867 if ( !myArgs( i ).IsEmpty() )
868 theCmd->SetArg( i, myArgs( i ));
870 theCmd->SetArg( i, "[]");
872 // set a new creation command
873 GetCreationCmd()->Clear();
874 SetCreationCmd( theCmd );
876 // clear commands setting arg values
877 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
878 for ( ; argCmd != myArgCommands.end(); ++argCmd )
883 // // set arg commands after hypo creation
884 // list<Handle(_pyCommand)>::iterator argCmd = myArgCommands.begin();
885 // for ( ; argCmd != myArgCommands.end(); ++argCmd )
886 // if ( !(*argCmd)->IsEmpty() && GetCommandNb() > (*argCmd)->GetOrderNb() )
887 // theGen->ExchangeCommands( GetCreationCmd(), *argCmd );
890 // set unknown arg commands after hypo creation
891 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
892 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
893 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
894 afterCmd->AddDependantCmd( *cmd );
900 //================================================================================
902 * \brief Remember hypothesis parameter values
903 * \param theCommand - The called hypothesis method
905 //================================================================================
907 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
911 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
912 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
913 while ( myArgs.Length() < i )
914 myArgs.Append( "[]" );
915 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
916 myArgCommands.push_back( theCommand );
920 myUnknownCommands.push_back( theCommand );
923 //================================================================================
925 * \brief Finish conversion
927 //================================================================================
929 void _pyHypothesis::Flush()
932 // forget previous hypothesis modifications
933 myArgCommands.clear();
934 myUnknownCommands.clear();
938 //================================================================================
940 * \brief Remember hypothesis parameter values
941 * \param theCommand - The called hypothesis method
943 //================================================================================
945 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
947 // ex: hyp.SetLength(start, 1)
948 // hyp.SetLength(end, 0)
949 ASSERT(( theCommand->GetMethod() == "SetLength" ));
950 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
951 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
952 while ( myArgs.Length() < i )
953 myArgs.Append( "[]" );
954 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
955 myArgCommands.push_back( theCommand );
958 //================================================================================
960 * \brief additionally to Addition2Creation, clears SetDistrType() command
961 * \param theCmd - AddHypothesis() command
962 * \param theMesh - mesh to which a hypothesis is added
963 * \retval bool - convertion result
965 //================================================================================
967 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
968 const _pyID& theMesh)
970 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
971 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
972 bool scaleDistrType = false;
973 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
974 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
975 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
976 if ( (*cmd)->GetArg( 1 ) == "1" ) {
977 scaleDistrType = true;
980 else if ( !scaleDistrType ) {
981 // distribution type changed: remove scale factor from args
982 myArgs.Remove( 2, myArgs.Length() );
988 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
991 //================================================================================
993 * \brief remove repeated commands defining distribution
995 //================================================================================
997 void _pyNumberOfSegmentsHyp::Flush()
999 const int nbCmdLists = 2;
1000 list<Handle(_pyCommand)> * cmds[nbCmdLists] = { &myArgCommands, &myUnknownCommands };
1001 for ( int i = 0; i < nbCmdLists; ++i ) {
1002 set<TCollection_AsciiString> uniqueMethods;
1003 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1004 list<Handle(_pyCommand)>::reverse_iterator cmd = cmdList.rbegin();
1005 for ( ; cmd != cmdList.rend(); ++cmd ) {
1006 bool isNewInSet = uniqueMethods.insert( (*cmd)->GetMethod() ).second;
1014 //================================================================================
1016 * \brief _pyAlgorithm constructor
1017 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1019 //================================================================================
1021 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1022 : _pyHypothesis( theCreationCmd )
1027 //================================================================================
1029 * \brief Convert the command adding an algorithm to mesh
1030 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1031 * \param theMesh - The mesh needing this algo
1032 * \retval bool - false if the command cant be converted
1034 //================================================================================
1036 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1037 const _pyID& theMeshID)
1039 if ( IsWrappable( theMeshID )) {
1041 myGeom = theCmd->GetArg( 1 );
1043 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1044 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1045 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1052 //================================================================================
1054 * \brief Return starting position of a part of python command
1055 * \param thePartIndex - The index of command part
1056 * \retval int - Part position
1058 //================================================================================
1060 int _pyCommand::GetBegPos( int thePartIndex )
1064 if ( myBegPos.Length() < thePartIndex )
1066 return myBegPos( thePartIndex );
1069 //================================================================================
1071 * \brief Store starting position of a part of python command
1072 * \param thePartIndex - The index of command part
1073 * \param thePosition - Part position
1075 //================================================================================
1077 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1079 while ( myBegPos.Length() < thePartIndex )
1080 myBegPos.Append( UNKNOWN );
1081 myBegPos( thePartIndex ) = thePosition;
1084 //================================================================================
1086 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1087 * \retval const TCollection_AsciiString & - ResultValue substring
1089 //================================================================================
1091 const TCollection_AsciiString & _pyCommand::GetResultValue()
1093 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1095 int begPos = myString.Location( "=", 1, Length() );
1097 myRes = GetWord( myString, begPos, false );
1100 SetBegPos( RESULT_IND, begPos );
1105 //================================================================================
1107 * \brief Return substring of python command looking like ResVal = Object.Meth()
1108 * \retval const TCollection_AsciiString & - Object substring
1110 //================================================================================
1112 const TCollection_AsciiString & _pyCommand::GetObject()
1114 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1117 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1119 begPos = myString.Location( "=", 1, Length() ) + 1;
1121 myObj = GetWord( myString, begPos, true );
1122 SetBegPos( OBJECT_IND, begPos );
1128 //================================================================================
1130 * \brief Return substring of python command looking like ResVal = Obj.Method()
1131 * \retval const TCollection_AsciiString & - Method substring
1133 //================================================================================
1135 const TCollection_AsciiString & _pyCommand::GetMethod()
1137 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1140 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1141 bool forward = true;
1143 begPos = myString.Location( "(", 1, Length() ) - 1;
1147 myMeth = GetWord( myString, begPos, forward );
1148 SetBegPos( METHOD_IND, begPos );
1154 //================================================================================
1156 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1157 * \retval const TCollection_AsciiString & - Arg<index> substring
1159 //================================================================================
1161 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1163 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1166 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1168 begPos = myString.Location( "(", 1, Length() ) + 1;
1170 int i = 0, prevLen = 0;
1171 while ( begPos != EMPTY ) {
1173 // check if we are looking at the closing parenthesis
1174 while ( begPos <= Length() && isspace( myString.Value( begPos )))
1176 if ( begPos > Length() || myString.Value( begPos ) == ')' )
1178 myArgs.Append( GetWord( myString, begPos, true, true ));
1179 SetBegPos( ARG1_IND + i, begPos );
1180 prevLen = myArgs.Last().Length();
1182 myArgs.Remove( myArgs.Length() ); // no more args
1186 if ( myArgs.Length() < index )
1187 return theEmptyString;
1188 return myArgs( index );
1191 //================================================================================
1193 * \brief Check if char is a word part
1194 * \param c - The character to check
1195 * \retval bool - The check result
1197 //================================================================================
1199 static inline bool isWord(const char c, const bool dotIsWord)
1202 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1205 //================================================================================
1207 * \brief Looks for a word in the string and returns word's beginning
1208 * \param theString - The input string
1209 * \param theStartPos - The position to start the search, returning word's beginning
1210 * \param theForward - The search direction
1211 * \retval TCollection_AsciiString - The found word
1213 //================================================================================
1215 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1217 const bool theForward,
1218 const bool dotIsWord )
1220 int beg = theStartPos, end = theStartPos;
1221 theStartPos = EMPTY;
1222 if ( beg < 1 || beg > theString.Length() )
1223 return theEmptyString;
1225 if ( theForward ) { // search forward
1227 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1229 if ( beg > theString.Length() )
1230 return theEmptyString; // no word found
1233 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1237 else { // search backward
1239 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1242 return theEmptyString; // no word found
1244 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1249 //cout << theString << " ---- " << beg << " - " << end << endl;
1250 return theString.SubString( beg, end );
1253 //================================================================================
1255 * \brief Look for position where not space char is
1256 * \param theString - The string
1257 * \param thePos - The position to search from and which returns result
1258 * \retval bool - false if there are only space after thePos in theString
1262 //================================================================================
1264 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1266 if ( thePos < 1 || thePos > theString.Length() )
1269 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1272 return thePos <= theString.Length();
1275 //================================================================================
1277 * \brief Modify a part of the command
1278 * \param thePartIndex - The index of the part
1279 * \param thePart - The new part string
1280 * \param theOldPart - The old part
1282 //================================================================================
1284 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1285 TCollection_AsciiString& theOldPart)
1287 int pos = GetBegPos( thePartIndex );
1288 if ( pos <= Length() && theOldPart != thePart)
1290 TCollection_AsciiString seperator;
1292 pos = GetBegPos( thePartIndex + 1 );
1293 if ( pos < 1 ) return;
1294 switch ( thePartIndex ) {
1295 case RESULT_IND: seperator = " = "; break;
1296 case OBJECT_IND: seperator = "."; break;
1297 case METHOD_IND: seperator = "()"; break;
1301 myString.Remove( pos, theOldPart.Length() );
1302 if ( !seperator.IsEmpty() )
1303 myString.Insert( pos , seperator );
1304 myString.Insert( pos, thePart );
1305 // update starting positions of the following parts
1306 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1307 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1308 if ( myBegPos( i ) > 0 )
1309 myBegPos( i ) += posDelta;
1311 theOldPart = thePart;
1315 //================================================================================
1317 * \brief Set agrument
1318 * \param index - The argument index, it counts from 1
1319 * \param theArg - The argument string
1321 //================================================================================
1323 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1326 int argInd = ARG1_IND + index - 1;
1327 int pos = GetBegPos( argInd );
1328 if ( pos < 1 ) // no index-th arg exist, append inexistent args
1330 // find a closing parenthesis
1331 if ( int lastArgInd = GetNbArgs() ) {
1332 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
1333 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
1338 while ( pos > 0 && myString.Value( pos ) != ')' )
1341 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
1345 while ( myArgs.Length() < index ) {
1346 if ( myArgs.Length() )
1347 myString.Insert( pos++, "," );
1348 myArgs.Append("None");
1349 myString.Insert( pos, myArgs.Last() );
1350 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1351 pos += myArgs.Last().Length();
1354 SetPart( argInd, theArg, myArgs( index ));
1357 //================================================================================
1359 * \brief Empty arg list
1361 //================================================================================
1363 void _pyCommand::RemoveArgs()
1365 if ( int pos = myString.Location( '(', 1, Length() ))
1366 myString.Trunc( pos );
1369 if ( myBegPos.Length() >= ARG1_IND )
1370 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1373 //================================================================================
1375 * \brief Set dependent commands after this one
1377 //================================================================================
1379 bool _pyCommand::SetDependentCmdsAfter() const
1381 bool orderChanged = false;
1382 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1383 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1384 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1385 orderChanged = true;
1386 theGen->SetCommandAfter( *cmd, this );
1387 (*cmd)->SetDependentCmdsAfter();
1390 return orderChanged;
1392 //================================================================================
1394 * \brief Insert accessor method after theObjectID
1395 * \param theObjectID - id of the accessed object
1396 * \param theAcsMethod - name of the method giving access to the object
1397 * \retval bool - false if theObjectID is not found in the command string
1399 //================================================================================
1401 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
1403 if ( !theAcsMethod )
1405 // start object search from the object, i.e. ignore result
1407 int beg = GetBegPos( OBJECT_IND );
1408 if ( beg < 1 || beg > Length() )
1410 while (( beg = myString.Location( theObjectID, beg, Length() )))
1412 // check that theObjectID is not just a part of a longer ID
1413 int afterEnd = beg + theObjectID.Length();
1414 Standard_Character c = myString.Value( afterEnd );
1415 if ( !isalnum( c ) && c != ':' ) {
1417 int oldLen = Length();
1418 myString.Insert( afterEnd, (char*) theAcsMethod );
1419 myString.Insert( afterEnd, "." );
1420 // update starting positions of the parts following the modified one
1421 int posDelta = Length() - oldLen;
1422 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
1423 if ( myBegPos( i ) > afterEnd )
1424 myBegPos( i ) += posDelta;
1428 beg = afterEnd; // is a part - next search
1433 //================================================================================
1435 * \brief Return method name giving access to an interaface object wrapped by python class
1436 * \retval const char* - method name
1438 //================================================================================
1440 const char* _pyObject::AccessorMethod() const