1 // File : SMESH_Gen_i_DumpPython.cxx
2 // Created : Thu Mar 24 17:17:59 2005
3 // Author : Julia DOROVSKIKH
7 #include "SMESH_PythonDump.hxx"
8 #include "SMESH_Gen_i.hxx"
9 #include "SMESH_Filter_i.hxx"
10 #include "SALOMEDSImpl_Study.hxx"
12 #include <TColStd_HSequenceOfInteger.hxx>
13 #include <TCollection_AsciiString.hxx>
17 static int MYDEBUG = 0;
19 static int MYDEBUG = 0;
22 static TCollection_AsciiString NotPublishedObjectName()
24 return "__NOT__Published__Object__";
30 size_t TPythonDump::myCounter = 0;
41 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
42 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
43 if(!aStudy->_is_nil()){
44 std::string aString = myStream.str();
45 TCollection_AsciiString aCollection(Standard_CString(aString.c_str()));
46 aSMESHGen->AddToPythonScript(aStudy->StudyId(),aCollection);
47 if(MYDEBUG) MESSAGE(aString);
54 operator<<(long int theArg){
61 operator<<(int theArg){
68 operator<<(double theArg){
75 operator<<(float theArg){
82 operator<<(const void* theArg){
89 operator<<(const char* theArg){
96 operator<<(const SMESH::ElementType& theArg)
122 operator<<(const SMESH::long_array& theArg)
125 CORBA::Long i = 1, iEnd = theArg.length();
126 for(; i <= iEnd; i++) {
127 myStream<<theArg[i-1];
138 operator<<(CORBA::Object_ptr theArg)
140 TCollection_AsciiString aString("None");
141 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
142 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
143 SALOMEDS::SObject_var aSObject = SMESH_Gen_i::ObjectToSObject(aStudy,theArg);
144 if(!aSObject->_is_nil()){
145 aString = aSObject->GetID();
146 }else if(!CORBA::is_nil(theArg)){
147 aString = "smeshObj_";
148 if ( aSMESHGen->CanPublishInStudy( theArg )) // not published SMESH object
149 aString += (int) theArg;
151 aString = NotPublishedObjectName();
153 myStream<<aString.ToCString();
159 operator<<(SMESH::FilterLibrary_i* theArg)
161 myStream<<"aFilterLibrary"<<theArg;
167 operator<<(SMESH::FilterManager_i* theArg)
169 myStream<<"aFilterManager";
175 operator<<(SMESH::Filter_i* theArg)
177 myStream<<"aFilter"<<theArg;
183 operator<<(SMESH::Functor_i* theArg)
185 FunctorType aFunctorType = theArg->GetFunctorType();
186 switch(aFunctorType){
188 myStream<<"anAspectRatio";
190 case FT_AspectRatio3D:
191 myStream<<"anAspectRatio3D";
194 myStream<<"aWarping";
196 case FT_MinimumAngle:
197 myStream<<"aMinimumAngle";
209 myStream<<"aFreeBorders";
212 myStream<<"aFreeEdges";
214 case FT_MultiConnection:
215 myStream<<"aMultiConnection";
217 case FT_MultiConnection2D:
218 myStream<<"aMultiConnection2D";
226 case FT_BelongToGeom:
227 myStream<<"aBelongToGeom";
229 case FT_BelongToPlane:
230 myStream<<"aBelongToPlane";
232 case FT_BelongToCylinder:
233 myStream<<"aBelongToCylinder";
236 myStream<<"aLyingOnGeom";
239 myStream<<"aRangeOfIds";
241 case FT_BadOrientedVolume:
242 myStream<<"aBadOrientedVolume";
245 myStream<<"aLessThan";
248 myStream<<"aMoreThan";
251 myStream<<"anEqualTo";
254 myStream<<"aLogicalNOT";
257 myStream<<"aLogicalAND";
260 myStream<<"aLogicalOR";
263 myStream<<"anUndefined";
271 //=======================================================================
272 //function : DumpPython
274 //=======================================================================
275 Engines::TMPFile* SMESH_Gen_i::DumpPython (CORBA::Object_ptr theStudy,
276 CORBA::Boolean isPublished,
277 CORBA::Boolean& isValidScript)
279 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
280 if (CORBA::is_nil(aStudy))
281 return new Engines::TMPFile(0);
283 SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
284 if (CORBA::is_nil(aSO))
285 return new Engines::TMPFile(0);
287 // Map study entries to object names
288 Resource_DataMapOfAsciiStringAsciiString aMap;
289 Resource_DataMapOfAsciiStringAsciiString aMapNames;
290 TCollection_AsciiString s ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
292 SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
293 for (Itr->InitEx(true); Itr->More(); Itr->Next()) {
294 SALOMEDS::SObject_var aValue = Itr->Value();
295 CORBA::String_var anID = aValue->GetID();
296 CORBA::String_var aName = aValue->GetName();
297 TCollection_AsciiString aGUIName ( (char*) aName.in() );
298 TCollection_AsciiString anEnrty ( (char*) anID.in() );
299 if (aGUIName.Length() > 0) {
300 aMapNames.Bind( anEnrty, aGUIName );
301 aMap.Bind( anEnrty, aGUIName );
305 // Get trace of restored study
306 //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
307 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
308 SALOMEDS::GenericAttribute_var anAttr =
309 aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
311 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
312 TCollection_AsciiString aSavedTrace (oldValue);
314 // Add trace of API methods calls and replace study entries by names
315 TCollection_AsciiString aScript =
316 SALOMEDSImpl_Study::GetDumpStudyComment("SMESH") + "\n\n" +
317 DumpPython_impl(aStudy->StudyId(), aMap, aMapNames, isPublished, isValidScript, aSavedTrace);
319 int aLen = aScript.Length();
320 unsigned char* aBuffer = new unsigned char[aLen+1];
321 strcpy((char*)aBuffer, aScript.ToCString());
323 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
324 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
326 bool hasNotPublishedObjects = aScript.Location( NotPublishedObjectName(), 1, aLen);
327 isValidScript = isValidScript && !hasNotPublishedObjects;
329 return aStreamFile._retn();
332 //=============================================================================
336 //=============================================================================
337 void SMESH_Gen_i::AddToPythonScript (int theStudyID, const TCollection_AsciiString& theString)
339 if (myPythonScripts.find(theStudyID) == myPythonScripts.end()) {
340 myPythonScripts[theStudyID] = new TColStd_HSequenceOfAsciiString;
342 myPythonScripts[theStudyID]->Append(theString);
345 //=============================================================================
347 * RemoveLastFromPythonScript
349 //=============================================================================
350 void SMESH_Gen_i::RemoveLastFromPythonScript (int theStudyID)
352 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
353 int aLen = myPythonScripts[theStudyID]->Length();
354 myPythonScripts[theStudyID]->Remove(aLen);
358 //=======================================================================
359 //function : AddToCurrentPyScript
361 //=======================================================================
363 void SMESH_Gen_i::AddToCurrentPyScript (const TCollection_AsciiString& theString)
365 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
366 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
367 if (aStudy->_is_nil()) return;
368 aSMESHGen->AddToPythonScript(aStudy->StudyId(), theString);
372 //=======================================================================
373 //function : AddObject
374 //purpose : add object to script string
375 //=======================================================================
377 TCollection_AsciiString& SMESH_Gen_i::AddObject(TCollection_AsciiString& theStr,
378 CORBA::Object_ptr theObject)
380 TCollection_AsciiString aString("None");
381 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
382 SALOMEDS::SObject_var aSObject =
383 aSMESHGen->ObjectToSObject(aSMESHGen->GetCurrentStudy(), theObject);
384 if ( !aSObject->_is_nil() ) {
385 aString = aSObject->GetID();
386 } else if ( !CORBA::is_nil( theObject )) {
387 aString = "smeshObj_";
388 if ( aSMESHGen->CanPublishInStudy( theObject )) // not published SMESH object
389 aString += (int) theObject;
391 aString = NotPublishedObjectName();
397 //=======================================================================
398 //function : SavePython
400 //=======================================================================
401 void SMESH_Gen_i::SavePython (SALOMEDS::Study_ptr theStudy)
403 // Dump trace of API methods calls
404 TCollection_AsciiString aScript = GetNewPythonLines(theStudy->StudyId());
406 // Check contents of PythonObject attribute
407 SALOMEDS::SObject_var aSO = theStudy->FindComponent(ComponentDataType());
408 //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
409 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
410 SALOMEDS::GenericAttribute_var anAttr =
411 aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
413 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
414 TCollection_AsciiString oldScript (oldValue);
416 if (oldScript.Length() > 0) {
418 oldScript += aScript;
423 // Store in PythonObject attribute
424 SALOMEDS::AttributePythonObject::_narrow(anAttr)->SetObject(oldScript.ToCString(), 1);
426 // Clean trace of API methods calls
427 CleanPythonTrace(theStudy->StudyId());
434 //=============================================================================
436 * FindEntries: Returns a sequence of start/end positions of entries in the string
438 //=============================================================================
439 Handle(TColStd_HSequenceOfInteger) FindEntries (TCollection_AsciiString& theString)
441 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
442 Standard_Integer aLen = theString.Length();
443 Standard_Boolean isFound = Standard_False;
445 char* arr = theString.ToCString();
446 Standard_Integer i = 0, j;
451 if(c >= 48 && c <= 57) { //Is digit?
453 isFound = Standard_False;
454 while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
456 if(c == 58) isFound = Standard_True;
460 int prev = (i < 1) ? 0 : (int)arr[i - 1];
461 // last char should be a diggit,
462 // previous char should not be '"'.
463 if (arr[j-2] != 58 && prev != 34) {
464 aSeq->Append(i+1); // +1 because AsciiString starts from 1
476 //=============================================================================
480 //=============================================================================
481 TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
483 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
484 Resource_DataMapOfAsciiStringAsciiString& theNames,
487 const TCollection_AsciiString& theSavedTrace)
489 TCollection_AsciiString aScript;
490 aScript = "def RebuildData(theStudy):";
491 aScript += "\n\tsmesh = salome.lcc.FindOrLoadComponent(\"FactoryServer\", \"SMESH\")";
492 aScript += "\n\taFilterManager = smesh.CreateFilterManager()";
494 aScript += "\n\tsmesh.SetCurrentStudy(theStudy)";
496 aScript += "\n\tsmesh.SetCurrentStudy(None)";
498 // Dump trace of restored study
499 if (theSavedTrace.Length() > 0) {
501 aScript += theSavedTrace;
504 // Dump trace of API methods calls
505 TCollection_AsciiString aNewLines = GetNewPythonLines(theStudyID);
506 if (aNewLines.Length() > 0) {
508 aScript += aNewLines;
511 // Find entries to be replaced by names
512 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
513 Standard_Integer aLen = aSeq->Length();
518 // Replace entries by the names
519 GEOM::GEOM_Gen_ptr geom = GetGeomEngine();
520 TColStd_SequenceOfAsciiString seqRemoved;
521 Resource_DataMapOfAsciiStringAsciiString mapRemoved;
522 Standard_Integer objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
523 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("smeshObj_"),
524 allowedChars ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
526 // Collect names of GEOM objects to exclude same names for SMESH objects
527 GEOM::string_array_var aGeomNames = geom->GetAllDumpNames();
528 int ign = 0, nbgn = aGeomNames->length();
529 for (; ign < nbgn; ign++) {
530 aName = aGeomNames[ign];
531 theObjectNames.Bind(aName, "1");
534 bool importGeom = false;
535 for (Standard_Integer i = 1; i <= aLen; i += 2) {
536 anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i) - 1);
537 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
539 aName = geom->GetDumpName( anEntry.ToCString() );
540 if (aName.IsEmpty()) {
542 if (theObjectNames.IsBound(anEntry)) {
543 // The Object is in Study
544 aName = theObjectNames.Find(anEntry);
545 // check validity of aName
546 bool isValidName = true;
547 if ( aName.IsIntegerValue() ) { // aName must not start with a digit
548 aName.Insert( 1, 'a' );
551 int p, p2=1; // replace not allowed chars
552 while ((p = aName.FirstLocationNotInSet(allowedChars, p2, aName.Length()))) {
553 aName.SetValue(p, '_');
557 if (theObjectNames.IsBound(aName) && anEntry != theObjectNames(aName)) {
558 // diff objects have same name - make a new name by appending a digit
559 TCollection_AsciiString aName2;
560 Standard_Integer i = 0;
562 aName2 = aName + "_" + ++i;
563 } while (theObjectNames.IsBound(aName2) && anEntry != theObjectNames(aName2));
568 theObjectNames(anEntry) = aName;
573 aName = aBaseName + TCollection_AsciiString(++objectCounter);
574 } while (theObjectNames.IsBound(aName));
575 seqRemoved.Append(aName);
576 mapRemoved.Bind(anEntry, "1");
577 theObjectNames.Bind(anEntry, aName);
579 theObjectNames.Bind(aName, anEntry); // to detect same name of diff objects
585 anUpdatedScript += aName;
586 aStart = aSeq->Value(i + 1) + 1;
589 // set initial part of aSript
590 TCollection_AsciiString initPart = "import salome, SMESH, StdMeshers\n\n";
593 initPart += ("import string, os, sys, re\n"
594 "sys.path.insert( 0, os.path.dirname(__file__) )\n"
595 "exec(\"from \"+re.sub(\"SMESH$\",\"GEOM\",__name__)+\" import *\")\n\n");
597 anUpdatedScript.Insert ( 1, initPart );
599 // add final part of aScript
600 if (aSeq->Value(aLen) < aScriptLength)
601 anUpdatedScript += aScript.SubString(aSeq->Value(aLen) + 1, aScriptLength);
603 // Remove removed objects
604 anUpdatedScript += "\n\taStudyBuilder = theStudy.NewBuilder()";
605 for (int ir = 1; ir <= seqRemoved.Length(); ir++) {
606 anUpdatedScript += "\n\tSO = theStudy.FindObjectIOR(theStudy.ConvertObjectToIOR(";
607 anUpdatedScript += seqRemoved.Value(ir);
608 anUpdatedScript += "))\n\tif SO is not None: aStudyBuilder.RemoveObjectWithChildren(SO)";
612 anUpdatedScript += "\n\n\tisGUIMode = ";
613 anUpdatedScript += isPublished;
614 anUpdatedScript += "\n\tif isGUIMode:";
615 anUpdatedScript += "\n\t\tsmeshgui = salome.ImportComponentGUI(\"SMESH\")";
616 anUpdatedScript += "\n\t\tsmeshgui.Init(theStudy._get_StudyId())";
617 anUpdatedScript += "\n";
619 TCollection_AsciiString aGUIName;
620 Resource_DataMapOfAsciiStringAsciiString mapEntries;
621 for (Standard_Integer i = 1; i <= aLen; i += 2) {
622 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
623 aName = geom->GetDumpName( anEntry.ToCString() );
624 if (aName.IsEmpty() && // Not a GEOM object
625 theNames.IsBound(anEntry) &&
626 !mapEntries.IsBound(anEntry) && // Not yet processed
627 !mapRemoved.IsBound(anEntry)) { // Was not removed
628 aName = theObjectNames.Find(anEntry);
629 aGUIName = theNames.Find(anEntry);
630 mapEntries.Bind(anEntry, aName);
631 anUpdatedScript += "\n\t\tsmeshgui.SetName(salome.ObjectToID(";
632 anUpdatedScript += aName + "), \"" + aGUIName + "\")";
635 anUpdatedScript += "\n\n\t\tsalome.sg.updateObjBrowser(0)";
637 anUpdatedScript += "\n\n\tpass\n";
641 return anUpdatedScript;
644 //=============================================================================
648 //=============================================================================
649 TCollection_AsciiString SMESH_Gen_i::GetNewPythonLines (int theStudyID)
651 TCollection_AsciiString aScript;
653 // Dump trace of API methods calls
654 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
655 Handle(TColStd_HSequenceOfAsciiString) aPythonScript = myPythonScripts[theStudyID];
656 Standard_Integer istr, aLen = aPythonScript->Length();
657 for (istr = 1; istr <= aLen; istr++) {
659 aScript += aPythonScript->Value(istr);
667 //=============================================================================
671 //=============================================================================
672 void SMESH_Gen_i::CleanPythonTrace (int theStudyID)
674 TCollection_AsciiString aScript;
676 // Clean trace of API methods calls
677 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
678 myPythonScripts[theStudyID]->Clear();