1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESH_Gen_i_DumpPython.cxx
23 // Created : Thu Mar 24 17:17:59 2005
24 // Author : Julia DOROVSKIKH
28 #include "SMESH_PythonDump.hxx"
29 #include "SMESH_Gen_i.hxx"
30 #include "SMESH_Filter_i.hxx"
31 #include "SMESH_MeshEditor_i.hxx"
32 #include "SMESH_2smeshpy.hxx"
34 #include <TColStd_HSequenceOfInteger.hxx>
35 #include <TCollection_AsciiString.hxx>
36 #include <SMESH_Comment.hxx>
40 static int MYDEBUG = 0;
42 static int MYDEBUG = 0;
45 static TCollection_AsciiString NotPublishedObjectName()
47 return "__NOT__Published__Object__";
53 size_t TPythonDump::myCounter = 0;
64 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
65 std::string aString = myStream.str();
66 TCollection_AsciiString aCollection(Standard_CString(aString.c_str()));
67 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
68 if(!aStudy->_is_nil() && !aCollection.IsEmpty()){
69 aSMESHGen->AddToPythonScript(aStudy->StudyId(),aCollection);
70 if(MYDEBUG) MESSAGE(aString);
77 operator<<(long int theArg){
84 operator<<(int theArg){
91 operator<<(double theArg){
98 operator<<(float theArg){
105 operator<<(const void* theArg){
112 operator<<(const char* theArg){
120 operator<<(const SMESH::ElementType& theArg)
124 case ALL: myStream<<"ALL";break;
125 case NODE: myStream<<"NODE";break;
126 case EDGE: myStream<<"EDGE";break;
127 case FACE: myStream<<"FACE";break;
128 case VOLUME:myStream<<"VOLUME";break;
133 template<class TArray>
134 void DumpArray(const TArray& theArray, std::ostringstream & theStream)
137 for (int i = 1; i <= theArray.length(); i++) {
138 theStream << theArray[i-1];
139 if ( i < theArray.length() )
146 TPythonDump::operator<<(const SMESH::long_array& theArg)
148 DumpArray( theArg, myStream );
153 TPythonDump::operator<<(const SMESH::double_array& theArg)
155 DumpArray( theArg, myStream );
161 operator<<(SALOMEDS::SObject_ptr aSObject)
163 if ( !aSObject->_is_nil() )
164 myStream << aSObject->GetID();
166 myStream << NotPublishedObjectName();
172 operator<<(CORBA::Object_ptr theArg)
174 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
175 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
176 SALOMEDS::SObject_var aSObject = SMESH_Gen_i::ObjectToSObject(aStudy,theArg);
177 if(!aSObject->_is_nil()) {
178 myStream << aSObject->GetID();
179 } else if ( !CORBA::is_nil(theArg)) {
180 if ( aSMESHGen->CanPublishInStudy( theArg )) // not published SMESH object
181 myStream << "smeshObj_" << size_t(theArg);
183 myStream << NotPublishedObjectName();
192 operator<<(SMESH::FilterLibrary_i* theArg)
194 myStream<<"aFilterLibrary"<<theArg;
200 operator<<(SMESH::FilterManager_i* theArg)
202 myStream<<"aFilterManager";
208 operator<<(SMESH::Filter_i* theArg)
210 myStream<<"aFilter"<<theArg;
216 operator<<(SMESH::Functor_i* theArg)
219 FunctorType aFunctorType = theArg->GetFunctorType();
220 switch(aFunctorType){
221 case FT_AspectRatio: myStream<< "anAspectRatio"; break;
222 case FT_AspectRatio3D: myStream<< "anAspectRatio3D"; break;
223 case FT_Warping: myStream<< "aWarping"; break;
224 case FT_MinimumAngle: myStream<< "aMinimumAngle"; break;
225 case FT_Taper: myStream<< "aTaper"; break;
226 case FT_Skew: myStream<< "aSkew"; break;
227 case FT_Area: myStream<< "aArea"; break;
228 case FT_FreeBorders: myStream<< "aFreeBorders"; break;
229 case FT_FreeEdges: myStream<< "aFreeEdges"; break;
230 case FT_FreeNodes: myStream<< "aFreeNodes"; break;
231 case FT_MultiConnection: myStream<< "aMultiConnection"; break;
232 case FT_MultiConnection2D:myStream<< "aMultiConnection2D";break;
233 case FT_Length: myStream<< "aLength"; break;
234 case FT_Length2D: myStream<< "aLength"; break;
235 case FT_BelongToGeom: myStream<< "aBelongToGeom"; break;
236 case FT_BelongToPlane: myStream<< "aBelongToPlane"; break;
237 case FT_BelongToCylinder: myStream<< "aBelongToCylinder"; break;
238 case FT_BelongToGenSurface:myStream<<"aBelongToGenSurface";break;
239 case FT_LyingOnGeom: myStream<< "aLyingOnGeom"; break;
240 case FT_RangeOfIds: myStream<< "aRangeOfIds"; break;
241 case FT_BadOrientedVolume:myStream<< "aBadOrientedVolume";break;
242 case FT_LessThan: myStream<< "aLessThan"; break;
243 case FT_MoreThan: myStream<< "aMoreThan"; break;
244 case FT_EqualTo: myStream<< "anEqualTo"; break;
245 case FT_LogicalNOT: myStream<< "aLogicalNOT"; break;
246 case FT_LogicalAND: myStream<< "aLogicalAND"; break;
247 case FT_LogicalOR: myStream<< "aLogicalOR"; break;
248 case FT_Undefined: myStream<< "anUndefined"; break;
255 TPythonDump& TPythonDump:: operator<<(SMESH_Gen_i* theArg)
257 myStream << SMESHGenName(); return *this;
260 TPythonDump& TPythonDump::operator<<(SMESH_MeshEditor_i* theArg)
262 myStream << MeshEditorName() << "_" << ( theArg ? theArg->GetMeshId() : -1 ); return *this;
265 TPythonDump& TPythonDump::operator<<(const TCollection_AsciiString & theStr)
267 myStream << theStr; return *this;
271 TPythonDump& TPythonDump::operator<<(SMESH::MED_VERSION theVersion)
273 switch (theVersion) {
274 case SMESH::MED_V2_1: myStream << "SMESH.MED_V2_1"; break;
275 case SMESH::MED_V2_2: myStream << "SMESH.MED_V2_2"; break;
276 default: myStream << theVersion;
281 TPythonDump& TPythonDump::operator<<(const SMESH::AxisStruct & theAxis)
283 myStream << "SMESH.AxisStruct( "
287 << theAxis.vx << ", "
288 << theAxis.vy << ", "
289 << theAxis.vz << " )";
293 TPythonDump& TPythonDump::operator<<(const SMESH::DirStruct & theDir)
295 const SMESH::PointStruct & P = theDir.PS;
296 myStream << "SMESH.DirStruct( SMESH.PointStruct ( "
303 TPythonDump& TPythonDump::operator<<(const SMESH::ListOfGroups * theList){
304 if(theList && theList->length() > 0 ) {
305 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
306 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
308 int aListLen = theList->length();
309 for(int i = 0 ; i < aListLen; i++){
310 SALOMEDS::SObject_var aSObject = SMESH_Gen_i::ObjectToSObject(aStudy,(*theList)[i]);
311 if(!aSObject->_is_nil()) {
312 myStream << aSObject->GetID();
313 i < (aListLen - 1) ? myStream<<", " : myStream<<"]";
321 TCollection_AsciiString myLongStringStart( "TPythonDump::LongStringStart" );
322 TCollection_AsciiString myLongStringEnd ( "TPythonDump::LongStringEnd" );
324 //================================================================================
326 * \brief Return marker of long string literal beginning
327 * \param type - a name of functionality producing the string literal
328 * \retval TCollection_AsciiString - the marker string to be written into
329 * a raw python script
331 //================================================================================
333 TCollection_AsciiString TPythonDump::LongStringStart(const char* type)
337 (Standard_Integer) strlen(type) +
342 //================================================================================
344 * \brief Return marker of long string literal end
345 * \retval TCollection_AsciiString - the marker string to be written into
346 * a raw python script
348 //================================================================================
350 TCollection_AsciiString TPythonDump::LongStringEnd()
352 return myLongStringEnd;
355 //================================================================================
357 * \brief Cut out a long string literal from a string
358 * \param theText - text possibly containing string literals
359 * \param theFrom - position in the text to search from
360 * \param theLongString - the retrieved literal
361 * \param theStringType - a name of functionality produced the literal
362 * \retval bool - true if a string literal found
364 * The literal is removed from theText; theFrom points position right after
365 * the removed literal
367 //================================================================================
369 bool TPythonDump::CutoutLongString( TCollection_AsciiString & theText,
371 TCollection_AsciiString & theLongString,
372 TCollection_AsciiString & theStringType)
374 if ( theFrom < 1 || theFrom > theText.Length() )
377 // ...script \ beg marker \ \ type \ literal \ end marker \ script...
378 // "theText myLongStringStart7 Pattern!!! SALOME Mesh Pattern file myLongStringEndtextEnd"
379 // 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
382 theFrom = theText.Location( myLongStringStart, theFrom, theText.Length() ); // = 09
386 // find where literal begins
387 int literalBeg = theFrom + myLongStringStart.Length(); // = 26
388 char* typeLenStr = (char*) theText.ToCString() + literalBeg - 1; // = "7 Pattern!!! SALO...."
389 int typeLen = atoi ( typeLenStr ); // = 7
390 while ( *typeLenStr != ' ' ) { // look for ' ' after typeLen
391 literalBeg++; // 26 -> 27
394 literalBeg += typeLen + 1; // = 35
395 if ( literalBeg > theText.Length() )
398 // where literal ends (i.e. end marker begins)
399 int literalEnd = theText.Location( myLongStringEnd, literalBeg, theText.Length() ); // = 64
401 literalEnd = theText.Length();
404 theLongString = theText.SubString( literalBeg, literalEnd - 1); // "!!! SALOME Mesh Pattern file "
406 theStringType = theText.SubString( literalBeg - typeLen, literalBeg - 1 ); // "Pattern"
408 literalEnd += myLongStringEnd.Length(); // = 79
409 TCollection_AsciiString textEnd = theText.SubString( literalEnd, theText.Length() ); // "textE..."
410 theText = theText.SubString( 1, theFrom - 1 ) + textEnd;
416 //=======================================================================
417 //function : DumpPython
419 //=======================================================================
420 Engines::TMPFile* SMESH_Gen_i::DumpPython (CORBA::Object_ptr theStudy,
421 CORBA::Boolean isPublished,
422 CORBA::Boolean& isValidScript)
424 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
425 if (CORBA::is_nil(aStudy))
426 return new Engines::TMPFile(0);
428 SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
429 if (CORBA::is_nil(aSO))
430 return new Engines::TMPFile(0);
432 // Map study entries to object names
433 Resource_DataMapOfAsciiStringAsciiString aMap;
434 Resource_DataMapOfAsciiStringAsciiString aMapNames;
435 //TCollection_AsciiString s ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
437 SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
438 for (Itr->InitEx(true); Itr->More(); Itr->Next()) {
439 SALOMEDS::SObject_var aValue = Itr->Value();
440 CORBA::String_var anID = aValue->GetID();
441 CORBA::String_var aName = aValue->GetName();
442 TCollection_AsciiString aGUIName ( (char*) aName.in() );
443 TCollection_AsciiString anEnrty ( (char*) anID.in() );
444 if (aGUIName.Length() > 0) {
445 aMapNames.Bind( anEnrty, aGUIName );
446 aMap.Bind( anEnrty, aGUIName );
450 // Get trace of restored study
451 //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
452 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
453 SALOMEDS::GenericAttribute_var anAttr =
454 aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
456 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
457 TCollection_AsciiString aSavedTrace (oldValue);
459 // Add trace of API methods calls and replace study entries by names
460 TCollection_AsciiString aScript =
461 "### This file is generated by SALOME automatically by dump python functionality of SMESH component\n\n";
462 aScript += DumpPython_impl(aStudy, aMap, aMapNames,
463 isPublished, isValidScript, aSavedTrace);
465 int aLen = aScript.Length();
466 unsigned char* aBuffer = new unsigned char[aLen+1];
467 strcpy((char*)aBuffer, aScript.ToCString());
469 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
470 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
472 bool hasNotPublishedObjects = aScript.Location( NotPublishedObjectName(), 1, aLen);
473 isValidScript = isValidScript && !hasNotPublishedObjects;
475 return aStreamFile._retn();
478 //=============================================================================
482 //=============================================================================
483 void SMESH_Gen_i::AddToPythonScript (int theStudyID, const TCollection_AsciiString& theString)
485 if (myPythonScripts.find(theStudyID) == myPythonScripts.end()) {
486 myPythonScripts[theStudyID] = new TColStd_HSequenceOfAsciiString;
488 myPythonScripts[theStudyID]->Append(theString);
491 //=============================================================================
493 * RemoveLastFromPythonScript
495 //=============================================================================
496 void SMESH_Gen_i::RemoveLastFromPythonScript (int theStudyID)
498 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
499 int aLen = myPythonScripts[theStudyID]->Length();
500 myPythonScripts[theStudyID]->Remove(aLen);
504 //=======================================================================
505 //function : SavePython
507 //=======================================================================
508 void SMESH_Gen_i::SavePython (SALOMEDS::Study_ptr theStudy)
510 // Dump trace of API methods calls
511 TCollection_AsciiString aScript = GetNewPythonLines(theStudy->StudyId());
513 // Check contents of PythonObject attribute
514 SALOMEDS::SObject_var aSO = theStudy->FindComponent(ComponentDataType());
515 //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
516 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
517 SALOMEDS::GenericAttribute_var anAttr =
518 aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
520 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
521 TCollection_AsciiString oldScript (oldValue);
523 if (oldScript.Length() > 0) {
525 oldScript += aScript;
530 // Store in PythonObject attribute
531 SALOMEDS::AttributePythonObject::_narrow(anAttr)->SetObject(oldScript.ToCString(), 1);
533 // Clean trace of API methods calls
534 CleanPythonTrace(theStudy->StudyId());
541 //=============================================================================
543 * FindEntries: Returns a sequence of start/end positions of entries in the string
545 //=============================================================================
546 Handle(TColStd_HSequenceOfInteger) FindEntries (TCollection_AsciiString& theString)
548 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
549 Standard_Integer aLen = theString.Length();
550 Standard_Boolean isFound = Standard_False;
552 char* arr = (char*) theString.ToCString();
553 Standard_Integer i = 0, j;
558 if ( isdigit( c )) { //Is digit?
560 isFound = Standard_False;
561 while((j < aLen) && ( isdigit(c) || c == ':' )) { //Check if it is an entry
563 if(c == ':') isFound = Standard_True;
567 int prev = (i < 1) ? 0 : (int)arr[i - 1];
568 // to distinguish from a sketcher command:
569 // last char should be a digit, not ":",
570 // previous char should not be '"'.
571 if (arr[j-2] != ':' && prev != '"') {
572 aSeq->Append(i+1); // +1 because AsciiString starts from 1
586 //================================================================================
588 * \brief Make a string be a valid python name
589 * \param aName - a string to fix
590 * \retval bool - true if aName was not modified
592 //================================================================================
594 bool fixPythonName(TCollection_AsciiString & aName )
596 const TCollection_AsciiString allowedChars =
597 "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_";
598 bool isValidName = true;
599 int p=1; // replace not allowed chars with underscore
600 while (p <= aName.Length() &&
601 (p = aName.FirstLocationNotInSet(allowedChars, p, aName.Length())))
603 if ( p == 1 || p == aName.Length() || aName.Value(p-1) == '_')
604 aName.Remove( p, 1 ); // remove double _ and from the start and the end
606 aName.SetValue(p, '_');
609 if ( aName.IsIntegerValue() ) { // aName must not start with a digit
610 aName.Insert( 1, 'a' );
617 //=============================================================================
621 //=============================================================================
622 TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
623 (SALOMEDS::Study_ptr theStudy,
624 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
625 Resource_DataMapOfAsciiStringAsciiString& theNames,
628 const TCollection_AsciiString& theSavedTrace)
630 int aStudyID = theStudy->StudyId();
632 TCollection_AsciiString helper; // to comfortably concatenate C strings
633 TCollection_AsciiString aSmeshpy( SMESH_2smeshpy::SmeshpyName() );
634 TCollection_AsciiString aSMESHGen( SMESH_2smeshpy::GenName() );
635 TCollection_AsciiString anOldGen( SMESH::TPythonDump::SMESHGenName() );
637 TCollection_AsciiString aScript;
638 aScript = "def RebuildData(theStudy):\n\t";
639 aScript += helper + "aFilterManager = " + aSMESHGen + ".CreateFilterManager()\n\t";
641 aScript += aSMESHGen + ".SetCurrentStudy(theStudy)";
643 aScript += aSMESHGen + ".SetCurrentStudy(None)";
645 // import python files corresponding to plugins
646 set<string> moduleNameSet;
647 map<string, GenericHypothesisCreator_i*>::iterator hyp_creator = myHypCreatorMap.begin();
648 for ( ; hyp_creator != myHypCreatorMap.end(); ++hyp_creator ) {
649 string moduleName = hyp_creator->second->GetModuleName();
650 bool newModule = moduleNameSet.insert( moduleName ).second;
652 aScript += helper + "\n\t" + "import " + (char*) moduleName.c_str();
655 // Dump trace of restored study
656 if (theSavedTrace.Length() > 0) {
657 // For the convertion of IDL API calls -> smesh.py API, "smesh" standing for SMESH_Gen
658 // was replaces with "smeshgen" (==TPythonDump::SMESHGenName()).
659 // Change "smesh" -> "smeshgen" in the trace saved before passage to smesh.py API
661 theSavedTrace.Location( anOldGen + ".", 1, theSavedTrace.Length() );
662 if ( !isNewVersion ) {
663 TCollection_AsciiString aSavedTrace( theSavedTrace );
664 TCollection_AsciiString aSmeshCall ( "smesh." ), gen( "gen" );
665 int beg, end = aSavedTrace.Length(), from = 1;
666 while ( from < end && ( beg = aSavedTrace.Location( aSmeshCall, from, end ))) {
667 char charBefore = ( beg == 1 ) ? ' ' : aSavedTrace.Value( beg - 1 );
668 if ( isspace( charBefore ) || charBefore == '=' ) { // "smesh." is not a part of a long word
669 aSavedTrace.Insert( beg + aSmeshCall.Length() - 1, gen );// "smesh" -> "smeshgen"
672 from = beg + aSmeshCall.Length();
674 aScript += helper + "\n" + aSavedTrace;
677 // append a saved trace to the script
678 aScript += helper + "\n" + theSavedTrace;
681 // Dump trace of API methods calls
682 TCollection_AsciiString aNewLines = GetNewPythonLines(aStudyID);
683 if (aNewLines.Length() > 0) {
684 aScript += helper + "\n" + aNewLines;
687 // Convert IDL API calls into smesh.py API.
688 // Some objects are wrapped with python classes and
689 // Resource_DataMapOfAsciiStringAsciiString holds methods returning wrapped objects
690 Resource_DataMapOfAsciiStringAsciiString anEntry2AccessorMethod;
691 aScript = SMESH_2smeshpy::ConvertScript( aScript, anEntry2AccessorMethod, theObjectNames );
693 // Find entries to be replaced by names
694 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
695 Standard_Integer aLen = aSeq->Length();
700 // Replace entries by the names
701 GEOM::GEOM_Gen_ptr geom = GetGeomEngine();
702 TColStd_SequenceOfAsciiString seqRemoved;
703 Resource_DataMapOfAsciiStringAsciiString mapRemoved;
704 Standard_Integer objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
705 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("smeshObj_");
707 // Collect names of GEOM objects to exclude same names for SMESH objects
708 GEOM::string_array_var aGeomNames = geom->GetAllDumpNames();
709 int ign = 0, nbgn = aGeomNames->length();
710 for (; ign < nbgn; ign++) {
711 aName = aGeomNames[ign];
712 theObjectNames.Bind(aName, "1");
715 bool importGeom = false;
716 for (Standard_Integer i = 1; i <= aLen; i += 2) {
717 anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i) - 1);
718 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
720 aName = geom->GetDumpName( anEntry.ToCString() );
721 if (aName.IsEmpty()) {
723 if (theObjectNames.IsBound(anEntry)) {
724 // The Object is in Study
725 aName = theObjectNames.Find(anEntry);
726 // check validity of aName
727 bool isValidName = fixPythonName( aName );
728 if (theObjectNames.IsBound(aName) && anEntry != theObjectNames(aName)) {
729 // diff objects have same name - make a new name by appending a digit
730 TCollection_AsciiString aName2;
731 Standard_Integer i = 0;
733 aName2 = aName + "_" + ++i;
734 } while (theObjectNames.IsBound(aName2) && anEntry != theObjectNames(aName2));
739 theObjectNames(anEntry) = aName;
744 aName = aBaseName + (++objectCounter);
745 } while (theObjectNames.IsBound(aName));
746 seqRemoved.Append(aName);
747 mapRemoved.Bind(anEntry, "1");
748 theObjectNames.Bind(anEntry, aName);
750 theObjectNames.Bind(aName, anEntry); // to detect same name of diff objects
756 anUpdatedScript += aName;
757 aStart = aSeq->Value(i + 1) + 1;
760 // set initial part of aSript
761 TCollection_AsciiString initPart = "import salome, SMESH\n";
762 initPart += helper + "import " + aSmeshpy + "\n\n";
765 initPart += ("## import GEOM dump file ## \n"
766 "import string, os, sys, re\n"
767 "sys.path.insert( 0, os.path.dirname(__file__) )\n"
768 "exec(\"from \"+re.sub(\"SMESH$\",\"GEOM\",__name__)+\" import *\")\n\n");
770 anUpdatedScript.Insert ( 1, initPart );
772 // add final part of aScript
773 if (aSeq->Value(aLen) < aScriptLength)
774 anUpdatedScript += aScript.SubString(aSeq->Value(aLen) + 1, aScriptLength);
777 SALOMEDS::SObject_var aComp = theStudy->FindComponent(ComponentDataType());
778 if( !CORBA::is_nil(aComp) )
780 SALOMEDS::ChildIterator_var Itr = theStudy->NewChildIterator(aComp);
781 for( Itr->InitEx(true); Itr->More(); Itr->Next() )
783 SALOMEDS::SObject_var aSObj = Itr->Value();
784 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( SObjectToObject( aSObj ) );
786 if( !CORBA::is_nil(aMesh) && aMesh->GetAutoColor() )
788 CORBA::String_var anEntry = aSObj->GetID();
790 SMESH_Comment("\n\t") << theObjectNames(anEntry.inout()) << ".SetAutoColor(1)";
792 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow( SObjectToObject(aSObj));
793 if( !CORBA::is_nil(aGroup) )
795 SALOMEDS::Color aColor = aGroup->GetColor();
796 if ( aColor.R > 0 || aColor.G > 0 || aColor.B > 0 )
798 CORBA::String_var anEntry = aSObj->GetID();
799 anUpdatedScript += SMESH_Comment("\n\t")
800 << theObjectNames(anEntry.inout()) << ".SetColor(SALOMEDS.Color("
801 << aColor.R <<", "<< aColor.G <<", "<< aColor.B <<" ))";
807 // Remove removed objects
808 if ( seqRemoved.Length() > 0 ) {
809 anUpdatedScript += "\n\t## some objects were removed";
810 anUpdatedScript += "\n\taStudyBuilder = theStudy.NewBuilder()";
812 for (int ir = 1; ir <= seqRemoved.Length(); ir++) {
813 anUpdatedScript += "\n\tSO = theStudy.FindObjectIOR(theStudy.ConvertObjectToIOR(";
814 anUpdatedScript += seqRemoved.Value(ir);
815 // for object wrapped by class of smesh.py
816 anEntry = theObjectNames( seqRemoved.Value(ir) );
817 if ( anEntry2AccessorMethod.IsBound( anEntry ) )
818 anUpdatedScript += helper + "." + anEntry2AccessorMethod( anEntry );
819 anUpdatedScript += "))\n\tif SO is not None: aStudyBuilder.RemoveObjectWithChildren(SO)";
823 anUpdatedScript += "\n\t## set object names";
824 // anUpdatedScript += "\n\t\tsmeshgui = salome.ImportComponentGUI(\"SMESH\")";
825 // anUpdatedScript += "\n\t\tsmeshgui.Init(theStudy._get_StudyId())";
826 // anUpdatedScript += "\n";
828 TCollection_AsciiString aGUIName;
829 Resource_DataMapOfAsciiStringAsciiString mapEntries;
830 for (Standard_Integer i = 1; i <= aLen; i += 2)
832 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
833 aName = geom->GetDumpName( anEntry.ToCString() );
834 if (aName.IsEmpty() && // Not a GEOM object
835 theNames.IsBound(anEntry) &&
836 !mapEntries.IsBound(anEntry) && // Not yet processed
837 !mapRemoved.IsBound(anEntry)) // Was not removed
839 aName = theObjectNames.Find(anEntry);
840 aGUIName = theNames.Find(anEntry);
841 mapEntries.Bind(anEntry, aName);
842 anUpdatedScript += helper + "\n\t" + aSMESHGen + ".SetName(" + aName;
843 if ( anEntry2AccessorMethod.IsBound( anEntry ) )
844 anUpdatedScript += helper + "." + anEntry2AccessorMethod( anEntry );
845 anUpdatedScript += helper + ", '" + aGUIName + "')";
848 anUpdatedScript += "\n\tif salome.sg.hasDesktop():";
849 anUpdatedScript += "\n\t\tsalome.sg.updateObjBrowser(0)";
851 // -----------------------------------------------------------------
852 // store visual properties of displayed objects
853 // -----------------------------------------------------------------
857 //Output the script that sets up the visual parameters.
858 char* script = theStudy->GetDefaultScript(ComponentDataType(), "\t");
859 if (script && strlen(script) > 0) {
860 anUpdatedScript += "\n\n\t### Store presentation parameters of displayed objects\n";
861 anUpdatedScript += script;
862 CORBA::string_free(script);
865 anUpdatedScript += "\n\n\tpass\n";
867 // -----------------------------------------------------------------
868 // put string literals describing patterns into separate functions
869 // -----------------------------------------------------------------
871 TCollection_AsciiString aLongString, aFunctionType;
873 set< string > functionNameSet;
874 while ( SMESH::TPythonDump::CutoutLongString( anUpdatedScript, where, aLongString, aFunctionType ))
876 // make a python string literal
877 aLongString.Prepend(":\n\treturn '''\n");
878 aLongString += "\n\t'''\n\tpass\n";
880 TCollection_AsciiString functionName;
882 // check if the function returning this literal is already defined
883 int posAlready = anUpdatedScript.Location( aLongString, where, anUpdatedScript.Length() );
884 if ( posAlready ) // already defined
886 // find the function name
887 int functBeg = posAlready;
888 char* script = (char*) anUpdatedScript.ToCString() + posAlready - 1; // look at ":" after "def fuction()"
889 while ( *script != ' ' ) {
893 functBeg++; // do not take ' '
894 posAlready--; // do not take ':'
895 functionName = anUpdatedScript.SubString( functBeg, posAlready );
897 else // not defined yet
899 // find a unique function name
900 fixPythonName( aFunctionType );
901 Standard_Integer nb = 0;
902 do functionName = aFunctionType + "_" + ( nb++ ) + "()";
903 while ( !functionNameSet.insert( functionName.ToCString() ).second );
905 anUpdatedScript += helper + "\n\ndef " + functionName + aLongString; // define function
907 anUpdatedScript.InsertBefore( where, functionName ); // call function
912 return anUpdatedScript;
915 //=============================================================================
919 //=============================================================================
920 TCollection_AsciiString SMESH_Gen_i::GetNewPythonLines (int theStudyID)
922 TCollection_AsciiString aScript;
924 // Dump trace of API methods calls
925 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
926 Handle(TColStd_HSequenceOfAsciiString) aPythonScript = myPythonScripts[theStudyID];
927 Standard_Integer istr, aLen = aPythonScript->Length();
928 for (istr = 1; istr <= aLen; istr++) {
930 aScript += aPythonScript->Value(istr);
938 //=============================================================================
942 //=============================================================================
943 void SMESH_Gen_i::CleanPythonTrace (int theStudyID)
945 TCollection_AsciiString aScript;
947 // Clean trace of API methods calls
948 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
949 myPythonScripts[theStudyID]->Clear();