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 "utilities.h"
36 #include "SMESH_PythonDump.hxx"
37 #include "Resource_DataMapOfAsciiStringAsciiString.hxx"
39 #include "SMESH_Gen_i.hxx"
40 /* SALOME headers that include CORBA headers that include windows.h
41 * that defines GetObject symbol as GetObjectA should stand before SALOME headers
42 * that declare methods named GetObject - to apply the same rules of GetObject renaming
43 * and thus to avoid mess with GetObject symbol on Windows */
45 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
46 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
47 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
48 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
49 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
50 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
51 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
52 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
54 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
55 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
56 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
57 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
58 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
59 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
60 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
61 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
62 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
63 IMPLEMENT_STANDARD_RTTIEXT(_pySegmentLengthAroundVertexHyp,_pyHypothesis);
66 using SMESH::TPythonDump;
69 * \brief Container of commands into which the initial script is split.
70 * It also contains data coresponding to SMESH_Gen contents
72 static Handle(_pyGen) theGen;
74 static TCollection_AsciiString theEmptyString;
76 //#define DUMP_CONVERSION
78 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
79 #undef DUMP_CONVERSION
82 //================================================================================
84 * \brief Convert python script using commands of smesh.py
85 * \param theScript - Input script
86 * \retval TCollection_AsciiString - Convertion result
88 //================================================================================
90 TCollection_AsciiString
91 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
92 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
94 theGen = new _pyGen( theEntry2AccessorMethod );
96 // split theScript into separate commands
97 int from = 1, end = theScript.Length(), to;
98 while ( from < end && ( to = theScript.Location( "\n", from, end )))
101 // cut out and store a command
102 theGen->AddCommand( theScript.SubString( from, to - 1 ));
107 #ifdef DUMP_CONVERSION
108 cout << endl << " ######## RESULT ######## " << endl<< endl;
110 // reorder commands after conversion
111 list< Handle(_pyCommand) >::iterator cmd;
114 orderChanges = false;
115 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
116 if ( (*cmd)->SetDependentCmdsAfter() )
118 } while ( orderChanges );
120 // concat commands back into a script
121 TCollection_AsciiString aScript;
122 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
124 #ifdef DUMP_CONVERSION
125 cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
127 if ( !(*cmd)->IsEmpty() ) {
129 aScript += (*cmd)->GetString();
139 //================================================================================
141 * \brief _pyGen constructor
143 //================================================================================
145 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
146 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
147 myID2AccessorMethod( theEntry2AccessorMethod )
150 myHasPattern = false;
151 // make that GetID() to return TPythonDump::SMESHGenName()
152 GetCreationCmd()->GetString() += "=";
155 //================================================================================
157 * \brief Convert a command using a specific converter
158 * \param theCommand - the command to convert
160 //================================================================================
162 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
164 // store theCommand in the sequence
165 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
167 Handle(_pyCommand) aCommand = myCommands.back();
168 #ifdef DUMP_CONVERSION
169 cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
172 _pyID objID = aCommand->GetObject();
174 if ( objID.IsEmpty() )
178 if ( objID == this->GetID() ) {
179 this->Process( aCommand );
182 // SMESH_Mesh method?
183 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
184 if ( id_mesh != myMeshes.end() ) {
185 id_mesh->second->Process( aCommand );
188 // SMESH_Hypothesis method?
189 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
190 for ( ; hyp != myHypos.end(); ++hyp )
191 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
192 (*hyp)->Process( aCommand );
196 // Add access to a wrapped mesh
197 AddMeshAccessorMethod( aCommand );
199 // Add access to a wrapped algorithm
200 AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
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();
223 //================================================================================
225 * \brief Convert the command or remember it for later conversion
226 * \param theCommand - The python command calling a method of SMESH_Gen
228 //================================================================================
230 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
232 // there are methods to convert:
233 // CreateMesh( shape )
234 // Concatenate( [mesh1, ...], ... )
235 // CreateHypothesis( theHypType, theLibName )
236 // Compute( mesh, geom )
238 if ( theCommand->GetMethod() == "CreateMesh" ||
239 theCommand->GetMethod() == "CreateEmptyMesh" )
241 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
242 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
246 // CreateHypothesis()
247 if ( theCommand->GetMethod() == "CreateHypothesis" )
249 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
253 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
254 if ( theCommand->GetMethod() == "Compute" )
256 const _pyID& meshID = theCommand->GetArg( 1 );
257 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
258 if ( id_mesh != myMeshes.end() ) {
259 theCommand->SetObject( meshID );
260 theCommand->RemoveArgs();
261 id_mesh->second->Flush();
266 // leave only one smeshgen.GetPattern() in the script
267 if ( theCommand->GetMethod() == "GetPattern" ) {
268 if ( myHasPattern ) {
275 // smeshgen.Method() --> smesh.smesh.Method()
276 theCommand->SetObject( SMESH_2smeshpy::GenName() );
278 // Concatenate( [mesh1, ...], ... )
279 if ( theCommand->GetMethod() == "Concatenate" )
281 AddMeshAccessorMethod( theCommand );
285 //================================================================================
287 * \brief Convert the remembered commands
289 //================================================================================
293 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
294 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
295 if ( ! id_mesh->second.IsNull() )
296 id_mesh->second->Flush();
298 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
299 for ( ; hyp != myHypos.end(); ++hyp )
300 if ( !hyp->IsNull() ) {
302 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
303 if ( !(*hyp)->IsWrapped() )
304 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
308 //================================================================================
310 * \brief Add access method to mesh that is an object or an argument
311 * \param theCmd - command to add access method
312 * \retval bool - true if added
314 //================================================================================
316 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
319 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
320 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
321 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
327 //================================================================================
329 * \brief Add access method to algo that is an object or an argument
330 * \param theCmd - command to add access method
331 * \retval bool - true if added
333 //================================================================================
335 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
338 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
339 for ( ; hyp != myHypos.end(); ++hyp ) {
340 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
341 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
347 //================================================================================
349 * \brief Find hypothesis by ID (entry)
350 * \param theHypID - The hypothesis ID
351 * \retval Handle(_pyHypothesis) - The found hypothesis
353 //================================================================================
355 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
357 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
358 for ( ; hyp != myHypos.end(); ++hyp )
359 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
361 return Handle(_pyHypothesis)();
364 //================================================================================
366 * \brief Find algorithm the created algorithm
367 * \param theGeom - The shape ID the algorithm was created on
368 * \param theMesh - The mesh ID that created the algorithm
369 * \param dim - The algo dimension
370 * \retval Handle(_pyHypothesis) - The found algo
372 //================================================================================
374 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
375 const TCollection_AsciiString& theAlgoType )
377 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
378 for ( ; hyp != myHypos.end(); ++hyp )
379 if ( !hyp->IsNull() &&
381 (*hyp)->GetType() == theAlgoType &&
382 (*hyp)->GetGeom() == theGeom &&
383 (*hyp)->GetMesh() == theMesh )
388 //================================================================================
390 * \brief Change order of commands in the script
391 * \param theCmd1 - One command
392 * \param theCmd2 - Another command
394 //================================================================================
396 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
398 list< Handle(_pyCommand) >::iterator pos1, pos2;
399 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
400 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
401 myCommands.insert( pos1, theCmd2 );
402 myCommands.insert( pos2, theCmd1 );
403 myCommands.erase( pos1 );
404 myCommands.erase( pos2 );
406 int nb1 = theCmd1->GetOrderNb();
407 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
408 theCmd2->SetOrderNb( nb1 );
409 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
410 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
413 //================================================================================
415 * \brief Set one command after the other
416 * \param theCmd - Command to move
417 * \param theAfterCmd - Command ater which to insert the first one
419 //================================================================================
421 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
424 //cout << "SET\t" << theAfterCmd->GetString() << endl << "BEFORE\t" << theCmd->GetString() << endl<<endl;
426 list< Handle(_pyCommand) >::iterator pos;
427 pos = find( myCommands.begin(), myCommands.end(), theCmd );
428 myCommands.erase( pos );
429 pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
430 myCommands.insert( ++pos, theCmd );
433 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
434 (*pos)->SetOrderNb( i++ );
437 //================================================================================
439 * \brief Set method to access to object wrapped with python class
440 * \param theID - The wrapped object entry
441 * \param theMethod - The accessor method
443 //================================================================================
445 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
447 myID2AccessorMethod.Bind( theID, (char*) theMethod );
450 //================================================================================
452 * \brief Find out type of geom group
453 * \param grpID - The geom group entry
454 * \retval int - The type
456 //================================================================================
458 static bool sameGroupType( const _pyID& grpID,
459 const TCollection_AsciiString& theType)
461 // define group type as smesh.Mesh.Group() does
463 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
464 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
465 if ( !aSObj->_is_nil() ) {
466 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
467 if ( !aGeomObj->_is_nil() ) {
468 switch ( aGeomObj->GetShapeType() ) {
469 case GEOM::VERTEX: type = SMESH::NODE; break;
470 case GEOM::EDGE: type = SMESH::EDGE; break;
471 case GEOM::FACE: type = SMESH::FACE; break;
473 case GEOM::SHELL: type = SMESH::VOLUME; break;
474 case GEOM::COMPOUND: {
475 GEOM::GEOM_Gen_var aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
476 if ( !aGeomGen->_is_nil() ) {
477 GEOM::GEOM_IGroupOperations_var aGrpOp =
478 aGeomGen->GetIGroupOperations( study->StudyId() );
479 if ( !aGrpOp->_is_nil() ) {
480 switch ( aGrpOp->GetType( aGeomObj )) {
481 case TopAbs_VERTEX: type = SMESH::NODE; break;
482 case TopAbs_EDGE: type = SMESH::EDGE; break;
483 case TopAbs_FACE: type = SMESH::FACE; break;
484 case TopAbs_SOLID: type = SMESH::VOLUME; break;
495 MESSAGE("Type of the group " << grpID << " not found");
498 if ( theType.IsIntegerValue() )
499 return type == theType.IntegerValue();
502 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
503 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
504 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
505 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
511 //================================================================================
514 * \param theCreationCmd -
516 //================================================================================
518 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd):
519 _pyObject(theCreationCmd), myHasEditor(false)
521 // convert my creation command
522 Handle(_pyCommand) creationCmd = GetCreationCmd();
523 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
524 creationCmd->SetMethod( "Mesh" );
526 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
529 //================================================================================
531 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
532 * \param theCommand - Engine method called for this mesh
534 //================================================================================
536 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
538 // smesh.py wraps the following methods:
540 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
541 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
542 // 2. AddHypothesis(geom, hyp)
543 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
544 // 3. CreateGroupFromGEOM(type, name, grp)
545 // --> in Mesh.Group(grp, name="")
546 // 4. ExportToMED(f, opt, version)
547 // --> in Mesh.ExportToMED( f, version, opt=0 )
548 // 5. ExportMED(f, opt)
549 // --> in Mesh.ExportMED( f,opt=0 )
551 // --> in Mesh.ExportDAT( f )
553 // --> in Mesh.ExportUNV(f)
554 // 8. ExportSTL(f, ascii)
555 // --> in Mesh.ExportSTL(f, ascii=1)
557 const TCollection_AsciiString method = theCommand->GetMethod();
558 if ( method == "GetSubMesh" ) {
559 mySubmeshes.push_back( theCommand );
561 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
562 myAddHypCmds.push_back( theCommand );
564 const _pyID& hypID = theCommand->GetArg( 2 );
565 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
566 if ( !hyp.IsNull() ) {
567 myHypos.push_back( hyp );
568 if ( hyp->GetMesh().IsEmpty() )
569 hyp->SetMesh( this->GetID() );
572 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
573 _pyID grp = theCommand->GetArg( 3 );
574 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
575 theCommand->SetMethod( "Group" );
576 theCommand->RemoveArgs();
577 theCommand->SetArg( 1, grp );
580 AddMeshAccess( theCommand );
583 else if ( method == "ExportToMED" ) {//(f, opt, version)
584 // --> (f, version, opt)
585 _pyID opt = theCommand->GetArg( 2 );
586 _pyID ver = theCommand->GetArg( 3 );
587 theCommand->SetArg( 2, ver );
588 theCommand->SetArg( 3, opt );
590 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
592 const _pyID & hypID = theCommand->GetArg( 2 );
594 // check if this mesh still has corresponding addition command
595 bool hasAddCmd = false;
596 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
597 while ( cmd != myAddHypCmds.end() )
599 // AddHypothesis(geom, hyp)
600 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
603 cmd = myAddHypCmds.erase( cmd );
610 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
611 if ( ! hasAddCmd ) { // hypo addition already wrapped
612 // access to wrapped mesh
613 AddMeshAccess( theCommand );
614 // access to wrapped algo
615 if ( !hyp.IsNull() && hyp->IsAlgo() && hyp->IsWrapped() )
616 theCommand->SetArg( 2, theCommand->GetArg( 2 ) + ".GetAlgorithm()" );
618 // remove hyp from myHypos
619 myHypos.remove( hyp );
622 // leave only one " mesh_editor_<nb> = mesh.GetMeshEditor()"
623 else if ( theCommand->GetMethod() == "GetMeshEditor")
628 AddMeshAccess( theCommand );
632 // apply theCommand to the mesh wrapped by smeshpy mesh
635 AddMeshAccess( theCommand );
641 //================================================================================
643 * \brief add addition result treatement command
644 * \param addCmd - hypothesis addition command
646 //================================================================================
648 void addErrorTreatmentCmd( Handle(_pyCommand) & addCmd,
651 return; // TO DEBUD - TreatHypoStatus() is not placed right after addCmd
652 // addCmd: status = mesh.AddHypothesis( geom, hypo )
653 // treatement command:
654 // def TreatHypoStatus(status, hypName, geomName, isAlgo):
655 TCollection_AsciiString status = addCmd->GetResultValue();
656 if ( !status.IsEmpty() ) {
657 const _pyID& geomID = addCmd->GetArg( 1 );
658 const _pyID& hypoID = addCmd->GetArg( 2 );
659 TCollection_AsciiString cmdStr = addCmd->GetIndentation() +
660 SMESH_2smeshpy::SmeshpyName() + ".TreatHypoStatus( " + status + ", " +
661 SMESH_2smeshpy::SmeshpyName() + ".GetName(" + hypoID + "), " +
662 SMESH_2smeshpy::SmeshpyName() + ".GetName(" + geomID + "), " +
663 (char*)( isAlgo ? "True" : "False" ) + " )";
664 Handle(_pyCommand) cmd = theGen->AddCommand( cmdStr );
665 addCmd->AddDependantCmd( cmd, true );
670 //================================================================================
672 * \brief Convert creation and addition of all algos and hypos
674 //================================================================================
676 void _pyMesh::Flush()
678 list < Handle(_pyCommand) >::iterator cmd, cmd2;
680 // try to convert algo addition like this:
681 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
682 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
684 Handle(_pyCommand) addCmd = *cmd;
685 const _pyID& algoID = addCmd->GetArg( 2 );
686 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
687 if ( algo.IsNull() || !algo->IsAlgo() )
690 _pyID geom = addCmd->GetArg( 1 );
691 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
693 // wrapped algo is created atfer mesh creation
694 GetCreationCmd()->AddDependantCmd( addCmd );
696 if ( geom != GetGeom() ) // local algo
698 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
699 addCmd->SetArg( addCmd->GetNbArgs() + 1,
700 TCollection_AsciiString( "geom=" ) + geom );
701 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
702 for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
703 Handle(_pyCommand) subCmd = *cmd2;
704 if ( geom == subCmd->GetArg( 1 )) {
705 subCmd->SetObject( algo->GetID() );
706 subCmd->RemoveArgs();
707 addCmd->AddDependantCmd( subCmd );
712 else // ALGO was already created
714 // mesh.AddHypothesis(geom, ALGO ) --> mesh.GetMesh().AddHypothesis(geom, ALGO )
715 AddMeshAccess( addCmd );
716 // mesh.GetMesh().AddHypothesis(geom, ALGO ) ->
717 // mesh.GetMesh().AddHypothesis(geom, ALGO.GetAlgorithm() )
718 addCmd->SetArg( 2, addCmd->GetArg( 2 ) + ".GetAlgorithm()" );
719 // add addition result treatement cmd
720 addErrorTreatmentCmd( addCmd, true );
724 // try to convert hypo addition like this:
725 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
726 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
728 Handle(_pyCommand) addCmd = *cmd;
729 const _pyID& hypID = addCmd->GetArg( 2 );
730 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
731 if ( hyp.IsNull() || hyp->IsAlgo() )
733 if ( !hyp->Addition2Creation( addCmd, this->GetID() ))
735 AddMeshAccess( addCmd );
736 // add addition result treatement cmd
737 addErrorTreatmentCmd( addCmd, false );
741 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
742 for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
743 Handle(_pyCommand) subCmd = *cmd;
744 if ( subCmd->GetNbArgs() > 0 )
745 AddMeshAccess( subCmd );
747 myAddHypCmds.clear();
751 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
752 for ( ; hyp != myHypos.end(); ++hyp )
756 //================================================================================
758 * \brief _pyHypothesis constructor
759 * \param theCreationCmd -
761 //================================================================================
763 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
764 _pyObject( theCreationCmd )
766 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
769 //================================================================================
771 * \brief Creates algorithm or hypothesis
772 * \param theCreationCmd - The engine command creating a hypothesis
773 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
775 //================================================================================
777 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
779 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
780 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
782 Handle(_pyHypothesis) hyp, algo;
785 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
786 if ( hypTypeQuoted.IsEmpty() )
789 TCollection_AsciiString hypType =
790 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
792 algo = new _pyAlgorithm( theCreationCmd );
793 hyp = new _pyHypothesis( theCreationCmd );
795 // 1D Regular_1D ----------
796 if ( hypType == "Regular_1D" ) {
797 // set mesh's method creating algo,
798 // i.e. convertion result will be "regular1d = Mesh.Segment()",
799 // and set hypType by which algo creating a hypothesis is searched for
800 algo->SetConvMethodAndType("Segment", hypType.ToCString());
802 else if ( hypType == "CompositeSegment_1D" ) {
803 algo->SetConvMethodAndType("Segment", "Regular_1D");
804 algo->myArgs.Append( "algo=smesh.COMPOSITE");
806 else if ( hypType == "LocalLength" ) {
807 // set algo's method creating hyp, and algo type
808 hyp->SetConvMethodAndType( "LocalLength", "Regular_1D");
809 // set method whose 1 arg will become the 1-st arg of hyp creation command
810 // i.e. convertion result will be "locallength = regular1d.LocalLength(<arg of SetLength()>)"
811 hyp->AddArgMethod( "SetLength" );
813 else if ( hypType == "NumberOfSegments" ) {
814 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
815 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
816 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
817 hyp->AddArgMethod( "SetNumberOfSegments" );
818 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
819 hyp->AddArgMethod( "SetScaleFactor" );
821 else if ( hypType == "Arithmetic1D" ) {
822 hyp = new _pyComplexParamHypo( theCreationCmd );
823 hyp->SetConvMethodAndType( "Arithmetic1D", "Regular_1D");
825 else if ( hypType == "StartEndLength" ) {
826 hyp = new _pyComplexParamHypo( theCreationCmd );
827 hyp->SetConvMethodAndType( "StartEndLength", "Regular_1D");
829 else if ( hypType == "Deflection1D" ) {
830 hyp->SetConvMethodAndType( "Deflection1D", "Regular_1D");
831 hyp->AddArgMethod( "SetDeflection" );
833 else if ( hypType == "Propagation" ) {
834 hyp->SetConvMethodAndType( "Propagation", "Regular_1D");
836 else if ( hypType == "QuadraticMesh" ) {
837 hyp->SetConvMethodAndType( "QuadraticMesh", "Regular_1D");
839 else if ( hypType == "AutomaticLength" ) {
840 hyp->SetConvMethodAndType( "AutomaticLength", "Regular_1D");
841 hyp->AddArgMethod( "SetFineness");
843 else if ( hypType == "SegmentLengthAroundVertex" ) {
844 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
845 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
846 hyp->AddArgMethod( "SetLength" );
848 // 1D Python_1D ----------
849 else if ( hypType == "Python_1D" ) {
850 algo->SetConvMethodAndType( "Segment", hypType.ToCString());
851 algo->myArgs.Append( "algo=smesh.PYTHON");
853 else if ( hypType == "PythonSplit1D" ) {
854 hyp->SetConvMethodAndType( "PythonSplit1D", "Python_1D");
855 hyp->AddArgMethod( "SetNumberOfSegments");
856 hyp->AddArgMethod( "SetPythonLog10RatioFunction");
859 else if ( hypType == "MEFISTO_2D" ) {
860 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
862 else if ( hypType == "MaxElementArea" ) {
863 hyp->SetConvMethodAndType( "MaxElementArea", "MEFISTO_2D");
864 hyp->AddArgMethod( "SetMaxElementArea");
866 else if ( hypType == "LengthFromEdges" ) {
867 hyp->SetConvMethodAndType( "LengthFromEdges", "MEFISTO_2D");
869 else if ( hypType == "Quadrangle_2D" ) {
870 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
872 else if ( hypType == "QuadranglePreference" ) {
873 hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
876 else if ( hypType == "NETGEN_3D") {
877 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
878 algo->myArgs.Append( "algo=smesh.NETGEN" );
880 else if ( hypType == "MaxElementVolume") {
881 hyp->SetConvMethodAndType( "MaxElementVolume", "NETGEN_3D");
882 hyp->AddArgMethod( "SetMaxElementVolume" );
884 else if ( hypType == "GHS3D_3D" ) {
885 algo->SetConvMethodAndType( "Tetrahedron", hypType.ToCString());
886 algo->myArgs.Append( "algo=smesh.GHS3D" );
888 else if ( hypType == "Hexa_3D" ) {
889 algo->SetConvMethodAndType( "Hexahedron", hypType.ToCString());
891 // Repetitive ---------
892 else if ( hypType == "Projection_1D" ) {
893 algo->SetConvMethodAndType( "Projection1D", hypType.ToCString());
895 else if ( hypType == "ProjectionSource1D" ) {
896 hyp->SetConvMethodAndType( "SourceEdge", "Projection_1D");
897 hyp->AddArgMethod( "SetSourceEdge");
898 hyp->AddArgMethod( "SetSourceMesh");
899 // 2 args of SetVertexAssociation() will become the 3-th and 4-th args of hyp creation command
900 hyp->AddArgMethod( "SetVertexAssociation", 2 );
902 else if ( hypType == "Projection_2D" ) {
903 algo->SetConvMethodAndType( "Projection2D", hypType.ToCString());
905 else if ( hypType == "ProjectionSource2D" ) {
906 hyp->SetConvMethodAndType( "SourceFace", "Projection_2D");
907 hyp->AddArgMethod( "SetSourceFace");
908 hyp->AddArgMethod( "SetSourceMesh");
909 hyp->AddArgMethod( "SetVertexAssociation", 4 );
911 else if ( hypType == "Projection_3D" ) {
912 algo->SetConvMethodAndType( "Projection3D", hypType.ToCString());
914 else if ( hypType == "ProjectionSource3D" ) {
915 hyp->SetConvMethodAndType( "SourceShape3D", "Projection_3D");
916 hyp->AddArgMethod( "SetSource3DShape");
917 hyp->AddArgMethod( "SetSourceMesh");
918 hyp->AddArgMethod( "SetVertexAssociation", 4 );
920 else if ( hypType == "Prism_3D" ) {
921 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
923 else if ( hypType == "RadialPrism_3D" ) {
924 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
926 else if ( hypType == "NumberOfLayers" ) {
927 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialPrism_3D");
928 hyp->AddArgMethod( "SetNumberOfLayers" );
930 else if ( hypType == "LayerDistribution" ) {
931 hyp = new _pyLayerDistributionHypo( theCreationCmd );
932 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
935 if ( !algo->GetCreationMethod().IsEmpty() ) {
941 //================================================================================
943 * \brief Convert the command adding a hypothesis to mesh into a smesh command
944 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
945 * \param theAlgo - The algo that can create this hypo
946 * \retval bool - false if the command cant be converted
948 //================================================================================
950 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
951 const _pyID& theMesh)
953 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
955 if ( !IsWrappable( theMesh ))
958 myGeom = theCmd->GetArg( 1 );
960 Handle(_pyHypothesis) algo;
962 // find algo created on myGeom in theMesh
963 algo = theGen->FindAlgo( myGeom, theMesh, GetType() );
966 algo->GetCreationCmd()->AddDependantCmd( theCmd );
970 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
971 theCmd->SetResultValue( GetID() );
972 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
973 theCmd->SetMethod( myCreationMethod );
975 theCmd->RemoveArgs();
976 for ( int i = 1; i <= myArgs.Length(); ++i ) {
977 if ( !myArgs( i ).IsEmpty() )
978 theCmd->SetArg( i, myArgs( i ));
980 theCmd->SetArg( i, "[]");
982 // set a new creation command
983 GetCreationCmd()->Clear();
984 SetCreationCmd( theCmd );
986 // clear commands setting arg values
987 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
988 for ( ; argCmd != myArgCommands.end(); ++argCmd )
991 // set unknown arg commands after hypo creation
992 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
993 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
994 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
995 afterCmd->AddDependantCmd( *cmd );
1001 //================================================================================
1003 * \brief Remember hypothesis parameter values
1004 * \param theCommand - The called hypothesis method
1006 //================================================================================
1008 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
1010 ASSERT( !myIsAlgo );
1013 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
1014 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
1015 while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
1016 myArgs.Append( "[]" );
1017 for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
1018 myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
1019 myArgCommands.push_back( theCommand );
1022 nbArgs += myNbArgsByMethod( i );
1024 myUnknownCommands.push_back( theCommand );
1027 //================================================================================
1029 * \brief Finish conversion
1031 //================================================================================
1033 void _pyHypothesis::Flush()
1035 if ( IsWrapped() ) {
1038 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1039 for ( ; cmd != myArgCommands.end(); ++cmd ) {
1040 // Add access to a wrapped mesh
1041 theGen->AddMeshAccessorMethod( *cmd );
1042 // Add access to a wrapped algorithm
1043 theGen->AddAlgoAccessorMethod( *cmd );
1045 cmd = myUnknownCommands.begin();
1046 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1047 // Add access to a wrapped mesh
1048 theGen->AddMeshAccessorMethod( *cmd );
1049 // Add access to a wrapped algorithm
1050 theGen->AddAlgoAccessorMethod( *cmd );
1053 // forget previous hypothesis modifications
1054 myArgCommands.clear();
1055 myUnknownCommands.clear();
1058 //================================================================================
1060 * \brief clear creation, arg and unkown commands
1062 //================================================================================
1064 void _pyHypothesis::ClearAllCommands()
1066 GetCreationCmd()->Clear();
1067 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1068 for ( ; cmd != myArgCommands.end(); ++cmd )
1070 cmd = myUnknownCommands.begin();
1071 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1075 //================================================================================
1077 * \brief Remember hypothesis parameter values
1078 * \param theCommand - The called hypothesis method
1080 //================================================================================
1082 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1084 // ex: hyp.SetLength(start, 1)
1085 // hyp.SetLength(end, 0)
1086 ASSERT(( theCommand->GetMethod() == "SetLength" ));
1087 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1088 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1089 while ( myArgs.Length() < i )
1090 myArgs.Append( "[]" );
1091 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1092 myArgCommands.push_back( theCommand );
1095 //================================================================================
1097 * \brief Convert methods of 1D hypotheses to my own methods
1098 * \param theCommand - The called hypothesis method
1100 //================================================================================
1102 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1104 if ( theCommand->GetMethod() != "SetLayerDistribution" )
1107 _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1109 const _pyID& hyp1dID = theCommand->GetArg( 1 );
1110 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1111 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1113 else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1114 // 1D hypo is already set, so distribution changes and the old
1115 // 1D hypo is thrown away
1116 my1dHyp->ClearAllCommands();
1119 if ( my1dHyp.IsNull() )
1120 return; // something wrong :(
1122 // make a new name for 1D hyp = "HypType" + "_Distribution"
1123 if ( my1dHyp->GetCreationCmd()->GetMethod() == "CreateHypothesis" ) {
1124 // not yet converted creation cmd
1125 TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1126 TCollection_AsciiString hypType = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1127 newName = hypType + "_Distribution";
1128 my1dHyp->GetCreationCmd()->SetResultValue( newName );
1131 // already converted creation cmd
1132 newName = my1dHyp->GetCreationCmd()->GetResultValue();
1135 // as creation of 1D hyp was written later then it's edition,
1136 // we need to find all it's edition calls and process them
1137 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1138 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1139 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1140 const _pyID& objID = (*cmdIt)->GetObject();
1141 if ( objID == hyp1dID ) {
1142 my1dHyp->Process( *cmdIt );
1143 my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1144 ( *cmdIt )->SetObject( newName );
1147 if ( !myArgCommands.empty() )
1148 myArgCommands.front()->Clear();
1149 theCommand->SetArg( 1, newName );
1150 myArgCommands.push_back( theCommand );
1151 // copy hyp1d's creation method and args
1152 // myCreationMethod = hyp1d->GetCreationMethod();
1153 // myArgs = hyp1d->GetArgs();
1154 // // make them cleared at conversion
1155 // myArgCommands = hyp1d->GetArgCommands();
1157 // // to be cleared at convertion only
1158 // myArgCommands.push_back( theCommand );
1161 //================================================================================
1164 * \param theAdditionCmd -
1168 //================================================================================
1170 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1171 const _pyID& theMesh)
1173 myIsWrapped = false;
1175 if ( my1dHyp.IsNull() )
1178 // set "SetLayerDistribution()" after addition cmd
1179 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1181 _pyID geom = theAdditionCmd->GetArg( 1 );
1183 my1dHyp->SetMesh( theMesh );
1184 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1187 // clear "SetLayerDistribution()" cmd
1188 myArgCommands.front()->Clear();
1190 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1192 // find RadialPrism algo created on <geom> for theMesh
1193 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this->GetType() );
1194 if ( !algo.IsNull() ) {
1195 GetCreationCmd()->SetObject( algo->GetID() );
1196 GetCreationCmd()->SetMethod( "Get3DHypothesis" );
1197 GetCreationCmd()->RemoveArgs();
1198 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1204 //================================================================================
1208 //================================================================================
1210 void _pyLayerDistributionHypo::Flush()
1212 //my1dHyp.Nullify();
1213 //_pyHypothesis::Flush();
1216 //================================================================================
1218 * \brief additionally to Addition2Creation, clears SetDistrType() command
1219 * \param theCmd - AddHypothesis() command
1220 * \param theMesh - mesh to which a hypothesis is added
1221 * \retval bool - convertion result
1223 //================================================================================
1225 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1226 const _pyID& theMesh)
1228 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1229 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1230 bool scaleDistrType = false;
1231 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1232 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1233 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1234 if ( (*cmd)->GetArg( 1 ) == "1" ) {
1235 scaleDistrType = true;
1238 else if ( !scaleDistrType ) {
1239 // distribution type changed: remove scale factor from args
1240 myArgs.Remove( 2, myArgs.Length() );
1246 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1249 //================================================================================
1251 * \brief remove repeated commands defining distribution
1253 //================================================================================
1255 void _pyNumberOfSegmentsHyp::Flush()
1257 // find number of the last SetDistrType() command
1258 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1259 int distrTypeNb = 0;
1260 for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1261 if ( (*cmd)->GetMethod() == "SetDistrType" )
1262 distrTypeNb = (*cmd)->GetOrderNb();
1264 // clear commands before the last SetDistrType()
1265 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1266 for ( int i = 0; i < 2; ++i ) {
1267 set<TCollection_AsciiString> uniqueMethods;
1268 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1269 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1271 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1272 const TCollection_AsciiString& method = (*cmd)->GetMethod();
1273 if ( !clear || method == "SetNumberOfSegments" ) {
1274 bool isNewInSet = uniqueMethods.insert( method ).second;
1275 clear = !isNewInSet;
1284 //================================================================================
1286 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
1287 * into regular1D.LengthNearVertex( length, vertex )
1288 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
1289 * \param theMesh - The mesh needing this hypo
1290 * \retval bool - false if the command cant be converted
1292 //================================================================================
1294 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1295 const _pyID& theMeshID)
1297 if ( IsWrappable( theMeshID )) {
1299 _pyID vertex = theCmd->GetArg( 1 );
1301 // the problem here is that segment algo will not be found
1302 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
1303 // geometry where segment algorithm is assigned
1304 Handle(_pyHypothesis) algo;
1305 _pyID geom = vertex;
1306 while ( algo.IsNull() && !geom.IsEmpty()) {
1307 // try to find geom as a father of <vertex>
1308 geom = FatherID( geom );
1309 algo = theGen->FindAlgo( geom, theMeshID, GetType() );
1311 if ( algo.IsNull() )
1312 return false; // also possible to find geom as brother of veretex...
1313 // set geom instead of vertex
1314 theCmd->SetArg( 1, geom );
1316 // set vertex as a second arg
1317 if ( myArgs.Length() < 1) myArgs.Append( "1" ); // :(
1318 myArgs.Append( vertex );
1320 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
1321 // theMeshID.LengthNearVertex( length, vertex )
1322 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
1327 //================================================================================
1329 * \brief _pyAlgorithm constructor
1330 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1332 //================================================================================
1334 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1335 : _pyHypothesis( theCreationCmd )
1340 //================================================================================
1342 * \brief Convert the command adding an algorithm to mesh
1343 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1344 * \param theMesh - The mesh needing this algo
1345 * \retval bool - false if the command cant be converted
1347 //================================================================================
1349 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1350 const _pyID& theMeshID)
1352 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1353 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1354 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1360 //================================================================================
1362 * \brief Return starting position of a part of python command
1363 * \param thePartIndex - The index of command part
1364 * \retval int - Part position
1366 //================================================================================
1368 int _pyCommand::GetBegPos( int thePartIndex )
1372 if ( myBegPos.Length() < thePartIndex )
1374 return myBegPos( thePartIndex );
1377 //================================================================================
1379 * \brief Store starting position of a part of python command
1380 * \param thePartIndex - The index of command part
1381 * \param thePosition - Part position
1383 //================================================================================
1385 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1387 while ( myBegPos.Length() < thePartIndex )
1388 myBegPos.Append( UNKNOWN );
1389 myBegPos( thePartIndex ) = thePosition;
1392 //================================================================================
1394 * \brief Returns whitespace symbols at the line beginning
1395 * \retval TCollection_AsciiString - result
1397 //================================================================================
1399 TCollection_AsciiString _pyCommand::GetIndentation()
1402 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1403 GetWord( myString, end, true );
1405 end = GetBegPos( RESULT_IND );
1406 return myString.SubString( 1, end - 1 );
1409 //================================================================================
1411 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1412 * \retval const TCollection_AsciiString & - ResultValue substring
1414 //================================================================================
1416 const TCollection_AsciiString & _pyCommand::GetResultValue()
1418 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1420 int begPos = myString.Location( "=", 1, Length() );
1422 myRes = GetWord( myString, begPos, false );
1425 SetBegPos( RESULT_IND, begPos );
1430 //================================================================================
1432 * \brief Return substring of python command looking like ResVal = Object.Meth()
1433 * \retval const TCollection_AsciiString & - Object substring
1435 //================================================================================
1437 const TCollection_AsciiString & _pyCommand::GetObject()
1439 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1442 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1444 begPos = myString.Location( "=", 1, Length() ) + 1;
1446 myObj = GetWord( myString, begPos, true );
1447 SetBegPos( OBJECT_IND, begPos );
1453 //================================================================================
1455 * \brief Return substring of python command looking like ResVal = Obj.Method()
1456 * \retval const TCollection_AsciiString & - Method substring
1458 //================================================================================
1460 const TCollection_AsciiString & _pyCommand::GetMethod()
1462 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1465 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1466 bool forward = true;
1468 begPos = myString.Location( "(", 1, Length() ) - 1;
1472 myMeth = GetWord( myString, begPos, forward );
1473 SetBegPos( METHOD_IND, begPos );
1479 //================================================================================
1481 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1482 * \retval const TCollection_AsciiString & - Arg<index> substring
1484 //================================================================================
1486 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1488 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1491 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1493 begPos = myString.Location( "(", 1, Length() ) + 1;
1495 int i = 0, prevLen = 0;
1496 while ( begPos != EMPTY ) {
1498 // check if we are looking at the closing parenthesis
1499 while ( begPos <= Length() && isspace( myString.Value( begPos )))
1501 if ( begPos > Length() || myString.Value( begPos ) == ')' )
1503 myArgs.Append( GetWord( myString, begPos, true, true ));
1504 SetBegPos( ARG1_IND + i, begPos );
1505 prevLen = myArgs.Last().Length();
1507 myArgs.Remove( myArgs.Length() ); // no more args
1511 if ( myArgs.Length() < index )
1512 return theEmptyString;
1513 return myArgs( index );
1516 //================================================================================
1518 * \brief Check if char is a word part
1519 * \param c - The character to check
1520 * \retval bool - The check result
1522 //================================================================================
1524 static inline bool isWord(const char c, const bool dotIsWord)
1527 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1530 //================================================================================
1532 * \brief Looks for a word in the string and returns word's beginning
1533 * \param theString - The input string
1534 * \param theStartPos - The position to start the search, returning word's beginning
1535 * \param theForward - The search direction
1536 * \retval TCollection_AsciiString - The found word
1538 //================================================================================
1540 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1542 const bool theForward,
1543 const bool dotIsWord )
1545 int beg = theStartPos, end = theStartPos;
1546 theStartPos = EMPTY;
1547 if ( beg < 1 || beg > theString.Length() )
1548 return theEmptyString;
1550 if ( theForward ) { // search forward
1552 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1554 if ( beg > theString.Length() )
1555 return theEmptyString; // no word found
1558 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1562 else { // search backward
1564 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1567 return theEmptyString; // no word found
1569 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1574 //cout << theString << " ---- " << beg << " - " << end << endl;
1575 return theString.SubString( beg, end );
1578 //================================================================================
1580 * \brief Look for position where not space char is
1581 * \param theString - The string
1582 * \param thePos - The position to search from and which returns result
1583 * \retval bool - false if there are only space after thePos in theString
1587 //================================================================================
1589 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1591 if ( thePos < 1 || thePos > theString.Length() )
1594 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1597 return thePos <= theString.Length();
1600 //================================================================================
1602 * \brief Modify a part of the command
1603 * \param thePartIndex - The index of the part
1604 * \param thePart - The new part string
1605 * \param theOldPart - The old part
1607 //================================================================================
1609 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1610 TCollection_AsciiString& theOldPart)
1612 int pos = GetBegPos( thePartIndex );
1613 if ( pos <= Length() && theOldPart != thePart)
1615 TCollection_AsciiString seperator;
1617 pos = GetBegPos( thePartIndex + 1 );
1618 if ( pos < 1 ) return;
1619 switch ( thePartIndex ) {
1620 case RESULT_IND: seperator = " = "; break;
1621 case OBJECT_IND: seperator = "."; break;
1622 case METHOD_IND: seperator = "()"; break;
1626 myString.Remove( pos, theOldPart.Length() );
1627 if ( !seperator.IsEmpty() )
1628 myString.Insert( pos , seperator );
1629 myString.Insert( pos, thePart );
1630 // update starting positions of the following parts
1631 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1632 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1633 if ( myBegPos( i ) > 0 )
1634 myBegPos( i ) += posDelta;
1636 theOldPart = thePart;
1640 //================================================================================
1642 * \brief Set agrument
1643 * \param index - The argument index, it counts from 1
1644 * \param theArg - The argument string
1646 //================================================================================
1648 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1651 int argInd = ARG1_IND + index - 1;
1652 int pos = GetBegPos( argInd );
1653 if ( pos < 1 ) // no index-th arg exist, append inexistent args
1655 // find a closing parenthesis
1656 if ( int lastArgInd = GetNbArgs() ) {
1657 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
1658 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
1663 while ( pos > 0 && myString.Value( pos ) != ')' )
1666 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
1670 while ( myArgs.Length() < index ) {
1671 if ( myArgs.Length() )
1672 myString.Insert( pos++, "," );
1673 myArgs.Append("None");
1674 myString.Insert( pos, myArgs.Last() );
1675 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1676 pos += myArgs.Last().Length();
1679 SetPart( argInd, theArg, myArgs( index ));
1682 //================================================================================
1684 * \brief Empty arg list
1686 //================================================================================
1688 void _pyCommand::RemoveArgs()
1690 if ( int pos = myString.Location( '(', 1, Length() ))
1691 myString.Trunc( pos );
1694 if ( myBegPos.Length() >= ARG1_IND )
1695 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1698 //================================================================================
1700 * \brief Set dependent commands after this one
1702 //================================================================================
1704 bool _pyCommand::SetDependentCmdsAfter() const
1706 bool orderChanged = false;
1707 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1708 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1709 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1710 orderChanged = true;
1711 theGen->SetCommandAfter( *cmd, this );
1712 (*cmd)->SetDependentCmdsAfter();
1715 return orderChanged;
1717 //================================================================================
1719 * \brief Insert accessor method after theObjectID
1720 * \param theObjectID - id of the accessed object
1721 * \param theAcsMethod - name of the method giving access to the object
1722 * \retval bool - false if theObjectID is not found in the command string
1724 //================================================================================
1726 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
1728 if ( !theAcsMethod )
1730 // start object search from the object, i.e. ignore result
1732 int beg = GetBegPos( OBJECT_IND );
1733 if ( beg < 1 || beg > Length() )
1736 while (( beg = myString.Location( theObjectID, beg, Length() )))
1738 // check that theObjectID is not just a part of a longer ID
1739 int afterEnd = beg + theObjectID.Length();
1740 Standard_Character c = myString.Value( afterEnd );
1741 if ( !isalnum( c ) && c != ':' ) {
1742 // check if accessor method already present
1744 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
1746 int oldLen = Length();
1747 myString.Insert( afterEnd, (char*) theAcsMethod );
1748 myString.Insert( afterEnd, "." );
1749 // update starting positions of the parts following the modified one
1750 int posDelta = Length() - oldLen;
1751 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
1752 if ( myBegPos( i ) > afterEnd )
1753 myBegPos( i ) += posDelta;
1758 beg = afterEnd; // is a part - next search
1763 //================================================================================
1765 * \brief Return method name giving access to an interaface object wrapped by python class
1766 * \retval const char* - method name
1768 //================================================================================
1770 const char* _pyObject::AccessorMethod() const
1774 //================================================================================
1776 * \brief Return ID of a father
1778 //================================================================================
1780 _pyID _pyObject::FatherID(const _pyID & childID)
1782 int colPos = childID.SearchFromEnd(':');
1784 return childID.SubString( 1, colPos-1 );