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" ||
233 theCommand->GetMethod() == "CreateEmptyMesh" )
235 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
236 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
240 // CreateHypothesis()
241 if ( theCommand->GetMethod() == "CreateHypothesis" )
243 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
247 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
248 if ( theCommand->GetMethod() == "Compute" )
250 const _pyID& meshID = theCommand->GetArg( 1 );
251 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
252 if ( id_mesh != myMeshes.end() ) {
253 theCommand->SetObject( meshID );
254 theCommand->RemoveArgs();
255 id_mesh->second->Flush();
260 // leave only one smeshgen.GetPattern() in the script
261 if ( theCommand->GetMethod() == "GetPattern" ) {
262 if ( myHasPattern ) {
269 // smeshgen.Method() --> smesh.smesh.Method()
270 theCommand->SetObject( SMESH_2smeshpy::GenName() );
273 //================================================================================
275 * \brief Convert the remembered commands
277 //================================================================================
281 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
282 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
283 if ( ! id_mesh->second.IsNull() )
284 id_mesh->second->Flush();
286 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
287 for ( ; hyp != myHypos.end(); ++hyp )
288 if ( !hyp->IsNull() ) {
290 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
291 if ( !(*hyp)->IsWrapped() )
292 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
296 //================================================================================
298 * \brief Add access method to mesh that is an object or an argument
299 * \param theCmd - command to add access method
300 * \retval bool - true if added
302 //================================================================================
304 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
306 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
307 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
308 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
314 //================================================================================
316 * \brief Add access method to algo that is an object or an argument
317 * \param theCmd - command to add access method
318 * \retval bool - true if added
320 //================================================================================
322 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
324 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
325 for ( ; hyp != myHypos.end(); ++hyp ) {
326 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
327 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
333 //================================================================================
335 * \brief Find hypothesis by ID (entry)
336 * \param theHypID - The hypothesis ID
337 * \retval Handle(_pyHypothesis) - The found hypothesis
339 //================================================================================
341 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
343 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
344 for ( ; hyp != myHypos.end(); ++hyp )
345 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
347 return Handle(_pyHypothesis)();
350 //================================================================================
352 * \brief Find algorithm the created algorithm
353 * \param theGeom - The shape ID the algorithm was created on
354 * \param theMesh - The mesh ID that created the algorithm
355 * \param dim - The algo dimension
356 * \retval Handle(_pyHypothesis) - The found algo
358 //================================================================================
360 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
361 const TCollection_AsciiString& theAlgoType )
363 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
364 for ( ; hyp != myHypos.end(); ++hyp )
365 if ( !hyp->IsNull() &&
367 (*hyp)->GetType() == theAlgoType &&
368 (*hyp)->GetGeom() == theGeom &&
369 (*hyp)->GetMesh() == theMesh )
374 //================================================================================
376 * \brief Change order of commands in the script
377 * \param theCmd1 - One command
378 * \param theCmd2 - Another command
380 //================================================================================
382 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
384 list< Handle(_pyCommand) >::iterator pos1, pos2;
385 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
386 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
387 myCommands.insert( pos1, theCmd2 );
388 myCommands.insert( pos2, theCmd1 );
389 myCommands.erase( pos1 );
390 myCommands.erase( pos2 );
392 int nb1 = theCmd1->GetOrderNb();
393 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
394 theCmd2->SetOrderNb( nb1 );
395 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
396 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
399 //================================================================================
401 * \brief Set one command after the other
402 * \param theCmd - Command to move
403 * \param theAfterCmd - Command ater which to insert the first one
405 //================================================================================
407 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
410 //cout << "SET\t" << theAfterCmd->GetString() << endl << "BEFORE\t" << theCmd->GetString() << endl<<endl;
412 list< Handle(_pyCommand) >::iterator pos;
413 pos = find( myCommands.begin(), myCommands.end(), theCmd );
414 myCommands.erase( pos );
415 pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
416 myCommands.insert( ++pos, theCmd );
419 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
420 (*pos)->SetOrderNb( i++ );
423 //================================================================================
425 * \brief Set method to access to object wrapped with python class
426 * \param theID - The wrapped object entry
427 * \param theMethod - The accessor method
429 //================================================================================
431 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
433 myID2AccessorMethod.Bind( theID, (char*) theMethod );
436 //================================================================================
438 * \brief Find out type of geom group
439 * \param grpID - The geom group entry
440 * \retval int - The type
442 //================================================================================
444 static bool sameGroupType( const _pyID& grpID,
445 const TCollection_AsciiString& theType)
447 // define group type as smesh.Mesh.Group() does
449 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
450 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
451 if ( !aSObj->_is_nil() ) {
452 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
453 if ( !aGeomObj->_is_nil() ) {
454 switch ( aGeomObj->GetShapeType() ) {
455 case GEOM::VERTEX: type = SMESH::NODE; break;
456 case GEOM::EDGE: type = SMESH::EDGE; break;
457 case GEOM::FACE: type = SMESH::FACE; break;
459 case GEOM::SHELL: type = SMESH::VOLUME; break;
460 case GEOM::COMPOUND: {
461 GEOM::GEOM_Gen_var aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
462 if ( !aGeomGen->_is_nil() ) {
463 GEOM::GEOM_IGroupOperations_var aGrpOp =
464 aGeomGen->GetIGroupOperations( study->StudyId() );
465 if ( !aGrpOp->_is_nil() ) {
466 switch ( aGrpOp->GetType( aGeomObj )) {
467 case TopAbs_VERTEX: type = SMESH::NODE; break;
468 case TopAbs_EDGE: type = SMESH::EDGE; break;
469 case TopAbs_FACE: type = SMESH::FACE; break;
470 case TopAbs_SOLID: type = SMESH::VOLUME; break;
481 MESSAGE("Type of the group " << grpID << " not found");
484 if ( theType.IsIntegerValue() )
485 return type == theType.IntegerValue();
488 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
489 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
490 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
491 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
497 //================================================================================
500 * \param theCreationCmd -
502 //================================================================================
504 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd):
505 _pyObject(theCreationCmd), myHasEditor(false)
507 // convert my creation command
508 Handle(_pyCommand) creationCmd = GetCreationCmd();
509 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
510 creationCmd->SetMethod( "Mesh" );
512 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
515 //================================================================================
517 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
518 * \param theCommand - Engine method called for this mesh
520 //================================================================================
522 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
524 // smesh.py wraps the following methods:
526 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
527 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
528 // 2. AddHypothesis(geom, hyp)
529 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
530 // 3. CreateGroupFromGEOM(type, name, grp)
531 // --> in Mesh.Group(grp, name="")
532 // 4. ExportToMED(f, opt, version)
533 // --> in Mesh.ExportToMED( f, version, opt=0 )
534 // 5. ExportMED(f, opt)
535 // --> in Mesh.ExportMED( f,opt=0 )
537 // --> in Mesh.ExportDAT( f )
539 // --> in Mesh.ExportUNV(f)
540 // 8. ExportSTL(f, ascii)
541 // --> in Mesh.ExportSTL(f, ascii=1)
543 const TCollection_AsciiString method = theCommand->GetMethod();
544 if ( method == "GetSubMesh" ) {
545 mySubmeshes.push_back( theCommand );
547 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
548 myAddHypCmds.push_back( theCommand );
550 const _pyID& hypID = theCommand->GetArg( 2 );
551 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
552 if ( !hyp.IsNull() ) {
553 myHypos.push_back( hyp );
554 if ( hyp->GetMesh().IsEmpty() )
555 hyp->SetMesh( this->GetID() );
558 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
559 _pyID grp = theCommand->GetArg( 3 );
560 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
561 theCommand->SetMethod( "Group" );
562 theCommand->RemoveArgs();
563 theCommand->SetArg( 1, grp );
566 AddMeshAccess( theCommand );
569 else if ( method == "ExportToMED" ) {//(f, opt, version)
570 // --> (f, version, opt)
571 _pyID opt = theCommand->GetArg( 2 );
572 _pyID ver = theCommand->GetArg( 3 );
573 theCommand->SetArg( 2, ver );
574 theCommand->SetArg( 3, opt );
576 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
578 const _pyID & hypID = theCommand->GetArg( 2 );
580 // check if this mesh still has corresponding addition command
581 bool hasAddCmd = false;
582 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
583 while ( cmd != myAddHypCmds.end() )
585 // AddHypothesis(geom, hyp)
586 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
589 cmd = myAddHypCmds.erase( cmd );
596 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
597 if ( ! hasAddCmd ) { // hypo addition already wrapped
598 // access to wrapped mesh
599 AddMeshAccess( theCommand );
600 // access to wrapped algo
601 if ( !hyp.IsNull() && hyp->IsAlgo() && hyp->IsWrapped() )
602 theCommand->SetArg( 2, theCommand->GetArg( 2 ) + ".GetAlgorithm()" );
604 // remove hyp from myHypos
605 myHypos.remove( hyp );
608 // leave only one " mesh_editor_<nb> = mesh.GetMeshEditor()"
609 else if ( theCommand->GetMethod() == "GetMeshEditor")
614 AddMeshAccess( theCommand );
618 // apply theCommand to the mesh wrapped by smeshpy mesh
621 AddMeshAccess( theCommand );
627 //================================================================================
629 * \brief add addition result treatement command
630 * \param addCmd - hypothesis addition command
632 //================================================================================
634 void addErrorTreatmentCmd( Handle(_pyCommand) & addCmd,
637 return; // TO DEBUD - TreatHypoStatus() is not placed right after addCmd
638 // addCmd: status = mesh.AddHypothesis( geom, hypo )
639 // treatement command:
640 // def TreatHypoStatus(status, hypName, geomName, isAlgo):
641 TCollection_AsciiString status = addCmd->GetResultValue();
642 if ( !status.IsEmpty() ) {
643 const _pyID& geomID = addCmd->GetArg( 1 );
644 const _pyID& hypoID = addCmd->GetArg( 2 );
645 TCollection_AsciiString cmdStr = addCmd->GetIndentation() +
646 SMESH_2smeshpy::SmeshpyName() + ".TreatHypoStatus( " + status + ", " +
647 SMESH_2smeshpy::SmeshpyName() + ".GetName(" + hypoID + "), " +
648 SMESH_2smeshpy::SmeshpyName() + ".GetName(" + geomID + "), " +
649 (char*)( isAlgo ? "True" : "False" ) + " )";
650 Handle(_pyCommand) cmd = theGen->AddCommand( cmdStr );
651 addCmd->AddDependantCmd( cmd, true );
656 //================================================================================
658 * \brief Convert creation and addition of all algos and hypos
660 //================================================================================
662 void _pyMesh::Flush()
664 list < Handle(_pyCommand) >::iterator cmd, cmd2;
666 // try to convert algo addition like this:
667 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
668 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
670 Handle(_pyCommand) addCmd = *cmd;
671 const _pyID& algoID = addCmd->GetArg( 2 );
672 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
673 if ( algo.IsNull() || !algo->IsAlgo() )
676 _pyID geom = addCmd->GetArg( 1 );
677 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
679 // wrapped algo is created atfer mesh creation
680 GetCreationCmd()->AddDependantCmd( addCmd );
682 if ( geom != GetGeom() ) // local algo
684 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
685 addCmd->SetArg( addCmd->GetNbArgs() + 1,
686 TCollection_AsciiString( "geom=" ) + geom );
687 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
688 for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
689 Handle(_pyCommand) subCmd = *cmd2;
690 if ( geom == subCmd->GetArg( 1 )) {
691 subCmd->SetObject( algo->GetID() );
692 subCmd->RemoveArgs();
693 addCmd->AddDependantCmd( subCmd );
698 else // ALGO was already created
700 // mesh.AddHypothesis(geom, ALGO ) --> mesh.GetMesh().AddHypothesis(geom, ALGO )
701 AddMeshAccess( addCmd );
702 // mesh.GetMesh().AddHypothesis(geom, ALGO ) ->
703 // mesh.GetMesh().AddHypothesis(geom, ALGO.GetAlgorithm() )
704 addCmd->SetArg( 2, addCmd->GetArg( 2 ) + ".GetAlgorithm()" );
705 // add addition result treatement cmd
706 addErrorTreatmentCmd( addCmd, true );
710 // try to convert hypo addition like this:
711 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
712 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
714 Handle(_pyCommand) addCmd = *cmd;
715 const _pyID& hypID = addCmd->GetArg( 2 );
716 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
717 if ( hyp.IsNull() || hyp->IsAlgo() )
719 if ( !hyp->Addition2Creation( addCmd, this->GetID() ))
721 AddMeshAccess( addCmd );
722 // add addition result treatement cmd
723 addErrorTreatmentCmd( addCmd, false );
727 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
728 for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
729 Handle(_pyCommand) subCmd = *cmd;
730 if ( subCmd->GetNbArgs() > 0 )
731 AddMeshAccess( subCmd );
733 myAddHypCmds.clear();
737 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
738 for ( ; hyp != myHypos.end(); ++hyp )
742 //================================================================================
744 * \brief _pyHypothesis constructor
745 * \param theCreationCmd -
747 //================================================================================
749 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
750 _pyObject( theCreationCmd )
752 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
755 //================================================================================
757 * \brief Creates algorithm or hypothesis
758 * \param theCreationCmd - The engine command creating a hypothesis
759 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
761 //================================================================================
763 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
765 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
766 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
768 Handle(_pyHypothesis) hyp, algo;
771 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
772 if ( hypTypeQuoted.IsEmpty() )
775 TCollection_AsciiString hypType =
776 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
778 algo = new _pyAlgorithm( theCreationCmd );
779 hyp = new _pyHypothesis( theCreationCmd );
781 // 1D Regular_1D ----------
782 if ( hypType == "Regular_1D" ) {
783 // set mesh's method creating algo,
784 // i.e. convertion result will be "regular1d = Mesh.Segment()",
785 // and set hypType by which algo creating a hypothesis is searched for
786 algo->SetConvMethodAndType("Segment", hypType.ToCString());
788 else if ( hypType == "CompositeSegment_1D" ) {
789 algo->SetConvMethodAndType("Segment", "Regular_1D");
790 algo->myArgs.Append( "algo=smesh.COMPOSITE");
792 else if ( hypType == "LocalLength" ) {
793 // set algo's method creating hyp, and algo type
794 hyp->SetConvMethodAndType( "LocalLength", "Regular_1D");
795 // set method whose 1 arg will become the 1-st arg of hyp creation command
796 // i.e. convertion result will be "locallength = regular1d.LocalLength(<arg of SetLength()>)"
797 hyp->AddArgMethod( "SetLength" );
799 else if ( hypType == "NumberOfSegments" ) {
800 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
801 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
802 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
803 hyp->AddArgMethod( "SetNumberOfSegments" );
804 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
805 hyp->AddArgMethod( "SetScaleFactor" );
807 else if ( hypType == "Arithmetic1D" ) {
808 hyp = new _pyComplexParamHypo( theCreationCmd );
809 hyp->SetConvMethodAndType( "Arithmetic1D", "Regular_1D");
811 else if ( hypType == "StartEndLength" ) {
812 hyp = new _pyComplexParamHypo( theCreationCmd );
813 hyp->SetConvMethodAndType( "StartEndLength", "Regular_1D");
815 else if ( hypType == "Deflection1D" ) {
816 hyp->SetConvMethodAndType( "Deflection1D", "Regular_1D");
817 hyp->AddArgMethod( "SetDeflection" );
819 else if ( hypType == "Propagation" ) {
820 hyp->SetConvMethodAndType( "Propagation", "Regular_1D");
822 else if ( hypType == "QuadraticMesh" ) {
823 hyp->SetConvMethodAndType( "QuadraticMesh", "Regular_1D");
825 else if ( hypType == "AutomaticLength" ) {
826 hyp->SetConvMethodAndType( "AutomaticLength", "Regular_1D");
827 hyp->AddArgMethod( "SetFineness");
829 else if ( hypType == "SegmentLengthAroundVertex" ) {
830 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
831 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
832 hyp->AddArgMethod( "SetLength" );
834 // 1D Python_1D ----------
835 else if ( hypType == "Python_1D" ) {
836 algo->SetConvMethodAndType( "Segment", hypType.ToCString());
837 algo->myArgs.Append( "algo=smesh.PYTHON");
839 else if ( hypType == "PythonSplit1D" ) {
840 hyp->SetConvMethodAndType( "PythonSplit1D", "Python_1D");
841 hyp->AddArgMethod( "SetNumberOfSegments");
842 hyp->AddArgMethod( "SetPythonLog10RatioFunction");
845 else if ( hypType == "MEFISTO_2D" ) {
846 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
848 else if ( hypType == "MaxElementArea" ) {
849 hyp->SetConvMethodAndType( "MaxElementArea", "MEFISTO_2D");
850 hyp->AddArgMethod( "SetMaxElementArea");
852 else if ( hypType == "LengthFromEdges" ) {
853 hyp->SetConvMethodAndType( "LengthFromEdges", "MEFISTO_2D");
855 else if ( hypType == "Quadrangle_2D" ) {
856 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
858 else if ( hypType == "QuadranglePreference" ) {
859 hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
862 else if ( hypType == "NETGEN_3D") {
863 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
864 algo->myArgs.Append( "algo=smesh.NETGEN" );
866 else if ( hypType == "MaxElementVolume") {
867 hyp->SetConvMethodAndType( "MaxElementVolume", "NETGEN_3D");
868 hyp->AddArgMethod( "SetMaxElementVolume" );
870 else if ( hypType == "GHS3D_3D" ) {
871 algo->SetConvMethodAndType( "Tetrahedron", hypType.ToCString());
872 algo->myArgs.Append( "algo=smesh.GHS3D" );
874 else if ( hypType == "Hexa_3D" ) {
875 algo->SetConvMethodAndType( "Hexahedron", hypType.ToCString());
877 // Repetitive ---------
878 else if ( hypType == "Projection_1D" ) {
879 algo->SetConvMethodAndType( "Projection1D", hypType.ToCString());
881 else if ( hypType == "ProjectionSource1D" ) {
882 hyp->SetConvMethodAndType( "SourceEdge", "Projection_1D");
883 hyp->AddArgMethod( "SetSourceEdge");
884 hyp->AddArgMethod( "SetSourceMesh");
885 // 2 args of SetVertexAssociation() will become the 3-th and 4-th args of hyp creation command
886 hyp->AddArgMethod( "SetVertexAssociation", 2 );
888 else if ( hypType == "Projection_2D" ) {
889 algo->SetConvMethodAndType( "Projection2D", hypType.ToCString());
891 else if ( hypType == "ProjectionSource2D" ) {
892 hyp->SetConvMethodAndType( "SourceFace", "Projection_2D");
893 hyp->AddArgMethod( "SetSourceFace");
894 hyp->AddArgMethod( "SetSourceMesh");
895 hyp->AddArgMethod( "SetVertexAssociation", 4 );
897 else if ( hypType == "Projection_3D" ) {
898 algo->SetConvMethodAndType( "Projection3D", hypType.ToCString());
900 else if ( hypType == "ProjectionSource3D" ) {
901 hyp->SetConvMethodAndType( "SourceShape3D", "Projection_3D");
902 hyp->AddArgMethod( "SetSource3DShape");
903 hyp->AddArgMethod( "SetSourceMesh");
904 hyp->AddArgMethod( "SetVertexAssociation", 4 );
906 else if ( hypType == "Prism_3D" ) {
907 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
909 else if ( hypType == "RadialPrism_3D" ) {
910 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
912 else if ( hypType == "NumberOfLayers" ) {
913 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialPrism_3D");
914 hyp->AddArgMethod( "SetNumberOfLayers" );
916 else if ( hypType == "LayerDistribution" ) {
917 hyp = new _pyLayerDistributionHypo( theCreationCmd );
918 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
921 if ( !algo->GetCreationMethod().IsEmpty() ) {
927 //================================================================================
929 * \brief Convert the command adding a hypothesis to mesh into a smesh command
930 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
931 * \param theAlgo - The algo that can create this hypo
932 * \retval bool - false if the command cant be converted
934 //================================================================================
936 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
937 const _pyID& theMesh)
939 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
941 if ( !IsWrappable( theMesh ))
944 myGeom = theCmd->GetArg( 1 );
946 Handle(_pyHypothesis) algo;
948 // find algo created on myGeom in theMesh
949 algo = theGen->FindAlgo( myGeom, theMesh, GetType() );
952 algo->GetCreationCmd()->AddDependantCmd( theCmd );
956 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
957 theCmd->SetResultValue( GetID() );
958 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
959 theCmd->SetMethod( myCreationMethod );
961 theCmd->RemoveArgs();
962 for ( int i = 1; i <= myArgs.Length(); ++i ) {
963 if ( !myArgs( i ).IsEmpty() )
964 theCmd->SetArg( i, myArgs( i ));
966 theCmd->SetArg( i, "[]");
968 // set a new creation command
969 GetCreationCmd()->Clear();
970 SetCreationCmd( theCmd );
972 // clear commands setting arg values
973 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
974 for ( ; argCmd != myArgCommands.end(); ++argCmd )
977 // set unknown arg commands after hypo creation
978 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
979 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
980 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
981 afterCmd->AddDependantCmd( *cmd );
987 //================================================================================
989 * \brief Remember hypothesis parameter values
990 * \param theCommand - The called hypothesis method
992 //================================================================================
994 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
999 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
1000 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
1001 while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
1002 myArgs.Append( "[]" );
1003 for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
1004 myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
1005 myArgCommands.push_back( theCommand );
1008 nbArgs += myNbArgsByMethod( i );
1010 myUnknownCommands.push_back( theCommand );
1013 //================================================================================
1015 * \brief Finish conversion
1017 //================================================================================
1019 void _pyHypothesis::Flush()
1021 if ( IsWrapped() ) {
1024 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1025 for ( ; cmd != myArgCommands.end(); ++cmd ) {
1026 // Add access to a wrapped mesh
1027 theGen->AddMeshAccessorMethod( *cmd );
1028 // Add access to a wrapped algorithm
1029 theGen->AddAlgoAccessorMethod( *cmd );
1031 cmd = myUnknownCommands.begin();
1032 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1033 // Add access to a wrapped mesh
1034 theGen->AddMeshAccessorMethod( *cmd );
1035 // Add access to a wrapped algorithm
1036 theGen->AddAlgoAccessorMethod( *cmd );
1039 // forget previous hypothesis modifications
1040 myArgCommands.clear();
1041 myUnknownCommands.clear();
1044 //================================================================================
1046 * \brief clear creation, arg and unkown commands
1048 //================================================================================
1050 void _pyHypothesis::ClearAllCommands()
1052 GetCreationCmd()->Clear();
1053 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1054 for ( ; cmd != myArgCommands.end(); ++cmd )
1056 cmd = myUnknownCommands.begin();
1057 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1061 //================================================================================
1063 * \brief Remember hypothesis parameter values
1064 * \param theCommand - The called hypothesis method
1066 //================================================================================
1068 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1070 // ex: hyp.SetLength(start, 1)
1071 // hyp.SetLength(end, 0)
1072 ASSERT(( theCommand->GetMethod() == "SetLength" ));
1073 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1074 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1075 while ( myArgs.Length() < i )
1076 myArgs.Append( "[]" );
1077 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1078 myArgCommands.push_back( theCommand );
1081 //================================================================================
1083 * \brief Convert methods of 1D hypotheses to my own methods
1084 * \param theCommand - The called hypothesis method
1086 //================================================================================
1088 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1090 if ( theCommand->GetMethod() != "SetLayerDistribution" )
1093 _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1095 const _pyID& hyp1dID = theCommand->GetArg( 1 );
1096 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1097 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1099 else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1100 // 1D hypo is already set, so distribution changes and the old
1101 // 1D hypo is thrown away
1102 my1dHyp->ClearAllCommands();
1105 if ( my1dHyp.IsNull() )
1106 return; // something wrong :(
1108 // make a new name for 1D hyp = "HypType" + "_Distribution"
1109 if ( my1dHyp->GetCreationCmd()->GetMethod() == "CreateHypothesis" ) {
1110 // not yet converted creation cmd
1111 TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1112 TCollection_AsciiString hypType = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1113 newName = hypType + "_Distribution";
1114 my1dHyp->GetCreationCmd()->SetResultValue( newName );
1117 // already converted creation cmd
1118 newName = my1dHyp->GetCreationCmd()->GetResultValue();
1121 // as creation of 1D hyp was written later then it's edition,
1122 // we need to find all it's edition calls and process them
1123 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1124 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1125 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1126 const _pyID& objID = (*cmdIt)->GetObject();
1127 if ( objID == hyp1dID ) {
1128 my1dHyp->Process( *cmdIt );
1129 my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1130 ( *cmdIt )->SetObject( newName );
1133 if ( !myArgCommands.empty() )
1134 myArgCommands.front()->Clear();
1135 theCommand->SetArg( 1, newName );
1136 myArgCommands.push_back( theCommand );
1137 // copy hyp1d's creation method and args
1138 // myCreationMethod = hyp1d->GetCreationMethod();
1139 // myArgs = hyp1d->GetArgs();
1140 // // make them cleared at conversion
1141 // myArgCommands = hyp1d->GetArgCommands();
1143 // // to be cleared at convertion only
1144 // myArgCommands.push_back( theCommand );
1147 //================================================================================
1150 * \param theAdditionCmd -
1154 //================================================================================
1156 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1157 const _pyID& theMesh)
1159 myIsWrapped = false;
1161 if ( my1dHyp.IsNull() )
1164 // set "SetLayerDistribution()" after addition cmd
1165 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1167 _pyID geom = theAdditionCmd->GetArg( 1 );
1169 my1dHyp->SetMesh( theMesh );
1170 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1173 // clear "SetLayerDistribution()" cmd
1174 myArgCommands.front()->Clear();
1176 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1178 // find RadialPrism algo created on <geom> for theMesh
1179 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this->GetType() );
1180 if ( !algo.IsNull() ) {
1181 GetCreationCmd()->SetObject( algo->GetID() );
1182 GetCreationCmd()->SetMethod( "Get3DHypothesis" );
1183 GetCreationCmd()->RemoveArgs();
1184 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1190 //================================================================================
1194 //================================================================================
1196 void _pyLayerDistributionHypo::Flush()
1198 //my1dHyp.Nullify();
1199 //_pyHypothesis::Flush();
1202 //================================================================================
1204 * \brief additionally to Addition2Creation, clears SetDistrType() command
1205 * \param theCmd - AddHypothesis() command
1206 * \param theMesh - mesh to which a hypothesis is added
1207 * \retval bool - convertion result
1209 //================================================================================
1211 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1212 const _pyID& theMesh)
1214 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1215 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1216 bool scaleDistrType = false;
1217 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1218 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1219 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1220 if ( (*cmd)->GetArg( 1 ) == "1" ) {
1221 scaleDistrType = true;
1224 else if ( !scaleDistrType ) {
1225 // distribution type changed: remove scale factor from args
1226 myArgs.Remove( 2, myArgs.Length() );
1232 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1235 //================================================================================
1237 * \brief remove repeated commands defining distribution
1239 //================================================================================
1241 void _pyNumberOfSegmentsHyp::Flush()
1243 // find number of the last SetDistrType() command
1244 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1245 int distrTypeNb = 0;
1246 for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1247 if ( (*cmd)->GetMethod() == "SetDistrType" )
1248 distrTypeNb = (*cmd)->GetOrderNb();
1250 // clear commands before the last SetDistrType()
1251 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1252 for ( int i = 0; i < 2; ++i ) {
1253 set<TCollection_AsciiString> uniqueMethods;
1254 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1255 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1257 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1258 const TCollection_AsciiString& method = (*cmd)->GetMethod();
1259 if ( !clear || method == "SetNumberOfSegments" ) {
1260 bool isNewInSet = uniqueMethods.insert( method ).second;
1261 clear = !isNewInSet;
1270 //================================================================================
1272 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
1273 * into regular1D.LengthNearVertex( length, vertex )
1274 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
1275 * \param theMesh - The mesh needing this hypo
1276 * \retval bool - false if the command cant be converted
1278 //================================================================================
1280 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1281 const _pyID& theMeshID)
1283 if ( IsWrappable( theMeshID )) {
1285 _pyID vertex = theCmd->GetArg( 1 );
1287 // the problem here is that segment algo will not be found
1288 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
1289 // geometry where segment algorithm is assigned
1290 Handle(_pyHypothesis) algo;
1291 _pyID geom = vertex;
1292 while ( algo.IsNull() && !geom.IsEmpty()) {
1293 // try to find geom as a father of <vertex>
1294 geom = FatherID( geom );
1295 algo = theGen->FindAlgo( geom, theMeshID, GetType() );
1297 if ( algo.IsNull() )
1298 return false; // also possible to find geom as brother of veretex...
1299 // set geom instead of vertex
1300 theCmd->SetArg( 1, geom );
1302 // set vertex as a second arg
1303 if ( myArgs.Length() < 1) myArgs.Append( "1" ); // :(
1304 myArgs.Append( vertex );
1306 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
1307 // theMeshID.LengthNearVertex( length, vertex )
1308 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
1313 //================================================================================
1315 * \brief _pyAlgorithm constructor
1316 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1318 //================================================================================
1320 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1321 : _pyHypothesis( theCreationCmd )
1326 //================================================================================
1328 * \brief Convert the command adding an algorithm to mesh
1329 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1330 * \param theMesh - The mesh needing this algo
1331 * \retval bool - false if the command cant be converted
1333 //================================================================================
1335 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1336 const _pyID& theMeshID)
1338 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1339 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1340 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1346 //================================================================================
1348 * \brief Return starting position of a part of python command
1349 * \param thePartIndex - The index of command part
1350 * \retval int - Part position
1352 //================================================================================
1354 int _pyCommand::GetBegPos( int thePartIndex )
1358 if ( myBegPos.Length() < thePartIndex )
1360 return myBegPos( thePartIndex );
1363 //================================================================================
1365 * \brief Store starting position of a part of python command
1366 * \param thePartIndex - The index of command part
1367 * \param thePosition - Part position
1369 //================================================================================
1371 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1373 while ( myBegPos.Length() < thePartIndex )
1374 myBegPos.Append( UNKNOWN );
1375 myBegPos( thePartIndex ) = thePosition;
1378 //================================================================================
1380 * \brief Returns whitespace symbols at the line beginning
1381 * \retval TCollection_AsciiString - result
1383 //================================================================================
1385 TCollection_AsciiString _pyCommand::GetIndentation()
1388 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1389 GetWord( myString, end, true );
1391 end = GetBegPos( RESULT_IND );
1392 return myString.SubString( 1, end - 1 );
1395 //================================================================================
1397 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1398 * \retval const TCollection_AsciiString & - ResultValue substring
1400 //================================================================================
1402 const TCollection_AsciiString & _pyCommand::GetResultValue()
1404 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1406 int begPos = myString.Location( "=", 1, Length() );
1408 myRes = GetWord( myString, begPos, false );
1411 SetBegPos( RESULT_IND, begPos );
1416 //================================================================================
1418 * \brief Return substring of python command looking like ResVal = Object.Meth()
1419 * \retval const TCollection_AsciiString & - Object substring
1421 //================================================================================
1423 const TCollection_AsciiString & _pyCommand::GetObject()
1425 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1428 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1430 begPos = myString.Location( "=", 1, Length() ) + 1;
1432 myObj = GetWord( myString, begPos, true );
1433 SetBegPos( OBJECT_IND, begPos );
1439 //================================================================================
1441 * \brief Return substring of python command looking like ResVal = Obj.Method()
1442 * \retval const TCollection_AsciiString & - Method substring
1444 //================================================================================
1446 const TCollection_AsciiString & _pyCommand::GetMethod()
1448 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1451 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1452 bool forward = true;
1454 begPos = myString.Location( "(", 1, Length() ) - 1;
1458 myMeth = GetWord( myString, begPos, forward );
1459 SetBegPos( METHOD_IND, begPos );
1465 //================================================================================
1467 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1468 * \retval const TCollection_AsciiString & - Arg<index> substring
1470 //================================================================================
1472 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1474 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1477 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1479 begPos = myString.Location( "(", 1, Length() ) + 1;
1481 int i = 0, prevLen = 0;
1482 while ( begPos != EMPTY ) {
1484 // check if we are looking at the closing parenthesis
1485 while ( begPos <= Length() && isspace( myString.Value( begPos )))
1487 if ( begPos > Length() || myString.Value( begPos ) == ')' )
1489 myArgs.Append( GetWord( myString, begPos, true, true ));
1490 SetBegPos( ARG1_IND + i, begPos );
1491 prevLen = myArgs.Last().Length();
1493 myArgs.Remove( myArgs.Length() ); // no more args
1497 if ( myArgs.Length() < index )
1498 return theEmptyString;
1499 return myArgs( index );
1502 //================================================================================
1504 * \brief Check if char is a word part
1505 * \param c - The character to check
1506 * \retval bool - The check result
1508 //================================================================================
1510 static inline bool isWord(const char c, const bool dotIsWord)
1513 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1516 //================================================================================
1518 * \brief Looks for a word in the string and returns word's beginning
1519 * \param theString - The input string
1520 * \param theStartPos - The position to start the search, returning word's beginning
1521 * \param theForward - The search direction
1522 * \retval TCollection_AsciiString - The found word
1524 //================================================================================
1526 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1528 const bool theForward,
1529 const bool dotIsWord )
1531 int beg = theStartPos, end = theStartPos;
1532 theStartPos = EMPTY;
1533 if ( beg < 1 || beg > theString.Length() )
1534 return theEmptyString;
1536 if ( theForward ) { // search forward
1538 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1540 if ( beg > theString.Length() )
1541 return theEmptyString; // no word found
1544 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1548 else { // search backward
1550 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1553 return theEmptyString; // no word found
1555 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1560 //cout << theString << " ---- " << beg << " - " << end << endl;
1561 return theString.SubString( beg, end );
1564 //================================================================================
1566 * \brief Look for position where not space char is
1567 * \param theString - The string
1568 * \param thePos - The position to search from and which returns result
1569 * \retval bool - false if there are only space after thePos in theString
1573 //================================================================================
1575 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1577 if ( thePos < 1 || thePos > theString.Length() )
1580 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1583 return thePos <= theString.Length();
1586 //================================================================================
1588 * \brief Modify a part of the command
1589 * \param thePartIndex - The index of the part
1590 * \param thePart - The new part string
1591 * \param theOldPart - The old part
1593 //================================================================================
1595 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1596 TCollection_AsciiString& theOldPart)
1598 int pos = GetBegPos( thePartIndex );
1599 if ( pos <= Length() && theOldPart != thePart)
1601 TCollection_AsciiString seperator;
1603 pos = GetBegPos( thePartIndex + 1 );
1604 if ( pos < 1 ) return;
1605 switch ( thePartIndex ) {
1606 case RESULT_IND: seperator = " = "; break;
1607 case OBJECT_IND: seperator = "."; break;
1608 case METHOD_IND: seperator = "()"; break;
1612 myString.Remove( pos, theOldPart.Length() );
1613 if ( !seperator.IsEmpty() )
1614 myString.Insert( pos , seperator );
1615 myString.Insert( pos, thePart );
1616 // update starting positions of the following parts
1617 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1618 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1619 if ( myBegPos( i ) > 0 )
1620 myBegPos( i ) += posDelta;
1622 theOldPart = thePart;
1626 //================================================================================
1628 * \brief Set agrument
1629 * \param index - The argument index, it counts from 1
1630 * \param theArg - The argument string
1632 //================================================================================
1634 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1637 int argInd = ARG1_IND + index - 1;
1638 int pos = GetBegPos( argInd );
1639 if ( pos < 1 ) // no index-th arg exist, append inexistent args
1641 // find a closing parenthesis
1642 if ( int lastArgInd = GetNbArgs() ) {
1643 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
1644 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
1649 while ( pos > 0 && myString.Value( pos ) != ')' )
1652 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
1656 while ( myArgs.Length() < index ) {
1657 if ( myArgs.Length() )
1658 myString.Insert( pos++, "," );
1659 myArgs.Append("None");
1660 myString.Insert( pos, myArgs.Last() );
1661 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1662 pos += myArgs.Last().Length();
1665 SetPart( argInd, theArg, myArgs( index ));
1668 //================================================================================
1670 * \brief Empty arg list
1672 //================================================================================
1674 void _pyCommand::RemoveArgs()
1676 if ( int pos = myString.Location( '(', 1, Length() ))
1677 myString.Trunc( pos );
1680 if ( myBegPos.Length() >= ARG1_IND )
1681 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1684 //================================================================================
1686 * \brief Set dependent commands after this one
1688 //================================================================================
1690 bool _pyCommand::SetDependentCmdsAfter() const
1692 bool orderChanged = false;
1693 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1694 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1695 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1696 orderChanged = true;
1697 theGen->SetCommandAfter( *cmd, this );
1698 (*cmd)->SetDependentCmdsAfter();
1701 return orderChanged;
1703 //================================================================================
1705 * \brief Insert accessor method after theObjectID
1706 * \param theObjectID - id of the accessed object
1707 * \param theAcsMethod - name of the method giving access to the object
1708 * \retval bool - false if theObjectID is not found in the command string
1710 //================================================================================
1712 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
1714 if ( !theAcsMethod )
1716 // start object search from the object, i.e. ignore result
1718 int beg = GetBegPos( OBJECT_IND );
1719 if ( beg < 1 || beg > Length() )
1721 while (( beg = myString.Location( theObjectID, beg, Length() )))
1723 // check that theObjectID is not just a part of a longer ID
1724 int afterEnd = beg + theObjectID.Length();
1725 Standard_Character c = myString.Value( afterEnd );
1726 if ( !isalnum( c ) && c != ':' ) {
1727 // check if accessor method already present
1729 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
1731 int oldLen = Length();
1732 myString.Insert( afterEnd, (char*) theAcsMethod );
1733 myString.Insert( afterEnd, "." );
1734 // update starting positions of the parts following the modified one
1735 int posDelta = Length() - oldLen;
1736 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
1737 if ( myBegPos( i ) > afterEnd )
1738 myBegPos( i ) += posDelta;
1743 beg = afterEnd; // is a part - next search
1748 //================================================================================
1750 * \brief Return method name giving access to an interaface object wrapped by python class
1751 * \retval const char* - method name
1753 //================================================================================
1755 const char* _pyObject::AccessorMethod() const
1759 //================================================================================
1761 * \brief Return ID of a father
1763 //================================================================================
1765 _pyID _pyObject::FatherID(const _pyID & childID)
1767 int colPos = childID.SearchFromEnd(':');
1769 return childID.SubString( 1, colPos-1 );