]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESH_I/SMESH_Gen_i_DumpPython.cxx
Salome HOME
adfb83764f822cf319a938ff38722d3476fc4e8c
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i_DumpPython.cxx
1 // File    : SMESH_Gen_i_DumpPython.cxx
2 // Created : Thu Mar 24 17:17:59 2005
3 // Author  : Julia DOROVSKIKH
4 // Module  : SMESH
5 // $Header : $
6
7 #include "SMESH_Gen_i.hxx"
8
9 #include <TColStd_HSequenceOfInteger.hxx>
10
11 //=======================================================================
12 //function : DumpPython
13 //purpose  : 
14 //=======================================================================
15 Engines::TMPFile* SMESH_Gen_i::DumpPython (CORBA::Object_ptr theStudy,
16                                            CORBA::Boolean isPublished,
17                                            CORBA::Boolean& isValidScript)
18 {
19   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
20   if (CORBA::is_nil(aStudy))
21     return new Engines::TMPFile(0);
22
23   SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
24   if (CORBA::is_nil(aSO))
25     return new Engines::TMPFile(0);
26
27   // Map study entries to object names
28   Resource_DataMapOfAsciiStringAsciiString aMap;
29   TCollection_AsciiString s ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
30
31   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
32   for (Itr->InitEx(true); Itr->More(); Itr->Next()) {
33     SALOMEDS::SObject_var aValue = Itr->Value();
34
35     TCollection_AsciiString aName (aValue->GetName());
36     if (aName.Length() > 0) {
37       int p, p2 = 1, e = aName.Length();
38       while ((p = aName.FirstLocationNotInSet(s, p2, e))) {
39         aName.SetValue(p, '_');
40         p2 = p;
41       }
42       aMap.Bind(TCollection_AsciiString(aValue->GetID()), aName);
43     }
44   }
45
46   // Get trace of restored study
47   //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
48   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
49   SALOMEDS::GenericAttribute_var anAttr =
50     aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
51
52   char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
53   TCollection_AsciiString aSavedTrace (oldValue);
54
55   // Add trace of API methods calls and replace study entries by names
56   bool aValidScript;
57   //TCollection_AsciiString aScript = myGen.DumpPython
58   TCollection_AsciiString aScript = DumpPython_impl
59     (aStudy->StudyId(), aMap, isPublished, aValidScript, aSavedTrace);
60
61   int aLen = aScript.Length(); 
62   unsigned char* aBuffer = new unsigned char[aLen+1];
63   strcpy((char*)aBuffer, aScript.ToCString());
64
65   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
66   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1); 
67   isValidScript = aValidScript;
68
69   return aStreamFile._retn(); 
70 }
71
72 //=============================================================================
73 /*!
74  *  AddToPythonScript
75  */
76 //=============================================================================
77 void SMESH_Gen_i::AddToPythonScript (int theStudyID, const TCollection_AsciiString& theString)
78 {
79   if (myPythonScripts.find(theStudyID) == myPythonScripts.end()) {
80     myPythonScripts[theStudyID] = new TColStd_HSequenceOfAsciiString;
81   }
82   myPythonScripts[theStudyID]->Append(theString);
83 }
84
85 //=============================================================================
86 /*!
87  *  RemoveLastFromPythonScript
88  */
89 //=============================================================================
90 void SMESH_Gen_i::RemoveLastFromPythonScript (int theStudyID)
91 {
92   if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
93     int aLen = myPythonScripts[theStudyID]->Length();
94     myPythonScripts[theStudyID]->Remove(aLen);
95   }
96 }
97
98 //=======================================================================
99 //function : AddToCurrentPyScript
100 //purpose  : 
101 //=======================================================================
102
103 void SMESH_Gen_i::AddToCurrentPyScript (const TCollection_AsciiString& theString)
104 {
105   SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
106   aSMESHGen->AddToPythonScript(aSMESHGen->GetCurrentStudy()->StudyId(), theString);
107 }
108
109
110 //=======================================================================
111 //function : AddObject
112 //purpose  : add object to script string
113 //=======================================================================
114
115 TCollection_AsciiString& SMESH_Gen_i::AddObject(TCollection_AsciiString& theStr,
116                                                 CORBA::Object_ptr        theObject)
117 {
118   GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( theObject );
119   if ( !geomObj->_is_nil() ) {
120     theStr += "salome.IDToObject(\"";
121     theStr += geomObj->GetStudyEntry();
122     theStr += "\")";
123   }
124   else {
125     SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
126     SALOMEDS::SObject_var aSO =
127       aSMESHGen->ObjectToSObject(aSMESHGen->GetCurrentStudy(), theObject);
128     if ( !aSO->_is_nil() )
129       theStr += aSO->GetID();
130     else if ( !CORBA::is_nil( theObject ) )
131       theStr += aSMESHGen->GetORB()->object_to_string( theObject );
132     else
133       theStr += "None";
134   }
135   return theStr;
136 }
137
138 //=======================================================================
139 //function : SavePython
140 //purpose  : 
141 //=======================================================================
142 void SMESH_Gen_i::SavePython (SALOMEDS::Study_ptr theStudy)
143 {
144   // Dump trace of API methods calls
145   TCollection_AsciiString aScript = GetNewPythonLines(theStudy->StudyId());
146
147   // Check contents of PythonObject attribute
148   SALOMEDS::SObject_var aSO = theStudy->FindComponent(ComponentDataType());
149   //SALOMEDS::SObject_var aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
150   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
151   SALOMEDS::GenericAttribute_var anAttr =
152     aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
153
154   char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
155   TCollection_AsciiString oldScript (oldValue);
156
157   if (oldScript.Length() > 0) {
158     oldScript += "\n";
159     oldScript += aScript;
160   } else {
161     oldScript = aScript;
162   }
163
164   // Store in PythonObject attribute
165   SALOMEDS::AttributePythonObject::_narrow(anAttr)->SetObject(oldScript.ToCString(), 1);
166
167   // Clean trace of API methods calls
168   CleanPythonTrace(theStudy->StudyId());
169 }
170
171
172 // impl
173
174
175 //=============================================================================
176 /*!
177  *  FindEntries: Returns a sequence of start/end positions of entries in the string
178  */
179 //=============================================================================
180 Handle(TColStd_HSequenceOfInteger) FindEntries (TCollection_AsciiString& theString)
181 {
182   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
183   Standard_Integer aLen = theString.Length();
184   Standard_Boolean isFound = Standard_False;
185
186   char* arr = theString.ToCString();
187   Standard_Integer i = 0, j;
188
189   while(i < aLen) {
190     int c = (int)arr[i];
191     j = i+1;
192     if(c >= 48 && c <= 57) { //Is digit?
193  
194       isFound = Standard_False;
195       while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
196         c = (int)arr[j++];  
197         if(c == 58) isFound = Standard_True;
198       }
199
200       if (isFound) {
201         int prev = (i < 1) ? 0 : (int)arr[i - 1];
202         // last char should be a diggit,
203         // previous char should not be '"'.
204         if (arr[j-2] != 58 && prev != 34) {
205           aSeq->Append(i+1); // +1 because AsciiString starts from 1
206           aSeq->Append(j-1);
207         }
208       }
209     }
210
211     i = j;
212   }
213
214   return aSeq;
215 }
216
217 //=============================================================================
218 /*!
219  *  DumpPython
220  */
221 //=============================================================================
222 TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
223                         (int theStudyID, 
224                          Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
225                          bool isPublished, 
226                          bool& aValidScript,
227                          const TCollection_AsciiString& theSavedTrace)
228 {
229   TCollection_AsciiString aScript;
230   aScript += "import salome\n";
231   aScript += "import geompy\n\n";
232   aScript += "import SMESH\n";
233   aScript += "import StdMeshers\n\n";
234   aScript += "def RebuildData(theStudy):";
235   aScript += "\n\tsmesh = salome.lcc.FindOrLoadComponent(\"FactoryServer\", \"SMESH\")";
236   aScript += "\n\tsmesh.SetCurrentStudy(theStudy)";
237
238   // Dump trace of restored study
239   if (theSavedTrace.Length() > 0) {
240     aScript += "\n";
241     aScript += theSavedTrace;
242   }
243
244   // Dump trace of API methods calls
245   TCollection_AsciiString aNewLines = GetNewPythonLines(theStudyID);
246   if (aNewLines.Length() > 0) {
247     aScript += "\n";
248     aScript += aNewLines;
249   }
250
251   // Find entries to be replaced by names
252   Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
253   Standard_Integer aLen = aSeq->Length();
254
255   if (aLen == 0)
256     return aScript;
257
258   // Replace entries by the names
259   TColStd_SequenceOfAsciiString seqRemoved;
260   Resource_DataMapOfAsciiStringAsciiString mapRemoved;
261   Resource_DataMapOfAsciiStringAsciiString aNames;
262   Standard_Integer objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
263   TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("smeshObj_");
264
265   for (Standard_Integer i = 1; i <= aLen; i += 2) {
266     anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i) - 1);
267     anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
268     if (theObjectNames.IsBound(anEntry)) {
269       aName = theObjectNames.Find(anEntry);
270       if (theObjectNames.IsBound(aName) && anEntry != theObjectNames(aName)) {
271         // diff objects have same name - make a new name
272         TCollection_AsciiString aName2;
273         Standard_Integer i = 0;
274         do {
275           aName2 = aName + "_" + ++i;
276         } while (theObjectNames.IsBound(aName2) && anEntry != theObjectNames(aName2));
277         aName = aName2;
278         theObjectNames(anEntry) = aName;
279       }
280     } else {
281       // ? Removed Object ?
282       do {
283         aName = aBaseName + TCollection_AsciiString(++objectCounter);
284       } while (theObjectNames.IsBound(aName));
285       theObjectNames.Bind(anEntry, aName);
286       seqRemoved.Append(aName);
287       mapRemoved.Bind(anEntry, "1");
288     }
289     theObjectNames.Bind(aName, anEntry); // to detect same name of diff objects
290
291     anUpdatedScript += aName;
292     aNames.Bind(aName, "1");
293     aStart = aSeq->Value(i + 1) + 1;
294   }
295
296   // add final part of aScript
297   if (aSeq->Value(aLen) < aScriptLength)
298     anUpdatedScript += aScript.SubString(aSeq->Value(aLen) + 1, aScriptLength);
299
300   // Remove removed objects
301   anUpdatedScript += "\n\taStudyBuilder = theStudy.NewBuilder()";
302   for (int ir = 1; ir <= seqRemoved.Length(); ir++) {
303     anUpdatedScript += "\n\tSO = theStudy.FindObjectIOR(theStudy.ConvertObjectToIOR(";
304     anUpdatedScript += seqRemoved.Value(ir);
305     anUpdatedScript += "))\n\tif SO is not None: aStudyBuilder.RemoveObjectWithChildren(SO)";
306   }
307   anUpdatedScript += "\n";
308
309   // Set object names
310   anUpdatedScript += "\n\tisGUIMode = 1";
311   anUpdatedScript += "\n\tif isGUIMode:";
312   anUpdatedScript += "\n\t\tsmeshgui = salome.ImportComponentGUI(\"SMESH\")";
313   anUpdatedScript += "\n\t\tsmeshgui.Init(theStudy._get_StudyId())";
314   anUpdatedScript += "\n";
315
316   Resource_DataMapOfAsciiStringAsciiString mapEntries;
317   for (Standard_Integer i = 1; i <= aLen; i += 2) {
318     anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
319     if (theObjectNames.IsBound(anEntry) &&
320         !mapEntries.IsBound(anEntry) &&
321         !mapRemoved.IsBound(anEntry)) {
322       aName = theObjectNames.Find(anEntry);
323       mapEntries.Bind(anEntry, aName);
324       anUpdatedScript += "\n\t\tsmeshgui.SetName(salome.ObjectToID(";
325       anUpdatedScript += aName + "), \"" + aName + "\")";
326     }
327   }
328   anUpdatedScript += "\n\n\t\tsalome.sg.updateObjBrowser(0)";
329
330   anUpdatedScript += "\n\n\tpass\n";
331
332   aValidScript = true;
333
334   return anUpdatedScript;
335 }
336
337 //=============================================================================
338 /*!
339  *  GetNewPythonLines
340  */
341 //=============================================================================
342 TCollection_AsciiString SMESH_Gen_i::GetNewPythonLines (int theStudyID)
343 {
344   TCollection_AsciiString aScript;
345
346   // Dump trace of API methods calls
347   if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
348     Handle(TColStd_HSequenceOfAsciiString) aPythonScript = myPythonScripts[theStudyID];
349     Standard_Integer istr, aLen = aPythonScript->Length();
350     for (istr = 1; istr <= aLen; istr++) {
351       aScript += "\n\t";
352       aScript += aPythonScript->Value(istr);
353     }
354     aScript += "\n";
355   }
356
357   return aScript;
358 }
359
360 //=============================================================================
361 /*!
362  *  CleanPythonTrace
363  */
364 //=============================================================================
365 void SMESH_Gen_i::CleanPythonTrace (int theStudyID)
366 {
367   TCollection_AsciiString aScript;
368
369   // Clean trace of API methods calls
370   if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
371     myPythonScripts[theStudyID]->Clear();
372   }
373 }