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"
11 #include <TColStd_HSequenceOfInteger.hxx>
12 #include <TCollection_AsciiString.hxx>
17 size_t TPythonDump::myCounter = 0;
28 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
29 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
30 if(!aStudy->_is_nil()){
31 std::string aString = myStream.str();
32 TCollection_AsciiString aCollection(Standard_CString(aString.c_str()));
33 aSMESHGen->AddToPythonScript(aStudy->StudyId(),aCollection);
40 operator<<(const SMESH::ElementType& theArg)
66 operator<<(const SMESH::long_array& theArg)
69 CORBA::Long i = 1, iEnd = theArg.length();
70 for(; i <= iEnd; i++) {
71 myStream<<theArg[i-1];
82 operator<<(CORBA::Object_ptr theArg)
84 CORBA::String_var aString("None");
85 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
86 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
87 SALOMEDS::SObject_var aSObject = SMESH_Gen_i::ObjectToSObject(aStudy,theArg);
88 if(!aSObject->_is_nil()){
89 aString = aSObject->GetID();
90 }else if(!CORBA::is_nil(theArg)){
91 aString = SMESH_Gen_i::GetORB()->object_to_string(theArg);
93 myStream<<aString.in();
99 operator<<(SMESH::FilterLibrary_i* theArg)
101 myStream<<"aFilterLibrary"<<theArg;
107 operator<<(SMESH::FilterManager_i* theArg)
109 myStream<<"aFilterManager"<<theArg;
115 operator<<(SMESH::Filter_i* theArg)
117 myStream<<"aFilter"<<theArg;
123 operator<<(SMESH::Functor_i* theArg)
125 FunctorType aFunctorType = theArg->GetFunctorType();
126 switch(aFunctorType){
128 myStream<<"anAspectRatio";
130 case FT_AspectRatio3D:
131 myStream<<"anAspectRatio3D";
134 myStream<<"aWarping";
136 case FT_MinimumAngle:
137 myStream<<"aMinimumAngle";
149 myStream<<"aFreeBorders";
152 myStream<<"aFreeEdges";
154 case FT_MultiConnection:
155 myStream<<"aMultiConnection";
157 case FT_MultiConnection2D:
158 myStream<<"aMultiConnection2D";
166 case FT_BelongToGeom:
167 myStream<<"aBelongToGeom";
169 case FT_BelongToPlane:
170 myStream<<"aBelongToPlane";
172 case FT_BelongToCylinder:
173 myStream<<"aBelongToCylinder";
176 myStream<<"aLyingOnGeom";
179 myStream<<"aRangeOfIds";
181 case FT_BadOrientedVolume:
182 myStream<<"aBadOrientedVolume";
185 myStream<<"aLessThan";
188 myStream<<"aMoreThan";
191 myStream<<"anEqualTo";
194 myStream<<"aLogicalNOT";
197 myStream<<"aLogicalAND";
200 myStream<<"aLogicalOR";
203 myStream<<"anUndefined";
211 //=======================================================================
212 //function : DumpPython
214 //=======================================================================
215 Engines::TMPFile* SMESH_Gen_i::DumpPython (CORBA::Object_ptr theStudy,
216 CORBA::Boolean isPublished,
217 CORBA::Boolean& isValidScript)
219 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
220 if (CORBA::is_nil(aStudy))
221 return new Engines::TMPFile(0);
223 SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
224 if (CORBA::is_nil(aSO))
225 return new Engines::TMPFile(0);
227 // Map study entries to object names
228 Resource_DataMapOfAsciiStringAsciiString aMap;
229 Resource_DataMapOfAsciiStringAsciiString aMapNames;
230 TCollection_AsciiString s ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
232 SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
233 for (Itr->InitEx(true); Itr->More(); Itr->Next()) {
234 SALOMEDS::SObject_var aValue = Itr->Value();
236 TCollection_AsciiString aName (aValue->GetName());
237 TCollection_AsciiString aGUIName (aName);
238 if (aName.Length() > 0) {
239 aMapNames.Bind(TCollection_AsciiString(aValue->GetID()), aGUIName);
240 int p, p2 = 1, e = aName.Length();
241 while ((p = aName.FirstLocationNotInSet(s, p2, e))) {
242 aName.SetValue(p, '_');
245 aMap.Bind(TCollection_AsciiString(aValue->GetID()), aName);
249 // Get trace of restored study
250 //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
251 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
252 SALOMEDS::GenericAttribute_var anAttr =
253 aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
255 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
256 TCollection_AsciiString aSavedTrace (oldValue);
258 // Add trace of API methods calls and replace study entries by names
260 TCollection_AsciiString aScript = DumpPython_impl
261 (aStudy->StudyId(), aMap, aMapNames, isPublished, aValidScript, aSavedTrace);
263 int aLen = aScript.Length();
264 unsigned char* aBuffer = new unsigned char[aLen+1];
265 strcpy((char*)aBuffer, aScript.ToCString());
267 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
268 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
269 isValidScript = aValidScript;
271 return aStreamFile._retn();
274 //=============================================================================
278 //=============================================================================
279 void SMESH_Gen_i::AddToPythonScript (int theStudyID, const TCollection_AsciiString& theString)
281 if (myPythonScripts.find(theStudyID) == myPythonScripts.end()) {
282 myPythonScripts[theStudyID] = new TColStd_HSequenceOfAsciiString;
284 myPythonScripts[theStudyID]->Append(theString);
287 //=============================================================================
289 * RemoveLastFromPythonScript
291 //=============================================================================
292 void SMESH_Gen_i::RemoveLastFromPythonScript (int theStudyID)
294 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
295 int aLen = myPythonScripts[theStudyID]->Length();
296 myPythonScripts[theStudyID]->Remove(aLen);
300 //=======================================================================
301 //function : AddToCurrentPyScript
303 //=======================================================================
305 void SMESH_Gen_i::AddToCurrentPyScript (const TCollection_AsciiString& theString)
307 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
308 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
309 if (aStudy->_is_nil()) return;
310 aSMESHGen->AddToPythonScript(aStudy->StudyId(), theString);
314 //=======================================================================
315 //function : AddObject
316 //purpose : add object to script string
317 //=======================================================================
319 TCollection_AsciiString& SMESH_Gen_i::AddObject(TCollection_AsciiString& theStr,
320 CORBA::Object_ptr theObject)
322 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
323 SALOMEDS::SObject_var aSO =
324 aSMESHGen->ObjectToSObject(aSMESHGen->GetCurrentStudy(), theObject);
325 if ( !aSO->_is_nil() )
326 theStr += aSO->GetID();
327 else if ( !CORBA::is_nil( theObject ) )
328 theStr += GetORB()->object_to_string( theObject );
335 //=======================================================================
336 //function : SavePython
338 //=======================================================================
339 void SMESH_Gen_i::SavePython (SALOMEDS::Study_ptr theStudy)
341 // Dump trace of API methods calls
342 TCollection_AsciiString aScript = GetNewPythonLines(theStudy->StudyId());
344 // Check contents of PythonObject attribute
345 SALOMEDS::SObject_var aSO = theStudy->FindComponent(ComponentDataType());
346 //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
347 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
348 SALOMEDS::GenericAttribute_var anAttr =
349 aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
351 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
352 TCollection_AsciiString oldScript (oldValue);
354 if (oldScript.Length() > 0) {
356 oldScript += aScript;
361 // Store in PythonObject attribute
362 SALOMEDS::AttributePythonObject::_narrow(anAttr)->SetObject(oldScript.ToCString(), 1);
364 // Clean trace of API methods calls
365 CleanPythonTrace(theStudy->StudyId());
372 //=============================================================================
374 * FindEntries: Returns a sequence of start/end positions of entries in the string
376 //=============================================================================
377 Handle(TColStd_HSequenceOfInteger) FindEntries (TCollection_AsciiString& theString)
379 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
380 Standard_Integer aLen = theString.Length();
381 Standard_Boolean isFound = Standard_False;
383 char* arr = theString.ToCString();
384 Standard_Integer i = 0, j;
389 if(c >= 48 && c <= 57) { //Is digit?
391 isFound = Standard_False;
392 while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
394 if(c == 58) isFound = Standard_True;
398 int prev = (i < 1) ? 0 : (int)arr[i - 1];
399 // last char should be a diggit,
400 // previous char should not be '"'.
401 if (arr[j-2] != 58 && prev != 34) {
402 aSeq->Append(i+1); // +1 because AsciiString starts from 1
414 //=============================================================================
418 //=============================================================================
419 TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
421 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
422 Resource_DataMapOfAsciiStringAsciiString& theNames,
425 const TCollection_AsciiString& theSavedTrace)
427 TCollection_AsciiString aScript;
428 aScript += "import salome\n";
429 aScript += "import geompy\n\n";
430 aScript += "import SMESH\n";
431 aScript += "import StdMeshers\n\n";
432 aScript += "#import GEOM module\n";
433 aScript += "import string\n";
434 aScript += "import os\n";
435 aScript += "import sys\n";
436 aScript += "import re\n";
437 aScript += "sys.path.append( os.path.dirname(__file__) )\n";
438 aScript += "exec(\"from \"+re.sub(\"SMESH$\",\"GEOM\",__name__)+\" import *\")\n\n";
440 aScript += "def RebuildData(theStudy):";
441 aScript += "\n\tsmesh = salome.lcc.FindOrLoadComponent(\"FactoryServer\", \"SMESH\")";
443 aScript += "\n\tsmesh.SetCurrentStudy(theStudy)";
445 aScript += "\n\tsmesh.SetCurrentStudy(None)";
447 TCollection_AsciiString globalVars;
449 // Dump trace of restored study
450 if (theSavedTrace.Length() > 0) {
452 aScript += theSavedTrace;
455 // Dump trace of API methods calls
456 TCollection_AsciiString aNewLines = GetNewPythonLines(theStudyID);
457 if (aNewLines.Length() > 0) {
459 aScript += aNewLines;
462 // Find entries to be replaced by names
463 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
464 Standard_Integer aLen = aSeq->Length();
469 // Replace entries by the names
470 GEOM::GEOM_Gen_ptr geom = GetGeomEngine();
471 TColStd_SequenceOfAsciiString seqRemoved;
472 Resource_DataMapOfAsciiStringAsciiString mapRemoved;
473 Resource_DataMapOfAsciiStringAsciiString aNames;
474 Standard_Integer objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
475 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("smeshObj_");
477 for (Standard_Integer i = 1; i <= aLen; i += 2) {
478 anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i) - 1);
479 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
480 if (theObjectNames.IsBound(anEntry)) {
481 aName = theObjectNames.Find(anEntry);
482 if (theObjectNames.IsBound(aName) && anEntry != theObjectNames(aName)) {
483 // diff objects have same name - make a new name
484 TCollection_AsciiString aName2;
485 Standard_Integer i = 0;
487 aName2 = aName + "_" + ++i;
488 } while (theObjectNames.IsBound(aName2) && anEntry != theObjectNames(aName2));
490 theObjectNames(anEntry) = aName;
494 aName = geom->GetDumpName( anEntry.ToCString() );
495 if ( aName.IsEmpty() ) {
496 // ? Removed Object ?
498 aName = aBaseName + TCollection_AsciiString(++objectCounter);
499 } while (theObjectNames.IsBound(aName));
500 seqRemoved.Append(aName);
501 mapRemoved.Bind(anEntry, "1");
503 theObjectNames.Bind(anEntry, aName);
505 theObjectNames.Bind(aName, anEntry); // to detect same name of diff objects
507 anUpdatedScript += aName;
508 aNames.Bind(aName, "1");
509 aStart = aSeq->Value(i + 1) + 1;
512 // add final part of aScript
513 if (aSeq->Value(aLen) < aScriptLength)
514 anUpdatedScript += aScript.SubString(aSeq->Value(aLen) + 1, aScriptLength);
516 // Remove removed objects
517 anUpdatedScript += "\n\taStudyBuilder = theStudy.NewBuilder()";
518 for (int ir = 1; ir <= seqRemoved.Length(); ir++) {
519 anUpdatedScript += "\n\tSO = theStudy.FindObjectIOR(theStudy.ConvertObjectToIOR(";
520 anUpdatedScript += seqRemoved.Value(ir);
521 anUpdatedScript += "))\n\tif SO is not None: aStudyBuilder.RemoveObjectWithChildren(SO)";
525 anUpdatedScript += "\n\n\tisGUIMode = ";
526 anUpdatedScript += isPublished;
527 anUpdatedScript += "\n\tif isGUIMode:";
528 anUpdatedScript += "\n\t\tsmeshgui = salome.ImportComponentGUI(\"SMESH\")";
529 anUpdatedScript += "\n\t\tsmeshgui.Init(theStudy._get_StudyId())";
530 anUpdatedScript += "\n";
532 TCollection_AsciiString aGUIName;
533 Resource_DataMapOfAsciiStringAsciiString mapEntries;
534 for (Standard_Integer i = 1; i <= aLen; i += 2) {
535 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
536 aName = geom->GetDumpName( anEntry.ToCString() );
537 if (aName.IsEmpty() && // Not a GEOM object
538 theNames.IsBound(anEntry) &&
539 !mapEntries.IsBound(anEntry) && // Not yet processed
540 !mapRemoved.IsBound(anEntry)) { // Was not removed
541 aName = theObjectNames.Find(anEntry);
542 aGUIName = theNames.Find(anEntry);
543 mapEntries.Bind(anEntry, aName);
544 anUpdatedScript += "\n\t\tsmeshgui.SetName(salome.ObjectToID(";
545 anUpdatedScript += aName + "), \"" + aGUIName + "\")";
548 anUpdatedScript += "\n\n\t\tsalome.sg.updateObjBrowser(0)";
550 anUpdatedScript += "\n\n\tpass\n";
554 return anUpdatedScript;
557 //=============================================================================
561 //=============================================================================
562 TCollection_AsciiString SMESH_Gen_i::GetNewPythonLines (int theStudyID)
564 TCollection_AsciiString aScript;
566 // Dump trace of API methods calls
567 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
568 Handle(TColStd_HSequenceOfAsciiString) aPythonScript = myPythonScripts[theStudyID];
569 Standard_Integer istr, aLen = aPythonScript->Length();
570 for (istr = 1; istr <= aLen; istr++) {
572 aScript += aPythonScript->Value(istr);
580 //=============================================================================
584 //=============================================================================
585 void SMESH_Gen_i::CleanPythonTrace (int theStudyID)
587 TCollection_AsciiString aScript;
589 // Clean trace of API methods calls
590 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
591 myPythonScripts[theStudyID]->Clear();