]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM/GEOM_Engine.cxx
Salome HOME
1bd760e5ce936cc607c33775ce4cf2901328511e
[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 posToInsertGlobalVars = 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)
536     anUpdatedScript += aScript.SubString(aSeq->Value(aLen)+1, aScriptLength); // mkr : IPAL11865
537
538   // ouv : NPAL12872
539   for (anEntryToNameIt.Initialize( theObjectNames );
540        anEntryToNameIt.More();
541        anEntryToNameIt.Next())
542   {
543     const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
544     const TCollection_AsciiString& aName = anEntryToNameIt.Value();
545
546     TDF_Label L;
547     TDF_Tool::Label( aDoc->GetData(), aEntry, L );
548     if ( L.IsNull() )
549       continue;
550
551     Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
552     if ( obj.IsNull() )
553       continue;
554
555     bool anAutoColor = obj->GetAutoColor();
556     if ( anAutoColor )
557     {
558       TCollection_AsciiString aCommand( "\n\t" );
559       aCommand += aName + ".SetAutoColor(1)";
560       anUpdatedScript += aCommand.ToCString();
561     }
562
563     SALOMEDS::Color aColor = obj->GetColor();
564     if ( aColor.R > 0 || aColor.G > 0 || aColor.B > 0 )
565     {
566       TCollection_AsciiString aCommand( "\n\t" );
567       aCommand += aName + ".SetColor(SALOMEDS.Color(" + aColor.R + "," + aColor.G + "," + aColor.B + "))";
568       anUpdatedScript += aCommand.ToCString();
569     }
570   }
571
572   // Make script to publish in study
573   if ( isPublished )
574   {
575     std::map< int, std::string > anEntryToCommandMap; // sort publishing commands by object entry
576     for (anEntryToNameIt.Initialize( theObjectNames );
577          anEntryToNameIt.More();
578          anEntryToNameIt.Next())
579     {
580       const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
581       const TCollection_AsciiString& aName = anEntryToNameIt.Value();
582       if ( !aEntry2StEntry.IsBound( aEntry ))
583         continue; // was not published
584       TCollection_AsciiString aCommand("\n\tgeompy."), aFatherEntry;
585
586       // find a father entry
587       const TCollection_AsciiString& aStudyEntry = aEntry2StEntry( aEntry );
588       TCollection_AsciiString aFatherStudyEntry =
589         aStudyEntry.SubString( 1, aStudyEntry.SearchFromEnd(":") - 1 );
590       if ( aStEntry2Entry.IsBound( aFatherStudyEntry ))
591         aFatherEntry = aStEntry2Entry( aFatherStudyEntry );
592
593       // make a command
594       if ( !aFatherEntry.IsEmpty() && theObjectNames.IsBound( aFatherEntry )) {
595         aCommand += "addToStudyInFather( ";
596         aCommand += theObjectNames( aFatherEntry ) + ", ";
597       }
598       else
599         aCommand += "addToStudy( ";
600       if ( anEntryToBadName.IsBound( aEntry ))
601         aCommand += aName + ", \"" + anEntryToBadName( aEntry ) + "\" )";
602       else
603         aCommand += aName + ", \"" + aName + "\" )";
604
605       // bind a command to the last digit of the entry
606       int tag =
607         aEntry.SubString( aEntry.SearchFromEnd(":")+1, aEntry.Length() ).IntegerValue();
608       anEntryToCommandMap.insert( std::make_pair( tag, aCommand.ToCString() ));
609     }
610
611     // add publishing commands to the script
612     std::map< int, std::string >::iterator anEntryToCommand = anEntryToCommandMap.begin();
613     for ( ; anEntryToCommand != anEntryToCommandMap.end(); ++anEntryToCommand ) {
614       anUpdatedScript += (char*)anEntryToCommand->second.c_str();
615     }
616   }
617
618   anUpdatedScript += "\n\tpass\n";
619   aValidScript = true;
620
621   // fill _studyEntry2NameMap and build globalVars
622   TCollection_AsciiString globalVars;
623   _studyEntry2NameMap.Clear();
624   Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString aStEntryToEntryIt;
625   for (aStEntryToEntryIt.Initialize( aStEntry2Entry );
626        aStEntryToEntryIt.More();
627        aStEntryToEntryIt.Next() )
628   {
629     const TCollection_AsciiString & name = theObjectNames( aStEntryToEntryIt.Value() );
630     _studyEntry2NameMap.Bind (aStEntryToEntryIt.Key(), name );
631     if ( !globalVars.IsEmpty() )
632       globalVars += ", ";
633     globalVars += name;
634   }
635   if ( !globalVars.IsEmpty() ) {
636     globalVars.Insert( 1, "\n\tglobal " );
637     anUpdatedScript.Insert( posToInsertGlobalVars, globalVars );
638   }
639
640   return anUpdatedScript;
641 }
642
643 //=======================================================================
644 //function : GetDumpName
645 //purpose  :
646 //=======================================================================
647
648 const char* GEOM_Engine::GetDumpName (const char* theStudyEntry) const
649 {
650   if ( _studyEntry2NameMap.IsBound( (char*)theStudyEntry ))
651     return _studyEntry2NameMap( (char*)theStudyEntry ).ToCString();
652
653   return NULL;
654 }
655
656 //=======================================================================
657 //function : GetAllDumpNames
658 //purpose  :
659 //=======================================================================
660
661 Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
662 {
663   Handle(TColStd_HSequenceOfAsciiString) aRetSeq = new TColStd_HSequenceOfAsciiString;
664
665   Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString it (_studyEntry2NameMap);
666   for (; it.More(); it.Next()) {
667     aRetSeq->Append(it.Value());
668   }
669
670   return aRetSeq;
671 }
672
673
674 //===========================================================================
675 //                     Internal functions
676 //===========================================================================
677 void ProcessFunction(Handle(GEOM_Function)& theFunction,
678                      TCollection_AsciiString& theScript,
679                      TColStd_MapOfTransient& theProcessed)
680 {
681   if(theFunction.IsNull() || theProcessed.Contains(theFunction)) return;
682
683 /*
684   TDF_LabelSequence aSeq;
685   theFunction->GetDependency(aSeq);
686   Standard_Integer aLen = aSeq.Length();
687   for(Standard_Integer i = 1; i <= aLen; i++) {
688     Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aSeq.Value(i));
689     if(aFunction.IsNull()) continue;
690     ProcessFunction(aFunction, theScript, theProcessed);
691   }
692 */
693
694   // pass functions, that depends on nonexisting ones
695   //jfa:test//bool doComment = false;
696   TDF_LabelSequence aSeq;
697   theFunction->GetDependency(aSeq);
698   Standard_Integer aLen = aSeq.Length();
699   //jfa:test//for (Standard_Integer i = 1; i <= aLen && !doComment; i++) {
700   for (Standard_Integer i = 1; i <= aLen; i++) {
701     Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aSeq.Value(i));
702     //jfa:test//if (aFunction.IsNull()) doComment = true;
703     //jfa:test//else if (!theProcessed.Contains(aFunction)) doComment = true;
704     if (aFunction.IsNull()) return;
705     if (!theProcessed.Contains(aFunction)) return;
706   }
707
708   TCollection_AsciiString aDescr = theFunction->GetDescription();
709   if(aDescr.Length() == 0) {
710     //cout << "Warning: the function has no description" << endl;
711     return;
712   }
713
714   //Check if its internal function which doesn't requires dumping
715   if(aDescr == "None") return;
716
717   theScript += "\n\t";
718   //jfa:test//if (doComment) theScript += "#";
719   theScript += aDescr;
720
721   theProcessed.Add(theFunction);
722 }
723
724 //=============================================================================
725 /*!
726  *  FindEntries: Returns a sequence of start/end positions of entries in the string
727  */
728 //=============================================================================
729 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString)
730 {
731   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
732   Standard_Integer aLen = theString.Length();
733   Standard_Boolean isFound = Standard_False;
734
735   const char* arr = theString.ToCString();
736   Standard_Integer i = 0, j;
737
738   while(i < aLen) {
739     int c = (int)arr[i];
740     j = i+1;
741     if(c >= 48 && c <= 57) { //Is digit?
742
743       isFound = Standard_False;
744       while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
745         c = (int)arr[j++];
746         if(c == 58) isFound = Standard_True;
747       }
748
749       if(isFound && arr[j-2] != 58) { // last char should be a diggit
750         aSeq->Append(i+1); // +1 because AsciiString starts from 1
751         aSeq->Append(j-1);
752       }
753     }
754
755     i = j;
756   }
757
758   return aSeq;
759 }