Salome HOME
Update copyright information
[modules/geom.git] / src / GEOM / GEOM_Engine.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #ifdef WNT
23 #pragma warning( disable:4786 )
24 #endif
25
26 #include "GEOM_Engine.hxx"
27
28 #include "GEOM_Solver.hxx"
29 #include "GEOM_Function.hxx"
30 #include "GEOM_ISubShape.hxx"
31 #include "GEOM_SubShapeDriver.hxx"
32 #include "GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient.hxx"
33 #include "GEOM_PythonDump.hxx"
34
35 #include "utilities.h"
36
37 #include <TDF_Tool.hxx>
38 #include <TDF_Data.hxx>
39 #include <TDF_LabelSequence.hxx>
40 #include <TDataStd_Integer.hxx>
41 #include <TDataStd_ChildNodeIterator.hxx>
42 #include <TFunction_Driver.hxx>
43 #include <TFunction_DriverTable.hxx>
44
45 #include <TopExp.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47
48 #include <TCollection_AsciiString.hxx>
49 #include <TCollection_ExtendedString.hxx>
50 #include <TColStd_SequenceOfAsciiString.hxx>
51 #include <TColStd_MapOfTransient.hxx>
52 #include <TColStd_HSequenceOfInteger.hxx>
53
54 #include <Interface_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
55 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
56
57 #include <map>
58 #include <string>
59
60 #include <Standard_Failure.hxx>
61 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
62
63 static GEOM_Engine* TheEngine = NULL;
64
65 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_Object)& theObject)
66 {
67   TCollection_AsciiString anID(theObject->GetDocID()), anEntry;
68   TDF_Tool::Entry(theObject->GetEntry(), anEntry);
69   anID+=(TCollection_AsciiString("_")+anEntry);
70   return anID;
71 }
72
73 static TCollection_AsciiString BuildID(Standard_Integer theDocID, char* theEntry)
74 {
75   TCollection_AsciiString anID(theDocID);
76   anID+=(TCollection_AsciiString("_")+theEntry);
77   return anID;
78 }
79
80 static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
81 {
82   TCollection_AsciiString aDocID = theID.Token("_");
83   if(aDocID.Length() < 1) return -1;
84   return aDocID.IntegerValue();
85 }
86
87 void ProcessFunction(Handle(GEOM_Function)& theFunction, 
88                      TCollection_AsciiString& theScript,
89                      TColStd_MapOfTransient& theProcessed);
90
91 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString);
92
93 //=============================================================================
94 /*!
95  *  GetEngine
96  */
97 //=============================================================================
98 GEOM_Engine* GEOM_Engine::GetEngine() { return TheEngine; }
99
100
101 //=============================================================================
102 /*!
103  *  SetEngine
104  */
105 //=============================================================================
106 void GEOM_Engine::SetEngine(GEOM_Engine* theEngine) { TheEngine = theEngine; }
107
108 //=============================================================================
109 /*!
110  *  Constructor
111  */
112 //=============================================================================
113 GEOM_Engine::GEOM_Engine()
114 {
115   TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver());
116
117   _OCAFApp = new GEOM_Application();
118   _UndoLimit = 10;
119 }
120
121 /*!
122  *  Destructor
123  */
124 GEOM_Engine::~GEOM_Engine()
125
126   GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It(_objects);
127   for(; It.More(); It.Next()) 
128     {
129       RemoveObject(Handle(GEOM_Object)::DownCast(It.Value()));
130     }
131
132   //Close all documents not closed
133   for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
134     Close(anItr.Key());
135
136   _mapIDDocument.Clear();
137   _objects.Clear();
138 }
139
140 //=============================================================================
141 /*!
142  *  GetDocument
143  */
144 //=============================================================================
145 Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID)
146 {
147   Handle(TDocStd_Document) aDoc;
148   if(!_mapIDDocument.IsBound(theDocID)) {
149     _OCAFApp->NewDocument("SALOME_GEOM", aDoc);
150     aDoc->SetUndoLimit(_UndoLimit);
151     _mapIDDocument.Bind(theDocID, aDoc);
152     TDataStd_Integer::Set(aDoc->Main(), theDocID);
153   }
154
155   return Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
156 }
157
158 //=============================================================================
159 /*!
160  *  GetDocID
161  */
162 //=============================================================================
163 int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument)
164 {
165   if(theDocument.IsNull()) return -1;
166   for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
167     if(anItr.Value() == theDocument) return anItr.Key();
168
169   return -1;
170
171 }
172
173 //=============================================================================
174 /*!
175  *  GetObject
176  */
177 //=============================================================================
178 Handle(GEOM_Object) GEOM_Engine::GetObject(int theDocID, char* theEntry)
179 {
180   TCollection_AsciiString anID = BuildID(theDocID, theEntry);
181   if(_objects.IsBound(anID)) return Handle(GEOM_Object)::DownCast(_objects(anID));
182
183   TDF_Label aLabel;
184   Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
185   TDF_Tool::Label(aDoc->Main().Data(), theEntry, aLabel, Standard_True);
186   Handle(GEOM_Object) anObject = new GEOM_Object(aLabel);
187
188   _objects.Bind(anID, anObject);
189
190   return anObject;
191 }
192
193 //=============================================================================
194 /*!
195  *  AddObject
196  */
197 //=============================================================================
198 Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
199 {
200   Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
201   Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
202
203   // NPAL18604: use existing label to decrease memory usage,
204   //            if this label has been freed (object deleted)
205   bool useExisting = false;
206   TDF_Label aChild;
207   if (!_lastCleared.IsNull()) {
208     if (_lastCleared.Root() == aDoc->Main().Root()) {
209       useExisting = true;
210       aChild = _lastCleared;
211       _lastCleared.Nullify();
212     }
213   }
214   if (!useExisting) {
215     // create new label
216     aChild = TDF_TagSource::NewChild(aDoc->Main());
217   }
218
219   Handle(GEOM_Object) anObject = new GEOM_Object(aChild, theType);
220
221   //Put an object in the map of created objects
222   TCollection_AsciiString anID = BuildIDFromObject(anObject);
223   if(_objects.IsBound(anID)) _objects.UnBind(anID);
224   _objects.Bind(anID, anObject);
225
226   return anObject;
227 }
228
229 //=============================================================================
230 /*!
231  *  AddSubShape
232  */
233 //=============================================================================
234 Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape, 
235                                              Handle(TColStd_HArray1OfInteger) theIndices,
236                                              bool isStandaloneOperation)
237 {
238   if(theMainShape.IsNull() || theIndices.IsNull()) return NULL;
239
240   Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID());
241   Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
242
243   // NPAL18604: use existing label to decrease memory usage,
244   //            if this label has been freed (object deleted)
245   bool useExisting = false;
246   TDF_Label aChild;
247   if (!_lastCleared.IsNull()) {
248     if (_lastCleared.Root() == aDoc->Main().Root()) {
249       useExisting = true;
250       aChild = _lastCleared;
251       _lastCleared.Nullify();
252     }
253   }
254   if (!useExisting) {
255     // create new label
256     aChild = TDF_TagSource::NewChild(aDoc->Main());
257   }
258
259   Handle(GEOM_Function) aMainShape = theMainShape->GetLastFunction();
260   Handle(GEOM_Object) anObject = new GEOM_Object(aChild, 28); //28 is SUBSHAPE type
261   Handle(GEOM_Function) aFunction = anObject->AddFunction(GEOM_Object::GetSubShapeID(), 1);
262
263   GEOM_ISubShape aSSI(aFunction);
264   aSSI.SetMainShape(aMainShape);
265   aSSI.SetIndices(theIndices);
266
267   try {
268 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
269     OCC_CATCH_SIGNALS;
270 #endif
271     GEOM_Solver aSolver (GEOM_Engine::GetEngine());
272     if (!aSolver.ComputeFunction(aFunction)) {
273       MESSAGE("GEOM_Engine::AddSubShape Error: Can't build a sub shape");
274       return NULL;
275     }
276   }
277   catch (Standard_Failure) {
278     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
279     MESSAGE("GEOM_Engine::AddSubShape Error: " << aFail->GetMessageString());
280     return NULL;
281   }
282
283   //Put an object in the map of created objects
284   TCollection_AsciiString anID = BuildIDFromObject(anObject);
285   if(_objects.IsBound(anID)) _objects.UnBind(anID);
286   _objects.Bind(anID, anObject);
287
288   GEOM::TPythonDump pd (aFunction);
289  
290   if (isStandaloneOperation) {
291     pd << anObject << " = geompy.GetSubShape(" << theMainShape << ", [";
292     Standard_Integer i = theIndices->Lower(), up = theIndices->Upper();
293     for (; i <= up - 1; i++) {
294       pd << theIndices->Value(i) << ", ";
295     }
296     pd << theIndices->Value(up) << "])";
297   }
298   else
299     pd << "None";
300
301   return anObject;
302 }
303
304 //=============================================================================
305 /*!
306  *  RemoveObject
307  */
308 //=============================================================================
309 bool GEOM_Engine::RemoveObject(Handle(GEOM_Object) theObject)
310 {
311   if (!theObject) return false;
312
313   //Remove an object from the map of available objects
314   TCollection_AsciiString anID = BuildIDFromObject(theObject);
315   if (_objects.IsBound(anID)) _objects.UnBind(anID);
316
317   int nb = theObject->GetNbFunctions();
318   Handle(TDataStd_TreeNode) aNode;
319   for (int i = 1; i<=nb; i++) {
320     Handle(GEOM_Function) aFunction = theObject->GetFunction(i);
321     if (aFunction->GetEntry().FindAttribute(GEOM_Function::GetFunctionTreeID(), aNode)) 
322       aNode->Remove();
323   }
324
325   TDF_Label aLabel = theObject->GetEntry();
326   aLabel.ForgetAllAttributes(Standard_True);
327   _lastCleared = aLabel;
328
329   theObject.Nullify();
330
331   return true;
332 }
333
334 //=============================================================================
335 /*!
336  *  Undo
337  */
338 //=============================================================================
339 void GEOM_Engine::Undo(int theDocID)
340 {
341   GetDocument(theDocID)->Undo();
342 }
343
344 //=============================================================================
345 /*!
346  *  Redo
347  */
348 //=============================================================================
349 void GEOM_Engine::Redo(int theDocID)
350 {
351   GetDocument(theDocID)->Redo();
352 }
353
354 //=============================================================================
355 /*!
356  *  Save
357  */
358 //=============================================================================
359 bool GEOM_Engine::Save(int theDocID, char* theFileName)
360 {
361   if(!_mapIDDocument.IsBound(theDocID)) return false;
362   Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
363
364   _OCAFApp->SaveAs(aDoc, theFileName);
365
366   return true;
367 }
368
369 //=============================================================================
370 /*!
371  *  Load
372  */
373 //=============================================================================
374 bool GEOM_Engine::Load(int theDocID, char* theFileName)
375 {
376   Handle(TDocStd_Document) aDoc;
377   if(_OCAFApp->Open(theFileName, aDoc) != CDF_RS_OK) {
378     return false;
379   }
380
381   aDoc->SetUndoLimit(_UndoLimit);
382
383   if(_mapIDDocument.IsBound(theDocID)) _mapIDDocument.UnBind(theDocID);
384   _mapIDDocument.Bind(theDocID, aDoc);
385
386   TDataStd_Integer::Set(aDoc->Main(), theDocID);
387
388   return true;
389 }
390
391 //=============================================================================
392 /*!
393  *  Close
394  */
395 //=============================================================================
396 void GEOM_Engine::Close(int theDocID)
397 {
398   if (_mapIDDocument.IsBound(theDocID)) {
399     Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
400
401     //Remove all GEOM Objects associated to the given document
402     TColStd_SequenceOfAsciiString aSeq;
403     GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It (_objects);
404     for (; It.More(); It.Next()) {
405       TCollection_AsciiString anObjID (It.Key());
406       Standard_Integer anID = ExtractDocID(anObjID);
407       if (theDocID == anID) aSeq.Append(It.Key());
408     }
409     for (Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
410
411     _lastCleared.Nullify();
412
413     _mapIDDocument.UnBind(theDocID);
414     _OCAFApp->Close(aDoc);
415     aDoc.Nullify();
416   }
417 }
418
419 //=============================================================================
420 /*!
421  *  DumpPython
422  */
423 //=============================================================================
424 TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID, 
425                                                 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
426                                                 bool isPublished, 
427                                                 bool& aValidScript)
428 {
429   TCollection_AsciiString aScript;
430   Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
431   
432   if(aDoc.IsNull()) return TCollection_AsciiString("def RebuildData(theStudy): pass\n");
433  
434   aScript = "import geompy\n";
435   aScript += "import math\n";
436   aScript += "import SALOMEDS\n\n";
437   aScript += "def RebuildData(theStudy):";
438   aScript += "\n\tgeompy.init_geom(theStudy)";
439   
440   Standard_Integer posToInertGlobalVars = aScript.Length() + 1;
441
442   Handle(TDataStd_TreeNode) aNode, aRoot;
443   Handle(GEOM_Function) aFunction;
444   TColStd_MapOfTransient aMap;
445
446   if(aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
447     TDataStd_ChildNodeIterator Itr(aRoot);
448     for(; Itr.More(); Itr.Next()) {
449       aNode = Itr.Value();
450       aFunction = GEOM_Function::GetFunction(aNode->Label());
451       if(aFunction.IsNull()) {
452         MESSAGE ( "Null function !!!!" );
453         continue;
454       }
455       ProcessFunction(aFunction, aScript, aMap);
456     }
457   }
458
459   Resource_DataMapOfAsciiStringAsciiString aEntry2StEntry, aStEntry2Entry;
460   Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString anEntryToNameIt;
461   // build maps entry <-> studyEntry
462   for (anEntryToNameIt.Initialize( theObjectNames );
463        anEntryToNameIt.More();
464        anEntryToNameIt.Next())
465   {
466     const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
467     // look for an object by entry
468     TDF_Label L;
469     TDF_Tool::Label( aDoc->GetData(), aEntry, L );
470     if ( L.IsNull() ) continue;
471     Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
472     // fill maps
473     if ( !obj.IsNull() ) {
474       TCollection_AsciiString aStudyEntry (obj->GetAuxData());
475       aEntry2StEntry.Bind( aEntry,  aStudyEntry);
476       aStEntry2Entry.Bind( aStudyEntry, aEntry );
477     }
478   }
479
480   Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
481   Standard_Integer aLen = aSeq->Length(), objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
482   Resource_DataMapOfAsciiStringAsciiString aNameToEntry, anEntryToBadName;
483
484   //Replace entries by the names
485   TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("geomObj_"),
486     allowedChars ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
487   if(aLen == 0) anUpdatedScript = aScript;
488
489   for(Standard_Integer i = 1; i <= aLen; i+=2) {
490     anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i)-1);
491     anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i+1));
492     if(theObjectNames.IsBound(anEntry)) {
493       aName = theObjectNames.Find(anEntry);
494       // check validity of aName
495       bool isValidName = true;
496       if ( aName.IsIntegerValue() ) { // aName must not start with a digit
497         aName.Insert( 1, 'a' );
498         isValidName = false;
499       }
500       int p, p2=1; // replace not allowed chars
501       while ((p = aName.FirstLocationNotInSet(allowedChars, p2, aName.Length()))) {
502         aName.SetValue(p, '_');
503         p2=p;
504         isValidName = false;
505       }
506       if ( aNameToEntry.IsBound( aName ) && anEntry != aNameToEntry( aName ))
507       {  // diff objects have same name - make a new name by appending a digit
508         TCollection_AsciiString aName2;
509         Standard_Integer i = 0;
510         do {
511           aName2 = aName + "_" + ++i;
512         } while ( aNameToEntry.IsBound( aName2 ) && anEntry != aNameToEntry( aName2 ));
513         aName = aName2;
514         isValidName = false;
515       }
516       if ( !isValidName ) {
517         if ( isPublished )
518           anEntryToBadName.Bind( anEntry, theObjectNames.Find(anEntry) );
519         theObjectNames( anEntry ) = aName;
520       }
521     }
522     else {
523       do {
524         aName = aBaseName + TCollection_AsciiString(++objectCounter);
525       } while(aNameToEntry.IsBound(aName));
526       theObjectNames.Bind(anEntry, aName);
527     }
528     aNameToEntry.Bind(aName, anEntry); // to detect same name of diff objects
529
530     anUpdatedScript += aName;
531     aStart = aSeq->Value(i+1) + 1;
532   }
533
534   //Add final part of the script
535   if(aLen && aSeq->Value(aLen) < aScriptLength)  anUpdatedScript += aScript.SubString(aSeq->Value(aLen)+1, aScriptLength); // mkr : IPAL11865
536  
537   // ouv : NPAL12872
538   for (anEntryToNameIt.Initialize( theObjectNames );
539        anEntryToNameIt.More();
540        anEntryToNameIt.Next())
541   {
542     const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
543     const TCollection_AsciiString& aName = anEntryToNameIt.Value();
544
545     TDF_Label L;
546     TDF_Tool::Label( aDoc->GetData(), aEntry, L );
547     if ( L.IsNull() )
548       continue;
549
550     Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
551     if ( obj.IsNull() )
552       continue;
553
554     bool anAutoColor = obj->GetAutoColor();
555     if ( anAutoColor )
556     {
557       TCollection_AsciiString aCommand( "\n\t" );
558       aCommand += aName + ".SetAutoColor(1)";
559       anUpdatedScript += aCommand.ToCString();
560     }
561
562     SALOMEDS::Color aColor = obj->GetColor();
563     if ( aColor.R > 0 || aColor.G > 0 || aColor.B > 0 )
564     {
565       TCollection_AsciiString aCommand( "\n\t" );
566       aCommand += aName + ".SetColor(SALOMEDS.Color(" + aColor.R + "," + aColor.G + "," + aColor.B + "))";
567       anUpdatedScript += aCommand.ToCString();
568     }
569   }
570
571   // Make script to publish in study
572   if ( isPublished )
573   {
574     std::map< int, std::string > anEntryToCommandMap; // sort publishing commands by object entry
575     for (anEntryToNameIt.Initialize( theObjectNames );
576          anEntryToNameIt.More();
577          anEntryToNameIt.Next())
578     {
579       const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
580       const TCollection_AsciiString& aName = anEntryToNameIt.Value();
581       if ( !aEntry2StEntry.IsBound( aEntry ))
582         continue; // was not published
583       TCollection_AsciiString aCommand("\n\tgeompy."), aFatherEntry;
584
585       // find a father entry
586       const TCollection_AsciiString& aStudyEntry = aEntry2StEntry( aEntry );
587       TCollection_AsciiString aFatherStudyEntry =
588         aStudyEntry.SubString( 1, aStudyEntry.SearchFromEnd(":") - 1 );
589       if ( aStEntry2Entry.IsBound( aFatherStudyEntry ))
590         aFatherEntry = aStEntry2Entry( aFatherStudyEntry );
591
592       // make a command
593       if ( !aFatherEntry.IsEmpty() && theObjectNames.IsBound( aFatherEntry )) {
594         aCommand += "addToStudyInFather( ";
595         aCommand += theObjectNames( aFatherEntry ) + ", ";
596       }
597       else
598         aCommand += "addToStudy( ";
599       if ( anEntryToBadName.IsBound( aEntry ))
600         aCommand += aName + ", \"" + anEntryToBadName( aEntry ) + "\" )";
601       else 
602         aCommand += aName + ", \"" + aName + "\" )";
603
604       // bind a command to the last digit of the entry
605       int tag =
606         aEntry.SubString( aEntry.SearchFromEnd(":")+1, aEntry.Length() ).IntegerValue();
607       anEntryToCommandMap.insert( std::make_pair( tag, aCommand.ToCString() ));
608     }
609
610     // add publishing commands to the script
611     std::map< int, std::string >::iterator anEntryToCommand = anEntryToCommandMap.begin();
612     for ( ; anEntryToCommand != anEntryToCommandMap.end(); ++anEntryToCommand ) {
613       anUpdatedScript += (char*)anEntryToCommand->second.c_str();
614     }
615   }
616
617   anUpdatedScript += "\n\tpass\n";
618   aValidScript = true;
619
620   // fill _studyEntry2NameMap and build globalVars
621   TCollection_AsciiString globalVars;
622   _studyEntry2NameMap.Clear();
623   Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString aStEntryToEntryIt;
624   for (aStEntryToEntryIt.Initialize( aStEntry2Entry );
625        aStEntryToEntryIt.More();
626        aStEntryToEntryIt.Next() )
627   {
628     const TCollection_AsciiString & name = theObjectNames( aStEntryToEntryIt.Value() );
629     _studyEntry2NameMap.Bind (aStEntryToEntryIt.Key(), name );
630     if ( !globalVars.IsEmpty() )
631       globalVars += ", ";
632     globalVars += name;
633   }
634   if ( !globalVars.IsEmpty() ) {
635     globalVars.Insert( 1, "\n\tglobal " );
636     anUpdatedScript.Insert( posToInertGlobalVars, globalVars );
637   }
638   
639   return anUpdatedScript;
640 }
641
642 //=======================================================================
643 //function : GetDumpName
644 //purpose  : 
645 //=======================================================================
646
647 const char* GEOM_Engine::GetDumpName (const char* theStudyEntry) const
648 {
649   if ( _studyEntry2NameMap.IsBound( (char*)theStudyEntry ))
650     return _studyEntry2NameMap( (char*)theStudyEntry ).ToCString();
651
652   return NULL;
653 }
654
655 //=======================================================================
656 //function : GetAllDumpNames
657 //purpose  : 
658 //=======================================================================
659
660 Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
661 {
662   Handle(TColStd_HSequenceOfAsciiString) aRetSeq = new TColStd_HSequenceOfAsciiString;
663
664   Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString it (_studyEntry2NameMap);
665   for (; it.More(); it.Next()) {
666     aRetSeq->Append(it.Value());
667   }
668
669   return aRetSeq;
670 }
671
672
673 //===========================================================================
674 //                     Internal functions
675 //===========================================================================
676 void ProcessFunction(Handle(GEOM_Function)& theFunction, 
677                      TCollection_AsciiString& theScript,
678                      TColStd_MapOfTransient& theProcessed)
679 {
680   if(theFunction.IsNull() || theProcessed.Contains(theFunction)) return;
681
682 /*
683   TDF_LabelSequence aSeq;
684   theFunction->GetDependency(aSeq);
685   Standard_Integer aLen = aSeq.Length();
686   for(Standard_Integer i = 1; i<= aLen; i++) {
687     Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aSeq.Value(i));
688     if(aFunction.IsNull()) continue;
689     ProcessFunction(aFunction, theScript, theProcessed);
690   }
691 */
692
693   TCollection_AsciiString aDescr = theFunction->GetDescription();
694   if(aDescr.Length() == 0) {
695     //cout << "Warning: the function has no description" << endl;
696     return;
697   }
698   //Check if its internal function which doesn't requires dumping
699   if(aDescr == "None") return;
700
701   theScript += "\n\t";
702   theScript += aDescr;
703  
704   theProcessed.Add(theFunction);
705   return;
706 }
707
708 //=============================================================================
709 /*!
710  *  FindEntries: Returns a sequence of start/end positions of entries in the string
711  */
712 //=============================================================================
713 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString)
714 {
715   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
716   Standard_Integer aLen = theString.Length();
717   Standard_Boolean isFound = Standard_False;
718
719   const char* arr = theString.ToCString();
720   Standard_Integer i = 0, j;
721
722   while(i < aLen) {
723     int c = (int)arr[i];
724     j = i+1;
725     if(c >= 48 && c <= 57) { //Is digit?
726  
727       isFound = Standard_False;
728       while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
729         c = (int)arr[j++];  
730         if(c == 58) isFound = Standard_True;
731       }
732       
733       if(isFound && arr[j-2] != 58) { // last char should be a diggit
734         aSeq->Append(i+1); // +1 because AsciiString starts from 1
735         aSeq->Append(j-1);
736       }
737     }
738      
739     i = j;
740   }
741
742   return aSeq;
743 }