1 // Copyright (C) 2007-2011 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" ||
427 method == "CreateFilterManager" ||
428 method == "CreateMeasurements" ) {
429 Handle(_pyObject) obj = new _pySelfEraser( theCommand );
430 if ( !myObjects.insert( make_pair( obj->GetID(), obj )).second )
431 theCommand->Clear(); // already created
434 // Concatenate( [mesh1, ...], ... )
435 if ( method == "Concatenate" || method == "ConcatenateWithGroups")
437 if ( method == "ConcatenateWithGroups" ) {
438 theCommand->SetMethod( "Concatenate" );
439 theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
441 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
442 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
443 AddMeshAccessorMethod( theCommand );
446 // Replace name of SMESH_Gen
448 // names of SMESH_Gen methods fully equal to methods defined in smesh.py
449 static TStringSet smeshpyMethods;
450 if ( smeshpyMethods.empty() ) {
451 const char * names[] =
452 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
453 "GetPattern","GetSubShapesId",
454 "" }; // <- mark of array end
455 smeshpyMethods.Insert( names );
457 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
458 // smeshgen.Method() --> smesh.Method()
459 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
461 // smeshgen.Method() --> smesh.smesh.Method()
462 theCommand->SetObject( SMESH_2smeshpy::GenName() );
465 //================================================================================
467 * \brief Convert the remembered commands
469 //================================================================================
473 // create empty command
474 myLastCommand = new _pyCommand();
476 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
477 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
478 if ( ! id_mesh->second.IsNull() )
479 id_mesh->second->Flush();
481 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
482 for ( ; hyp != myHypos.end(); ++hyp )
483 if ( !hyp->IsNull() ) {
485 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
486 if ( !(*hyp)->IsWrapped() )
487 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
490 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
491 for ( ; id_obj != myObjects.end(); ++id_obj )
492 if ( ! id_obj->second.IsNull() )
493 id_obj->second->Flush();
495 myLastCommand->SetOrderNb( ++myNbCommands );
496 myCommands.push_back( myLastCommand );
499 //================================================================================
501 * \brief Add access method to mesh that is an argument
502 * \param theCmd - command to add access method
503 * \retval bool - true if added
505 //================================================================================
507 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
510 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
511 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
512 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
518 //================================================================================
520 * \brief Add access method to algo that is an object or an argument
521 * \param theCmd - command to add access method
522 * \retval bool - true if added
524 //================================================================================
526 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
529 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
530 for ( ; hyp != myHypos.end(); ++hyp ) {
531 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
532 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
538 //================================================================================
540 * \brief Find hypothesis by ID (entry)
541 * \param theHypID - The hypothesis ID
542 * \retval Handle(_pyHypothesis) - The found hypothesis
544 //================================================================================
546 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
548 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
549 for ( ; hyp != myHypos.end(); ++hyp )
550 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
552 return Handle(_pyHypothesis)();
555 //================================================================================
557 * \brief Find algorithm the created algorithm
558 * \param theGeom - The shape ID the algorithm was created on
559 * \param theMesh - The mesh ID that created the algorithm
560 * \param dim - The algo dimension
561 * \retval Handle(_pyHypothesis) - The found algo
563 //================================================================================
565 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
566 const Handle(_pyHypothesis)& theHypothesis )
568 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
569 for ( ; hyp != myHypos.end(); ++hyp )
570 if ( !hyp->IsNull() &&
572 theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
573 (*hyp)->GetGeom() == theGeom &&
574 (*hyp)->GetMesh() == theMesh )
579 //================================================================================
581 * \brief Find subMesh by ID (entry)
582 * \param theSubMeshID - The subMesh ID
583 * \retval Handle(_pySubMesh) - The found subMesh
585 //================================================================================
587 Handle(_pySubMesh) _pyGen::FindSubMesh( const _pyID& theSubMeshID )
589 map< _pyID, Handle(_pyObject) >::iterator id_subMesh = myObjects.find(theSubMeshID);
590 if ( id_subMesh != myObjects.end() )
591 return Handle(_pySubMesh)::DownCast( id_subMesh->second );
592 return Handle(_pySubMesh)();
596 //================================================================================
598 * \brief Change order of commands in the script
599 * \param theCmd1 - One command
600 * \param theCmd2 - Another command
602 //================================================================================
604 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
606 list< Handle(_pyCommand) >::iterator pos1, pos2;
607 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
608 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
609 myCommands.insert( pos1, theCmd2 );
610 myCommands.insert( pos2, theCmd1 );
611 myCommands.erase( pos1 );
612 myCommands.erase( pos2 );
614 int nb1 = theCmd1->GetOrderNb();
615 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
616 theCmd2->SetOrderNb( nb1 );
617 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
618 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
621 //================================================================================
623 * \brief Set one command after the other
624 * \param theCmd - Command to move
625 * \param theAfterCmd - Command ater which to insert the first one
627 //================================================================================
629 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
631 setNeighbourCommand( theCmd, theAfterCmd, true );
634 //================================================================================
636 * \brief Set one command before the other
637 * \param theCmd - Command to move
638 * \param theBeforeCmd - Command before which to insert the first one
640 //================================================================================
642 void _pyGen::SetCommandBefore( Handle(_pyCommand) theCmd, Handle(_pyCommand) theBeforeCmd )
644 setNeighbourCommand( theCmd, theBeforeCmd, false );
647 //================================================================================
649 * \brief Set one command before or after the other
650 * \param theCmd - Command to move
651 * \param theOtherCmd - Command ater or before which to insert the first one
653 //================================================================================
655 void _pyGen::setNeighbourCommand( Handle(_pyCommand)& theCmd,
656 Handle(_pyCommand)& theOtherCmd,
657 const bool theIsAfter )
659 list< Handle(_pyCommand) >::iterator pos;
660 pos = find( myCommands.begin(), myCommands.end(), theCmd );
661 myCommands.erase( pos );
662 pos = find( myCommands.begin(), myCommands.end(), theOtherCmd );
663 myCommands.insert( (theIsAfter ? ++pos : pos), theCmd );
666 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
667 (*pos)->SetOrderNb( i++ );
670 //================================================================================
672 * \brief Set command be last in list of commands
673 * \param theCmd - Command to be last
675 //================================================================================
677 Handle(_pyCommand)& _pyGen::GetLastCommand()
679 return myLastCommand;
682 //================================================================================
684 * \brief Set method to access to object wrapped with python class
685 * \param theID - The wrapped object entry
686 * \param theMethod - The accessor method
688 //================================================================================
690 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
692 myID2AccessorMethod.Bind( theID, (char*) theMethod );
695 //================================================================================
697 * \brief Generated new ID for object and assign with existing name
698 * \param theID - ID of existing object
700 //================================================================================
702 _pyID _pyGen::GenerateNewID( const _pyID& theID )
707 aNewID = theID + _pyID( ":" ) + _pyID( index++ );
709 while ( myObjectNames.IsBound( aNewID ) );
711 myObjectNames.Bind( aNewID, myObjectNames.IsBound( theID )
712 ? (myObjectNames.Find( theID ) + _pyID( "_" ) + _pyID( index-1 ))
713 : _pyID( "A" ) + aNewID );
717 //================================================================================
719 * \brief Find out type of geom group
720 * \param grpID - The geom group entry
721 * \retval int - The type
723 //================================================================================
725 // static bool sameGroupType( const _pyID& grpID,
726 // const TCollection_AsciiString& theType)
728 // // define group type as smesh.Mesh.Group() does
730 // SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
731 // SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
732 // if ( !aSObj->_is_nil() ) {
733 // GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
734 // if ( !aGeomObj->_is_nil() ) {
735 // switch ( aGeomObj->GetShapeType() ) {
736 // case GEOM::VERTEX: type = SMESH::NODE; break;
737 // case GEOM::EDGE: type = SMESH::EDGE; break;
738 // case GEOM::FACE: type = SMESH::FACE; break;
740 // case GEOM::SHELL: type = SMESH::VOLUME; break;
741 // case GEOM::COMPOUND: {
742 // GEOM::GEOM_Gen_ptr aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
743 // if ( !aGeomGen->_is_nil() ) {
744 // GEOM::GEOM_IGroupOperations_var aGrpOp =
745 // aGeomGen->GetIGroupOperations( study->StudyId() );
746 // if ( !aGrpOp->_is_nil() ) {
747 // switch ( aGrpOp->GetType( aGeomObj )) {
748 // case TopAbs_VERTEX: type = SMESH::NODE; break;
749 // case TopAbs_EDGE: type = SMESH::EDGE; break;
750 // case TopAbs_FACE: type = SMESH::FACE; break;
751 // case TopAbs_SOLID: type = SMESH::VOLUME; break;
762 // MESSAGE("Type of the group " << grpID << " not found");
765 // if ( theType.IsIntegerValue() )
766 // return type == theType.IntegerValue();
769 // case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
770 // case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
771 // case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
772 // case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
778 //================================================================================
781 * \param theCreationCmd -
783 //================================================================================
785 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd)
786 : _pyObject(theCreationCmd), myHasEditor(false)
788 // convert my creation command
789 Handle(_pyCommand) creationCmd = GetCreationCmd();
790 //TCollection_AsciiString str = creationCmd->GetMethod();
791 // if(str != "CreateMeshesFromUNV" &&
792 // str != "CreateMeshesFromMED" &&
793 // str != "CreateMeshesFromSTL")
794 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
795 creationCmd->SetMethod( "Mesh" );
797 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
800 //================================================================================
803 * \param theCreationCmd -
805 //================================================================================
806 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd, const TCollection_AsciiString& id):
807 _pyObject(theCreationCmd), myHasEditor(false)
809 // convert my creation command
810 Handle(_pyCommand) creationCmd = GetCreationCmd();
811 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
812 theGen->SetAccessorMethod( id, "GetMesh()" );
815 //================================================================================
817 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
818 * \param theCommand - Engine method called for this mesh
820 //================================================================================
822 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
824 // some methods of SMESH_Mesh interface needs special conversion
825 // to methods of Mesh python class
827 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
828 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
829 // 2. AddHypothesis(geom, hyp)
830 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
831 // 3. CreateGroupFromGEOM(type, name, grp)
832 // --> in Mesh.Group(grp, name="")
833 // 4. ExportToMED(f, auto_groups, version)
834 // --> in Mesh.ExportMED( f, auto_groups, version )
837 const TCollection_AsciiString method = theCommand->GetMethod();
838 // ----------------------------------------------------------------------
839 if ( method == "GetSubMesh" ) {
840 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetResultValue() );
841 if ( !subMesh.IsNull() ) {
842 subMesh->SetCreator( this );
843 mySubmeshes.push_back( subMesh );
846 // ----------------------------------------------------------------------
847 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
848 myAddHypCmds.push_back( theCommand );
850 const _pyID& hypID = theCommand->GetArg( 2 );
851 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
852 if ( !hyp.IsNull() ) {
853 myHypos.push_back( hyp );
854 if ( hyp->GetMesh().IsEmpty() )
855 hyp->SetMesh( this->GetID() );
858 // ----------------------------------------------------------------------
859 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
860 _pyID grp = theCommand->GetArg( 3 );
861 // VSR 24/12/2010. PAL21106: always use GroupOnGeom() function on dump
862 // next if(){...} section is commented
863 //if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
864 // theCommand->SetMethod( "Group" );
865 // theCommand->RemoveArgs();
866 // theCommand->SetArg( 1, grp );
869 _pyID type = theCommand->GetArg( 1 );
870 _pyID name = theCommand->GetArg( 2 );
871 theCommand->SetMethod( "GroupOnGeom" );
872 theCommand->RemoveArgs();
873 theCommand->SetArg( 1, grp );
874 theCommand->SetArg( 2, name );
875 theCommand->SetArg( 3, type );
878 // ----------------------------------------------------------------------
879 else if ( method == "ExportToMED" || // ExportToMED() --> ExportMED()
880 method == "ExportToMEDX" ) { // ExportToMEDX() --> ExportMED()
881 theCommand->SetMethod( "ExportMED" );
883 // ----------------------------------------------------------------------
884 else if ( method == "CreateGroup" ) { // CreateGroup() --> CreateEmptyGroup()
885 theCommand->SetMethod( "CreateEmptyGroup" );
887 // ----------------------------------------------------------------------
888 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
890 _pyID hypID = theCommand->GetArg( 2 );
892 // check if this mesh still has corresponding addition command
893 bool hasAddCmd = false;
894 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
895 while ( cmd != myAddHypCmds.end() )
897 // AddHypothesis(geom, hyp)
898 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
901 cmd = myAddHypCmds.erase( cmd );
908 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
909 if ( ! hasAddCmd && hypID.Length() != 0 ) { // hypo addition already wrapped
910 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
911 _pyID geom = theCommand->GetArg( 1 );
912 theCommand->RemoveArgs();
913 theCommand->SetArg( 1, hypID );
914 if ( geom != GetGeom() )
915 theCommand->SetArg( 2, geom );
917 // remove hyp from myHypos
918 myHypos.remove( hyp );
920 // check for SubMesh order commands
921 else if ( theCommand->GetMethod() == "GetMeshOrder" ||
922 theCommand->GetMethod() == "SetMeshOrder" ) {
923 // In fact arguments and result values does not support complex containers
924 // such as list of list
925 // So, here we parse it manually
927 //for(int ind = 0, n = theCommand->GetNbResultValues();ind<n;ind++) {
928 // Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetResultValue(ind) );
930 //for(int ind = 0, n = theCommand->GetNbArgs();ind<n;ind++) {
931 // Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetArg(ind) );
932 const bool isArg = theCommand->GetMethod() == "SetMeshOrder";
933 const TCollection_AsciiString& cmdStr = theCommand->GetString();
934 int begPos = (/*isArg ? cmdStr.Search( "(" ) :*/ cmdStr.Search( "[" )) + 1;
935 int endPos = (isArg ? cmdStr.Search( ")" ) : cmdStr.Search( "=" )) - 1;
936 if ( begPos != -1 && begPos < endPos && endPos <= cmdStr.Length() ) {
937 TCollection_AsciiString aSubStr = cmdStr.SubString( begPos, endPos );
938 Standard_Integer index = 1;
939 TCollection_AsciiString anIDStr = aSubStr.Token("\t ,[]", index++);
940 while ( !anIDStr.IsEmpty() ) {
941 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( anIDStr );
942 if ( !subMesh.IsNull() )
943 subMesh->Process( theCommand );
944 anIDStr = aSubStr.Token("\t ,[]", index++);
948 // add accessor method if necessary
951 if ( NeedMeshAccess( theCommand ))
952 // apply theCommand to the mesh wrapped by smeshpy mesh
953 AddMeshAccess( theCommand );
957 //================================================================================
959 * \brief Return True if addition of accesor method is needed
961 //================================================================================
963 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
965 // names of SMESH_Mesh methods fully equal to methods of class Mesh, so
966 // no conversion is needed for them at all:
967 static TStringSet sameMethods;
968 if ( sameMethods.empty() ) {
969 const char * names[] =
970 { "ExportDAT","ExportUNV","ExportSTL", "RemoveGroup","RemoveGroupWithContents",
971 "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
972 "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
973 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
974 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
975 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
976 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
977 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
978 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
979 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
980 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
981 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
982 "Clear", "ConvertToStandalone", "GetMeshOrder", "SetMeshOrder"
983 ,"" }; // <- mark of end
984 sameMethods.Insert( names );
987 return !sameMethods.Contains( theCommand->GetMethod() );
990 //================================================================================
992 * \brief Convert creation and addition of all algos and hypos
994 //================================================================================
996 void _pyMesh::Flush()
998 list < Handle(_pyCommand) >::iterator cmd;
1000 // try to convert algo addition like this:
1001 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
1002 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1004 Handle(_pyCommand) addCmd = *cmd;
1006 _pyID algoID = addCmd->GetArg( 2 );
1007 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
1008 if ( algo.IsNull() || !algo->IsAlgo() )
1011 // check and create new algorithm instance if it is already wrapped
1012 if ( algo->IsWrapped() ) {
1013 _pyID localAlgoID = theGen->GenerateNewID( algoID );
1014 TCollection_AsciiString aNewCmdStr = localAlgoID +
1015 TCollection_AsciiString( " = " ) + theGen->GetID() +
1016 TCollection_AsciiString( ".CreateHypothesis( \"" ) + algo->GetAlgoType() +
1017 TCollection_AsciiString( "\" )" );
1019 Handle(_pyCommand) newCmd = theGen->AddCommand( aNewCmdStr );
1020 Handle(_pyAlgorithm) newAlgo = Handle(_pyAlgorithm)::DownCast(theGen->FindHyp( localAlgoID ));
1021 if ( !newAlgo.IsNull() ) {
1022 newAlgo->Assign( algo, this->GetID() );
1023 newAlgo->SetCreationCmd( newCmd );
1025 // set algorithm creation
1026 theGen->SetCommandBefore( newCmd, addCmd );
1031 _pyID geom = addCmd->GetArg( 1 );
1032 bool isLocalAlgo = ( geom != GetGeom() );
1035 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
1037 // wrapped algo is created atfer mesh creation
1038 GetCreationCmd()->AddDependantCmd( addCmd );
1040 if ( isLocalAlgo ) {
1041 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
1042 addCmd->SetArg( addCmd->GetNbArgs() + 1,
1043 TCollection_AsciiString( "geom=" ) + geom );
1044 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
1045 list < Handle(_pySubMesh) >::iterator smIt;
1046 for ( smIt = mySubmeshes.begin(); smIt != mySubmeshes.end(); ++smIt ) {
1047 Handle(_pySubMesh) subMesh = *smIt;
1048 Handle(_pyCommand) subCmd = subMesh->GetCreationCmd();
1049 if ( geom == subCmd->GetArg( 1 )) {
1050 subCmd->SetObject( algo->GetID() );
1051 subCmd->RemoveArgs();
1052 subMesh->SetCreator( algo );
1057 else // KO - ALGO was already created
1059 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
1060 addCmd->RemoveArgs();
1061 addCmd->SetArg( 1, algoID );
1063 addCmd->SetArg( 2, geom );
1067 // try to convert hypo addition like this:
1068 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
1069 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1071 Handle(_pyCommand) addCmd = *cmd;
1072 _pyID hypID = addCmd->GetArg( 2 );
1073 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1074 if ( hyp.IsNull() || hyp->IsAlgo() )
1076 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
1078 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
1079 _pyID geom = addCmd->GetArg( 1 );
1080 addCmd->RemoveArgs();
1081 addCmd->SetArg( 1, hypID );
1082 if ( geom != GetGeom() )
1083 addCmd->SetArg( 2, geom );
1087 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
1088 // for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
1089 // Handle(_pyCommand) subCmd = *cmd;
1090 // if ( subCmd->GetNbArgs() > 0 )
1091 // AddMeshAccess( subCmd );
1093 myAddHypCmds.clear();
1094 mySubmeshes.clear();
1097 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1098 for ( ; hyp != myHypos.end(); ++hyp )
1102 //================================================================================
1104 * \brief MeshEditor convert its commands to ones of mesh
1106 //================================================================================
1108 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
1109 _pyObject( theCreationCmd )
1111 myMesh = theCreationCmd->GetObject();
1112 myCreationCmdStr = theCreationCmd->GetString();
1113 theCreationCmd->Clear();
1116 //================================================================================
1118 * \brief convert its commands to ones of mesh
1120 //================================================================================
1122 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
1124 // names of SMESH_MeshEditor methods fully equal to methods of python class Mesh, so
1125 // commands calling this methods are converted to calls of methods of Mesh
1126 static TStringSet sameMethods;
1127 if ( sameMethods.empty() ) {
1128 const char * names[] = {
1129 "RemoveElements","RemoveNodes","RemoveOrphanNodes","AddNode","Add0DElement","AddEdge","AddFace","AddPolygonalFace",
1130 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode", "MoveClosestNodeToPoint",
1131 "InverseDiag","DeleteDiag","Reorient","ReorientObject","TriToQuad","SplitQuad","SplitQuadObject",
1132 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
1133 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
1134 "RotationSweep","RotationSweepObject","RotationSweepObject1D","RotationSweepObject2D",
1135 "ExtrusionSweep","AdvancedExtrusion","ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D",
1136 "ExtrusionAlongPath","ExtrusionAlongPathObject","ExtrusionAlongPathX",
1137 "ExtrusionAlongPathObject1D","ExtrusionAlongPathObject2D",
1138 "Mirror","MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
1139 "FindCoincidentNodes",/*"FindCoincidentNodesOnPart",*/"MergeNodes","FindEqualElements",
1140 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
1141 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
1142 "GetLastCreatedElems",
1143 "MirrorMakeMesh","MirrorObjectMakeMesh","TranslateMakeMesh",
1144 "TranslateObjectMakeMesh","RotateMakeMesh","RotateObjectMakeMesh","MakeBoundaryMesh"
1145 ,"" }; // <- mark of the end
1146 sameMethods.Insert( names );
1149 // names of SMESH_MeshEditor methods which differ from methods of class Mesh
1150 // only by last two arguments
1151 static TStringSet diffLastTwoArgsMethods;
1152 if (diffLastTwoArgsMethods.empty() ) {
1153 const char * names[] = {
1154 "MirrorMakeGroups","MirrorObjectMakeGroups",
1155 "TranslateMakeGroups","TranslateObjectMakeGroups",
1156 "RotateMakeGroups","RotateObjectMakeGroups",
1157 ""};// <- mark of the end
1158 diffLastTwoArgsMethods.Insert( names );
1161 const TCollection_AsciiString & method = theCommand->GetMethod();
1162 bool isPyMeshMethod = sameMethods.Contains( method );
1163 if ( !isPyMeshMethod )
1165 //Replace SMESH_MeshEditor "MakeGroups" functions by the Mesh
1166 //functions with the flag "theMakeGroups = True" like:
1167 //SMESH_MeshEditor.CmdMakeGroups => Mesh.Cmd(...,True)
1168 int pos = method.Search("MakeGroups");
1171 isPyMeshMethod = true;
1173 // 1. Remove "MakeGroups" from the Command
1174 TCollection_AsciiString aMethod = theCommand->GetMethod();
1175 int nbArgsToAdd = diffLastTwoArgsMethods.Contains(aMethod) ? 2 : 1;
1176 aMethod.Trunc(pos-1);
1177 theCommand->SetMethod(aMethod);
1179 // 2. And add last "True" argument(s)
1180 while(nbArgsToAdd--)
1181 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
1185 // set "ExtrusionAlongPathX()" instead of "ExtrusionAlongPathObjX()"
1186 if ( !isPyMeshMethod && method == "ExtrusionAlongPathObjX")
1188 isPyMeshMethod=true;
1189 theCommand->SetMethod("ExtrusionAlongPathX");
1192 // set "FindCoincidentNodesOnPart()" instead of "FindCoincidentNodesOnPartBut()"
1193 if ( !isPyMeshMethod && method == "FindCoincidentNodesOnPartBut")
1195 isPyMeshMethod=true;
1196 theCommand->SetMethod("FindCoincidentNodesOnPart");
1198 // DoubleNodeElemGroupNew() -> DoubleNodeElemGroup()
1199 // DoubleNodeGroupNew() -> DoubleNodeGroup()
1200 // DoubleNodeGroupsNew() -> DoubleNodeGroups()
1201 // DoubleNodeElemGroupsNew() -> DoubleNodeElemGroups()
1202 if ( !isPyMeshMethod && ( method == "DoubleNodeElemGroupNew" ||
1203 method == "DoubleNodeElemGroupsNew" ||
1204 method == "DoubleNodeGroupNew" ||
1205 method == "DoubleNodeGroupsNew"))
1207 isPyMeshMethod=true;
1208 theCommand->SetMethod( method.SubString( 1, method.Length()-3));
1209 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
1211 // ConvertToQuadraticObject(bool,obj) -> ConvertToQuadratic(bool,obj)
1212 // ConvertFromQuadraticObject(obj) -> ConvertFromQuadratic(obj)
1213 if ( !isPyMeshMethod && ( method == "ConvertToQuadraticObject" ||
1214 method == "ConvertFromQuadraticObject" ))
1216 isPyMeshMethod=true;
1217 theCommand->SetMethod( method.SubString( 1, method.Length()-6));
1218 // prevent moving creation of the converted sub-mesh to the end of the script
1219 bool isFromQua = ( method.Value( 8 ) == 'F' );
1220 Handle(_pySubMesh) sm = theGen->FindSubMesh( theCommand->GetArg( isFromQua ? 1 : 2 ));
1222 sm->Process( theCommand );
1225 // meshes made by *MakeMesh() methods are not wrapped by _pyMesh,
1226 // so let _pyMesh care of it (TMP?)
1227 // if ( theCommand->GetMethod().Search("MakeMesh") != -1 )
1228 // _pyMesh( new _pyCommand( theCommand->GetString(), 0 )); // for theGen->SetAccessorMethod()
1229 if ( isPyMeshMethod )
1231 theCommand->SetObject( myMesh );
1235 // editor creation command is needed only if any editor function is called
1236 theGen->AddMeshAccessorMethod( theCommand ); // for *Object()
1237 if ( !myCreationCmdStr.IsEmpty() ) {
1238 GetCreationCmd()->GetString() = myCreationCmdStr;
1239 myCreationCmdStr.Clear();
1244 //================================================================================
1246 * \brief _pyHypothesis constructor
1247 * \param theCreationCmd -
1249 //================================================================================
1251 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
1252 _pyObject( theCreationCmd )
1254 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
1257 //================================================================================
1259 * \brief Creates algorithm or hypothesis
1260 * \param theCreationCmd - The engine command creating a hypothesis
1261 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
1263 //================================================================================
1265 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
1267 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
1268 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
1270 Handle(_pyHypothesis) hyp, algo;
1273 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
1274 if ( hypTypeQuoted.IsEmpty() )
1277 TCollection_AsciiString hypType =
1278 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1280 algo = new _pyAlgorithm( theCreationCmd );
1281 hyp = new _pyHypothesis( theCreationCmd );
1283 // 1D Regular_1D ----------
1284 if ( hypType == "Regular_1D" ) {
1285 // set mesh's method creating algo,
1286 // i.e. convertion result will be "regular1d = Mesh.Segment()",
1287 // and set hypType by which algo creating a hypothesis is searched for
1288 algo->SetConvMethodAndType("Segment", hypType.ToCString());
1290 else if ( hypType == "CompositeSegment_1D" ) {
1291 algo->SetConvMethodAndType("Segment", "Regular_1D");
1292 algo->myArgs.Append( "algo=smesh.COMPOSITE");
1294 else if ( hypType == "LocalLength" ) {
1295 // set algo's method creating hyp, and algo type
1296 hyp->SetConvMethodAndType( "LocalLength", "Regular_1D");
1297 // set method whose 1 arg will become the 1-st arg of hyp creation command
1298 // i.e. convertion result will be "locallength = regular1d.LocalLength(<arg of SetLength()>)"
1299 hyp->AddArgMethod( "SetLength" );
1301 else if ( hypType == "MaxLength" ) {
1302 // set algo's method creating hyp, and algo type
1303 hyp->SetConvMethodAndType( "MaxSize", "Regular_1D");
1304 // set method whose 1 arg will become the 1-st arg of hyp creation command
1305 // i.e. convertion result will be "maxsize = regular1d.MaxSize(<arg of SetLength()>)"
1306 hyp->AddArgMethod( "SetLength" );
1308 else if ( hypType == "NumberOfSegments" ) {
1309 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
1310 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
1311 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
1312 hyp->AddArgMethod( "SetNumberOfSegments" );
1313 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
1314 hyp->AddArgMethod( "SetScaleFactor" );
1315 hyp->AddArgMethod( "SetReversedEdges" );
1317 else if ( hypType == "Arithmetic1D" ) {
1318 hyp = new _pyComplexParamHypo( theCreationCmd );
1319 hyp->SetConvMethodAndType( "Arithmetic1D", "Regular_1D");
1320 hyp->AddArgMethod( "SetStartLength" );
1321 hyp->AddArgMethod( "SetEndLength" );
1322 hyp->AddArgMethod( "SetReversedEdges" );
1324 else if ( hypType == "StartEndLength" ) {
1325 hyp = new _pyComplexParamHypo( theCreationCmd );
1326 hyp->SetConvMethodAndType( "StartEndLength", "Regular_1D");
1327 hyp->AddArgMethod( "SetStartLength" );
1328 hyp->AddArgMethod( "SetEndLength" );
1329 hyp->AddArgMethod( "SetReversedEdges" );
1331 else if ( hypType == "Deflection1D" ) {
1332 hyp->SetConvMethodAndType( "Deflection1D", "Regular_1D");
1333 hyp->AddArgMethod( "SetDeflection" );
1335 else if ( hypType == "Propagation" ) {
1336 hyp->SetConvMethodAndType( "Propagation", "Regular_1D");
1338 else if ( hypType == "QuadraticMesh" ) {
1339 hyp->SetConvMethodAndType( "QuadraticMesh", "Regular_1D");
1341 else if ( hypType == "AutomaticLength" ) {
1342 hyp->SetConvMethodAndType( "AutomaticLength", "Regular_1D");
1343 hyp->AddArgMethod( "SetFineness");
1345 else if ( hypType == "SegmentLengthAroundVertex" ) {
1346 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
1347 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
1348 hyp->AddArgMethod( "SetLength" );
1350 // 1D Python_1D ----------
1351 else if ( hypType == "Python_1D" ) {
1352 algo->SetConvMethodAndType( "Segment", hypType.ToCString());
1353 algo->myArgs.Append( "algo=smesh.PYTHON");
1355 else if ( hypType == "PythonSplit1D" ) {
1356 hyp->SetConvMethodAndType( "PythonSplit1D", "Python_1D");
1357 hyp->AddArgMethod( "SetNumberOfSegments");
1358 hyp->AddArgMethod( "SetPythonLog10RatioFunction");
1360 // MEFISTO_2D ----------
1361 else if ( hypType == "MEFISTO_2D" ) { // MEFISTO_2D
1362 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
1364 else if ( hypType == "MaxElementArea" ) {
1365 hyp->SetConvMethodAndType( "MaxElementArea", "MEFISTO_2D");
1366 hyp->SetConvMethodAndType( "MaxElementArea", "NETGEN_2D_ONLY");
1367 hyp->AddArgMethod( "SetMaxElementArea");
1369 else if ( hypType == "LengthFromEdges" ) {
1370 hyp->SetConvMethodAndType( "LengthFromEdges", "MEFISTO_2D");
1371 hyp->SetConvMethodAndType( "LengthFromEdges", "NETGEN_2D_ONLY");
1373 // Quadrangle_2D ----------
1374 else if ( hypType == "Quadrangle_2D" ) {
1375 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
1377 else if ( hypType == "QuadranglePreference" ) {
1378 hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
1379 hyp->SetConvMethodAndType( "SetQuadAllowed", "NETGEN_2D_ONLY");
1381 else if ( hypType == "TrianglePreference" ) {
1382 hyp->SetConvMethodAndType( "TrianglePreference", "Quadrangle_2D");
1384 // RadialQuadrangle_1D2D ----------
1385 else if ( hypType == "RadialQuadrangle_1D2D" ) {
1386 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
1387 algo->myArgs.Append( "algo=smesh.RADIAL_QUAD" );
1389 else if ( hypType == "NumberOfLayers2D" ) {
1390 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialQuadrangle_1D2D");
1391 hyp->AddArgMethod( "SetNumberOfLayers" );
1393 else if ( hypType == "LayerDistribution2D" ) {
1394 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get2DHypothesis" );
1395 hyp->SetConvMethodAndType( "LayerDistribution", "RadialQuadrangle_1D2D");
1397 // BLSURF ----------
1398 else if ( hypType == "BLSURF" ) {
1399 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
1400 algo->myArgs.Append( "algo=smesh.BLSURF" );
1402 else if ( hypType == "BLSURF_Parameters") {
1403 hyp->SetConvMethodAndType( "Parameters", "BLSURF");
1405 // NETGEN ----------
1406 else if ( hypType == "NETGEN_2D") { // 1D-2D
1407 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1408 algo->myArgs.Append( "algo=smesh.NETGEN" );
1410 else if ( hypType == "NETGEN_Parameters_2D") {
1411 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D");
1413 else if ( hypType == "NETGEN_SimpleParameters_2D") {
1414 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D");
1415 hyp->myArgs.Append( "which=smesh.SIMPLE" );
1417 else if ( hypType == "NETGEN_2D3D") { // 1D-2D-3D
1418 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1419 algo->myArgs.Append( "algo=smesh.FULL_NETGEN" );
1421 else if ( hypType == "NETGEN_Parameters") {
1422 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D3D");
1424 else if ( hypType == "NETGEN_SimpleParameters_3D") {
1425 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D3D");
1426 hyp->myArgs.Append( "which=smesh.SIMPLE" );
1428 else if ( hypType == "NETGEN_2D_ONLY") { // 2D
1429 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1430 algo->myArgs.Append( "algo=smesh.NETGEN_2D" );
1432 else if ( hypType == "NETGEN_3D") { // 3D
1433 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1434 algo->myArgs.Append( "algo=smesh.NETGEN" );
1436 else if ( hypType == "MaxElementVolume") {
1437 hyp->SetConvMethodAndType( "MaxElementVolume", "NETGEN_3D");
1438 hyp->AddArgMethod( "SetMaxElementVolume" );
1440 // GHS3D_3D ----------
1441 else if ( hypType == "GHS3D_3D" ) {
1442 algo->SetConvMethodAndType( "Tetrahedron", hypType.ToCString());
1443 algo->myArgs.Append( "algo=smesh.GHS3D" );
1445 else if ( hypType == "GHS3D_Parameters") {
1446 hyp->SetConvMethodAndType( "Parameters", "GHS3D_3D");
1448 // Hexa_3D ---------
1449 else if ( hypType == "BLSURF" ) {
1450 algo->SetConvMethodAndType( "Hexahedron", hypType.ToCString());
1452 // Repetitive Projection_1D ---------
1453 else if ( hypType == "Projection_1D" ) {
1454 algo->SetConvMethodAndType( "Projection1D", hypType.ToCString());
1456 else if ( hypType == "ProjectionSource1D" ) {
1457 hyp->SetConvMethodAndType( "SourceEdge", "Projection_1D");
1458 hyp->AddArgMethod( "SetSourceEdge");
1459 hyp->AddArgMethod( "SetSourceMesh");
1460 // 2 args of SetVertexAssociation() will become the 3-th and 4-th args of hyp creation command
1461 hyp->AddArgMethod( "SetVertexAssociation", 2 );
1463 // Projection_2D ---------
1464 else if ( hypType == "Projection_2D" ) {
1465 algo->SetConvMethodAndType( "Projection2D", hypType.ToCString());
1467 else if ( hypType == "ProjectionSource2D" ) {
1468 hyp->SetConvMethodAndType( "SourceFace", "Projection_2D");
1469 hyp->AddArgMethod( "SetSourceFace");
1470 hyp->AddArgMethod( "SetSourceMesh");
1471 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1473 // Projection_3D ---------
1474 else if ( hypType == "Projection_3D" ) {
1475 algo->SetConvMethodAndType( "Projection3D", hypType.ToCString());
1477 else if ( hypType == "ProjectionSource3D" ) {
1478 hyp->SetConvMethodAndType( "SourceShape3D", "Projection_3D");
1479 hyp->AddArgMethod( "SetSource3DShape");
1480 hyp->AddArgMethod( "SetSourceMesh");
1481 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1483 // Prism_3D ---------
1484 else if ( hypType == "Prism_3D" ) {
1485 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1487 // RadialPrism_3D ---------
1488 else if ( hypType == "RadialPrism_3D" ) {
1489 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1491 else if ( hypType == "NumberOfLayers" ) {
1492 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialPrism_3D");
1493 hyp->AddArgMethod( "SetNumberOfLayers" );
1495 else if ( hypType == "LayerDistribution" ) {
1496 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get3DHypothesis" );
1497 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
1500 return algo->IsValid() ? algo : hyp;
1503 //================================================================================
1505 * \brief Convert the command adding a hypothesis to mesh into a smesh command
1506 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
1507 * \param theAlgo - The algo that can create this hypo
1508 * \retval bool - false if the command cant be converted
1510 //================================================================================
1512 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
1513 const _pyID& theMesh)
1515 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
1517 if ( !IsWrappable( theMesh ))
1520 myGeom = theCmd->GetArg( 1 );
1522 Handle(_pyHypothesis) algo;
1524 // find algo created on myGeom in theMesh
1525 algo = theGen->FindAlgo( myGeom, theMesh, this );
1526 if ( algo.IsNull() )
1528 // attach hypothesis creation command to be after algo creation command
1529 // because it can be new created instance of algorithm
1530 algo->GetCreationCmd()->AddDependantCmd( theCmd );
1534 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
1535 theCmd->SetResultValue( GetID() );
1536 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
1537 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
1539 theCmd->RemoveArgs();
1540 for ( int i = 1; i <= myArgs.Length(); ++i ) {
1541 if ( !myArgs( i ).IsEmpty() )
1542 theCmd->SetArg( i, myArgs( i ));
1544 theCmd->SetArg( i, "[]");
1546 // set a new creation command
1547 GetCreationCmd()->Clear();
1548 // replace creation command by wrapped instance
1549 // please note, that hypothesis attaches to algo creation command (see upper)
1550 SetCreationCmd( theCmd );
1553 // clear commands setting arg values
1554 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
1555 for ( ; argCmd != myArgCommands.end(); ++argCmd )
1558 // set unknown arg commands after hypo creation
1559 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
1560 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
1561 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1562 afterCmd->AddDependantCmd( *cmd );
1568 //================================================================================
1570 * \brief Remember hypothesis parameter values
1571 * \param theCommand - The called hypothesis method
1573 //================================================================================
1575 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
1577 ASSERT( !myIsAlgo );
1580 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
1581 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
1582 while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
1583 myArgs.Append( "[]" );
1584 for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
1585 myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
1586 myArgCommands.push_back( theCommand );
1589 nbArgs += myNbArgsByMethod( i );
1591 myUnknownCommands.push_back( theCommand );
1594 //================================================================================
1596 * \brief Finish conversion
1598 //================================================================================
1600 void _pyHypothesis::Flush()
1602 if ( IsWrapped() ) {
1605 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1606 for ( ; cmd != myArgCommands.end(); ++cmd ) {
1607 // Add access to a wrapped mesh
1608 theGen->AddMeshAccessorMethod( *cmd );
1609 // Add access to a wrapped algorithm
1610 theGen->AddAlgoAccessorMethod( *cmd );
1612 cmd = myUnknownCommands.begin();
1613 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1614 // Add access to a wrapped mesh
1615 theGen->AddMeshAccessorMethod( *cmd );
1616 // Add access to a wrapped algorithm
1617 theGen->AddAlgoAccessorMethod( *cmd );
1620 // forget previous hypothesis modifications
1621 myArgCommands.clear();
1622 myUnknownCommands.clear();
1625 //================================================================================
1627 * \brief clear creation, arg and unkown commands
1629 //================================================================================
1631 void _pyHypothesis::ClearAllCommands()
1633 GetCreationCmd()->Clear();
1634 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1635 for ( ; cmd != myArgCommands.end(); ++cmd )
1637 cmd = myUnknownCommands.begin();
1638 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1643 //================================================================================
1645 * \brief Assign fields of theOther to me except myIsWrapped
1647 //================================================================================
1649 void _pyHypothesis::Assign( const Handle(_pyHypothesis)& theOther,
1650 const _pyID& theMesh )
1652 myIsWrapped = false;
1655 // myCreationCmd = theOther->myCreationCmd;
1656 myIsAlgo = theOther->myIsAlgo;
1657 myGeom = theOther->myGeom;
1658 myType2CreationMethod = theOther->myType2CreationMethod;
1659 myArgs = theOther->myArgs;
1660 myArgMethods = theOther->myArgMethods;
1661 myNbArgsByMethod = theOther->myNbArgsByMethod;
1662 myArgCommands = theOther->myArgCommands;
1663 myUnknownCommands = theOther->myUnknownCommands;
1666 //================================================================================
1668 * \brief Remember hypothesis parameter values
1669 * \param theCommand - The called hypothesis method
1671 //================================================================================
1673 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1675 if( theCommand->GetMethod() == "SetLength" )
1677 // NOW it becomes OBSOLETE
1678 // ex: hyp.SetLength(start, 1)
1679 // hyp.SetLength(end, 0)
1680 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1681 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1682 while ( myArgs.Length() < i )
1683 myArgs.Append( "[]" );
1684 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1685 myArgCommands.push_back( theCommand );
1689 _pyHypothesis::Process( theCommand );
1692 //================================================================================
1694 * \brief Clear SetObjectEntry() as it is called by methods of Mesh_Segment
1696 //================================================================================
1698 void _pyComplexParamHypo::Flush()
1702 list < Handle(_pyCommand) >::iterator cmd = myUnknownCommands.begin();
1703 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1704 if ((*cmd)->GetMethod() == "SetObjectEntry" )
1709 //================================================================================
1711 * \brief Convert methods of 1D hypotheses to my own methods
1712 * \param theCommand - The called hypothesis method
1714 //================================================================================
1716 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1718 if ( theCommand->GetMethod() != "SetLayerDistribution" )
1721 _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1723 const _pyID& hyp1dID = theCommand->GetArg( 1 );
1724 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1725 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1727 else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1728 // 1D hypo is already set, so distribution changes and the old
1729 // 1D hypo is thrown away
1730 my1dHyp->ClearAllCommands();
1734 if ( !myArgCommands.empty() )
1735 myArgCommands.front()->Clear();
1736 myArgCommands.push_back( theCommand );
1739 //================================================================================
1742 * \param theAdditionCmd - command to be converted
1743 * \param theMesh - mesh instance
1744 * \retval bool - status
1746 //================================================================================
1748 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1749 const _pyID& theMesh)
1751 myIsWrapped = false;
1753 if ( my1dHyp.IsNull() )
1756 // set "SetLayerDistribution()" after addition cmd
1757 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1759 _pyID geom = theAdditionCmd->GetArg( 1 );
1761 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
1762 if ( !algo.IsNull() )
1764 my1dHyp->SetMesh( theMesh );
1765 my1dHyp->SetConvMethodAndType(my1dHyp->GetAlgoCreationMethod().ToCString(),
1766 algo->GetAlgoType().ToCString());
1767 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1770 // clear "SetLayerDistribution()" cmd
1771 myArgCommands.back()->Clear();
1773 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1775 // find RadialPrism algo created on <geom> for theMesh
1776 GetCreationCmd()->SetObject( algo->GetID() );
1777 GetCreationCmd()->SetMethod( myAlgoMethod );
1778 GetCreationCmd()->RemoveArgs();
1779 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1785 //================================================================================
1789 //================================================================================
1791 void _pyLayerDistributionHypo::Flush()
1793 // as creation of 1D hyp was written later then it's edition,
1794 // we need to find all it's edition calls and process them
1795 if ( !my1dHyp.IsNull() )
1797 _pyID hyp1dID = my1dHyp->GetCreationCmd()->GetResultValue();
1799 // make a new name for 1D hyp = "HypType" + "_Distribution"
1801 if ( my1dHyp->IsWrapped() ) {
1802 newName = my1dHyp->GetCreationCmd()->GetMethod();
1805 TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1806 newName = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1808 newName += "_Distribution";
1809 my1dHyp->GetCreationCmd()->SetResultValue( newName );
1811 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1812 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1813 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1814 const _pyID& objID = (*cmdIt)->GetObject();
1815 if ( objID == hyp1dID ) {
1816 my1dHyp->Process( *cmdIt );
1817 my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1818 ( *cmdIt )->SetObject( newName );
1821 // Set new hyp name to SetLayerDistribution() cmd
1822 if ( !myArgCommands.empty() && !myArgCommands.back()->IsEmpty() )
1823 myArgCommands.back()->SetArg( 1, newName );
1827 //================================================================================
1829 * \brief additionally to Addition2Creation, clears SetDistrType() command
1830 * \param theCmd - AddHypothesis() command
1831 * \param theMesh - mesh to which a hypothesis is added
1832 * \retval bool - convertion result
1834 //================================================================================
1836 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1837 const _pyID& theMesh)
1839 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1840 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1841 bool scaleDistrType = false;
1842 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1843 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1844 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1845 if ( (*cmd)->GetArg( 1 ) == "1" ) {
1846 scaleDistrType = true;
1849 else if ( !scaleDistrType ) {
1850 // distribution type changed: remove scale factor from args
1851 myArgs.Remove( 2, myArgs.Length() );
1857 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1860 //================================================================================
1862 * \brief remove repeated commands defining distribution
1864 //================================================================================
1866 void _pyNumberOfSegmentsHyp::Flush()
1868 // find number of the last SetDistrType() command
1869 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1870 int distrTypeNb = 0;
1871 for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1872 if ( (*cmd)->GetMethod() == "SetDistrType" )
1873 distrTypeNb = (*cmd)->GetOrderNb();
1874 else if (IsWrapped() && (*cmd)->GetMethod() == "SetObjectEntry" )
1877 // clear commands before the last SetDistrType()
1878 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1879 for ( int i = 0; i < 2; ++i ) {
1880 set<TCollection_AsciiString> uniqueMethods;
1881 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1882 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1884 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1885 const TCollection_AsciiString& method = (*cmd)->GetMethod();
1886 if ( !clear || method == "SetNumberOfSegments" ) {
1887 bool isNewInSet = uniqueMethods.insert( method ).second;
1888 clear = !isNewInSet;
1897 //================================================================================
1899 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
1900 * into regular1D.LengthNearVertex( length, vertex )
1901 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
1902 * \param theMesh - The mesh needing this hypo
1903 * \retval bool - false if the command cant be converted
1905 //================================================================================
1907 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1908 const _pyID& theMeshID)
1910 if ( IsWrappable( theMeshID )) {
1912 _pyID vertex = theCmd->GetArg( 1 );
1914 // the problem here is that segment algo will not be found
1915 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
1916 // geometry where segment algorithm is assigned
1917 Handle(_pyHypothesis) algo;
1918 _pyID geom = vertex;
1919 while ( algo.IsNull() && !geom.IsEmpty()) {
1920 // try to find geom as a father of <vertex>
1921 geom = FatherID( geom );
1922 algo = theGen->FindAlgo( geom, theMeshID, this );
1924 if ( algo.IsNull() )
1925 return false; // also possible to find geom as brother of veretex...
1926 // set geom instead of vertex
1927 theCmd->SetArg( 1, geom );
1929 // set vertex as a second arg
1930 if ( myArgs.Length() < 1) myArgs.Append( "1" ); // :(
1931 myArgs.Append( vertex );
1933 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
1934 // theMeshID.LengthNearVertex( length, vertex )
1935 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
1940 //================================================================================
1942 * \brief _pyAlgorithm constructor
1943 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1945 //================================================================================
1947 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1948 : _pyHypothesis( theCreationCmd )
1953 //================================================================================
1955 * \brief Convert the command adding an algorithm to mesh
1956 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1957 * \param theMesh - The mesh needing this algo
1958 * \retval bool - false if the command cant be converted
1960 //================================================================================
1962 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1963 const _pyID& theMeshID)
1965 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1966 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1967 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1973 //================================================================================
1975 * \brief Return starting position of a part of python command
1976 * \param thePartIndex - The index of command part
1977 * \retval int - Part position
1979 //================================================================================
1981 int _pyCommand::GetBegPos( int thePartIndex )
1985 if ( myBegPos.Length() < thePartIndex )
1987 return myBegPos( thePartIndex );
1990 //================================================================================
1992 * \brief Store starting position of a part of python command
1993 * \param thePartIndex - The index of command part
1994 * \param thePosition - Part position
1996 //================================================================================
1998 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
2000 while ( myBegPos.Length() < thePartIndex )
2001 myBegPos.Append( UNKNOWN );
2002 myBegPos( thePartIndex ) = thePosition;
2005 //================================================================================
2007 * \brief Returns whitespace symbols at the line beginning
2008 * \retval TCollection_AsciiString - result
2010 //================================================================================
2012 TCollection_AsciiString _pyCommand::GetIndentation()
2015 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
2016 GetWord( myString, end, true );
2018 end = GetBegPos( RESULT_IND );
2019 return myString.SubString( 1, end - 1 );
2022 //================================================================================
2024 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
2025 * \retval const TCollection_AsciiString & - ResultValue substring
2027 //================================================================================
2029 const TCollection_AsciiString & _pyCommand::GetResultValue()
2031 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
2033 int begPos = myString.Location( "=", 1, Length() );
2035 myRes = GetWord( myString, begPos, false );
2038 SetBegPos( RESULT_IND, begPos );
2043 //================================================================================
2045 * \brief Return number of python command result value ResultValue = Obj.Meth()
2048 //================================================================================
2050 const int _pyCommand::GetNbResultValues()
2054 int endPos = myString.Location( "=", 1, Length() );
2055 TCollection_AsciiString str = "";
2056 while ( begPos < endPos) {
2057 str = GetWord( myString, begPos, true );
2058 begPos = begPos+ str.Length();
2065 //================================================================================
2067 * \brief Return substring of python command looking like
2068 * ResultValue1 , ResultValue1,... = Obj.Meth() with res index
2069 * \retval const TCollection_AsciiString & - ResultValue with res index substring
2071 //================================================================================
2072 const TCollection_AsciiString & _pyCommand::GetResultValue(int res)
2076 int endPos = myString.Location( "=", 1, Length() );
2077 while ( begPos < endPos) {
2078 myRes = GetWord( myString, begPos, true );
2079 begPos = begPos + myRes.Length();
2082 myRes.RemoveAll('[');myRes.RemoveAll(']');
2088 return theEmptyString;
2091 //================================================================================
2093 * \brief Return substring of python command looking like ResVal = Object.Meth()
2094 * \retval const TCollection_AsciiString & - Object substring
2096 //================================================================================
2098 const TCollection_AsciiString & _pyCommand::GetObject()
2100 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
2103 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
2105 begPos = myString.Location( "=", 1, Length() ) + 1;
2106 // is '=' in the string argument (for example, name) or not
2107 int nb1 = 0; // number of ' character at the left of =
2108 int nb2 = 0; // number of " character at the left of =
2109 for ( int i = 1; i < begPos-1; i++ ) {
2110 if ( myString.Value( i )=='\'' )
2112 else if ( myString.Value( i )=='"' )
2115 // if number of ' or " is not divisible by 2,
2116 // then get an object at the start of the command
2117 if ( nb1 % 2 != 0 || nb2 % 2 != 0 )
2120 myObj = GetWord( myString, begPos, true );
2121 // check if object is complex,
2122 // so far consider case like "smesh.smesh.Method()"
2123 if ( int bracketPos = myString.Location( "(", begPos, Length() )) {
2124 //if ( bracketPos==0 ) bracketPos = Length();
2125 int dotPos = begPos+myObj.Length();
2126 while ( dotPos+1 < bracketPos ) {
2127 if ( int pos = myString.Location( ".", dotPos+1, bracketPos ))
2132 if ( dotPos > begPos+myObj.Length() )
2133 myObj = myString.SubString( begPos, dotPos-1 );
2136 SetBegPos( OBJECT_IND, begPos );
2142 //================================================================================
2144 * \brief Return substring of python command looking like ResVal = Obj.Method()
2145 * \retval const TCollection_AsciiString & - Method substring
2147 //================================================================================
2149 const TCollection_AsciiString & _pyCommand::GetMethod()
2151 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
2154 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
2155 bool forward = true;
2157 begPos = myString.Location( "(", 1, Length() ) - 1;
2161 myMeth = GetWord( myString, begPos, forward );
2162 SetBegPos( METHOD_IND, begPos );
2168 //================================================================================
2170 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
2171 * \retval const TCollection_AsciiString & - Arg<index> substring
2173 //================================================================================
2175 const TCollection_AsciiString & _pyCommand::GetArg( int index )
2177 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
2180 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
2182 begPos = myString.Location( "(", 1, Length() ) + 1;
2184 int i = 0, prevLen = 0, nbNestings = 0;
2185 while ( begPos != EMPTY ) {
2187 if( myString.Value( begPos ) == '(' )
2189 // check if we are looking at the closing parenthesis
2190 while ( begPos <= Length() && isspace( myString.Value( begPos )))
2192 if ( begPos > Length() )
2194 if ( myString.Value( begPos ) == ')' ) {
2196 if( nbNestings == 0 )
2199 myArgs.Append( GetWord( myString, begPos, true, true ));
2200 SetBegPos( ARG1_IND + i, begPos );
2201 prevLen = myArgs.Last().Length();
2203 myArgs.Remove( myArgs.Length() ); // no more args
2207 if ( myArgs.Length() < index )
2208 return theEmptyString;
2209 return myArgs( index );
2212 //================================================================================
2214 * \brief Check if char is a word part
2215 * \param c - The character to check
2216 * \retval bool - The check result
2218 //================================================================================
2220 static inline bool isWord(const char c, const bool dotIsWord)
2223 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
2226 //================================================================================
2228 * \brief Looks for a word in the string and returns word's beginning
2229 * \param theString - The input string
2230 * \param theStartPos - The position to start the search, returning word's beginning
2231 * \param theForward - The search direction
2232 * \retval TCollection_AsciiString - The found word
2234 //================================================================================
2236 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
2238 const bool theForward,
2239 const bool dotIsWord )
2241 int beg = theStartPos, end = theStartPos;
2242 theStartPos = EMPTY;
2243 if ( beg < 1 || beg > theString.Length() )
2244 return theEmptyString;
2246 if ( theForward ) { // search forward
2248 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
2250 if ( beg > theString.Length() )
2251 return theEmptyString; // no word found
2254 char begChar = theString.Value( beg );
2255 if ( begChar == '"' || begChar == '\'' || begChar == '[') {
2256 char endChar = ( begChar == '[' ) ? ']' : begChar;
2257 // end is at the corresponding quoting mark or bracket
2258 while ( end < theString.Length() &&
2259 ( theString.Value( end ) != endChar || theString.Value( end-1 ) == '\\'))
2263 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
2268 else { // search backward
2270 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
2273 return theEmptyString; // no word found
2275 char endChar = theString.Value( end );
2276 if ( endChar == '"' || endChar == '\'' ) {
2277 // beg is at the corresponding quoting mark
2279 ( theString.Value( beg ) != endChar || theString.Value( beg-1 ) == '\\'))
2283 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
2289 //cout << theString << " ---- " << beg << " - " << end << endl;
2290 return theString.SubString( beg, end );
2293 //================================================================================
2295 * \brief Look for position where not space char is
2296 * \param theString - The string
2297 * \param thePos - The position to search from and which returns result
2298 * \retval bool - false if there are only space after thePos in theString
2302 //================================================================================
2304 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
2306 if ( thePos < 1 || thePos > theString.Length() )
2309 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
2312 return thePos <= theString.Length();
2315 //================================================================================
2317 * \brief Modify a part of the command
2318 * \param thePartIndex - The index of the part
2319 * \param thePart - The new part string
2320 * \param theOldPart - The old part
2322 //================================================================================
2324 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
2325 TCollection_AsciiString& theOldPart)
2327 int pos = GetBegPos( thePartIndex );
2328 if ( pos <= Length() && theOldPart != thePart)
2330 TCollection_AsciiString seperator;
2332 pos = GetBegPos( thePartIndex + 1 );
2333 if ( pos < 1 ) return;
2334 switch ( thePartIndex ) {
2335 case RESULT_IND: seperator = " = "; break;
2336 case OBJECT_IND: seperator = "."; break;
2337 case METHOD_IND: seperator = "()"; break;
2341 myString.Remove( pos, theOldPart.Length() );
2342 if ( !seperator.IsEmpty() )
2343 myString.Insert( pos , seperator );
2344 myString.Insert( pos, thePart );
2345 // update starting positions of the following parts
2346 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
2347 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
2348 if ( myBegPos( i ) > 0 )
2349 myBegPos( i ) += posDelta;
2351 theOldPart = thePart;
2355 //================================================================================
2357 * \brief Set agrument
2358 * \param index - The argument index, it counts from 1
2359 * \param theArg - The argument string
2361 //================================================================================
2363 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
2366 int argInd = ARG1_IND + index - 1;
2367 int pos = GetBegPos( argInd );
2368 if ( pos < 1 ) // no index-th arg exist, append inexistent args
2370 // find a closing parenthesis
2371 if ( GetNbArgs() != 0 && index <= GetNbArgs() ) {
2372 int lastArgInd = GetNbArgs();
2373 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
2374 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
2379 while ( pos > 0 && myString.Value( pos ) != ')' )
2382 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
2386 while ( myArgs.Length() < index ) {
2387 if ( myArgs.Length() )
2388 myString.Insert( pos++, "," );
2389 myArgs.Append("None");
2390 myString.Insert( pos, myArgs.Last() );
2391 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
2392 pos += myArgs.Last().Length();
2395 SetPart( argInd, theArg, myArgs( index ));
2398 //================================================================================
2400 * \brief Empty arg list
2402 //================================================================================
2404 void _pyCommand::RemoveArgs()
2406 if ( int pos = myString.Location( '(', 1, Length() ))
2407 myString.Trunc( pos );
2410 if ( myBegPos.Length() >= ARG1_IND )
2411 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
2414 //================================================================================
2416 * \brief Set dependent commands after this one
2418 //================================================================================
2420 bool _pyCommand::SetDependentCmdsAfter() const
2422 bool orderChanged = false;
2423 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
2424 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
2425 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
2426 orderChanged = true;
2427 theGen->SetCommandAfter( *cmd, this );
2428 (*cmd)->SetDependentCmdsAfter();
2431 return orderChanged;
2433 //================================================================================
2435 * \brief Insert accessor method after theObjectID
2436 * \param theObjectID - id of the accessed object
2437 * \param theAcsMethod - name of the method giving access to the object
2438 * \retval bool - false if theObjectID is not found in the command string
2440 //================================================================================
2442 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
2444 if ( !theAcsMethod )
2446 // start object search from the object, i.e. ignore result
2448 int beg = GetBegPos( OBJECT_IND );
2449 if ( beg < 1 || beg > Length() )
2452 while (( beg = myString.Location( theObjectID, beg, Length() )))
2454 // check that theObjectID is not just a part of a longer ID
2455 int afterEnd = beg + theObjectID.Length();
2456 Standard_Character c = myString.Value( afterEnd );
2457 if ( !isalnum( c ) && c != ':' ) {
2458 // check if accessor method already present
2460 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
2462 int oldLen = Length();
2463 myString.Insert( afterEnd, (char*) theAcsMethod );
2464 myString.Insert( afterEnd, "." );
2465 // update starting positions of the parts following the modified one
2466 int posDelta = Length() - oldLen;
2467 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
2468 if ( myBegPos( i ) > afterEnd )
2469 myBegPos( i ) += posDelta;
2474 beg = afterEnd; // is a part - next search
2479 //================================================================================
2481 * \brief Return method name giving access to an interaface object wrapped by python class
2482 * \retval const char* - method name
2484 //================================================================================
2486 const char* _pyObject::AccessorMethod() const
2490 //================================================================================
2492 * \brief Return ID of a father
2494 //================================================================================
2496 _pyID _pyObject::FatherID(const _pyID & childID)
2498 int colPos = childID.SearchFromEnd(':');
2500 return childID.SubString( 1, colPos-1 );
2504 //================================================================================
2506 * \brief SelfEraser erases creation command if no more it's commands invoked
2508 //================================================================================
2510 void _pySelfEraser::Flush()
2512 if ( GetNbCalls() == 0 )
2513 GetCreationCmd()->Clear();
2516 //================================================================================
2518 * \brief count invoked commands
2520 //================================================================================
2522 void _pySubMesh::Process( const Handle(_pyCommand)& theCommand )
2524 _pyObject::Process(theCommand); // count calls of Process()
2525 GetCreationCmd()->AddDependantCmd( theCommand );
2528 //================================================================================
2530 * \brief Clear creation command if no commands invoked
2532 //================================================================================
2534 void _pySubMesh::Flush()
2536 if ( GetNbCalls() == 0 ) // move to the end of all commands
2537 theGen->GetLastCommand()->AddDependantCmd( GetCreationCmd() );
2538 else if ( !myCreator.IsNull() )
2539 // move to be just after creator
2540 myCreator->GetCreationCmd()->AddDependantCmd( GetCreationCmd() );