1 // Copyright (C) 2007-2012 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 // File : SMESH_2smeshpy.cxx
24 // Created : Fri Nov 18 13:20:10 2005
25 // Author : Edward AGAPOV (eap)
27 #include "SMESH_2smeshpy.hxx"
29 #include "SMESH_PythonDump.hxx"
30 #include "SMESH_NoteBook.hxx"
31 #include "SMESH_Filter_i.hxx"
33 #include <utilities.h>
35 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
36 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
38 #include "SMESH_Gen_i.hxx"
39 /* SALOME headers that include CORBA headers that include windows.h
40 * that defines GetObject symbol as GetObjectA should stand before SALOME headers
41 * that declare methods named GetObject - to apply the same rules of GetObject renaming
42 * and thus to avoid mess with GetObject symbol on Windows */
44 #include <LDOMParser.hxx>
53 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
54 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
55 IMPLEMENT_STANDARD_HANDLE (_pyHypothesisReader,Standard_Transient);
56 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
57 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
58 IMPLEMENT_STANDARD_HANDLE (_pySubMesh ,_pyObject);
59 IMPLEMENT_STANDARD_HANDLE (_pyMeshEditor ,_pyObject);
60 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
61 IMPLEMENT_STANDARD_HANDLE (_pySelfEraser ,_pyObject);
62 IMPLEMENT_STANDARD_HANDLE (_pyGroup ,_pyObject);
63 IMPLEMENT_STANDARD_HANDLE (_pyFilter ,_pyObject);
64 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
65 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
66 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
68 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
69 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
70 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesisReader,Standard_Transient);
71 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
72 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
73 IMPLEMENT_STANDARD_RTTIEXT(_pySubMesh ,_pyObject);
74 IMPLEMENT_STANDARD_RTTIEXT(_pyMeshEditor ,_pyObject);
75 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
76 IMPLEMENT_STANDARD_RTTIEXT(_pySelfEraser ,_pyObject);
77 IMPLEMENT_STANDARD_RTTIEXT(_pyGroup ,_pyObject);
78 IMPLEMENT_STANDARD_RTTIEXT(_pyFilter ,_pyObject);
79 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
80 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
81 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
82 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
83 IMPLEMENT_STANDARD_RTTIEXT(_pySegmentLengthAroundVertexHyp,_pyHypothesis);
86 using SMESH::TPythonDump;
89 * \brief Container of commands into which the initial script is split.
90 * It also contains data coresponding to SMESH_Gen contents
92 static Handle(_pyGen) theGen;
94 static TCollection_AsciiString theEmptyString;
96 //#define DUMP_CONVERSION
98 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
99 #undef DUMP_CONVERSION
105 //================================================================================
107 * \brief Set of TCollection_AsciiString initialized by C array of C strings
109 //================================================================================
111 struct TStringSet: public set<TCollection_AsciiString>
114 * \brief Filling. The last string must be ""
116 void Insert(const char* names[]) {
117 for ( int i = 0; names[i][0] ; ++i )
118 insert( (char*) names[i] );
121 * \brief Check if a string is in
123 bool Contains(const TCollection_AsciiString& name ) {
124 return find( name ) != end();
128 //================================================================================
130 * \brief Map of TCollection_AsciiString initialized by C array of C strings.
131 * Odd items of the C array are map keys, and even items are values
133 //================================================================================
135 struct TStringMap: public map<TCollection_AsciiString,TCollection_AsciiString>
138 * \brief Filling. The last string must be ""
140 void Insert(const char* names_values[]) {
141 for ( int i = 0; names_values[i][0] ; i += 2 )
142 insert( make_pair( (char*) names_values[i], names_values[i+1] ));
145 * \brief Check if a string is in
147 TCollection_AsciiString Value(const TCollection_AsciiString& name ) {
148 map< _AString, _AString >::iterator it = find( name );
149 return it == end() ? "" : it->second;
153 //================================================================================
155 * \brief Returns a mesh by object
157 //================================================================================
159 Handle(_pyMesh) ObjectToMesh( const Handle( _pyObject )& obj )
163 if ( obj->IsKind( STANDARD_TYPE( _pyMesh )))
164 return Handle(_pyMesh)::DownCast( obj );
165 else if ( obj->IsKind( STANDARD_TYPE( _pySubMesh )))
166 return Handle(_pySubMesh)::DownCast( obj )->GetMesh();
167 else if ( obj->IsKind( STANDARD_TYPE( _pyGroup )))
168 return Handle(_pyGroup)::DownCast( obj )->GetMesh();
170 return Handle(_pyMesh)();
173 //================================================================================
175 * \brief Check if objects used as args have been created by previous commands
177 //================================================================================
179 void CheckObjectPresence( const Handle(_pyCommand)& cmd, set<_pyID> & presentObjects)
181 // either comment or erase a command including NotPublishedObjectName()
182 if ( cmd->GetString().Location( TPythonDump::NotPublishedObjectName(), 1, cmd->Length() ))
184 bool isResultPublished = false;
185 for ( int i = 0; i < cmd->GetNbResultValues(); i++ )
187 _pyID objID = cmd->GetResultValue( i+1 );
188 if ( cmd->IsStudyEntry( objID ))
189 isResultPublished = (! theGen->IsNotPublished( objID ));
190 theGen->ObjectCreationRemoved( objID ); // objID.SetName( name ) is not needed
192 if ( isResultPublished )
198 // check if an Object was created in the script
200 const _pyID& obj = cmd->GetObject();
201 if ( !obj.IsEmpty() && cmd->IsStudyEntry( obj ) && !presentObjects.count( obj ))
203 comment = "not created Object";
204 theGen->ObjectCreationRemoved( obj );
206 // check if a command has not created args
207 for ( int iArg = cmd->GetNbArgs(); iArg && comment.IsEmpty(); --iArg )
209 const _pyID& arg = cmd->GetArg( iArg );
210 if ( arg.IsEmpty() || arg.Value( 1 ) == '"' || arg.Value( 1 ) == '\'' )
212 list< _pyID > idList = cmd->GetStudyEntries( arg );
213 list< _pyID >::iterator id = idList.begin();
214 for ( ; id != idList.end(); ++id )
215 if ( !theGen->IsGeomObject( *id ) && !presentObjects.count( *id ))
217 comment += *id + " has not been yet created";
221 // treat result objects
222 const _pyID& result = cmd->GetResultValue();
223 if ( !result.IsEmpty() && result.Value( 1 ) != '"' && result.Value( 1 ) != '\'' )
225 list< _pyID > idList = cmd->GetStudyEntries( result );
226 list< _pyID >::iterator id = idList.begin();
227 for ( ; id != idList.end(); ++id )
228 if ( comment.IsEmpty() )
229 presentObjects.insert( *id );
231 theGen->ObjectCreationRemoved( *id ); // objID.SetName( name ) is not needed
233 // comment the command
234 if ( !comment.IsEmpty() )
237 cmd->GetString() += " ### ";
238 cmd->GetString() += comment;
242 //================================================================================
244 * \brief Fix SMESH::FunctorType arguments of SMESH::Filter::Criterion()
246 //================================================================================
248 void fixFunctorType( TCollection_AsciiString& Type,
249 TCollection_AsciiString& Compare,
250 TCollection_AsciiString& UnaryOp,
251 TCollection_AsciiString& BinaryOp )
253 // The problem is that dumps of old studies created using filters becomes invalid
254 // when new items are inserted in the enum SMESH::FunctorType since values
255 // of this enum are dumped as integer values.
256 // This function corrects enum values of old studies given as args (Type,Compare,...)
257 // We can find out how to correct them by value of BinaryOp which can have only two
258 // values: FT_Undefined or FT_LogicalNOT.
259 // Hereafter is the history of the enum SMESH::FunctorType since v3.0.0
260 // where PythonDump appeared
261 // v 3.0.0: FT_Undefined == 25
262 // v 3.1.0: FT_Undefined == 26, new items:
264 // v 4.1.2: FT_Undefined == 27, new items:
265 // - FT_BelongToGenSurface = 17
266 // v 5.1.1: FT_Undefined == 32, new items:
267 // - FT_FreeNodes = 10
268 // - FT_FreeFaces = 11
269 // - FT_LinearOrQuadratic = 23
270 // - FT_GroupColor = 24
271 // - FT_ElemGeomType = 25
272 // v 5.1.5: FT_Undefined == 33, new items:
273 // - FT_CoplanarFaces = 26
274 // v 6.2.0: FT_Undefined == 39, new items:
275 // - FT_MaxElementLength2D = 8
276 // - FT_MaxElementLength3D = 9
277 // - FT_BareBorderVolume = 25
278 // - FT_BareBorderFace = 26
279 // - FT_OverConstrainedVolume = 27
280 // - FT_OverConstrainedFace = 28
281 // v 6.5.0: FT_Undefined == 43, new items:
282 // - FT_EqualNodes = 14
283 // - FT_EqualEdges = 15
284 // - FT_EqualFaces = 16
285 // - FT_EqualVolumes = 17
286 // v 6.6.0: FT_Undefined == 44, new items:
287 // - FT_BallDiameter = 37
288 // v 6.7.1: FT_Undefined == 45, new items:
289 // - FT_EntityType = 36
291 // It's necessary to continue recording this history and to fill
292 // undef2newItems (see below) accordingly.
294 typedef map< int, vector< int > > TUndef2newItems;
295 static TUndef2newItems undef2newItems;
296 if ( undef2newItems.empty() )
298 undef2newItems[ 26 ].push_back( 7 );
299 undef2newItems[ 27 ].push_back( 17 );
300 { int items[] = { 10, 11, 23, 24, 25 };
301 undef2newItems[ 32 ].assign( items, items+5 ); }
302 undef2newItems[ 33 ].push_back( 26 );
303 { int items[] = { 8, 9, 25, 26, 27, 28 };
304 undef2newItems[ 39 ].assign( items, items+6 ); }
305 { int items[] = { 14, 15, 16, 17 };
306 undef2newItems[ 43 ].assign( items, items+4 ); }
307 { int items[] = { 37 };
308 undef2newItems[ 44 ].assign( items, items+1 ); }
309 { int items[] = { 36 };
310 undef2newItems[ 45 ].assign( items, items+1 ); }
313 int iType = Type.IntegerValue();
314 int iCompare = Compare.IntegerValue();
315 int iUnaryOp = UnaryOp.IntegerValue();
316 int iBinaryOp = BinaryOp.IntegerValue();
318 // find out integer value of FT_Undefined at the moment of dump
319 int oldUndefined = iBinaryOp;
320 if ( iBinaryOp < iUnaryOp ) // BinaryOp was FT_LogicalNOT
323 // apply history to args
324 TUndef2newItems::const_iterator undef_items =
325 undef2newItems.upper_bound( oldUndefined );
326 if ( undef_items != undef2newItems.end() )
328 int* pArg[4] = { &iType, &iCompare, &iUnaryOp, &iBinaryOp };
329 for ( ; undef_items != undef2newItems.end(); ++undef_items )
331 const vector< int > & addedItems = undef_items->second;
332 for ( size_t i = 0; i < addedItems.size(); ++i )
333 for ( int iArg = 0; iArg < 4; ++iArg )
335 int& arg = *pArg[iArg];
336 if ( arg >= addedItems[i] )
340 Type = TCollection_AsciiString( iType );
341 Compare = TCollection_AsciiString( iCompare );
342 UnaryOp = TCollection_AsciiString( iUnaryOp );
343 BinaryOp = TCollection_AsciiString( iBinaryOp );
348 //================================================================================
350 * \brief Convert a python script using commands of smesh.py
351 * \param theScript - Input script
352 * \param theEntry2AccessorMethod - returns method names to access to
353 * objects wrapped with python class
354 * \param theObjectNames - names of objects
355 * \param theRemovedObjIDs - entries of objects whose created commands were removed
356 * \param theHistoricalDump - true means to keep all commands, false means
357 * to exclude commands relating to objects removed from study
358 * \retval TCollection_AsciiString - Convertion result
360 //================================================================================
362 TCollection_AsciiString
363 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
364 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
365 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
366 std::set< TCollection_AsciiString >& theRemovedObjIDs,
367 SALOMEDS::Study_ptr& theStudy,
368 const bool theToKeepAllCommands)
370 theGen = new _pyGen( theEntry2AccessorMethod,
374 theToKeepAllCommands );
376 // split theScript into separate commands
378 SMESH_NoteBook * aNoteBook = new SMESH_NoteBook();
380 int from = 1, end = theScript.Length(), to;
381 while ( from < end && ( to = theScript.Location( "\n", from, end )))
384 // cut out and store a command
385 aNoteBook->AddCommand( theScript.SubString( from, to - 1 ));
389 aNoteBook->ReplaceVariables();
391 TCollection_AsciiString aNoteScript = aNoteBook->GetResultScript();
395 // split theScript into separate commands
396 from = 1, end = aNoteScript.Length();
397 while ( from < end && ( to = aNoteScript.Location( "\n", from, end )))
400 // cut out and store a command
401 theGen->AddCommand( aNoteScript.SubString( from, to - 1 ));
407 #ifdef DUMP_CONVERSION
408 MESSAGE_BEGIN ( std::endl << " ######## RESULT ######## " << std::endl<< std::endl );
411 // clean commmands of removed objects depending on myIsPublished flag
412 theGen->ClearCommands();
414 // reorder commands after conversion
415 list< Handle(_pyCommand) >::iterator cmd;
418 orderChanges = false;
419 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
420 if ( (*cmd)->SetDependentCmdsAfter() )
422 } while ( orderChanges );
424 // concat commands back into a script
425 TCollection_AsciiString aScript, aPrevCmd;
426 set<_pyID> createdObjects;
427 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
429 #ifdef DUMP_CONVERSION
430 MESSAGE_ADD ( "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << std::endl );
432 if ( !(*cmd)->IsEmpty() && aPrevCmd != (*cmd)->GetString()) {
433 CheckObjectPresence( *cmd, createdObjects );
434 aPrevCmd = (*cmd)->GetString();
447 //================================================================================
449 * \brief _pyGen constructor
451 //================================================================================
453 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
454 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
455 std::set< TCollection_AsciiString >& theRemovedObjIDs,
456 SALOMEDS::Study_ptr& theStudy,
457 const bool theToKeepAllCommands)
458 : _pyObject( new _pyCommand( "", 0 )),
460 myID2AccessorMethod( theEntry2AccessorMethod ),
461 myObjectNames( theObjectNames ),
462 myRemovedObjIDs( theRemovedObjIDs ),
464 myToKeepAllCommands( theToKeepAllCommands ),
465 myStudy( SALOMEDS::Study::_duplicate( theStudy )),
466 myGeomIDNb(0), myGeomIDIndex(-1)
468 // make that GetID() to return TPythonDump::SMESHGenName()
469 GetCreationCmd()->Clear();
470 GetCreationCmd()->GetString() = TPythonDump::SMESHGenName();
471 GetCreationCmd()->GetString() += "=";
473 // Find 1st digit of study entry by which a GEOM object differs from a SMESH object
474 if ( !theObjectNames.IsEmpty() && !CORBA::is_nil( theStudy ))
478 SALOMEDS::SComponent_var geomComp = theStudy->FindComponent("GEOM");
479 if ( geomComp->_is_nil() ) return;
480 CORBA::String_var entry = geomComp->GetID();
483 // find a SMESH entry
485 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString e2n( theObjectNames );
486 for ( ; e2n.More() && smeshID.IsEmpty(); e2n.Next() )
487 if ( _pyCommand::IsStudyEntry( e2n.Key() ))
490 // find 1st difference between smeshID and geomID
491 if ( !geomID.IsEmpty() && !smeshID.IsEmpty() )
492 for ( int i = 1; i <= geomID.Length() && i <= smeshID.Length(); ++i )
493 if ( geomID.Value( i ) != smeshID.Value( i ))
495 myGeomIDNb = geomID.Value( i );
501 //================================================================================
503 * \brief name of SMESH_Gen in smesh.py
505 //================================================================================
507 const char* _pyGen::AccessorMethod() const
509 return SMESH_2smeshpy::GenName();
512 //================================================================================
514 * \brief Convert a command using a specific converter
515 * \param theCommand - the command to convert
517 //================================================================================
519 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
521 // store theCommand in the sequence
522 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
524 Handle(_pyCommand) aCommand = myCommands.back();
525 #ifdef DUMP_CONVERSION
526 MESSAGE ( "## COM " << myNbCommands << ": "<< aCommand->GetString() );
529 const _pyID& objID = aCommand->GetObject();
531 if ( objID.IsEmpty() )
534 // Prevent moving a command creating a sub-mesh to the end of the script
535 // if the sub-mesh is used in theCommand as argument
536 if ( _pySubMesh::CanBeArgOfMethod( aCommand->GetMethod() ))
538 PlaceSubmeshAfterItsCreation( aCommand );
541 // Find an object to process theCommand
544 if ( objID == this->GetID() || objID == SMESH_2smeshpy::GenName())
546 this->Process( aCommand );
550 // SMESH_Mesh method?
551 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
552 if ( id_mesh != myMeshes.end() )
554 //id_mesh->second->AddProcessedCmd( aCommand );
556 // check for mesh editor object
557 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
558 _pyID editorID = aCommand->GetResultValue();
559 Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
560 myMeshEditors.insert( make_pair( editorID, editor ));
563 // check for SubMesh objects
564 else if ( aCommand->GetMethod() == "GetSubMesh" ) { // SubMesh creation
565 _pyID subMeshID = aCommand->GetResultValue();
566 Handle(_pySubMesh) subMesh = new _pySubMesh( aCommand );
567 myObjects.insert( make_pair( subMeshID, subMesh ));
570 id_mesh->second->Process( aCommand );
571 id_mesh->second->AddProcessedCmd( aCommand );
575 // SMESH_MeshEditor method?
576 map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
577 if ( id_editor != myMeshEditors.end() )
579 const TCollection_AsciiString& method = aCommand->GetMethod();
581 // some commands of SMESH_MeshEditor create meshes and groups
582 _pyID meshID, groups;
583 if ( method.Search("MakeMesh") != -1 )
584 meshID = aCommand->GetResultValue();
585 else if ( method == "MakeBoundaryMesh")
586 meshID = aCommand->GetResultValue(1);
587 else if ( method == "MakeBoundaryElements")
588 meshID = aCommand->GetResultValue(2);
590 if ( method.Search("MakeGroups") != -1 ||
591 method == "ExtrusionAlongPathX" ||
592 method == "ExtrusionAlongPathObjX" ||
593 method == "DoubleNodeGroupNew" ||
594 method == "DoubleNodeGroupsNew" ||
595 method == "DoubleNodeElemGroupNew" ||
596 method == "DoubleNodeElemGroupsNew"||
597 method == "DoubleNodeElemGroup2New"||
598 method == "DoubleNodeElemGroups2New"
600 groups = aCommand->GetResultValue();
601 else if ( method == "MakeBoundaryMesh" )
602 groups = aCommand->GetResultValue(2);
603 else if ( method == "MakeBoundaryElements")
604 groups = aCommand->GetResultValue(3);
605 else if ( method == "Create0DElementsOnAllNodes" &&
606 aCommand->GetArg(2).Length() > 2 ) // group name != ''
607 groups = aCommand->GetResultValue();
609 id_editor->second->Process( aCommand );
610 id_editor->second->AddProcessedCmd( aCommand );
613 if ( !meshID.IsEmpty() &&
614 !myMeshes.count( meshID ) &&
615 aCommand->IsStudyEntry( meshID ))
617 TCollection_AsciiString processedCommand = aCommand->GetString();
618 Handle(_pyMesh) mesh = new _pyMesh( aCommand, meshID );
619 myMeshes.insert( make_pair( meshID, mesh ));
621 aCommand->GetString() = processedCommand; // discard changes made by _pyMesh
624 if ( !groups.IsEmpty() )
626 if ( !aCommand->IsStudyEntry( meshID ))
627 meshID = id_editor->second->GetMesh();
628 Handle(_pyMesh) mesh = myMeshes[ meshID ];
630 list< _pyID > idList = aCommand->GetStudyEntries( groups );
631 list< _pyID >::iterator grID = idList.begin();
632 for ( ; grID != idList.end(); ++grID )
633 if ( !myObjects.count( *grID ))
635 Handle(_pyGroup) group = new _pyGroup( aCommand, *grID );
637 if ( !mesh.IsNull() ) mesh->AddGroup( group );
641 } // SMESH_MeshEditor methods
643 // SMESH_Hypothesis method?
644 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
645 for ( ; hyp != myHypos.end(); ++hyp )
646 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
647 (*hyp)->Process( aCommand );
648 (*hyp)->AddProcessedCmd( aCommand );
652 // aFilterManager.CreateFilter() ?
653 if ( aCommand->GetMethod() == "CreateFilter" )
655 // Set a more human readable name to a filter
656 // aFilter0x7fbf6c71cfb0 -> aFilter_nb
657 _pyID newID, filterID = aCommand->GetResultValue();
658 int pos = filterID.Search( "0x" );
660 newID = (filterID.SubString(1,pos-1) + "_") + _pyID( ++myNbFilters );
662 Handle(_pyObject) filter( new _pyFilter( aCommand, newID ));
666 // other object method?
667 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.find( objID );
668 if ( id_obj != myObjects.end() ) {
669 id_obj->second->Process( aCommand );
670 id_obj->second->AddProcessedCmd( aCommand );
674 // Add access to a wrapped mesh
675 AddMeshAccessorMethod( aCommand );
677 // Add access to a wrapped algorithm
678 // AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
680 // PAL12227. PythonDump was not updated at proper time; result is
681 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
682 // TypeError: __init__() takes exactly 11 arguments (10 given)
683 const char wrongCommand[] = "SMESH.Filter.Criterion(";
684 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
686 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
687 // there must be 10 arguments, 5-th arg ThresholdID is missing,
688 const int wrongNbArgs = 9, missingArg = 5;
689 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
691 for ( int i = wrongNbArgs; i > missingArg; --i )
692 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
693 tmpCmd.SetArg( missingArg, "''");
694 aCommand->GetString().Trunc( beg - 1 );
695 aCommand->GetString() += tmpCmd.GetString();
698 // set GetCriterion(elementType,CritType,Compare,Treshold,UnaryOp,BinaryOp,Tolerance)
700 // instead of "SMESH.Filter.Criterion(
701 // Type,Compare,Threshold,ThresholdStr,ThresholdID,UnaryOp,BinaryOp,Tolerance,TypeOfElement,Precision)
702 // 1 2 3 4 5 6 7 8 9 10
703 // in order to avoid the problem of type mismatch of long and FunctorType
704 const TCollection_AsciiString
705 SMESH("SMESH."), dfltFunctor("SMESH.FT_Undefined"), dftlTol("1e-07"), dftlPreci("-1");
706 TCollection_AsciiString
707 Type = aCommand->GetArg(1), // long
708 Compare = aCommand->GetArg(2), // long
709 Threshold = aCommand->GetArg(3), // double
710 ThresholdStr = aCommand->GetArg(4), // string
711 ThresholdID = aCommand->GetArg(5), // string
712 UnaryOp = aCommand->GetArg(6), // long
713 BinaryOp = aCommand->GetArg(7), // long
714 Tolerance = aCommand->GetArg(8), // double
715 TypeOfElement = aCommand->GetArg(9), // ElementType
716 Precision = aCommand->GetArg(10); // long
717 fixFunctorType( Type, Compare, UnaryOp, BinaryOp );
718 Type = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( Type.IntegerValue() ));
719 Compare = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( Compare.IntegerValue() ));
720 UnaryOp = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( UnaryOp.IntegerValue() ));
721 BinaryOp = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( BinaryOp.IntegerValue() ));
723 aCommand->RemoveArgs();
724 aCommand->SetObject( SMESH_2smeshpy::GenName() );
725 aCommand->SetMethod( "GetCriterion" );
727 aCommand->SetArg( 1, TypeOfElement );
728 aCommand->SetArg( 2, Type );
729 aCommand->SetArg( 3, Compare );
731 if ( Threshold.IsIntegerValue() )
733 int iGeom = Threshold.IntegerValue();
734 if ( Type == "SMESH.FT_ElemGeomType" )
736 // set SMESH.GeometryType instead of a numerical Threshold
737 const char* types[SMESH::Geom_BALL+1] = {
738 "Geom_POINT", "Geom_EDGE", "Geom_TRIANGLE", "Geom_QUADRANGLE", "Geom_POLYGON",
739 "Geom_TETRA", "Geom_PYRAMID", "Geom_HEXA", "Geom_PENTA", "Geom_HEXAGONAL_PRISM",
740 "Geom_POLYHEDRA", "Geom_BALL" };
741 if ( -1 < iGeom && iGeom < SMESH::Geom_POLYHEDRA+1 )
742 Threshold = SMESH + types[ iGeom ];
744 if (Type == "SMESH.FT_EntityType")
746 // set SMESH.EntityType instead of a numerical Threshold
747 const char* types[SMESH::Entity_Ball+1] = {
748 "Entity_Node", "Entity_0D", "Entity_Edge", "Entity_Quad_Edge",
749 "Entity_Triangle", "Entity_Quad_Triangle",
750 "Entity_Quadrangle", "Entity_Quad_Quadrangle", "Entity_BiQuad_Quadrangle",
751 "Entity_Polygon", "Entity_Quad_Polygon", "Entity_Tetra", "Entity_Quad_Tetra",
752 "Entity_Pyramid", "Entity_Quad_Pyramid",
753 "Entity_Hexa", "Entity_Quad_Hexa", "Entity_TriQuad_Hexa",
754 "Entity_Penta", "Entity_Quad_Penta", "Entity_Hexagonal_Prism",
755 "Entity_Polyhedra", "Entity_Quad_Polyhedra", "Entity_Ball" };
756 if ( -1 < iGeom && iGeom < SMESH::Entity_Quad_Polyhedra+1 )
757 Threshold = SMESH + types[ iGeom ];
760 if ( ThresholdID.Length() != 2 && ThresholdStr.Length() != 2) // not '' or ""
761 aCommand->SetArg( 4, ThresholdID.SubString( 2, ThresholdID.Length()-1 )); // shape entry
762 else if ( ThresholdStr.Length() != 2 )
763 aCommand->SetArg( 4, ThresholdStr );
764 else if ( ThresholdID.Length() != 2 )
765 aCommand->SetArg( 4, ThresholdID );
767 aCommand->SetArg( 4, Threshold );
768 // find the last not default arg
770 if ( Tolerance == dftlTol ) {
772 if ( BinaryOp == dfltFunctor ) {
774 if ( UnaryOp == dfltFunctor )
778 if ( 5 < lastDefault ) aCommand->SetArg( 5, UnaryOp );
779 if ( 6 < lastDefault ) aCommand->SetArg( 6, BinaryOp );
780 if ( 7 < lastDefault ) aCommand->SetArg( 7, Tolerance );
781 if ( Precision != dftlPreci )
783 TCollection_AsciiString crit = aCommand->GetResultValue();
784 aCommand->GetString() += "; ";
785 aCommand->GetString() += crit + ".Precision = " + Precision;
791 //================================================================================
793 * \brief Convert the command or remember it for later conversion
794 * \param theCommand - The python command calling a method of SMESH_Gen
796 //================================================================================
798 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
800 // there are methods to convert:
801 // CreateMesh( shape )
802 // Concatenate( [mesh1, ...], ... )
803 // CreateHypothesis( theHypType, theLibName )
804 // Compute( mesh, geom )
805 // Evaluate( mesh, geom )
807 TCollection_AsciiString method = theCommand->GetMethod();
809 if ( method == "CreateMesh" || method == "CreateEmptyMesh")
811 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
812 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
815 if ( method == "CreateMeshesFromUNV" ||
816 method == "CreateMeshesFromSTL" ||
817 method == "CreateMeshesFromCGNS" ||
818 method == "CopyMesh" )
820 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
821 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
824 if( method == "CreateMeshesFromMED" ||
825 method == "CreateMeshesFromSAUV"||
826 method == "CreateMeshesFromGMF" )
828 for ( int ind = 0; ind < theCommand->GetNbResultValues(); ind++ )
830 _pyID meshID = theCommand->GetResultValue(ind+1);
831 if ( !theCommand->IsStudyEntry( meshID ) ) continue;
832 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue(ind+1));
833 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
835 if ( method == "CreateMeshesFromGMF" )
837 // CreateMeshesFromGMF( theFileName, theMakeRequiredGroups ) ->
838 // CreateMeshesFromGMF( theFileName )
839 _AString file = theCommand->GetArg(1);
840 theCommand->RemoveArgs();
841 theCommand->SetArg( 1, file );
845 // CreateHypothesis()
846 if ( method == "CreateHypothesis" )
848 // issue 199929, remove standard library name (default parameter)
849 const TCollection_AsciiString & aLibName = theCommand->GetArg( 2 );
850 if ( aLibName.Search( "StdMeshersEngine" ) != -1 ) {
851 // keep first argument
852 TCollection_AsciiString arg = theCommand->GetArg( 1 );
853 theCommand->RemoveArgs();
854 theCommand->SetArg( 1, arg );
857 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
861 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
862 if ( method == "Compute" )
864 const _pyID& meshID = theCommand->GetArg( 1 );
865 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
866 if ( id_mesh != myMeshes.end() ) {
867 theCommand->SetObject( meshID );
868 theCommand->RemoveArgs();
869 id_mesh->second->Process( theCommand );
870 id_mesh->second->AddProcessedCmd( theCommand );
875 // smeshgen.Evaluate( mesh, geom ) --> mesh.Evaluate(geom)
876 if ( method == "Evaluate" )
878 const _pyID& meshID = theCommand->GetArg( 1 );
879 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
880 if ( id_mesh != myMeshes.end() ) {
881 theCommand->SetObject( meshID );
882 _pyID geom = theCommand->GetArg( 2 );
883 theCommand->RemoveArgs();
884 theCommand->SetArg( 1, geom );
885 id_mesh->second->AddProcessedCmd( theCommand );
890 // objects erasing creation command if no more its commands invoked:
891 // SMESH_Pattern, FilterManager
892 if ( method == "GetPattern" ||
893 method == "CreateFilterManager" ||
894 method == "CreateMeasurements" ) {
895 Handle(_pyObject) obj = new _pySelfEraser( theCommand );
896 if ( !myObjects.insert( make_pair( obj->GetID(), obj )).second )
897 theCommand->Clear(); // already created
899 // Concatenate( [mesh1, ...], ... )
900 else if ( method == "Concatenate" || method == "ConcatenateWithGroups")
902 if ( method == "ConcatenateWithGroups" ) {
903 theCommand->SetMethod( "Concatenate" );
904 theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
906 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
907 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
908 AddMeshAccessorMethod( theCommand );
910 else if ( method == "SetName" ) // SetName(obj,name)
912 // store theCommand as one of object commands to erase it along with the object
913 const _pyID& objID = theCommand->GetArg( 1 );
914 Handle(_pyObject) obj = FindObject( objID );
916 obj->AddProcessedCmd( theCommand );
919 // Replace name of SMESH_Gen
921 // names of SMESH_Gen methods fully equal to methods defined in smesh.py
922 static TStringSet smeshpyMethods;
923 if ( smeshpyMethods.empty() ) {
924 const char * names[] =
925 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
926 "GetPattern","GetSubShapesId",
927 "" }; // <- mark of array end
928 smeshpyMethods.Insert( names );
930 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
931 // smeshgen.Method() --> smesh.Method()
932 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
934 // smeshgen.Method() --> smesh.smesh.Method()
935 theCommand->SetObject( SMESH_2smeshpy::GenName() );
938 //================================================================================
940 * \brief Convert the remembered commands
942 //================================================================================
946 // create an empty command
947 myLastCommand = new _pyCommand();
949 map< _pyID, Handle(_pyMesh) >::iterator id_mesh;
950 map< _pyID, Handle(_pyObject) >::iterator id_obj;
951 list< Handle(_pyHypothesis) >::iterator hyp;
953 if ( IsToKeepAllCommands() ) // historical dump
955 // set myIsPublished = true to all objects
956 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
957 id_mesh->second->SetRemovedFromStudy( false );
958 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
959 (*hyp)->SetRemovedFromStudy( false );
960 for ( id_obj = myObjects.begin(); id_obj != myObjects.end(); ++id_obj )
961 id_obj->second->SetRemovedFromStudy( false );
965 // let hypotheses find referred objects in order to prevent clearing
966 // not published referred hyps (it's needed for hyps like "LayerDistribution")
967 list< Handle(_pyMesh) > fatherMeshes;
968 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
969 if ( !hyp->IsNull() )
970 (*hyp)->GetReferredMeshesAndGeom( fatherMeshes );
972 // set myIsPublished = false to all objects depending on
973 // meshes built on a removed geometry
974 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
975 if ( id_mesh->second->IsNotGeomPublished() )
976 id_mesh->second->SetRemovedFromStudy( true );
979 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
980 if ( ! id_mesh->second.IsNull() )
981 id_mesh->second->Flush();
984 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
985 if ( !hyp->IsNull() ) {
987 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
988 if ( !(*hyp)->IsWrapped() )
989 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
992 // Flush other objects
993 for ( id_obj = myObjects.begin(); id_obj != myObjects.end(); ++id_obj )
994 if ( ! id_obj->second.IsNull() )
995 id_obj->second->Flush();
997 myLastCommand->SetOrderNb( ++myNbCommands );
998 myCommands.push_back( myLastCommand );
1001 //================================================================================
1003 * \brief Prevent moving a command creating a sub-mesh to the end of the script
1004 * if the sub-mesh is used in theCmdUsingSubmesh as argument
1006 //================================================================================
1008 void _pyGen::PlaceSubmeshAfterItsCreation( Handle(_pyCommand) theCmdUsingSubmesh ) const
1010 map< _pyID, Handle(_pyObject) >::const_iterator id_obj = myObjects.begin();
1011 for ( ; id_obj != myObjects.end(); ++id_obj )
1013 if ( !id_obj->second->IsKind( STANDARD_TYPE( _pySubMesh ))) continue;
1014 for ( int iArg = theCmdUsingSubmesh->GetNbArgs(); iArg; --iArg )
1016 const _pyID& arg = theCmdUsingSubmesh->GetArg( iArg );
1017 if ( arg.IsEmpty() || arg.Value( 1 ) == '"' || arg.Value( 1 ) == '\'' )
1019 list< _pyID > idList = theCmdUsingSubmesh->GetStudyEntries( arg );
1020 list< _pyID >::iterator id = idList.begin();
1021 for ( ; id != idList.end(); ++id )
1022 if ( id_obj->first == *id )
1023 // _pySubMesh::Process() does what we need
1024 Handle(_pySubMesh)::DownCast( id_obj->second )->Process( theCmdUsingSubmesh );
1029 //================================================================================
1031 * \brief Clean commmands of removed objects depending on myIsPublished flag
1033 //================================================================================
1035 void _pyGen::ClearCommands()
1037 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
1038 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
1039 id_mesh->second->ClearCommands();
1041 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1042 for ( ; hyp != myHypos.end(); ++hyp )
1043 if ( !hyp->IsNull() )
1044 (*hyp)->ClearCommands();
1046 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
1047 for ( ; id_obj != myObjects.end(); ++id_obj )
1048 id_obj->second->ClearCommands();
1051 //================================================================================
1053 * \brief Release mutual handles of objects
1055 //================================================================================
1059 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
1060 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
1061 id_mesh->second->Free();
1064 map< _pyID, Handle(_pyMeshEditor) >::iterator id_ed = myMeshEditors.begin();
1065 for ( ; id_ed != myMeshEditors.end(); ++id_ed )
1066 id_ed->second->Free();
1067 myMeshEditors.clear();
1069 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
1070 for ( ; id_obj != myObjects.end(); ++id_obj )
1071 id_obj->second->Free();
1074 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1075 for ( ; hyp != myHypos.end(); ++hyp )
1076 if ( !hyp->IsNull() )
1080 myFile2ExportedMesh.clear();
1083 //================================================================================
1085 * \brief Add access method to mesh that is an argument
1086 * \param theCmd - command to add access method
1087 * \retval bool - true if added
1089 //================================================================================
1091 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
1094 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
1095 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
1096 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
1102 //================================================================================
1104 * \brief Add access method to algo that is an object or an argument
1105 * \param theCmd - command to add access method
1106 * \retval bool - true if added
1108 //================================================================================
1110 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
1113 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
1114 for ( ; hyp != myHypos.end(); ++hyp ) {
1115 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
1116 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
1122 //================================================================================
1124 * \brief Find hypothesis by ID (entry)
1125 * \param theHypID - The hypothesis ID
1126 * \retval Handle(_pyHypothesis) - The found hypothesis
1128 //================================================================================
1130 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
1132 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1133 for ( ; hyp != myHypos.end(); ++hyp )
1134 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
1136 return Handle(_pyHypothesis)();
1139 //================================================================================
1141 * \brief Find algorithm the created algorithm
1142 * \param theGeom - The shape ID the algorithm was created on
1143 * \param theMesh - The mesh ID that created the algorithm
1144 * \param dim - The algo dimension
1145 * \retval Handle(_pyHypothesis) - The found algo
1147 //================================================================================
1149 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
1150 const Handle(_pyHypothesis)& theHypothesis )
1152 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1153 for ( ; hyp != myHypos.end(); ++hyp )
1154 if ( !hyp->IsNull() &&
1156 theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
1157 (*hyp)->GetGeom() == theGeom &&
1158 (*hyp)->GetMesh() == theMesh )
1163 //================================================================================
1165 * \brief Find subMesh by ID (entry)
1166 * \param theSubMeshID - The subMesh ID
1167 * \retval Handle(_pySubMesh) - The found subMesh
1169 //================================================================================
1171 Handle(_pySubMesh) _pyGen::FindSubMesh( const _pyID& theSubMeshID )
1173 map< _pyID, Handle(_pyObject) >::iterator id_subMesh = myObjects.find(theSubMeshID);
1174 if ( id_subMesh != myObjects.end() )
1175 return Handle(_pySubMesh)::DownCast( id_subMesh->second );
1176 return Handle(_pySubMesh)();
1180 //================================================================================
1182 * \brief Change order of commands in the script
1183 * \param theCmd1 - One command
1184 * \param theCmd2 - Another command
1186 //================================================================================
1188 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
1190 list< Handle(_pyCommand) >::iterator pos1, pos2;
1191 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
1192 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
1193 myCommands.insert( pos1, theCmd2 );
1194 myCommands.insert( pos2, theCmd1 );
1195 myCommands.erase( pos1 );
1196 myCommands.erase( pos2 );
1198 int nb1 = theCmd1->GetOrderNb();
1199 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
1200 theCmd2->SetOrderNb( nb1 );
1201 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
1202 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
1205 //================================================================================
1207 * \brief Set one command after the other
1208 * \param theCmd - Command to move
1209 * \param theAfterCmd - Command ater which to insert the first one
1211 //================================================================================
1213 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
1215 setNeighbourCommand( theCmd, theAfterCmd, true );
1218 //================================================================================
1220 * \brief Set one command before the other
1221 * \param theCmd - Command to move
1222 * \param theBeforeCmd - Command before which to insert the first one
1224 //================================================================================
1226 void _pyGen::SetCommandBefore( Handle(_pyCommand) theCmd, Handle(_pyCommand) theBeforeCmd )
1228 setNeighbourCommand( theCmd, theBeforeCmd, false );
1231 //================================================================================
1233 * \brief Set one command before or after the other
1234 * \param theCmd - Command to move
1235 * \param theOtherCmd - Command ater or before which to insert the first one
1237 //================================================================================
1239 void _pyGen::setNeighbourCommand( Handle(_pyCommand)& theCmd,
1240 Handle(_pyCommand)& theOtherCmd,
1241 const bool theIsAfter )
1243 list< Handle(_pyCommand) >::iterator pos;
1244 pos = find( myCommands.begin(), myCommands.end(), theCmd );
1245 myCommands.erase( pos );
1246 pos = find( myCommands.begin(), myCommands.end(), theOtherCmd );
1247 myCommands.insert( (theIsAfter ? ++pos : pos), theCmd );
1250 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
1251 (*pos)->SetOrderNb( i++ );
1254 //================================================================================
1256 * \brief Set command be last in list of commands
1257 * \param theCmd - Command to be last
1259 //================================================================================
1261 Handle(_pyCommand)& _pyGen::GetLastCommand()
1263 return myLastCommand;
1266 //================================================================================
1268 * \brief Set method to access to object wrapped with python class
1269 * \param theID - The wrapped object entry
1270 * \param theMethod - The accessor method
1272 //================================================================================
1274 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
1276 myID2AccessorMethod.Bind( theID, (char*) theMethod );
1279 //================================================================================
1281 * \brief Generated new ID for object and assign with existing name
1282 * \param theID - ID of existing object
1284 //================================================================================
1286 _pyID _pyGen::GenerateNewID( const _pyID& theID )
1291 aNewID = theID + _pyID( ":" ) + _pyID( index++ );
1293 while ( myObjectNames.IsBound( aNewID ) );
1295 myObjectNames.Bind( aNewID, myObjectNames.IsBound( theID )
1296 ? (myObjectNames.Find( theID ) + _pyID( "_" ) + _pyID( index-1 ))
1297 : _pyID( "A" ) + aNewID );
1301 //================================================================================
1303 * \brief Stores theObj in myObjects
1305 //================================================================================
1307 void _pyGen::AddObject( Handle(_pyObject)& theObj )
1309 if ( theObj.IsNull() ) return;
1311 if ( theObj->IsKind( STANDARD_TYPE( _pyMesh )))
1312 myMeshes.insert( make_pair( theObj->GetID(), Handle(_pyMesh)::DownCast( theObj )));
1314 else if ( theObj->IsKind( STANDARD_TYPE( _pyMeshEditor )))
1315 myMeshEditors.insert( make_pair( theObj->GetID(), Handle(_pyMeshEditor)::DownCast( theObj )));
1318 myObjects.insert( make_pair( theObj->GetID(), theObj ));
1321 //================================================================================
1323 * \brief Re-register an object with other ID to make it Process() commands of
1324 * other object having this ID
1326 //================================================================================
1328 void _pyGen::SetProxyObject( const _pyID& theID, Handle(_pyObject)& theObj )
1330 if ( theObj.IsNull() ) return;
1332 if ( theObj->IsKind( STANDARD_TYPE( _pyMesh )))
1333 myMeshes.insert( make_pair( theID, Handle(_pyMesh)::DownCast( theObj )));
1335 else if ( theObj->IsKind( STANDARD_TYPE( _pyMeshEditor )))
1336 myMeshEditors.insert( make_pair( theID, Handle(_pyMeshEditor)::DownCast( theObj )));
1339 myObjects.insert( make_pair( theID, theObj ));
1342 //================================================================================
1344 * \brief Finds a _pyObject by ID
1346 //================================================================================
1348 Handle(_pyObject) _pyGen::FindObject( const _pyID& theObjID ) const
1351 map< _pyID, Handle(_pyObject) >::const_iterator id_obj = myObjects.find( theObjID );
1352 if ( id_obj != myObjects.end() )
1353 return id_obj->second;
1356 map< _pyID, Handle(_pyMesh) >::const_iterator id_obj = myMeshes.find( theObjID );
1357 if ( id_obj != myMeshes.end() )
1358 return id_obj->second;
1361 // map< _pyID, Handle(_pyMeshEditor) >::const_iterator id_obj = myMeshEditors.find( theObjID );
1362 // if ( id_obj != myMeshEditors.end() )
1363 // return id_obj->second;
1365 return Handle(_pyObject)();
1368 //================================================================================
1370 * \brief Check if a study entry is under GEOM component
1372 //================================================================================
1374 bool _pyGen::IsGeomObject(const _pyID& theObjID) const
1378 return ( myGeomIDIndex <= theObjID.Length() &&
1379 int( theObjID.Value( myGeomIDIndex )) == myGeomIDNb &&
1380 _pyCommand::IsStudyEntry( theObjID ));
1385 //================================================================================
1387 * \brief Returns true if an object is not present in a study
1389 //================================================================================
1391 bool _pyGen::IsNotPublished(const _pyID& theObjID) const
1393 if ( theObjID.IsEmpty() ) return false;
1395 if ( myObjectNames.IsBound( theObjID ))
1396 return false; // SMESH object is in study
1398 // either the SMESH object is not in study or it is a GEOM object
1399 if ( IsGeomObject( theObjID ))
1401 SALOMEDS::SObject_var so = myStudy->FindObjectID( theObjID.ToCString() );
1402 if ( so->_is_nil() ) return true;
1403 CORBA::Object_var obj = so->GetObject();
1404 return CORBA::is_nil( obj );
1406 return true; // SMESH object not in study
1409 //================================================================================
1411 * \brief Remove object name from myObjectNames that leads to that SetName() for
1412 * this object is not dumped
1413 * \param [in] theObjID - entry of the object whose creation command was eliminated
1415 //================================================================================
1417 void _pyGen::ObjectCreationRemoved(const _pyID& theObjID)
1419 myRemovedObjIDs.insert( theObjID );
1422 //================================================================================
1424 * \brief Return reader of hypotheses of plugins
1426 //================================================================================
1428 Handle( _pyHypothesisReader ) _pyGen::GetHypothesisReader() const
1430 if (myHypReader.IsNull() )
1431 ((_pyGen*) this)->myHypReader = new _pyHypothesisReader;
1437 //================================================================================
1439 * \brief Mesh created by SMESH_Gen
1441 //================================================================================
1443 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd)
1444 : _pyObject( theCreationCmd ), myGeomNotInStudy( false )
1446 if ( theCreationCmd->GetMethod() == "CreateMesh" && theGen->IsNotPublished( GetGeom() ))
1447 myGeomNotInStudy = true;
1449 // convert my creation command --> smeshpy.Mesh(...)
1450 Handle(_pyCommand) creationCmd = GetCreationCmd();
1451 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
1452 creationCmd->SetMethod( "Mesh" );
1453 theGen->SetAccessorMethod( GetID(), _pyMesh::AccessorMethod() );
1456 //================================================================================
1458 * \brief Mesh created by SMESH_MeshEditor
1460 //================================================================================
1462 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd, const _pyID& meshId):
1463 _pyObject(theCreationCmd,meshId), myGeomNotInStudy(false )
1465 if ( theCreationCmd->MethodStartsFrom( "CreateMeshesFrom" ))
1467 // this mesh depends on the exported mesh
1468 const TCollection_AsciiString& file = theCreationCmd->GetArg( 1 );
1469 if ( !file.IsEmpty() )
1471 ExportedMeshData& exportData = theGen->FindExportedMesh( file );
1472 addFatherMesh( exportData.myMesh );
1473 if ( !exportData.myLastComputeCmd.IsNull() )
1475 // restore cleared Compute() by which the exported mesh was generated
1476 exportData.myLastComputeCmd->GetString() = exportData.myLastComputeCmdString;
1477 // protect that Compute() cmd from clearing
1478 if ( exportData.myMesh->myLastComputeCmd == exportData.myLastComputeCmd )
1479 exportData.myMesh->myLastComputeCmd.Nullify();
1483 else if ( theCreationCmd->MethodStartsFrom( "Concatenate" ))
1485 // this mesh depends on concatenated meshes
1486 const TCollection_AsciiString& meshIDs = theCreationCmd->GetArg( 1 );
1487 list< _pyID > idList = theCreationCmd->GetStudyEntries( meshIDs );
1488 list< _pyID >::iterator meshID = idList.begin();
1489 for ( ; meshID != idList.end(); ++meshID )
1490 addFatherMesh( *meshID );
1492 else if ( theCreationCmd->GetMethod() == "CopyMesh" )
1494 // this mesh depends on a copied IdSource
1495 const _pyID& objID = theCreationCmd->GetArg( 1 );
1496 addFatherMesh( objID );
1498 else if ( theCreationCmd->GetMethod().Search("MakeMesh") != -1 ||
1499 theCreationCmd->GetMethod() == "MakeBoundaryMesh" ||
1500 theCreationCmd->GetMethod() == "MakeBoundaryElements" )
1502 // this mesh depends on a source mesh
1503 // (theCreationCmd is already Process()ed by _pyMeshEditor)
1504 const _pyID& meshID = theCreationCmd->GetObject();
1505 addFatherMesh( meshID );
1508 // convert my creation command
1509 Handle(_pyCommand) creationCmd = GetCreationCmd();
1510 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
1511 theGen->SetAccessorMethod( meshId, _pyMesh::AccessorMethod() );
1514 //================================================================================
1516 * \brief Convert an IDL API command of SMESH::SMESH_Mesh to a method call of python Mesh
1517 * \param theCommand - Engine method called for this mesh
1519 //================================================================================
1521 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
1523 // some methods of SMESH_Mesh interface needs special conversion
1524 // to methods of Mesh python class
1526 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
1527 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
1528 // 2. AddHypothesis(geom, hyp)
1529 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
1530 // 3. CreateGroupFromGEOM(type, name, grp)
1531 // --> in Mesh.Group(grp, name="")
1532 // 4. ExportToMED(f, auto_groups, version)
1533 // --> in Mesh.ExportMED( f, auto_groups, version )
1536 const TCollection_AsciiString& method = theCommand->GetMethod();
1537 // ----------------------------------------------------------------------
1538 if ( method == "Compute" ) // in snapshot mode, clear the previous Compute()
1540 if ( !theGen->IsToKeepAllCommands() ) // !historical
1542 list< Handle(_pyHypothesis) >::iterator hyp;
1543 if ( !myLastComputeCmd.IsNull() )
1545 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1546 (*hyp)->ComputeDiscarded( myLastComputeCmd );
1548 myLastComputeCmd->Clear();
1550 myLastComputeCmd = theCommand;
1552 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1553 (*hyp)->MeshComputed( myLastComputeCmd );
1557 // ----------------------------------------------------------------------
1558 else if ( method == "Clear" ) // in snapshot mode, clear all previous commands
1560 if ( !theGen->IsToKeepAllCommands() ) // !historical
1563 myChildMeshes.empty() ? 0 : myChildMeshes.back()->GetCreationCmd()->GetOrderNb();
1564 // list< Handle(_pyCommand) >::reverse_iterator cmd = myProcessedCmds.rbegin();
1565 // for ( ; cmd != myProcessedCmds.rend() && (*cmd)->GetOrderNb() > untilCmdNb; ++cmd )
1567 if ( !myLastComputeCmd.IsNull() )
1569 list< Handle(_pyHypothesis) >::iterator hyp;
1570 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1571 (*hyp)->ComputeDiscarded( myLastComputeCmd );
1573 myLastComputeCmd->Clear();
1576 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
1577 for ( ; e != myEditors.end(); ++e )
1579 list< Handle(_pyCommand)>& cmds = (*e)->GetProcessedCmds();
1580 list< Handle(_pyCommand) >::reverse_iterator cmd = cmds.rbegin();
1581 for ( ; cmd != cmds.rend() && (*cmd)->GetOrderNb() > untilCmdNb; ++cmd )
1582 if ( !(*cmd)->IsEmpty() )
1584 if ( (*cmd)->GetStudyEntries( (*cmd)->GetResultValue() ).empty() ) // no object created
1588 myLastComputeCmd = theCommand; // to clear Clear() the same way as Compute()
1591 // ----------------------------------------------------------------------
1592 else if ( method == "GetSubMesh" ) { // collect submeshes of the mesh
1593 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetResultValue() );
1594 if ( !subMesh.IsNull() ) {
1595 subMesh->SetCreator( this );
1596 mySubmeshes.push_back( subMesh );
1599 // ----------------------------------------------------------------------
1600 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
1601 myAddHypCmds.push_back( theCommand );
1603 const _pyID& hypID = theCommand->GetArg( 2 );
1604 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1605 if ( !hyp.IsNull() ) {
1606 myHypos.push_back( hyp );
1607 if ( hyp->GetMesh().IsEmpty() )
1608 hyp->SetMesh( this->GetID() );
1611 // ----------------------------------------------------------------------
1612 else if ( method == "CreateGroup" ||
1613 method == "CreateGroupFromGEOM" ||
1614 method == "CreateGroupFromFilter" )
1616 Handle(_pyGroup) group = new _pyGroup( theCommand );
1617 myGroups.push_back( group );
1618 theGen->AddObject( group );
1620 // update list of groups
1621 else if ( method == "GetGroups" )
1623 TCollection_AsciiString grIDs = theCommand->GetResultValue();
1624 list< _pyID > idList = theCommand->GetStudyEntries( grIDs );
1625 list< _pyID >::iterator grID = idList.begin();
1626 for ( ; grID != idList.end(); ++grID )
1628 Handle(_pyObject) obj = theGen->FindObject( *grID );
1631 Handle(_pyGroup) group = new _pyGroup( theCommand, *grID );
1632 theGen->AddObject( group );
1633 myGroups.push_back( group );
1637 // notify a group about full removal
1638 else if ( method == "RemoveGroupWithContents" )
1640 if ( !theGen->IsToKeepAllCommands() ) { // snapshot mode
1641 const _pyID groupID = theCommand->GetArg( 1 );
1642 Handle(_pyGroup) grp = Handle(_pyGroup)::DownCast( theGen->FindObject( groupID ));
1643 if ( !grp.IsNull() )
1644 grp->RemovedWithContents();
1647 // ----------------------------------------------------------------------
1648 else if ( theCommand->MethodStartsFrom( "Export" ))
1650 if ( method == "ExportToMED" || // ExportToMED() --> ExportMED()
1651 method == "ExportToMEDX" ) { // ExportToMEDX() --> ExportMED()
1652 theCommand->SetMethod( "ExportMED" );
1654 else if ( method == "ExportCGNS" )
1655 { // ExportCGNS(part, ...) -> ExportCGNS(..., part)
1656 _pyID partID = theCommand->GetArg( 1 );
1657 int nbArgs = theCommand->GetNbArgs();
1658 for ( int i = 2; i <= nbArgs; ++i )
1659 theCommand->SetArg( i-1, theCommand->GetArg( i ));
1660 theCommand->SetArg( nbArgs, partID );
1662 else if ( method == "ExportGMF" )
1663 { // ExportGMF(part,file,bool) -> ExportCGNS(file, part)
1664 _pyID partID = theCommand->GetArg( 1 );
1665 _AString file = theCommand->GetArg( 2 );
1666 theCommand->RemoveArgs();
1667 theCommand->SetArg( 1, file );
1668 theCommand->SetArg( 2, partID );
1670 else if ( theCommand->MethodStartsFrom( "ExportPartTo" ))
1671 { // ExportPartTo*(part, ...) -> Export*(..., part)
1673 // remove "PartTo" from the method
1674 TCollection_AsciiString newMethod = method;
1675 newMethod.Remove( 7, 6 );
1676 theCommand->SetMethod( newMethod );
1677 // make the 1st arg be the last one
1678 _pyID partID = theCommand->GetArg( 1 );
1679 int nbArgs = theCommand->GetNbArgs();
1680 for ( int i = 2; i <= nbArgs; ++i )
1681 theCommand->SetArg( i-1, theCommand->GetArg( i ));
1682 theCommand->SetArg( nbArgs, partID );
1684 // remember file name
1685 theGen->AddExportedMesh( theCommand->GetArg( 1 ),
1686 ExportedMeshData( this, myLastComputeCmd ));
1688 // ----------------------------------------------------------------------
1689 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
1691 _pyID hypID = theCommand->GetArg( 2 );
1692 _pyID geomID = theCommand->GetArg( 1 );
1693 bool isLocal = ( geomID != GetGeom() );
1695 // check if this mesh still has corresponding addition command
1696 Handle(_pyCommand) addCmd;
1697 list< Handle(_pyCommand) >::iterator cmd;
1698 list< Handle(_pyCommand) >* addCmds[2] = { &myAddHypCmds, &myNotConvertedAddHypCmds };
1699 for ( int i = 0; i < 2; ++i )
1701 list< Handle(_pyCommand )> & addHypCmds = *(addCmds[i]);
1702 for ( cmd = addHypCmds.begin(); cmd != addHypCmds.end(); )
1704 bool sameHyp = true;
1705 if ( hypID != (*cmd)->GetArg( 1 ) && hypID != (*cmd)->GetArg( 2 ))
1706 sameHyp = false; // other hyp
1707 if ( (*cmd)->GetNbArgs() == 2 &&
1708 geomID != (*cmd)->GetArg( 1 ) && geomID != (*cmd)->GetArg( 2 ))
1709 sameHyp = false; // other geom
1710 if ( (*cmd)->GetNbArgs() == 1 && isLocal )
1711 sameHyp = false; // other geom
1715 cmd = addHypCmds.erase( cmd );
1716 if ( !theGen->IsToKeepAllCommands() ) {
1718 theCommand->Clear();
1727 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1728 if ( !theCommand->IsEmpty() && !hypID.IsEmpty() ) {
1729 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
1730 _pyID geom = theCommand->GetArg( 1 );
1731 theCommand->RemoveArgs();
1732 theCommand->SetArg( 1, hypID );
1733 if ( geom != GetGeom() )
1734 theCommand->SetArg( 2, geom );
1736 // remove hyp from myHypos
1737 myHypos.remove( hyp );
1739 // check for SubMesh order commands
1740 else if ( method == "GetMeshOrder" || method == "SetMeshOrder" )
1742 // make commands GetSubMesh() returning sub-meshes be before using sub-meshes
1743 // by GetMeshOrder() and SetMeshOrder(), since by defalut GetSubMesh()
1744 // commands are moved at the end of the script
1745 TCollection_AsciiString subIDs =
1746 ( method == "SetMeshOrder" ) ? theCommand->GetArg(1) : theCommand->GetResultValue();
1747 list< _pyID > idList = theCommand->GetStudyEntries( subIDs );
1748 list< _pyID >::iterator subID = idList.begin();
1749 for ( ; subID != idList.end(); ++subID )
1751 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( *subID );
1752 if ( !subMesh.IsNull() )
1753 subMesh->Process( theCommand ); // it moves GetSubMesh() before theCommand
1756 // add accessor method if necessary
1759 if ( NeedMeshAccess( theCommand ))
1760 // apply theCommand to the mesh wrapped by smeshpy mesh
1761 AddMeshAccess( theCommand );
1765 //================================================================================
1767 * \brief Return True if addition of accesor method is needed
1769 //================================================================================
1771 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
1773 // names of SMESH_Mesh methods fully equal to methods of python class Mesh,
1774 // so no conversion is needed for them at all:
1775 static TStringSet sameMethods;
1776 if ( sameMethods.empty() ) {
1777 const char * names[] =
1778 { "ExportDAT","ExportUNV","ExportSTL","ExportSAUV", "RemoveGroup","RemoveGroupWithContents",
1779 "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
1780 "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
1781 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
1782 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
1783 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
1784 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
1785 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
1786 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
1787 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
1788 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
1789 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
1790 "Clear", "ConvertToStandalone", "GetMeshOrder", "SetMeshOrder"
1791 ,"" }; // <- mark of end
1792 sameMethods.Insert( names );
1795 return !sameMethods.Contains( theCommand->GetMethod() );
1798 //================================================================================
1800 * \brief Convert creation and addition of all algos and hypos
1802 //================================================================================
1804 void _pyMesh::Flush()
1807 // get the meshes this mesh depends on via hypotheses
1808 list< Handle(_pyMesh) > fatherMeshes;
1809 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1810 for ( ; hyp != myHypos.end(); ++hyp )
1811 if ( ! (*hyp)->GetReferredMeshesAndGeom( fatherMeshes ))
1812 myGeomNotInStudy = true;
1814 list< Handle(_pyMesh) >::iterator m = fatherMeshes.begin();
1815 for ( ; m != fatherMeshes.end(); ++m )
1816 addFatherMesh( *m );
1817 // if ( removedGeom )
1818 // SetRemovedFromStudy(); // as reffered geometry not in study
1820 if ( myGeomNotInStudy )
1823 list < Handle(_pyCommand) >::iterator cmd;
1825 // try to convert algo addition like this:
1826 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
1827 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1829 Handle(_pyCommand) addCmd = *cmd;
1831 _pyID algoID = addCmd->GetArg( 2 );
1832 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
1833 if ( algo.IsNull() || !algo->IsAlgo() )
1836 // check and create new algorithm instance if it is already wrapped
1837 if ( algo->IsWrapped() ) {
1838 _pyID localAlgoID = theGen->GenerateNewID( algoID );
1839 TCollection_AsciiString aNewCmdStr = addCmd->GetIndentation() + localAlgoID +
1840 TCollection_AsciiString( " = " ) + theGen->GetID() +
1841 TCollection_AsciiString( ".CreateHypothesis( \"" ) + algo->GetAlgoType() +
1842 TCollection_AsciiString( "\" )" );
1844 Handle(_pyCommand) newCmd = theGen->AddCommand( aNewCmdStr );
1845 Handle(_pyAlgorithm) newAlgo = Handle(_pyAlgorithm)::DownCast(theGen->FindHyp( localAlgoID ));
1846 if ( !newAlgo.IsNull() ) {
1847 newAlgo->Assign( algo, this->GetID() );
1848 newAlgo->SetCreationCmd( newCmd );
1850 // set algorithm creation
1851 theGen->SetCommandBefore( newCmd, addCmd );
1852 myHypos.push_back( newAlgo );
1853 if ( !myLastComputeCmd.IsNull() &&
1854 newCmd->GetOrderNb() == myLastComputeCmd->GetOrderNb() + 1)
1855 newAlgo->MeshComputed( myLastComputeCmd );
1860 _pyID geom = addCmd->GetArg( 1 );
1861 bool isLocalAlgo = ( geom != GetGeom() );
1864 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
1866 // wrapped algo is created after mesh creation
1867 GetCreationCmd()->AddDependantCmd( addCmd );
1869 if ( isLocalAlgo ) {
1870 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
1871 addCmd->SetArg( addCmd->GetNbArgs() + 1,
1872 TCollection_AsciiString( "geom=" ) + geom );
1873 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
1874 list < Handle(_pySubMesh) >::iterator smIt;
1875 for ( smIt = mySubmeshes.begin(); smIt != mySubmeshes.end(); ++smIt ) {
1876 Handle(_pySubMesh) subMesh = *smIt;
1877 Handle(_pyCommand) subCmd = subMesh->GetCreationCmd();
1878 if ( geom == subCmd->GetArg( 1 )) {
1879 subCmd->SetObject( algo->GetID() );
1880 subCmd->RemoveArgs();
1881 subMesh->SetCreator( algo );
1886 else // KO - ALGO was already created
1888 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
1889 addCmd->RemoveArgs();
1890 addCmd->SetArg( 1, algoID );
1892 addCmd->SetArg( 2, geom );
1893 myNotConvertedAddHypCmds.push_back( addCmd );
1897 // try to convert hypo addition like this:
1898 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
1899 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1901 Handle(_pyCommand) addCmd = *cmd;
1902 _pyID hypID = addCmd->GetArg( 2 );
1903 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1904 if ( hyp.IsNull() || hyp->IsAlgo() )
1906 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
1908 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
1909 _pyID geom = addCmd->GetArg( 1 );
1910 addCmd->RemoveArgs();
1911 addCmd->SetArg( 1, hypID );
1912 if ( geom != GetGeom() )
1913 addCmd->SetArg( 2, geom );
1914 myNotConvertedAddHypCmds.push_back( addCmd );
1918 myAddHypCmds.clear();
1919 mySubmeshes.clear();
1922 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1923 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1927 //================================================================================
1929 * \brief Sets myIsPublished of me and of all objects depending on me.
1931 //================================================================================
1933 void _pyMesh::SetRemovedFromStudy(const bool isRemoved)
1935 _pyObject::SetRemovedFromStudy(isRemoved);
1937 list< Handle(_pySubMesh) >::iterator sm = mySubmeshes.begin();
1938 for ( ; sm != mySubmeshes.end(); ++sm )
1939 (*sm)->SetRemovedFromStudy(isRemoved);
1941 list< Handle(_pyGroup) >::iterator gr = myGroups.begin();
1942 for ( ; gr != myGroups.end(); ++gr )
1943 (*gr)->SetRemovedFromStudy(isRemoved);
1945 list< Handle(_pyMesh) >::iterator m = myChildMeshes.begin();
1946 for ( ; m != myChildMeshes.end(); ++m )
1947 (*m)->SetRemovedFromStudy(isRemoved);
1949 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
1950 for ( ; e != myEditors.end(); ++e )
1951 (*e)->SetRemovedFromStudy(isRemoved);
1954 //================================================================================
1956 * \brief Return true if none of myChildMeshes is in study
1958 //================================================================================
1960 bool _pyMesh::CanClear()
1965 list< Handle(_pyMesh) >::iterator m = myChildMeshes.begin();
1966 for ( ; m != myChildMeshes.end(); ++m )
1967 if ( !(*m)->CanClear() )
1973 //================================================================================
1975 * \brief Clear my commands and commands of mesh editor
1977 //================================================================================
1979 void _pyMesh::ClearCommands()
1985 // mark all sub-objects as not removed, except child meshes
1986 list< Handle(_pyMesh) > children;
1987 children.swap( myChildMeshes );
1988 SetRemovedFromStudy( false );
1989 children.swap( myChildMeshes );
1993 _pyObject::ClearCommands();
1995 list< Handle(_pySubMesh) >::iterator sm = mySubmeshes.begin();
1996 for ( ; sm != mySubmeshes.end(); ++sm )
1997 (*sm)->ClearCommands();
1999 list< Handle(_pyGroup) >::iterator gr = myGroups.begin();
2000 for ( ; gr != myGroups.end(); ++gr )
2001 (*gr)->ClearCommands();
2003 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
2004 for ( ; e != myEditors.end(); ++e )
2005 (*e)->ClearCommands();
2008 //================================================================================
2010 * \brief Add a father mesh by ID
2012 //================================================================================
2014 void _pyMesh::addFatherMesh( const _pyID& meshID )
2016 if ( !meshID.IsEmpty() )
2017 addFatherMesh( Handle(_pyMesh)::DownCast( theGen->FindObject( meshID )));
2020 //================================================================================
2022 * \brief Add a father mesh
2024 //================================================================================
2026 void _pyMesh::addFatherMesh( const Handle(_pyMesh)& mesh )
2028 if ( !mesh.IsNull() )
2030 //myFatherMeshes.push_back( mesh );
2031 mesh->myChildMeshes.push_back( this );
2033 // protect last Compute() from clearing by the next Compute()
2034 mesh->myLastComputeCmd.Nullify();
2038 //================================================================================
2040 * \brief MeshEditor convert its commands to ones of mesh
2042 //================================================================================
2044 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
2045 _pyObject( theCreationCmd )
2047 myMesh = theCreationCmd->GetObject();
2048 myCreationCmdStr = theCreationCmd->GetString();
2049 theCreationCmd->Clear();
2051 Handle(_pyMesh) mesh = ObjectToMesh( theGen->FindObject( myMesh ));
2052 if ( !mesh.IsNull() )
2053 mesh->AddEditor( this );
2056 //================================================================================
2058 * \brief convert its commands to ones of mesh
2060 //================================================================================
2062 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
2064 // names of SMESH_MeshEditor methods fully equal to methods of the python class Mesh, so
2065 // commands calling this methods are converted to calls of Mesh methods
2066 static TStringSet sameMethods;
2067 if ( sameMethods.empty() ) {
2068 const char * names[] = {
2069 "RemoveElements","RemoveNodes","RemoveOrphanNodes","AddNode","Add0DElement","AddEdge","AddFace","AddPolygonalFace","AddBall",
2070 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode", "MoveClosestNodeToPoint",
2071 "InverseDiag","DeleteDiag","Reorient","ReorientObject",
2072 "TriToQuad","TriToQuadObject", "SplitQuad","SplitQuadObject",
2073 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
2074 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
2075 "RotationSweep","RotationSweepObject","RotationSweepObject1D","RotationSweepObject2D",
2076 "ExtrusionSweep","AdvancedExtrusion","ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D",
2077 "ExtrusionAlongPath","ExtrusionAlongPathObject","ExtrusionAlongPathX",
2078 "ExtrusionAlongPathObject1D","ExtrusionAlongPathObject2D",
2079 "Mirror","MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
2080 "FindCoincidentNodes",/*"FindCoincidentNodesOnPart",*/"MergeNodes","FindEqualElements",
2081 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
2082 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
2083 "GetLastCreatedElems",
2084 "MirrorMakeMesh","MirrorObjectMakeMesh","TranslateMakeMesh",
2085 "TranslateObjectMakeMesh","RotateMakeMesh","RotateObjectMakeMesh","MakeBoundaryMesh",
2086 "MakeBoundaryElements", "SplitVolumesIntoTetra"
2087 ,"" }; // <- mark of the end
2088 sameMethods.Insert( names );
2091 // names of SMESH_MeshEditor commands in which only a method name must be replaced
2092 TStringMap diffMethods;
2093 if ( diffMethods.empty() ) {
2094 const char * orig2newName[] = {
2095 // original name --------------> new name
2096 "ExtrusionAlongPathObjX" , "ExtrusionAlongPathX",
2097 "FindCoincidentNodesOnPartBut", "FindCoincidentNodesOnPart",
2098 "ConvertToQuadraticObject" , "ConvertToQuadratic",
2099 "ConvertFromQuadraticObject" , "ConvertFromQuadratic",
2100 "Create0DElementsOnAllNodes" , "Add0DElementsToAllNodes",
2101 ""};// <- mark of the end
2102 diffMethods.Insert( orig2newName );
2105 // names of SMESH_MeshEditor methods which differ from methods of Mesh class
2106 // only by last two arguments
2107 static TStringSet diffLastTwoArgsMethods;
2108 if (diffLastTwoArgsMethods.empty() ) {
2109 const char * names[] = {
2110 "MirrorMakeGroups","MirrorObjectMakeGroups",
2111 "TranslateMakeGroups","TranslateObjectMakeGroups",
2112 "RotateMakeGroups","RotateObjectMakeGroups",
2113 ""};// <- mark of the end
2114 diffLastTwoArgsMethods.Insert( names );
2117 // only a method name is to change?
2118 const TCollection_AsciiString & method = theCommand->GetMethod();
2119 bool isPyMeshMethod = sameMethods.Contains( method );
2120 if ( !isPyMeshMethod )
2122 TCollection_AsciiString newMethod = diffMethods.Value( method );
2123 if (( isPyMeshMethod = ( newMethod.Length() > 0 )))
2124 theCommand->SetMethod( newMethod );
2126 // ConvertToBiQuadratic(...) -> ConvertToQuadratic(...,True)
2127 if ( !isPyMeshMethod && (method == "ConvertToBiQuadratic" || method == "ConvertToBiQuadraticObject") )
2129 isPyMeshMethod = true;
2130 theCommand->SetMethod( method.SubString( 1, 9) + method.SubString( 12, method.Length()));
2131 theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
2134 if ( !isPyMeshMethod )
2136 // Replace SMESH_MeshEditor "*MakeGroups" functions by the Mesh
2137 // functions with the flag "theMakeGroups = True" like:
2138 // SMESH_MeshEditor.CmdMakeGroups => Mesh.Cmd(...,True)
2139 int pos = method.Search("MakeGroups");
2142 isPyMeshMethod = true;
2143 bool is0DmethId = ( method == "ExtrusionSweepMakeGroups0D" );
2144 bool is0DmethObj = ( method == "ExtrusionSweepObject0DMakeGroups");
2146 // 1. Remove "MakeGroups" from the Command
2147 TCollection_AsciiString aMethod = theCommand->GetMethod();
2148 int nbArgsToAdd = diffLastTwoArgsMethods.Contains(aMethod) ? 2 : 1;
2151 pos = pos-2; //Remove "0D" from the Command too
2152 aMethod.Trunc(pos-1);
2153 theCommand->SetMethod(aMethod);
2155 // 2. And add last "True" argument(s)
2156 while(nbArgsToAdd--)
2157 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
2158 if( is0DmethId || is0DmethObj )
2159 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
2163 // ExtrusionSweep0D() -> ExtrusionSweep()
2164 // ExtrusionSweepObject0D() -> ExtrusionSweepObject()
2165 if ( !isPyMeshMethod && ( method == "ExtrusionSweep0D" ||
2166 method == "ExtrusionSweepObject0D" ))
2168 isPyMeshMethod = true;
2169 theCommand->SetMethod( method.SubString( 1, method.Length()-2));
2170 theCommand->SetArg(theCommand->GetNbArgs()+1,"False"); //sets flag "MakeGroups = False"
2171 theCommand->SetArg(theCommand->GetNbArgs()+1,"True"); //sets flag "IsNode = True"
2174 // DoubleNode...New(...) -> DoubleNode...(...,True)
2175 if ( !isPyMeshMethod && ( method == "DoubleNodeElemGroupNew" ||
2176 method == "DoubleNodeElemGroupsNew" ||
2177 method == "DoubleNodeGroupNew" ||
2178 method == "DoubleNodeGroupsNew" ||
2179 method == "DoubleNodeElemGroup2New" ||
2180 method == "DoubleNodeElemGroups2New"))
2182 isPyMeshMethod = true;
2183 const int excessLen = 3 + int( method.Value( method.Length()-3 ) == '2' );
2184 theCommand->SetMethod( method.SubString( 1, method.Length()-excessLen));
2185 if ( excessLen == 3 )
2187 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
2189 else if ( theCommand->GetArg(4) == "0" ||
2190 theCommand->GetArg(5) == "0" )
2192 // [ nothing, Group ] = DoubleNodeGroup2New(,,,False, True) ->
2193 // Group = DoubleNodeGroup2New(,,,False, True)
2194 _pyID groupID = theCommand->GetResultValue( 1 + int( theCommand->GetArg(4) == "0"));
2195 theCommand->SetResultValue( groupID );
2198 // FindAmongElementsByPoint(meshPart, x, y, z, elementType) ->
2199 // FindElementsByPoint(x, y, z, elementType, meshPart)
2200 if ( !isPyMeshMethod && method == "FindAmongElementsByPoint" )
2202 isPyMeshMethod = true;
2203 theCommand->SetMethod( "FindElementsByPoint" );
2204 // make the 1st arg be the last one
2205 _pyID partID = theCommand->GetArg( 1 );
2206 int nbArgs = theCommand->GetNbArgs();
2207 for ( int i = 2; i <= nbArgs; ++i )
2208 theCommand->SetArg( i-1, theCommand->GetArg( i ));
2209 theCommand->SetArg( nbArgs, partID );
2211 // Reorient2D( mesh, dir, face, point ) -> Reorient2D( mesh, dir, faceORpoint )
2212 if ( !isPyMeshMethod && method == "Reorient2D" )
2214 isPyMeshMethod = true;
2215 _AString mesh = theCommand->GetArg( 1 );
2216 _AString dir = theCommand->GetArg( 2 );
2217 _AString face = theCommand->GetArg( 3 );
2218 _AString point = theCommand->GetArg( 4 );
2219 theCommand->RemoveArgs();
2220 theCommand->SetArg( 1, mesh );
2221 theCommand->SetArg( 2, dir );
2222 if ( face.Value(1) == '-' || face.Value(1) == '0' ) // invalid: face <= 0
2223 theCommand->SetArg( 3, point );
2225 theCommand->SetArg( 3, face );
2228 if ( method == "QuadToTri" || method == "QuadToTriObject" )
2230 isPyMeshMethod = true;
2231 int crit_arg = theCommand->GetNbArgs();
2232 const _AString& crit = theCommand->GetArg(crit_arg);
2233 if (crit.Search("MaxElementLength2D") != -1)
2234 theCommand->SetArg(crit_arg, "");
2237 if ( isPyMeshMethod )
2239 theCommand->SetObject( myMesh );
2243 // editor creation command is needed only if any editor function is called
2244 theGen->AddMeshAccessorMethod( theCommand ); // for *Object() methods
2245 if ( !myCreationCmdStr.IsEmpty() ) {
2246 GetCreationCmd()->GetString() = myCreationCmdStr;
2247 myCreationCmdStr.Clear();
2252 //================================================================================
2254 * \brief Return true if my mesh can be removed
2256 //================================================================================
2258 bool _pyMeshEditor::CanClear()
2260 Handle(_pyMesh) mesh = ObjectToMesh( theGen->FindObject( myMesh ));
2261 return mesh.IsNull() ? true : mesh->CanClear();
2264 //================================================================================
2266 * \brief _pyHypothesis constructor
2267 * \param theCreationCmd -
2269 //================================================================================
2271 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
2272 _pyObject( theCreationCmd ), myCurCrMethod(0)
2274 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
2277 //================================================================================
2279 * \brief Creates algorithm or hypothesis
2280 * \param theCreationCmd - The engine command creating a hypothesis
2281 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
2283 //================================================================================
2285 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
2287 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
2288 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
2290 Handle(_pyHypothesis) hyp, algo;
2293 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
2294 if ( hypTypeQuoted.IsEmpty() )
2297 TCollection_AsciiString hypType =
2298 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
2300 algo = new _pyAlgorithm( theCreationCmd );
2301 hyp = new _pyHypothesis( theCreationCmd );
2303 if ( hypType == "NumberOfSegments" ) {
2304 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
2305 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
2306 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
2307 hyp->AddArgMethod( "SetNumberOfSegments" );
2308 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
2309 hyp->AddArgMethod( "SetScaleFactor" );
2310 hyp->AddArgMethod( "SetReversedEdges" );
2311 // same for ""CompositeSegment_1D:
2312 hyp->SetConvMethodAndType( "NumberOfSegments", "CompositeSegment_1D");
2313 hyp->AddArgMethod( "SetNumberOfSegments" );
2314 hyp->AddArgMethod( "SetScaleFactor" );
2315 hyp->AddArgMethod( "SetReversedEdges" );
2317 else if ( hypType == "SegmentLengthAroundVertex" ) {
2318 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
2319 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
2320 hyp->AddArgMethod( "SetLength" );
2321 // same for ""CompositeSegment_1D:
2322 hyp->SetConvMethodAndType( "LengthNearVertex", "CompositeSegment_1D");
2323 hyp->AddArgMethod( "SetLength" );
2325 else if ( hypType == "LayerDistribution2D" ) {
2326 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get2DHypothesis" );
2327 hyp->SetConvMethodAndType( "LayerDistribution", "RadialQuadrangle_1D2D");
2329 else if ( hypType == "LayerDistribution" ) {
2330 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get3DHypothesis" );
2331 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
2333 else if ( hypType == "CartesianParameters3D" ) {
2334 hyp = new _pyComplexParamHypo( theCreationCmd );
2335 hyp->SetConvMethodAndType( "SetGrid", "Cartesian_3D");
2336 for ( int iArg = 0; iArg < 4; ++iArg )
2337 hyp->setCreationArg( iArg+1, "[]");
2341 hyp = theGen->GetHypothesisReader()->GetHypothesis( hypType, theCreationCmd );
2344 return algo->IsValid() ? algo : hyp;
2347 //================================================================================
2349 * \brief Returns true if addition of this hypothesis to a given mesh can be
2350 * wrapped into hypothesis creation
2352 //================================================================================
2354 bool _pyHypothesis::IsWrappable(const _pyID& theMesh) const
2356 if ( !myIsWrapped && myMesh == theMesh && IsInStudy() )
2358 Handle(_pyObject) pyMesh = theGen->FindObject( myMesh );
2359 if ( !pyMesh.IsNull() && pyMesh->IsInStudy() )
2365 //================================================================================
2367 * \brief Convert the command adding a hypothesis to mesh into a smesh command
2368 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
2369 * \param theAlgo - The algo that can create this hypo
2370 * \retval bool - false if the command cant be converted
2372 //================================================================================
2374 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
2375 const _pyID& theMesh)
2377 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
2379 if ( !IsWrappable( theMesh ))
2382 myGeom = theCmd->GetArg( 1 );
2384 Handle(_pyHypothesis) algo;
2386 // find algo created on myGeom in theMesh
2387 algo = theGen->FindAlgo( myGeom, theMesh, this );
2388 if ( algo.IsNull() )
2390 // attach hypothesis creation command to be after algo creation command
2391 // because it can be new created instance of algorithm
2392 algo->GetCreationCmd()->AddDependantCmd( theCmd );
2396 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
2397 theCmd->SetResultValue( GetID() );
2398 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
2399 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
2400 // set args (geom will be set by _pyMesh calling this method)
2401 theCmd->RemoveArgs();
2402 for ( size_t i = 0; i < myCurCrMethod->myArgs.size(); ++i ) {
2403 if ( !myCurCrMethod->myArgs[ i ].IsEmpty() )
2404 theCmd->SetArg( i+1, myCurCrMethod->myArgs[ i ]);
2406 theCmd->SetArg( i+1, "[]");
2408 // set a new creation command
2409 GetCreationCmd()->Clear();
2410 // replace creation command by wrapped instance
2411 // please note, that hypothesis attaches to algo creation command (see upper)
2412 SetCreationCmd( theCmd );
2415 // clear commands setting arg values
2416 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
2417 for ( ; argCmd != myArgCommands.end(); ++argCmd )
2420 // set unknown arg commands after hypo creation
2421 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
2422 list<Handle(_pyCommand)>::iterator cmd = myUnusedCommands.begin();
2423 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2424 afterCmd->AddDependantCmd( *cmd );
2430 //================================================================================
2432 * \brief Remember hypothesis parameter values
2433 * \param theCommand - The called hypothesis method
2435 //================================================================================
2437 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
2439 ASSERT( !myIsAlgo );
2440 if ( !theGen->IsToKeepAllCommands() )
2441 rememberCmdOfParameter( theCommand );
2443 bool usedCommand = false;
2444 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2445 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2447 CreationMethod& crMethod = type2meth->second;
2448 for ( size_t i = 0; i < crMethod.myArgMethods.size(); ++i ) {
2449 if ( crMethod.myArgMethods[ i ] == theCommand->GetMethod() ) {
2451 myArgCommands.push_back( theCommand );
2453 while ( crMethod.myArgs.size() < i+1 )
2454 crMethod.myArgs.push_back( "[]" );
2455 crMethod.myArgs[ i ] = theCommand->GetArg( crMethod.myArgNb[i] );
2460 myUnusedCommands.push_back( theCommand );
2463 //================================================================================
2465 * \brief Finish conversion
2467 //================================================================================
2469 void _pyHypothesis::Flush()
2473 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
2474 for ( ; cmd != myArgCommands.end(); ++cmd ) {
2475 // Add access to a wrapped mesh
2476 theGen->AddMeshAccessorMethod( *cmd );
2477 // Add access to a wrapped algorithm
2478 theGen->AddAlgoAccessorMethod( *cmd );
2480 cmd = myUnusedCommands.begin();
2481 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2482 // Add access to a wrapped mesh
2483 theGen->AddMeshAccessorMethod( *cmd );
2484 // Add access to a wrapped algorithm
2485 theGen->AddAlgoAccessorMethod( *cmd );
2488 // forget previous hypothesis modifications
2489 myArgCommands.clear();
2490 myUnusedCommands.clear();
2493 //================================================================================
2495 * \brief clear creation, arg and unkown commands
2497 //================================================================================
2499 void _pyHypothesis::ClearAllCommands()
2501 GetCreationCmd()->Clear();
2502 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
2503 for ( ; cmd != myArgCommands.end(); ++cmd )
2505 cmd = myUnusedCommands.begin();
2506 for ( ; cmd != myUnusedCommands.end(); ++cmd )
2511 //================================================================================
2513 * \brief Assign fields of theOther to me except myIsWrapped
2515 //================================================================================
2517 void _pyHypothesis::Assign( const Handle(_pyHypothesis)& theOther,
2518 const _pyID& theMesh )
2520 // myCreationCmd = theOther->myCreationCmd;
2521 myIsAlgo = theOther->myIsAlgo;
2522 myIsWrapped = false;
2523 myGeom = theOther->myGeom;
2525 myAlgoType2CreationMethod = theOther->myAlgoType2CreationMethod;
2526 myAccumulativeMethods = theOther->myAccumulativeMethods;
2527 //myUnusedCommands = theOther->myUnusedCommands;
2528 // init myCurCrMethod
2529 GetCreationMethod( theOther->GetAlgoType() );
2532 //================================================================================
2534 * \brief Analyze my erasability depending on myReferredObjs
2536 //================================================================================
2538 bool _pyHypothesis::CanClear()
2542 list< Handle(_pyObject) >::iterator obj = myReferredObjs.begin();
2543 for ( ; obj != myReferredObjs.end(); ++obj )
2544 if ( (*obj)->CanClear() )
2551 //================================================================================
2553 * \brief Clear my commands depending on usage by meshes
2555 //================================================================================
2557 void _pyHypothesis::ClearCommands()
2559 // if ( !theGen->IsToKeepAllCommands() )
2561 // bool isUsed = false;
2562 // int lastComputeOrder = 0;
2563 // list<Handle(_pyCommand) >::iterator cmd = myComputeCmds.begin();
2564 // for ( ; cmd != myComputeCmds.end(); ++cmd )
2565 // if ( ! (*cmd)->IsEmpty() )
2568 // if ( (*cmd)->GetOrderNb() > lastComputeOrder )
2569 // lastComputeOrder = (*cmd)->GetOrderNb();
2573 // SetRemovedFromStudy( true );
2577 // // clear my commands invoked after lastComputeOrder
2578 // // map<TCollection_AsciiString, list< Handle(_pyCommand) > >::iterator m2c;
2579 // // for ( m2c = myMeth2Commands.begin(); m2c != myMeth2Commands.end(); ++m2c )
2581 // // list< Handle(_pyCommand)> & cmds = m2c->second;
2582 // // if ( !cmds.empty() && cmds.back()->GetOrderNb() > lastComputeOrder )
2583 // // cmds.back()->Clear();
2587 _pyObject::ClearCommands();
2590 //================================================================================
2592 * \brief Find arguments that are objects like mesh, group, geometry
2593 * \param meshes - referred meshes (directly or indirrectly)
2594 * \retval bool - false if a referred geometry is not in the study
2596 //================================================================================
2598 bool _pyHypothesis::GetReferredMeshesAndGeom( list< Handle(_pyMesh) >& meshes )
2600 if ( IsAlgo() ) return true;
2602 bool geomPublished = true;
2603 vector< _AString > args;
2604 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2605 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2607 CreationMethod& crMethod = type2meth->second;
2608 args.insert( args.end(), crMethod.myArgs.begin(), crMethod.myArgs.end());
2610 list<Handle(_pyCommand)>::iterator cmd = myUnusedCommands.begin();
2611 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2612 for ( int nb = (*cmd)->GetNbArgs(); nb; --nb )
2613 args.push_back( (*cmd)->GetArg( nb ));
2616 for ( size_t i = 0; i < args.size(); ++i )
2618 list< _pyID > idList = _pyCommand::GetStudyEntries( args[ i ]);
2619 if ( idList.empty() && !args[ i ].IsEmpty() )
2620 idList.push_back( args[ i ]);
2621 list< _pyID >::iterator id = idList.begin();
2622 for ( ; id != idList.end(); ++id )
2624 Handle(_pyObject) obj = theGen->FindObject( *id );
2625 if ( obj.IsNull() ) obj = theGen->FindHyp( *id );
2628 if ( theGen->IsGeomObject( *id ) && theGen->IsNotPublished( *id ))
2629 geomPublished = false;
2633 myReferredObjs.push_back( obj );
2634 Handle(_pyMesh) mesh = ObjectToMesh( obj );
2635 if ( !mesh.IsNull() )
2636 meshes.push_back( mesh );
2637 // prevent clearing not published hyps referred e.g. by "LayerDistribution"
2638 else if ( obj->IsKind( STANDARD_TYPE( _pyHypothesis )) && this->IsInStudy() )
2639 obj->SetRemovedFromStudy( false );
2643 return geomPublished;
2646 //================================================================================
2648 * \brief Remember theCommand setting a parameter
2650 //================================================================================
2652 void _pyHypothesis::rememberCmdOfParameter( const Handle(_pyCommand) & theCommand )
2654 // parameters are discriminated by method name
2655 _AString method = theCommand->GetMethod();
2656 if ( myAccumulativeMethods.count( method ))
2657 return; // this method adds values and not override the previus value
2659 // discriminate commands setting different parameters via one method
2660 // by passing parameter names like e.g. SetOption("size", "0.2")
2661 if ( theCommand->GetString().FirstLocationInSet( "'\"", 1, theCommand->Length() ) &&
2662 theCommand->GetNbArgs() > 1 )
2664 // mangle method by appending a 1st textual arg
2665 for ( int iArg = 1; iArg <= theCommand->GetNbArgs(); ++iArg )
2667 const TCollection_AsciiString& arg = theCommand->GetArg( iArg );
2668 if ( arg.Value(1) != '\"' && arg.Value(1) != '\'' ) continue;
2669 if ( !isalpha( arg.Value(2))) continue;
2674 // parameters are discriminated by method name
2675 list< Handle(_pyCommand)>& cmds = myMeth2Commands[ method /*theCommand->GetMethod()*/ ];
2676 if ( !cmds.empty() && !isCmdUsedForCompute( cmds.back() ))
2678 cmds.back()->Clear(); // previous parameter value has not been used
2679 cmds.back() = theCommand;
2683 cmds.push_back( theCommand );
2687 //================================================================================
2689 * \brief Return true if a setting parameter command ha been used to compute mesh
2691 //================================================================================
2693 bool _pyHypothesis::isCmdUsedForCompute( const Handle(_pyCommand) & cmd,
2694 _pyCommand::TAddr avoidComputeAddr ) const
2696 bool isUsed = false;
2697 map< _pyCommand::TAddr, list<Handle(_pyCommand) > >::const_iterator addr2cmds =
2698 myComputeAddr2Cmds.begin();
2699 for ( ; addr2cmds != myComputeAddr2Cmds.end() && !isUsed; ++addr2cmds )
2701 if ( addr2cmds->first == avoidComputeAddr ) continue;
2702 const list<Handle(_pyCommand)> & cmds = addr2cmds->second;
2703 isUsed = ( std::find( cmds.begin(), cmds.end(), cmd ) != cmds.end() );
2708 //================================================================================
2710 * \brief Save commands setting parameters as they are used for a mesh computation
2712 //================================================================================
2714 void _pyHypothesis::MeshComputed( const Handle(_pyCommand)& theComputeCmd )
2716 myComputeCmds.push_back( theComputeCmd );
2717 list<Handle(_pyCommand)>& savedCmds = myComputeAddr2Cmds[ theComputeCmd->GetAddress() ];
2719 map<TCollection_AsciiString, list< Handle(_pyCommand) > >::iterator m2c;
2720 for ( m2c = myMeth2Commands.begin(); m2c != myMeth2Commands.end(); ++m2c )
2721 savedCmds.push_back( m2c->second.back() );
2724 //================================================================================
2726 * \brief Clear commands setting parameters as a mesh computed using them is cleared
2728 //================================================================================
2730 void _pyHypothesis::ComputeDiscarded( const Handle(_pyCommand)& theComputeCmd )
2732 list<Handle(_pyCommand)>& savedCmds = myComputeAddr2Cmds[ theComputeCmd->GetAddress() ];
2734 list<Handle(_pyCommand)>::iterator cmd = savedCmds.begin();
2735 for ( ; cmd != savedCmds.end(); ++cmd )
2737 // check if a cmd has been used to compute another mesh
2738 if ( isCmdUsedForCompute( *cmd, theComputeCmd->GetAddress() ))
2740 // check if a cmd is a sole command setting its parameter;
2741 // don't use method name for search as it can change
2742 map<TCollection_AsciiString, list<Handle(_pyCommand)> >::iterator
2743 m2cmds = myMeth2Commands.begin();
2744 for ( ; m2cmds != myMeth2Commands.end(); ++m2cmds )
2746 list< Handle(_pyCommand)>& cmds = m2cmds->second;
2747 list< Handle(_pyCommand)>::iterator cmdIt = std::find( cmds.begin(), cmds.end(), *cmd );
2748 if ( cmdIt != cmds.end() )
2750 if ( cmds.back() != *cmd )
2752 cmds.erase( cmdIt );
2759 myComputeAddr2Cmds.erase( theComputeCmd->GetAddress() );
2762 //================================================================================
2764 * \brief Sets an argNb-th argument of current creation command
2765 * \param argNb - argument index countered from 1
2767 //================================================================================
2769 void _pyHypothesis::setCreationArg( const int argNb, const _AString& arg )
2771 if ( myCurCrMethod )
2773 while ( myCurCrMethod->myArgs.size() < argNb )
2774 myCurCrMethod->myArgs.push_back( "None" );
2775 if ( arg.IsEmpty() )
2776 myCurCrMethod->myArgs[ argNb-1 ] = "None";
2778 myCurCrMethod->myArgs[ argNb-1 ] = arg;
2783 //================================================================================
2785 * \brief Remember hypothesis parameter values
2786 * \param theCommand - The called hypothesis method
2788 //================================================================================
2790 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
2792 if ( GetAlgoType() == "Cartesian_3D" )
2794 // CartesianParameters3D hyp
2796 if ( theCommand->GetMethod() == "SetSizeThreshold" )
2798 setCreationArg( 4, theCommand->GetArg( 1 ));
2799 myArgCommands.push_back( theCommand );
2802 if ( theCommand->GetMethod() == "SetGrid" ||
2803 theCommand->GetMethod() == "SetGridSpacing" )
2805 TCollection_AsciiString axis = theCommand->GetArg( theCommand->GetNbArgs() );
2806 int iArg = axis.Value(1) - '0';
2807 if ( theCommand->GetMethod() == "SetGrid" )
2809 setCreationArg( 1+iArg, theCommand->GetArg( 1 ));
2813 myCurCrMethod->myArgs[ iArg ] = "[ ";
2814 myCurCrMethod->myArgs[ iArg ] += theCommand->GetArg( 1 );
2815 myCurCrMethod->myArgs[ iArg ] += ", ";
2816 myCurCrMethod->myArgs[ iArg ] += theCommand->GetArg( 2 );
2817 myCurCrMethod->myArgs[ iArg ] += "]";
2819 myArgCommands.push_back( theCommand );
2820 rememberCmdOfParameter( theCommand );
2825 if( theCommand->GetMethod() == "SetLength" )
2827 // NOW it is OBSOLETE
2828 // ex: hyp.SetLength(start, 1)
2829 // hyp.SetLength(end, 0)
2830 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
2831 int i = 1 - theCommand->GetArg( 2 ).IntegerValue();
2832 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2833 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2835 CreationMethod& crMethod = type2meth->second;
2836 while ( crMethod.myArgs.size() < i+1 )
2837 crMethod.myArgs.push_back( "[]" );
2838 crMethod.myArgs[ i ] = theCommand->GetArg( 1 ); // arg value
2840 myArgCommands.push_back( theCommand );
2844 _pyHypothesis::Process( theCommand );
2847 //================================================================================
2849 * \brief Clear SetObjectEntry() as it is called by methods of Mesh_Segment
2851 //================================================================================
2853 void _pyComplexParamHypo::Flush()
2857 list < Handle(_pyCommand) >::iterator cmd = myUnusedCommands.begin();
2858 for ( ; cmd != myUnusedCommands.end(); ++cmd )
2859 if ((*cmd)->GetMethod() == "SetObjectEntry" )
2864 //================================================================================
2866 * \brief Convert methods of 1D hypotheses to my own methods
2867 * \param theCommand - The called hypothesis method
2869 //================================================================================
2871 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
2873 if ( theCommand->GetMethod() != "SetLayerDistribution" )
2876 const _pyID& hyp1dID = theCommand->GetArg( 1 );
2877 // Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
2878 // if ( hyp1d.IsNull() && ! my1dHyp.IsNull()) // apparently hypId changed at study restoration
2880 // TCollection_AsciiString cmd =
2881 // my1dHyp->GetCreationCmd()->GetIndentation() + hyp1dID + " = " + my1dHyp->GetID();
2882 // Handle(_pyCommand) newCmd = theGen->AddCommand( cmd );
2883 // theGen->SetCommandAfter( newCmd, my1dHyp->GetCreationCmd() );
2886 // else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() )
2888 // // 1D hypo is already set, so distribution changes and the old
2889 // // 1D hypo is thrown away
2890 // my1dHyp->ClearAllCommands();
2893 // //my1dHyp->SetRemovedFromStudy( false );
2895 // if ( !myArgCommands.empty() )
2896 // myArgCommands.back()->Clear();
2897 myCurCrMethod->myArgs.push_back( hyp1dID );
2898 myArgCommands.push_back( theCommand );
2901 //================================================================================
2904 * \param theAdditionCmd - command to be converted
2905 * \param theMesh - mesh instance
2906 * \retval bool - status
2908 //================================================================================
2910 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
2911 const _pyID& theMesh)
2913 myIsWrapped = false;
2915 if ( my1dHyp.IsNull() )
2918 // set "SetLayerDistribution()" after addition cmd
2919 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
2921 _pyID geom = theAdditionCmd->GetArg( 1 );
2923 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
2924 if ( !algo.IsNull() )
2926 my1dHyp->SetMesh( theMesh );
2927 my1dHyp->SetConvMethodAndType(my1dHyp->GetAlgoCreationMethod().ToCString(),
2928 algo->GetAlgoType().ToCString());
2929 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
2932 // clear "SetLayerDistribution()" cmd
2933 myArgCommands.back()->Clear();
2935 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
2937 // find RadialPrism algo created on <geom> for theMesh
2938 GetCreationCmd()->SetObject( algo->GetID() );
2939 GetCreationCmd()->SetMethod( myAlgoMethod );
2940 GetCreationCmd()->RemoveArgs();
2941 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
2947 //================================================================================
2951 //================================================================================
2953 void _pyLayerDistributionHypo::Flush()
2955 // as creation of 1D hyp was written later then it's edition,
2956 // we need to find all it's edition calls and process them
2957 list< Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
2959 for ( cmd = myArgCommands.begin(); cmd != myArgCommands.end(); ++cmd )
2961 const _pyID& hyp1dID = (*cmd)->GetArg( 1 );
2962 if ( hyp1dID.IsEmpty() ) continue;
2964 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
2966 // make a new name for 1D hyp = "HypType" + "_Distribution"
2968 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
2970 if ( prevNewName.IsEmpty() ) continue;
2971 newName = prevNewName;
2975 if ( hyp1d->IsWrapped() ) {
2976 newName = hyp1d->GetCreationCmd()->GetMethod();
2979 TCollection_AsciiString hypTypeQuoted = hyp1d->GetCreationCmd()->GetArg(1);
2980 newName = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
2982 newName += "_Distribution";
2983 prevNewName = newName;
2985 hyp1d->GetCreationCmd()->SetResultValue( newName );
2987 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
2988 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
2989 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
2990 const _pyID& objID = (*cmdIt)->GetObject();
2991 if ( objID == hyp1dID ) {
2992 if ( !hyp1d.IsNull() )
2994 hyp1d->Process( *cmdIt );
2995 hyp1d->GetCreationCmd()->AddDependantCmd( *cmdIt );
2997 ( *cmdIt )->SetObject( newName );
3000 // Set new hyp name to SetLayerDistribution(hyp1dID) cmd
3001 (*cmd)->SetArg( 1, newName );
3005 //================================================================================
3007 * \brief additionally to Addition2Creation, clears SetDistrType() command
3008 * \param theCmd - AddHypothesis() command
3009 * \param theMesh - mesh to which a hypothesis is added
3010 * \retval bool - convertion result
3012 //================================================================================
3014 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
3015 const _pyID& theMesh)
3017 if ( IsWrappable( theMesh ) && myCurCrMethod->myArgs.size() > 1 ) {
3018 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
3019 bool scaleDistrType = false;
3020 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnusedCommands.rbegin();
3021 for ( ; cmd != myUnusedCommands.rend(); ++cmd ) {
3022 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
3023 if ( (*cmd)->GetArg( 1 ) == "1" ) {
3024 scaleDistrType = true;
3027 else if ( !scaleDistrType ) {
3028 // distribution type changed: remove scale factor from args
3029 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
3030 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
3032 CreationMethod& crMethod = type2meth->second;
3033 if ( crMethod.myArgs.size() == 2 )
3034 crMethod.myArgs.pop_back();
3041 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
3044 //================================================================================
3046 * \brief remove repeated commands defining distribution
3048 //================================================================================
3050 void _pyNumberOfSegmentsHyp::Flush()
3052 // find number of the last SetDistrType() command
3053 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnusedCommands.rbegin();
3054 int distrTypeNb = 0;
3055 for ( ; !distrTypeNb && cmd != myUnusedCommands.rend(); ++cmd )
3056 if ( (*cmd)->GetMethod() == "SetDistrType" )
3057 distrTypeNb = (*cmd)->GetOrderNb();
3058 else if (IsWrapped() && (*cmd)->GetMethod() == "SetObjectEntry" )
3061 // clear commands before the last SetDistrType()
3062 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnusedCommands };
3063 for ( int i = 0; i < 2; ++i ) {
3064 set<TCollection_AsciiString> uniqueMethods;
3065 list<Handle(_pyCommand)> & cmdList = *cmds[i];
3066 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
3068 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
3069 const TCollection_AsciiString& method = (*cmd)->GetMethod();
3070 if ( !clear || method == "SetNumberOfSegments" ) {
3071 bool isNewInSet = uniqueMethods.insert( method ).second;
3072 clear = !isNewInSet;
3081 //================================================================================
3083 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
3084 * into regular1D.LengthNearVertex( length, vertex )
3085 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
3086 * \param theMesh - The mesh needing this hypo
3087 * \retval bool - false if the command cant be converted
3089 //================================================================================
3091 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
3092 const _pyID& theMeshID)
3094 if ( IsWrappable( theMeshID )) {
3096 _pyID vertex = theCmd->GetArg( 1 );
3098 // the problem here is that segment algo will not be found
3099 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
3100 // geometry where segment algorithm is assigned
3101 Handle(_pyHypothesis) algo;
3102 _pyID geom = vertex;
3103 while ( algo.IsNull() && !geom.IsEmpty()) {
3104 // try to find geom as a father of <vertex>
3105 geom = FatherID( geom );
3106 algo = theGen->FindAlgo( geom, theMeshID, this );
3108 if ( algo.IsNull() )
3109 return false; // also possible to find geom as brother of veretex...
3110 // set geom instead of vertex
3111 theCmd->SetArg( 1, geom );
3113 // set vertex as a second arg
3114 if ( myCurCrMethod->myArgs.size() < 1) setCreationArg( 1, "1" ); // :(
3115 setCreationArg( 2, vertex );
3117 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
3118 // theMeshID.LengthNearVertex( length, vertex )
3119 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
3124 //================================================================================
3126 * \brief _pyAlgorithm constructor
3127 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
3129 //================================================================================
3131 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
3132 : _pyHypothesis( theCreationCmd )
3137 //================================================================================
3139 * \brief Convert the command adding an algorithm to mesh
3140 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
3141 * \param theMesh - The mesh needing this algo
3142 * \retval bool - false if the command cant be converted
3144 //================================================================================
3146 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
3147 const _pyID& theMeshID)
3149 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
3150 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
3151 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
3157 //================================================================================
3159 * \brief Return starting position of a part of python command
3160 * \param thePartIndex - The index of command part
3161 * \retval int - Part position
3163 //================================================================================
3165 int _pyCommand::GetBegPos( int thePartIndex )
3169 if ( myBegPos.Length() < thePartIndex )
3171 ASSERT( thePartIndex > 0 );
3172 return myBegPos( thePartIndex );
3175 //================================================================================
3177 * \brief Store starting position of a part of python command
3178 * \param thePartIndex - The index of command part
3179 * \param thePosition - Part position
3181 //================================================================================
3183 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
3185 while ( myBegPos.Length() < thePartIndex )
3186 myBegPos.Append( UNKNOWN );
3187 ASSERT( thePartIndex > 0 );
3188 myBegPos( thePartIndex ) = thePosition;
3191 //================================================================================
3193 * \brief Returns whitespace symbols at the line beginning
3194 * \retval TCollection_AsciiString - result
3196 //================================================================================
3198 TCollection_AsciiString _pyCommand::GetIndentation()
3201 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
3202 GetWord( myString, end, true );
3204 end = GetBegPos( RESULT_IND );
3205 return myString.SubString( 1, end - 1 );
3208 //================================================================================
3210 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
3211 * \retval const TCollection_AsciiString & - ResultValue substring
3213 //================================================================================
3215 const TCollection_AsciiString & _pyCommand::GetResultValue()
3217 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
3219 SetBegPos( RESULT_IND, EMPTY );
3220 int begPos, endPos = myString.Location( "=", 1, Length() );
3224 while ( begPos < endPos && isspace( myString.Value( begPos ))) ++begPos;
3225 if ( begPos < endPos )
3227 SetBegPos( RESULT_IND, begPos );
3229 while ( begPos < endPos && isspace( myString.Value( endPos ))) --endPos;
3230 myRes = myString.SubString( begPos, endPos );
3237 //================================================================================
3239 * \brief Return number of python command result value ResultValue = Obj.Meth()
3241 //================================================================================
3243 int _pyCommand::GetNbResultValues()
3247 int endPos = myString.Location( "=", 1, Length() );
3248 while ( begPos < endPos )
3250 _AString str = GetWord( myString, begPos, true );
3251 begPos = begPos+ str.Length();
3258 //================================================================================
3260 * \brief Return substring of python command looking like
3261 * ResultValue1 , ResultValue2,... = Obj.Meth() with res index
3262 * \retval const TCollection_AsciiString & - ResultValue with res index substring
3264 //================================================================================
3265 TCollection_AsciiString _pyCommand::GetResultValue(int res)
3268 if ( SkipSpaces( myString, begPos ) && myString.Value( begPos ) == '[' )
3269 ++begPos; // skip [, else the whole list is returned
3270 int endPos = myString.Location( "=", 1, Length() );
3272 while ( begPos < endPos) {
3273 _AString result = GetWord( myString, begPos, true );
3274 begPos = begPos + result.Length();
3277 result.RemoveAll('[');
3278 result.RemoveAll(']');
3284 return theEmptyString;
3287 //================================================================================
3289 * \brief Return substring of python command looking like ResVal = Object.Meth()
3290 * \retval const TCollection_AsciiString & - Object substring
3292 //================================================================================
3294 const TCollection_AsciiString & _pyCommand::GetObject()
3296 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
3299 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
3301 begPos = myString.Location( "=", 1, Length() ) + 1;
3302 // is '=' in the string argument (for example, name) or not
3303 int nb1 = 0; // number of ' character at the left of =
3304 int nb2 = 0; // number of " character at the left of =
3305 for ( int i = 1; i < begPos-1; i++ ) {
3306 if ( myString.Value( i )=='\'' )
3308 else if ( myString.Value( i )=='"' )
3311 // if number of ' or " is not divisible by 2,
3312 // then get an object at the start of the command
3313 if ( nb1 % 2 != 0 || nb2 % 2 != 0 )
3316 myObj = GetWord( myString, begPos, true );
3317 // check if object is complex,
3318 // so far consider case like "smesh.smesh.Method()"
3319 if ( int bracketPos = myString.Location( "(", begPos, Length() )) {
3320 //if ( bracketPos==0 ) bracketPos = Length();
3321 int dotPos = begPos+myObj.Length();
3322 while ( dotPos+1 < bracketPos ) {
3323 if ( int pos = myString.Location( ".", dotPos+1, bracketPos ))
3328 if ( dotPos > begPos+myObj.Length() )
3329 myObj = myString.SubString( begPos, dotPos-1 );
3331 // 1st word after '=' is an object
3332 // else // no method -> no object
3338 SetBegPos( OBJECT_IND, begPos );
3344 //================================================================================
3346 * \brief Return substring of python command looking like ResVal = Obj.Method()
3347 * \retval const TCollection_AsciiString & - Method substring
3349 //================================================================================
3351 const TCollection_AsciiString & _pyCommand::GetMethod()
3353 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
3356 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
3357 bool forward = true;
3359 begPos = myString.Location( "(", 1, Length() ) - 1;
3363 myMeth = GetWord( myString, begPos, forward );
3364 SetBegPos( METHOD_IND, begPos );
3370 //================================================================================
3372 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
3373 * \retval const TCollection_AsciiString & - Arg<index> substring
3375 //================================================================================
3377 const TCollection_AsciiString & _pyCommand::GetArg( int index )
3379 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
3383 int pos = GetBegPos( METHOD_IND ) + myMeth.Length();
3385 pos = myString.Location( "(", 1, Length() );
3389 // we are at or before '(', skip it if present
3391 while ( pos <= Length() && myString.Value( pos ) != '(' ) ++pos;
3392 if ( pos > Length() )
3396 SetBegPos( ARG1_IND, 0 ); // even no '('
3397 return theEmptyString;
3401 list< TCollection_AsciiString > separatorStack( 1, ",)");
3402 bool ignoreNesting = false;
3404 while ( pos <= Length() )
3406 const char chr = myString.Value( pos );
3408 if ( separatorStack.back().Location( chr, 1, separatorStack.back().Length()))
3410 if ( separatorStack.size() == 1 ) // a comma dividing args or a terminal ')' found
3412 while ( pos-1 >= prevPos && isspace( myString.Value( prevPos )))
3414 TCollection_AsciiString arg;
3415 if ( pos-1 >= prevPos ) {
3416 arg = myString.SubString( prevPos, pos-1 );
3417 arg.RightAdjust(); // remove spaces
3420 if ( !arg.IsEmpty() || chr == ',' )
3422 SetBegPos( ARG1_IND + myArgs.Length(), prevPos );
3423 myArgs.Append( arg );
3429 else // end of nesting args found
3431 separatorStack.pop_back();
3432 ignoreNesting = false;
3435 else if ( !ignoreNesting )
3438 case '(' : separatorStack.push_back(")"); break;
3439 case '[' : separatorStack.push_back("]"); break;
3440 case '\'': separatorStack.push_back("'"); ignoreNesting=true; break;
3441 case '"' : separatorStack.push_back("\""); ignoreNesting=true; break;
3448 if ( myArgs.Length() < index )
3449 return theEmptyString;
3450 return myArgs( index );
3453 //================================================================================
3455 * \brief Check if char is a word part
3456 * \param c - The character to check
3457 * \retval bool - The check result
3459 //================================================================================
3461 static inline bool isWord(const char c, const bool dotIsWord)
3464 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
3467 //================================================================================
3469 * \brief Looks for a word in the string and returns word's beginning
3470 * \param theString - The input string
3471 * \param theStartPos - The position to start the search, returning word's beginning
3472 * \param theForward - The search direction
3473 * \retval TCollection_AsciiString - The found word
3475 //================================================================================
3477 TCollection_AsciiString _pyCommand::GetWord( const _AString & theString,
3479 const bool theForward,
3480 const bool dotIsWord )
3482 int beg = theStartPos, end = theStartPos;
3483 theStartPos = EMPTY;
3484 if ( beg < 1 || beg > theString.Length() )
3485 return theEmptyString;
3487 if ( theForward ) { // search forward
3489 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
3491 if ( beg > theString.Length() )
3492 return theEmptyString; // no word found
3495 char begChar = theString.Value( beg );
3496 if ( begChar == '"' || begChar == '\'' || begChar == '[') {
3497 char endChar = ( begChar == '[' ) ? ']' : begChar;
3498 // end is at the corresponding quoting mark or bracket
3499 while ( end < theString.Length() &&
3500 ( theString.Value( end ) != endChar || theString.Value( end-1 ) == '\\'))
3504 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
3509 else { // search backward
3511 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
3514 return theEmptyString; // no word found
3516 char endChar = theString.Value( end );
3517 if ( endChar == '"' || endChar == '\'' || endChar == ']') {
3518 char begChar = ( endChar == ']' ) ? '[' : endChar;
3519 // beg is at the corresponding quoting mark
3521 ( theString.Value( beg ) != begChar || theString.Value( beg-1 ) == '\\'))
3525 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
3531 //cout << theString << " ---- " << beg << " - " << end << endl;
3532 return theString.SubString( beg, end );
3535 //================================================================================
3537 * \brief Returns true if the string looks like a study entry
3539 //================================================================================
3541 bool _pyCommand::IsStudyEntry( const TCollection_AsciiString& str )
3543 if ( str.Length() < 5 ) return false;
3545 int nbColons = 0, isColon;
3546 for ( int i = 1; i <= str.Length(); ++i )
3548 char c = str.Value(i);
3549 if (!( isColon = (c == ':')) && ( c < '0' || c > '9' ))
3551 nbColons += isColon;
3553 return nbColons > 2 && str.Length()-nbColons > 2;
3556 //================================================================================
3558 * \brief Finds entries in a sting
3560 //================================================================================
3562 std::list< _pyID > _pyCommand::GetStudyEntries( const TCollection_AsciiString& str )
3564 std::list< _pyID > resList;
3566 while ( ++pos <= str.Length() )
3568 if ( !isdigit( str.Value( pos ))) continue;
3569 if ( pos != 1 && ( isalpha( str.Value( pos-1 ) || str.Value( pos-1 ) == ':'))) continue;
3572 while ( ++end <= str.Length() && ( isdigit( str.Value( end )) || str.Value( end ) == ':' ));
3573 _pyID entry = str.SubString( pos, end-1 );
3575 if ( IsStudyEntry( entry ))
3576 resList.push_back( entry );
3581 //================================================================================
3583 * \brief Look for position where not space char is
3584 * \param theString - The string
3585 * \param thePos - The position to search from and which returns result
3586 * \retval bool - false if there are only space after thePos in theString
3588 //================================================================================
3590 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
3592 if ( thePos < 1 || thePos > theString.Length() )
3595 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
3598 return thePos <= theString.Length();
3601 //================================================================================
3603 * \brief Modify a part of the command
3604 * \param thePartIndex - The index of the part
3605 * \param thePart - The new part string
3606 * \param theOldPart - The old part
3608 //================================================================================
3610 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
3611 TCollection_AsciiString& theOldPart)
3613 int pos = GetBegPos( thePartIndex );
3614 if ( pos <= Length() && theOldPart != thePart)
3616 TCollection_AsciiString seperator;
3618 pos = GetBegPos( thePartIndex + 1 );
3619 if ( pos < 1 ) return;
3620 switch ( thePartIndex ) {
3621 case RESULT_IND: seperator = " = "; break;
3622 case OBJECT_IND: seperator = "."; break;
3623 case METHOD_IND: seperator = "()"; break;
3627 myString.Remove( pos, theOldPart.Length() );
3628 if ( !seperator.IsEmpty() )
3629 myString.Insert( pos , seperator );
3630 myString.Insert( pos, thePart );
3631 // update starting positions of the following parts
3632 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
3633 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
3634 if ( myBegPos( i ) > 0 )
3635 myBegPos( i ) += posDelta;
3637 theOldPart = thePart;
3641 //================================================================================
3643 * \brief Set agrument
3644 * \param index - The argument index, it counts from 1
3645 * \param theArg - The argument string
3647 //================================================================================
3649 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
3652 int argInd = ARG1_IND + index - 1;
3653 int pos = GetBegPos( argInd );
3654 if ( pos < 1 ) // no index-th arg exist, append inexistent args
3656 // find a closing parenthesis
3657 if ( GetNbArgs() != 0 && index <= GetNbArgs() ) {
3658 int lastArgInd = GetNbArgs();
3659 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
3660 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
3665 while ( pos > 0 && myString.Value( pos ) != ')' )
3668 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
3672 while ( myArgs.Length() < index ) {
3673 if ( myArgs.Length() )
3674 myString.Insert( pos++, "," );
3675 myArgs.Append("None");
3676 myString.Insert( pos, myArgs.Last() );
3677 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
3678 pos += myArgs.Last().Length();
3681 SetPart( argInd, theArg, myArgs( index ));
3684 //================================================================================
3686 * \brief Empty arg list
3688 //================================================================================
3690 void _pyCommand::RemoveArgs()
3692 if ( int pos = myString.Location( '(', Max( 1, GetBegPos( METHOD_IND )), Length() ))
3693 myString.Trunc( pos );
3696 if ( myBegPos.Length() >= ARG1_IND )
3697 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
3700 //================================================================================
3702 * \brief Comment a python command
3704 //================================================================================
3706 void _pyCommand::Comment()
3708 if ( IsEmpty() ) return;
3711 while ( i <= Length() && isspace( myString.Value(i) )) ++i;
3712 if ( i <= Length() )
3714 myString.Insert( i, "#" );
3715 for ( int iPart = 1; iPart <= myBegPos.Length(); ++iPart )
3717 int begPos = GetBegPos( iPart );
3718 if ( begPos != UNKNOWN )
3719 SetBegPos( iPart, begPos + 1 );
3724 //================================================================================
3726 * \brief Set dependent commands after this one
3728 //================================================================================
3730 bool _pyCommand::SetDependentCmdsAfter() const
3732 bool orderChanged = false;
3733 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
3734 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
3735 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
3736 orderChanged = true;
3737 theGen->SetCommandAfter( *cmd, this );
3738 (*cmd)->SetDependentCmdsAfter();
3741 return orderChanged;
3743 //================================================================================
3745 * \brief Insert accessor method after theObjectID
3746 * \param theObjectID - id of the accessed object
3747 * \param theAcsMethod - name of the method giving access to the object
3748 * \retval bool - false if theObjectID is not found in the command string
3750 //================================================================================
3752 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
3754 if ( !theAcsMethod )
3756 // start object search from the object, i.e. ignore result
3758 int beg = GetBegPos( OBJECT_IND );
3759 if ( beg < 1 || beg > Length() )
3762 while (( beg = myString.Location( theObjectID, beg, Length() )))
3764 // check that theObjectID is not just a part of a longer ID
3765 int afterEnd = beg + theObjectID.Length();
3766 Standard_Character c = myString.Value( afterEnd );
3767 if ( !isalnum( c ) && c != ':' ) {
3768 // check if accessor method already present
3770 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
3772 int oldLen = Length();
3773 myString.Insert( afterEnd, (char*) theAcsMethod );
3774 myString.Insert( afterEnd, "." );
3775 // update starting positions of the parts following the modified one
3776 int posDelta = Length() - oldLen;
3777 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
3778 if ( myBegPos( i ) > afterEnd )
3779 myBegPos( i ) += posDelta;
3784 beg = afterEnd; // is a part - next search
3789 //================================================================================
3791 * \brief Creates pyObject
3793 //================================================================================
3795 _pyObject::_pyObject(const Handle(_pyCommand)& theCreationCmd, const _pyID& theID)
3796 : myID(theID), myCreationCmd(theCreationCmd), myIsPublished(false)
3801 //================================================================================
3803 * \brief Set up myID and myIsPublished
3805 //================================================================================
3807 void _pyObject::setID(const _pyID& theID)
3810 myIsPublished = !theGen->IsNotPublished( GetID() );
3813 //================================================================================
3815 * \brief Clear myCreationCmd and myProcessedCmds
3817 //================================================================================
3819 void _pyObject::ClearCommands()
3824 if ( !myCreationCmd.IsNull() )
3825 myCreationCmd->Clear();
3827 list< Handle(_pyCommand) >::iterator cmd = myProcessedCmds.begin();
3828 for ( ; cmd != myProcessedCmds.end(); ++cmd )
3832 //================================================================================
3834 * \brief Return method name giving access to an interaface object wrapped by python class
3835 * \retval const char* - method name
3837 //================================================================================
3839 const char* _pyObject::AccessorMethod() const
3843 //================================================================================
3845 * \brief Return ID of a father
3847 //================================================================================
3849 _pyID _pyObject::FatherID(const _pyID & childID)
3851 int colPos = childID.SearchFromEnd(':');
3853 return childID.SubString( 1, colPos-1 );
3857 //================================================================================
3859 * \brief SelfEraser erases creation command if no more it's commands invoked
3861 //================================================================================
3863 void _pySelfEraser::Flush()
3865 if ( GetNbCalls() == 0 )
3866 GetCreationCmd()->Clear();
3869 //================================================================================
3871 * \brief _pySubMesh constructor
3873 //================================================================================
3875 _pySubMesh::_pySubMesh(const Handle(_pyCommand)& theCreationCmd):
3876 _pyObject(theCreationCmd)
3878 myMesh = ObjectToMesh( theGen->FindObject( theCreationCmd->GetObject() ));
3881 //================================================================================
3883 * \brief Return true if a sub-mesh can be used as argument of the given method
3885 //================================================================================
3887 bool _pySubMesh::CanBeArgOfMethod(const _AString& theMethodName)
3889 // names of all methods where a sub-mesh can be used as argument
3890 static TStringSet methods;
3891 if ( methods.empty() ) {
3892 const char * names[] = {
3893 // methods of SMESH_Gen
3895 // methods of SMESH_Group
3897 // methods of SMESH_Measurements
3899 // methods of SMESH_Mesh
3900 "ExportPartToMED","ExportCGNS","ExportPartToDAT","ExportPartToUNV","ExportPartToSTL",
3902 // methods of SMESH_MeshEditor
3903 "ReorientObject","Reorient2D","TriToQuadObject","QuadToTriObject","SplitQuadObject",
3904 "SplitVolumesIntoTetra","SmoothObject","SmoothParametricObject","ConvertFromQuadraticObject",
3905 "RotationSweepObject","RotationSweepObjectMakeGroups","RotationSweepObject1D",
3906 "RotationSweepObject1DMakeGroups","RotationSweepObject2D","RotationSweepObject2DMakeGroups",
3907 "ExtrusionSweepObject","ExtrusionSweepObjectMakeGroups","ExtrusionSweepObject0D",
3908 "ExtrusionSweepObject0DMakeGroups","ExtrusionSweepObject1D","ExtrusionSweepObject2D",
3909 "ExtrusionSweepObject1DMakeGroups","ExtrusionSweepObject2DMakeGroups",
3910 "ExtrusionAlongPathObjX","ExtrusionAlongPathObject","ExtrusionAlongPathObjectMakeGroups",
3911 "ExtrusionAlongPathObject1D","ExtrusionAlongPathObject1DMakeGroups",
3912 "ExtrusionAlongPathObject2D","ExtrusionAlongPathObject2DMakeGroups","MirrorObject",
3913 "MirrorObjectMakeGroups","MirrorObjectMakeMesh","TranslateObject","Scale",
3914 "TranslateObjectMakeGroups","TranslateObjectMakeMesh","ScaleMakeGroups","ScaleMakeMesh",
3915 "RotateObject","RotateObjectMakeGroups","RotateObjectMakeMesh","FindCoincidentNodesOnPart",
3916 "FindCoincidentNodesOnPartBut","FindEqualElements","FindAmongElementsByPoint",
3917 "MakeBoundaryMesh","Create0DElementsOnAllNodes",
3918 "" }; // <- mark of end
3919 methods.Insert( names );
3921 return methods.Contains( theMethodName );
3924 //================================================================================
3926 * \brief count invoked commands
3928 //================================================================================
3930 void _pySubMesh::Process( const Handle(_pyCommand)& theCommand )
3932 _pyObject::Process(theCommand); // count calls of Process()
3933 GetCreationCmd()->AddDependantCmd( theCommand );
3936 //================================================================================
3938 * \brief Move creation command depending on invoked commands
3940 //================================================================================
3942 void _pySubMesh::Flush()
3944 if ( GetNbCalls() == 0 ) // move to the end of all commands
3945 theGen->GetLastCommand()->AddDependantCmd( GetCreationCmd() );
3946 else if ( !myCreator.IsNull() )
3947 // move to be just after creator
3948 myCreator->GetCreationCmd()->AddDependantCmd( GetCreationCmd() );
3951 //================================================================================
3953 * \brief Creates _pyGroup
3955 //================================================================================
3957 _pyGroup::_pyGroup(const Handle(_pyCommand)& theCreationCmd, const _pyID & id)
3958 :_pySubMesh(theCreationCmd)
3960 if ( !id.IsEmpty() )
3963 myCanClearCreationCmd = true;
3965 const _AString& method = theCreationCmd->GetMethod();
3966 if ( method == "CreateGroup" ) // CreateGroup() --> CreateEmptyGroup()
3968 theCreationCmd->SetMethod( "CreateEmptyGroup" );
3970 // ----------------------------------------------------------------------
3971 else if ( method == "CreateGroupFromGEOM" ) // (type, name, grp)
3973 _pyID geom = theCreationCmd->GetArg( 3 );
3974 // VSR 24/12/2010. PAL21106: always use GroupOnGeom() function on dump
3975 // next if(){...} section is commented
3976 //if ( sameGroupType( geom, theCreationCmd->GetArg( 1 )) ) { // --> Group(geom)
3977 // theCreationCmd->SetMethod( "Group" );
3978 // theCreationCmd->RemoveArgs();
3979 // theCreationCmd->SetArg( 1, geom );
3982 // ------------------------->>>>> GroupOnGeom( geom, name, typ )
3983 _pyID type = theCreationCmd->GetArg( 1 );
3984 _pyID name = theCreationCmd->GetArg( 2 );
3985 theCreationCmd->SetMethod( "GroupOnGeom" );
3986 theCreationCmd->RemoveArgs();
3987 theCreationCmd->SetArg( 1, geom );
3988 theCreationCmd->SetArg( 2, name );
3989 theCreationCmd->SetArg( 3, type );
3992 else if ( method == "CreateGroupFromFilter" )
3994 // -> GroupOnFilter(typ, name, aFilter0x4743dc0 -> aFilter_1)
3995 theCreationCmd->SetMethod( "GroupOnFilter" );
3997 _pyID filterID = theCreationCmd->GetArg(3);
3998 Handle(_pyFilter) filter = Handle(_pyFilter)::DownCast( theGen->FindObject( filterID ));
3999 if ( !filter.IsNull())
4001 if ( !filter->GetNewID().IsEmpty() )
4002 theCreationCmd->SetArg( 3, filter->GetNewID() );
4003 filter->AddUser( this );
4009 // theCreationCmd does something else apart from creation of this group
4010 // and thus it can't be cleared if this group is removed
4011 myCanClearCreationCmd = false;
4015 //================================================================================
4017 * \brief set myCanClearCreationCmd = true if the main action of the creation
4018 * command is discarded
4020 //================================================================================
4022 void _pyGroup::RemovedWithContents()
4024 // this code would be appropriate if Add0DElementsToAllNodes() returned only new nodes
4025 // via a created group
4026 //if ( GetCreationCmd()->GetMethod() == "Add0DElementsToAllNodes")
4027 // myCanClearCreationCmd = true;
4030 //================================================================================
4032 * \brief To convert creation of a group by filter
4034 //================================================================================
4036 void _pyGroup::Process( const Handle(_pyCommand)& theCommand)
4038 // Convert the following set of commands into mesh.MakeGroupByFilter(groupName, theFilter)
4039 // group = mesh.CreateEmptyGroup( elemType, groupName )
4040 // aFilter.SetMesh(mesh)
4041 // nbAdd = group.AddFrom( aFilter )
4042 Handle(_pyFilter) filter;
4043 if ( theCommand->GetMethod() == "AddFrom" )
4045 _pyID idSource = theCommand->GetArg(1);
4046 // check if idSource is a filter
4047 filter = Handle(_pyFilter)::DownCast( theGen->FindObject( idSource ));
4048 if ( !filter.IsNull() )
4050 // find aFilter.SetMesh(mesh) to clear it, it should be just before theCommand
4051 list< Handle(_pyCommand) >::reverse_iterator cmdIt = theGen->GetCommands().rbegin();
4052 while ( *cmdIt != theCommand ) ++cmdIt;
4053 while ( (*cmdIt)->GetOrderNb() != 1 )
4055 const Handle(_pyCommand)& setMeshCmd = *(++cmdIt);
4056 if ((setMeshCmd->GetObject() == idSource ||
4057 setMeshCmd->GetObject() == filter->GetNewID() )
4059 setMeshCmd->GetMethod() == "SetMesh")
4061 setMeshCmd->Clear();
4065 // replace 3 commands by one
4066 theCommand->Clear();
4067 const Handle(_pyCommand)& makeGroupCmd = GetCreationCmd();
4068 TCollection_AsciiString name = makeGroupCmd->GetArg( 2 );
4069 if ( !filter->GetNewID().IsEmpty() )
4070 idSource = filter->GetNewID();
4071 makeGroupCmd->SetMethod( "MakeGroupByFilter" );
4072 makeGroupCmd->SetArg( 1, name );
4073 makeGroupCmd->SetArg( 2, idSource );
4076 else if ( theCommand->GetMethod() == "SetFilter" )
4078 // set new name of a filter or clear the command if the same filter is set
4079 _pyID filterID = theCommand->GetArg(1);
4080 filter = Handle(_pyFilter)::DownCast( theGen->FindObject( filterID ));
4081 if ( !myFilter.IsNull() && filter == myFilter )
4082 theCommand->Clear();
4083 else if ( !filter.IsNull() && !filter->GetNewID().IsEmpty() )
4084 theCommand->SetArg( 1, filter->GetNewID() );
4087 else if ( theCommand->GetMethod() == "GetFilter" )
4089 // GetFilter() returns a filter with other ID, make myFilter process
4090 // calls of the returned filter
4091 if ( !myFilter.IsNull() )
4093 theGen->SetProxyObject( theCommand->GetResultValue(), myFilter );
4094 theCommand->Clear();
4098 if ( !filter.IsNull() )
4099 filter->AddUser( this );
4101 theGen->AddMeshAccessorMethod( theCommand );
4104 //================================================================================
4106 * \brief Prevent clearing "DoubleNode...() command if a group created by it is removed
4108 //================================================================================
4110 void _pyGroup::Flush()
4112 if ( !theGen->IsToKeepAllCommands() &&
4113 myCreationCmd && !myCanClearCreationCmd )
4115 myCreationCmd.Nullify(); // this way myCreationCmd won't be cleared
4119 //================================================================================
4121 * \brief Constructor of _pyFilter
4123 //================================================================================
4125 _pyFilter::_pyFilter(const Handle(_pyCommand)& theCreationCmd, const _pyID& newID/*=""*/)
4126 :_pyObject(theCreationCmd), myNewID( newID )
4130 //================================================================================
4132 * \brief To convert creation of a filter by criteria and
4133 * to replace an old name by a new one
4135 //================================================================================
4137 void _pyFilter::Process( const Handle(_pyCommand)& theCommand)
4139 if ( theCommand->GetObject() == GetID() )
4140 _pyObject::Process(theCommand); // count commands
4142 if ( !myNewID.IsEmpty() )
4143 theCommand->SetObject( myNewID );
4145 // Convert the following set of commands into smesh.GetFilterFromCriteria(criteria)
4146 // aFilter0x2aaab0487080 = aFilterManager.CreateFilter()
4147 // aFilter0x2aaab0487080.SetCriteria(aCriteria)
4148 if ( GetNbCalls() == 1 && // none method was called before this SetCriteria() call
4149 theCommand->GetMethod() == "SetCriteria")
4151 // aFilter.SetCriteria(aCriteria) ->
4152 // aFilter = smesh.GetFilterFromCriteria(criteria)
4153 if ( myNewID.IsEmpty() )
4154 theCommand->SetResultValue( GetID() );
4156 theCommand->SetResultValue( myNewID );
4157 theCommand->SetObject( SMESH_2smeshpy::GenName() );
4158 theCommand->SetMethod( "GetFilterFromCriteria" );
4160 // Clear aFilterManager.CreateFilter()
4161 GetCreationCmd()->Clear();
4163 else if ( theCommand->GetMethod() == "SetMesh" )
4165 if ( myMesh == theCommand->GetArg( 1 ))
4166 theCommand->Clear();
4168 myMesh = theCommand->GetArg( 1 );
4169 theGen->AddMeshAccessorMethod( theCommand );
4173 //================================================================================
4175 * \brief Set new filter name to the creation command
4177 //================================================================================
4179 void _pyFilter::Flush()
4181 if ( !myNewID.IsEmpty() && !GetCreationCmd()->IsEmpty() )
4182 GetCreationCmd()->SetResultValue( myNewID );
4185 //================================================================================
4187 * \brief Return true if all my users can be cleared
4189 //================================================================================
4191 bool _pyFilter::CanClear()
4193 list< Handle(_pyObject) >::iterator obj = myUsers.begin();
4194 for ( ; obj != myUsers.end(); ++obj )
4195 if ( !(*obj)->CanClear() )
4201 //================================================================================
4203 * \brief Reads _pyHypothesis'es from resource files of mesher Plugins
4205 //================================================================================
4207 _pyHypothesisReader::_pyHypothesisReader()
4210 vector< string > xmlPaths = SMESH_Gen::GetPluginXMLPaths();
4211 LDOMParser xmlParser;
4212 for ( size_t i = 0; i < xmlPaths.size(); ++i )
4214 bool error = xmlParser.parse( xmlPaths[i].c_str() );
4218 INFOS( xmlParser.GetError(data) );
4221 // <algorithm type="Regular_1D"
4222 // label-id="Wire discretisation"
4225 // <algo>Regular_1D=Segment()</algo>
4226 // <hypo>LocalLength=LocalLength(SetLength(1),,SetPrecision(1))</hypo>
4228 LDOM_Document xmlDoc = xmlParser.getDocument();
4229 LDOM_NodeList algoNodeList = xmlDoc.getElementsByTagName( "algorithm" );
4230 for ( int i = 0; i < algoNodeList.getLength(); ++i )
4232 LDOM_Node algoNode = algoNodeList.item( i );
4233 LDOM_Element& algoElem = (LDOM_Element&) algoNode;
4234 LDOM_NodeList pyAlgoNodeList = algoElem.getElementsByTagName( "algo" );
4235 if ( pyAlgoNodeList.getLength() < 1 ) continue;
4237 _AString text, algoType, method, arg;
4238 for ( int iA = 0; iA < pyAlgoNodeList.getLength(); ++iA )
4240 LDOM_Node pyAlgoNode = pyAlgoNodeList.item( iA );
4241 LDOM_Node textNode = pyAlgoNode.getFirstChild();
4242 text = textNode.getNodeValue();
4243 Handle(_pyCommand) algoCmd = new _pyCommand( text );
4244 algoType = algoCmd->GetResultValue();
4245 method = algoCmd->GetMethod();
4246 arg = algoCmd->GetArg(1);
4247 if ( !algoType.IsEmpty() && !method.IsEmpty() )
4249 Handle(_pyAlgorithm) algo = new _pyAlgorithm( algoCmd );
4250 algo->SetConvMethodAndType( method, algoType );
4251 if ( !arg.IsEmpty() )
4252 algo->setCreationArg( 1, arg );
4254 myType2Hyp[ algoType ] = algo;
4258 if ( algoType.IsEmpty() ) continue;
4260 LDOM_NodeList pyHypoNodeList = algoElem.getElementsByTagName( "hypo" );
4262 Handle( _pyHypothesis ) hyp;
4263 for ( int iH = 0; iH < pyHypoNodeList.getLength(); ++iH )
4265 LDOM_Node pyHypoNode = pyHypoNodeList.item( iH );
4266 LDOM_Node textNode = pyHypoNode.getFirstChild();
4267 text = textNode.getNodeValue();
4268 Handle(_pyCommand) hypoCmd = new _pyCommand( text );
4269 hypType = hypoCmd->GetResultValue();
4270 method = hypoCmd->GetMethod();
4271 if ( !hypType.IsEmpty() && !method.IsEmpty() )
4273 map<_AString, Handle(_pyHypothesis)>::iterator type2hyp = myType2Hyp.find( hypType );
4274 if ( type2hyp == myType2Hyp.end() )
4275 hyp = new _pyHypothesis( hypoCmd );
4277 hyp = type2hyp->second;
4278 hyp->SetConvMethodAndType( method, algoType );
4279 for ( int iArg = 1; iArg <= hypoCmd->GetNbArgs(); ++iArg )
4281 _pyCommand argCmd( hypoCmd->GetArg( iArg ));
4282 _AString argMethod = argCmd.GetMethod();
4283 _AString argNbText = argCmd.GetArg( 1 );
4284 if ( argMethod.IsEmpty() && !argCmd.IsEmpty() )
4285 hyp->setCreationArg( 1, argCmd.GetString() ); // e.g. Parameters(smesh.SIMPLE)
4287 hyp->AddArgMethod( argMethod,
4288 argNbText.IsIntegerValue() ? argNbText.IntegerValue() : 1 );
4290 myType2Hyp[ hypType ] = hyp;
4294 // <hypothesis type="BLSURF_Parameters"
4298 // <accumulative-methods>
4299 // SetEnforcedVertex,
4300 // SetEnforcedVertexNamed
4301 // </accumulative-methods>
4305 LDOM_NodeList hypNodeList = xmlDoc.getElementsByTagName( "hypothesis" );
4306 for ( int i = 0; i < hypNodeList.getLength(); ++i )
4308 LDOM_Node hypNode = hypNodeList.item( i );
4309 LDOM_Element& hypElem = (LDOM_Element&) hypNode;
4310 _AString hypType = hypElem.getAttribute("type");
4311 LDOM_NodeList methNodeList = hypElem.getElementsByTagName( "accumulative-methods" );
4312 if ( methNodeList.getLength() != 1 || hypType.IsEmpty() ) continue;
4314 map<_AString, Handle(_pyHypothesis)>::const_iterator type2hyp = myType2Hyp.find( hypType );
4315 if ( type2hyp == myType2Hyp.end() ) continue;
4317 LDOM_Node methNode = methNodeList.item( 0 );
4318 LDOM_Node textNode = methNode.getFirstChild();
4319 _AString text = textNode.getNodeValue();
4323 method = _pyCommand::GetWord( text, pos, /*forward= */true );
4324 pos += method.Length();
4325 type2hyp->second->AddAccumulativeMethod( method );
4327 while ( !method.IsEmpty() );
4330 } // loop on xmlPaths
4333 //================================================================================
4335 * \brief Returns a new hypothesis initialized according to the read information
4337 //================================================================================
4339 Handle(_pyHypothesis)
4340 _pyHypothesisReader::GetHypothesis(const _AString& hypType,
4341 const Handle(_pyCommand)& creationCmd) const
4343 Handle(_pyHypothesis) resHyp, sampleHyp;
4345 map<_AString, Handle(_pyHypothesis)>::const_iterator type2hyp = myType2Hyp.find( hypType );
4346 if ( type2hyp != myType2Hyp.end() )
4347 sampleHyp = type2hyp->second;
4349 if ( sampleHyp.IsNull() )
4351 resHyp = new _pyHypothesis(creationCmd);
4355 if ( sampleHyp->IsAlgo() )
4356 resHyp = new _pyAlgorithm( creationCmd );
4358 resHyp = new _pyHypothesis(creationCmd);
4359 resHyp->Assign( sampleHyp, _pyID() );