1 // File : SMESH_Gen_i_DumpPython.cxx
2 // Created : Thu Mar 24 17:17:59 2005
3 // Author : Julia DOROVSKIKH
7 #include "SMESH_Gen_i.hxx"
9 #include <TColStd_HSequenceOfInteger.hxx>
11 //=======================================================================
12 //function : DumpPython
14 //=======================================================================
15 Engines::TMPFile* SMESH_Gen_i::DumpPython (CORBA::Object_ptr theStudy,
16 CORBA::Boolean isPublished,
17 CORBA::Boolean& isValidScript)
19 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
20 if (CORBA::is_nil(aStudy))
21 return new Engines::TMPFile(0);
23 SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
24 if (CORBA::is_nil(aSO))
25 return new Engines::TMPFile(0);
27 // Map study entries to object names
28 Resource_DataMapOfAsciiStringAsciiString aMap;
29 TCollection_AsciiString s ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
31 SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
32 for (Itr->InitEx(true); Itr->More(); Itr->Next()) {
33 SALOMEDS::SObject_var aValue = Itr->Value();
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, '_');
42 aMap.Bind(TCollection_AsciiString(aValue->GetID()), aName);
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");
52 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
53 TCollection_AsciiString aSavedTrace (oldValue);
55 // Add trace of API methods calls and replace study entries by names
57 //TCollection_AsciiString aScript = myGen.DumpPython
58 TCollection_AsciiString aScript = DumpPython_impl
59 (aStudy->StudyId(), aMap, isPublished, aValidScript, aSavedTrace);
61 int aLen = aScript.Length();
62 unsigned char* aBuffer = new unsigned char[aLen+1];
63 strcpy((char*)aBuffer, aScript.ToCString());
65 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
66 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
67 isValidScript = aValidScript;
69 return aStreamFile._retn();
72 //=============================================================================
76 //=============================================================================
77 void SMESH_Gen_i::AddToPythonScript (int theStudyID, const TCollection_AsciiString& theString)
79 if (myPythonScripts.find(theStudyID) == myPythonScripts.end()) {
80 myPythonScripts[theStudyID] = new TColStd_HSequenceOfAsciiString;
82 myPythonScripts[theStudyID]->Append(theString);
85 //=============================================================================
87 * RemoveLastFromPythonScript
89 //=============================================================================
90 void SMESH_Gen_i::RemoveLastFromPythonScript (int theStudyID)
92 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
93 int aLen = myPythonScripts[theStudyID]->Length();
94 myPythonScripts[theStudyID]->Remove(aLen);
98 //=======================================================================
99 //function : AddToCurrentPyScript
101 //=======================================================================
103 void SMESH_Gen_i::AddToCurrentPyScript (const TCollection_AsciiString& theString)
105 SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
106 aSMESHGen->AddToPythonScript(aSMESHGen->GetCurrentStudy()->StudyId(), theString);
110 //=======================================================================
111 //function : AddObject
112 //purpose : add object to script string
113 //=======================================================================
115 TCollection_AsciiString& SMESH_Gen_i::AddObject(TCollection_AsciiString& theStr,
116 CORBA::Object_ptr theObject)
118 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( theObject );
119 if ( !geomObj->_is_nil() ) {
120 theStr += "salome.IDToObject(\"";
121 theStr += geomObj->GetStudyEntry();
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 );
138 //=======================================================================
139 //function : SavePython
141 //=======================================================================
142 void SMESH_Gen_i::SavePython (SALOMEDS::Study_ptr theStudy)
144 // Dump trace of API methods calls
145 TCollection_AsciiString aScript = GetNewPythonLines(theStudy->StudyId());
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");
154 char* oldValue = SALOMEDS::AttributePythonObject::_narrow(anAttr)->GetObject();
155 TCollection_AsciiString oldScript (oldValue);
157 if (oldScript.Length() > 0) {
159 oldScript += aScript;
164 // Store in PythonObject attribute
165 SALOMEDS::AttributePythonObject::_narrow(anAttr)->SetObject(oldScript.ToCString(), 1);
167 // Clean trace of API methods calls
168 CleanPythonTrace(theStudy->StudyId());
175 //=============================================================================
177 * FindEntries: Returns a sequence of start/end positions of entries in the string
179 //=============================================================================
180 Handle(TColStd_HSequenceOfInteger) FindEntries (TCollection_AsciiString& theString)
182 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
183 Standard_Integer aLen = theString.Length();
184 Standard_Boolean isFound = Standard_False;
186 char* arr = theString.ToCString();
187 Standard_Integer i = 0, j;
192 if(c >= 48 && c <= 57) { //Is digit?
194 isFound = Standard_False;
195 while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
197 if(c == 58) isFound = Standard_True;
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
217 //=============================================================================
221 //=============================================================================
222 TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
224 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
227 const TCollection_AsciiString& theSavedTrace)
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)";
238 // Dump trace of restored study
239 if (theSavedTrace.Length() > 0) {
241 aScript += theSavedTrace;
244 // Dump trace of API methods calls
245 TCollection_AsciiString aNewLines = GetNewPythonLines(theStudyID);
246 if (aNewLines.Length() > 0) {
248 aScript += aNewLines;
251 // Find entries to be replaced by names
252 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
253 Standard_Integer aLen = aSeq->Length();
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_");
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;
275 aName2 = aName + "_" + ++i;
276 } while (theObjectNames.IsBound(aName2) && anEntry != theObjectNames(aName2));
278 theObjectNames(anEntry) = aName;
281 // ? Removed Object ?
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");
289 theObjectNames.Bind(aName, anEntry); // to detect same name of diff objects
291 anUpdatedScript += aName;
292 aNames.Bind(aName, "1");
293 aStart = aSeq->Value(i + 1) + 1;
296 // add final part of aScript
297 if (aSeq->Value(aLen) < aScriptLength)
298 anUpdatedScript += aScript.SubString(aSeq->Value(aLen) + 1, aScriptLength);
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)";
307 anUpdatedScript += "\n";
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";
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 + "\")";
328 anUpdatedScript += "\n\n\t\tsalome.sg.updateObjBrowser(0)";
330 anUpdatedScript += "\n\n\tpass\n";
334 return anUpdatedScript;
337 //=============================================================================
341 //=============================================================================
342 TCollection_AsciiString SMESH_Gen_i::GetNewPythonLines (int theStudyID)
344 TCollection_AsciiString aScript;
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++) {
352 aScript += aPythonScript->Value(istr);
360 //=============================================================================
364 //=============================================================================
365 void SMESH_Gen_i::CleanPythonTrace (int theStudyID)
367 TCollection_AsciiString aScript;
369 // Clean trace of API methods calls
370 if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
371 myPythonScripts[theStudyID]->Clear();