1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
23 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
24 // File : SMESH_2smeshpy.cxx
25 // Created : Fri Nov 18 13:20:10 2005
26 // Author : Edward AGAPOV (eap)
28 #include "SMESH_2smeshpy.hxx"
30 #include "utilities.h"
31 #include "SMESH_PythonDump.hxx"
32 #include "SMESH_NoteBook.hxx"
33 #include "Resource_DataMapOfAsciiStringAsciiString.hxx"
35 #include "SMESH_Gen_i.hxx"
36 /* SALOME headers that include CORBA headers that include windows.h
37 * that defines GetObject symbol as GetObjectA should stand before SALOME headers
38 * that declare methods named GetObject - to apply the same rules of GetObject renaming
39 * and thus to avoid mess with GetObject symbol on Windows */
41 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
42 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
43 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
44 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
45 IMPLEMENT_STANDARD_HANDLE (_pySubMesh ,_pyObject);
46 IMPLEMENT_STANDARD_HANDLE (_pyMeshEditor ,_pyObject);
47 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
48 IMPLEMENT_STANDARD_HANDLE (_pySelfEraser ,_pyObject);
49 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
50 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
51 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
53 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
54 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
55 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
56 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
57 IMPLEMENT_STANDARD_RTTIEXT(_pySubMesh ,_pyObject);
58 IMPLEMENT_STANDARD_RTTIEXT(_pyMeshEditor ,_pyObject);
59 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
60 IMPLEMENT_STANDARD_RTTIEXT(_pySelfEraser ,_pyObject);
61 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
62 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
63 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
64 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
65 IMPLEMENT_STANDARD_RTTIEXT(_pySegmentLengthAroundVertexHyp,_pyHypothesis);
68 using SMESH::TPythonDump;
71 * \brief Container of commands into which the initial script is split.
72 * It also contains data coresponding to SMESH_Gen contents
74 static Handle(_pyGen) theGen;
76 static TCollection_AsciiString theEmptyString;
78 //#define DUMP_CONVERSION
80 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
81 #undef DUMP_CONVERSION
87 //================================================================================
89 * \brief Set of TCollection_AsciiString initialized by C array of C strings
91 //================================================================================
93 struct TStringSet: public set<TCollection_AsciiString>
96 * \brief Filling. The last string must be ""
98 void Insert(const char* names[]) {
99 for ( int i = 0; names[i][0] ; ++i )
100 insert( (char*) names[i] );
103 * \brief Check if a string is in
105 bool Contains(const TCollection_AsciiString& name ) {
106 return find( name ) != end();
111 //================================================================================
113 * \brief Convert python script using commands of smesh.py
114 * \param theScript - Input script
115 * \retval TCollection_AsciiString - Convertion result
117 * Class SMESH_2smeshpy declared in SMESH_PythonDump.hxx
119 //================================================================================
121 TCollection_AsciiString
122 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
123 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
124 Resource_DataMapOfAsciiStringAsciiString& theObjectNames)
126 theGen = new _pyGen( theEntry2AccessorMethod, theObjectNames );
128 // split theScript into separate commands
130 SMESH_NoteBook * aNoteBook = new SMESH_NoteBook();
132 int from = 1, end = theScript.Length(), to;
133 while ( from < end && ( to = theScript.Location( "\n", from, end )))
136 // cut out and store a command
137 aNoteBook->AddCommand( theScript.SubString( from, to - 1 ));
141 aNoteBook->ReplaceVariables();
143 TCollection_AsciiString aNoteScript = aNoteBook->GetResultScript();
147 // split theScript into separate commands
148 from = 1, end = aNoteScript.Length();
149 while ( from < end && ( to = aNoteScript.Location( "\n", from, end )))
152 // cut out and store a command
153 theGen->AddCommand( aNoteScript.SubString( from, to - 1 ));
159 #ifdef DUMP_CONVERSION
160 MESSAGE_BEGIN ( std::endl << " ######## RESULT ######## " << std::endl<< std::endl );
163 // reorder commands after conversion
164 list< Handle(_pyCommand) >::iterator cmd;
167 orderChanges = false;
168 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
169 if ( (*cmd)->SetDependentCmdsAfter() )
171 } while ( orderChanges );
173 // concat commands back into a script
174 TCollection_AsciiString aScript;
175 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
177 #ifdef DUMP_CONVERSION
178 MESSAGE_ADD ( "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << std::endl );
180 if ( !(*cmd)->IsEmpty() ) {
182 aScript += (*cmd)->GetString();
192 //================================================================================
194 * \brief _pyGen constructor
196 //================================================================================
198 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
199 Resource_DataMapOfAsciiStringAsciiString& theObjectNames)
200 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
201 myID2AccessorMethod( theEntry2AccessorMethod ),
202 myObjectNames( theObjectNames )
205 // make that GetID() to return TPythonDump::SMESHGenName()
206 GetCreationCmd()->GetString() += "=";
209 //================================================================================
211 * \brief name of SMESH_Gen in smesh.py
213 //================================================================================
215 const char* _pyGen::AccessorMethod() const
217 return SMESH_2smeshpy::GenName();
220 //================================================================================
222 * \brief Convert a command using a specific converter
223 * \param theCommand - the command to convert
225 //================================================================================
227 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
229 // store theCommand in the sequence
230 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
232 Handle(_pyCommand) aCommand = myCommands.back();
233 #ifdef DUMP_CONVERSION
234 MESSAGE ( "## COM " << myNbCommands << ": "<< aCommand->GetString() );
237 _pyID objID = aCommand->GetObject();
239 if ( objID.IsEmpty() )
243 if ( objID == this->GetID() || objID == SMESH_2smeshpy::GenName()) {
244 this->Process( aCommand );
248 // SMESH_Mesh method?
249 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
250 if ( id_mesh != myMeshes.end() )
252 // check for mesh editor object
253 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
254 _pyID editorID = aCommand->GetResultValue();
255 Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
256 myMeshEditors.insert( make_pair( editorID, editor ));
259 // check for SubMesh objects
260 else if ( aCommand->GetMethod() == "GetSubMesh" ) { // SubMesh creation
261 _pyID subMeshID = aCommand->GetResultValue();
262 Handle(_pySubMesh) subMesh = new _pySubMesh( aCommand );
263 myObjects.insert( make_pair( subMeshID, subMesh ));
266 id_mesh->second->Process( aCommand );
270 // SMESH_MeshEditor method?
271 map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
272 if ( id_editor != myMeshEditors.end() )
274 id_editor->second->Process( aCommand );
275 TCollection_AsciiString processedCommand = aCommand->GetString();
276 // some commands of SMESH_MeshEditor create meshes
277 if ( aCommand->GetMethod().Search("MakeMesh") != -1 ) {
278 Handle(_pyMesh) mesh = new _pyMesh( aCommand, aCommand->GetResultValue() );
279 aCommand->GetString() = processedCommand; // discard changes made by _pyMesh
280 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
282 if ( aCommand->GetMethod() == "MakeBoundaryMesh") {
283 _pyID meshID = aCommand->GetResultValue(0);
284 if ( !myMeshes.count( meshID ) )
286 Handle(_pyMesh) mesh = new _pyMesh( aCommand, meshID );
287 aCommand->GetString() = processedCommand; // discard changes made by _pyMesh
288 myMeshes.insert( make_pair( meshID, mesh ));
293 // SMESH_Hypothesis method?
294 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
295 for ( ; hyp != myHypos.end(); ++hyp )
296 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
297 (*hyp)->Process( aCommand );
301 // other object method?
302 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.find( objID );
303 if ( id_obj != myObjects.end() ) {
304 id_obj->second->Process( aCommand );
307 // if ( theCommand.Search( "aFilterManager" ) != -1 ) {
308 // if ( theCommand.Search( "CreateFilterManager" ) != -1 )
309 // myFilterManager = new _pySelfEraser( aCommand );
310 // else if ( !myFilterManager.IsNull() )
311 // myFilterManager->Process( aCommand );
315 // Add access to a wrapped mesh
316 AddMeshAccessorMethod( aCommand );
318 // Add access to a wrapped algorithm
319 // AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
321 // PAL12227. PythonDump was not updated at proper time; result is
322 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
323 // TypeError: __init__() takes exactly 11 arguments (10 given)
324 char wrongCommand[] = "SMESH.Filter.Criterion(";
325 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
327 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
328 // there must be 10 arguments, 5-th arg ThresholdID is missing,
329 const int wrongNbArgs = 9, missingArg = 5;
330 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
332 for ( int i = wrongNbArgs; i > missingArg; --i )
333 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
334 tmpCmd.SetArg( missingArg, "''");
335 aCommand->GetString().Trunc( beg - 1 );
336 aCommand->GetString() += tmpCmd.GetString();
342 //================================================================================
344 * \brief Convert the command or remember it for later conversion
345 * \param theCommand - The python command calling a method of SMESH_Gen
347 //================================================================================
349 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
351 // there are methods to convert:
352 // CreateMesh( shape )
353 // Concatenate( [mesh1, ...], ... )
354 // CreateHypothesis( theHypType, theLibName )
355 // Compute( mesh, geom )
356 // Evaluate( mesh, geom )
358 TCollection_AsciiString method = theCommand->GetMethod();
360 if ( method == "CreateMesh" || method == "CreateEmptyMesh")
362 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
363 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
366 if ( method == "CreateMeshesFromUNV" || method == "CreateMeshesFromSTL" || method == "CopyMesh" )
368 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
369 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
372 if( method == "CreateMeshesFromMED")
374 for(int ind = 0;ind<theCommand->GetNbResultValues();ind++)
376 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue(ind));
377 myMeshes.insert( make_pair( theCommand->GetResultValue(ind), mesh ));
381 // CreateHypothesis()
382 if ( method == "CreateHypothesis" )
384 // issue 199929, remove standard library name (default parameter)
385 const TCollection_AsciiString & aLibName = theCommand->GetArg( 2 );
386 if ( aLibName.Search( "StdMeshersEngine" ) != -1 ) {
387 // keep first argument
388 TCollection_AsciiString arg = theCommand->GetArg( 1 );
389 theCommand->RemoveArgs();
390 theCommand->SetArg( 1, arg );
393 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
397 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
398 if ( method == "Compute" )
400 const _pyID& meshID = theCommand->GetArg( 1 );
401 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
402 if ( id_mesh != myMeshes.end() ) {
403 theCommand->SetObject( meshID );
404 theCommand->RemoveArgs();
405 id_mesh->second->Flush();
410 // smeshgen.Evaluate( mesh, geom ) --> mesh.Evaluate(geom)
411 if ( method == "Evaluate" )
413 const _pyID& meshID = theCommand->GetArg( 1 );
414 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
415 if ( id_mesh != myMeshes.end() ) {
416 theCommand->SetObject( meshID );
417 _pyID geom = theCommand->GetArg( 2 );
418 theCommand->RemoveArgs();
419 theCommand->SetArg( 1, geom );
424 // objects erasing creation command if no more it's commands invoked:
425 // SMESH_Pattern, FilterManager
426 if ( method == "GetPattern" || method == "CreateFilterManager" ) {
427 Handle(_pyObject) obj = new _pySelfEraser( theCommand );
428 if ( !myObjects.insert( make_pair( obj->GetID(), obj )).second )
429 theCommand->Clear(); // already created
432 // Concatenate( [mesh1, ...], ... )
433 if ( method == "Concatenate" || method == "ConcatenateWithGroups")
435 if ( method == "ConcatenateWithGroups" ) {
436 theCommand->SetMethod( "Concatenate" );
437 theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
439 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
440 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
441 AddMeshAccessorMethod( theCommand );
444 // Replace name of SMESH_Gen
446 // names of SMESH_Gen methods fully equal to methods defined in smesh.py
447 static TStringSet smeshpyMethods;
448 if ( smeshpyMethods.empty() ) {
449 const char * names[] =
450 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
451 "GetPattern","GetSubShapesId",
452 "" }; // <- mark of array end
453 smeshpyMethods.Insert( names );
455 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
456 // smeshgen.Method() --> smesh.Method()
457 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
459 // smeshgen.Method() --> smesh.smesh.Method()
460 theCommand->SetObject( SMESH_2smeshpy::GenName() );
463 //================================================================================
465 * \brief Convert the remembered commands
467 //================================================================================
471 // create empty command
472 myLastCommand = new _pyCommand();
474 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
475 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
476 if ( ! id_mesh->second.IsNull() )
477 id_mesh->second->Flush();
479 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
480 for ( ; hyp != myHypos.end(); ++hyp )
481 if ( !hyp->IsNull() ) {
483 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
484 if ( !(*hyp)->IsWrapped() )
485 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
488 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
489 for ( ; id_obj != myObjects.end(); ++id_obj )
490 if ( ! id_obj->second.IsNull() )
491 id_obj->second->Flush();
493 myLastCommand->SetOrderNb( ++myNbCommands );
494 myCommands.push_back( myLastCommand );
497 //================================================================================
499 * \brief Add access method to mesh that is an argument
500 * \param theCmd - command to add access method
501 * \retval bool - true if added
503 //================================================================================
505 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
508 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
509 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
510 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
516 //================================================================================
518 * \brief Add access method to algo that is an object or an argument
519 * \param theCmd - command to add access method
520 * \retval bool - true if added
522 //================================================================================
524 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
527 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
528 for ( ; hyp != myHypos.end(); ++hyp ) {
529 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
530 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
536 //================================================================================
538 * \brief Find hypothesis by ID (entry)
539 * \param theHypID - The hypothesis ID
540 * \retval Handle(_pyHypothesis) - The found hypothesis
542 //================================================================================
544 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
546 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
547 for ( ; hyp != myHypos.end(); ++hyp )
548 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
550 return Handle(_pyHypothesis)();
553 //================================================================================
555 * \brief Find algorithm the created algorithm
556 * \param theGeom - The shape ID the algorithm was created on
557 * \param theMesh - The mesh ID that created the algorithm
558 * \param dim - The algo dimension
559 * \retval Handle(_pyHypothesis) - The found algo
561 //================================================================================
563 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
564 const Handle(_pyHypothesis)& theHypothesis )
566 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
567 for ( ; hyp != myHypos.end(); ++hyp )
568 if ( !hyp->IsNull() &&
570 theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
571 (*hyp)->GetGeom() == theGeom &&
572 (*hyp)->GetMesh() == theMesh )
577 //================================================================================
579 * \brief Find subMesh by ID (entry)
580 * \param theSubMeshID - The subMesh ID
581 * \retval Handle(_pySubMesh) - The found subMesh
583 //================================================================================
585 Handle(_pySubMesh) _pyGen::FindSubMesh( const _pyID& theSubMeshID )
587 map< _pyID, Handle(_pyObject) >::iterator id_subMesh = myObjects.find(theSubMeshID);
588 if ( id_subMesh != myObjects.end() )
589 return Handle(_pySubMesh)::DownCast( id_subMesh->second );
590 return Handle(_pySubMesh)();
594 //================================================================================
596 * \brief Change order of commands in the script
597 * \param theCmd1 - One command
598 * \param theCmd2 - Another command
600 //================================================================================
602 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
604 list< Handle(_pyCommand) >::iterator pos1, pos2;
605 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
606 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
607 myCommands.insert( pos1, theCmd2 );
608 myCommands.insert( pos2, theCmd1 );
609 myCommands.erase( pos1 );
610 myCommands.erase( pos2 );
612 int nb1 = theCmd1->GetOrderNb();
613 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
614 theCmd2->SetOrderNb( nb1 );
615 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
616 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
619 //================================================================================
621 * \brief Set one command after the other
622 * \param theCmd - Command to move
623 * \param theAfterCmd - Command ater which to insert the first one
625 //================================================================================
627 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
629 setNeighbourCommand( theCmd, theAfterCmd, true );
632 //================================================================================
634 * \brief Set one command before the other
635 * \param theCmd - Command to move
636 * \param theBeforeCmd - Command before which to insert the first one
638 //================================================================================
640 void _pyGen::SetCommandBefore( Handle(_pyCommand) theCmd, Handle(_pyCommand) theBeforeCmd )
642 setNeighbourCommand( theCmd, theBeforeCmd, false );
645 //================================================================================
647 * \brief Set one command before or after the other
648 * \param theCmd - Command to move
649 * \param theOtherCmd - Command ater or before which to insert the first one
651 //================================================================================
653 void _pyGen::setNeighbourCommand( Handle(_pyCommand)& theCmd,
654 Handle(_pyCommand)& theOtherCmd,
655 const bool theIsAfter )
657 list< Handle(_pyCommand) >::iterator pos;
658 pos = find( myCommands.begin(), myCommands.end(), theCmd );
659 myCommands.erase( pos );
660 pos = find( myCommands.begin(), myCommands.end(), theOtherCmd );
661 myCommands.insert( (theIsAfter ? ++pos : pos), theCmd );
664 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
665 (*pos)->SetOrderNb( i++ );
668 //================================================================================
670 * \brief Set command be last in list of commands
671 * \param theCmd - Command to be last
673 //================================================================================
675 Handle(_pyCommand)& _pyGen::GetLastCommand()
677 return myLastCommand;
680 //================================================================================
682 * \brief Set method to access to object wrapped with python class
683 * \param theID - The wrapped object entry
684 * \param theMethod - The accessor method
686 //================================================================================
688 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
690 myID2AccessorMethod.Bind( theID, (char*) theMethod );
693 //================================================================================
695 * \brief Generated new ID for object and assign with existing name
696 * \param theID - ID of existing object
698 //================================================================================
700 _pyID _pyGen::GenerateNewID( const _pyID& theID )
705 aNewID = theID + _pyID( ":" ) + _pyID( index++ );
707 while ( myObjectNames.IsBound( aNewID ) );
709 myObjectNames.Bind( aNewID, myObjectNames.IsBound( theID )
710 ? (myObjectNames.Find( theID ) + _pyID( "_" ) + _pyID( index-1 ))
711 : _pyID( "A" ) + aNewID );
715 //================================================================================
717 * \brief Find out type of geom group
718 * \param grpID - The geom group entry
719 * \retval int - The type
721 //================================================================================
723 static bool sameGroupType( const _pyID& grpID,
724 const TCollection_AsciiString& theType)
726 // define group type as smesh.Mesh.Group() does
728 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
729 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
730 if ( !aSObj->_is_nil() ) {
731 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
732 if ( !aGeomObj->_is_nil() ) {
733 switch ( aGeomObj->GetShapeType() ) {
734 case GEOM::VERTEX: type = SMESH::NODE; break;
735 case GEOM::EDGE: type = SMESH::EDGE; break;
736 case GEOM::FACE: type = SMESH::FACE; break;
738 case GEOM::SHELL: type = SMESH::VOLUME; break;
739 case GEOM::COMPOUND: {
740 GEOM::GEOM_Gen_ptr aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
741 if ( !aGeomGen->_is_nil() ) {
742 GEOM::GEOM_IGroupOperations_var aGrpOp =
743 aGeomGen->GetIGroupOperations( study->StudyId() );
744 if ( !aGrpOp->_is_nil() ) {
745 switch ( aGrpOp->GetType( aGeomObj )) {
746 case TopAbs_VERTEX: type = SMESH::NODE; break;
747 case TopAbs_EDGE: type = SMESH::EDGE; break;
748 case TopAbs_FACE: type = SMESH::FACE; break;
749 case TopAbs_SOLID: type = SMESH::VOLUME; break;
760 MESSAGE("Type of the group " << grpID << " not found");
763 if ( theType.IsIntegerValue() )
764 return type == theType.IntegerValue();
767 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
768 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
769 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
770 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
776 //================================================================================
779 * \param theCreationCmd -
781 //================================================================================
783 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd)
784 : _pyObject(theCreationCmd), myHasEditor(false)
786 // convert my creation command
787 Handle(_pyCommand) creationCmd = GetCreationCmd();
788 //TCollection_AsciiString str = creationCmd->GetMethod();
789 // if(str != "CreateMeshesFromUNV" &&
790 // str != "CreateMeshesFromMED" &&
791 // str != "CreateMeshesFromSTL")
792 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
793 creationCmd->SetMethod( "Mesh" );
795 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
798 //================================================================================
801 * \param theCreationCmd -
803 //================================================================================
804 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd, const TCollection_AsciiString& id):
805 _pyObject(theCreationCmd), myHasEditor(false)
807 // convert my creation command
808 Handle(_pyCommand) creationCmd = GetCreationCmd();
809 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
810 theGen->SetAccessorMethod( id, "GetMesh()" );
813 //================================================================================
815 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
816 * \param theCommand - Engine method called for this mesh
818 //================================================================================
820 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
822 // some methods of SMESH_Mesh interface needs special conversion
823 // to methods of Mesh python class
825 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
826 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
827 // 2. AddHypothesis(geom, hyp)
828 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
829 // 3. CreateGroupFromGEOM(type, name, grp)
830 // --> in Mesh.Group(grp, name="")
831 // 4. ExportToMED(f, auto_groups, version)
832 // --> in Mesh.ExportMED( f, auto_groups, version )
835 const TCollection_AsciiString method = theCommand->GetMethod();
836 // ----------------------------------------------------------------------
837 if ( method == "GetSubMesh" ) {
838 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetResultValue() );
839 if ( !subMesh.IsNull() ) {
840 subMesh->SetCreator( this );
841 mySubmeshes.push_back( subMesh );
844 // ----------------------------------------------------------------------
845 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
846 myAddHypCmds.push_back( theCommand );
848 const _pyID& hypID = theCommand->GetArg( 2 );
849 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
850 if ( !hyp.IsNull() ) {
851 myHypos.push_back( hyp );
852 if ( hyp->GetMesh().IsEmpty() )
853 hyp->SetMesh( this->GetID() );
856 // ----------------------------------------------------------------------
857 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
858 _pyID grp = theCommand->GetArg( 3 );
859 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
860 theCommand->SetMethod( "Group" );
861 theCommand->RemoveArgs();
862 theCommand->SetArg( 1, grp );
865 _pyID type = theCommand->GetArg( 1 );
866 _pyID name = theCommand->GetArg( 2 );
867 theCommand->SetMethod( "GroupOnGeom" );
868 theCommand->RemoveArgs();
869 theCommand->SetArg( 1, grp );
870 theCommand->SetArg( 2, name );
871 theCommand->SetArg( 3, type );
874 // ----------------------------------------------------------------------
875 else if ( method == "ExportToMED" || // ExportToMED() --> ExportMED()
876 method == "ExportToMEDX" ) { // ExportToMEDX() --> ExportMED()
877 theCommand->SetMethod( "ExportMED" );
879 // ----------------------------------------------------------------------
880 else if ( method == "CreateGroup" ) { // CreateGroup() --> CreateEmptyGroup()
881 theCommand->SetMethod( "CreateEmptyGroup" );
883 // ----------------------------------------------------------------------
884 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
886 _pyID hypID = theCommand->GetArg( 2 );
888 // check if this mesh still has corresponding addition command
889 bool hasAddCmd = false;
890 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
891 while ( cmd != myAddHypCmds.end() )
893 // AddHypothesis(geom, hyp)
894 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
897 cmd = myAddHypCmds.erase( cmd );
904 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
905 if ( ! hasAddCmd && hypID.Length() != 0 ) { // hypo addition already wrapped
906 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
907 _pyID geom = theCommand->GetArg( 1 );
908 theCommand->RemoveArgs();
909 theCommand->SetArg( 1, hypID );
910 if ( geom != GetGeom() )
911 theCommand->SetArg( 2, geom );
913 // remove hyp from myHypos
914 myHypos.remove( hyp );
916 // check for SubMesh order commands
917 else if ( theCommand->GetMethod() == "GetMeshOrder" ||
918 theCommand->GetMethod() == "SetMeshOrder" ) {
919 // In fact arguments and result values does not support complex containers
920 // such as list of list
921 // So, here we parse it manually
923 //for(int ind = 0, n = theCommand->GetNbResultValues();ind<n;ind++) {
924 // Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetResultValue(ind) );
926 //for(int ind = 0, n = theCommand->GetNbArgs();ind<n;ind++) {
927 // Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetArg(ind) );
928 const bool isArg = theCommand->GetMethod() == "SetMeshOrder";
929 const TCollection_AsciiString& cmdStr = theCommand->GetString();
930 int begPos = (/*isArg ? cmdStr.Search( "(" ) :*/ cmdStr.Search( "[" )) + 1;
931 int endPos = (isArg ? cmdStr.Search( ")" ) : cmdStr.Search( "=" )) - 1;
932 if ( begPos != -1 && begPos < endPos && endPos <= cmdStr.Length() ) {
933 TCollection_AsciiString aSubStr = cmdStr.SubString( begPos, endPos );
934 Standard_Integer index = 1;
935 TCollection_AsciiString anIDStr = aSubStr.Token("\t ,[]", index++);
936 while ( !anIDStr.IsEmpty() ) {
937 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( anIDStr );
938 if ( !subMesh.IsNull() )
939 subMesh->Process( theCommand );
940 anIDStr = aSubStr.Token("\t ,[]", index++);
944 // add accessor method if necessary
947 if ( NeedMeshAccess( theCommand ))
948 // apply theCommand to the mesh wrapped by smeshpy mesh
949 AddMeshAccess( theCommand );
953 //================================================================================
955 * \brief Return True if addition of accesor method is needed
957 //================================================================================
959 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
961 // names of SMESH_Mesh methods fully equal to methods of class Mesh, so
962 // no conversion is needed for them at all:
963 static TStringSet sameMethods;
964 if ( sameMethods.empty() ) {
965 const char * names[] =
966 { "ExportDAT","ExportUNV","ExportSTL", "RemoveGroup","RemoveGroupWithContents",
967 "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
968 "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
969 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
970 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
971 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
972 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
973 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
974 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
975 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
976 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
977 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
978 "Clear", "ConvertToStandalone", "GetMeshOrder", "SetMeshOrder"
979 ,"" }; // <- mark of end
980 sameMethods.Insert( names );
983 return !sameMethods.Contains( theCommand->GetMethod() );
986 //================================================================================
988 * \brief Convert creation and addition of all algos and hypos
990 //================================================================================
992 void _pyMesh::Flush()
994 list < Handle(_pyCommand) >::iterator cmd;
996 // try to convert algo addition like this:
997 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
998 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1000 Handle(_pyCommand) addCmd = *cmd;
1002 _pyID algoID = addCmd->GetArg( 2 );
1003 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
1004 if ( algo.IsNull() || !algo->IsAlgo() )
1007 // check and create new algorithm instance if it is already wrapped
1008 if ( algo->IsWrapped() ) {
1009 _pyID localAlgoID = theGen->GenerateNewID( algoID );
1010 TCollection_AsciiString aNewCmdStr = localAlgoID +
1011 TCollection_AsciiString( " = " ) + theGen->GetID() +
1012 TCollection_AsciiString( ".CreateHypothesis( \"" ) + algo->GetAlgoType() +
1013 TCollection_AsciiString( "\" )" );
1015 Handle(_pyCommand) newCmd = theGen->AddCommand( aNewCmdStr );
1016 Handle(_pyAlgorithm) newAlgo = Handle(_pyAlgorithm)::DownCast(theGen->FindHyp( localAlgoID ));
1017 if ( !newAlgo.IsNull() ) {
1018 newAlgo->Assign( algo, this->GetID() );
1019 newAlgo->SetCreationCmd( newCmd );
1021 // set algorithm creation
1022 theGen->SetCommandBefore( newCmd, addCmd );
1027 _pyID geom = addCmd->GetArg( 1 );
1028 bool isLocalAlgo = ( geom != GetGeom() );
1031 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
1033 // wrapped algo is created atfer mesh creation
1034 GetCreationCmd()->AddDependantCmd( addCmd );
1036 if ( isLocalAlgo ) {
1037 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
1038 addCmd->SetArg( addCmd->GetNbArgs() + 1,
1039 TCollection_AsciiString( "geom=" ) + geom );
1040 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
1041 list < Handle(_pySubMesh) >::iterator smIt;
1042 for ( smIt = mySubmeshes.begin(); smIt != mySubmeshes.end(); ++smIt ) {
1043 Handle(_pySubMesh) subMesh = *smIt;
1044 Handle(_pyCommand) subCmd = subMesh->GetCreationCmd();
1045 if ( geom == subCmd->GetArg( 1 )) {
1046 subCmd->SetObject( algo->GetID() );
1047 subCmd->RemoveArgs();
1048 subMesh->SetCreator( algo );
1053 else // KO - ALGO was already created
1055 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
1056 addCmd->RemoveArgs();
1057 addCmd->SetArg( 1, algoID );
1059 addCmd->SetArg( 2, geom );
1063 // try to convert hypo addition like this:
1064 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
1065 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1067 Handle(_pyCommand) addCmd = *cmd;
1068 _pyID hypID = addCmd->GetArg( 2 );
1069 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1070 if ( hyp.IsNull() || hyp->IsAlgo() )
1072 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
1074 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
1075 _pyID geom = addCmd->GetArg( 1 );
1076 addCmd->RemoveArgs();
1077 addCmd->SetArg( 1, hypID );
1078 if ( geom != GetGeom() )
1079 addCmd->SetArg( 2, geom );
1083 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
1084 // for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
1085 // Handle(_pyCommand) subCmd = *cmd;
1086 // if ( subCmd->GetNbArgs() > 0 )
1087 // AddMeshAccess( subCmd );
1089 myAddHypCmds.clear();
1090 mySubmeshes.clear();
1093 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1094 for ( ; hyp != myHypos.end(); ++hyp )
1098 //================================================================================
1100 * \brief MeshEditor convert its commands to ones of mesh
1102 //================================================================================
1104 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
1105 _pyObject( theCreationCmd )
1107 myMesh = theCreationCmd->GetObject();
1108 myCreationCmdStr = theCreationCmd->GetString();
1109 theCreationCmd->Clear();
1112 //================================================================================
1114 * \brief convert its commands to ones of mesh
1116 //================================================================================
1118 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
1120 // names of SMESH_MeshEditor methods fully equal to methods of python class Mesh, so
1121 // commands calling this methods are converted to calls of methods of Mesh
1122 static TStringSet sameMethods;
1123 if ( sameMethods.empty() ) {
1124 const char * names[] = {
1125 "RemoveElements","RemoveNodes","RemoveOrphanNodes","AddNode","Add0DElement","AddEdge","AddFace","AddPolygonalFace",
1126 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode", "MoveClosestNodeToPoint",
1127 "InverseDiag","DeleteDiag","Reorient","ReorientObject","TriToQuad","SplitQuad","SplitQuadObject",
1128 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
1129 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
1130 "RotationSweep","RotationSweepObject","RotationSweepObject1D","RotationSweepObject2D",
1131 "ExtrusionSweep","AdvancedExtrusion","ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D",
1132 "ExtrusionAlongPath","ExtrusionAlongPathObject","ExtrusionAlongPathX",
1133 "ExtrusionAlongPathObject1D","ExtrusionAlongPathObject2D",
1134 "Mirror","MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
1135 "FindCoincidentNodes",/*"FindCoincidentNodesOnPart",*/"MergeNodes","FindEqualElements",
1136 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
1137 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
1138 "GetLastCreatedElems",
1139 "MirrorMakeMesh","MirrorObjectMakeMesh","TranslateMakeMesh",
1140 "TranslateObjectMakeMesh","RotateMakeMesh","RotateObjectMakeMesh","MakeBoundaryMesh"
1141 ,"" }; // <- mark of the end
1142 sameMethods.Insert( names );
1145 // names of SMESH_MeshEditor methods which differ from methods of class Mesh
1146 // only by last two arguments
1147 static TStringSet diffLastTwoArgsMethods;
1148 if (diffLastTwoArgsMethods.empty() ) {
1149 const char * names[] = {
1150 "MirrorMakeGroups","MirrorObjectMakeGroups",
1151 "TranslateMakeGroups","TranslateObjectMakeGroups",
1152 "RotateMakeGroups","RotateObjectMakeGroups",
1153 ""};// <- mark of the end
1154 diffLastTwoArgsMethods.Insert( names );
1157 const TCollection_AsciiString & method = theCommand->GetMethod();
1158 bool isPyMeshMethod = sameMethods.Contains( method );
1159 if ( !isPyMeshMethod )
1161 //Replace SMESH_MeshEditor "MakeGroups" functions on the Mesh
1162 //functions with the flag "theMakeGroups = True" like:
1163 //SMESH_MeshEditor.CmdMakeGroups => Mesh.Cmd(...,True)
1164 int pos = method.Search("MakeGroups");
1167 isPyMeshMethod = true;
1169 // 1. Remove "MakeGroups" from the Command
1170 TCollection_AsciiString aMethod = theCommand->GetMethod();
1171 int nbArgsToAdd = diffLastTwoArgsMethods.Contains(aMethod) ? 2 : 1;
1172 aMethod.Trunc(pos-1);
1173 theCommand->SetMethod(aMethod);
1175 // 2. And add last "True" argument(s)
1176 while(nbArgsToAdd--)
1177 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
1181 // set "ExtrusionAlongPathX()" instead of "ExtrusionAlongPathObjX()"
1182 if ( !isPyMeshMethod && method == "ExtrusionAlongPathObjX")
1184 isPyMeshMethod=true;
1185 theCommand->SetMethod("ExtrusionAlongPathX");
1188 // set "FindCoincidentNodesOnPart()" instead of "FindCoincidentNodesOnPartBut()"
1189 if ( !isPyMeshMethod && method == "FindCoincidentNodesOnPartBut")
1191 isPyMeshMethod=true;
1192 theCommand->SetMethod("FindCoincidentNodesOnPart");
1194 // DoubleNodeElemGroupNew() -> DoubleNodeElemGroup()
1195 if ( !isPyMeshMethod && ( method == "DoubleNodeElemGroupNew" || method == "DoubleNodeGroupNew"))
1197 isPyMeshMethod=true;
1198 theCommand->SetMethod( method.SubString( 1, method.Length()-3));
1199 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
1202 // meshes made by *MakeMesh() methods are not wrapped by _pyMesh,
1203 // so let _pyMesh care of it (TMP?)
1204 // if ( theCommand->GetMethod().Search("MakeMesh") != -1 )
1205 // _pyMesh( new _pyCommand( theCommand->GetString(), 0 )); // for theGen->SetAccessorMethod()
1206 if ( isPyMeshMethod )
1208 theCommand->SetObject( myMesh );
1212 // editor creation command is needed only if any editor function is called
1213 theGen->AddMeshAccessorMethod( theCommand ); // for *Object()
1214 if ( !myCreationCmdStr.IsEmpty() ) {
1215 GetCreationCmd()->GetString() = myCreationCmdStr;
1216 myCreationCmdStr.Clear();
1221 //================================================================================
1223 * \brief _pyHypothesis constructor
1224 * \param theCreationCmd -
1226 //================================================================================
1228 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
1229 _pyObject( theCreationCmd )
1231 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
1234 //================================================================================
1236 * \brief Creates algorithm or hypothesis
1237 * \param theCreationCmd - The engine command creating a hypothesis
1238 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
1240 //================================================================================
1242 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
1244 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
1245 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
1247 Handle(_pyHypothesis) hyp, algo;
1250 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
1251 if ( hypTypeQuoted.IsEmpty() )
1254 TCollection_AsciiString hypType =
1255 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1257 algo = new _pyAlgorithm( theCreationCmd );
1258 hyp = new _pyHypothesis( theCreationCmd );
1260 // 1D Regular_1D ----------
1261 if ( hypType == "Regular_1D" ) {
1262 // set mesh's method creating algo,
1263 // i.e. convertion result will be "regular1d = Mesh.Segment()",
1264 // and set hypType by which algo creating a hypothesis is searched for
1265 algo->SetConvMethodAndType("Segment", hypType.ToCString());
1267 else if ( hypType == "CompositeSegment_1D" ) {
1268 algo->SetConvMethodAndType("Segment", "Regular_1D");
1269 algo->myArgs.Append( "algo=smesh.COMPOSITE");
1271 else if ( hypType == "LocalLength" ) {
1272 // set algo's method creating hyp, and algo type
1273 hyp->SetConvMethodAndType( "LocalLength", "Regular_1D");
1274 // set method whose 1 arg will become the 1-st arg of hyp creation command
1275 // i.e. convertion result will be "locallength = regular1d.LocalLength(<arg of SetLength()>)"
1276 hyp->AddArgMethod( "SetLength" );
1278 else if ( hypType == "MaxLength" ) {
1279 // set algo's method creating hyp, and algo type
1280 hyp->SetConvMethodAndType( "MaxSize", "Regular_1D");
1281 // set method whose 1 arg will become the 1-st arg of hyp creation command
1282 // i.e. convertion result will be "maxsize = regular1d.MaxSize(<arg of SetLength()>)"
1283 hyp->AddArgMethod( "SetLength" );
1285 else if ( hypType == "NumberOfSegments" ) {
1286 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
1287 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
1288 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
1289 hyp->AddArgMethod( "SetNumberOfSegments" );
1290 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
1291 hyp->AddArgMethod( "SetScaleFactor" );
1292 hyp->AddArgMethod( "SetReversedEdges" );
1294 else if ( hypType == "Arithmetic1D" ) {
1295 hyp = new _pyComplexParamHypo( theCreationCmd );
1296 hyp->SetConvMethodAndType( "Arithmetic1D", "Regular_1D");
1297 hyp->AddArgMethod( "SetStartLength" );
1298 hyp->AddArgMethod( "SetEndLength" );
1299 hyp->AddArgMethod( "SetReversedEdges" );
1301 else if ( hypType == "StartEndLength" ) {
1302 hyp = new _pyComplexParamHypo( theCreationCmd );
1303 hyp->SetConvMethodAndType( "StartEndLength", "Regular_1D");
1304 hyp->AddArgMethod( "SetStartLength" );
1305 hyp->AddArgMethod( "SetEndLength" );
1306 hyp->AddArgMethod( "SetReversedEdges" );
1308 else if ( hypType == "Deflection1D" ) {
1309 hyp->SetConvMethodAndType( "Deflection1D", "Regular_1D");
1310 hyp->AddArgMethod( "SetDeflection" );
1312 else if ( hypType == "Propagation" ) {
1313 hyp->SetConvMethodAndType( "Propagation", "Regular_1D");
1315 else if ( hypType == "QuadraticMesh" ) {
1316 hyp->SetConvMethodAndType( "QuadraticMesh", "Regular_1D");
1318 else if ( hypType == "AutomaticLength" ) {
1319 hyp->SetConvMethodAndType( "AutomaticLength", "Regular_1D");
1320 hyp->AddArgMethod( "SetFineness");
1322 else if ( hypType == "SegmentLengthAroundVertex" ) {
1323 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
1324 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
1325 hyp->AddArgMethod( "SetLength" );
1327 // 1D Python_1D ----------
1328 else if ( hypType == "Python_1D" ) {
1329 algo->SetConvMethodAndType( "Segment", hypType.ToCString());
1330 algo->myArgs.Append( "algo=smesh.PYTHON");
1332 else if ( hypType == "PythonSplit1D" ) {
1333 hyp->SetConvMethodAndType( "PythonSplit1D", "Python_1D");
1334 hyp->AddArgMethod( "SetNumberOfSegments");
1335 hyp->AddArgMethod( "SetPythonLog10RatioFunction");
1337 // MEFISTO_2D ----------
1338 else if ( hypType == "MEFISTO_2D" ) { // MEFISTO_2D
1339 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
1341 else if ( hypType == "MaxElementArea" ) {
1342 hyp->SetConvMethodAndType( "MaxElementArea", "MEFISTO_2D");
1343 hyp->SetConvMethodAndType( "MaxElementArea", "NETGEN_2D_ONLY");
1344 hyp->AddArgMethod( "SetMaxElementArea");
1346 else if ( hypType == "LengthFromEdges" ) {
1347 hyp->SetConvMethodAndType( "LengthFromEdges", "MEFISTO_2D");
1348 hyp->SetConvMethodAndType( "LengthFromEdges", "NETGEN_2D_ONLY");
1350 // Quadrangle_2D ----------
1351 else if ( hypType == "Quadrangle_2D" ) {
1352 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
1354 else if ( hypType == "QuadranglePreference" ) {
1355 hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
1356 hyp->SetConvMethodAndType( "SetQuadAllowed", "NETGEN_2D_ONLY");
1358 else if ( hypType == "TrianglePreference" ) {
1359 hyp->SetConvMethodAndType( "TrianglePreference", "Quadrangle_2D");
1361 // RadialQuadrangle_1D2D ----------
1362 else if ( hypType == "RadialQuadrangle_1D2D" ) {
1363 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
1364 algo->myArgs.Append( "algo=smesh.RADIAL_QUAD" );
1366 else if ( hypType == "NumberOfLayers2D" ) {
1367 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialQuadrangle_1D2D");
1368 hyp->AddArgMethod( "SetNumberOfLayers" );
1370 else if ( hypType == "LayerDistribution2D" ) {
1371 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get2DHypothesis" );
1372 hyp->SetConvMethodAndType( "LayerDistribution", "RadialQuadrangle_1D2D");
1374 // BLSURF ----------
1375 else if ( hypType == "BLSURF" ) {
1376 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
1377 algo->myArgs.Append( "algo=smesh.BLSURF" );
1379 else if ( hypType == "BLSURF_Parameters") {
1380 hyp->SetConvMethodAndType( "Parameters", "BLSURF");
1382 // NETGEN ----------
1383 else if ( hypType == "NETGEN_2D") { // 1D-2D
1384 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1385 algo->myArgs.Append( "algo=smesh.NETGEN" );
1387 else if ( hypType == "NETGEN_Parameters_2D") {
1388 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D");
1390 else if ( hypType == "NETGEN_SimpleParameters_2D") {
1391 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D");
1392 hyp->myArgs.Append( "which=smesh.SIMPLE" );
1394 else if ( hypType == "NETGEN_2D3D") { // 1D-2D-3D
1395 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1396 algo->myArgs.Append( "algo=smesh.FULL_NETGEN" );
1398 else if ( hypType == "NETGEN_Parameters") {
1399 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D3D");
1401 else if ( hypType == "NETGEN_SimpleParameters_3D") {
1402 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D3D");
1403 hyp->myArgs.Append( "which=smesh.SIMPLE" );
1405 else if ( hypType == "NETGEN_2D_ONLY") { // 2D
1406 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1407 algo->myArgs.Append( "algo=smesh.NETGEN_2D" );
1409 else if ( hypType == "NETGEN_3D") { // 3D
1410 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1411 algo->myArgs.Append( "algo=smesh.NETGEN" );
1413 else if ( hypType == "MaxElementVolume") {
1414 hyp->SetConvMethodAndType( "MaxElementVolume", "NETGEN_3D");
1415 hyp->AddArgMethod( "SetMaxElementVolume" );
1417 // GHS3D_3D ----------
1418 else if ( hypType == "GHS3D_3D" ) {
1419 algo->SetConvMethodAndType( "Tetrahedron", hypType.ToCString());
1420 algo->myArgs.Append( "algo=smesh.GHS3D" );
1422 else if ( hypType == "GHS3D_Parameters") {
1423 hyp->SetConvMethodAndType( "Parameters", "GHS3D_3D");
1425 // Hexa_3D ---------
1426 else if ( hypType == "BLSURF" ) {
1427 algo->SetConvMethodAndType( "Hexahedron", hypType.ToCString());
1429 // Repetitive Projection_1D ---------
1430 else if ( hypType == "Projection_1D" ) {
1431 algo->SetConvMethodAndType( "Projection1D", hypType.ToCString());
1433 else if ( hypType == "ProjectionSource1D" ) {
1434 hyp->SetConvMethodAndType( "SourceEdge", "Projection_1D");
1435 hyp->AddArgMethod( "SetSourceEdge");
1436 hyp->AddArgMethod( "SetSourceMesh");
1437 // 2 args of SetVertexAssociation() will become the 3-th and 4-th args of hyp creation command
1438 hyp->AddArgMethod( "SetVertexAssociation", 2 );
1440 // Projection_2D ---------
1441 else if ( hypType == "Projection_2D" ) {
1442 algo->SetConvMethodAndType( "Projection2D", hypType.ToCString());
1444 else if ( hypType == "ProjectionSource2D" ) {
1445 hyp->SetConvMethodAndType( "SourceFace", "Projection_2D");
1446 hyp->AddArgMethod( "SetSourceFace");
1447 hyp->AddArgMethod( "SetSourceMesh");
1448 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1450 // Projection_3D ---------
1451 else if ( hypType == "Projection_3D" ) {
1452 algo->SetConvMethodAndType( "Projection3D", hypType.ToCString());
1454 else if ( hypType == "ProjectionSource3D" ) {
1455 hyp->SetConvMethodAndType( "SourceShape3D", "Projection_3D");
1456 hyp->AddArgMethod( "SetSource3DShape");
1457 hyp->AddArgMethod( "SetSourceMesh");
1458 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1460 // Prism_3D ---------
1461 else if ( hypType == "Prism_3D" ) {
1462 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1464 // RadialPrism_3D ---------
1465 else if ( hypType == "RadialPrism_3D" ) {
1466 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1468 else if ( hypType == "NumberOfLayers" ) {
1469 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialPrism_3D");
1470 hyp->AddArgMethod( "SetNumberOfLayers" );
1472 else if ( hypType == "LayerDistribution" ) {
1473 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get3DHypothesis" );
1474 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
1477 return algo->IsValid() ? algo : hyp;
1480 //================================================================================
1482 * \brief Convert the command adding a hypothesis to mesh into a smesh command
1483 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
1484 * \param theAlgo - The algo that can create this hypo
1485 * \retval bool - false if the command cant be converted
1487 //================================================================================
1489 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
1490 const _pyID& theMesh)
1492 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
1494 if ( !IsWrappable( theMesh ))
1497 myGeom = theCmd->GetArg( 1 );
1499 Handle(_pyHypothesis) algo;
1501 // find algo created on myGeom in theMesh
1502 algo = theGen->FindAlgo( myGeom, theMesh, this );
1503 if ( algo.IsNull() )
1505 // attach hypothesis creation command to be after algo creation command
1506 // because it can be new created instance of algorithm
1507 algo->GetCreationCmd()->AddDependantCmd( theCmd );
1511 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
1512 theCmd->SetResultValue( GetID() );
1513 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
1514 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
1516 theCmd->RemoveArgs();
1517 for ( int i = 1; i <= myArgs.Length(); ++i ) {
1518 if ( !myArgs( i ).IsEmpty() )
1519 theCmd->SetArg( i, myArgs( i ));
1521 theCmd->SetArg( i, "[]");
1523 // set a new creation command
1524 GetCreationCmd()->Clear();
1525 // replace creation command by wrapped instance
1526 // please note, that hypothesis attaches to algo creation command (see upper)
1527 SetCreationCmd( theCmd );
1530 // clear commands setting arg values
1531 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
1532 for ( ; argCmd != myArgCommands.end(); ++argCmd )
1535 // set unknown arg commands after hypo creation
1536 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
1537 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
1538 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1539 afterCmd->AddDependantCmd( *cmd );
1545 //================================================================================
1547 * \brief Remember hypothesis parameter values
1548 * \param theCommand - The called hypothesis method
1550 //================================================================================
1552 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
1554 ASSERT( !myIsAlgo );
1557 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
1558 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
1559 while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
1560 myArgs.Append( "[]" );
1561 for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
1562 myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
1563 myArgCommands.push_back( theCommand );
1566 nbArgs += myNbArgsByMethod( i );
1568 myUnknownCommands.push_back( theCommand );
1571 //================================================================================
1573 * \brief Finish conversion
1575 //================================================================================
1577 void _pyHypothesis::Flush()
1579 if ( IsWrapped() ) {
1582 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1583 for ( ; cmd != myArgCommands.end(); ++cmd ) {
1584 // Add access to a wrapped mesh
1585 theGen->AddMeshAccessorMethod( *cmd );
1586 // Add access to a wrapped algorithm
1587 theGen->AddAlgoAccessorMethod( *cmd );
1589 cmd = myUnknownCommands.begin();
1590 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1591 // Add access to a wrapped mesh
1592 theGen->AddMeshAccessorMethod( *cmd );
1593 // Add access to a wrapped algorithm
1594 theGen->AddAlgoAccessorMethod( *cmd );
1597 // forget previous hypothesis modifications
1598 myArgCommands.clear();
1599 myUnknownCommands.clear();
1602 //================================================================================
1604 * \brief clear creation, arg and unkown commands
1606 //================================================================================
1608 void _pyHypothesis::ClearAllCommands()
1610 GetCreationCmd()->Clear();
1611 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1612 for ( ; cmd != myArgCommands.end(); ++cmd )
1614 cmd = myUnknownCommands.begin();
1615 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1620 //================================================================================
1622 * \brief Assign fields of theOther to me except myIsWrapped
1624 //================================================================================
1626 void _pyHypothesis::Assign( const Handle(_pyHypothesis)& theOther,
1627 const _pyID& theMesh )
1629 myIsWrapped = false;
1632 // myCreationCmd = theOther->myCreationCmd;
1633 myIsAlgo = theOther->myIsAlgo;
1634 myGeom = theOther->myGeom;
1635 myType2CreationMethod = theOther->myType2CreationMethod;
1636 myArgs = theOther->myArgs;
1637 myArgMethods = theOther->myArgMethods;
1638 myNbArgsByMethod = theOther->myNbArgsByMethod;
1639 myArgCommands = theOther->myArgCommands;
1640 myUnknownCommands = theOther->myUnknownCommands;
1643 //================================================================================
1645 * \brief Remember hypothesis parameter values
1646 * \param theCommand - The called hypothesis method
1648 //================================================================================
1650 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1652 if( theCommand->GetMethod() == "SetLength" )
1654 // NOW it becomes OBSOLETE
1655 // ex: hyp.SetLength(start, 1)
1656 // hyp.SetLength(end, 0)
1657 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1658 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1659 while ( myArgs.Length() < i )
1660 myArgs.Append( "[]" );
1661 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1662 myArgCommands.push_back( theCommand );
1666 _pyHypothesis::Process( theCommand );
1669 //================================================================================
1671 * \brief Clear SetObjectEntry() as it is called by methods of Mesh_Segment
1673 //================================================================================
1675 void _pyComplexParamHypo::Flush()
1679 list < Handle(_pyCommand) >::iterator cmd = myUnknownCommands.begin();
1680 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1681 if ((*cmd)->GetMethod() == "SetObjectEntry" )
1686 //================================================================================
1688 * \brief Convert methods of 1D hypotheses to my own methods
1689 * \param theCommand - The called hypothesis method
1691 //================================================================================
1693 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1695 if ( theCommand->GetMethod() != "SetLayerDistribution" )
1698 _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1700 const _pyID& hyp1dID = theCommand->GetArg( 1 );
1701 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1702 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1704 else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1705 // 1D hypo is already set, so distribution changes and the old
1706 // 1D hypo is thrown away
1707 my1dHyp->ClearAllCommands();
1711 if ( !myArgCommands.empty() )
1712 myArgCommands.front()->Clear();
1713 myArgCommands.push_back( theCommand );
1716 //================================================================================
1719 * \param theAdditionCmd - command to be converted
1720 * \param theMesh - mesh instance
1721 * \retval bool - status
1723 //================================================================================
1725 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1726 const _pyID& theMesh)
1728 myIsWrapped = false;
1730 if ( my1dHyp.IsNull() )
1733 // set "SetLayerDistribution()" after addition cmd
1734 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1736 _pyID geom = theAdditionCmd->GetArg( 1 );
1738 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
1739 if ( !algo.IsNull() )
1741 my1dHyp->SetMesh( theMesh );
1742 my1dHyp->SetConvMethodAndType(my1dHyp->GetAlgoCreationMethod().ToCString(),
1743 algo->GetAlgoType().ToCString());
1744 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1747 // clear "SetLayerDistribution()" cmd
1748 myArgCommands.back()->Clear();
1750 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1752 // find RadialPrism algo created on <geom> for theMesh
1753 GetCreationCmd()->SetObject( algo->GetID() );
1754 GetCreationCmd()->SetMethod( myAlgoMethod );
1755 GetCreationCmd()->RemoveArgs();
1756 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1762 //================================================================================
1766 //================================================================================
1768 void _pyLayerDistributionHypo::Flush()
1770 // as creation of 1D hyp was written later then it's edition,
1771 // we need to find all it's edition calls and process them
1772 if ( !my1dHyp.IsNull() )
1774 _pyID hyp1dID = my1dHyp->GetCreationCmd()->GetResultValue();
1776 // make a new name for 1D hyp = "HypType" + "_Distribution"
1778 if ( my1dHyp->IsWrapped() ) {
1779 newName = my1dHyp->GetCreationCmd()->GetMethod();
1782 TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1783 newName = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1785 newName += "_Distribution";
1786 my1dHyp->GetCreationCmd()->SetResultValue( newName );
1788 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1789 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1790 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1791 const _pyID& objID = (*cmdIt)->GetObject();
1792 if ( objID == hyp1dID ) {
1793 my1dHyp->Process( *cmdIt );
1794 my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1795 ( *cmdIt )->SetObject( newName );
1798 // Set new hyp name to SetLayerDistribution() cmd
1799 if ( !myArgCommands.empty() && !myArgCommands.back()->IsEmpty() )
1800 myArgCommands.back()->SetArg( 1, newName );
1804 //================================================================================
1806 * \brief additionally to Addition2Creation, clears SetDistrType() command
1807 * \param theCmd - AddHypothesis() command
1808 * \param theMesh - mesh to which a hypothesis is added
1809 * \retval bool - convertion result
1811 //================================================================================
1813 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1814 const _pyID& theMesh)
1816 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1817 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1818 bool scaleDistrType = false;
1819 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1820 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1821 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1822 if ( (*cmd)->GetArg( 1 ) == "1" ) {
1823 scaleDistrType = true;
1826 else if ( !scaleDistrType ) {
1827 // distribution type changed: remove scale factor from args
1828 myArgs.Remove( 2, myArgs.Length() );
1834 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1837 //================================================================================
1839 * \brief remove repeated commands defining distribution
1841 //================================================================================
1843 void _pyNumberOfSegmentsHyp::Flush()
1845 // find number of the last SetDistrType() command
1846 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1847 int distrTypeNb = 0;
1848 for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1849 if ( (*cmd)->GetMethod() == "SetDistrType" )
1850 distrTypeNb = (*cmd)->GetOrderNb();
1851 else if (IsWrapped() && (*cmd)->GetMethod() == "SetObjectEntry" )
1854 // clear commands before the last SetDistrType()
1855 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1856 for ( int i = 0; i < 2; ++i ) {
1857 set<TCollection_AsciiString> uniqueMethods;
1858 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1859 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1861 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1862 const TCollection_AsciiString& method = (*cmd)->GetMethod();
1863 if ( !clear || method == "SetNumberOfSegments" ) {
1864 bool isNewInSet = uniqueMethods.insert( method ).second;
1865 clear = !isNewInSet;
1874 //================================================================================
1876 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
1877 * into regular1D.LengthNearVertex( length, vertex )
1878 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
1879 * \param theMesh - The mesh needing this hypo
1880 * \retval bool - false if the command cant be converted
1882 //================================================================================
1884 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1885 const _pyID& theMeshID)
1887 if ( IsWrappable( theMeshID )) {
1889 _pyID vertex = theCmd->GetArg( 1 );
1891 // the problem here is that segment algo will not be found
1892 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
1893 // geometry where segment algorithm is assigned
1894 Handle(_pyHypothesis) algo;
1895 _pyID geom = vertex;
1896 while ( algo.IsNull() && !geom.IsEmpty()) {
1897 // try to find geom as a father of <vertex>
1898 geom = FatherID( geom );
1899 algo = theGen->FindAlgo( geom, theMeshID, this );
1901 if ( algo.IsNull() )
1902 return false; // also possible to find geom as brother of veretex...
1903 // set geom instead of vertex
1904 theCmd->SetArg( 1, geom );
1906 // set vertex as a second arg
1907 if ( myArgs.Length() < 1) myArgs.Append( "1" ); // :(
1908 myArgs.Append( vertex );
1910 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
1911 // theMeshID.LengthNearVertex( length, vertex )
1912 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
1917 //================================================================================
1919 * \brief _pyAlgorithm constructor
1920 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1922 //================================================================================
1924 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1925 : _pyHypothesis( theCreationCmd )
1930 //================================================================================
1932 * \brief Convert the command adding an algorithm to mesh
1933 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1934 * \param theMesh - The mesh needing this algo
1935 * \retval bool - false if the command cant be converted
1937 //================================================================================
1939 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1940 const _pyID& theMeshID)
1942 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1943 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1944 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1950 //================================================================================
1952 * \brief Return starting position of a part of python command
1953 * \param thePartIndex - The index of command part
1954 * \retval int - Part position
1956 //================================================================================
1958 int _pyCommand::GetBegPos( int thePartIndex )
1962 if ( myBegPos.Length() < thePartIndex )
1964 return myBegPos( thePartIndex );
1967 //================================================================================
1969 * \brief Store starting position of a part of python command
1970 * \param thePartIndex - The index of command part
1971 * \param thePosition - Part position
1973 //================================================================================
1975 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1977 while ( myBegPos.Length() < thePartIndex )
1978 myBegPos.Append( UNKNOWN );
1979 myBegPos( thePartIndex ) = thePosition;
1982 //================================================================================
1984 * \brief Returns whitespace symbols at the line beginning
1985 * \retval TCollection_AsciiString - result
1987 //================================================================================
1989 TCollection_AsciiString _pyCommand::GetIndentation()
1992 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1993 GetWord( myString, end, true );
1995 end = GetBegPos( RESULT_IND );
1996 return myString.SubString( 1, end - 1 );
1999 //================================================================================
2001 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
2002 * \retval const TCollection_AsciiString & - ResultValue substring
2004 //================================================================================
2006 const TCollection_AsciiString & _pyCommand::GetResultValue()
2008 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
2010 int begPos = myString.Location( "=", 1, Length() );
2012 myRes = GetWord( myString, begPos, false );
2015 SetBegPos( RESULT_IND, begPos );
2020 //================================================================================
2022 * \brief Return number of python command result value ResultValue = Obj.Meth()
2025 //================================================================================
2027 const int _pyCommand::GetNbResultValues()
2031 int endPos = myString.Location( "=", 1, Length() );
2032 TCollection_AsciiString str = "";
2033 while ( begPos < endPos) {
2034 str = GetWord( myString, begPos, true );
2035 begPos = begPos+ str.Length();
2042 //================================================================================
2044 * \brief Return substring of python command looking like
2045 * ResultValue1 , ResultValue1,... = Obj.Meth() with res index
2046 * \retval const TCollection_AsciiString & - ResultValue with res index substring
2048 //================================================================================
2049 const TCollection_AsciiString & _pyCommand::GetResultValue(int res)
2053 int endPos = myString.Location( "=", 1, Length() );
2054 while ( begPos < endPos) {
2055 myRes = GetWord( myString, begPos, true );
2056 begPos = begPos + myRes.Length();
2059 myRes.RemoveAll('[');myRes.RemoveAll(']');
2065 return theEmptyString;
2068 //================================================================================
2070 * \brief Return substring of python command looking like ResVal = Object.Meth()
2071 * \retval const TCollection_AsciiString & - Object substring
2073 //================================================================================
2075 const TCollection_AsciiString & _pyCommand::GetObject()
2077 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
2080 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
2082 begPos = myString.Location( "=", 1, Length() ) + 1;
2083 // is '=' in the string argument (for example, name) or not
2084 int nb1 = 0; // number of ' character at the left of =
2085 int nb2 = 0; // number of " character at the left of =
2086 for ( int i = 1; i < begPos-1; i++ ) {
2087 if ( myString.Value( i )=='\'' )
2089 else if ( myString.Value( i )=='"' )
2092 // if number of ' or " is not divisible by 2,
2093 // then get an object at the start of the command
2094 if ( nb1 % 2 != 0 || nb2 % 2 != 0 )
2097 myObj = GetWord( myString, begPos, true );
2098 // check if object is complex,
2099 // so far consider case like "smesh.smesh.Method()"
2100 if ( int bracketPos = myString.Location( "(", begPos, Length() )) {
2101 //if ( bracketPos==0 ) bracketPos = Length();
2102 int dotPos = begPos+myObj.Length();
2103 while ( dotPos+1 < bracketPos ) {
2104 if ( int pos = myString.Location( ".", dotPos+1, bracketPos ))
2109 if ( dotPos > begPos+myObj.Length() )
2110 myObj = myString.SubString( begPos, dotPos-1 );
2113 SetBegPos( OBJECT_IND, begPos );
2119 //================================================================================
2121 * \brief Return substring of python command looking like ResVal = Obj.Method()
2122 * \retval const TCollection_AsciiString & - Method substring
2124 //================================================================================
2126 const TCollection_AsciiString & _pyCommand::GetMethod()
2128 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
2131 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
2132 bool forward = true;
2134 begPos = myString.Location( "(", 1, Length() ) - 1;
2138 myMeth = GetWord( myString, begPos, forward );
2139 SetBegPos( METHOD_IND, begPos );
2145 //================================================================================
2147 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
2148 * \retval const TCollection_AsciiString & - Arg<index> substring
2150 //================================================================================
2152 const TCollection_AsciiString & _pyCommand::GetArg( int index )
2154 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
2157 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
2159 begPos = myString.Location( "(", 1, Length() ) + 1;
2161 int i = 0, prevLen = 0, nbNestings = 0;
2162 while ( begPos != EMPTY ) {
2164 if( myString.Value( begPos ) == '(' )
2166 // check if we are looking at the closing parenthesis
2167 while ( begPos <= Length() && isspace( myString.Value( begPos )))
2169 if ( begPos > Length() )
2171 if ( myString.Value( begPos ) == ')' ) {
2173 if( nbNestings == 0 )
2176 myArgs.Append( GetWord( myString, begPos, true, true ));
2177 SetBegPos( ARG1_IND + i, begPos );
2178 prevLen = myArgs.Last().Length();
2180 myArgs.Remove( myArgs.Length() ); // no more args
2184 if ( myArgs.Length() < index )
2185 return theEmptyString;
2186 return myArgs( index );
2189 //================================================================================
2191 * \brief Check if char is a word part
2192 * \param c - The character to check
2193 * \retval bool - The check result
2195 //================================================================================
2197 static inline bool isWord(const char c, const bool dotIsWord)
2200 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
2203 //================================================================================
2205 * \brief Looks for a word in the string and returns word's beginning
2206 * \param theString - The input string
2207 * \param theStartPos - The position to start the search, returning word's beginning
2208 * \param theForward - The search direction
2209 * \retval TCollection_AsciiString - The found word
2211 //================================================================================
2213 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
2215 const bool theForward,
2216 const bool dotIsWord )
2218 int beg = theStartPos, end = theStartPos;
2219 theStartPos = EMPTY;
2220 if ( beg < 1 || beg > theString.Length() )
2221 return theEmptyString;
2223 if ( theForward ) { // search forward
2225 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
2227 if ( beg > theString.Length() )
2228 return theEmptyString; // no word found
2231 char begChar = theString.Value( beg );
2232 if ( begChar == '"' || begChar == '\'' || begChar == '[') {
2233 char endChar = ( begChar == '[' ) ? ']' : begChar;
2234 // end is at the corresponding quoting mark or bracket
2235 while ( end < theString.Length() &&
2236 ( theString.Value( end ) != endChar || theString.Value( end-1 ) == '\\'))
2240 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
2245 else { // search backward
2247 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
2250 return theEmptyString; // no word found
2252 char endChar = theString.Value( end );
2253 if ( endChar == '"' || endChar == '\'' ) {
2254 // beg is at the corresponding quoting mark
2256 ( theString.Value( beg ) != endChar || theString.Value( beg-1 ) == '\\'))
2260 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
2266 //cout << theString << " ---- " << beg << " - " << end << endl;
2267 return theString.SubString( beg, end );
2270 //================================================================================
2272 * \brief Look for position where not space char is
2273 * \param theString - The string
2274 * \param thePos - The position to search from and which returns result
2275 * \retval bool - false if there are only space after thePos in theString
2279 //================================================================================
2281 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
2283 if ( thePos < 1 || thePos > theString.Length() )
2286 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
2289 return thePos <= theString.Length();
2292 //================================================================================
2294 * \brief Modify a part of the command
2295 * \param thePartIndex - The index of the part
2296 * \param thePart - The new part string
2297 * \param theOldPart - The old part
2299 //================================================================================
2301 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
2302 TCollection_AsciiString& theOldPart)
2304 int pos = GetBegPos( thePartIndex );
2305 if ( pos <= Length() && theOldPart != thePart)
2307 TCollection_AsciiString seperator;
2309 pos = GetBegPos( thePartIndex + 1 );
2310 if ( pos < 1 ) return;
2311 switch ( thePartIndex ) {
2312 case RESULT_IND: seperator = " = "; break;
2313 case OBJECT_IND: seperator = "."; break;
2314 case METHOD_IND: seperator = "()"; break;
2318 myString.Remove( pos, theOldPart.Length() );
2319 if ( !seperator.IsEmpty() )
2320 myString.Insert( pos , seperator );
2321 myString.Insert( pos, thePart );
2322 // update starting positions of the following parts
2323 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
2324 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
2325 if ( myBegPos( i ) > 0 )
2326 myBegPos( i ) += posDelta;
2328 theOldPart = thePart;
2332 //================================================================================
2334 * \brief Set agrument
2335 * \param index - The argument index, it counts from 1
2336 * \param theArg - The argument string
2338 //================================================================================
2340 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
2343 int argInd = ARG1_IND + index - 1;
2344 int pos = GetBegPos( argInd );
2345 if ( pos < 1 ) // no index-th arg exist, append inexistent args
2347 // find a closing parenthesis
2348 if ( GetNbArgs() != 0 && index <= GetNbArgs() ) {
2349 int lastArgInd = GetNbArgs();
2350 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
2351 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
2356 while ( pos > 0 && myString.Value( pos ) != ')' )
2359 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
2363 while ( myArgs.Length() < index ) {
2364 if ( myArgs.Length() )
2365 myString.Insert( pos++, "," );
2366 myArgs.Append("None");
2367 myString.Insert( pos, myArgs.Last() );
2368 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
2369 pos += myArgs.Last().Length();
2372 SetPart( argInd, theArg, myArgs( index ));
2375 //================================================================================
2377 * \brief Empty arg list
2379 //================================================================================
2381 void _pyCommand::RemoveArgs()
2383 if ( int pos = myString.Location( '(', 1, Length() ))
2384 myString.Trunc( pos );
2387 if ( myBegPos.Length() >= ARG1_IND )
2388 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
2391 //================================================================================
2393 * \brief Set dependent commands after this one
2395 //================================================================================
2397 bool _pyCommand::SetDependentCmdsAfter() const
2399 bool orderChanged = false;
2400 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
2401 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
2402 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
2403 orderChanged = true;
2404 theGen->SetCommandAfter( *cmd, this );
2405 (*cmd)->SetDependentCmdsAfter();
2408 return orderChanged;
2410 //================================================================================
2412 * \brief Insert accessor method after theObjectID
2413 * \param theObjectID - id of the accessed object
2414 * \param theAcsMethod - name of the method giving access to the object
2415 * \retval bool - false if theObjectID is not found in the command string
2417 //================================================================================
2419 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
2421 if ( !theAcsMethod )
2423 // start object search from the object, i.e. ignore result
2425 int beg = GetBegPos( OBJECT_IND );
2426 if ( beg < 1 || beg > Length() )
2429 while (( beg = myString.Location( theObjectID, beg, Length() )))
2431 // check that theObjectID is not just a part of a longer ID
2432 int afterEnd = beg + theObjectID.Length();
2433 Standard_Character c = myString.Value( afterEnd );
2434 if ( !isalnum( c ) && c != ':' ) {
2435 // check if accessor method already present
2437 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
2439 int oldLen = Length();
2440 myString.Insert( afterEnd, (char*) theAcsMethod );
2441 myString.Insert( afterEnd, "." );
2442 // update starting positions of the parts following the modified one
2443 int posDelta = Length() - oldLen;
2444 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
2445 if ( myBegPos( i ) > afterEnd )
2446 myBegPos( i ) += posDelta;
2451 beg = afterEnd; // is a part - next search
2456 //================================================================================
2458 * \brief Return method name giving access to an interaface object wrapped by python class
2459 * \retval const char* - method name
2461 //================================================================================
2463 const char* _pyObject::AccessorMethod() const
2467 //================================================================================
2469 * \brief Return ID of a father
2471 //================================================================================
2473 _pyID _pyObject::FatherID(const _pyID & childID)
2475 int colPos = childID.SearchFromEnd(':');
2477 return childID.SubString( 1, colPos-1 );
2481 //================================================================================
2483 * \brief SelfEraser erases creation command if no more it's commands invoked
2485 //================================================================================
2487 void _pySelfEraser::Flush()
2489 if ( GetNbCalls() == 0 )
2490 GetCreationCmd()->Clear();
2493 //================================================================================
2495 * \brief count invoked commands
2497 //================================================================================
2499 void _pySubMesh::Process( const Handle(_pyCommand)& theCommand )
2501 _pyObject::Process(theCommand); // count calls of Process()
2502 GetCreationCmd()->AddDependantCmd( theCommand );
2505 //================================================================================
2507 * \brief Clear creatin command if no commands invoked
2509 //================================================================================
2511 void _pySubMesh::Flush()
2513 if ( GetNbCalls() == 0 ) // move to the end of all commands
2514 theGen->GetLastCommand()->AddDependantCmd( GetCreationCmd() );
2515 else if ( !myCreator.IsNull() )
2516 // move to be just after creator
2517 myCreator->GetCreationCmd()->AddDependantCmd( GetCreationCmd() );