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>
15 TCollection_AsciiString&
16 operator<<(TCollection_AsciiString& theString, const char* theArg){
17 theString += Standard_CString(theArg);
21 TCollection_AsciiString&
22 operator<<(TCollection_AsciiString& theString, int theArg){
23 theString += TCollection_AsciiString(theArg);
27 TCollection_AsciiString&
28 operator<<(TCollection_AsciiString& theString, float theArg){
29 theString += TCollection_AsciiString(theArg);
33 TCollection_AsciiString&
34 operator<<(TCollection_AsciiString& theString,
35 const SMESH::long_array& theArg)
38 CORBA::Long i = 1, iEnd = theArg.length();
39 for(; i <= iEnd; i++) {
40 theString<<int(theArg[i-1]);
49 TCollection_AsciiString&
50 operator<<(TCollection_AsciiString& theString,
51 CORBA::Object_ptr theArg)
53 CORBA::String_var aString("None");
54 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
55 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
56 SALOMEDS::SObject_var aSObject = SMESH_Gen_i::ObjectToSObject(aStudy,theArg);
57 if(!aSObject->_is_nil()){
58 aString = aSObject->GetID();
59 }else if(!CORBA::is_nil(theArg)){
60 aString = SMESH_Gen_i::GetORB()->object_to_string(theArg);
62 theString<<aString.in();
66 TCollection_AsciiString&
67 operator<<(TCollection_AsciiString& theString,
68 SMESH::FilterLibrary_i* theArg)
70 theString += TCollection_AsciiString("aFilterLibrary_");
71 theString += TCollection_AsciiString(int(theArg));
75 TCollection_AsciiString&
76 operator<<(TCollection_AsciiString& theString,
77 SMESH::FilterManager_i* theArg)
79 theString += TCollection_AsciiString("aFilterManager_");
80 theString += TCollection_AsciiString(int(theArg));
84 TCollection_AsciiString&
85 operator<<(TCollection_AsciiString& theString,
86 SMESH::Filter_i* theArg)
88 theString += TCollection_AsciiString("aFilter_");
89 theString += TCollection_AsciiString(int(theArg));
93 TCollection_AsciiString&
94 operator<<(TCollection_AsciiString& theString,
95 SMESH::Functor_i* theArg)
97 FunctorType aFunctorType = theArg->GetFunctorType();
100 theString += TCollection_AsciiString("anAspectRatio");
102 case FT_AspectRatio3D:
103 theString += TCollection_AsciiString("anAspectRatio3D");
106 theString += TCollection_AsciiString("aWarping");
108 case FT_MinimumAngle:
109 theString += TCollection_AsciiString("aMinimumAngle");
112 theString += TCollection_AsciiString("aTaper");
115 theString += TCollection_AsciiString("aSkew");
118 theString += TCollection_AsciiString("aArea");
121 theString += TCollection_AsciiString("aFreeBorders");
124 theString += TCollection_AsciiString("aFreeEdges");
126 case FT_MultiConnection:
127 theString += TCollection_AsciiString("aMultiConnection");
129 case FT_MultiConnection2D:
130 theString += TCollection_AsciiString("aMultiConnection2D");
133 theString += TCollection_AsciiString("aLength");
136 theString += TCollection_AsciiString("aLength");
138 case FT_BelongToGeom:
139 theString += TCollection_AsciiString("aBelongToGeom");
141 case FT_BelongToPlane:
142 theString += TCollection_AsciiString("aBelongToPlane");
144 case FT_BelongToCylinder:
145 theString += TCollection_AsciiString("aBelongToCylinder");
148 theString += TCollection_AsciiString("aLyingOnGeom");
151 theString += TCollection_AsciiString("aRangeOfIds");
153 case FT_BadOrientedVolume:
154 theString += TCollection_AsciiString("aBadOrientedVolume");
157 theString += TCollection_AsciiString("aLessThan");
160 theString += TCollection_AsciiString("aMoreThan");
163 theString += TCollection_AsciiString("anEqualTo");
166 theString += TCollection_AsciiString("aLogicalNOT");
169 theString += TCollection_AsciiString("aLogicalAND");
172 theString += TCollection_AsciiString("aLogicalOR");
175 theString += TCollection_AsciiString("anUndefined");
178 theString += Standard_CString("_");
179 theString += TCollection_AsciiString(int(theArg));
186 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
187 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
188 if(!aStudy->_is_nil()){
189 aSMESHGen->AddToPythonScript(aStudy->StudyId(),myString);
194 //=======================================================================
195 //function : DumpPython
197 //=======================================================================
198 Engines::TMPFile* SMESH_Gen_i::DumpPython (CORBA::Object_ptr theStudy,
199 CORBA::Boolean isPublished,
200 CORBA::Boolean& isValidScript)
202 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
203 if (CORBA::is_nil(aStudy))
204 return new Engines::TMPFile(0);
206 SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
207 if (CORBA::is_nil(aSO))
208 return new Engines::TMPFile(0);
210 // Map study entries to object names
211 Resource_DataMapOfAsciiStringAsciiString aMap;
212 TCollection_AsciiString s ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
214 SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
215 for (Itr->InitEx(true); Itr->More(); Itr->Next()) {
216 SALOMEDS::SObject_var aValue = Itr->Value();
218 TCollection_AsciiString aName (aValue->GetName());
219 if (aName.Length() > 0) {
220 int p, p2 = 1, e = aName.Length();
221 while ((p = aName.FirstLocationNotInSet(s, p2, e))) {
222 aName.SetValue(p, '_');
225 aMap.Bind(TCollection_AsciiString(aValue->GetID()), aName);
229 // Get trace of restored study
230 //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
231 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
232 SALOMEDS::GenericAttribute_var anAttr =
233 aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
235 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
236 TCollection_AsciiString aSavedTrace (oldValue);
238 // Add trace of API methods calls and replace study entries by names
240 //TCollection_AsciiString aScript = myGen.DumpPython
241 TCollection_AsciiString aScript = DumpPython_impl
242 (aStudy->StudyId(), aMap, isPublished, aValidScript, aSavedTrace);
244 int aLen = aScript.Length();
245 unsigned char* aBuffer = new unsigned char[aLen+1];
246 strcpy((char*)aBuffer, aScript.ToCString());
248 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
249 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
250 isValidScript = aValidScript;
252 return aStreamFile._retn();
255 //=============================================================================
259 //=============================================================================
260 void SMESH_Gen_i::AddToPythonScript (int theStudyID, const TCollection_AsciiString& theString)
262 if (myPythonScripts.find(theStudyID) == myPythonScripts.end()) {
263 myPythonScripts[theStudyID] = new TColStd_HSequenceOfAsciiString;
265 myPythonScripts[theStudyID]->Append(theString);
268 //=============================================================================
270 * RemoveLastFromPythonScript
272 //=============================================================================
273 void SMESH_Gen_i::RemoveLastFromPythonScript (int theStudyID)
275 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
276 int aLen = myPythonScripts[theStudyID]->Length();
277 myPythonScripts[theStudyID]->Remove(aLen);
281 //=======================================================================
282 //function : AddToCurrentPyScript
284 //=======================================================================
286 void SMESH_Gen_i::AddToCurrentPyScript (const TCollection_AsciiString& theString)
288 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
289 SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
290 if (aStudy->_is_nil()) return;
291 aSMESHGen->AddToPythonScript(aStudy->StudyId(), theString);
295 //=======================================================================
296 //function : AddObject
297 //purpose : add object to script string
298 //=======================================================================
300 TCollection_AsciiString& SMESH_Gen_i::AddObject(TCollection_AsciiString& theStr,
301 CORBA::Object_ptr theObject)
303 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
304 SALOMEDS::SObject_var aSO =
305 aSMESHGen->ObjectToSObject(aSMESHGen->GetCurrentStudy(), theObject);
306 if ( !aSO->_is_nil() )
307 theStr += aSO->GetID();
308 else if ( !CORBA::is_nil( theObject ) )
309 theStr += GetORB()->object_to_string( theObject );
316 //=======================================================================
317 //function : SavePython
319 //=======================================================================
320 void SMESH_Gen_i::SavePython (SALOMEDS::Study_ptr theStudy)
322 // Dump trace of API methods calls
323 TCollection_AsciiString aScript = GetNewPythonLines(theStudy->StudyId());
325 // Check contents of PythonObject attribute
326 SALOMEDS::SObject_var aSO = theStudy->FindComponent(ComponentDataType());
327 //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
328 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
329 SALOMEDS::GenericAttribute_var anAttr =
330 aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
332 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
333 TCollection_AsciiString oldScript (oldValue);
335 if (oldScript.Length() > 0) {
337 oldScript += aScript;
342 // Store in PythonObject attribute
343 SALOMEDS::AttributePythonObject::_narrow(anAttr)->SetObject(oldScript.ToCString(), 1);
345 // Clean trace of API methods calls
346 CleanPythonTrace(theStudy->StudyId());
353 //=============================================================================
355 * FindEntries: Returns a sequence of start/end positions of entries in the string
357 //=============================================================================
358 Handle(TColStd_HSequenceOfInteger) FindEntries (TCollection_AsciiString& theString)
360 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
361 Standard_Integer aLen = theString.Length();
362 Standard_Boolean isFound = Standard_False;
364 char* arr = theString.ToCString();
365 Standard_Integer i = 0, j;
370 if(c >= 48 && c <= 57) { //Is digit?
372 isFound = Standard_False;
373 while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
375 if(c == 58) isFound = Standard_True;
379 int prev = (i < 1) ? 0 : (int)arr[i - 1];
380 // last char should be a diggit,
381 // previous char should not be '"'.
382 if (arr[j-2] != 58 && prev != 34) {
383 aSeq->Append(i+1); // +1 because AsciiString starts from 1
395 //=============================================================================
399 //=============================================================================
400 TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
402 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
405 const TCollection_AsciiString& theSavedTrace)
407 TCollection_AsciiString aScript;
408 aScript += "import salome\n";
409 aScript += "import geompy\n\n";
410 aScript += "import SMESH\n";
411 aScript += "import StdMeshers\n\n";
412 aScript += "#import GEOM module\n";
413 aScript += "import string\n";
414 aScript += "import os\n";
415 aScript += "import sys\n";
416 aScript += "sys.path.append( os.path.dirname(__file__) )\n";
417 aScript += "exec(\"from \"+string.replace(__name__,\"SMESH\",\"GEOM\")+\" import *\")\n\n";
419 aScript += "def RebuildData(theStudy):";
420 aScript += "\n\tsmesh = salome.lcc.FindOrLoadComponent(\"FactoryServer\", \"SMESH\")";
422 aScript += "\n\tsmesh.SetCurrentStudy(theStudy)";
424 aScript += "\n\tsmesh.SetCurrentStudy(None)";
426 TCollection_AsciiString globalVars;
428 // Dump trace of restored study
429 if (theSavedTrace.Length() > 0) {
431 aScript += theSavedTrace;
434 // Dump trace of API methods calls
435 TCollection_AsciiString aNewLines = GetNewPythonLines(theStudyID);
436 if (aNewLines.Length() > 0) {
438 aScript += aNewLines;
441 // Find entries to be replaced by names
442 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
443 Standard_Integer aLen = aSeq->Length();
448 // Replace entries by the names
449 GEOM::GEOM_Gen_ptr geom = GetGeomEngine();
450 TColStd_SequenceOfAsciiString seqRemoved;
451 Resource_DataMapOfAsciiStringAsciiString mapRemoved;
452 Resource_DataMapOfAsciiStringAsciiString aNames;
453 Standard_Integer objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
454 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("smeshObj_");
456 for (Standard_Integer i = 1; i <= aLen; i += 2) {
457 anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i) - 1);
458 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
459 if (theObjectNames.IsBound(anEntry)) {
460 aName = theObjectNames.Find(anEntry);
461 if (theObjectNames.IsBound(aName) && anEntry != theObjectNames(aName)) {
462 // diff objects have same name - make a new name
463 TCollection_AsciiString aName2;
464 Standard_Integer i = 0;
466 aName2 = aName + "_" + ++i;
467 } while (theObjectNames.IsBound(aName2) && anEntry != theObjectNames(aName2));
469 theObjectNames(anEntry) = aName;
473 aName = geom->GetDumpName( anEntry.ToCString() );
474 if ( aName.IsEmpty() ) {
475 // ? Removed Object ?
477 aName = aBaseName + TCollection_AsciiString(++objectCounter);
478 } while (theObjectNames.IsBound(aName));
479 seqRemoved.Append(aName);
480 mapRemoved.Bind(anEntry, "1");
482 theObjectNames.Bind(anEntry, aName);
484 theObjectNames.Bind(aName, anEntry); // to detect same name of diff objects
486 anUpdatedScript += aName;
487 aNames.Bind(aName, "1");
488 aStart = aSeq->Value(i + 1) + 1;
491 // add final part of aScript
492 if (aSeq->Value(aLen) < aScriptLength)
493 anUpdatedScript += aScript.SubString(aSeq->Value(aLen) + 1, aScriptLength);
495 // Remove removed objects
496 anUpdatedScript += "\n\taStudyBuilder = theStudy.NewBuilder()";
497 for (int ir = 1; ir <= seqRemoved.Length(); ir++) {
498 anUpdatedScript += "\n\tSO = theStudy.FindObjectIOR(theStudy.ConvertObjectToIOR(";
499 anUpdatedScript += seqRemoved.Value(ir);
500 anUpdatedScript += "))\n\tif SO is not None: aStudyBuilder.RemoveObjectWithChildren(SO)";
502 anUpdatedScript += "\n";
505 anUpdatedScript += "\n\tisGUIMode = ";
506 anUpdatedScript += isPublished;
507 anUpdatedScript += "\n\tif isGUIMode:";
508 anUpdatedScript += "\n\t\tsmeshgui = salome.ImportComponentGUI(\"SMESH\")";
509 anUpdatedScript += "\n\t\tsmeshgui.Init(theStudy._get_StudyId())";
510 anUpdatedScript += "\n";
512 Resource_DataMapOfAsciiStringAsciiString mapEntries;
513 for (Standard_Integer i = 1; i <= aLen; i += 2) {
514 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
515 if (theObjectNames.IsBound(anEntry) &&
516 !mapEntries.IsBound(anEntry) &&
517 !mapRemoved.IsBound(anEntry)) {
518 aName = theObjectNames.Find(anEntry);
519 mapEntries.Bind(anEntry, aName);
520 anUpdatedScript += "\n\t\tsmeshgui.SetName(salome.ObjectToID(";
521 anUpdatedScript += aName + "), \"" + aName + "\")";
524 anUpdatedScript += "\n\n\t\tsalome.sg.updateObjBrowser(0)";
526 anUpdatedScript += "\n\n\tpass\n";
530 return anUpdatedScript;
533 //=============================================================================
537 //=============================================================================
538 TCollection_AsciiString SMESH_Gen_i::GetNewPythonLines (int theStudyID)
540 TCollection_AsciiString aScript;
542 // Dump trace of API methods calls
543 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
544 Handle(TColStd_HSequenceOfAsciiString) aPythonScript = myPythonScripts[theStudyID];
545 Standard_Integer istr, aLen = aPythonScript->Length();
546 for (istr = 1; istr <= aLen; istr++) {
548 aScript += aPythonScript->Value(istr);
556 //=============================================================================
560 //=============================================================================
561 void SMESH_Gen_i::CleanPythonTrace (int theStudyID)
563 TCollection_AsciiString aScript;
565 // Clean trace of API methods calls
566 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
567 myPythonScripts[theStudyID]->Clear();