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/ or email : webmaster.salome@opencascade.com
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_BelongToGenSurface:myStream<<"aBelongToGenSurface";break;
235 case FT_LyingOnGeom: myStream<< "aLyingOnGeom"; break;
236 case FT_RangeOfIds: myStream<< "aRangeOfIds"; break;
237 case FT_BadOrientedVolume:myStream<< "aBadOrientedVolume";break;
238 case FT_LessThan: myStream<< "aLessThan"; break;
239 case FT_MoreThan: myStream<< "aMoreThan"; break;
240 case FT_EqualTo: myStream<< "anEqualTo"; break;
241 case FT_LogicalNOT: myStream<< "aLogicalNOT"; break;
242 case FT_LogicalAND: myStream<< "aLogicalAND"; break;
243 case FT_LogicalOR: myStream<< "aLogicalOR"; break;
244 case FT_Undefined: myStream<< "anUndefined"; break;
251 TPythonDump& TPythonDump:: operator<<(SMESH_Gen_i* theArg)
253 myStream << SMESHGenName(); return *this;
256 TPythonDump& TPythonDump::operator<<(SMESH_MeshEditor_i* theArg)
258 myStream << MeshEditorName() << "_" << ( theArg ? theArg->GetMeshId() : -1 ); return *this;
261 TPythonDump& TPythonDump::operator<<(const TCollection_AsciiString & theStr)
263 myStream << theStr; return *this;
267 TPythonDump& TPythonDump::operator<<(SMESH::MED_VERSION theVersion)
269 switch (theVersion) {
270 case SMESH::MED_V2_1: myStream << "SMESH.MED_V2_1"; break;
271 case SMESH::MED_V2_2: myStream << "SMESH.MED_V2_2"; break;
272 default: myStream << theVersion;
277 TPythonDump& TPythonDump::operator<<(const SMESH::AxisStruct & theAxis)
279 myStream << "SMESH.AxisStruct( "
283 << theAxis.vx << ", "
284 << theAxis.vy << ", "
285 << theAxis.vz << " )";
289 TPythonDump& TPythonDump::operator<<(const SMESH::DirStruct & theDir)
291 const SMESH::PointStruct & P = theDir.PS;
292 myStream << "SMESH.DirStruct( SMESH.PointStruct ( "
299 TPythonDump& TPythonDump::operator<<(const SMESH::ListOfGroups * theList){
300 if(theList && theList->length() > 0 ) {
301 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
302 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
304 int aListLen = theList->length();
305 for(int i = 0 ; i < aListLen; i++){
306 SALOMEDS::SObject_var aSObject = SMESH_Gen_i::ObjectToSObject(aStudy,(*theList)[i]);
307 if(!aSObject->_is_nil()) {
308 myStream << aSObject->GetID();
309 i < (aListLen - 1) ? myStream<<", " : myStream<<"]";
317 TCollection_AsciiString myLongStringStart( "TPythonDump::LongStringStart" );
318 TCollection_AsciiString myLongStringEnd ( "TPythonDump::LongStringEnd" );
320 //================================================================================
322 * \brief Return marker of long string literal beginning
323 * \param type - a name of functionality producing the string literal
324 * \retval TCollection_AsciiString - the marker string to be written into
325 * a raw python script
327 //================================================================================
329 TCollection_AsciiString TPythonDump::LongStringStart(const char* type)
333 (Standard_Integer) strlen(type) +
338 //================================================================================
340 * \brief Return marker of long string literal end
341 * \retval TCollection_AsciiString - the marker string to be written into
342 * a raw python script
344 //================================================================================
346 TCollection_AsciiString TPythonDump::LongStringEnd()
348 return myLongStringEnd;
351 //================================================================================
353 * \brief Cut out a long string literal from a string
354 * \param theText - text possibly containing string literals
355 * \param theFrom - position in the text to search from
356 * \param theLongString - the retrieved literal
357 * \param theStringType - a name of functionality produced the literal
358 * \retval bool - true if a string literal found
360 * The literal is removed from theText; theFrom points position right after
361 * the removed literal
363 //================================================================================
365 bool TPythonDump::CutoutLongString( TCollection_AsciiString & theText,
367 TCollection_AsciiString & theLongString,
368 TCollection_AsciiString & theStringType)
370 if ( theFrom < 1 || theFrom > theText.Length() )
373 // ...script \ beg marker \ \ type \ literal \ end marker \ script...
374 // "theText myLongStringStart7 Pattern!!! SALOME Mesh Pattern file myLongStringEndtextEnd"
375 // 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
378 theFrom = theText.Location( myLongStringStart, theFrom, theText.Length() ); // = 09
382 // find where literal begins
383 int literalBeg = theFrom + myLongStringStart.Length(); // = 26
384 char* typeLenStr = (char*) theText.ToCString() + literalBeg - 1; // = "7 Pattern!!! SALO...."
385 int typeLen = atoi ( typeLenStr ); // = 7
386 while ( *typeLenStr != ' ' ) { // look for ' ' after typeLen
387 literalBeg++; // 26 -> 27
390 literalBeg += typeLen + 1; // = 35
391 if ( literalBeg > theText.Length() )
394 // where literal ends (i.e. end marker begins)
395 int literalEnd = theText.Location( myLongStringEnd, literalBeg, theText.Length() ); // = 64
397 literalEnd = theText.Length();
400 theLongString = theText.SubString( literalBeg, literalEnd - 1); // "!!! SALOME Mesh Pattern file "
402 theStringType = theText.SubString( literalBeg - typeLen, literalBeg - 1 ); // "Pattern"
404 literalEnd += myLongStringEnd.Length(); // = 79
405 TCollection_AsciiString textEnd = theText.SubString( literalEnd, theText.Length() ); // "textE..."
406 theText = theText.SubString( 1, theFrom - 1 ) + textEnd;
412 //=======================================================================
413 //function : DumpPython
415 //=======================================================================
416 Engines::TMPFile* SMESH_Gen_i::DumpPython (CORBA::Object_ptr theStudy,
417 CORBA::Boolean isPublished,
418 CORBA::Boolean& isValidScript)
420 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
421 if (CORBA::is_nil(aStudy))
422 return new Engines::TMPFile(0);
424 SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
425 if (CORBA::is_nil(aSO))
426 return new Engines::TMPFile(0);
428 // Map study entries to object names
429 Resource_DataMapOfAsciiStringAsciiString aMap;
430 Resource_DataMapOfAsciiStringAsciiString aMapNames;
431 //TCollection_AsciiString s ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
433 SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
434 for (Itr->InitEx(true); Itr->More(); Itr->Next()) {
435 SALOMEDS::SObject_var aValue = Itr->Value();
436 CORBA::String_var anID = aValue->GetID();
437 CORBA::String_var aName = aValue->GetName();
438 TCollection_AsciiString aGUIName ( (char*) aName.in() );
439 TCollection_AsciiString anEnrty ( (char*) anID.in() );
440 if (aGUIName.Length() > 0) {
441 aMapNames.Bind( anEnrty, aGUIName );
442 aMap.Bind( anEnrty, aGUIName );
446 // Get trace of restored study
447 //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
448 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
449 SALOMEDS::GenericAttribute_var anAttr =
450 aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
452 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
453 TCollection_AsciiString aSavedTrace (oldValue);
455 // Add trace of API methods calls and replace study entries by names
456 TCollection_AsciiString aScript =
457 "### This file is generated by SALOME automatically by dump python functionality of SMESH component\n\n";
458 aScript += DumpPython_impl(aStudy, aMap, aMapNames,
459 isPublished, isValidScript, aSavedTrace);
461 int aLen = aScript.Length();
462 unsigned char* aBuffer = new unsigned char[aLen+1];
463 strcpy((char*)aBuffer, aScript.ToCString());
465 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
466 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
468 bool hasNotPublishedObjects = aScript.Location( NotPublishedObjectName(), 1, aLen);
469 isValidScript = isValidScript && !hasNotPublishedObjects;
471 return aStreamFile._retn();
474 //=============================================================================
478 //=============================================================================
479 void SMESH_Gen_i::AddToPythonScript (int theStudyID, const TCollection_AsciiString& theString)
481 if (myPythonScripts.find(theStudyID) == myPythonScripts.end()) {
482 myPythonScripts[theStudyID] = new TColStd_HSequenceOfAsciiString;
484 myPythonScripts[theStudyID]->Append(theString);
487 //=============================================================================
489 * RemoveLastFromPythonScript
491 //=============================================================================
492 void SMESH_Gen_i::RemoveLastFromPythonScript (int theStudyID)
494 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
495 int aLen = myPythonScripts[theStudyID]->Length();
496 myPythonScripts[theStudyID]->Remove(aLen);
500 //=======================================================================
501 //function : SavePython
503 //=======================================================================
504 void SMESH_Gen_i::SavePython (SALOMEDS::Study_ptr theStudy)
506 // Dump trace of API methods calls
507 TCollection_AsciiString aScript = GetNewPythonLines(theStudy->StudyId());
509 // Check contents of PythonObject attribute
510 SALOMEDS::SObject_var aSO = theStudy->FindComponent(ComponentDataType());
511 //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
512 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
513 SALOMEDS::GenericAttribute_var anAttr =
514 aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
516 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
517 TCollection_AsciiString oldScript (oldValue);
519 if (oldScript.Length() > 0) {
521 oldScript += aScript;
526 // Store in PythonObject attribute
527 SALOMEDS::AttributePythonObject::_narrow(anAttr)->SetObject(oldScript.ToCString(), 1);
529 // Clean trace of API methods calls
530 CleanPythonTrace(theStudy->StudyId());
537 //=============================================================================
539 * FindEntries: Returns a sequence of start/end positions of entries in the string
541 //=============================================================================
542 Handle(TColStd_HSequenceOfInteger) FindEntries (TCollection_AsciiString& theString)
544 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
545 Standard_Integer aLen = theString.Length();
546 Standard_Boolean isFound = Standard_False;
548 char* arr = (char*) theString.ToCString();
549 Standard_Integer i = 0, j;
554 if ( isdigit( c )) { //Is digit?
556 isFound = Standard_False;
557 while((j < aLen) && ( isdigit(c) || c == ':' )) { //Check if it is an entry
559 if(c == ':') isFound = Standard_True;
563 int prev = (i < 1) ? 0 : (int)arr[i - 1];
564 // to distinguish from a sketcher command:
565 // last char should be a digit, not ":",
566 // previous char should not be '"'.
567 if (arr[j-2] != ':' && prev != '"') {
568 aSeq->Append(i+1); // +1 because AsciiString starts from 1
582 //================================================================================
584 * \brief Make a string be a valid python name
585 * \param aName - a string to fix
586 * \retval bool - true if aName was not modified
588 //================================================================================
590 bool fixPythonName(TCollection_AsciiString & aName )
592 const TCollection_AsciiString allowedChars =
593 "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_";
594 bool isValidName = true;
595 int p=1; // replace not allowed chars with underscore
596 while (p <= aName.Length() &&
597 (p = aName.FirstLocationNotInSet(allowedChars, p, aName.Length())))
599 if ( p == 1 || p == aName.Length() || aName.Value(p-1) == '_')
600 aName.Remove( p, 1 ); // remove double _ and from the start and the end
602 aName.SetValue(p, '_');
605 if ( aName.IsIntegerValue() ) { // aName must not start with a digit
606 aName.Insert( 1, 'a' );
613 //=============================================================================
617 //=============================================================================
618 TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
619 (SALOMEDS::Study_ptr theStudy,
620 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
621 Resource_DataMapOfAsciiStringAsciiString& theNames,
624 const TCollection_AsciiString& theSavedTrace)
626 int aStudyID = theStudy->StudyId();
628 TCollection_AsciiString helper; // to comfortably concatenate C strings
629 TCollection_AsciiString aSmeshpy( SMESH_2smeshpy::SmeshpyName() );
630 TCollection_AsciiString aSMESHGen( SMESH_2smeshpy::GenName() );
631 TCollection_AsciiString anOldGen( SMESH::TPythonDump::SMESHGenName() );
633 TCollection_AsciiString aScript;
634 aScript = "def RebuildData(theStudy):\n\t";
635 aScript += helper + "aFilterManager = " + aSMESHGen + ".CreateFilterManager()\n\t";
637 aScript += aSMESHGen + ".SetCurrentStudy(theStudy)";
639 aScript += aSMESHGen + ".SetCurrentStudy(None)";
641 // import python files corresponding to plugins
642 set<string> moduleNameSet;
643 map<string, GenericHypothesisCreator_i*>::iterator hyp_creator = myHypCreatorMap.begin();
644 for ( ; hyp_creator != myHypCreatorMap.end(); ++hyp_creator ) {
645 string moduleName = hyp_creator->second->GetModuleName();
646 bool newModule = moduleNameSet.insert( moduleName ).second;
648 aScript += helper + "\n\t" + "import " + (char*) moduleName.c_str();
651 // Dump trace of restored study
652 if (theSavedTrace.Length() > 0) {
653 // For the convertion of IDL API calls -> smesh.py API, "smesh" standing for SMESH_Gen
654 // was replaces with "smeshgen" (==TPythonDump::SMESHGenName()).
655 // Change "smesh" -> "smeshgen" in the trace saved before passage to smesh.py API
657 theSavedTrace.Location( anOldGen + ".", 1, theSavedTrace.Length() );
658 if ( !isNewVersion ) {
659 TCollection_AsciiString aSavedTrace( theSavedTrace );
660 TCollection_AsciiString aSmeshCall ( "smesh." ), gen( "gen" );
661 int beg, end = aSavedTrace.Length(), from = 1;
662 while ( from < end && ( beg = aSavedTrace.Location( aSmeshCall, from, end ))) {
663 char charBefore = ( beg == 1 ) ? ' ' : aSavedTrace.Value( beg - 1 );
664 if ( isspace( charBefore ) || charBefore == '=' ) { // "smesh." is not a part of a long word
665 aSavedTrace.Insert( beg + aSmeshCall.Length() - 1, gen );// "smesh" -> "smeshgen"
668 from = beg + aSmeshCall.Length();
670 aScript += helper + "\n" + aSavedTrace;
673 // append a saved trace to the script
674 aScript += helper + "\n" + theSavedTrace;
677 // Dump trace of API methods calls
678 TCollection_AsciiString aNewLines = GetNewPythonLines(aStudyID);
679 if (aNewLines.Length() > 0) {
680 aScript += helper + "\n" + aNewLines;
683 // Convert IDL API calls into smesh.py API.
684 // Some objects are wrapped with python classes and
685 // Resource_DataMapOfAsciiStringAsciiString holds methods returning wrapped objects
686 Resource_DataMapOfAsciiStringAsciiString anEntry2AccessorMethod;
687 aScript = SMESH_2smeshpy::ConvertScript( aScript, anEntry2AccessorMethod );
689 // Find entries to be replaced by names
690 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
691 Standard_Integer aLen = aSeq->Length();
696 // Replace entries by the names
697 GEOM::GEOM_Gen_ptr geom = GetGeomEngine();
698 TColStd_SequenceOfAsciiString seqRemoved;
699 Resource_DataMapOfAsciiStringAsciiString mapRemoved;
700 Standard_Integer objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
701 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("smeshObj_");
703 // Collect names of GEOM objects to exclude same names for SMESH objects
704 GEOM::string_array_var aGeomNames = geom->GetAllDumpNames();
705 int ign = 0, nbgn = aGeomNames->length();
706 for (; ign < nbgn; ign++) {
707 aName = aGeomNames[ign];
708 theObjectNames.Bind(aName, "1");
711 bool importGeom = false;
712 for (Standard_Integer i = 1; i <= aLen; i += 2) {
713 anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i) - 1);
714 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
716 aName = geom->GetDumpName( anEntry.ToCString() );
717 if (aName.IsEmpty()) {
719 if (theObjectNames.IsBound(anEntry)) {
720 // The Object is in Study
721 aName = theObjectNames.Find(anEntry);
722 // check validity of aName
723 bool isValidName = fixPythonName( aName );
724 if (theObjectNames.IsBound(aName) && anEntry != theObjectNames(aName)) {
725 // diff objects have same name - make a new name by appending a digit
726 TCollection_AsciiString aName2;
727 Standard_Integer i = 0;
729 aName2 = aName + "_" + ++i;
730 } while (theObjectNames.IsBound(aName2) && anEntry != theObjectNames(aName2));
735 theObjectNames(anEntry) = aName;
740 aName = aBaseName + (++objectCounter);
741 } while (theObjectNames.IsBound(aName));
742 seqRemoved.Append(aName);
743 mapRemoved.Bind(anEntry, "1");
744 theObjectNames.Bind(anEntry, aName);
746 theObjectNames.Bind(aName, anEntry); // to detect same name of diff objects
752 anUpdatedScript += aName;
753 aStart = aSeq->Value(i + 1) + 1;
756 // set initial part of aSript
757 TCollection_AsciiString initPart = "import salome, SMESH\n";
758 initPart += helper + "import " + aSmeshpy + "\n\n";
761 initPart += ("## import GEOM dump file ## \n"
762 "import string, os, sys, re\n"
763 "sys.path.insert( 0, os.path.dirname(__file__) )\n"
764 "exec(\"from \"+re.sub(\"SMESH$\",\"GEOM\",__name__)+\" import *\")\n\n");
766 anUpdatedScript.Insert ( 1, initPart );
768 // add final part of aScript
769 if (aSeq->Value(aLen) < aScriptLength)
770 anUpdatedScript += aScript.SubString(aSeq->Value(aLen) + 1, aScriptLength);
773 //SMESH_Gen_i* aSMESHGenI = SMESH_Gen_i::GetSMESHGen();
774 if( !CORBA::is_nil(theStudy) )
776 SALOMEDS::SObject_var aComp = theStudy->FindComponent(ComponentDataType());
777 if( !CORBA::is_nil(aComp) )
779 SALOMEDS::ChildIterator_var Itr = theStudy->NewChildIterator(aComp);
780 for( Itr->InitEx(true); Itr->More(); Itr->Next() )
782 SALOMEDS::SObject_var aSObj = Itr->Value();
783 CORBA::String_var aName = aSObj->GetName();
785 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( SMESH_Gen_i::SObjectToObject( aSObj ) );
786 if( !CORBA::is_nil(aMesh) )
788 bool isAutoColor = aMesh->GetAutoColor();
791 anUpdatedScript += "\n\t";
792 anUpdatedScript += (char*)aName.in();
793 anUpdatedScript += ".SetAutoColor(1)";
797 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow( SMESH_Gen_i::SObjectToObject( aSObj ) );
798 if( !CORBA::is_nil(aGroup) )
800 SALOMEDS::Color aColor = aGroup->GetColor();
801 if ( aColor.R > 0 || aColor.G > 0 || aColor.B > 0 )
803 anUpdatedScript += "\n\t";
804 anUpdatedScript += (char*)aName.in();
805 anUpdatedScript += ".SetColor(SALOMEDS.Color(";
806 anUpdatedScript += aColor.R;
807 anUpdatedScript += ",";
808 anUpdatedScript += aColor.G;
809 anUpdatedScript += ",";
810 anUpdatedScript += aColor.B;
811 anUpdatedScript += "))";
818 // Remove removed objects
819 if ( seqRemoved.Length() > 0 ) {
820 anUpdatedScript += "\n\t## some objects were removed";
821 anUpdatedScript += "\n\taStudyBuilder = theStudy.NewBuilder()";
823 for (int ir = 1; ir <= seqRemoved.Length(); ir++) {
824 anUpdatedScript += "\n\tSO = theStudy.FindObjectIOR(theStudy.ConvertObjectToIOR(";
825 anUpdatedScript += seqRemoved.Value(ir);
826 // for object wrapped by class of smesh.py
827 anEntry = theObjectNames( seqRemoved.Value(ir) );
828 if ( anEntry2AccessorMethod.IsBound( anEntry ) )
829 anUpdatedScript += helper + "." + anEntry2AccessorMethod( anEntry );
830 anUpdatedScript += "))\n\tif SO is not None: aStudyBuilder.RemoveObjectWithChildren(SO)";
834 anUpdatedScript += "\n\t## set object names";
835 anUpdatedScript += helper + " \n\tisGUIMode = " + isPublished;
836 anUpdatedScript += "\n\tif isGUIMode and salome.sg.hasDesktop():";
837 // anUpdatedScript += "\n\t\tsmeshgui = salome.ImportComponentGUI(\"SMESH\")";
838 // anUpdatedScript += "\n\t\tsmeshgui.Init(theStudy._get_StudyId())";
839 // anUpdatedScript += "\n";
841 TCollection_AsciiString aGUIName;
842 Resource_DataMapOfAsciiStringAsciiString mapEntries;
843 for (Standard_Integer i = 1; i <= aLen; i += 2)
845 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
846 aName = geom->GetDumpName( anEntry.ToCString() );
847 if (aName.IsEmpty() && // Not a GEOM object
848 theNames.IsBound(anEntry) &&
849 !mapEntries.IsBound(anEntry) && // Not yet processed
850 !mapRemoved.IsBound(anEntry)) // Was not removed
852 aName = theObjectNames.Find(anEntry);
853 aGUIName = theNames.Find(anEntry);
854 mapEntries.Bind(anEntry, aName);
855 anUpdatedScript += helper + "\n\t\t" + aSmeshpy + ".SetName(" + aName;
856 if ( anEntry2AccessorMethod.IsBound( anEntry ) )
857 anUpdatedScript += helper + "." + anEntry2AccessorMethod( anEntry );
858 anUpdatedScript += helper + ", '" + aGUIName + "')";
861 anUpdatedScript += "\n\n\t\tsalome.sg.updateObjBrowser(0)";
863 // -----------------------------------------------------------------
864 // store visual properties of displayed objects
865 // -----------------------------------------------------------------
869 //Output the script that sets up the visual parameters.
870 char* script = theStudy->GetDefaultScript(ComponentDataType(), "\t");
871 if (script && strlen(script) > 0) {
872 anUpdatedScript += "\n\n\t### Store presentation parameters of displayed objects\n";
873 anUpdatedScript += script;
874 CORBA::string_free(script);
878 anUpdatedScript += "\n\n\tpass\n";
880 // -----------------------------------------------------------------
881 // put string literals describing patterns into separate functions
882 // -----------------------------------------------------------------
884 TCollection_AsciiString aLongString, aFunctionType;
886 set< string > functionNameSet;
887 while ( SMESH::TPythonDump::CutoutLongString( anUpdatedScript, where, aLongString, aFunctionType ))
889 // make a python string literal
890 aLongString.Prepend(":\n\treturn '''\n");
891 aLongString += "\n\t'''\n\tpass\n";
893 TCollection_AsciiString functionName;
895 // check if the function returning this literal is already defined
896 int posAlready = anUpdatedScript.Location( aLongString, where, anUpdatedScript.Length() );
897 if ( posAlready ) // already defined
899 // find the function name
900 int functBeg = posAlready;
901 char* script = (char*) anUpdatedScript.ToCString() + posAlready - 1; // look at ":" after "def fuction()"
902 while ( *script != ' ' ) {
906 functBeg++; // do not take ' '
907 posAlready--; // do not take ':'
908 functionName = anUpdatedScript.SubString( functBeg, posAlready );
910 else // not defined yet
912 // find a unique function name
913 fixPythonName( aFunctionType );
914 Standard_Integer nb = 0;
915 do functionName = aFunctionType + "_" + ( nb++ ) + "()";
916 while ( !functionNameSet.insert( functionName.ToCString() ).second );
918 anUpdatedScript += helper + "\n\ndef " + functionName + aLongString; // define function
920 anUpdatedScript.InsertBefore( where, functionName ); // call function
925 return anUpdatedScript;
928 //=============================================================================
932 //=============================================================================
933 TCollection_AsciiString SMESH_Gen_i::GetNewPythonLines (int theStudyID)
935 TCollection_AsciiString aScript;
937 // Dump trace of API methods calls
938 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
939 Handle(TColStd_HSequenceOfAsciiString) aPythonScript = myPythonScripts[theStudyID];
940 Standard_Integer istr, aLen = aPythonScript->Length();
941 for (istr = 1; istr <= aLen; istr++) {
943 aScript += aPythonScript->Value(istr);
951 //=============================================================================
955 //=============================================================================
956 void SMESH_Gen_i::CleanPythonTrace (int theStudyID)
958 TCollection_AsciiString aScript;
960 // Clean trace of API methods calls
961 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
962 myPythonScripts[theStudyID]->Clear();