1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/
20 // File : SMESH_Gen_i_DumpPython.cxx
21 // Created : Thu Mar 24 17:17:59 2005
22 // Author : Julia DOROVSKIKH
26 #include "SMESH_PythonDump.hxx"
27 #include "SMESH_Gen_i.hxx"
28 #include "SMESH_Filter_i.hxx"
29 #include "SMESH_MeshEditor_i.hxx"
30 #include "SMESH_2smeshpy.hxx"
32 #include <TColStd_HSequenceOfInteger.hxx>
33 #include <TCollection_AsciiString.hxx>
37 static int MYDEBUG = 0;
39 static int MYDEBUG = 0;
42 static TCollection_AsciiString NotPublishedObjectName()
44 return "__NOT__Published__Object__";
50 size_t TPythonDump::myCounter = 0;
61 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
62 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
63 if(!aStudy->_is_nil()){
64 std::string aString = myStream.str();
65 TCollection_AsciiString aCollection(Standard_CString(aString.c_str()));
66 aSMESHGen->AddToPythonScript(aStudy->StudyId(),aCollection);
67 if(MYDEBUG) MESSAGE(aString);
74 operator<<(long int theArg){
81 operator<<(int theArg){
88 operator<<(double theArg){
95 operator<<(float theArg){
102 operator<<(const void* theArg){
109 operator<<(const char* theArg){
117 operator<<(const SMESH::ElementType& theArg)
121 case ALL: myStream<<"ALL";break;
122 case NODE: myStream<<"NODE";break;
123 case EDGE: myStream<<"EDGE";break;
124 case FACE: myStream<<"FACE";break;
125 case VOLUME:myStream<<"VOLUME";break;
130 template<class TArray>
131 void DumpArray(const TArray& theArray, std::ostringstream & theStream)
134 for (int i = 1; i <= theArray.length(); i++) {
135 theStream << theArray[i-1];
136 if ( i < theArray.length() )
143 TPythonDump::operator<<(const SMESH::long_array& theArg)
145 DumpArray( theArg, myStream );
150 TPythonDump::operator<<(const SMESH::double_array& theArg)
152 DumpArray( theArg, myStream );
158 operator<<(SALOMEDS::SObject_ptr aSObject)
160 if ( !aSObject->_is_nil() )
161 myStream << aSObject->GetID();
163 myStream << NotPublishedObjectName();
169 operator<<(CORBA::Object_ptr theArg)
171 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
172 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
173 SALOMEDS::SObject_var aSObject = SMESH_Gen_i::ObjectToSObject(aStudy,theArg);
174 if(!aSObject->_is_nil()) {
175 myStream << aSObject->GetID();
176 } else if ( !CORBA::is_nil(theArg)) {
177 if ( aSMESHGen->CanPublishInStudy( theArg )) // not published SMESH object
178 myStream << "smeshObj_" << size_t(theArg);
180 myStream << NotPublishedObjectName();
189 operator<<(SMESH::FilterLibrary_i* theArg)
191 myStream<<"aFilterLibrary"<<theArg;
197 operator<<(SMESH::FilterManager_i* theArg)
199 myStream<<"aFilterManager";
205 operator<<(SMESH::Filter_i* theArg)
207 myStream<<"aFilter"<<theArg;
213 operator<<(SMESH::Functor_i* theArg)
216 FunctorType aFunctorType = theArg->GetFunctorType();
217 switch(aFunctorType){
218 case FT_AspectRatio: myStream<< "anAspectRatio"; break;
219 case FT_AspectRatio3D: myStream<< "anAspectRatio3D"; break;
220 case FT_Warping: myStream<< "aWarping"; break;
221 case FT_MinimumAngle: myStream<< "aMinimumAngle"; break;
222 case FT_Taper: myStream<< "aTaper"; break;
223 case FT_Skew: myStream<< "aSkew"; break;
224 case FT_Area: myStream<< "aArea"; break;
225 case FT_FreeBorders: myStream<< "aFreeBorders"; break;
226 case FT_FreeEdges: myStream<< "aFreeEdges"; break;
227 case FT_MultiConnection: myStream<< "aMultiConnection"; break;
228 case FT_MultiConnection2D:myStream<< "aMultiConnection2D";break;
229 case FT_Length: myStream<< "aLength"; break;
230 case FT_Length2D: myStream<< "aLength"; break;
231 case FT_BelongToGeom: myStream<< "aBelongToGeom"; break;
232 case FT_BelongToPlane: myStream<< "aBelongToPlane"; break;
233 case FT_BelongToCylinder: myStream<< "aBelongToCylinder"; break;
234 case FT_LyingOnGeom: myStream<< "aLyingOnGeom"; break;
235 case FT_RangeOfIds: myStream<< "aRangeOfIds"; break;
236 case FT_BadOrientedVolume:myStream<< "aBadOrientedVolume";break;
237 case FT_LessThan: myStream<< "aLessThan"; break;
238 case FT_MoreThan: myStream<< "aMoreThan"; break;
239 case FT_EqualTo: myStream<< "anEqualTo"; break;
240 case FT_LogicalNOT: myStream<< "aLogicalNOT"; break;
241 case FT_LogicalAND: myStream<< "aLogicalAND"; break;
242 case FT_LogicalOR: myStream<< "aLogicalOR"; break;
243 case FT_Undefined: myStream<< "anUndefined"; break;
250 TPythonDump& TPythonDump:: operator<<(SMESH_Gen_i* theArg)
252 myStream << SMESHGenName(); return *this;
255 TPythonDump& TPythonDump::operator<<(SMESH_MeshEditor_i* theArg)
257 myStream << MeshEditorName() << "_" << ( theArg ? theArg->GetMeshId() : -1 ); return *this;
260 TPythonDump& TPythonDump::operator<<(const TCollection_AsciiString & theStr)
262 myStream << theStr; return *this;
266 TPythonDump& TPythonDump::operator<<(SMESH::MED_VERSION theVersion)
268 switch (theVersion) {
269 case SMESH::MED_V2_1: myStream << "SMESH.MED_V2_1"; break;
270 case SMESH::MED_V2_2: myStream << "SMESH.MED_V2_2"; break;
271 default: myStream << theVersion;
276 TPythonDump& TPythonDump::operator<<(const SMESH::AxisStruct & theAxis)
278 myStream << "SMESH.AxisStruct( "
282 << theAxis.vx << ", "
283 << theAxis.vy << ", "
284 << theAxis.vz << " )";
288 TPythonDump& TPythonDump::operator<<(const SMESH::DirStruct & theDir)
290 const SMESH::PointStruct & P = theDir.PS;
291 myStream << "SMESH.DirStruct( SMESH.PointStruct ( "
298 TCollection_AsciiString myLongStringStart( "TPythonDump::LongStringStart" );
299 TCollection_AsciiString myLongStringEnd ( "TPythonDump::LongStringEnd" );
301 //================================================================================
303 * \brief Return marker of long string literal beginning
304 * \param type - a name of functionality producing the string literal
305 * \retval TCollection_AsciiString - the marker string to be written into
306 * a raw python script
308 //================================================================================
310 TCollection_AsciiString TPythonDump::LongStringStart(const char* type)
314 (Standard_Integer) strlen(type) +
319 //================================================================================
321 * \brief Return marker of long string literal end
322 * \retval TCollection_AsciiString - the marker string to be written into
323 * a raw python script
325 //================================================================================
327 TCollection_AsciiString TPythonDump::LongStringEnd()
329 return myLongStringEnd;
332 //================================================================================
334 * \brief Cut out a long string literal from a string
335 * \param theText - text possibly containing string literals
336 * \param theFrom - position in the text to search from
337 * \param theLongString - the retrieved literal
338 * \param theStringType - a name of functionality produced the literal
339 * \retval bool - true if a string literal found
341 * The literal is removed from theText; theFrom points position right after
342 * the removed literal
344 //================================================================================
346 bool TPythonDump::CutoutLongString( TCollection_AsciiString & theText,
348 TCollection_AsciiString & theLongString,
349 TCollection_AsciiString & theStringType)
351 if ( theFrom < 1 || theFrom > theText.Length() )
354 // ...script \ beg marker \ \ type \ literal \ end marker \ script...
355 // "theText myLongStringStart7 Pattern!!! SALOME Mesh Pattern file myLongStringEndtextEnd"
356 // 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
359 theFrom = theText.Location( myLongStringStart, theFrom, theText.Length() ); // = 09
363 // find where literal begins
364 int literalBeg = theFrom + myLongStringStart.Length(); // = 26
365 char* typeLenStr = theText.ToCString() + literalBeg - 1; // = "7 Pattern!!! SALO...."
366 int typeLen = atoi ( typeLenStr ); // = 7
367 while ( *typeLenStr != ' ' ) { // look for ' ' after typeLen
368 literalBeg++; // 26 -> 27
371 literalBeg += typeLen + 1; // = 35
372 if ( literalBeg > theText.Length() )
375 // where literal ends (i.e. end marker begins)
376 int literalEnd = theText.Location( myLongStringEnd, literalBeg, theText.Length() ); // = 64
378 literalEnd = theText.Length();
381 theLongString = theText.SubString( literalBeg, literalEnd - 1); // "!!! SALOME Mesh Pattern file "
383 theStringType = theText.SubString( literalBeg - typeLen, literalBeg - 1 ); // "Pattern"
385 literalEnd += myLongStringEnd.Length(); // = 79
386 TCollection_AsciiString textEnd = theText.SubString( literalEnd, theText.Length() ); // "textE..."
387 theText = theText.SubString( 1, theFrom - 1 ) + textEnd;
393 //=======================================================================
394 //function : DumpPython
396 //=======================================================================
397 Engines::TMPFile* SMESH_Gen_i::DumpPython (CORBA::Object_ptr theStudy,
398 CORBA::Boolean isPublished,
399 CORBA::Boolean& isValidScript)
401 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
402 if (CORBA::is_nil(aStudy))
403 return new Engines::TMPFile(0);
405 SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
406 if (CORBA::is_nil(aSO))
407 return new Engines::TMPFile(0);
409 // Map study entries to object names
410 Resource_DataMapOfAsciiStringAsciiString aMap;
411 Resource_DataMapOfAsciiStringAsciiString aMapNames;
412 //TCollection_AsciiString s ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
414 SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
415 for (Itr->InitEx(true); Itr->More(); Itr->Next()) {
416 SALOMEDS::SObject_var aValue = Itr->Value();
417 CORBA::String_var anID = aValue->GetID();
418 CORBA::String_var aName = aValue->GetName();
419 TCollection_AsciiString aGUIName ( (char*) aName.in() );
420 TCollection_AsciiString anEnrty ( (char*) anID.in() );
421 if (aGUIName.Length() > 0) {
422 aMapNames.Bind( anEnrty, aGUIName );
423 aMap.Bind( anEnrty, aGUIName );
427 // Get trace of restored study
428 //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
429 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
430 SALOMEDS::GenericAttribute_var anAttr =
431 aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
433 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
434 TCollection_AsciiString aSavedTrace (oldValue);
436 // Add trace of API methods calls and replace study entries by names
437 TCollection_AsciiString aScript =
438 "### This file is generated by SALOME automatically by dump python functionality of SMESH component\n\n";
439 aScript += DumpPython_impl(aStudy->StudyId(), aMap, aMapNames,
440 isPublished, isValidScript, aSavedTrace);
442 int aLen = aScript.Length();
443 unsigned char* aBuffer = new unsigned char[aLen+1];
444 strcpy((char*)aBuffer, aScript.ToCString());
446 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
447 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
449 bool hasNotPublishedObjects = aScript.Location( NotPublishedObjectName(), 1, aLen);
450 isValidScript = isValidScript && !hasNotPublishedObjects;
452 return aStreamFile._retn();
455 //=============================================================================
459 //=============================================================================
460 void SMESH_Gen_i::AddToPythonScript (int theStudyID, const TCollection_AsciiString& theString)
462 if (myPythonScripts.find(theStudyID) == myPythonScripts.end()) {
463 myPythonScripts[theStudyID] = new TColStd_HSequenceOfAsciiString;
465 myPythonScripts[theStudyID]->Append(theString);
468 //=============================================================================
470 * RemoveLastFromPythonScript
472 //=============================================================================
473 void SMESH_Gen_i::RemoveLastFromPythonScript (int theStudyID)
475 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
476 int aLen = myPythonScripts[theStudyID]->Length();
477 myPythonScripts[theStudyID]->Remove(aLen);
481 //=======================================================================
482 //function : SavePython
484 //=======================================================================
485 void SMESH_Gen_i::SavePython (SALOMEDS::Study_ptr theStudy)
487 // Dump trace of API methods calls
488 TCollection_AsciiString aScript = GetNewPythonLines(theStudy->StudyId());
490 // Check contents of PythonObject attribute
491 SALOMEDS::SObject_var aSO = theStudy->FindComponent(ComponentDataType());
492 //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
493 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
494 SALOMEDS::GenericAttribute_var anAttr =
495 aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
497 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
498 TCollection_AsciiString oldScript (oldValue);
500 if (oldScript.Length() > 0) {
502 oldScript += aScript;
507 // Store in PythonObject attribute
508 SALOMEDS::AttributePythonObject::_narrow(anAttr)->SetObject(oldScript.ToCString(), 1);
510 // Clean trace of API methods calls
511 CleanPythonTrace(theStudy->StudyId());
518 //=============================================================================
520 * FindEntries: Returns a sequence of start/end positions of entries in the string
522 //=============================================================================
523 Handle(TColStd_HSequenceOfInteger) FindEntries (TCollection_AsciiString& theString)
525 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
526 Standard_Integer aLen = theString.Length();
527 Standard_Boolean isFound = Standard_False;
529 char* arr = theString.ToCString();
530 Standard_Integer i = 0, j;
535 if ( isdigit( c )) { //Is digit?
537 isFound = Standard_False;
538 while((j < aLen) && ( isdigit(c) || c == ':' )) { //Check if it is an entry
540 if(c == ':') isFound = Standard_True;
544 int prev = (i < 1) ? 0 : (int)arr[i - 1];
545 // to distinguish from a sketcher command:
546 // last char should be a digit, not ":",
547 // previous char should not be '"'.
548 if (arr[j-2] != ':' && prev != '"') {
549 aSeq->Append(i+1); // +1 because AsciiString starts from 1
563 //================================================================================
565 * \brief Make a string be a valid python name
566 * \param aName - a string to fix
567 * \retval bool - true if aName was not modified
569 //================================================================================
571 bool fixPythonName(TCollection_AsciiString & aName )
573 const TCollection_AsciiString allowedChars =
574 "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_";
575 bool isValidName = true;
576 int p=1; // replace not allowed chars with underscore
577 while (p <= aName.Length() &&
578 (p = aName.FirstLocationNotInSet(allowedChars, p, aName.Length())))
580 if ( p == 1 || p == aName.Length() || aName.Value(p-1) == '_')
581 aName.Remove( p, 1 ); // remove double _ and from the start and the end
583 aName.SetValue(p, '_');
586 if ( aName.IsIntegerValue() ) { // aName must not start with a digit
587 aName.Insert( 1, 'a' );
594 //=============================================================================
598 //=============================================================================
599 TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
601 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
602 Resource_DataMapOfAsciiStringAsciiString& theNames,
605 const TCollection_AsciiString& theSavedTrace)
607 TCollection_AsciiString helper; // to comfortably concatenate C strings
608 TCollection_AsciiString aSmeshpy( SMESH_2smeshpy::SmeshpyName() );
609 TCollection_AsciiString aSMESHGen( SMESH_2smeshpy::GenName() );
610 TCollection_AsciiString anOldGen( SMESH::TPythonDump::SMESHGenName() );
612 TCollection_AsciiString aScript;
613 aScript = "def RebuildData(theStudy):\n\t";
614 aScript += helper + "aFilterManager = " + aSMESHGen + ".CreateFilterManager()\n\t";
616 aScript += aSMESHGen + ".SetCurrentStudy(theStudy)";
618 aScript += aSMESHGen + ".SetCurrentStudy(None)";
620 // import python files corresponding to plugins
621 set<string> moduleNameSet;
622 map<string, GenericHypothesisCreator_i*>::iterator hyp_creator = myHypCreatorMap.begin();
623 for ( ; hyp_creator != myHypCreatorMap.end(); ++hyp_creator ) {
624 string moduleName = hyp_creator->second->GetModuleName();
625 bool newModule = moduleNameSet.insert( moduleName ).second;
627 aScript += helper + "\n\t" + "import " + (char*) moduleName.c_str();
630 // Dump trace of restored study
631 if (theSavedTrace.Length() > 0) {
632 // For the convertion of IDL API calls -> smesh.py API, "smesh" standing for SMESH_Gen
633 // was replaces with "smeshgen" (==TPythonDump::SMESHGenName()).
634 // Change "smesh" -> "smeshgen" in the trace saved before passage to smesh.py API
636 theSavedTrace.Location( anOldGen + ".", 1, theSavedTrace.Length() );
637 if ( !isNewVersion ) {
638 TCollection_AsciiString aSavedTrace( theSavedTrace );
639 TCollection_AsciiString aSmeshCall ( "smesh." ), gen( "gen" );
640 int beg, end = aSavedTrace.Length(), from = 1;
641 while ( from < end && ( beg = aSavedTrace.Location( aSmeshCall, from, end ))) {
642 char charBefore = ( beg == 1 ) ? ' ' : aSavedTrace.Value( beg - 1 );
643 if ( isspace( charBefore ) || charBefore == '=' ) { // "smesh." is not a part of a long word
644 aSavedTrace.Insert( beg + aSmeshCall.Length() - 1, gen );// "smesh" -> "smeshgen"
647 from = beg + aSmeshCall.Length();
649 aScript += helper + "\n" + aSavedTrace;
652 // append a saved trace to the script
653 aScript += helper + "\n" + theSavedTrace;
656 // Dump trace of API methods calls
657 TCollection_AsciiString aNewLines = GetNewPythonLines(theStudyID);
658 if (aNewLines.Length() > 0) {
659 aScript += helper + "\n" + aNewLines;
662 // Convert IDL API calls into smesh.py API.
663 // Some objects are wrapped with python classes and
664 // Resource_DataMapOfAsciiStringAsciiString holds methods returning wrapped objects
665 Resource_DataMapOfAsciiStringAsciiString anEntry2AccessorMethod;
666 aScript = SMESH_2smeshpy::ConvertScript( aScript, anEntry2AccessorMethod );
668 // Find entries to be replaced by names
669 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
670 Standard_Integer aLen = aSeq->Length();
675 // Replace entries by the names
676 GEOM::GEOM_Gen_ptr geom = GetGeomEngine();
677 TColStd_SequenceOfAsciiString seqRemoved;
678 Resource_DataMapOfAsciiStringAsciiString mapRemoved;
679 Standard_Integer objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
680 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("smeshObj_");
682 // Collect names of GEOM objects to exclude same names for SMESH objects
683 GEOM::string_array_var aGeomNames = geom->GetAllDumpNames();
684 int ign = 0, nbgn = aGeomNames->length();
685 for (; ign < nbgn; ign++) {
686 aName = aGeomNames[ign];
687 theObjectNames.Bind(aName, "1");
690 bool importGeom = false;
691 for (Standard_Integer i = 1; i <= aLen; i += 2) {
692 anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i) - 1);
693 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
695 aName = geom->GetDumpName( anEntry.ToCString() );
696 if (aName.IsEmpty()) {
698 if (theObjectNames.IsBound(anEntry)) {
699 // The Object is in Study
700 aName = theObjectNames.Find(anEntry);
701 // check validity of aName
702 bool isValidName = fixPythonName( aName );
703 if (theObjectNames.IsBound(aName) && anEntry != theObjectNames(aName)) {
704 // diff objects have same name - make a new name by appending a digit
705 TCollection_AsciiString aName2;
706 Standard_Integer i = 0;
708 aName2 = aName + "_" + ++i;
709 } while (theObjectNames.IsBound(aName2) && anEntry != theObjectNames(aName2));
714 theObjectNames(anEntry) = aName;
719 aName = aBaseName + (++objectCounter);
720 } while (theObjectNames.IsBound(aName));
721 seqRemoved.Append(aName);
722 mapRemoved.Bind(anEntry, "1");
723 theObjectNames.Bind(anEntry, aName);
725 theObjectNames.Bind(aName, anEntry); // to detect same name of diff objects
731 anUpdatedScript += aName;
732 aStart = aSeq->Value(i + 1) + 1;
735 // set initial part of aSript
736 TCollection_AsciiString initPart = "import salome, SMESH\n";
737 initPart += helper + "import " + aSmeshpy + "\n\n";
740 initPart += ("## import GEOM dump file ## \n"
741 "import string, os, sys, re\n"
742 "sys.path.insert( 0, os.path.dirname(__file__) )\n"
743 "exec(\"from \"+re.sub(\"SMESH$\",\"GEOM\",__name__)+\" import *\")\n\n");
745 anUpdatedScript.Insert ( 1, initPart );
747 // add final part of aScript
748 if (aSeq->Value(aLen) < aScriptLength)
749 anUpdatedScript += aScript.SubString(aSeq->Value(aLen) + 1, aScriptLength);
751 // Remove removed objects
752 if ( seqRemoved.Length() > 0 ) {
753 anUpdatedScript += "\n\t## some objects were removed";
754 anUpdatedScript += "\n\taStudyBuilder = theStudy.NewBuilder()";
756 for (int ir = 1; ir <= seqRemoved.Length(); ir++) {
757 anUpdatedScript += "\n\tSO = theStudy.FindObjectIOR(theStudy.ConvertObjectToIOR(";
758 anUpdatedScript += seqRemoved.Value(ir);
759 // for object wrapped by class of smesh.py
760 anEntry = theObjectNames( seqRemoved.Value(ir) );
761 if ( anEntry2AccessorMethod.IsBound( anEntry ) )
762 anUpdatedScript += helper + "." + anEntry2AccessorMethod( anEntry );
763 anUpdatedScript += "))\n\tif SO is not None: aStudyBuilder.RemoveObjectWithChildren(SO)";
767 anUpdatedScript += "\n\t## set object names";
768 anUpdatedScript += helper + " \n\tisGUIMode = " + isPublished;
769 anUpdatedScript += "\n\tif isGUIMode and salome.sg.hasDesktop():";
770 // anUpdatedScript += "\n\t\tsmeshgui = salome.ImportComponentGUI(\"SMESH\")";
771 // anUpdatedScript += "\n\t\tsmeshgui.Init(theStudy._get_StudyId())";
772 // anUpdatedScript += "\n";
774 TCollection_AsciiString aGUIName;
775 Resource_DataMapOfAsciiStringAsciiString mapEntries;
776 for (Standard_Integer i = 1; i <= aLen; i += 2)
778 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
779 aName = geom->GetDumpName( anEntry.ToCString() );
780 if (aName.IsEmpty() && // Not a GEOM object
781 theNames.IsBound(anEntry) &&
782 !mapEntries.IsBound(anEntry) && // Not yet processed
783 !mapRemoved.IsBound(anEntry)) // Was not removed
785 aName = theObjectNames.Find(anEntry);
786 aGUIName = theNames.Find(anEntry);
787 mapEntries.Bind(anEntry, aName);
788 anUpdatedScript += helper + "\n\t\t" + aSmeshpy + ".SetName(" + aName;
789 if ( anEntry2AccessorMethod.IsBound( anEntry ) )
790 anUpdatedScript += helper + "." + anEntry2AccessorMethod( anEntry );
791 anUpdatedScript += helper + ", '" + aGUIName + "')";
794 anUpdatedScript += "\n\n\t\tsalome.sg.updateObjBrowser(0)";
796 anUpdatedScript += "\n\n\tpass\n";
798 // -----------------------------------------------------------------
799 // put string literals describing patterns into separate functions
800 // -----------------------------------------------------------------
802 TCollection_AsciiString aLongString, aFunctionType;
804 set< string > functionNameSet;
805 while ( SMESH::TPythonDump::CutoutLongString( anUpdatedScript, where, aLongString, aFunctionType ))
807 // make a python string literal
808 aLongString.Prepend(":\n\treturn '''\n");
809 aLongString += "\n\t'''\n\tpass\n";
811 TCollection_AsciiString functionName;
813 // check if the function returning this literal is already defined
814 int posAlready = anUpdatedScript.Location( aLongString, where, anUpdatedScript.Length() );
815 if ( posAlready ) // already defined
817 // find the function name
818 int functBeg = posAlready;
819 char* script = anUpdatedScript.ToCString() + posAlready - 1; // look at ":" after "def fuction()"
820 while ( *script != ' ' ) {
824 functBeg++; // do not take ' '
825 posAlready--; // do not take ':'
826 functionName = anUpdatedScript.SubString( functBeg, posAlready );
828 else // not defined yet
830 // find a unique function name
831 fixPythonName( aFunctionType );
832 Standard_Integer nb = 0;
833 do functionName = aFunctionType + "_" + ( nb++ ) + "()";
834 while ( !functionNameSet.insert( functionName.ToCString() ).second );
836 anUpdatedScript += helper + "\n\ndef " + functionName + aLongString; // define function
838 anUpdatedScript.InsertBefore( where, functionName ); // call function
843 return anUpdatedScript;
846 //=============================================================================
850 //=============================================================================
851 TCollection_AsciiString SMESH_Gen_i::GetNewPythonLines (int theStudyID)
853 TCollection_AsciiString aScript;
855 // Dump trace of API methods calls
856 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
857 Handle(TColStd_HSequenceOfAsciiString) aPythonScript = myPythonScripts[theStudyID];
858 Standard_Integer istr, aLen = aPythonScript->Length();
859 for (istr = 1; istr <= aLen; istr++) {
861 aScript += aPythonScript->Value(istr);
869 //=============================================================================
873 //=============================================================================
874 void SMESH_Gen_i::CleanPythonTrace (int theStudyID)
876 TCollection_AsciiString aScript;
878 // Clean trace of API methods calls
879 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
880 myPythonScripts[theStudyID]->Clear();