1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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 <SALOMEDS_wrap.hxx>
34 #include <utilities.h>
36 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
37 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
39 #include "SMESH_Gen_i.hxx"
40 /* SALOME headers that include CORBA headers that include windows.h
41 * that defines GetObject symbol as GetObjectA should stand before SALOME headers
42 * that declare methods named GetObject - to apply the same rules of GetObject renaming
43 * and thus to avoid mess with GetObject symbol on Windows */
45 #include <LDOMParser.hxx>
54 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
55 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
56 IMPLEMENT_STANDARD_HANDLE (_pyHypothesisReader,Standard_Transient);
57 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
58 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
59 IMPLEMENT_STANDARD_HANDLE (_pySubMesh ,_pyObject);
60 IMPLEMENT_STANDARD_HANDLE (_pyMeshEditor ,_pyObject);
61 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
62 IMPLEMENT_STANDARD_HANDLE (_pySelfEraser ,_pyObject);
63 IMPLEMENT_STANDARD_HANDLE (_pyGroup ,_pyObject);
64 IMPLEMENT_STANDARD_HANDLE (_pyFilter ,_pyObject);
65 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
66 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
67 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
69 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
70 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
71 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesisReader,Standard_Transient);
72 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
73 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
74 IMPLEMENT_STANDARD_RTTIEXT(_pySubMesh ,_pyObject);
75 IMPLEMENT_STANDARD_RTTIEXT(_pyMeshEditor ,_pyObject);
76 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
77 IMPLEMENT_STANDARD_RTTIEXT(_pySelfEraser ,_pyObject);
78 IMPLEMENT_STANDARD_RTTIEXT(_pyGroup ,_pyObject);
79 IMPLEMENT_STANDARD_RTTIEXT(_pyFilter ,_pyObject);
80 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
81 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
82 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
83 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
84 IMPLEMENT_STANDARD_RTTIEXT(_pySegmentLengthAroundVertexHyp,_pyHypothesis);
87 using SMESH::TPythonDump;
90 * \brief Container of commands into which the initial script is split.
91 * It also contains data coresponding to SMESH_Gen contents
93 static Handle(_pyGen) theGen;
95 static TCollection_AsciiString theEmptyString;
97 //#define DUMP_CONVERSION
99 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
100 #undef DUMP_CONVERSION
106 //================================================================================
108 * \brief Set of TCollection_AsciiString initialized by C array of C strings
110 //================================================================================
112 struct TStringSet: public set<TCollection_AsciiString>
115 * \brief Filling. The last string must be ""
117 void Insert(const char* names[]) {
118 for ( int i = 0; names[i][0] ; ++i )
119 insert( (char*) names[i] );
122 * \brief Check if a string is in
124 bool Contains(const TCollection_AsciiString& name ) {
125 return find( name ) != end();
129 //================================================================================
131 * \brief Map of TCollection_AsciiString initialized by C array of C strings.
132 * Odd items of the C array are map keys, and even items are values
134 //================================================================================
136 struct TStringMap: public map<TCollection_AsciiString,TCollection_AsciiString>
139 * \brief Filling. The last string must be ""
141 void Insert(const char* names_values[]) {
142 for ( int i = 0; names_values[i][0] ; i += 2 )
143 insert( make_pair( (char*) names_values[i], names_values[i+1] ));
146 * \brief Check if a string is in
148 TCollection_AsciiString Value(const TCollection_AsciiString& name ) {
149 map< _AString, _AString >::iterator it = find( name );
150 return it == end() ? "" : it->second;
154 //================================================================================
156 * \brief Returns a mesh by object
158 //================================================================================
160 Handle(_pyMesh) ObjectToMesh( const Handle( _pyObject )& obj )
164 if ( obj->IsKind( STANDARD_TYPE( _pyMesh )))
165 return Handle(_pyMesh)::DownCast( obj );
166 else if ( obj->IsKind( STANDARD_TYPE( _pySubMesh )))
167 return Handle(_pySubMesh)::DownCast( obj )->GetMesh();
168 else if ( obj->IsKind( STANDARD_TYPE( _pyGroup )))
169 return Handle(_pyGroup)::DownCast( obj )->GetMesh();
171 return Handle(_pyMesh)();
174 //================================================================================
176 * \brief Check if objects used as args have been created by previous commands
178 //================================================================================
180 void CheckObjectPresence( const Handle(_pyCommand)& cmd, set<_pyID> & presentObjects)
182 // either comment or erase a command including NotPublishedObjectName()
183 if ( cmd->GetString().Location( TPythonDump::NotPublishedObjectName(), 1, cmd->Length() ))
185 bool isResultPublished = false;
186 const int nbRes = cmd->GetNbResultValues();
187 for ( int i = 0; i < nbRes; i++ )
189 _pyID objID = cmd->GetResultValue( i+1 );
190 if ( cmd->IsStudyEntry( objID ))
191 isResultPublished = (! theGen->IsNotPublished( objID ));
192 theGen->ObjectCreationRemoved( objID ); // objID.SetName( name ) is not needed
194 if ( isResultPublished )
200 // check if an Object was created in the script
203 _pyID obj = cmd->GetObject();
204 if ( obj.Search( "print " ) == 1 )
205 return; // print statement
207 if ( !obj.IsEmpty() && obj.Value( obj.Length() ) == ')' )
208 // remove an accessor method
209 obj = _pyCommand( obj ).GetObject();
211 const bool isMethodCall = cmd->IsMethodCall();
212 if ( !obj.IsEmpty() && isMethodCall && !presentObjects.count( obj ) )
214 comment = "not created Object";
215 theGen->ObjectCreationRemoved( obj );
217 // check if a command has not created args
218 for ( int iArg = cmd->GetNbArgs(); iArg && comment.IsEmpty(); --iArg )
220 const _pyID& arg = cmd->GetArg( iArg );
221 if ( arg.IsEmpty() || arg.Value( 1 ) == '"' || arg.Value( 1 ) == '\'' )
223 list< _pyID > idList = cmd->GetStudyEntries( arg );
224 list< _pyID >::iterator id = idList.begin();
225 for ( ; id != idList.end(); ++id )
226 if ( !theGen->IsGeomObject( *id ) && !presentObjects.count( *id ))
228 comment += *id + " has not been yet created";
231 // if ( idList.empty() && cmd->IsID( arg ) && !presentObjects.count( arg ))
232 // comment += arg + " has not been yet created";
234 // treat result objects
235 const _pyID& result = cmd->GetResultValue();
236 if ( !result.IsEmpty() && result.Value( 1 ) != '"' && result.Value( 1 ) != '\'' )
238 list< _pyID > idList = cmd->GetStudyEntries( result );
239 list< _pyID >::iterator id = idList.begin();
240 for ( ; id != idList.end(); ++id )
242 if ( comment.IsEmpty() )
243 presentObjects.insert( *id );
245 theGen->ObjectCreationRemoved( *id ); // objID.SetName( name ) is not needed
247 if ( idList.empty() && cmd->IsID( result ))
248 presentObjects.insert( result );
250 // comment the command
251 if ( !comment.IsEmpty() )
254 cmd->GetString() += " ### ";
255 cmd->GetString() += comment;
259 //================================================================================
261 * \brief Fix SMESH::FunctorType arguments of SMESH::Filter::Criterion()
263 //================================================================================
265 void fixFunctorType( TCollection_AsciiString& Type,
266 TCollection_AsciiString& Compare,
267 TCollection_AsciiString& UnaryOp,
268 TCollection_AsciiString& BinaryOp )
270 // The problem is that dumps of old studies created using filters becomes invalid
271 // when new items are inserted in the enum SMESH::FunctorType since values
272 // of this enum are dumped as integer values.
273 // This function corrects enum values of old studies given as args (Type,Compare,...)
274 // We can find out how to correct them by value of BinaryOp which can have only two
275 // values: FT_Undefined or FT_LogicalNOT.
276 // Hereafter is the history of the enum SMESH::FunctorType since v3.0.0
277 // where PythonDump appeared
278 // v 3.0.0: FT_Undefined == 25
279 // v 3.1.0: FT_Undefined == 26, new items:
281 // v 4.1.2: FT_Undefined == 27, new items:
282 // - FT_BelongToGenSurface = 17
283 // v 5.1.1: FT_Undefined == 32, new items:
284 // - FT_FreeNodes = 10
285 // - FT_FreeFaces = 11
286 // - FT_LinearOrQuadratic = 23
287 // - FT_GroupColor = 24
288 // - FT_ElemGeomType = 25
289 // v 5.1.5: FT_Undefined == 33, new items:
290 // - FT_CoplanarFaces = 26
291 // v 6.2.0: FT_Undefined == 39, new items:
292 // - FT_MaxElementLength2D = 8
293 // - FT_MaxElementLength3D = 9
294 // - FT_BareBorderVolume = 25
295 // - FT_BareBorderFace = 26
296 // - FT_OverConstrainedVolume = 27
297 // - FT_OverConstrainedFace = 28
298 // v 6.5.0: FT_Undefined == 43, new items:
299 // - FT_EqualNodes = 14
300 // - FT_EqualEdges = 15
301 // - FT_EqualFaces = 16
302 // - FT_EqualVolumes = 17
303 // v 6.6.0: FT_Undefined == 44, new items:
304 // - FT_BallDiameter = 37
305 // v 6.7.1: FT_Undefined == 45, new items:
306 // - FT_EntityType = 36
307 // v 7.3.0: FT_Undefined == 46, new items:
308 // - FT_ConnectedElements = 39
309 // v 7.6.0: FT_Undefined == 47, new items:
310 // - FT_BelongToMeshGroup = 22
312 // It's necessary to continue recording this history and to fill
313 // undef2newItems (see below) accordingly.
315 typedef map< int, vector< int > > TUndef2newItems;
316 static TUndef2newItems undef2newItems;
317 if ( undef2newItems.empty() )
319 undef2newItems[ 26 ].push_back( 7 );
320 undef2newItems[ 27 ].push_back( 17 );
321 { int items[] = { 10, 11, 23, 24, 25 };
322 undef2newItems[ 32 ].assign( items, items+5 ); }
323 undef2newItems[ 33 ].push_back( 26 );
324 { int items[] = { 8, 9, 25, 26, 27, 28 };
325 undef2newItems[ 39 ].assign( items, items+6 ); }
326 { int items[] = { 14, 15, 16, 17 };
327 undef2newItems[ 43 ].assign( items, items+4 ); }
328 undef2newItems[ 44 ].push_back( 37 );
329 undef2newItems[ 45 ].push_back( 36 );
330 undef2newItems[ 46 ].push_back( 39 );
331 undef2newItems[ 47 ].push_back( 22 );
333 ASSERT( undef2newItems.rbegin()->first == SMESH::FT_Undefined );
336 int iType = Type.IntegerValue();
337 int iCompare = Compare.IntegerValue();
338 int iUnaryOp = UnaryOp.IntegerValue();
339 int iBinaryOp = BinaryOp.IntegerValue();
341 // find out integer value of FT_Undefined at the moment of dump
342 int oldUndefined = iBinaryOp;
343 if ( iBinaryOp < iUnaryOp ) // BinaryOp was FT_LogicalNOT
346 // apply history to args
347 TUndef2newItems::const_iterator undef_items =
348 undef2newItems.upper_bound( oldUndefined );
349 if ( undef_items != undef2newItems.end() )
351 int* pArg[4] = { &iType, &iCompare, &iUnaryOp, &iBinaryOp };
352 for ( ; undef_items != undef2newItems.end(); ++undef_items )
354 const vector< int > & addedItems = undef_items->second;
355 for ( size_t i = 0; i < addedItems.size(); ++i )
356 for ( int iArg = 0; iArg < 4; ++iArg )
358 int& arg = *pArg[iArg];
359 if ( arg >= addedItems[i] )
363 Type = TCollection_AsciiString( iType );
364 Compare = TCollection_AsciiString( iCompare );
365 UnaryOp = TCollection_AsciiString( iUnaryOp );
366 BinaryOp = TCollection_AsciiString( iBinaryOp );
370 //================================================================================
372 * \brief Replaces "SMESH.PointStruct(x,y,z)" and "SMESH.DirStruct( SMESH.PointStruct(x,y,z))"
373 * arguments of a given command by a list "[x,y,z]" if the list is accesible
376 //================================================================================
378 void StructToList( Handle( _pyCommand)& theCommand, const bool checkMethod=true )
380 static TStringSet methodsAcceptingList;
381 if ( methodsAcceptingList.empty() ) {
382 const char * methodNames[] = {
383 "GetCriterion","Reorient2D","ExtrusionSweep","ExtrusionSweepMakeGroups0D",
384 "ExtrusionSweepMakeGroups","ExtrusionSweep0D",
385 "AdvancedExtrusion","AdvancedExtrusionMakeGroups",
386 "ExtrusionSweepObject","ExtrusionSweepObject0DMakeGroups",
387 "ExtrusionSweepObjectMakeGroups","ExtrusionSweepObject0D",
388 "ExtrusionSweepObject1D","ExtrusionSweepObject1DMakeGroups",
389 "ExtrusionSweepObject2D","ExtrusionSweepObject2DMakeGroups",
390 "ExtrusionSweepObjects","RotationSweepObjects","ExtrusionAlongPathObjects",
391 "Translate","TranslateMakeGroups","TranslateMakeMesh",
392 "TranslateObject","TranslateObjectMakeGroups", "TranslateObjectMakeMesh",
393 "ExtrusionAlongPathX","ExtrusionAlongPathObjX","SplitHexahedraIntoPrisms"
394 ,"" }; // <- mark of the end
395 methodsAcceptingList.Insert( methodNames );
397 if ( !checkMethod || methodsAcceptingList.Contains( theCommand->GetMethod() ))
399 for ( int i = theCommand->GetNbArgs(); i > 0; --i )
401 const _AString & arg = theCommand->GetArg( i );
402 if ( arg.Search( "SMESH.PointStruct" ) == 1 ||
403 arg.Search( "SMESH.DirStruct" ) == 1 )
405 Handle(_pyCommand) workCmd = new _pyCommand( arg );
406 if ( workCmd->GetNbArgs() == 1 ) // SMESH.DirStruct( SMESH.PointStruct(x,y,z))
408 workCmd = new _pyCommand( workCmd->GetArg( 1 ) );
410 if ( workCmd->GetNbArgs() == 3 ) // SMESH.PointStruct(x,y,z)
412 _AString newArg = "[ ";
413 newArg += ( workCmd->GetArg( 1 ) + ", " +
414 workCmd->GetArg( 2 ) + ", " +
415 workCmd->GetArg( 3 ) + " ]");
416 theCommand->SetArg( i, newArg );
422 //================================================================================
424 * \brief Replaces "mesh.GetIDSource([id1,id2])" argument of a given command by
425 * a list "[id1,id2]" if the list is an accesible type of argument.
427 //================================================================================
429 void GetIDSourceToList( Handle( _pyCommand)& theCommand )
431 static TStringSet methodsAcceptingList;
432 if ( methodsAcceptingList.empty() ) {
433 const char * methodNames[] = {
434 "ExportPartToMED","ExportPartToDAT","ExportPartToUNV","ExportPartToSTL",
435 "ExportCGNS","ExportGMF",
436 "Create0DElementsOnAllNodes","Reorient2D","QuadTo4Tri",
437 "ScaleMakeGroups","Scale","ScaleMakeMesh",
438 "FindCoincidentNodesOnPartBut","DoubleElements",
439 "ExtrusionSweepObjects","RotationSweepObjects","ExtrusionAlongPathObjects"
440 ,"" }; // <- mark of the end
441 methodsAcceptingList.Insert( methodNames );
443 if ( methodsAcceptingList.Contains( theCommand->GetMethod() ))
445 for ( int i = theCommand->GetNbArgs(); i > 0; --i )
447 _pyCommand argCmd( theCommand->GetArg( i ));
448 if ( argCmd.GetMethod() == "GetIDSource" &&
449 argCmd.GetNbArgs() == 2 )
451 theCommand->SetArg( i, argCmd.GetArg( 1 ));
458 //================================================================================
460 * \brief Convert a python script using commands of smeshBuilder.py
461 * \param theScriptLines - Lines of the input script
462 * \param theEntry2AccessorMethod - returns method names to access to
463 * objects wrapped with python class
464 * \param theObjectNames - names of objects
465 * \param theRemovedObjIDs - entries of objects whose created commands were removed
466 * \param theHistoricalDump - true means to keep all commands, false means
467 * to exclude commands relating to objects removed from study
468 * \retval TCollection_AsciiString - Convertion result
470 //================================================================================
473 SMESH_2smeshpy::ConvertScript(std::list< TCollection_AsciiString >& theScriptLines,
474 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
475 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
476 std::set< TCollection_AsciiString >& theRemovedObjIDs,
477 SALOMEDS::Study_ptr& theStudy,
478 const bool theToKeepAllCommands)
480 std::list< TCollection_AsciiString >::iterator lineIt;
481 // process notebook variables
483 SMESH_NoteBook aNoteBook;
485 for ( lineIt = theScriptLines.begin(); lineIt != theScriptLines.end(); ++lineIt )
486 aNoteBook.AddCommand( *lineIt );
488 theScriptLines.clear();
490 aNoteBook.ReplaceVariables();
492 aNoteBook.GetResultLines( theScriptLines );
495 // convert to smeshBuilder.py API
497 theGen = new _pyGen( theEntry2AccessorMethod,
501 theToKeepAllCommands );
503 for ( lineIt = theScriptLines.begin(); lineIt != theScriptLines.end(); ++lineIt )
504 theGen->AddCommand( *lineIt );
506 theScriptLines.clear();
510 #ifdef DUMP_CONVERSION
511 MESSAGE_BEGIN ( std::endl << " ######## RESULT ######## " << std::endl<< std::endl );
514 // clean commmands of removed objects depending on myIsPublished flag
515 theGen->ClearCommands();
517 // reorder commands after conversion
518 list< Handle(_pyCommand) >::iterator cmd;
521 orderChanges = false;
522 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
523 if ( (*cmd)->SetDependentCmdsAfter() )
525 } while ( orderChanges );
527 // concat commands back into a script
528 TCollection_AsciiString aPrevCmd;
529 set<_pyID> createdObjects;
530 createdObjects.insert( "smeshBuilder" );
531 createdObjects.insert( "smesh" );
532 createdObjects.insert( "theStudy" );
533 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
535 #ifdef DUMP_CONVERSION
536 MESSAGE_ADD ( "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << std::endl );
538 if ( !(*cmd)->IsEmpty() && aPrevCmd != (*cmd)->GetString()) {
539 CheckObjectPresence( *cmd, createdObjects );
540 if ( !(*cmd)->IsEmpty() ) {
541 aPrevCmd = (*cmd)->GetString();
542 theScriptLines.push_back( aPrevCmd );
551 //================================================================================
553 * \brief _pyGen constructor
555 //================================================================================
557 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
558 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
559 std::set< TCollection_AsciiString >& theRemovedObjIDs,
560 SALOMEDS::Study_ptr& theStudy,
561 const bool theToKeepAllCommands)
562 : _pyObject( new _pyCommand( "", 0 )),
564 myID2AccessorMethod( theEntry2AccessorMethod ),
565 myObjectNames( theObjectNames ),
566 myRemovedObjIDs( theRemovedObjIDs ),
568 myToKeepAllCommands( theToKeepAllCommands ),
569 myStudy( SALOMEDS::Study::_duplicate( theStudy )),
570 myGeomIDNb(0), myGeomIDIndex(-1)
572 // make that GetID() to return TPythonDump::SMESHGenName()
573 GetCreationCmd()->Clear();
574 GetCreationCmd()->GetString() = TPythonDump::SMESHGenName();
575 GetCreationCmd()->GetString() += "=";
577 // Find 1st digit of study entry by which a GEOM object differs from a SMESH object
578 if ( !theObjectNames.IsEmpty() && !CORBA::is_nil( theStudy ))
582 SALOMEDS::SComponent_wrap geomComp = theStudy->FindComponent("GEOM");
583 if ( geomComp->_is_nil() ) return;
584 CORBA::String_var entry = geomComp->GetID();
587 // find a SMESH entry
589 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString e2n( theObjectNames );
590 for ( ; e2n.More() && smeshID.IsEmpty(); e2n.Next() )
591 if ( _pyCommand::IsStudyEntry( e2n.Key() ))
594 // find 1st difference between smeshID and geomID
595 if ( !geomID.IsEmpty() && !smeshID.IsEmpty() )
596 for ( int i = 1; i <= geomID.Length() && i <= smeshID.Length(); ++i )
597 if ( geomID.Value( i ) != smeshID.Value( i ))
599 myGeomIDNb = geomID.Value( i );
605 //================================================================================
607 * \brief name of SMESH_Gen in smeshBuilder.py
609 //================================================================================
611 const char* _pyGen::AccessorMethod() const
613 return SMESH_2smeshpy::GenName();
616 //================================================================================
618 * \brief Convert a command using a specific converter
619 * \param theCommand - the command to convert
621 //================================================================================
623 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
625 // store theCommand in the sequence
626 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
628 Handle(_pyCommand) aCommand = myCommands.back();
629 #ifdef DUMP_CONVERSION
630 MESSAGE ( "## COM " << myNbCommands << ": "<< aCommand->GetString() );
633 const _pyID& objID = aCommand->GetObject();
635 if ( objID.IsEmpty() )
638 // Prevent moving a command creating a sub-mesh to the end of the script
639 // if the sub-mesh is used in theCommand as argument
640 // if ( _pySubMesh::CanBeArgOfMethod( aCommand->GetMethod() ))
642 // PlaceSubmeshAfterItsCreation( aCommand );
645 // Method( SMESH.PointStruct(x,y,z)... -> Method( [x,y,z]...
646 StructToList( aCommand );
648 const TCollection_AsciiString& method = aCommand->GetMethod();
650 // not to erase _pySelfEraser's etc. used as args in some commands
652 #ifdef USE_STRING_FAMILY
653 std::list<_pyID> objIDs;
654 if ( myKeepAgrCmdsIDs.IsInArgs( aCommand, objIDs ))
656 std::list<_pyID>::iterator objID = objIDs.begin();
657 for ( ; objID != objIDs.end(); ++objID )
659 Handle(_pyObject) obj = FindObject( *objID );
662 obj->AddArgCmd( aCommand );
663 //cout << objID << " found in " << theCommand << endl;
668 std::list< _pyID >::const_iterator id = myKeepAgrCmdsIDs.begin();
669 for ( ; id != myKeepAgrCmdsIDs.end(); ++id )
670 if ( *id != objID && theCommand.Search( *id ) > id->Length() )
672 Handle(_pyObject) obj = FindObject( *id );
674 obj->AddArgCmd( aCommand );
679 // Find an object to process theCommand
682 if ( objID == this->GetID() || objID == SMESH_2smeshpy::GenName())
684 this->Process( aCommand );
685 //addFilterUser( aCommand, theGen ); // protect filters from clearing
689 // SMESH_Mesh method?
690 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
691 if ( id_mesh != myMeshes.end() )
693 //id_mesh->second->AddProcessedCmd( aCommand );
695 // check for mesh editor object
696 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
697 _pyID editorID = aCommand->GetResultValue();
698 Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
699 myMeshEditors.insert( make_pair( editorID, editor ));
702 // check for SubMesh objects
703 else if ( aCommand->GetMethod() == "GetSubMesh" ) { // SubMesh creation
704 _pyID subMeshID = aCommand->GetResultValue();
705 Handle(_pySubMesh) subMesh = new _pySubMesh( aCommand );
706 AddObject( subMesh );
709 // Method( mesh.GetIDSource([id1,id2]) -> Method( [id1,id2]
710 GetIDSourceToList( aCommand );
712 //addFilterUser( aCommand, theGen ); // protect filters from clearing
714 id_mesh->second->Process( aCommand );
715 id_mesh->second->AddProcessedCmd( aCommand );
719 // SMESH_MeshEditor method?
720 map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
721 if ( id_editor != myMeshEditors.end() )
723 // Method( mesh.GetIDSource([id1,id2]) -> Method( [id1,id2]
724 GetIDSourceToList( aCommand );
726 //addFilterUser( aCommand, theGen ); // protect filters from clearing
728 // some commands of SMESH_MeshEditor create meshes and groups
729 _pyID meshID, groups;
730 if ( method.Search("MakeMesh") != -1 )
731 meshID = aCommand->GetResultValue();
732 else if ( method == "MakeBoundaryMesh")
733 meshID = aCommand->GetResultValue(1);
734 else if ( method == "MakeBoundaryElements")
735 meshID = aCommand->GetResultValue(2);
737 if ( method.Search("MakeGroups") != -1 ||
738 method == "ExtrusionAlongPathX" ||
739 method == "ExtrusionAlongPathObjX" ||
740 method == "DoubleNodeGroupNew" ||
741 method == "DoubleNodeGroupsNew" ||
742 method == "DoubleNodeElemGroupNew" ||
743 method == "DoubleNodeElemGroupsNew"||
744 method == "DoubleNodeElemGroup2New"||
745 method == "DoubleNodeElemGroups2New"
747 groups = aCommand->GetResultValue();
748 else if ( method == "MakeBoundaryMesh" )
749 groups = aCommand->GetResultValue(2);
750 else if ( method == "MakeBoundaryElements")
751 groups = aCommand->GetResultValue(3);
752 else if ( method == "Create0DElementsOnAllNodes" &&
753 aCommand->GetArg(2).Length() > 2 ) // group name != ''
754 groups = aCommand->GetResultValue();
756 id_editor->second->Process( aCommand );
757 id_editor->second->AddProcessedCmd( aCommand );
760 if ( !meshID.IsEmpty() &&
761 !myMeshes.count( meshID ) &&
762 aCommand->IsStudyEntry( meshID ))
764 _AString processedCommand = aCommand->GetString();
765 Handle(_pyMesh) mesh = new _pyMesh( aCommand, meshID );
766 CheckObjectIsReCreated( mesh );
767 myMeshes.insert( make_pair( meshID, mesh ));
769 aCommand->GetString() = processedCommand; // discard changes made by _pyMesh
772 if ( !groups.IsEmpty() )
774 if ( !aCommand->IsStudyEntry( meshID ))
775 meshID = id_editor->second->GetMesh();
776 Handle(_pyMesh) mesh = myMeshes[ meshID ];
778 list< _pyID > idList = aCommand->GetStudyEntries( groups );
779 list< _pyID >::iterator grID = idList.begin();
780 for ( ; grID != idList.end(); ++grID )
781 if ( !myObjects.count( *grID ))
783 Handle(_pyGroup) group = new _pyGroup( aCommand, *grID );
785 if ( !mesh.IsNull() ) mesh->AddGroup( group );
789 } // SMESH_MeshEditor methods
791 // SMESH_Hypothesis method?
792 Handle(_pyHypothesis) hyp = FindHyp( objID );
793 if ( !hyp.IsNull() && !hyp->IsAlgo() )
795 hyp->Process( aCommand );
796 hyp->AddProcessedCmd( aCommand );
800 // aFilterManager.CreateFilter() ?
801 if ( aCommand->GetMethod() == "CreateFilter" )
803 // Set a more human readable name to a filter
804 // aFilter0x7fbf6c71cfb0 -> aFilter_nb
805 _pyID newID, filterID = aCommand->GetResultValue();
806 int pos = filterID.Search( "0x" );
808 newID = (filterID.SubString(1,pos-1) + "_") + _pyID( ++myNbFilters );
810 Handle(_pyObject) filter( new _pyFilter( aCommand, newID ));
813 // aFreeNodes0x5011f80 = aFilterManager.CreateFreeNodes() ## issue 0020976
814 else if ( theCommand.Search( "aFilterManager.Create" ) > 0 )
816 // create _pySelfEraser for functors
817 Handle(_pySelfEraser) functor = new _pySelfEraser( aCommand );
818 functor->IgnoreOwnCalls(); // to erase if not used as an argument
819 AddObject( functor );
822 // other object method?
823 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.find( objID );
824 if ( id_obj != myObjects.end() ) {
825 id_obj->second->Process( aCommand );
826 id_obj->second->AddProcessedCmd( aCommand );
830 // Add access to a wrapped mesh
831 AddMeshAccessorMethod( aCommand );
833 // Add access to a wrapped algorithm
834 // AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
836 // PAL12227. PythonDump was not updated at proper time; result is
837 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
838 // TypeError: __init__() takes exactly 11 arguments (10 given)
839 const char wrongCommand[] = "SMESH.Filter.Criterion(";
840 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
842 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
843 // there must be 10 arguments, 5-th arg ThresholdID is missing,
844 const int wrongNbArgs = 9, missingArg = 5;
845 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
847 for ( int i = wrongNbArgs; i > missingArg; --i )
848 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
849 tmpCmd.SetArg( missingArg, "''");
850 aCommand->GetString().Trunc( beg - 1 );
851 aCommand->GetString() += tmpCmd.GetString();
854 // set GetCriterion(elementType,CritType,Compare,Treshold,UnaryOp,BinaryOp,Tolerance)
856 // instead of "SMESH.Filter.Criterion(
857 // Type,Compare,Threshold,ThresholdStr,ThresholdID,UnaryOp,BinaryOp,Tolerance,TypeOfElement,Precision)
858 // 1 2 3 4 5 6 7 8 9 10
859 // in order to avoid the problem of type mismatch of long and FunctorType
860 const TCollection_AsciiString
861 SMESH("SMESH."), dfltFunctor("SMESH.FT_Undefined"), dfltTol("1e-07"), dfltPreci("-1");
862 TCollection_AsciiString
863 Type = aCommand->GetArg(1), // long
864 Compare = aCommand->GetArg(2), // long
865 Threshold = aCommand->GetArg(3), // double
866 ThresholdStr = aCommand->GetArg(4), // string
867 ThresholdID = aCommand->GetArg(5), // string
868 UnaryOp = aCommand->GetArg(6), // long
869 BinaryOp = aCommand->GetArg(7), // long
870 Tolerance = aCommand->GetArg(8), // double
871 TypeOfElement = aCommand->GetArg(9), // ElementType
872 Precision = aCommand->GetArg(10); // long
873 fixFunctorType( Type, Compare, UnaryOp, BinaryOp );
874 Type = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( Type.IntegerValue() ));
875 Compare = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( Compare.IntegerValue() ));
876 UnaryOp = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( UnaryOp.IntegerValue() ));
877 BinaryOp = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( BinaryOp.IntegerValue() ));
879 if ( Compare == "SMESH.FT_EqualTo" )
882 aCommand->RemoveArgs();
883 aCommand->SetObject( SMESH_2smeshpy::GenName() );
884 aCommand->SetMethod( "GetCriterion" );
886 aCommand->SetArg( 1, TypeOfElement );
887 aCommand->SetArg( 2, Type );
888 aCommand->SetArg( 3, Compare );
890 if ( Threshold.IsIntegerValue() )
892 int iGeom = Threshold.IntegerValue();
893 if ( Type == "SMESH.FT_ElemGeomType" )
895 // set SMESH.GeometryType instead of a numerical Threshold
896 const int nbTypes = SMESH::Geom_LAST;
897 const char* types[nbTypes] = {
898 "Geom_POINT", "Geom_EDGE", "Geom_TRIANGLE", "Geom_QUADRANGLE", "Geom_POLYGON",
899 "Geom_TETRA", "Geom_PYRAMID", "Geom_HEXA", "Geom_PENTA", "Geom_HEXAGONAL_PRISM",
900 "Geom_POLYHEDRA", "Geom_BALL" };
901 if ( -1 < iGeom && iGeom < nbTypes )
902 Threshold = SMESH + types[ iGeom ];
904 // is types complete? (compilation failure mains that enum GeometryType changed)
905 int _assert[( sizeof(types) / sizeof(const char*) == nbTypes ) ? 1 : -1 ];
908 if (Type == "SMESH.FT_EntityType")
910 // set SMESH.EntityType instead of a numerical Threshold
911 const int nbTypes = SMESH::Entity_Last;
912 const char* types[nbTypes] = {
913 "Entity_Node", "Entity_0D", "Entity_Edge", "Entity_Quad_Edge",
914 "Entity_Triangle", "Entity_Quad_Triangle", "Entity_BiQuad_Triangle",
915 "Entity_Quadrangle", "Entity_Quad_Quadrangle", "Entity_BiQuad_Quadrangle",
916 "Entity_Polygon", "Entity_Quad_Polygon", "Entity_Tetra", "Entity_Quad_Tetra",
917 "Entity_Pyramid", "Entity_Quad_Pyramid",
918 "Entity_Hexa", "Entity_Quad_Hexa", "Entity_TriQuad_Hexa",
919 "Entity_Penta", "Entity_Quad_Penta", "Entity_Hexagonal_Prism",
920 "Entity_Polyhedra", "Entity_Quad_Polyhedra", "Entity_Ball" };
921 if ( -1 < iGeom && iGeom < nbTypes )
922 Threshold = SMESH + types[ iGeom ];
924 // is types complete? (compilation failure mains that enum EntityType changed)
925 int _assert[( sizeof(types) / sizeof(const char*) == nbTypes ) ? 1 : -1 ];
929 if ( ThresholdID.Length() != 2 ) // neither '' nor ""
930 aCommand->SetArg( 4, ThresholdID.SubString( 2, ThresholdID.Length()-1 )); // shape entry
931 else if ( ThresholdStr.Length() != 2 )
932 aCommand->SetArg( 4, ThresholdStr );
933 else if ( ThresholdID.Length() != 2 )
934 aCommand->SetArg( 4, ThresholdID );
936 aCommand->SetArg( 4, Threshold );
937 // find the last not default arg
939 if ( Tolerance == dfltTol ) {
941 if ( BinaryOp == dfltFunctor ) {
943 if ( UnaryOp == dfltFunctor )
947 if ( 5 < lastDefault ) aCommand->SetArg( 5, UnaryOp );
948 if ( 6 < lastDefault ) aCommand->SetArg( 6, BinaryOp );
949 if ( 7 < lastDefault ) aCommand->SetArg( 7, Tolerance );
950 if ( Precision != dfltPreci )
952 TCollection_AsciiString crit = aCommand->GetResultValue();
953 aCommand->GetString() += "; ";
954 aCommand->GetString() += crit + ".Precision = " + Precision;
960 //================================================================================
962 * \brief Convert the command or remember it for later conversion
963 * \param theCommand - The python command calling a method of SMESH_Gen
965 //================================================================================
967 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
969 // there are methods to convert:
970 // CreateMesh( shape )
971 // Concatenate( [mesh1, ...], ... )
972 // CreateHypothesis( theHypType, theLibName )
973 // Compute( mesh, geom )
974 // Evaluate( mesh, geom )
976 TCollection_AsciiString method = theCommand->GetMethod();
978 if ( method == "CreateMesh" || method == "CreateEmptyMesh")
980 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
984 if ( method == "CreateMeshesFromUNV" ||
985 method == "CreateMeshesFromSTL" ||
986 method == "CopyMesh" ) // command result is a mesh
988 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
992 if( method == "CreateMeshesFromMED" ||
993 method == "CreateMeshesFromSAUV"||
994 method == "CreateMeshesFromCGNS" ||
995 method == "CreateMeshesFromGMF" ) // command result is ( [mesh1,mesh2], status )
997 std::list< _pyID > meshIDs = theCommand->GetStudyEntries( theCommand->GetResultValue() );
998 std::list< _pyID >::iterator meshID = meshIDs.begin();
999 for ( ; meshID != meshIDs.end(); ++meshID )
1001 Handle(_pyMesh) mesh = new _pyMesh( theCommand, *meshID );
1004 if ( method == "CreateMeshesFromGMF" )
1006 // CreateMeshesFromGMF( theFileName, theMakeRequiredGroups ) ->
1007 // CreateMeshesFromGMF( theFileName )
1008 _AString file = theCommand->GetArg(1);
1009 theCommand->RemoveArgs();
1010 theCommand->SetArg( 1, file );
1014 // CreateHypothesis()
1015 if ( method == "CreateHypothesis" )
1017 // issue 199929, remove standard library name (default parameter)
1018 const TCollection_AsciiString & aLibName = theCommand->GetArg( 2 );
1019 if ( aLibName.Search( "StdMeshersEngine" ) != -1 ) {
1020 // keep the first argument
1021 TCollection_AsciiString arg = theCommand->GetArg( 1 );
1022 theCommand->RemoveArgs();
1023 theCommand->SetArg( 1, arg );
1026 Handle(_pyHypothesis) hyp = _pyHypothesis::NewHypothesis( theCommand );
1027 CheckObjectIsReCreated( hyp );
1028 myHypos.insert( make_pair( hyp->GetID(), hyp ));
1033 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
1034 if ( method == "Compute" )
1036 const _pyID& meshID = theCommand->GetArg( 1 );
1037 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
1038 if ( id_mesh != myMeshes.end() ) {
1039 theCommand->SetObject( meshID );
1040 theCommand->RemoveArgs();
1041 id_mesh->second->Process( theCommand );
1042 id_mesh->second->AddProcessedCmd( theCommand );
1047 // smeshgen.Evaluate( mesh, geom ) --> mesh.Evaluate(geom)
1048 if ( method == "Evaluate" )
1050 const _pyID& meshID = theCommand->GetArg( 1 );
1051 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
1052 if ( id_mesh != myMeshes.end() ) {
1053 theCommand->SetObject( meshID );
1054 _pyID geom = theCommand->GetArg( 2 );
1055 theCommand->RemoveArgs();
1056 theCommand->SetArg( 1, geom );
1057 id_mesh->second->AddProcessedCmd( theCommand );
1062 // objects erasing creation command if no more its commands invoked:
1063 // SMESH_Pattern, FilterManager
1064 if ( method == "GetPattern" ||
1065 method == "CreateFilterManager" ||
1066 method == "CreateMeasurements" )
1068 Handle(_pyObject) obj = new _pySelfEraser( theCommand );
1069 if ( !AddObject( obj ) )
1070 theCommand->Clear(); // already created
1072 // Concatenate( [mesh1, ...], ... )
1073 else if ( method == "Concatenate" || method == "ConcatenateWithGroups")
1075 if ( method == "ConcatenateWithGroups" ) {
1076 theCommand->SetMethod( "Concatenate" );
1077 theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
1079 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
1081 AddMeshAccessorMethod( theCommand );
1083 else if ( method == "SetName" ) // SetName(obj,name)
1085 // store theCommand as one of object commands to erase it along with the object
1086 const _pyID& objID = theCommand->GetArg( 1 );
1087 Handle(_pyObject) obj = FindObject( objID );
1088 if ( !obj.IsNull() )
1089 obj->AddProcessedCmd( theCommand );
1092 // Replace name of SMESH_Gen
1094 // names of SMESH_Gen methods fully equal to methods defined in smeshBuilder.py
1095 static TStringSet smeshpyMethods;
1096 if ( smeshpyMethods.empty() ) {
1097 const char * names[] =
1098 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
1099 "GetPattern","GetSubShapesId",
1100 "" }; // <- mark of array end
1101 smeshpyMethods.Insert( names );
1103 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
1104 // smeshgen.Method() --> smesh.Method()
1105 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
1107 // smeshgen.Method() --> smesh.Method()
1108 theCommand->SetObject( SMESH_2smeshpy::GenName() );
1111 //================================================================================
1113 * \brief Convert the remembered commands
1115 //================================================================================
1117 void _pyGen::Flush()
1119 // create an empty command
1120 myLastCommand = new _pyCommand();
1122 map< _pyID, Handle(_pyMesh) >::iterator id_mesh;
1123 map< _pyID, Handle(_pyObject) >::iterator id_obj;
1124 map< _pyID, Handle(_pyHypothesis) >::iterator id_hyp;
1126 if ( IsToKeepAllCommands() ) // historical dump
1128 // set myIsPublished = true to all objects
1129 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
1130 id_mesh->second->SetRemovedFromStudy( false );
1131 for ( id_hyp = myHypos.begin(); id_hyp != myHypos.end(); ++id_hyp )
1132 id_hyp->second->SetRemovedFromStudy( false );
1133 for ( id_obj = myObjects.begin(); id_obj != myObjects.end(); ++id_obj )
1134 id_obj->second->SetRemovedFromStudy( false );
1138 // let hypotheses find referred objects in order to prevent clearing
1139 // not published referred hyps (it's needed for hyps like "LayerDistribution")
1140 list< Handle(_pyMesh) > fatherMeshes;
1141 for ( id_hyp = myHypos.begin(); id_hyp != myHypos.end(); ++id_hyp )
1142 if ( !id_hyp->second.IsNull() )
1143 id_hyp->second->GetReferredMeshesAndGeom( fatherMeshes );
1145 // set myIsPublished = false to all objects depending on
1146 // meshes built on a removed geometry
1147 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
1148 if ( id_mesh->second->IsNotGeomPublished() )
1149 id_mesh->second->SetRemovedFromStudy( true );
1152 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
1153 if ( ! id_mesh->second.IsNull() )
1154 id_mesh->second->Flush();
1157 for ( id_hyp = myHypos.begin(); id_hyp != myHypos.end(); ++id_hyp )
1158 if ( !id_hyp->second.IsNull() ) {
1159 id_hyp->second->Flush();
1160 // smeshgen.CreateHypothesis() --> smesh.CreateHypothesis()
1161 if ( !id_hyp->second->IsWrapped() )
1162 id_hyp->second->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
1165 // Flush other objects. 2 times, for objects depending on Flush() of later created objects
1166 std::list< Handle(_pyObject) >::reverse_iterator robj = myOrderedObjects.rbegin();
1167 for ( ; robj != myOrderedObjects.rend(); ++robj )
1168 if ( ! robj->IsNull() )
1170 std::list< Handle(_pyObject) >::iterator obj = myOrderedObjects.begin();
1171 for ( ; obj != myOrderedObjects.end(); ++obj )
1172 if ( ! obj->IsNull() )
1175 myLastCommand->SetOrderNb( ++myNbCommands );
1176 myCommands.push_back( myLastCommand );
1179 //================================================================================
1181 * \brief Prevent moving a command creating a sub-mesh to the end of the script
1182 * if the sub-mesh is used in theCmdUsingSubmesh as argument
1184 //================================================================================
1186 void _pyGen::PlaceSubmeshAfterItsCreation( Handle(_pyCommand) theCmdUsingSubmesh ) const
1188 // map< _pyID, Handle(_pyObject) >::const_iterator id_obj = myObjects.begin();
1189 // for ( ; id_obj != myObjects.end(); ++id_obj )
1191 // if ( !id_obj->second->IsKind( STANDARD_TYPE( _pySubMesh ))) continue;
1192 // for ( int iArg = theCmdUsingSubmesh->GetNbArgs(); iArg; --iArg )
1194 // const _pyID& arg = theCmdUsingSubmesh->GetArg( iArg );
1195 // if ( arg.IsEmpty() || arg.Value( 1 ) == '"' || arg.Value( 1 ) == '\'' )
1197 // list< _pyID > idList = theCmdUsingSubmesh->GetStudyEntries( arg );
1198 // list< _pyID >::iterator id = idList.begin();
1199 // for ( ; id != idList.end(); ++id )
1200 // if ( id_obj->first == *id )
1201 // // _pySubMesh::Process() does what we need
1202 // Handle(_pySubMesh)::DownCast( id_obj->second )->Process( theCmdUsingSubmesh );
1207 //================================================================================
1209 * \brief Clean commmands of removed objects depending on myIsPublished flag
1211 //================================================================================
1213 void _pyGen::ClearCommands()
1215 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
1216 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
1217 id_mesh->second->ClearCommands();
1219 map< _pyID, Handle(_pyHypothesis) >::iterator id_hyp = myHypos.begin();
1220 for ( ; id_hyp != myHypos.end(); ++id_hyp )
1221 if ( !id_hyp->second.IsNull() )
1222 id_hyp->second->ClearCommands();
1224 // Other objects. 2 times, for objects depending on ClearCommands() of later created objects
1225 std::list< Handle(_pyObject) >::reverse_iterator robj = myOrderedObjects.rbegin();
1226 for ( ; robj != myOrderedObjects.rend(); ++robj )
1227 if ( ! robj->IsNull() )
1228 (*robj)->ClearCommands();
1229 std::list< Handle(_pyObject) >::iterator obj = myOrderedObjects.begin();
1230 for ( ; obj != myOrderedObjects.end(); ++obj )
1231 if ( ! obj->IsNull() )
1232 (*obj)->ClearCommands();
1235 //================================================================================
1237 * \brief Release mutual handles of objects
1239 //================================================================================
1243 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
1244 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
1245 id_mesh->second->Free();
1248 map< _pyID, Handle(_pyMeshEditor) >::iterator id_ed = myMeshEditors.begin();
1249 for ( ; id_ed != myMeshEditors.end(); ++id_ed )
1250 id_ed->second->Free();
1251 myMeshEditors.clear();
1253 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
1254 for ( ; id_obj != myObjects.end(); ++id_obj )
1255 id_obj->second->Free();
1258 map< _pyID, Handle(_pyHypothesis) >::iterator id_hyp = myHypos.begin();
1259 for ( ; id_hyp != myHypos.end(); ++id_hyp )
1260 if ( !id_hyp->second.IsNull() )
1261 id_hyp->second->Free();
1264 myFile2ExportedMesh.clear();
1266 //myKeepAgrCmdsIDs.Print();
1269 //================================================================================
1271 * \brief Add access method to mesh that is an argument
1272 * \param theCmd - command to add access method
1273 * \retval bool - true if added
1275 //================================================================================
1277 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
1280 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
1281 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
1282 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
1288 //================================================================================
1290 * \brief Add access method to algo that is an object or an argument
1291 * \param theCmd - command to add access method
1292 * \retval bool - true if added
1294 //================================================================================
1296 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
1299 map< _pyID, Handle(_pyHypothesis) >::const_iterator id_hyp = myHypos.begin();
1300 for ( ; id_hyp != myHypos.end(); ++id_hyp )
1301 if ( !id_hyp->second.IsNull() &&
1302 id_hyp->second->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
1303 theCmd->AddAccessorMethod( id_hyp->second->GetID(),
1304 id_hyp->second->AccessorMethod() ))
1310 //================================================================================
1312 * \brief Find hypothesis by ID (entry)
1313 * \param theHypID - The hypothesis ID
1314 * \retval Handle(_pyHypothesis) - The found hypothesis
1316 //================================================================================
1318 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
1320 map< _pyID, Handle(_pyHypothesis) >::iterator id_hyp = myHypos.find( theHypID );
1321 if ( id_hyp != myHypos.end() &&
1322 !id_hyp->second.IsNull() &&
1323 theHypID == id_hyp->second->GetID() )
1324 return id_hyp->second;
1325 return Handle(_pyHypothesis)();
1328 //================================================================================
1330 * \brief Find algorithm able to create a hypothesis
1331 * \param theGeom - The shape ID the algorithm was created on
1332 * \param theMesh - The mesh ID that created the algorithm
1333 * \param theHypothesis - The hypothesis the algorithm sould be able to create
1334 * \retval Handle(_pyHypothesis) - The found algo
1336 //================================================================================
1338 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
1339 const Handle(_pyHypothesis)& theHypothesis )
1341 map< _pyID, Handle(_pyHypothesis) >::iterator id_hyp = myHypos.begin();
1342 for ( ; id_hyp != myHypos.end(); ++id_hyp )
1343 if ( !id_hyp->second.IsNull() &&
1344 id_hyp->second->IsAlgo() &&
1345 theHypothesis->CanBeCreatedBy( id_hyp->second->GetAlgoType() ) &&
1346 id_hyp->second->GetGeom() == theGeom &&
1347 id_hyp->second->GetMesh() == theMesh )
1348 return id_hyp->second;
1349 return Handle(_pyHypothesis)();
1352 //================================================================================
1354 * \brief Find subMesh by ID (entry)
1355 * \param theSubMeshID - The subMesh ID
1356 * \retval Handle(_pySubMesh) - The found subMesh
1358 //================================================================================
1360 Handle(_pySubMesh) _pyGen::FindSubMesh( const _pyID& theSubMeshID )
1362 map< _pyID, Handle(_pyObject) >::iterator id_subMesh = myObjects.find(theSubMeshID);
1363 if ( id_subMesh != myObjects.end() )
1364 return Handle(_pySubMesh)::DownCast( id_subMesh->second );
1365 return Handle(_pySubMesh)();
1369 //================================================================================
1371 * \brief Change order of commands in the script
1372 * \param theCmd1 - One command
1373 * \param theCmd2 - Another command
1375 //================================================================================
1377 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
1379 list< Handle(_pyCommand) >::iterator pos1, pos2;
1380 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
1381 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
1382 myCommands.insert( pos1, theCmd2 );
1383 myCommands.insert( pos2, theCmd1 );
1384 myCommands.erase( pos1 );
1385 myCommands.erase( pos2 );
1387 int nb1 = theCmd1->GetOrderNb();
1388 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
1389 theCmd2->SetOrderNb( nb1 );
1390 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
1391 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
1394 //================================================================================
1396 * \brief Set one command after the other
1397 * \param theCmd - Command to move
1398 * \param theAfterCmd - Command ater which to insert the first one
1400 //================================================================================
1402 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
1404 setNeighbourCommand( theCmd, theAfterCmd, true );
1407 //================================================================================
1409 * \brief Set one command before the other
1410 * \param theCmd - Command to move
1411 * \param theBeforeCmd - Command before which to insert the first one
1413 //================================================================================
1415 void _pyGen::SetCommandBefore( Handle(_pyCommand) theCmd, Handle(_pyCommand) theBeforeCmd )
1417 setNeighbourCommand( theCmd, theBeforeCmd, false );
1420 //================================================================================
1422 * \brief Set one command before or after the other
1423 * \param theCmd - Command to move
1424 * \param theOtherCmd - Command ater or before which to insert the first one
1426 //================================================================================
1428 void _pyGen::setNeighbourCommand( Handle(_pyCommand)& theCmd,
1429 Handle(_pyCommand)& theOtherCmd,
1430 const bool theIsAfter )
1432 list< Handle(_pyCommand) >::iterator pos;
1433 pos = find( myCommands.begin(), myCommands.end(), theCmd );
1434 myCommands.erase( pos );
1435 pos = find( myCommands.begin(), myCommands.end(), theOtherCmd );
1436 myCommands.insert( (theIsAfter ? ++pos : pos), theCmd );
1439 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
1440 (*pos)->SetOrderNb( i++ );
1443 //================================================================================
1445 * \brief Call _pyFilter.AddUser() if a filter is used as a command arg
1447 //================================================================================
1449 // void _pyGen::addFilterUser( Handle(_pyCommand)& theCommand, const Handle(_pyObject)& user )
1451 // No more needed after adding _pyObject::myArgCommands
1453 // const char filterPrefix[] = "aFilter0x";
1454 // if ( theCommand->GetString().Search( filterPrefix ) < 1 )
1457 // for ( int i = theCommand->GetNbArgs(); i > 0; --i )
1459 // const _AString & arg = theCommand->GetArg( i );
1460 // // NOT TREATED CASE: arg == "[something, aFilter0x36a2f60]"
1461 // if ( arg.Search( filterPrefix ) != 1 )
1464 // Handle(_pyFilter) filter = Handle(_pyFilter)::DownCast( FindObject( arg ));
1465 // if ( !filter.IsNull() )
1467 // filter->AddUser( user );
1468 // if ( !filter->GetNewID().IsEmpty() )
1469 // theCommand->SetArg( i, filter->GetNewID() );
1474 //================================================================================
1476 * \brief Set command be last in list of commands
1477 * \param theCmd - Command to be last
1479 //================================================================================
1481 Handle(_pyCommand)& _pyGen::GetLastCommand()
1483 return myLastCommand;
1486 //================================================================================
1488 * \brief Set method to access to object wrapped with python class
1489 * \param theID - The wrapped object entry
1490 * \param theMethod - The accessor method
1492 //================================================================================
1494 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
1496 myID2AccessorMethod.Bind( theID, (char*) theMethod );
1499 //================================================================================
1501 * \brief Generated new ID for object and assign with existing name
1502 * \param theID - ID of existing object
1504 //================================================================================
1506 _pyID _pyGen::GenerateNewID( const _pyID& theID )
1511 aNewID = theID + _pyID( ":" ) + _pyID( index++ );
1513 while ( myObjectNames.IsBound( aNewID ) );
1515 myObjectNames.Bind( aNewID, myObjectNames.IsBound( theID )
1516 ? (myObjectNames.Find( theID ) + _pyID( "_" ) + _pyID( index-1 ))
1517 : _pyID( "A" ) + aNewID );
1521 //================================================================================
1523 * \brief Stores theObj in myObjects
1525 //================================================================================
1527 bool _pyGen::AddObject( Handle(_pyObject)& theObj )
1529 if ( theObj.IsNull() ) return false;
1531 CheckObjectIsReCreated( theObj );
1535 if ( theObj->IsKind( STANDARD_TYPE( _pyMesh ))) {
1536 add = myMeshes.insert( make_pair( theObj->GetID(),
1537 Handle(_pyMesh)::DownCast( theObj ))).second;
1539 else if ( theObj->IsKind( STANDARD_TYPE( _pyMeshEditor ))) {
1540 add = myMeshEditors.insert( make_pair( theObj->GetID(),
1541 Handle(_pyMeshEditor)::DownCast( theObj ))).second;
1544 add = myObjects.insert( make_pair( theObj->GetID(), theObj )).second;
1545 if ( add ) myOrderedObjects.push_back( theObj );
1550 //================================================================================
1552 * \brief Erases an existing object with the same ID. This method should be called
1553 * before storing theObj in _pyGen
1555 //================================================================================
1557 void _pyGen::CheckObjectIsReCreated( Handle(_pyObject)& theObj )
1559 if ( theObj.IsNull() || !_pyCommand::IsStudyEntry( theObj->GetID() ))
1562 const bool isHyp = theObj->IsKind( STANDARD_TYPE( _pyHypothesis ));
1563 Handle(_pyObject) existing =
1564 isHyp ? FindHyp( theObj->GetID() ) : FindObject( theObj->GetID() );
1565 if ( !existing.IsNull() && existing != theObj )
1567 existing->SetRemovedFromStudy( true );
1568 existing->ClearCommands();
1571 if ( myHypos.count( theObj->GetID() ))
1572 myHypos.erase( theObj->GetID() );
1574 else if ( myMeshes.count( theObj->GetID() ))
1576 myMeshes.erase( theObj->GetID() );
1578 else if ( myObjects.count( theObj->GetID() ))
1580 myObjects.erase( theObj->GetID() );
1585 //================================================================================
1587 * \brief Re-register an object with other ID to make it Process() commands of
1588 * other object having this ID
1590 //================================================================================
1592 void _pyGen::SetProxyObject( const _pyID& theID, Handle(_pyObject)& theObj )
1594 if ( theObj.IsNull() ) return;
1596 if ( theObj->IsKind( STANDARD_TYPE( _pyMesh )))
1597 myMeshes.insert( make_pair( theID, Handle(_pyMesh)::DownCast( theObj )));
1599 else if ( theObj->IsKind( STANDARD_TYPE( _pyMeshEditor )))
1600 myMeshEditors.insert( make_pair( theID, Handle(_pyMeshEditor)::DownCast( theObj )));
1603 myObjects.insert( make_pair( theID, theObj ));
1606 //================================================================================
1608 * \brief Finds a _pyObject by ID
1610 //================================================================================
1612 Handle(_pyObject) _pyGen::FindObject( const _pyID& theObjID ) const
1615 map< _pyID, Handle(_pyObject) >::const_iterator id_obj = myObjects.find( theObjID );
1616 if ( id_obj != myObjects.end() )
1617 return id_obj->second;
1620 map< _pyID, Handle(_pyMesh) >::const_iterator id_obj = myMeshes.find( theObjID );
1621 if ( id_obj != myMeshes.end() )
1622 return id_obj->second;
1625 // map< _pyID, Handle(_pyMeshEditor) >::const_iterator id_obj = myMeshEditors.find( theObjID );
1626 // if ( id_obj != myMeshEditors.end() )
1627 // return id_obj->second;
1629 return Handle(_pyObject)();
1632 //================================================================================
1634 * \brief Check if a study entry is under GEOM component
1636 //================================================================================
1638 bool _pyGen::IsGeomObject(const _pyID& theObjID) const
1642 return ( myGeomIDIndex <= theObjID.Length() &&
1643 int( theObjID.Value( myGeomIDIndex )) == myGeomIDNb &&
1644 _pyCommand::IsStudyEntry( theObjID ));
1649 //================================================================================
1651 * \brief Returns true if an object is not present in a study
1653 //================================================================================
1655 bool _pyGen::IsNotPublished(const _pyID& theObjID) const
1657 if ( theObjID.IsEmpty() ) return false;
1659 if ( myObjectNames.IsBound( theObjID ))
1660 return false; // SMESH object is in study
1662 // either the SMESH object is not in study or it is a GEOM object
1663 if ( IsGeomObject( theObjID ))
1665 SALOMEDS::SObject_wrap so = myStudy->FindObjectID( theObjID.ToCString() );
1666 if ( so->_is_nil() ) return true;
1667 CORBA::Object_var obj = so->GetObject();
1668 return CORBA::is_nil( obj );
1670 return true; // SMESH object not in study
1673 //================================================================================
1675 * \brief Add an object to myRemovedObjIDs that leads to that SetName() for
1676 * this object is not dumped
1677 * \param [in] theObjID - entry of the object whose creation command was eliminated
1679 //================================================================================
1681 void _pyGen::ObjectCreationRemoved(const _pyID& theObjID)
1683 myRemovedObjIDs.insert( theObjID );
1686 //================================================================================
1688 * \brief Return reader of hypotheses of plugins
1690 //================================================================================
1692 Handle( _pyHypothesisReader ) _pyGen::GetHypothesisReader() const
1694 if (myHypReader.IsNull() )
1695 ((_pyGen*) this)->myHypReader = new _pyHypothesisReader;
1701 //================================================================================
1703 * \brief Mesh created by SMESH_Gen
1705 //================================================================================
1707 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd)
1708 : _pyObject( theCreationCmd ), myGeomNotInStudy( false )
1710 if ( theCreationCmd->GetMethod() == "CreateMesh" && theGen->IsNotPublished( GetGeom() ))
1711 myGeomNotInStudy = true;
1713 // convert my creation command --> smeshpy.Mesh(...)
1714 Handle(_pyCommand) creationCmd = GetCreationCmd();
1715 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
1716 creationCmd->SetMethod( "Mesh" );
1717 theGen->SetAccessorMethod( GetID(), _pyMesh::AccessorMethod() );
1720 //================================================================================
1722 * \brief Mesh created by SMESH_MeshEditor
1724 //================================================================================
1726 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd, const _pyID& meshId):
1727 _pyObject(theCreationCmd,meshId), myGeomNotInStudy(false )
1729 if ( theCreationCmd->MethodStartsFrom( "CreateMeshesFrom" ))
1731 // this mesh depends on the exported mesh
1732 const TCollection_AsciiString& file = theCreationCmd->GetArg( 1 );
1733 if ( !file.IsEmpty() )
1735 ExportedMeshData& exportData = theGen->FindExportedMesh( file );
1736 addFatherMesh( exportData.myMesh );
1737 if ( !exportData.myLastComputeCmd.IsNull() )
1739 // restore cleared Compute() by which the exported mesh was generated
1740 exportData.myLastComputeCmd->GetString() = exportData.myLastComputeCmdString;
1741 // protect that Compute() cmd from clearing
1742 if ( exportData.myMesh->myLastComputeCmd == exportData.myLastComputeCmd )
1743 exportData.myMesh->myLastComputeCmd.Nullify();
1747 else if ( theCreationCmd->MethodStartsFrom( "Concatenate" ))
1749 // this mesh depends on concatenated meshes
1750 const TCollection_AsciiString& meshIDs = theCreationCmd->GetArg( 1 );
1751 list< _pyID > idList = theCreationCmd->GetStudyEntries( meshIDs );
1752 list< _pyID >::iterator meshID = idList.begin();
1753 for ( ; meshID != idList.end(); ++meshID )
1754 addFatherMesh( *meshID );
1756 else if ( theCreationCmd->GetMethod() == "CopyMesh" )
1758 // this mesh depends on a copied IdSource
1759 const _pyID& objID = theCreationCmd->GetArg( 1 );
1760 addFatherMesh( objID );
1762 else if ( theCreationCmd->GetMethod().Search("MakeMesh") != -1 ||
1763 theCreationCmd->GetMethod() == "MakeBoundaryMesh" ||
1764 theCreationCmd->GetMethod() == "MakeBoundaryElements" )
1766 // this mesh depends on a source mesh
1767 // (theCreationCmd is already Process()ed by _pyMeshEditor)
1768 const _pyID& meshID = theCreationCmd->GetObject();
1769 addFatherMesh( meshID );
1772 // convert my creation command
1773 Handle(_pyCommand) creationCmd = GetCreationCmd();
1774 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
1775 theGen->SetAccessorMethod( meshId, _pyMesh::AccessorMethod() );
1778 //================================================================================
1780 * \brief Convert an IDL API command of SMESH::SMESH_Mesh to a method call of python Mesh
1781 * \param theCommand - Engine method called for this mesh
1783 //================================================================================
1785 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
1787 // some methods of SMESH_Mesh interface needs special conversion
1788 // to methods of Mesh python class
1790 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
1791 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
1792 // 2. AddHypothesis(geom, hyp)
1793 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
1794 // 3. CreateGroupFromGEOM(type, name, grp)
1795 // --> in Mesh.Group(grp, name="")
1796 // 4. ExportToMED(f, auto_groups, version)
1797 // --> in Mesh.ExportMED( f, auto_groups, version )
1800 const TCollection_AsciiString& method = theCommand->GetMethod();
1801 // ----------------------------------------------------------------------
1802 if ( method == "Compute" ) // in snapshot mode, clear the previous Compute()
1804 if ( !theGen->IsToKeepAllCommands() ) // !historical
1806 list< Handle(_pyHypothesis) >::iterator hyp;
1807 if ( !myLastComputeCmd.IsNull() )
1809 // check if the previously computed mesh has been edited,
1810 // if so then we do not clear the previous Compute()
1811 bool toClear = true;
1812 if ( myLastComputeCmd->GetMethod() == "Compute" )
1814 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
1815 for ( ; e != myEditors.end() && toClear; ++e )
1817 list< Handle(_pyCommand)>& cmds = (*e)->GetProcessedCmds();
1818 list< Handle(_pyCommand) >::reverse_iterator cmd = cmds.rbegin();
1819 if ( cmd != cmds.rend() &&
1820 (*cmd)->GetOrderNb() > myLastComputeCmd->GetOrderNb() )
1826 // clear hyp commands called before myLastComputeCmd
1827 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1828 (*hyp)->ComputeDiscarded( myLastComputeCmd );
1830 myLastComputeCmd->Clear();
1833 myLastComputeCmd = theCommand;
1835 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1836 (*hyp)->MeshComputed( myLastComputeCmd );
1840 // ----------------------------------------------------------------------
1841 else if ( method == "Clear" ) // in snapshot mode, clear all previous commands
1843 if ( !theGen->IsToKeepAllCommands() ) // !historical
1846 myChildMeshes.empty() ? 0 : myChildMeshes.back()->GetCreationCmd()->GetOrderNb();
1847 // list< Handle(_pyCommand) >::reverse_iterator cmd = myProcessedCmds.rbegin();
1848 // for ( ; cmd != myProcessedCmds.rend() && (*cmd)->GetOrderNb() > untilCmdNb; ++cmd )
1850 if ( !myLastComputeCmd.IsNull() )
1852 list< Handle(_pyHypothesis) >::iterator hyp;
1853 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1854 (*hyp)->ComputeDiscarded( myLastComputeCmd );
1856 myLastComputeCmd->Clear();
1859 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
1860 for ( ; e != myEditors.end(); ++e )
1862 list< Handle(_pyCommand)>& cmds = (*e)->GetProcessedCmds();
1863 list< Handle(_pyCommand) >::reverse_iterator cmd = cmds.rbegin();
1864 for ( ; cmd != cmds.rend() && (*cmd)->GetOrderNb() > untilCmdNb; ++cmd )
1865 if ( !(*cmd)->IsEmpty() )
1867 if ( (*cmd)->GetStudyEntries( (*cmd)->GetResultValue() ).empty() ) // no object created
1871 myLastComputeCmd = theCommand; // to clear Clear() the same way as Compute()
1874 // ----------------------------------------------------------------------
1875 else if ( method == "GetSubMesh" ) { // collect submeshes of the mesh
1876 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetResultValue() );
1877 if ( !subMesh.IsNull() ) {
1878 subMesh->SetCreator( this );
1879 mySubmeshes.push_back( subMesh );
1882 // ----------------------------------------------------------------------
1883 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
1884 myAddHypCmds.push_back( theCommand );
1886 const _pyID& hypID = theCommand->GetArg( 2 );
1887 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1888 if ( !hyp.IsNull() ) {
1889 myHypos.push_back( hyp );
1890 if ( hyp->GetMesh().IsEmpty() )
1891 hyp->SetMesh( this->GetID() );
1894 // ----------------------------------------------------------------------
1895 else if ( method == "CreateGroup" ||
1896 method == "CreateGroupFromGEOM" ||
1897 method == "CreateGroupFromFilter" )
1899 Handle(_pyGroup) group = new _pyGroup( theCommand );
1900 myGroups.push_back( group );
1901 theGen->AddObject( group );
1903 // ----------------------------------------------------------------------
1904 // update list of groups
1905 else if ( method == "GetGroups" )
1907 bool allGroupsRemoved = true;
1908 TCollection_AsciiString grIDs = theCommand->GetResultValue();
1909 list< _pyID > idList = theCommand->GetStudyEntries( grIDs );
1910 list< _pyID >::iterator grID = idList.begin();
1911 const int nbGroupsBefore = myGroups.size();
1912 Handle(_pyObject) obj;
1913 for ( ; grID != idList.end(); ++grID )
1915 obj = theGen->FindObject( *grID );
1918 Handle(_pyGroup) group = new _pyGroup( theCommand, *grID );
1919 theGen->AddObject( group );
1920 myGroups.push_back( group );
1923 if ( !obj->CanClear() )
1924 allGroupsRemoved = false;
1926 if ( nbGroupsBefore == myGroups.size() ) // no new _pyGroup created
1927 obj->AddProcessedCmd( theCommand ); // to clear theCommand if all groups are removed
1929 if ( !allGroupsRemoved && !theGen->IsToKeepAllCommands() )
1931 // check if the preceding command is Compute();
1932 // if GetGroups() is just after Compute(), this can mean that the groups
1933 // were created by some algorithm and hence Compute() should not be discarded
1934 std::list< Handle(_pyCommand) >& cmdList = theGen->GetCommands();
1935 std::list< Handle(_pyCommand) >::iterator cmd = cmdList.begin();
1936 while ( (*cmd)->GetMethod() == "GetGroups" )
1938 if ( myLastComputeCmd == (*cmd))
1939 // protect last Compute() from clearing by the next Compute()
1940 myLastComputeCmd.Nullify();
1943 // ----------------------------------------------------------------------
1944 // notify a group about full removal
1945 else if ( method == "RemoveGroupWithContents" ||
1946 method == "RemoveGroup")
1948 if ( !theGen->IsToKeepAllCommands() ) { // snapshot mode
1949 const _pyID groupID = theCommand->GetArg( 1 );
1950 Handle(_pyGroup) grp = Handle(_pyGroup)::DownCast( theGen->FindObject( groupID ));
1951 if ( !grp.IsNull() )
1953 if ( method == "RemoveGroupWithContents" )
1954 grp->RemovedWithContents();
1955 // to clear RemoveGroup() if the group creation is cleared
1956 grp->AddProcessedCmd( theCommand );
1960 // ----------------------------------------------------------------------
1961 else if ( theCommand->MethodStartsFrom( "Export" ))
1963 if ( method == "ExportToMED" || // ExportToMED() --> ExportMED()
1964 method == "ExportToMEDX" ) // ExportToMEDX() --> ExportMED()
1966 theCommand->SetMethod( "ExportMED" );
1967 if ( theCommand->GetNbArgs() == 5 )
1969 // ExportToMEDX(...,autoDimension) -> ExportToMEDX(...,meshPart=None,autoDimension)
1970 _AString autoDimension = theCommand->GetArg( 5 );
1971 theCommand->SetArg( 5, "None" );
1972 theCommand->SetArg( 6, autoDimension );
1975 else if ( method == "ExportCGNS" )
1976 { // ExportCGNS(part, ...) -> ExportCGNS(..., part)
1977 _pyID partID = theCommand->GetArg( 1 );
1978 int nbArgs = theCommand->GetNbArgs();
1979 for ( int i = 2; i <= nbArgs; ++i )
1980 theCommand->SetArg( i-1, theCommand->GetArg( i ));
1981 theCommand->SetArg( nbArgs, partID );
1983 else if ( method == "ExportGMF" )
1984 { // ExportGMF(part,file,bool) -> ExportCGNS(file, part)
1985 _pyID partID = theCommand->GetArg( 1 );
1986 _AString file = theCommand->GetArg( 2 );
1987 theCommand->RemoveArgs();
1988 theCommand->SetArg( 1, file );
1989 theCommand->SetArg( 2, partID );
1991 else if ( theCommand->MethodStartsFrom( "ExportPartTo" ))
1992 { // ExportPartTo*(part, ...) -> Export*(..., part)
1994 // remove "PartTo" from the method
1995 TCollection_AsciiString newMethod = method;
1996 newMethod.Remove( /*where=*/7, /*howmany=*/6 );
1997 theCommand->SetMethod( newMethod );
1998 // make the 1st arg be the last one (or last but three for ExportMED())
1999 _pyID partID = theCommand->GetArg( 1 );
2000 int nbArgs = theCommand->GetNbArgs() - 3 * (newMethod == "ExportMED");
2001 for ( int i = 2; i <= nbArgs; ++i )
2002 theCommand->SetArg( i-1, theCommand->GetArg( i ));
2003 theCommand->SetArg( nbArgs, partID );
2005 // remember file name
2006 theGen->AddExportedMesh( theCommand->GetArg( 1 ),
2007 ExportedMeshData( this, myLastComputeCmd ));
2009 // ----------------------------------------------------------------------
2010 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
2012 _pyID hypID = theCommand->GetArg( 2 );
2013 _pyID geomID = theCommand->GetArg( 1 );
2014 bool isLocal = ( geomID != GetGeom() );
2016 // check if this mesh still has corresponding addition command
2017 Handle(_pyCommand) addCmd;
2018 list< Handle(_pyCommand) >::iterator cmd;
2019 list< Handle(_pyCommand) >* addCmds[2] = { &myAddHypCmds, &myNotConvertedAddHypCmds };
2020 for ( int i = 0; i < 2; ++i )
2022 list< Handle(_pyCommand )> & addHypCmds = *(addCmds[i]);
2023 for ( cmd = addHypCmds.begin(); cmd != addHypCmds.end(); )
2025 bool sameHyp = true;
2026 if ( hypID != (*cmd)->GetArg( 1 ) && hypID != (*cmd)->GetArg( 2 ))
2027 sameHyp = false; // other hyp
2028 if ( (*cmd)->GetNbArgs() == 2 &&
2029 geomID != (*cmd)->GetArg( 1 ) && geomID != (*cmd)->GetArg( 2 ))
2030 sameHyp = false; // other geom
2031 if ( (*cmd)->GetNbArgs() == 1 && isLocal )
2032 sameHyp = false; // other geom
2036 cmd = addHypCmds.erase( cmd );
2037 if ( !theGen->IsToKeepAllCommands() && CanClear() ) {
2039 theCommand->Clear();
2043 // mesh.AddHypothesis(geom, hyp) --> mesh.AddHypothesis(hyp, geom=0)
2044 addCmd->RemoveArgs();
2045 addCmd->SetArg( 1, hypID );
2047 addCmd->SetArg( 2, geomID );
2056 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
2057 if ( !theCommand->IsEmpty() && !hypID.IsEmpty() ) {
2058 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
2059 _pyID geom = theCommand->GetArg( 1 );
2060 theCommand->RemoveArgs();
2061 theCommand->SetArg( 1, hypID );
2062 if ( geom != GetGeom() )
2063 theCommand->SetArg( 2, geom );
2065 // remove hyp from myHypos
2066 myHypos.remove( hyp );
2068 // check for SubMesh order commands
2069 else if ( method == "GetMeshOrder" || method == "SetMeshOrder" )
2071 // make commands GetSubMesh() returning sub-meshes be before using sub-meshes
2072 // by GetMeshOrder() and SetMeshOrder(), since by defalut GetSubMesh()
2073 // commands are moved at the end of the script
2074 TCollection_AsciiString subIDs =
2075 ( method == "SetMeshOrder" ) ? theCommand->GetArg(1) : theCommand->GetResultValue();
2076 list< _pyID > idList = theCommand->GetStudyEntries( subIDs );
2077 list< _pyID >::iterator subID = idList.begin();
2078 for ( ; subID != idList.end(); ++subID )
2080 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( *subID );
2081 if ( !subMesh.IsNull() )
2082 subMesh->Process( theCommand ); // it moves GetSubMesh() before theCommand
2085 // add accessor method if necessary
2088 if ( NeedMeshAccess( theCommand ))
2089 // apply theCommand to the mesh wrapped by smeshpy mesh
2090 AddMeshAccess( theCommand );
2094 //================================================================================
2096 * \brief Return True if addition of accesor method is needed
2098 //================================================================================
2100 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
2102 // names of SMESH_Mesh methods fully equal to methods of python class Mesh,
2103 // so no conversion is needed for them at all:
2104 static TStringSet sameMethods;
2105 if ( sameMethods.empty() ) {
2106 const char * names[] =
2107 { "ExportDAT","ExportUNV","ExportSTL","ExportSAUV", "RemoveGroup","RemoveGroupWithContents",
2108 "GetGroups","UnionGroups","IntersectGroups","CutGroups","CreateDimGroup","GetLog","GetId",
2109 "ClearLog","GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
2110 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
2111 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
2112 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
2113 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
2114 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
2115 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
2116 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
2117 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
2118 "GetElemFaceNodes", "GetFaceNormal", "FindElementByNodes",
2119 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
2120 "Clear", "ConvertToStandalone", "GetMeshOrder", "SetMeshOrder"
2121 ,"" }; // <- mark of end
2122 sameMethods.Insert( names );
2125 return !sameMethods.Contains( theCommand->GetMethod() );
2128 //================================================================================
2130 * \brief Convert creation and addition of all algos and hypos
2132 //================================================================================
2134 void _pyMesh::Flush()
2137 // get the meshes this mesh depends on via hypotheses
2138 list< Handle(_pyMesh) > fatherMeshes;
2139 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
2140 for ( ; hyp != myHypos.end(); ++hyp )
2141 if ( ! (*hyp)->GetReferredMeshesAndGeom( fatherMeshes ))
2142 myGeomNotInStudy = true;
2144 list< Handle(_pyMesh) >::iterator m = fatherMeshes.begin();
2145 for ( ; m != fatherMeshes.end(); ++m )
2146 addFatherMesh( *m );
2147 // if ( removedGeom )
2148 // SetRemovedFromStudy(); // as reffered geometry not in study
2150 if ( myGeomNotInStudy )
2153 list < Handle(_pyCommand) >::iterator cmd;
2155 // try to convert algo addition like this:
2156 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
2157 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
2159 Handle(_pyCommand) addCmd = *cmd;
2161 _pyID algoID = addCmd->GetArg( 2 );
2162 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
2163 if ( algo.IsNull() || !algo->IsAlgo() )
2166 // check and create new algorithm instance if it is already wrapped
2167 if ( algo->IsWrapped() ) {
2168 _pyID localAlgoID = theGen->GenerateNewID( algoID );
2169 TCollection_AsciiString aNewCmdStr = addCmd->GetIndentation() + localAlgoID +
2170 TCollection_AsciiString( " = " ) + theGen->GetID() +
2171 TCollection_AsciiString( ".CreateHypothesis( \"" ) + algo->GetAlgoType() +
2172 TCollection_AsciiString( "\" )" );
2174 Handle(_pyCommand) newCmd = theGen->AddCommand( aNewCmdStr );
2175 Handle(_pyAlgorithm) newAlgo = Handle(_pyAlgorithm)::DownCast(theGen->FindHyp( localAlgoID ));
2176 if ( !newAlgo.IsNull() ) {
2177 newAlgo->Assign( algo, this->GetID() );
2178 newAlgo->SetCreationCmd( newCmd );
2180 // set algorithm creation
2181 theGen->SetCommandBefore( newCmd, addCmd );
2182 myHypos.push_back( newAlgo );
2183 if ( !myLastComputeCmd.IsNull() &&
2184 newCmd->GetOrderNb() == myLastComputeCmd->GetOrderNb() + 1)
2185 newAlgo->MeshComputed( myLastComputeCmd );
2190 _pyID geom = addCmd->GetArg( 1 );
2191 bool isLocalAlgo = ( geom != GetGeom() );
2194 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
2196 // wrapped algo is created after mesh creation
2197 GetCreationCmd()->AddDependantCmd( addCmd );
2199 if ( isLocalAlgo ) {
2200 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
2201 addCmd->SetArg( addCmd->GetNbArgs() + 1,
2202 TCollection_AsciiString( "geom=" ) + geom );
2203 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
2204 list < Handle(_pySubMesh) >::iterator smIt;
2205 for ( smIt = mySubmeshes.begin(); smIt != mySubmeshes.end(); ++smIt ) {
2206 Handle(_pySubMesh) subMesh = *smIt;
2207 Handle(_pyCommand) subCmd = subMesh->GetCreationCmd();
2208 if ( geom == subCmd->GetArg( 1 )) {
2209 subCmd->SetObject( algo->GetID() );
2210 subCmd->RemoveArgs();
2211 subMesh->SetCreator( algo );
2216 else // KO - ALGO was already created
2218 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
2219 addCmd->RemoveArgs();
2220 addCmd->SetArg( 1, algoID );
2222 addCmd->SetArg( 2, geom );
2223 myNotConvertedAddHypCmds.push_back( addCmd );
2227 // try to convert hypo addition like this:
2228 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
2229 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
2231 Handle(_pyCommand) addCmd = *cmd;
2232 _pyID hypID = addCmd->GetArg( 2 );
2233 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
2234 if ( hyp.IsNull() || hyp->IsAlgo() )
2236 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
2238 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
2239 _pyID geom = addCmd->GetArg( 1 );
2240 addCmd->RemoveArgs();
2241 addCmd->SetArg( 1, hypID );
2242 if ( geom != GetGeom() )
2243 addCmd->SetArg( 2, geom );
2244 myNotConvertedAddHypCmds.push_back( addCmd );
2248 myAddHypCmds.clear();
2249 mySubmeshes.clear();
2252 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
2253 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
2257 //================================================================================
2259 * \brief Sets myIsPublished of me and of all objects depending on me.
2261 //================================================================================
2263 void _pyMesh::SetRemovedFromStudy(const bool isRemoved)
2265 _pyObject::SetRemovedFromStudy(isRemoved);
2267 list< Handle(_pySubMesh) >::iterator sm = mySubmeshes.begin();
2268 for ( ; sm != mySubmeshes.end(); ++sm )
2269 (*sm)->SetRemovedFromStudy(isRemoved);
2271 list< Handle(_pyGroup) >::iterator gr = myGroups.begin();
2272 for ( ; gr != myGroups.end(); ++gr )
2273 (*gr)->SetRemovedFromStudy(isRemoved);
2275 list< Handle(_pyMesh) >::iterator m = myChildMeshes.begin();
2276 for ( ; m != myChildMeshes.end(); ++m )
2277 (*m)->SetRemovedFromStudy(isRemoved);
2279 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
2280 for ( ; e != myEditors.end(); ++e )
2281 (*e)->SetRemovedFromStudy(isRemoved);
2284 //================================================================================
2286 * \brief Return true if none of myChildMeshes is in study
2288 //================================================================================
2290 bool _pyMesh::CanClear()
2295 list< Handle(_pyMesh) >::iterator m = myChildMeshes.begin();
2296 for ( ; m != myChildMeshes.end(); ++m )
2297 if ( !(*m)->CanClear() )
2303 //================================================================================
2305 * \brief Clear my commands and commands of mesh editor
2307 //================================================================================
2309 void _pyMesh::ClearCommands()
2315 // mark all sub-objects as not removed, except child meshes
2316 list< Handle(_pyMesh) > children;
2317 children.swap( myChildMeshes );
2318 SetRemovedFromStudy( false );
2319 children.swap( myChildMeshes );
2323 _pyObject::ClearCommands();
2325 list< Handle(_pySubMesh) >::iterator sm = mySubmeshes.begin();
2326 for ( ; sm != mySubmeshes.end(); ++sm )
2327 (*sm)->ClearCommands();
2329 list< Handle(_pyGroup) >::iterator gr = myGroups.begin();
2330 for ( ; gr != myGroups.end(); ++gr )
2331 (*gr)->ClearCommands();
2333 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
2334 for ( ; e != myEditors.end(); ++e )
2335 (*e)->ClearCommands();
2338 //================================================================================
2340 * \brief Add a father mesh by ID
2342 //================================================================================
2344 void _pyMesh::addFatherMesh( const _pyID& meshID )
2346 if ( !meshID.IsEmpty() && meshID != GetID() )
2347 addFatherMesh( Handle(_pyMesh)::DownCast( theGen->FindObject( meshID )));
2350 //================================================================================
2352 * \brief Add a father mesh
2354 //================================================================================
2356 void _pyMesh::addFatherMesh( const Handle(_pyMesh)& mesh )
2358 if ( !mesh.IsNull() && mesh->GetID() != GetID() )
2360 //myFatherMeshes.push_back( mesh );
2361 mesh->myChildMeshes.push_back( this );
2363 // protect last Compute() from clearing by the next Compute()
2364 mesh->myLastComputeCmd.Nullify();
2368 //================================================================================
2370 * \brief MeshEditor convert its commands to ones of mesh
2372 //================================================================================
2374 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
2375 _pyObject( theCreationCmd )
2377 myMesh = theCreationCmd->GetObject();
2378 myCreationCmdStr = theCreationCmd->GetString();
2379 theCreationCmd->Clear();
2381 Handle(_pyMesh) mesh = ObjectToMesh( theGen->FindObject( myMesh ));
2382 if ( !mesh.IsNull() )
2383 mesh->AddEditor( this );
2386 //================================================================================
2388 * \brief convert its commands to ones of mesh
2390 //================================================================================
2392 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
2394 // Names of SMESH_MeshEditor methods fully equal to methods of the python class Mesh, so
2395 // commands calling these methods are converted to calls of Mesh methods without
2396 // additional modifs, only object is changed from MeshEditor to Mesh.
2397 static TStringSet sameMethods;
2398 if ( sameMethods.empty() ) {
2399 const char * names[] = {
2400 "RemoveElements","RemoveNodes","RemoveOrphanNodes",
2401 "AddNode","Add0DElement","AddEdge","AddFace","AddPolygonalFace","AddBall",
2402 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces",
2403 "MoveNode", "MoveClosestNodeToPoint",
2404 "InverseDiag","DeleteDiag","Reorient","ReorientObject","Reorient2DBy3D",
2405 "TriToQuad","TriToQuadObject", "QuadTo4Tri", "SplitQuad","SplitQuadObject",
2406 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
2407 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
2408 "RotationSweep","RotationSweepObject","RotationSweepObject1D","RotationSweepObject2D",
2409 "ExtrusionSweep","AdvancedExtrusion","ExtrusionSweepObject","ExtrusionSweepObject1D",
2410 "ExtrusionByNormal", "ExtrusionSweepObject2D","ExtrusionAlongPath","ExtrusionAlongPathObject",
2411 "ExtrusionAlongPathX","ExtrusionAlongPathObject1D","ExtrusionAlongPathObject2D",
2412 "ExtrusionSweepObjects","RotationSweepObjects","ExtrusionAlongPathObjects",
2413 "Mirror","MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
2414 "FindCoincidentNodes","MergeNodes","FindEqualElements",
2415 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
2416 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
2417 "GetLastCreatedElems",
2418 "MirrorMakeMesh","MirrorObjectMakeMesh","TranslateMakeMesh","TranslateObjectMakeMesh",
2419 "Scale","ScaleMakeMesh","RotateMakeMesh","RotateObjectMakeMesh","MakeBoundaryMesh",
2420 "MakeBoundaryElements", "SplitVolumesIntoTetra","SplitHexahedraIntoPrisms",
2421 "DoubleElements","DoubleNodes","DoubleNode","DoubleNodeGroup","DoubleNodeGroups",
2422 "DoubleNodeElem","DoubleNodeElemInRegion","DoubleNodeElemGroup",
2423 "DoubleNodeElemGroupInRegion","DoubleNodeElemGroups","DoubleNodeElemGroupsInRegion",
2424 "DoubleNodesOnGroupBoundaries","CreateFlatElementsOnFacesGroups","CreateHoleSkin"
2425 ,"" }; // <- mark of the end
2426 sameMethods.Insert( names );
2429 // names of SMESH_MeshEditor commands in which only a method name must be replaced
2430 TStringMap diffMethods;
2431 if ( diffMethods.empty() ) {
2432 const char * orig2newName[] = {
2433 // original name --------------> new name
2434 "ExtrusionAlongPathObjX" , "ExtrusionAlongPathX",
2435 "FindCoincidentNodesOnPartBut", "FindCoincidentNodesOnPart",
2436 "ConvertToQuadraticObject" , "ConvertToQuadratic",
2437 "ConvertFromQuadraticObject" , "ConvertFromQuadratic",
2438 "Create0DElementsOnAllNodes" , "Add0DElementsToAllNodes",
2439 ""};// <- mark of the end
2440 diffMethods.Insert( orig2newName );
2443 // names of SMESH_MeshEditor methods which differ from methods of Mesh class
2444 // only by last two arguments
2445 static TStringSet diffLastTwoArgsMethods;
2446 if (diffLastTwoArgsMethods.empty() ) {
2447 const char * names[] = {
2448 "MirrorMakeGroups","MirrorObjectMakeGroups",
2449 "TranslateMakeGroups","TranslateObjectMakeGroups","ScaleMakeGroups",
2450 "RotateMakeGroups","RotateObjectMakeGroups",
2451 ""};// <- mark of the end
2452 diffLastTwoArgsMethods.Insert( names );
2455 // only a method name is to change?
2456 const TCollection_AsciiString & method = theCommand->GetMethod();
2457 bool isPyMeshMethod = sameMethods.Contains( method );
2458 if ( !isPyMeshMethod )
2460 TCollection_AsciiString newMethod = diffMethods.Value( method );
2461 if (( isPyMeshMethod = ( newMethod.Length() > 0 )))
2462 theCommand->SetMethod( newMethod );
2464 // ConvertToBiQuadratic(...) -> ConvertToQuadratic(...,True)
2465 if ( !isPyMeshMethod && (method == "ConvertToBiQuadratic" || method == "ConvertToBiQuadraticObject") )
2467 isPyMeshMethod = true;
2468 theCommand->SetMethod( method.SubString( 1, 9) + method.SubString( 12, method.Length()));
2469 theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
2472 if ( !isPyMeshMethod )
2474 // Replace SMESH_MeshEditor "*MakeGroups" functions by the Mesh
2475 // functions with the flag "theMakeGroups = True" like:
2476 // SMESH_MeshEditor.CmdMakeGroups => Mesh.Cmd(...,True)
2477 int pos = method.Search("MakeGroups");
2480 isPyMeshMethod = true;
2481 bool is0DmethId = ( method == "ExtrusionSweepMakeGroups0D" );
2482 bool is0DmethObj = ( method == "ExtrusionSweepObject0DMakeGroups");
2484 // 1. Remove "MakeGroups" from the Command
2485 TCollection_AsciiString aMethod = theCommand->GetMethod();
2486 int nbArgsToAdd = diffLastTwoArgsMethods.Contains(aMethod) ? 2 : 1;
2489 pos = pos-2; //Remove "0D" from the Command too
2490 aMethod.Trunc(pos-1);
2491 theCommand->SetMethod(aMethod);
2493 // 2. And add last "True" argument(s)
2494 while(nbArgsToAdd--)
2495 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
2496 if( is0DmethId || is0DmethObj )
2497 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
2501 // ExtrusionSweep0D() -> ExtrusionSweep()
2502 // ExtrusionSweepObject0D() -> ExtrusionSweepObject()
2503 if ( !isPyMeshMethod && ( method == "ExtrusionSweep0D" ||
2504 method == "ExtrusionSweepObject0D" ))
2506 isPyMeshMethod = true;
2507 theCommand->SetMethod( method.SubString( 1, method.Length()-2));
2508 theCommand->SetArg(theCommand->GetNbArgs()+1,"False"); //sets flag "MakeGroups = False"
2509 theCommand->SetArg(theCommand->GetNbArgs()+1,"True"); //sets flag "IsNode = True"
2512 // DoubleNode...New(...) -> DoubleNode...(...,True)
2513 if ( !isPyMeshMethod && ( method == "DoubleNodeElemGroupNew" ||
2514 method == "DoubleNodeElemGroupsNew" ||
2515 method == "DoubleNodeGroupNew" ||
2516 method == "DoubleNodeGroupsNew" ||
2517 method == "DoubleNodeElemGroup2New" ||
2518 method == "DoubleNodeElemGroups2New"))
2520 isPyMeshMethod = true;
2521 const int excessLen = 3 + int( method.Value( method.Length()-3 ) == '2' );
2522 theCommand->SetMethod( method.SubString( 1, method.Length()-excessLen));
2523 if ( excessLen == 3 )
2525 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
2527 else if ( theCommand->GetArg(4) == "0" ||
2528 theCommand->GetArg(5) == "0" )
2530 // [ nothing, Group ] = DoubleNodeGroup2New(,,,False, True) ->
2531 // Group = DoubleNodeGroup2New(,,,False, True)
2532 _pyID groupID = theCommand->GetResultValue( 1 + int( theCommand->GetArg(4) == "0"));
2533 theCommand->SetResultValue( groupID );
2536 // FindAmongElementsByPoint(meshPart, x, y, z, elementType) ->
2537 // FindElementsByPoint(x, y, z, elementType, meshPart)
2538 if ( !isPyMeshMethod && method == "FindAmongElementsByPoint" )
2540 isPyMeshMethod = true;
2541 theCommand->SetMethod( "FindElementsByPoint" );
2542 // make the 1st arg be the last one
2543 _pyID partID = theCommand->GetArg( 1 );
2544 int nbArgs = theCommand->GetNbArgs();
2545 for ( int i = 2; i <= nbArgs; ++i )
2546 theCommand->SetArg( i-1, theCommand->GetArg( i ));
2547 theCommand->SetArg( nbArgs, partID );
2549 // Reorient2D( mesh, dir, face, point ) -> Reorient2D( mesh, dir, faceORpoint )
2550 if ( !isPyMeshMethod && method == "Reorient2D" )
2552 isPyMeshMethod = true;
2553 _AString mesh = theCommand->GetArg( 1 );
2554 _AString dir = theCommand->GetArg( 2 );
2555 _AString face = theCommand->GetArg( 3 );
2556 _AString point = theCommand->GetArg( 4 );
2557 theCommand->RemoveArgs();
2558 theCommand->SetArg( 1, mesh );
2559 theCommand->SetArg( 2, dir );
2560 if ( face.Value(1) == '-' || face.Value(1) == '0' ) // invalid: face <= 0
2561 theCommand->SetArg( 3, point );
2563 theCommand->SetArg( 3, face );
2566 if ( method == "QuadToTri" || method == "QuadToTriObject" )
2568 isPyMeshMethod = true;
2569 int crit_arg = theCommand->GetNbArgs();
2570 const _AString& crit = theCommand->GetArg(crit_arg);
2571 if (crit.Search("MaxElementLength2D") != -1)
2572 theCommand->SetArg(crit_arg, "");
2575 if ( isPyMeshMethod )
2577 theCommand->SetObject( myMesh );
2581 // editor creation command is needed only if any editor function is called
2582 theGen->AddMeshAccessorMethod( theCommand ); // for *Object() methods
2583 if ( !myCreationCmdStr.IsEmpty() ) {
2584 GetCreationCmd()->GetString() = myCreationCmdStr;
2585 myCreationCmdStr.Clear();
2590 //================================================================================
2592 * \brief Return true if my mesh can be removed
2594 //================================================================================
2596 bool _pyMeshEditor::CanClear()
2598 Handle(_pyMesh) mesh = ObjectToMesh( theGen->FindObject( myMesh ));
2599 return mesh.IsNull() ? true : mesh->CanClear();
2602 //================================================================================
2604 * \brief _pyHypothesis constructor
2605 * \param theCreationCmd -
2607 //================================================================================
2609 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
2610 _pyObject( theCreationCmd ), myCurCrMethod(0)
2612 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
2615 //================================================================================
2617 * \brief Creates algorithm or hypothesis
2618 * \param theCreationCmd - The engine command creating a hypothesis
2619 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
2621 //================================================================================
2623 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
2625 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
2626 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
2628 Handle(_pyHypothesis) hyp, algo;
2631 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
2632 if ( hypTypeQuoted.IsEmpty() )
2635 TCollection_AsciiString hypType =
2636 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
2638 algo = new _pyAlgorithm( theCreationCmd );
2639 hyp = new _pyHypothesis( theCreationCmd );
2641 if ( hypType == "NumberOfSegments" ) {
2642 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
2643 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
2644 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
2645 hyp->AddArgMethod( "SetNumberOfSegments" );
2646 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
2647 hyp->AddArgMethod( "SetScaleFactor" );
2648 hyp->AddArgMethod( "SetReversedEdges" );
2649 // same for ""CompositeSegment_1D:
2650 hyp->SetConvMethodAndType( "NumberOfSegments", "CompositeSegment_1D");
2651 hyp->AddArgMethod( "SetNumberOfSegments" );
2652 hyp->AddArgMethod( "SetScaleFactor" );
2653 hyp->AddArgMethod( "SetReversedEdges" );
2655 else if ( hypType == "SegmentLengthAroundVertex" ) {
2656 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
2657 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
2658 hyp->AddArgMethod( "SetLength" );
2659 // same for ""CompositeSegment_1D:
2660 hyp->SetConvMethodAndType( "LengthNearVertex", "CompositeSegment_1D");
2661 hyp->AddArgMethod( "SetLength" );
2663 else if ( hypType == "LayerDistribution2D" ) {
2664 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get2DHypothesis" );
2665 hyp->SetConvMethodAndType( "LayerDistribution", "RadialQuadrangle_1D2D");
2667 else if ( hypType == "LayerDistribution" ) {
2668 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get3DHypothesis" );
2669 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
2671 else if ( hypType == "CartesianParameters3D" ) {
2672 hyp = new _pyComplexParamHypo( theCreationCmd );
2673 hyp->SetConvMethodAndType( "SetGrid", "Cartesian_3D");
2674 for ( int iArg = 0; iArg < 4; ++iArg )
2675 hyp->setCreationArg( iArg+1, "[]");
2676 hyp->AddAccumulativeMethod( "SetGrid" );
2677 hyp->AddAccumulativeMethod( "SetGridSpacing" );
2681 hyp = theGen->GetHypothesisReader()->GetHypothesis( hypType, theCreationCmd );
2684 return algo->IsValid() ? algo : hyp;
2687 //================================================================================
2689 * \brief Returns true if addition of this hypothesis to a given mesh can be
2690 * wrapped into hypothesis creation
2692 //================================================================================
2694 bool _pyHypothesis::IsWrappable(const _pyID& theMesh) const
2696 if ( !myIsWrapped && myMesh == theMesh && IsInStudy() )
2698 Handle(_pyObject) pyMesh = theGen->FindObject( myMesh );
2699 if ( !pyMesh.IsNull() && pyMesh->IsInStudy() )
2705 //================================================================================
2707 * \brief Convert the command adding a hypothesis to mesh into a smesh command
2708 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
2709 * \param theAlgo - The algo that can create this hypo
2710 * \retval bool - false if the command cant be converted
2712 //================================================================================
2714 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
2715 const _pyID& theMesh)
2717 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
2719 if ( !IsWrappable( theMesh ))
2722 myGeom = theCmd->GetArg( 1 );
2724 Handle(_pyHypothesis) algo;
2726 // find algo created on myGeom in theMesh
2727 algo = theGen->FindAlgo( myGeom, theMesh, this );
2728 if ( algo.IsNull() )
2730 // attach hypothesis creation command to be after algo creation command
2731 // because it can be new created instance of algorithm
2732 algo->GetCreationCmd()->AddDependantCmd( theCmd );
2736 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
2737 theCmd->SetResultValue( GetID() );
2738 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
2739 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
2740 // set args (geom will be set by _pyMesh calling this method)
2741 theCmd->RemoveArgs();
2742 for ( size_t i = 0; i < myCurCrMethod->myArgs.size(); ++i ) {
2743 if ( !myCurCrMethod->myArgs[ i ].IsEmpty() )
2744 theCmd->SetArg( i+1, myCurCrMethod->myArgs[ i ]);
2746 theCmd->SetArg( i+1, "[]");
2748 // set a new creation command
2749 GetCreationCmd()->Clear();
2750 // replace creation command by wrapped instance
2751 // please note, that hypothesis attaches to algo creation command (see upper)
2752 SetCreationCmd( theCmd );
2755 // clear commands setting arg values
2756 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
2757 for ( ; argCmd != myArgCommands.end(); ++argCmd )
2760 // set unknown arg commands after hypo creation
2761 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
2762 list<Handle(_pyCommand)>::iterator cmd = myUnusedCommands.begin();
2763 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2764 afterCmd->AddDependantCmd( *cmd );
2770 //================================================================================
2772 * \brief Remember hypothesis parameter values
2773 * \param theCommand - The called hypothesis method
2775 //================================================================================
2777 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
2779 ASSERT( !myIsAlgo );
2780 if ( !theGen->IsToKeepAllCommands() )
2781 rememberCmdOfParameter( theCommand );
2783 bool usedCommand = false;
2784 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2785 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2787 CreationMethod& crMethod = type2meth->second;
2788 for ( size_t i = 0; i < crMethod.myArgMethods.size(); ++i ) {
2789 if ( crMethod.myArgMethods[ i ] == theCommand->GetMethod() ) {
2791 myArgCommands.push_back( theCommand );
2793 while ( crMethod.myArgs.size() < i+1 )
2794 crMethod.myArgs.push_back( "None" );
2795 crMethod.myArgs[ i ] = theCommand->GetArg( crMethod.myArgNb[i] );
2800 myUnusedCommands.push_back( theCommand );
2803 //================================================================================
2805 * \brief Finish conversion
2807 //================================================================================
2809 void _pyHypothesis::Flush()
2813 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
2814 for ( ; cmd != myArgCommands.end(); ++cmd ) {
2815 // Add access to a wrapped mesh
2816 theGen->AddMeshAccessorMethod( *cmd );
2817 // Add access to a wrapped algorithm
2818 theGen->AddAlgoAccessorMethod( *cmd );
2820 cmd = myUnusedCommands.begin();
2821 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2822 // Add access to a wrapped mesh
2823 theGen->AddMeshAccessorMethod( *cmd );
2824 // Add access to a wrapped algorithm
2825 theGen->AddAlgoAccessorMethod( *cmd );
2828 // forget previous hypothesis modifications
2829 myArgCommands.clear();
2830 myUnusedCommands.clear();
2833 //================================================================================
2835 * \brief clear creation, arg and unkown commands
2837 //================================================================================
2839 void _pyHypothesis::ClearAllCommands()
2841 GetCreationCmd()->Clear();
2842 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
2843 for ( ; cmd != myArgCommands.end(); ++cmd )
2845 cmd = myUnusedCommands.begin();
2846 for ( ; cmd != myUnusedCommands.end(); ++cmd )
2851 //================================================================================
2853 * \brief Assign fields of theOther to me except myIsWrapped
2855 //================================================================================
2857 void _pyHypothesis::Assign( const Handle(_pyHypothesis)& theOther,
2858 const _pyID& theMesh )
2860 // myCreationCmd = theOther->myCreationCmd;
2861 myIsAlgo = theOther->myIsAlgo;
2862 myIsWrapped = false;
2863 myGeom = theOther->myGeom;
2865 myAlgoType2CreationMethod = theOther->myAlgoType2CreationMethod;
2866 myAccumulativeMethods = theOther->myAccumulativeMethods;
2867 //myUnusedCommands = theOther->myUnusedCommands;
2868 // init myCurCrMethod
2869 GetCreationMethod( theOther->GetAlgoType() );
2872 //================================================================================
2874 * \brief Analyze my erasability depending on myReferredObjs
2876 //================================================================================
2878 bool _pyHypothesis::CanClear()
2882 list< Handle(_pyObject) >::iterator obj = myReferredObjs.begin();
2883 for ( ; obj != myReferredObjs.end(); ++obj )
2884 if ( (*obj)->CanClear() )
2891 //================================================================================
2893 * \brief Clear my commands depending on usage by meshes
2895 //================================================================================
2897 void _pyHypothesis::ClearCommands()
2899 // if ( !theGen->IsToKeepAllCommands() )
2901 // bool isUsed = false;
2902 // int lastComputeOrder = 0;
2903 // list<Handle(_pyCommand) >::iterator cmd = myComputeCmds.begin();
2904 // for ( ; cmd != myComputeCmds.end(); ++cmd )
2905 // if ( ! (*cmd)->IsEmpty() )
2908 // if ( (*cmd)->GetOrderNb() > lastComputeOrder )
2909 // lastComputeOrder = (*cmd)->GetOrderNb();
2913 // SetRemovedFromStudy( true );
2917 // // clear my commands invoked after lastComputeOrder
2918 // // map<TCollection_AsciiString, list< Handle(_pyCommand) > >::iterator m2c;
2919 // // for ( m2c = myMeth2Commands.begin(); m2c != myMeth2Commands.end(); ++m2c )
2921 // // list< Handle(_pyCommand)> & cmds = m2c->second;
2922 // // if ( !cmds.empty() && cmds.back()->GetOrderNb() > lastComputeOrder )
2923 // // cmds.back()->Clear();
2927 _pyObject::ClearCommands();
2930 //================================================================================
2932 * \brief Find arguments that are objects like mesh, group, geometry
2933 * \param meshes - referred meshes (directly or indirrectly)
2934 * \retval bool - false if a referred geometry is not in the study
2936 //================================================================================
2938 bool _pyHypothesis::GetReferredMeshesAndGeom( list< Handle(_pyMesh) >& meshes )
2940 if ( IsAlgo() ) return true;
2942 bool geomPublished = true;
2943 vector< _AString > args;
2944 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2945 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2947 CreationMethod& crMethod = type2meth->second;
2948 args.insert( args.end(), crMethod.myArgs.begin(), crMethod.myArgs.end());
2950 list<Handle(_pyCommand)>::iterator cmd = myUnusedCommands.begin();
2951 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2952 for ( int nb = (*cmd)->GetNbArgs(); nb; --nb )
2953 args.push_back( (*cmd)->GetArg( nb ));
2956 for ( size_t i = 0; i < args.size(); ++i )
2958 list< _pyID > idList = _pyCommand::GetStudyEntries( args[ i ]);
2959 if ( idList.empty() && !args[ i ].IsEmpty() )
2960 idList.push_back( args[ i ]);
2961 list< _pyID >::iterator id = idList.begin();
2962 for ( ; id != idList.end(); ++id )
2964 Handle(_pyObject) obj = theGen->FindObject( *id );
2965 if ( obj.IsNull() ) obj = theGen->FindHyp( *id );
2968 if ( theGen->IsGeomObject( *id ) && theGen->IsNotPublished( *id ))
2969 geomPublished = false;
2973 myReferredObjs.push_back( obj );
2974 Handle(_pyMesh) mesh = ObjectToMesh( obj );
2975 if ( !mesh.IsNull() )
2976 meshes.push_back( mesh );
2977 // prevent clearing not published hyps referred e.g. by "LayerDistribution"
2978 else if ( obj->IsKind( STANDARD_TYPE( _pyHypothesis )) && this->IsInStudy() )
2979 obj->SetRemovedFromStudy( false );
2983 return geomPublished;
2986 //================================================================================
2988 * \brief Remember theCommand setting a parameter
2990 //================================================================================
2992 void _pyHypothesis::rememberCmdOfParameter( const Handle(_pyCommand) & theCommand )
2994 // parameters are discriminated by method name
2995 _AString method = theCommand->GetMethod();
2996 if ( myAccumulativeMethods.count( method ))
2997 return; // this method adds values and not override the previus value
2999 // discriminate commands setting different parameters via one method
3000 // by passing parameter names like e.g. SetOption("size", "0.2")
3001 if ( theCommand->GetString().FirstLocationInSet( "'\"", 1, theCommand->Length() ) &&
3002 theCommand->GetNbArgs() > 1 )
3004 // mangle method by appending a 1st textual arg
3005 for ( int iArg = 1; iArg <= theCommand->GetNbArgs(); ++iArg )
3007 const TCollection_AsciiString& arg = theCommand->GetArg( iArg );
3008 if ( arg.Value(1) != '\"' && arg.Value(1) != '\'' ) continue;
3009 if ( !isalpha( arg.Value(2))) continue;
3014 // parameters are discriminated by method name
3015 list< Handle(_pyCommand)>& cmds = myMeth2Commands[ method /*theCommand->GetMethod()*/ ];
3016 if ( !cmds.empty() && !isCmdUsedForCompute( cmds.back() ))
3018 cmds.back()->Clear(); // previous parameter value has not been used
3019 cmds.back() = theCommand;
3023 cmds.push_back( theCommand );
3027 //================================================================================
3029 * \brief Return true if a setting parameter command ha been used to compute mesh
3031 //================================================================================
3033 bool _pyHypothesis::isCmdUsedForCompute( const Handle(_pyCommand) & cmd,
3034 _pyCommand::TAddr avoidComputeAddr ) const
3036 bool isUsed = false;
3037 map< _pyCommand::TAddr, list<Handle(_pyCommand) > >::const_iterator addr2cmds =
3038 myComputeAddr2Cmds.begin();
3039 for ( ; addr2cmds != myComputeAddr2Cmds.end() && !isUsed; ++addr2cmds )
3041 if ( addr2cmds->first == avoidComputeAddr ) continue;
3042 const list<Handle(_pyCommand)> & cmds = addr2cmds->second;
3043 isUsed = ( std::find( cmds.begin(), cmds.end(), cmd ) != cmds.end() );
3048 //================================================================================
3050 * \brief Save commands setting parameters as they are used for a mesh computation
3052 //================================================================================
3054 void _pyHypothesis::MeshComputed( const Handle(_pyCommand)& theComputeCmd )
3056 myComputeCmds.push_back( theComputeCmd );
3057 list<Handle(_pyCommand)>& savedCmds = myComputeAddr2Cmds[ theComputeCmd->GetAddress() ];
3059 map<TCollection_AsciiString, list< Handle(_pyCommand) > >::iterator m2c;
3060 for ( m2c = myMeth2Commands.begin(); m2c != myMeth2Commands.end(); ++m2c )
3061 savedCmds.push_back( m2c->second.back() );
3064 //================================================================================
3066 * \brief Clear commands setting parameters as a mesh computed using them is cleared
3068 //================================================================================
3070 void _pyHypothesis::ComputeDiscarded( const Handle(_pyCommand)& theComputeCmd )
3072 list<Handle(_pyCommand)>& savedCmds = myComputeAddr2Cmds[ theComputeCmd->GetAddress() ];
3074 list<Handle(_pyCommand)>::iterator cmd = savedCmds.begin();
3075 for ( ; cmd != savedCmds.end(); ++cmd )
3077 // check if a cmd has been used to compute another mesh
3078 if ( isCmdUsedForCompute( *cmd, theComputeCmd->GetAddress() ))
3080 // check if a cmd is a sole command setting its parameter;
3081 // don't use method name for search as it can change
3082 map<TCollection_AsciiString, list<Handle(_pyCommand)> >::iterator
3083 m2cmds = myMeth2Commands.begin();
3084 for ( ; m2cmds != myMeth2Commands.end(); ++m2cmds )
3086 list< Handle(_pyCommand)>& cmds = m2cmds->second;
3087 list< Handle(_pyCommand)>::iterator cmdIt = std::find( cmds.begin(), cmds.end(), *cmd );
3088 if ( cmdIt != cmds.end() )
3090 if ( cmds.back() != *cmd )
3092 cmds.erase( cmdIt );
3099 myComputeAddr2Cmds.erase( theComputeCmd->GetAddress() );
3102 //================================================================================
3104 * \brief Sets an argNb-th argument of current creation command
3105 * \param argNb - argument index countered from 1
3107 //================================================================================
3109 void _pyHypothesis::setCreationArg( const int argNb, const _AString& arg )
3111 if ( myCurCrMethod )
3113 while ( myCurCrMethod->myArgs.size() < argNb )
3114 myCurCrMethod->myArgs.push_back( "None" );
3115 if ( arg.IsEmpty() )
3116 myCurCrMethod->myArgs[ argNb-1 ] = "None";
3118 myCurCrMethod->myArgs[ argNb-1 ] = arg;
3123 //================================================================================
3125 * \brief Remember hypothesis parameter values
3126 * \param theCommand - The called hypothesis method
3128 //================================================================================
3130 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
3132 if ( GetAlgoType() == "Cartesian_3D" )
3134 // CartesianParameters3D hyp
3136 if ( theCommand->GetMethod() == "SetSizeThreshold" ||
3137 theCommand->GetMethod() == "SetToAddEdges" )
3139 int iEdges = ( theCommand->GetMethod().Value( 4 ) == 'T' );
3140 setCreationArg( 4+iEdges, theCommand->GetArg( 1 ));
3141 myArgCommands.push_back( theCommand );
3144 if ( theCommand->GetMethod() == "SetGrid" ||
3145 theCommand->GetMethod() == "SetGridSpacing" )
3147 TCollection_AsciiString axis = theCommand->GetArg( theCommand->GetNbArgs() );
3148 int iArg = axis.Value(1) - '0';
3149 if ( theCommand->GetMethod() == "SetGrid" )
3151 setCreationArg( 1+iArg, theCommand->GetArg( 1 ));
3155 myCurCrMethod->myArgs[ iArg ] = "[ ";
3156 myCurCrMethod->myArgs[ iArg ] += theCommand->GetArg( 1 );
3157 myCurCrMethod->myArgs[ iArg ] += ", ";
3158 myCurCrMethod->myArgs[ iArg ] += theCommand->GetArg( 2 );
3159 myCurCrMethod->myArgs[ iArg ] += "]";
3161 myArgCommands.push_back( theCommand );
3162 //rememberCmdOfParameter( theCommand ); -- these commands are marked as
3163 // accumulative, else, if the creation
3164 // is not converted, commands for axes 1 and 2 are lost
3169 if( theCommand->GetMethod() == "SetLength" )
3171 // NOW it is OBSOLETE
3172 // ex: hyp.SetLength(start, 1)
3173 // hyp.SetLength(end, 0)
3174 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
3175 int i = 1 - theCommand->GetArg( 2 ).IntegerValue();
3176 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
3177 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
3179 CreationMethod& crMethod = type2meth->second;
3180 while ( crMethod.myArgs.size() < i+1 )
3181 crMethod.myArgs.push_back( "[]" );
3182 crMethod.myArgs[ i ] = theCommand->GetArg( 1 ); // arg value
3184 myArgCommands.push_back( theCommand );
3188 _pyHypothesis::Process( theCommand );
3191 //================================================================================
3193 * \brief Clear SetObjectEntry() as it is called by methods of Mesh_Segment
3195 //================================================================================
3197 void _pyComplexParamHypo::Flush()
3199 list < Handle(_pyCommand) >::iterator cmd;
3202 for ( cmd = myUnusedCommands.begin(); cmd != myUnusedCommands.end(); ++cmd )
3203 if ((*cmd)->GetMethod() == "SetObjectEntry" )
3207 // if ( GetAlgoType() == "Cartesian_3D" )
3209 // _pyID algo = myCreationCmd->GetObject();
3210 // for ( cmd = myProcessedCmds.begin(); cmd != myProcessedCmds.end(); ++cmd )
3212 // if ( IsWrapped() )
3214 // StructToList( *cmd, /*checkMethod=*/false );
3215 // const _AString & method = (*cmd)->GetMethod();
3216 // if ( method == "SetFixedPoint" )
3217 // (*cmd)->SetObject( algo );
3223 //================================================================================
3225 * \brief Convert methods of 1D hypotheses to my own methods
3226 * \param theCommand - The called hypothesis method
3228 //================================================================================
3230 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
3232 if ( theCommand->GetMethod() != "SetLayerDistribution" )
3235 const _pyID& hyp1dID = theCommand->GetArg( 1 );
3236 // Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
3237 // if ( hyp1d.IsNull() && ! my1dHyp.IsNull()) // apparently hypId changed at study restoration
3239 // TCollection_AsciiString cmd =
3240 // my1dHyp->GetCreationCmd()->GetIndentation() + hyp1dID + " = " + my1dHyp->GetID();
3241 // Handle(_pyCommand) newCmd = theGen->AddCommand( cmd );
3242 // theGen->SetCommandAfter( newCmd, my1dHyp->GetCreationCmd() );
3245 // else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() )
3247 // // 1D hypo is already set, so distribution changes and the old
3248 // // 1D hypo is thrown away
3249 // my1dHyp->ClearAllCommands();
3252 // //my1dHyp->SetRemovedFromStudy( false );
3254 // if ( !myArgCommands.empty() )
3255 // myArgCommands.back()->Clear();
3256 myCurCrMethod->myArgs.push_back( hyp1dID );
3257 myArgCommands.push_back( theCommand );
3260 //================================================================================
3263 * \param theAdditionCmd - command to be converted
3264 * \param theMesh - mesh instance
3265 * \retval bool - status
3267 //================================================================================
3269 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
3270 const _pyID& theMesh)
3272 myIsWrapped = false;
3274 if ( my1dHyp.IsNull() )
3277 // set "SetLayerDistribution()" after addition cmd
3278 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
3280 _pyID geom = theAdditionCmd->GetArg( 1 );
3282 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
3283 if ( !algo.IsNull() )
3285 my1dHyp->SetMesh( theMesh );
3286 my1dHyp->SetConvMethodAndType(my1dHyp->GetAlgoCreationMethod().ToCString(),
3287 algo->GetAlgoType().ToCString());
3288 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
3291 // clear "SetLayerDistribution()" cmd
3292 myArgCommands.back()->Clear();
3294 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
3296 // find RadialPrism algo created on <geom> for theMesh
3297 GetCreationCmd()->SetObject( algo->GetID() );
3298 GetCreationCmd()->SetMethod( myAlgoMethod );
3299 GetCreationCmd()->RemoveArgs();
3300 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
3306 //================================================================================
3310 //================================================================================
3312 void _pyLayerDistributionHypo::Flush()
3314 // as creation of 1D hyp was written later then it's edition,
3315 // we need to find all it's edition calls and process them
3316 list< Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
3318 for ( cmd = myArgCommands.begin(); cmd != myArgCommands.end(); ++cmd )
3320 const _pyID& hyp1dID = (*cmd)->GetArg( 1 );
3321 if ( hyp1dID.IsEmpty() ) continue;
3323 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
3325 // make a new name for 1D hyp = "HypType" + "_Distribution"
3327 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
3329 if ( prevNewName.IsEmpty() ) continue;
3330 newName = prevNewName;
3334 if ( hyp1d->IsWrapped() ) {
3335 newName = hyp1d->GetCreationCmd()->GetMethod();
3338 TCollection_AsciiString hypTypeQuoted = hyp1d->GetCreationCmd()->GetArg(1);
3339 newName = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
3341 newName += "_Distribution";
3342 prevNewName = newName;
3344 hyp1d->GetCreationCmd()->SetResultValue( newName );
3346 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
3347 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
3348 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
3349 const _pyID& objID = (*cmdIt)->GetObject();
3350 if ( objID == hyp1dID ) {
3351 if ( !hyp1d.IsNull() )
3353 hyp1d->Process( *cmdIt );
3354 hyp1d->GetCreationCmd()->AddDependantCmd( *cmdIt );
3356 ( *cmdIt )->SetObject( newName );
3359 // Set new hyp name to SetLayerDistribution(hyp1dID) cmd
3360 (*cmd)->SetArg( 1, newName );
3364 //================================================================================
3366 * \brief additionally to Addition2Creation, clears SetDistrType() command
3367 * \param theCmd - AddHypothesis() command
3368 * \param theMesh - mesh to which a hypothesis is added
3369 * \retval bool - convertion result
3371 //================================================================================
3373 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
3374 const _pyID& theMesh)
3376 if ( IsWrappable( theMesh ) && myCurCrMethod->myArgs.size() > 1 ) {
3377 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
3378 bool scaleDistrType = false;
3379 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnusedCommands.rbegin();
3380 for ( ; cmd != myUnusedCommands.rend(); ++cmd ) {
3381 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
3382 if ( (*cmd)->GetArg( 1 ) == "1" ) {
3383 scaleDistrType = true;
3386 else if ( !scaleDistrType ) {
3387 // distribution type changed: remove scale factor from args
3388 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
3389 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
3391 CreationMethod& crMethod = type2meth->second;
3392 if ( crMethod.myArgs.size() == 2 )
3393 crMethod.myArgs.pop_back();
3400 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
3403 //================================================================================
3405 * \brief remove repeated commands defining distribution
3407 //================================================================================
3409 void _pyNumberOfSegmentsHyp::Flush()
3411 // find number of the last SetDistrType() command
3412 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnusedCommands.rbegin();
3413 int distrTypeNb = 0;
3414 for ( ; !distrTypeNb && cmd != myUnusedCommands.rend(); ++cmd )
3415 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
3416 if ( cmd != myUnusedCommands.rbegin() )
3417 distrTypeNb = (*cmd)->GetOrderNb();
3419 else if (IsWrapped() && (*cmd)->GetMethod() == "SetObjectEntry" ) {
3422 // clear commands before the last SetDistrType()
3423 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnusedCommands };
3424 set< int > treatedCmdNbs; // avoid treating same cmd twice
3425 for ( int i = 0; i < 2; ++i ) {
3426 set<TCollection_AsciiString> uniqueMethods;
3427 list<Handle(_pyCommand)> & cmdList = *cmds[i];
3428 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
3430 if ( !treatedCmdNbs.insert( (*cmd)->GetOrderNb() ).second )
3431 continue;// avoid treating same cmd twice
3432 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
3433 const TCollection_AsciiString& method = (*cmd)->GetMethod();
3434 if ( !clear || method == "SetNumberOfSegments" ) {
3435 bool isNewInSet = uniqueMethods.insert( method ).second;
3436 clear = !isNewInSet;
3445 //================================================================================
3447 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
3448 * into regular1D.LengthNearVertex( length, vertex )
3449 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
3450 * \param theMesh - The mesh needing this hypo
3451 * \retval bool - false if the command cant be converted
3453 //================================================================================
3455 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
3456 const _pyID& theMeshID)
3458 if ( IsWrappable( theMeshID )) {
3460 _pyID vertex = theCmd->GetArg( 1 );
3462 // the problem here is that segment algo will not be found
3463 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
3464 // geometry where segment algorithm is assigned
3465 Handle(_pyHypothesis) algo;
3466 _pyID geom = vertex;
3467 while ( algo.IsNull() && !geom.IsEmpty()) {
3468 // try to find geom as a father of <vertex>
3469 geom = FatherID( geom );
3470 algo = theGen->FindAlgo( geom, theMeshID, this );
3472 if ( algo.IsNull() )
3473 return false; // also possible to find geom as brother of veretex...
3474 // set geom instead of vertex
3475 theCmd->SetArg( 1, geom );
3477 // set vertex as a second arg
3478 if ( myCurCrMethod->myArgs.size() < 1) setCreationArg( 1, "1" ); // :(
3479 setCreationArg( 2, vertex );
3481 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
3482 // theMeshID.LengthNearVertex( length, vertex )
3483 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
3488 //================================================================================
3490 * \brief _pyAlgorithm constructor
3491 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
3493 //================================================================================
3495 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
3496 : _pyHypothesis( theCreationCmd )
3501 //================================================================================
3503 * \brief Convert the command adding an algorithm to mesh
3504 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
3505 * \param theMesh - The mesh needing this algo
3506 * \retval bool - false if the command cant be converted
3508 //================================================================================
3510 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
3511 const _pyID& theMeshID)
3513 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
3514 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
3515 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
3521 //================================================================================
3523 * \brief Return starting position of a part of python command
3524 * \param thePartIndex - The index of command part
3525 * \retval int - Part position
3527 //================================================================================
3529 int _pyCommand::GetBegPos( int thePartIndex ) const
3533 if ( myBegPos.Length() < thePartIndex )
3535 ASSERT( thePartIndex > 0 );
3536 return myBegPos( thePartIndex );
3539 //================================================================================
3541 * \brief Store starting position of a part of python command
3542 * \param thePartIndex - The index of command part
3543 * \param thePosition - Part position
3545 //================================================================================
3547 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
3549 while ( myBegPos.Length() < thePartIndex )
3550 myBegPos.Append( UNKNOWN );
3551 ASSERT( thePartIndex > 0 );
3552 myBegPos( thePartIndex ) = thePosition;
3555 //================================================================================
3557 * \brief Returns whitespace symbols at the line beginning
3558 * \retval TCollection_AsciiString - result
3560 //================================================================================
3562 TCollection_AsciiString _pyCommand::GetIndentation()
3565 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
3566 GetWord( myString, end, true );
3568 end = GetBegPos( RESULT_IND );
3569 return myString.SubString( 1, Max( end - 1, 1 ));
3572 //================================================================================
3574 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
3575 * \retval const TCollection_AsciiString & - ResultValue substring
3577 //================================================================================
3579 const TCollection_AsciiString & _pyCommand::GetResultValue()
3581 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
3583 SetBegPos( RESULT_IND, EMPTY );
3584 int begPos, endPos = myString.Location( "=", 1, Length() );
3588 while ( begPos < endPos && isspace( myString.Value( begPos ))) ++begPos;
3589 if ( begPos < endPos )
3591 SetBegPos( RESULT_IND, begPos );
3593 while ( begPos < endPos && isspace( myString.Value( endPos ))) --endPos;
3594 myRes = myString.SubString( begPos, endPos );