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() ) {
251 // check for mesh editor object
252 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
253 _pyID editorID = aCommand->GetResultValue();
254 Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
255 myMeshEditors.insert( make_pair( editorID, editor ));
258 // check for SubMesh objects
259 else if ( aCommand->GetMethod() == "GetSubMesh" ) { // SubMesh creation
260 _pyID subMeshID = aCommand->GetResultValue();
261 Handle(_pySubMesh) subMesh = new _pySubMesh( aCommand );
262 myObjects.insert( make_pair( subMeshID, subMesh ));
265 id_mesh->second->Process( aCommand );
269 // SMESH_MeshEditor method?
270 map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
271 if ( id_editor != myMeshEditors.end() ) {
272 id_editor->second->Process( aCommand );
273 TCollection_AsciiString processedCommand = aCommand->GetString();
274 // some commands of SMESH_MeshEditor create meshes
275 if ( aCommand->GetMethod().Search("MakeMesh") != -1 ) {
276 Handle(_pyMesh) mesh = new _pyMesh( aCommand, aCommand->GetResultValue() );
277 aCommand->GetString() = processedCommand; // discard changes made by _pyMesh
278 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
282 // SMESH_Hypothesis method?
283 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
284 for ( ; hyp != myHypos.end(); ++hyp )
285 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
286 (*hyp)->Process( aCommand );
290 // other object method?
291 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.find( objID );
292 if ( id_obj != myObjects.end() ) {
293 id_obj->second->Process( aCommand );
296 // if ( theCommand.Search( "aFilterManager" ) != -1 ) {
297 // if ( theCommand.Search( "CreateFilterManager" ) != -1 )
298 // myFilterManager = new _pySelfEraser( aCommand );
299 // else if ( !myFilterManager.IsNull() )
300 // myFilterManager->Process( aCommand );
304 // Add access to a wrapped mesh
305 AddMeshAccessorMethod( aCommand );
307 // Add access to a wrapped algorithm
308 // AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
310 // PAL12227. PythonDump was not updated at proper time; result is
311 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
312 // TypeError: __init__() takes exactly 11 arguments (10 given)
313 char wrongCommand[] = "SMESH.Filter.Criterion(";
314 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
316 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
317 // there must be 10 arguments, 5-th arg ThresholdID is missing,
318 const int wrongNbArgs = 9, missingArg = 5;
319 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
321 for ( int i = wrongNbArgs; i > missingArg; --i )
322 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
323 tmpCmd.SetArg( missingArg, "''");
324 aCommand->GetString().Trunc( beg - 1 );
325 aCommand->GetString() += tmpCmd.GetString();
331 //================================================================================
333 * \brief Convert the command or remember it for later conversion
334 * \param theCommand - The python command calling a method of SMESH_Gen
336 //================================================================================
338 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
340 // there are methods to convert:
341 // CreateMesh( shape )
342 // Concatenate( [mesh1, ...], ... )
343 // CreateHypothesis( theHypType, theLibName )
344 // Compute( mesh, geom )
346 TCollection_AsciiString method = theCommand->GetMethod();
348 if ( method == "CreateMesh" || method == "CreateEmptyMesh")
350 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
351 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
354 if ( method == "CreateMeshesFromUNV" || method == "CreateMeshesFromSTL")
356 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
357 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
360 if( method == "CreateMeshesFromMED")
362 for(int ind = 0;ind<theCommand->GetNbResultValues();ind++)
364 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue(ind));
365 myMeshes.insert( make_pair( theCommand->GetResultValue(ind), mesh ));
369 // CreateHypothesis()
370 if ( method == "CreateHypothesis" )
372 // issue 199929, remove standard library name (default parameter)
373 const TCollection_AsciiString & aLibName = theCommand->GetArg( 2 );
374 if ( aLibName.Search( "StdMeshersEngine" ) != -1 ) {
375 // keep first argument
376 TCollection_AsciiString arg = theCommand->GetArg( 1 );
377 theCommand->RemoveArgs();
378 theCommand->SetArg( 1, arg );
381 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
385 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
386 if ( method == "Compute" )
388 const _pyID& meshID = theCommand->GetArg( 1 );
389 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
390 if ( id_mesh != myMeshes.end() ) {
391 theCommand->SetObject( meshID );
392 theCommand->RemoveArgs();
393 id_mesh->second->Flush();
398 // objects erasing creation command if no more it's commands invoked:
399 // SMESH_Pattern, FilterManager
400 if ( method == "GetPattern" || method == "CreateFilterManager" ) {
401 Handle(_pyObject) obj = new _pySelfEraser( theCommand );
402 if ( !myObjects.insert( make_pair( obj->GetID(), obj )).second )
403 theCommand->Clear(); // already created
406 // Concatenate( [mesh1, ...], ... )
407 if ( method == "Concatenate" || method == "ConcatenateWithGroups")
409 if ( method == "ConcatenateWithGroups" ) {
410 theCommand->SetMethod( "Concatenate" );
411 theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
413 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
414 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
415 AddMeshAccessorMethod( theCommand );
418 // Replace name of SMESH_Gen
420 // names of SMESH_Gen methods fully equal to methods defined in smesh.py
421 static TStringSet smeshpyMethods;
422 if ( smeshpyMethods.empty() ) {
423 const char * names[] =
424 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
425 "GetPattern","GetSubShapesId",
426 "" }; // <- mark of array end
427 smeshpyMethods.Insert( names );
429 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
430 // smeshgen.Method() --> smesh.Method()
431 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
433 // smeshgen.Method() --> smesh.smesh.Method()
434 theCommand->SetObject( SMESH_2smeshpy::GenName() );
437 //================================================================================
439 * \brief Convert the remembered commands
441 //================================================================================
445 // create empty command
446 myLastCommand = new _pyCommand();
448 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
449 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
450 if ( ! id_mesh->second.IsNull() )
451 id_mesh->second->Flush();
453 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
454 for ( ; hyp != myHypos.end(); ++hyp )
455 if ( !hyp->IsNull() ) {
457 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
458 if ( !(*hyp)->IsWrapped() )
459 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
462 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
463 for ( ; id_obj != myObjects.end(); ++id_obj )
464 if ( ! id_obj->second.IsNull() )
465 id_obj->second->Flush();
467 myLastCommand->SetOrderNb( ++myNbCommands );
468 myCommands.push_back( myLastCommand );
471 //================================================================================
473 * \brief Add access method to mesh that is an argument
474 * \param theCmd - command to add access method
475 * \retval bool - true if added
477 //================================================================================
479 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
482 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
483 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
484 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
490 //================================================================================
492 * \brief Add access method to algo that is an object or an argument
493 * \param theCmd - command to add access method
494 * \retval bool - true if added
496 //================================================================================
498 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
501 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
502 for ( ; hyp != myHypos.end(); ++hyp ) {
503 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
504 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
510 //================================================================================
512 * \brief Find hypothesis by ID (entry)
513 * \param theHypID - The hypothesis ID
514 * \retval Handle(_pyHypothesis) - The found hypothesis
516 //================================================================================
518 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
520 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
521 for ( ; hyp != myHypos.end(); ++hyp )
522 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
524 return Handle(_pyHypothesis)();
527 //================================================================================
529 * \brief Find algorithm the created algorithm
530 * \param theGeom - The shape ID the algorithm was created on
531 * \param theMesh - The mesh ID that created the algorithm
532 * \param dim - The algo dimension
533 * \retval Handle(_pyHypothesis) - The found algo
535 //================================================================================
537 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
538 const Handle(_pyHypothesis)& theHypothesis )
540 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
541 for ( ; hyp != myHypos.end(); ++hyp )
542 if ( !hyp->IsNull() &&
544 theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
545 (*hyp)->GetGeom() == theGeom &&
546 (*hyp)->GetMesh() == theMesh )
551 //================================================================================
553 * \brief Find subMesh by ID (entry)
554 * \param theSubMeshID - The subMesh ID
555 * \retval Handle(_pySubMesh) - The found subMesh
557 //================================================================================
559 Handle(_pySubMesh) _pyGen::FindSubMesh( const _pyID& theSubMeshID )
561 map< _pyID, Handle(_pyObject) >::iterator id_subMesh = myObjects.find(theSubMeshID);
562 if ( id_subMesh != myObjects.end() )
563 return Handle(_pySubMesh)::DownCast( id_subMesh->second );
564 return Handle(_pySubMesh)();
568 //================================================================================
570 * \brief Change order of commands in the script
571 * \param theCmd1 - One command
572 * \param theCmd2 - Another command
574 //================================================================================
576 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
578 list< Handle(_pyCommand) >::iterator pos1, pos2;
579 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
580 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
581 myCommands.insert( pos1, theCmd2 );
582 myCommands.insert( pos2, theCmd1 );
583 myCommands.erase( pos1 );
584 myCommands.erase( pos2 );
586 int nb1 = theCmd1->GetOrderNb();
587 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
588 theCmd2->SetOrderNb( nb1 );
589 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
590 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
593 //================================================================================
595 * \brief Set one command after the other
596 * \param theCmd - Command to move
597 * \param theAfterCmd - Command ater which to insert the first one
599 //================================================================================
601 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
603 setNeighbourCommand( theCmd, theAfterCmd, true );
606 //================================================================================
608 * \brief Set one command before the other
609 * \param theCmd - Command to move
610 * \param theBeforeCmd - Command before which to insert the first one
612 //================================================================================
614 void _pyGen::SetCommandBefore( Handle(_pyCommand) theCmd, Handle(_pyCommand) theBeforeCmd )
616 setNeighbourCommand( theCmd, theBeforeCmd, false );
619 //================================================================================
621 * \brief Set one command before or after the other
622 * \param theCmd - Command to move
623 * \param theOtherCmd - Command ater or before which to insert the first one
625 //================================================================================
627 void _pyGen::setNeighbourCommand( Handle(_pyCommand)& theCmd,
628 Handle(_pyCommand)& theOtherCmd,
629 const bool theIsAfter )
631 list< Handle(_pyCommand) >::iterator pos;
632 pos = find( myCommands.begin(), myCommands.end(), theCmd );
633 myCommands.erase( pos );
634 pos = find( myCommands.begin(), myCommands.end(), theOtherCmd );
635 myCommands.insert( (theIsAfter ? ++pos : pos), theCmd );
638 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
639 (*pos)->SetOrderNb( i++ );
642 //================================================================================
644 * \brief Set command be last in list of commands
645 * \param theCmd - Command to be last
647 //================================================================================
649 Handle(_pyCommand)& _pyGen::GetLastCommand()
651 return myLastCommand;
654 //================================================================================
656 * \brief Set method to access to object wrapped with python class
657 * \param theID - The wrapped object entry
658 * \param theMethod - The accessor method
660 //================================================================================
662 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
664 myID2AccessorMethod.Bind( theID, (char*) theMethod );
667 //================================================================================
669 * \brief Generated new ID for object and assign with existing name
670 * \param theID - ID of existing object
672 //================================================================================
674 _pyID _pyGen::GenerateNewID( const _pyID& theID )
679 aNewID = theID + _pyID( ":" ) + _pyID( index++ );
681 while ( myObjectNames.IsBound( aNewID ) );
683 myObjectNames.Bind( aNewID, myObjectNames.IsBound( theID )
684 ? (myObjectNames.Find( theID ) + _pyID( "_" ) + _pyID( index-1 ))
685 : _pyID( "A" ) + aNewID );
689 //================================================================================
691 * \brief Find out type of geom group
692 * \param grpID - The geom group entry
693 * \retval int - The type
695 //================================================================================
697 static bool sameGroupType( const _pyID& grpID,
698 const TCollection_AsciiString& theType)
700 // define group type as smesh.Mesh.Group() does
702 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
703 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
704 if ( !aSObj->_is_nil() ) {
705 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
706 if ( !aGeomObj->_is_nil() ) {
707 switch ( aGeomObj->GetShapeType() ) {
708 case GEOM::VERTEX: type = SMESH::NODE; break;
709 case GEOM::EDGE: type = SMESH::EDGE; break;
710 case GEOM::FACE: type = SMESH::FACE; break;
712 case GEOM::SHELL: type = SMESH::VOLUME; break;
713 case GEOM::COMPOUND: {
714 GEOM::GEOM_Gen_ptr aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
715 if ( !aGeomGen->_is_nil() ) {
716 GEOM::GEOM_IGroupOperations_var aGrpOp =
717 aGeomGen->GetIGroupOperations( study->StudyId() );
718 if ( !aGrpOp->_is_nil() ) {
719 switch ( aGrpOp->GetType( aGeomObj )) {
720 case TopAbs_VERTEX: type = SMESH::NODE; break;
721 case TopAbs_EDGE: type = SMESH::EDGE; break;
722 case TopAbs_FACE: type = SMESH::FACE; break;
723 case TopAbs_SOLID: type = SMESH::VOLUME; break;
734 MESSAGE("Type of the group " << grpID << " not found");
737 if ( theType.IsIntegerValue() )
738 return type == theType.IntegerValue();
741 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
742 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
743 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
744 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
750 //================================================================================
753 * \param theCreationCmd -
755 //================================================================================
757 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd)
758 : _pyObject(theCreationCmd), myHasEditor(false)
760 // convert my creation command
761 Handle(_pyCommand) creationCmd = GetCreationCmd();
762 //TCollection_AsciiString str = creationCmd->GetMethod();
763 // if(str != "CreateMeshesFromUNV" &&
764 // str != "CreateMeshesFromMED" &&
765 // str != "CreateMeshesFromSTL")
766 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
767 creationCmd->SetMethod( "Mesh" );
769 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
772 //================================================================================
775 * \param theCreationCmd -
777 //================================================================================
778 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd, const TCollection_AsciiString& id):
779 _pyObject(theCreationCmd), myHasEditor(false)
781 // convert my creation command
782 Handle(_pyCommand) creationCmd = GetCreationCmd();
783 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
784 theGen->SetAccessorMethod( id, "GetMesh()" );
787 //================================================================================
789 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
790 * \param theCommand - Engine method called for this mesh
792 //================================================================================
794 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
796 // some methods of SMESH_Mesh interface needs special conversion
797 // to methods of Mesh python class
799 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
800 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
801 // 2. AddHypothesis(geom, hyp)
802 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
803 // 3. CreateGroupFromGEOM(type, name, grp)
804 // --> in Mesh.Group(grp, name="")
805 // 4. ExportToMED(f, auto_groups, version)
806 // --> in Mesh.ExportMED( f, auto_groups, version )
809 const TCollection_AsciiString method = theCommand->GetMethod();
810 // ----------------------------------------------------------------------
811 if ( method == "GetSubMesh" ) {
812 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetResultValue() );
813 if ( !subMesh.IsNull() ) {
814 subMesh->SetCreator( this );
815 mySubmeshes.push_back( subMesh );
818 // ----------------------------------------------------------------------
819 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
820 myAddHypCmds.push_back( theCommand );
822 const _pyID& hypID = theCommand->GetArg( 2 );
823 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
824 if ( !hyp.IsNull() ) {
825 myHypos.push_back( hyp );
826 if ( hyp->GetMesh().IsEmpty() )
827 hyp->SetMesh( this->GetID() );
830 // ----------------------------------------------------------------------
831 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
832 _pyID grp = theCommand->GetArg( 3 );
833 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
834 theCommand->SetMethod( "Group" );
835 theCommand->RemoveArgs();
836 theCommand->SetArg( 1, grp );
839 _pyID type = theCommand->GetArg( 1 );
840 _pyID name = theCommand->GetArg( 2 );
841 theCommand->SetMethod( "GroupOnGeom" );
842 theCommand->RemoveArgs();
843 theCommand->SetArg( 1, grp );
844 theCommand->SetArg( 2, name );
845 theCommand->SetArg( 3, type );
848 // ----------------------------------------------------------------------
849 else if ( method == "ExportToMED" || // ExportToMED() --> ExportMED()
850 method == "ExportToMEDX" ) { // ExportToMEDX() --> ExportMED()
851 theCommand->SetMethod( "ExportMED" );
853 // ----------------------------------------------------------------------
854 else if ( method == "CreateGroup" ) { // CreateGroup() --> CreateEmptyGroup()
855 theCommand->SetMethod( "CreateEmptyGroup" );
857 // ----------------------------------------------------------------------
858 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
860 _pyID hypID = theCommand->GetArg( 2 );
862 // check if this mesh still has corresponding addition command
863 bool hasAddCmd = false;
864 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
865 while ( cmd != myAddHypCmds.end() )
867 // AddHypothesis(geom, hyp)
868 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
871 cmd = myAddHypCmds.erase( cmd );
878 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
879 if ( ! hasAddCmd && hypID.Length() != 0 ) { // hypo addition already wrapped
880 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
881 _pyID geom = theCommand->GetArg( 1 );
882 theCommand->RemoveArgs();
883 theCommand->SetArg( 1, hypID );
884 if ( geom != GetGeom() )
885 theCommand->SetArg( 2, geom );
887 // remove hyp from myHypos
888 myHypos.remove( hyp );
890 // check for SubMesh order commands
891 else if ( theCommand->GetMethod() == "GetMeshOrder" ||
892 theCommand->GetMethod() == "SetMeshOrder" ) {
893 // In fact arguments and result values does not support complex containers
894 // such as list of list
895 // So, here we parse it manually
897 //for(int ind = 0, n = theCommand->GetNbResultValues();ind<n;ind++) {
898 // Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetResultValue(ind) );
900 //for(int ind = 0, n = theCommand->GetNbArgs();ind<n;ind++) {
901 // Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetArg(ind) );
902 const bool isArg = theCommand->GetMethod() == "SetMeshOrder";
903 const TCollection_AsciiString& cmdStr = theCommand->GetString();
904 int begPos = (/*isArg ? cmdStr.Search( "(" ) :*/ cmdStr.Search( "[" )) + 1;
905 int endPos = (isArg ? cmdStr.Search( ")" ) : cmdStr.Search( "=" )) - 1;
906 if ( begPos != -1 && begPos < endPos && endPos <= cmdStr.Length() ) {
907 TCollection_AsciiString aSubStr = cmdStr.SubString( begPos, endPos );
908 Standard_Integer index = 1;
909 TCollection_AsciiString anIDStr = aSubStr.Token("\t ,[]", index++);
910 while ( !anIDStr.IsEmpty() ) {
911 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( anIDStr );
912 if ( !subMesh.IsNull() )
913 subMesh->Process( theCommand );
914 anIDStr = aSubStr.Token("\t ,[]", index++);
918 // add accessor method if necessary
921 if ( NeedMeshAccess( theCommand ))
922 // apply theCommand to the mesh wrapped by smeshpy mesh
923 AddMeshAccess( theCommand );
927 //================================================================================
929 * \brief Return True if addition of accesor method is needed
931 //================================================================================
933 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
935 // names of SMESH_Mesh methods fully equal to methods of class Mesh, so
936 // no conversion is needed for them at all:
937 static TStringSet sameMethods;
938 if ( sameMethods.empty() ) {
939 const char * names[] =
940 { "ExportDAT","ExportUNV","ExportSTL", "RemoveGroup","RemoveGroupWithContents",
941 "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
942 "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
943 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
944 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
945 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
946 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
947 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
948 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
949 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
950 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
951 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
952 "Clear", "ConvertToStandalone", "GetMeshOrder", "SetMeshOrder"
953 ,"" }; // <- mark of end
954 sameMethods.Insert( names );
957 return !sameMethods.Contains( theCommand->GetMethod() );
960 //================================================================================
962 * \brief Convert creation and addition of all algos and hypos
964 //================================================================================
966 void _pyMesh::Flush()
968 list < Handle(_pyCommand) >::iterator cmd;
970 // try to convert algo addition like this:
971 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
972 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
974 Handle(_pyCommand) addCmd = *cmd;
976 _pyID algoID = addCmd->GetArg( 2 );
977 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
978 if ( algo.IsNull() || !algo->IsAlgo() )
981 // check and create new algorithm instance if it is already wrapped
982 if ( algo->IsWrapped() ) {
983 _pyID localAlgoID = theGen->GenerateNewID( algoID );
984 TCollection_AsciiString aNewCmdStr = localAlgoID +
985 TCollection_AsciiString( " = " ) + theGen->GetID() +
986 TCollection_AsciiString( ".CreateHypothesis( \"" ) + algo->GetAlgoType() +
987 TCollection_AsciiString( "\" )" );
989 Handle(_pyCommand) newCmd = theGen->AddCommand( aNewCmdStr );
990 Handle(_pyAlgorithm) newAlgo = Handle(_pyAlgorithm)::DownCast(theGen->FindHyp( localAlgoID ));
991 if ( !newAlgo.IsNull() ) {
992 newAlgo->Assign( algo, this->GetID() );
993 newAlgo->SetCreationCmd( newCmd );
995 // set algorithm creation
996 theGen->SetCommandBefore( newCmd, addCmd );
1001 _pyID geom = addCmd->GetArg( 1 );
1002 bool isLocalAlgo = ( geom != GetGeom() );
1005 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
1007 // wrapped algo is created atfer mesh creation
1008 GetCreationCmd()->AddDependantCmd( addCmd );
1010 if ( isLocalAlgo ) {
1011 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
1012 addCmd->SetArg( addCmd->GetNbArgs() + 1,
1013 TCollection_AsciiString( "geom=" ) + geom );
1014 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
1015 list < Handle(_pySubMesh) >::iterator smIt;
1016 for ( smIt = mySubmeshes.begin(); smIt != mySubmeshes.end(); ++smIt ) {
1017 Handle(_pySubMesh) subMesh = *smIt;
1018 Handle(_pyCommand) subCmd = subMesh->GetCreationCmd();
1019 if ( geom == subCmd->GetArg( 1 )) {
1020 subCmd->SetObject( algo->GetID() );
1021 subCmd->RemoveArgs();
1022 subMesh->SetCreator( algo );
1027 else // KO - ALGO was already created
1029 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
1030 addCmd->RemoveArgs();
1031 addCmd->SetArg( 1, algoID );
1033 addCmd->SetArg( 2, geom );
1037 // try to convert hypo addition like this:
1038 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
1039 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1041 Handle(_pyCommand) addCmd = *cmd;
1042 _pyID hypID = addCmd->GetArg( 2 );
1043 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1044 if ( hyp.IsNull() || hyp->IsAlgo() )
1046 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
1048 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
1049 _pyID geom = addCmd->GetArg( 1 );
1050 addCmd->RemoveArgs();
1051 addCmd->SetArg( 1, hypID );
1052 if ( geom != GetGeom() )
1053 addCmd->SetArg( 2, geom );
1057 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
1058 // for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
1059 // Handle(_pyCommand) subCmd = *cmd;
1060 // if ( subCmd->GetNbArgs() > 0 )
1061 // AddMeshAccess( subCmd );
1063 myAddHypCmds.clear();
1064 mySubmeshes.clear();
1067 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1068 for ( ; hyp != myHypos.end(); ++hyp )
1072 //================================================================================
1074 * \brief MeshEditor convert its commands to ones of mesh
1076 //================================================================================
1078 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
1079 _pyObject( theCreationCmd )
1081 myMesh = theCreationCmd->GetObject();
1082 myCreationCmdStr = theCreationCmd->GetString();
1083 theCreationCmd->Clear();
1086 //================================================================================
1088 * \brief convert its commands to ones of mesh
1090 //================================================================================
1092 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
1094 // names of SMESH_MeshEditor methods fully equal to methods of python class Mesh, so
1095 // commands calling this methods are converted to calls of methods of Mesh
1096 static TStringSet sameMethods;
1097 if ( sameMethods.empty() ) {
1098 const char * names[] = {
1099 "RemoveElements","RemoveNodes","RemoveOrphanNodes","AddNode","Add0DElement","AddEdge","AddFace","AddPolygonalFace",
1100 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode", "MoveClosestNodeToPoint",
1101 "InverseDiag","DeleteDiag","Reorient","ReorientObject","TriToQuad","SplitQuad","SplitQuadObject",
1102 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
1103 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
1104 "RotationSweep","RotationSweepObject","RotationSweepObject1D","RotationSweepObject2D",
1105 "ExtrusionSweep","AdvancedExtrusion","ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D",
1106 "ExtrusionAlongPath","ExtrusionAlongPathObject","ExtrusionAlongPathObject1D","ExtrusionAlongPathObject2D",
1107 "Mirror","MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
1108 "FindCoincidentNodes",/*"FindCoincidentNodesOnPart",*/"MergeNodes","FindEqualElements",
1109 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
1110 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
1111 "GetLastCreatedElems",
1112 "MirrorMakeMesh","MirrorObjectMakeMesh","TranslateMakeMesh",
1113 "TranslateObjectMakeMesh","RotateMakeMesh","RotateObjectMakeMesh"
1114 ,"" }; // <- mark of the end
1115 sameMethods.Insert( names );
1118 // names of SMESH_MeshEditor methods which differ from methods of class Mesh
1119 // only by last two arguments
1120 static TStringSet diffLastTwoArgsMethods;
1121 if (diffLastTwoArgsMethods.empty() ) {
1122 const char * names[] = {
1123 "MirrorMakeGroups","MirrorObjectMakeGroups",
1124 "TranslateMakeGroups","TranslateObjectMakeGroups",
1125 "RotateMakeGroups","RotateObjectMakeGroups",
1126 ""};// <- mark of the end
1127 diffLastTwoArgsMethods.Insert( names );
1130 const TCollection_AsciiString & method = theCommand->GetMethod();
1131 bool isPyMeshMethod = sameMethods.Contains( method );
1132 if ( !isPyMeshMethod )
1134 //Replace SMESH_MeshEditor "MakeGroups" functions on the Mesh
1135 //functions with the flag "theMakeGroups = True" like:
1136 //SMESH_MeshEditor.CmdMakeGroups => Mesh.Cmd(...,True)
1137 int pos = method.Search("MakeGroups");
1140 isPyMeshMethod = true;
1142 // 1. Remove "MakeGroups" from the Command
1143 TCollection_AsciiString aMethod = theCommand->GetMethod();
1144 int nbArgsToAdd = diffLastTwoArgsMethods.Contains(aMethod) ? 2 : 1;
1145 aMethod.Trunc(pos-1);
1146 theCommand->SetMethod(aMethod);
1148 // 2. And add last "True" argument(s)
1149 while(nbArgsToAdd--)
1150 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
1154 // set "FindCoincidentNodesOnPart()" instead of "FindCoincidentNodesOnPartBut()"
1155 if ( !isPyMeshMethod && method == "FindCoincidentNodesOnPartBut")
1157 isPyMeshMethod=true;
1158 theCommand->SetMethod("FindCoincidentNodesOnPart");
1160 // DoubleNodeElemGroupNew() -> DoubleNodeElemGroup()
1161 if ( !isPyMeshMethod && ( method == "DoubleNodeElemGroupNew" || method == "DoubleNodeGroupNew"))
1163 isPyMeshMethod=true;
1164 theCommand->SetMethod( method.SubString( 1, method.Length()-3));
1167 // meshes made by *MakeMesh() methods are not wrapped by _pyMesh,
1168 // so let _pyMesh care of it (TMP?)
1169 // if ( theCommand->GetMethod().Search("MakeMesh") != -1 )
1170 // _pyMesh( new _pyCommand( theCommand->GetString(), 0 )); // for theGen->SetAccessorMethod()
1171 if ( isPyMeshMethod )
1173 theCommand->SetObject( myMesh );
1177 // editor creation command is needed only if any editor function is called
1178 theGen->AddMeshAccessorMethod( theCommand ); // for *Object()
1179 if ( !myCreationCmdStr.IsEmpty() ) {
1180 GetCreationCmd()->GetString() = myCreationCmdStr;
1181 myCreationCmdStr.Clear();
1186 //================================================================================
1188 * \brief _pyHypothesis constructor
1189 * \param theCreationCmd -
1191 //================================================================================
1193 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
1194 _pyObject( theCreationCmd )
1196 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
1199 //================================================================================
1201 * \brief Creates algorithm or hypothesis
1202 * \param theCreationCmd - The engine command creating a hypothesis
1203 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
1205 //================================================================================
1207 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
1209 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
1210 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
1212 Handle(_pyHypothesis) hyp, algo;
1215 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
1216 if ( hypTypeQuoted.IsEmpty() )
1219 TCollection_AsciiString hypType =
1220 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1222 algo = new _pyAlgorithm( theCreationCmd );
1223 hyp = new _pyHypothesis( theCreationCmd );
1225 // 1D Regular_1D ----------
1226 if ( hypType == "Regular_1D" ) {
1227 // set mesh's method creating algo,
1228 // i.e. convertion result will be "regular1d = Mesh.Segment()",
1229 // and set hypType by which algo creating a hypothesis is searched for
1230 algo->SetConvMethodAndType("Segment", hypType.ToCString());
1232 else if ( hypType == "CompositeSegment_1D" ) {
1233 algo->SetConvMethodAndType("Segment", "Regular_1D");
1234 algo->myArgs.Append( "algo=smesh.COMPOSITE");
1236 else if ( hypType == "LocalLength" ) {
1237 // set algo's method creating hyp, and algo type
1238 hyp->SetConvMethodAndType( "LocalLength", "Regular_1D");
1239 // set method whose 1 arg will become the 1-st arg of hyp creation command
1240 // i.e. convertion result will be "locallength = regular1d.LocalLength(<arg of SetLength()>)"
1241 hyp->AddArgMethod( "SetLength" );
1243 else if ( hypType == "MaxLength" ) {
1244 // set algo's method creating hyp, and algo type
1245 hyp->SetConvMethodAndType( "MaxSize", "Regular_1D");
1246 // set method whose 1 arg will become the 1-st arg of hyp creation command
1247 // i.e. convertion result will be "maxsize = regular1d.MaxSize(<arg of SetLength()>)"
1248 hyp->AddArgMethod( "SetLength" );
1250 else if ( hypType == "NumberOfSegments" ) {
1251 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
1252 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
1253 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
1254 hyp->AddArgMethod( "SetNumberOfSegments" );
1255 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
1256 hyp->AddArgMethod( "SetScaleFactor" );
1257 hyp->AddArgMethod( "SetReversedEdges" );
1259 else if ( hypType == "Arithmetic1D" ) {
1260 hyp = new _pyComplexParamHypo( theCreationCmd );
1261 hyp->SetConvMethodAndType( "Arithmetic1D", "Regular_1D");
1262 hyp->AddArgMethod( "SetStartLength" );
1263 hyp->AddArgMethod( "SetEndLength" );
1264 hyp->AddArgMethod( "SetReversedEdges" );
1266 else if ( hypType == "StartEndLength" ) {
1267 hyp = new _pyComplexParamHypo( theCreationCmd );
1268 hyp->SetConvMethodAndType( "StartEndLength", "Regular_1D");
1269 hyp->AddArgMethod( "SetStartLength" );
1270 hyp->AddArgMethod( "SetEndLength" );
1271 hyp->AddArgMethod( "SetReversedEdges" );
1273 else if ( hypType == "Deflection1D" ) {
1274 hyp->SetConvMethodAndType( "Deflection1D", "Regular_1D");
1275 hyp->AddArgMethod( "SetDeflection" );
1277 else if ( hypType == "Propagation" ) {
1278 hyp->SetConvMethodAndType( "Propagation", "Regular_1D");
1280 else if ( hypType == "QuadraticMesh" ) {
1281 hyp->SetConvMethodAndType( "QuadraticMesh", "Regular_1D");
1283 else if ( hypType == "AutomaticLength" ) {
1284 hyp->SetConvMethodAndType( "AutomaticLength", "Regular_1D");
1285 hyp->AddArgMethod( "SetFineness");
1287 else if ( hypType == "SegmentLengthAroundVertex" ) {
1288 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
1289 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
1290 hyp->AddArgMethod( "SetLength" );
1292 // 1D Python_1D ----------
1293 else if ( hypType == "Python_1D" ) {
1294 algo->SetConvMethodAndType( "Segment", hypType.ToCString());
1295 algo->myArgs.Append( "algo=smesh.PYTHON");
1297 else if ( hypType == "PythonSplit1D" ) {
1298 hyp->SetConvMethodAndType( "PythonSplit1D", "Python_1D");
1299 hyp->AddArgMethod( "SetNumberOfSegments");
1300 hyp->AddArgMethod( "SetPythonLog10RatioFunction");
1302 // MEFISTO_2D ----------
1303 else if ( hypType == "MEFISTO_2D" ) { // MEFISTO_2D
1304 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
1306 else if ( hypType == "MaxElementArea" ) {
1307 hyp->SetConvMethodAndType( "MaxElementArea", "MEFISTO_2D");
1308 hyp->SetConvMethodAndType( "MaxElementArea", "NETGEN_2D_ONLY");
1309 hyp->AddArgMethod( "SetMaxElementArea");
1311 else if ( hypType == "LengthFromEdges" ) {
1312 hyp->SetConvMethodAndType( "LengthFromEdges", "MEFISTO_2D");
1313 hyp->SetConvMethodAndType( "LengthFromEdges", "NETGEN_2D_ONLY");
1315 // Quadrangle_2D ----------
1316 else if ( hypType == "Quadrangle_2D" ) {
1317 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
1319 else if ( hypType == "QuadranglePreference" ) {
1320 hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
1321 hyp->SetConvMethodAndType( "SetQuadAllowed", "NETGEN_2D_ONLY");
1323 else if ( hypType == "TrianglePreference" ) {
1324 hyp->SetConvMethodAndType( "TrianglePreference", "Quadrangle_2D");
1326 // RadialQuadrangle_1D2D ----------
1327 else if ( hypType == "RadialQuadrangle_1D2D" ) {
1328 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
1329 algo->myArgs.Append( "algo=smesh.RADIAL_QUAD" );
1331 else if ( hypType == "NumberOfLayers2D" ) {
1332 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialQuadrangle_1D2D");
1333 hyp->AddArgMethod( "SetNumberOfLayers" );
1335 else if ( hypType == "LayerDistribution2D" ) {
1336 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get2DHypothesis" );
1337 hyp->SetConvMethodAndType( "LayerDistribution", "RadialQuadrangle_1D2D");
1339 // BLSURF ----------
1340 else if ( hypType == "BLSURF" ) {
1341 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
1342 algo->myArgs.Append( "algo=smesh.BLSURF" );
1344 else if ( hypType == "BLSURF_Parameters") {
1345 hyp->SetConvMethodAndType( "Parameters", "BLSURF");
1347 // NETGEN ----------
1348 else if ( hypType == "NETGEN_2D") { // 1D-2D
1349 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1350 algo->myArgs.Append( "algo=smesh.NETGEN" );
1352 else if ( hypType == "NETGEN_Parameters_2D") {
1353 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D");
1355 else if ( hypType == "NETGEN_SimpleParameters_2D") {
1356 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D");
1357 hyp->myArgs.Append( "which=smesh.SIMPLE" );
1359 else if ( hypType == "NETGEN_2D3D") { // 1D-2D-3D
1360 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1361 algo->myArgs.Append( "algo=smesh.FULL_NETGEN" );
1363 else if ( hypType == "NETGEN_Parameters") {
1364 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D3D");
1366 else if ( hypType == "NETGEN_SimpleParameters_3D") {
1367 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D3D");
1368 hyp->myArgs.Append( "which=smesh.SIMPLE" );
1370 else if ( hypType == "NETGEN_2D_ONLY") { // 2D
1371 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1372 algo->myArgs.Append( "algo=smesh.NETGEN_2D" );
1374 else if ( hypType == "NETGEN_3D") { // 3D
1375 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1376 algo->myArgs.Append( "algo=smesh.NETGEN" );
1378 else if ( hypType == "MaxElementVolume") {
1379 hyp->SetConvMethodAndType( "MaxElementVolume", "NETGEN_3D");
1380 hyp->AddArgMethod( "SetMaxElementVolume" );
1382 // GHS3D_3D ----------
1383 else if ( hypType == "GHS3D_3D" ) {
1384 algo->SetConvMethodAndType( "Tetrahedron", hypType.ToCString());
1385 algo->myArgs.Append( "algo=smesh.GHS3D" );
1387 else if ( hypType == "GHS3D_Parameters") {
1388 hyp->SetConvMethodAndType( "Parameters", "GHS3D_3D");
1390 // Hexa_3D ---------
1391 else if ( hypType == "BLSURF" ) {
1392 algo->SetConvMethodAndType( "Hexahedron", hypType.ToCString());
1394 // Repetitive Projection_1D ---------
1395 else if ( hypType == "Projection_1D" ) {
1396 algo->SetConvMethodAndType( "Projection1D", hypType.ToCString());
1398 else if ( hypType == "ProjectionSource1D" ) {
1399 hyp->SetConvMethodAndType( "SourceEdge", "Projection_1D");
1400 hyp->AddArgMethod( "SetSourceEdge");
1401 hyp->AddArgMethod( "SetSourceMesh");
1402 // 2 args of SetVertexAssociation() will become the 3-th and 4-th args of hyp creation command
1403 hyp->AddArgMethod( "SetVertexAssociation", 2 );
1405 // Projection_2D ---------
1406 else if ( hypType == "Projection_2D" ) {
1407 algo->SetConvMethodAndType( "Projection2D", hypType.ToCString());
1409 else if ( hypType == "ProjectionSource2D" ) {
1410 hyp->SetConvMethodAndType( "SourceFace", "Projection_2D");
1411 hyp->AddArgMethod( "SetSourceFace");
1412 hyp->AddArgMethod( "SetSourceMesh");
1413 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1415 // Projection_3D ---------
1416 else if ( hypType == "Projection_3D" ) {
1417 algo->SetConvMethodAndType( "Projection3D", hypType.ToCString());
1419 else if ( hypType == "ProjectionSource3D" ) {
1420 hyp->SetConvMethodAndType( "SourceShape3D", "Projection_3D");
1421 hyp->AddArgMethod( "SetSource3DShape");
1422 hyp->AddArgMethod( "SetSourceMesh");
1423 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1425 // Prism_3D ---------
1426 else if ( hypType == "Prism_3D" ) {
1427 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1429 // RadialPrism_3D ---------
1430 else if ( hypType == "RadialPrism_3D" ) {
1431 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1433 else if ( hypType == "NumberOfLayers" ) {
1434 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialPrism_3D");
1435 hyp->AddArgMethod( "SetNumberOfLayers" );
1437 else if ( hypType == "LayerDistribution" ) {
1438 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get3DHypothesis" );
1439 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
1442 return algo->IsValid() ? algo : hyp;
1445 //================================================================================
1447 * \brief Convert the command adding a hypothesis to mesh into a smesh command
1448 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
1449 * \param theAlgo - The algo that can create this hypo
1450 * \retval bool - false if the command cant be converted
1452 //================================================================================
1454 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
1455 const _pyID& theMesh)
1457 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
1459 if ( !IsWrappable( theMesh ))
1462 myGeom = theCmd->GetArg( 1 );
1464 Handle(_pyHypothesis) algo;
1466 // find algo created on myGeom in theMesh
1467 algo = theGen->FindAlgo( myGeom, theMesh, this );
1468 if ( algo.IsNull() )
1470 // attach hypothesis creation command to be after algo creation command
1471 // because it can be new created instance of algorithm
1472 algo->GetCreationCmd()->AddDependantCmd( theCmd );
1476 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
1477 theCmd->SetResultValue( GetID() );
1478 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
1479 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
1481 theCmd->RemoveArgs();
1482 for ( int i = 1; i <= myArgs.Length(); ++i ) {
1483 if ( !myArgs( i ).IsEmpty() )
1484 theCmd->SetArg( i, myArgs( i ));
1486 theCmd->SetArg( i, "[]");
1488 // set a new creation command
1489 GetCreationCmd()->Clear();
1490 // replace creation command by wrapped instance
1491 // please note, that hypothesis attaches to algo creation command (see upper)
1492 SetCreationCmd( theCmd );
1495 // clear commands setting arg values
1496 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
1497 for ( ; argCmd != myArgCommands.end(); ++argCmd )
1500 // set unknown arg commands after hypo creation
1501 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
1502 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
1503 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1504 afterCmd->AddDependantCmd( *cmd );
1510 //================================================================================
1512 * \brief Remember hypothesis parameter values
1513 * \param theCommand - The called hypothesis method
1515 //================================================================================
1517 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
1519 ASSERT( !myIsAlgo );
1522 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
1523 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
1524 while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
1525 myArgs.Append( "[]" );
1526 for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
1527 myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
1528 myArgCommands.push_back( theCommand );
1531 nbArgs += myNbArgsByMethod( i );
1533 myUnknownCommands.push_back( theCommand );
1536 //================================================================================
1538 * \brief Finish conversion
1540 //================================================================================
1542 void _pyHypothesis::Flush()
1544 if ( IsWrapped() ) {
1547 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1548 for ( ; cmd != myArgCommands.end(); ++cmd ) {
1549 // Add access to a wrapped mesh
1550 theGen->AddMeshAccessorMethod( *cmd );
1551 // Add access to a wrapped algorithm
1552 theGen->AddAlgoAccessorMethod( *cmd );
1554 cmd = myUnknownCommands.begin();
1555 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1556 // Add access to a wrapped mesh
1557 theGen->AddMeshAccessorMethod( *cmd );
1558 // Add access to a wrapped algorithm
1559 theGen->AddAlgoAccessorMethod( *cmd );
1562 // forget previous hypothesis modifications
1563 myArgCommands.clear();
1564 myUnknownCommands.clear();
1567 //================================================================================
1569 * \brief clear creation, arg and unkown commands
1571 //================================================================================
1573 void _pyHypothesis::ClearAllCommands()
1575 GetCreationCmd()->Clear();
1576 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1577 for ( ; cmd != myArgCommands.end(); ++cmd )
1579 cmd = myUnknownCommands.begin();
1580 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1585 //================================================================================
1587 * \brief Assign fields of theOther to me except myIsWrapped
1589 //================================================================================
1591 void _pyHypothesis::Assign( const Handle(_pyHypothesis)& theOther,
1592 const _pyID& theMesh )
1594 myIsWrapped = false;
1597 // myCreationCmd = theOther->myCreationCmd;
1598 myIsAlgo = theOther->myIsAlgo;
1599 myGeom = theOther->myGeom;
1600 myType2CreationMethod = theOther->myType2CreationMethod;
1601 myArgs = theOther->myArgs;
1602 myArgMethods = theOther->myArgMethods;
1603 myNbArgsByMethod = theOther->myNbArgsByMethod;
1604 myArgCommands = theOther->myArgCommands;
1605 myUnknownCommands = theOther->myUnknownCommands;
1608 //================================================================================
1610 * \brief Remember hypothesis parameter values
1611 * \param theCommand - The called hypothesis method
1613 //================================================================================
1615 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1617 if( theCommand->GetMethod() == "SetLength" )
1619 // NOW it becomes OBSOLETE
1620 // ex: hyp.SetLength(start, 1)
1621 // hyp.SetLength(end, 0)
1622 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1623 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1624 while ( myArgs.Length() < i )
1625 myArgs.Append( "[]" );
1626 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1627 myArgCommands.push_back( theCommand );
1631 _pyHypothesis::Process( theCommand );
1634 //================================================================================
1636 * \brief Clear SetObjectEntry() as it is called by methods of Mesh_Segment
1638 //================================================================================
1640 void _pyComplexParamHypo::Flush()
1644 list < Handle(_pyCommand) >::iterator cmd = myUnknownCommands.begin();
1645 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1646 if ((*cmd)->GetMethod() == "SetObjectEntry" )
1651 //================================================================================
1653 * \brief Convert methods of 1D hypotheses to my own methods
1654 * \param theCommand - The called hypothesis method
1656 //================================================================================
1658 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1660 if ( theCommand->GetMethod() != "SetLayerDistribution" )
1663 _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1665 const _pyID& hyp1dID = theCommand->GetArg( 1 );
1666 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1667 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1669 else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1670 // 1D hypo is already set, so distribution changes and the old
1671 // 1D hypo is thrown away
1672 my1dHyp->ClearAllCommands();
1676 if ( !myArgCommands.empty() )
1677 myArgCommands.front()->Clear();
1678 myArgCommands.push_back( theCommand );
1681 //================================================================================
1684 * \param theAdditionCmd - command to be converted
1685 * \param theMesh - mesh instance
1686 * \retval bool - status
1688 //================================================================================
1690 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1691 const _pyID& theMesh)
1693 myIsWrapped = false;
1695 if ( my1dHyp.IsNull() )
1698 // set "SetLayerDistribution()" after addition cmd
1699 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1701 _pyID geom = theAdditionCmd->GetArg( 1 );
1703 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
1704 if ( !algo.IsNull() )
1706 my1dHyp->SetMesh( theMesh );
1707 my1dHyp->SetConvMethodAndType(my1dHyp->GetAlgoCreationMethod().ToCString(),
1708 algo->GetAlgoType().ToCString());
1709 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1712 // clear "SetLayerDistribution()" cmd
1713 myArgCommands.back()->Clear();
1715 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1717 // find RadialPrism algo created on <geom> for theMesh
1718 GetCreationCmd()->SetObject( algo->GetID() );
1719 GetCreationCmd()->SetMethod( myAlgoMethod );
1720 GetCreationCmd()->RemoveArgs();
1721 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1727 //================================================================================
1731 //================================================================================
1733 void _pyLayerDistributionHypo::Flush()
1735 // as creation of 1D hyp was written later then it's edition,
1736 // we need to find all it's edition calls and process them
1737 if ( !my1dHyp.IsNull() )
1739 _pyID hyp1dID = my1dHyp->GetCreationCmd()->GetResultValue();
1741 // make a new name for 1D hyp = "HypType" + "_Distribution"
1743 if ( my1dHyp->IsWrapped() ) {
1744 newName = my1dHyp->GetCreationCmd()->GetMethod();
1747 TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1748 newName = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1750 newName += "_Distribution";
1751 my1dHyp->GetCreationCmd()->SetResultValue( newName );
1753 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1754 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1755 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1756 const _pyID& objID = (*cmdIt)->GetObject();
1757 if ( objID == hyp1dID ) {
1758 my1dHyp->Process( *cmdIt );
1759 my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1760 ( *cmdIt )->SetObject( newName );
1763 // Set new hyp name to SetLayerDistribution() cmd
1764 if ( !myArgCommands.empty() && !myArgCommands.back()->IsEmpty() )
1765 myArgCommands.back()->SetArg( 1, newName );
1769 //================================================================================
1771 * \brief additionally to Addition2Creation, clears SetDistrType() command
1772 * \param theCmd - AddHypothesis() command
1773 * \param theMesh - mesh to which a hypothesis is added
1774 * \retval bool - convertion result
1776 //================================================================================
1778 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1779 const _pyID& theMesh)
1781 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1782 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1783 bool scaleDistrType = false;
1784 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1785 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1786 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1787 if ( (*cmd)->GetArg( 1 ) == "1" ) {
1788 scaleDistrType = true;
1791 else if ( !scaleDistrType ) {
1792 // distribution type changed: remove scale factor from args
1793 myArgs.Remove( 2, myArgs.Length() );
1799 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1802 //================================================================================
1804 * \brief remove repeated commands defining distribution
1806 //================================================================================
1808 void _pyNumberOfSegmentsHyp::Flush()
1810 // find number of the last SetDistrType() command
1811 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1812 int distrTypeNb = 0;
1813 for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1814 if ( (*cmd)->GetMethod() == "SetDistrType" )
1815 distrTypeNb = (*cmd)->GetOrderNb();
1816 else if (IsWrapped() && (*cmd)->GetMethod() == "SetObjectEntry" )
1819 // clear commands before the last SetDistrType()
1820 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1821 for ( int i = 0; i < 2; ++i ) {
1822 set<TCollection_AsciiString> uniqueMethods;
1823 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1824 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1826 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1827 const TCollection_AsciiString& method = (*cmd)->GetMethod();
1828 if ( !clear || method == "SetNumberOfSegments" ) {
1829 bool isNewInSet = uniqueMethods.insert( method ).second;
1830 clear = !isNewInSet;
1839 //================================================================================
1841 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
1842 * into regular1D.LengthNearVertex( length, vertex )
1843 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
1844 * \param theMesh - The mesh needing this hypo
1845 * \retval bool - false if the command cant be converted
1847 //================================================================================
1849 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1850 const _pyID& theMeshID)
1852 if ( IsWrappable( theMeshID )) {
1854 _pyID vertex = theCmd->GetArg( 1 );
1856 // the problem here is that segment algo will not be found
1857 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
1858 // geometry where segment algorithm is assigned
1859 Handle(_pyHypothesis) algo;
1860 _pyID geom = vertex;
1861 while ( algo.IsNull() && !geom.IsEmpty()) {
1862 // try to find geom as a father of <vertex>
1863 geom = FatherID( geom );
1864 algo = theGen->FindAlgo( geom, theMeshID, this );
1866 if ( algo.IsNull() )
1867 return false; // also possible to find geom as brother of veretex...
1868 // set geom instead of vertex
1869 theCmd->SetArg( 1, geom );
1871 // set vertex as a second arg
1872 if ( myArgs.Length() < 1) myArgs.Append( "1" ); // :(
1873 myArgs.Append( vertex );
1875 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
1876 // theMeshID.LengthNearVertex( length, vertex )
1877 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
1882 //================================================================================
1884 * \brief _pyAlgorithm constructor
1885 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1887 //================================================================================
1889 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1890 : _pyHypothesis( theCreationCmd )
1895 //================================================================================
1897 * \brief Convert the command adding an algorithm to mesh
1898 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1899 * \param theMesh - The mesh needing this algo
1900 * \retval bool - false if the command cant be converted
1902 //================================================================================
1904 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1905 const _pyID& theMeshID)
1907 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1908 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1909 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1915 //================================================================================
1917 * \brief Return starting position of a part of python command
1918 * \param thePartIndex - The index of command part
1919 * \retval int - Part position
1921 //================================================================================
1923 int _pyCommand::GetBegPos( int thePartIndex )
1927 if ( myBegPos.Length() < thePartIndex )
1929 return myBegPos( thePartIndex );
1932 //================================================================================
1934 * \brief Store starting position of a part of python command
1935 * \param thePartIndex - The index of command part
1936 * \param thePosition - Part position
1938 //================================================================================
1940 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1942 while ( myBegPos.Length() < thePartIndex )
1943 myBegPos.Append( UNKNOWN );
1944 myBegPos( thePartIndex ) = thePosition;
1947 //================================================================================
1949 * \brief Returns whitespace symbols at the line beginning
1950 * \retval TCollection_AsciiString - result
1952 //================================================================================
1954 TCollection_AsciiString _pyCommand::GetIndentation()
1957 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1958 GetWord( myString, end, true );
1960 end = GetBegPos( RESULT_IND );
1961 return myString.SubString( 1, end - 1 );
1964 //================================================================================
1966 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1967 * \retval const TCollection_AsciiString & - ResultValue substring
1969 //================================================================================
1971 const TCollection_AsciiString & _pyCommand::GetResultValue()
1973 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1975 int begPos = myString.Location( "=", 1, Length() );
1977 myRes = GetWord( myString, begPos, false );
1980 SetBegPos( RESULT_IND, begPos );
1985 //================================================================================
1987 * \brief Return number of python command result value ResultValue = Obj.Meth()
1990 //================================================================================
1992 const int _pyCommand::GetNbResultValues()
1996 int endPos = myString.Location( "=", 1, Length() );
1997 TCollection_AsciiString str = "";
1998 while ( begPos < endPos) {
1999 str = GetWord( myString, begPos, true );
2000 begPos = begPos+ str.Length();
2007 //================================================================================
2009 * \brief Return substring of python command looking like
2010 * ResultValue1 , ResultValue1,... = Obj.Meth() with res index
2011 * \retval const TCollection_AsciiString & - ResultValue with res index substring
2013 //================================================================================
2014 const TCollection_AsciiString & _pyCommand::GetResultValue(int res)
2018 int endPos = myString.Location( "=", 1, Length() );
2019 while ( begPos < endPos) {
2020 myRes = GetWord( myString, begPos, true );
2021 begPos = begPos + myRes.Length();
2024 myRes.RemoveAll('[');myRes.RemoveAll(']');
2030 return theEmptyString;
2033 //================================================================================
2035 * \brief Return substring of python command looking like ResVal = Object.Meth()
2036 * \retval const TCollection_AsciiString & - Object substring
2038 //================================================================================
2040 const TCollection_AsciiString & _pyCommand::GetObject()
2042 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
2045 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
2047 begPos = myString.Location( "=", 1, Length() ) + 1;
2048 // is '=' in the string argument (for example, name) or not
2049 int nb1 = 0; // number of ' character at the left of =
2050 int nb2 = 0; // number of " character at the left of =
2051 for ( int i = 1; i < begPos-1; i++ ) {
2052 if ( myString.Value( i )=='\'' )
2054 else if ( myString.Value( i )=='"' )
2057 // if number of ' or " is not divisible by 2,
2058 // then get an object at the start of the command
2059 if ( nb1 % 2 != 0 || nb2 % 2 != 0 )
2062 myObj = GetWord( myString, begPos, true );
2063 // check if object is complex,
2064 // so far consider case like "smesh.smesh.Method()"
2065 if ( int bracketPos = myString.Location( "(", begPos, Length() )) {
2066 //if ( bracketPos==0 ) bracketPos = Length();
2067 int dotPos = begPos+myObj.Length();
2068 while ( dotPos+1 < bracketPos ) {
2069 if ( int pos = myString.Location( ".", dotPos+1, bracketPos ))
2074 if ( dotPos > begPos+myObj.Length() )
2075 myObj = myString.SubString( begPos, dotPos-1 );
2078 SetBegPos( OBJECT_IND, begPos );
2084 //================================================================================
2086 * \brief Return substring of python command looking like ResVal = Obj.Method()
2087 * \retval const TCollection_AsciiString & - Method substring
2089 //================================================================================
2091 const TCollection_AsciiString & _pyCommand::GetMethod()
2093 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
2096 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
2097 bool forward = true;
2099 begPos = myString.Location( "(", 1, Length() ) - 1;
2103 myMeth = GetWord( myString, begPos, forward );
2104 SetBegPos( METHOD_IND, begPos );
2110 //================================================================================
2112 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
2113 * \retval const TCollection_AsciiString & - Arg<index> substring
2115 //================================================================================
2117 const TCollection_AsciiString & _pyCommand::GetArg( int index )
2119 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
2122 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
2124 begPos = myString.Location( "(", 1, Length() ) + 1;
2126 int i = 0, prevLen = 0, nbNestings = 0;
2127 while ( begPos != EMPTY ) {
2129 if( myString.Value( begPos ) == '(' )
2131 // check if we are looking at the closing parenthesis
2132 while ( begPos <= Length() && isspace( myString.Value( begPos )))
2134 if ( begPos > Length() )
2136 if ( myString.Value( begPos ) == ')' ) {
2138 if( nbNestings == 0 )
2141 myArgs.Append( GetWord( myString, begPos, true, true ));
2142 SetBegPos( ARG1_IND + i, begPos );
2143 prevLen = myArgs.Last().Length();
2145 myArgs.Remove( myArgs.Length() ); // no more args
2149 if ( myArgs.Length() < index )
2150 return theEmptyString;
2151 return myArgs( index );
2154 //================================================================================
2156 * \brief Check if char is a word part
2157 * \param c - The character to check
2158 * \retval bool - The check result
2160 //================================================================================
2162 static inline bool isWord(const char c, const bool dotIsWord)
2165 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
2168 //================================================================================
2170 * \brief Looks for a word in the string and returns word's beginning
2171 * \param theString - The input string
2172 * \param theStartPos - The position to start the search, returning word's beginning
2173 * \param theForward - The search direction
2174 * \retval TCollection_AsciiString - The found word
2176 //================================================================================
2178 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
2180 const bool theForward,
2181 const bool dotIsWord )
2183 int beg = theStartPos, end = theStartPos;
2184 theStartPos = EMPTY;
2185 if ( beg < 1 || beg > theString.Length() )
2186 return theEmptyString;
2188 if ( theForward ) { // search forward
2190 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
2192 if ( beg > theString.Length() )
2193 return theEmptyString; // no word found
2196 char begChar = theString.Value( beg );
2197 if ( begChar == '"' || begChar == '\'' || begChar == '[') {
2198 char endChar = ( begChar == '[' ) ? ']' : begChar;
2199 // end is at the corresponding quoting mark or bracket
2200 while ( end < theString.Length() &&
2201 ( theString.Value( end ) != endChar || theString.Value( end-1 ) == '\\'))
2205 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
2210 else { // search backward
2212 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
2215 return theEmptyString; // no word found
2217 char endChar = theString.Value( end );
2218 if ( endChar == '"' || endChar == '\'' ) {
2219 // beg is at the corresponding quoting mark
2221 ( theString.Value( beg ) != endChar || theString.Value( beg-1 ) == '\\'))
2225 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
2231 //cout << theString << " ---- " << beg << " - " << end << endl;
2232 return theString.SubString( beg, end );
2235 //================================================================================
2237 * \brief Look for position where not space char is
2238 * \param theString - The string
2239 * \param thePos - The position to search from and which returns result
2240 * \retval bool - false if there are only space after thePos in theString
2244 //================================================================================
2246 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
2248 if ( thePos < 1 || thePos > theString.Length() )
2251 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
2254 return thePos <= theString.Length();
2257 //================================================================================
2259 * \brief Modify a part of the command
2260 * \param thePartIndex - The index of the part
2261 * \param thePart - The new part string
2262 * \param theOldPart - The old part
2264 //================================================================================
2266 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
2267 TCollection_AsciiString& theOldPart)
2269 int pos = GetBegPos( thePartIndex );
2270 if ( pos <= Length() && theOldPart != thePart)
2272 TCollection_AsciiString seperator;
2274 pos = GetBegPos( thePartIndex + 1 );
2275 if ( pos < 1 ) return;
2276 switch ( thePartIndex ) {
2277 case RESULT_IND: seperator = " = "; break;
2278 case OBJECT_IND: seperator = "."; break;
2279 case METHOD_IND: seperator = "()"; break;
2283 myString.Remove( pos, theOldPart.Length() );
2284 if ( !seperator.IsEmpty() )
2285 myString.Insert( pos , seperator );
2286 myString.Insert( pos, thePart );
2287 // update starting positions of the following parts
2288 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
2289 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
2290 if ( myBegPos( i ) > 0 )
2291 myBegPos( i ) += posDelta;
2293 theOldPart = thePart;
2297 //================================================================================
2299 * \brief Set agrument
2300 * \param index - The argument index, it counts from 1
2301 * \param theArg - The argument string
2303 //================================================================================
2305 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
2308 int argInd = ARG1_IND + index - 1;
2309 int pos = GetBegPos( argInd );
2310 if ( pos < 1 ) // no index-th arg exist, append inexistent args
2312 // find a closing parenthesis
2313 if ( GetNbArgs() != 0 && index <= GetNbArgs() ) {
2314 int lastArgInd = GetNbArgs();
2315 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
2316 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
2321 while ( pos > 0 && myString.Value( pos ) != ')' )
2324 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
2328 while ( myArgs.Length() < index ) {
2329 if ( myArgs.Length() )
2330 myString.Insert( pos++, "," );
2331 myArgs.Append("None");
2332 myString.Insert( pos, myArgs.Last() );
2333 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
2334 pos += myArgs.Last().Length();
2337 SetPart( argInd, theArg, myArgs( index ));
2340 //================================================================================
2342 * \brief Empty arg list
2344 //================================================================================
2346 void _pyCommand::RemoveArgs()
2348 if ( int pos = myString.Location( '(', 1, Length() ))
2349 myString.Trunc( pos );
2352 if ( myBegPos.Length() >= ARG1_IND )
2353 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
2356 //================================================================================
2358 * \brief Set dependent commands after this one
2360 //================================================================================
2362 bool _pyCommand::SetDependentCmdsAfter() const
2364 bool orderChanged = false;
2365 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
2366 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
2367 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
2368 orderChanged = true;
2369 theGen->SetCommandAfter( *cmd, this );
2370 (*cmd)->SetDependentCmdsAfter();
2373 return orderChanged;
2375 //================================================================================
2377 * \brief Insert accessor method after theObjectID
2378 * \param theObjectID - id of the accessed object
2379 * \param theAcsMethod - name of the method giving access to the object
2380 * \retval bool - false if theObjectID is not found in the command string
2382 //================================================================================
2384 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
2386 if ( !theAcsMethod )
2388 // start object search from the object, i.e. ignore result
2390 int beg = GetBegPos( OBJECT_IND );
2391 if ( beg < 1 || beg > Length() )
2394 while (( beg = myString.Location( theObjectID, beg, Length() )))
2396 // check that theObjectID is not just a part of a longer ID
2397 int afterEnd = beg + theObjectID.Length();
2398 Standard_Character c = myString.Value( afterEnd );
2399 if ( !isalnum( c ) && c != ':' ) {
2400 // check if accessor method already present
2402 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
2404 int oldLen = Length();
2405 myString.Insert( afterEnd, (char*) theAcsMethod );
2406 myString.Insert( afterEnd, "." );
2407 // update starting positions of the parts following the modified one
2408 int posDelta = Length() - oldLen;
2409 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
2410 if ( myBegPos( i ) > afterEnd )
2411 myBegPos( i ) += posDelta;
2416 beg = afterEnd; // is a part - next search
2421 //================================================================================
2423 * \brief Return method name giving access to an interaface object wrapped by python class
2424 * \retval const char* - method name
2426 //================================================================================
2428 const char* _pyObject::AccessorMethod() const
2432 //================================================================================
2434 * \brief Return ID of a father
2436 //================================================================================
2438 _pyID _pyObject::FatherID(const _pyID & childID)
2440 int colPos = childID.SearchFromEnd(':');
2442 return childID.SubString( 1, colPos-1 );
2446 //================================================================================
2448 * \brief SelfEraser erases creation command if no more it's commands invoked
2450 //================================================================================
2452 void _pySelfEraser::Flush()
2454 if ( GetNbCalls() == 0 )
2455 GetCreationCmd()->Clear();
2458 //================================================================================
2460 * \brief count invoked commands
2462 //================================================================================
2464 void _pySubMesh::Process( const Handle(_pyCommand)& theCommand )
2466 _pyObject::Process(theCommand); // count calls of Process()
2467 GetCreationCmd()->AddDependantCmd( theCommand );
2470 //================================================================================
2472 * \brief Clear creatin command if no commands invoked
2474 //================================================================================
2476 void _pySubMesh::Flush()
2478 if ( GetNbCalls() == 0 ) // move to the end of all commands
2479 theGen->GetLastCommand()->AddDependantCmd( GetCreationCmd() );
2480 else if ( !myCreator.IsNull() )
2481 // move to be just after creator
2482 myCreator->GetCreationCmd()->AddDependantCmd( GetCreationCmd() );