Salome HOME
Dump Python
[modules/geom.git] / src / GEOM / GEOM_Engine.cxx
1 #include "GEOM_Engine.hxx"
2
3 #include "GEOM_Solver.hxx"
4 #include "GEOM_Function.hxx"
5 #include "GEOM_ISubShape.hxx"
6 #include "GEOM_SubShapeDriver.hxx"
7 #include "GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient.hxx"
8
9 #include "utilities.h"
10
11 #include <Interface_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
12
13 #include <TDF_Tool.hxx>
14 #include <TDF_Data.hxx>
15 #include <TDF_LabelSequence.hxx>
16 #include <TDataStd_Integer.hxx>
17 #include <TDataStd_ChildNodeIterator.hxx>
18 #include <TFunction_Driver.hxx>
19 #include <TFunction_DriverTable.hxx>
20
21 #include <TopExp.hxx>
22 #include <TopTools_IndexedMapOfShape.hxx>
23
24 #include <TCollection_AsciiString.hxx>
25 #include <TCollection_ExtendedString.hxx>
26 #include <TColStd_SequenceOfAsciiString.hxx>
27 #include <TColStd_SequenceOfTransient.hxx>
28 #include <TColStd_HSequenceOfTransient.hxx>
29 #include <TColStd_ListOfTransient.hxx>
30 #include <TColStd_MapOfTransient.hxx>
31 #include <TColStd_HSequenceOfInteger.hxx>
32
33 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
34
35 static GEOM_Engine* TheEngine = NULL;
36
37 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_Object)& theObject)
38 {
39   TCollection_AsciiString anID(theObject->GetDocID()), anEntry;
40   TDF_Tool::Entry(theObject->GetEntry(), anEntry);
41   anID+=(TCollection_AsciiString("_")+anEntry);
42   return anID;
43 }
44
45 static TCollection_AsciiString BuildID(Standard_Integer theDocID, char* theEntry)
46 {
47   TCollection_AsciiString anID(theDocID);
48   anID+=(TCollection_AsciiString("_")+theEntry);
49   return anID;
50 }
51
52 static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
53 {
54   TCollection_AsciiString aDocID = theID.Token("_");
55   if(aDocID.Length() < 1) return -1;
56   return aDocID.IntegerValue();
57 }
58
59 void ProcessFunction(Handle(GEOM_Function)& theFunction, 
60                      TCollection_AsciiString& theScript,
61                      TColStd_MapOfTransient& theProcessed);
62
63 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString);
64
65 //=============================================================================
66 /*!
67  *  GetEngine
68  */
69 //=============================================================================
70 GEOM_Engine* GEOM_Engine::GetEngine() { return TheEngine; }
71
72
73 //=============================================================================
74 /*!
75  *  SetEngine
76  */
77 //=============================================================================
78 void GEOM_Engine::SetEngine(GEOM_Engine* theEngine) { TheEngine = theEngine; }
79
80 //=============================================================================
81 /*!
82  *  Constructor
83  */
84 //=============================================================================
85 GEOM_Engine::GEOM_Engine()
86 {
87   TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver());
88
89   _OCAFApp = new GEOM_Application();
90   _UndoLimit = 10;
91 }
92
93 //=============================================================================
94 /*!
95  *  GetDocument
96  */
97 //=============================================================================
98 Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID)
99 {
100   Handle(TDocStd_Document) aDoc;
101   if(!_mapIDDocument.IsBound(theDocID)) {
102     _OCAFApp->NewDocument("SALOME_GEOM", aDoc);
103     aDoc->SetUndoLimit(_UndoLimit);
104     _mapIDDocument.Bind(theDocID, aDoc);
105     TDataStd_Integer::Set(aDoc->Main(), theDocID);
106   }
107
108   return Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
109 }
110
111 //=============================================================================
112 /*!
113  *  GetDocID
114  */
115 //=============================================================================
116 int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument)
117 {
118   if(theDocument.IsNull()) return -1;
119   for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
120     if(anItr.Value() == theDocument) return anItr.Key();
121
122   return -1;
123
124 }
125
126 //=============================================================================
127 /*!
128  *  GetObject
129  */
130 //=============================================================================
131 Handle(GEOM_Object) GEOM_Engine::GetObject(int theDocID, char* theEntry)
132 {
133   TCollection_AsciiString anID = BuildID(theDocID, theEntry);
134   if(_objects.IsBound(anID)) return Handle(GEOM_Object)::DownCast(_objects(anID));
135
136   TDF_Label aLabel;
137   Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
138   TDF_Tool::Label(aDoc->Main().Data(), theEntry, aLabel, Standard_True);
139   Handle(GEOM_Object) anObject = new GEOM_Object(aLabel);
140
141   _objects.Bind(anID, anObject);
142
143   return anObject;
144 }
145
146 //=============================================================================
147 /*!
148  *  AddObject
149  */
150 //=============================================================================
151 Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
152 {
153     Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
154     Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
155
156     TDF_Label aChild = TDF_TagSource::NewChild(aDoc->Main());
157     Handle(GEOM_Object) anObject = new GEOM_Object(aChild, theType);
158
159     //Put an object in the map of created objects
160     TCollection_AsciiString anID = BuildIDFromObject(anObject);
161     if(_objects.IsBound(anID)) _objects.UnBind(anID);
162     _objects.Bind(anID, anObject);
163
164     return anObject;
165 }
166
167 //=============================================================================
168 /*!
169  *  AddSubShape
170  */
171 //=============================================================================
172 Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape, 
173                                              Handle(TColStd_HArray1OfInteger) theIndices,
174                                              bool isStandaloneOperation)
175 {
176   if(theMainShape.IsNull() || theIndices.IsNull()) return NULL;
177
178   Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID());
179   Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
180
181   TDF_Label aChild = TDF_TagSource::NewChild(aDoc->Main());
182
183   Handle(GEOM_Function) aMainShape = theMainShape->GetLastFunction();
184   Handle(GEOM_Object) anObject = new GEOM_Object(aChild, 28); //28 is SUBSHAPE type
185   Handle(GEOM_Function) aFunction = anObject->AddFunction(GEOM_Object::GetSubShapeID(), 1);
186
187   GEOM_ISubShape aSSI(aFunction);
188   aSSI.SetMainShape(aMainShape);
189   aSSI.SetIndices(theIndices);
190
191   try {
192     GEOM_Solver aSolver (GEOM_Engine::GetEngine());
193     if (!aSolver.ComputeFunction(aFunction)) {
194       MESSAGE("GEOM_Engine::AddSubShape Error: Can't build a sub shape");
195       return NULL;
196     }
197   }
198   catch (Standard_Failure) {
199     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
200     MESSAGE("GEOM_Engine::AddSubShape Error: " << aFail->GetMessageString());
201     return NULL;
202   }
203
204   //Put an object in the map of created objects
205   TCollection_AsciiString anID = BuildIDFromObject(anObject);
206   if(_objects.IsBound(anID)) _objects.UnBind(anID);
207   _objects.Bind(anID, anObject);
208
209   TCollection_AsciiString aDescr("");
210  
211   if(isStandaloneOperation) {
212     TCollection_AsciiString anEntry;
213     TDF_Tool::Entry(anObject->GetEntry(), anEntry);
214     aDescr += anEntry;
215     aDescr += " = geom.AddSubShape(";
216     TDF_Tool::Entry(theMainShape->GetEntry(), anEntry);
217     aDescr += (anEntry+", ");
218     aDescr += (", [");
219     for(Standard_Integer i=theIndices->Lower(); i<=theIndices->Upper(); i++) {
220       aDescr += (TCollection_AsciiString(theIndices->Value(i))+", ");
221     }
222     aDescr.Trunc(aDescr.Length()-1);
223     aDescr += "])";  
224   }
225   else 
226     TCollection_AsciiString aDescr("None");
227  
228   aFunction->SetDescription(aDescr);
229
230   return anObject;
231 }
232
233 //=============================================================================
234 /*!
235  *  RemoveObject
236  */
237 //=============================================================================
238 bool GEOM_Engine::RemoveObject(Handle(GEOM_Object) theObject)
239 {
240   if(!theObject) return false;
241
242   //Remove an object from the map of available objects
243   TCollection_AsciiString anID = BuildIDFromObject(theObject);
244   if(_objects.IsBound(anID)) _objects.UnBind(anID);
245
246   int nb = theObject->GetNbFunctions();
247   Handle(TDataStd_TreeNode) aNode;
248   for(int i = 1; i<=nb; i++) {
249     Handle(GEOM_Function) aFunction = theObject->GetFunction(i);
250     if(aFunction->GetEntry().FindAttribute(GEOM_Function::GetFunctionTreeID(), aNode)) 
251       aNode->Remove();
252   }
253
254   TDF_Label aLabel = theObject->GetEntry();
255   aLabel.ForgetAllAttributes(Standard_True);
256
257   theObject.Nullify();
258
259   return true;
260 }
261
262 //=============================================================================
263 /*!
264  *  Undo
265  */
266 //=============================================================================
267 void GEOM_Engine::Undo(int theDocID)
268 {
269   GetDocument(theDocID)->Undo();
270 }
271
272 //=============================================================================
273 /*!
274  *  Redo
275  */
276 //=============================================================================
277 void GEOM_Engine::Redo(int theDocID)
278 {
279   GetDocument(theDocID)->Redo();
280 }
281
282 //=============================================================================
283 /*!
284  *  Save
285  */
286 //=============================================================================
287 bool GEOM_Engine::Save(int theDocID, char* theFileName)
288 {
289   if(!_mapIDDocument.IsBound(theDocID)) return false;
290   Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
291
292   _OCAFApp->SaveAs(aDoc, theFileName);
293
294   return true;
295 }
296
297 //=============================================================================
298 /*!
299  *  Load
300  */
301 //=============================================================================
302 bool GEOM_Engine::Load(int theDocID, char* theFileName)
303 {
304   Handle(TDocStd_Document) aDoc;
305   if(_OCAFApp->Open(theFileName, aDoc) != CDF_RS_OK) {
306     return false;
307   }
308
309   aDoc->SetUndoLimit(_UndoLimit);
310
311   if(_mapIDDocument.IsBound(theDocID)) _mapIDDocument.UnBind(theDocID);
312   _mapIDDocument.Bind(theDocID, aDoc);
313
314   TDataStd_Integer::Set(aDoc->Main(), theDocID);
315
316   _OCAFApp->SaveAs(aDoc, "/dn05/salome/srn/Test.sdg");
317
318   return true;
319 }
320
321 //=============================================================================
322 /*!
323  *  Close
324  */
325 //=============================================================================
326 void GEOM_Engine::Close(int theDocID)
327 {
328   if(_mapIDDocument.IsBound(theDocID)) {
329     Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
330
331     //Remove all GEOM Objects associated to the given document
332     TColStd_SequenceOfAsciiString aSeq;
333     GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It(_objects);
334     for(; It.More(); It.Next()) {
335       TCollection_AsciiString anObjID(It.Key());
336       Standard_Integer anID = ExtractDocID(anObjID);
337       if(theDocID == anID) aSeq.Append(It.Key());
338     }
339     for(Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
340
341    _mapIDDocument.UnBind(theDocID);
342     _OCAFApp->Close(aDoc);
343     aDoc.Nullify();
344   }
345 }
346
347 //=============================================================================
348 /*!
349  *  DumpPython
350  */
351 //=============================================================================
352 TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID, 
353                                                 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
354                                                 bool isPublished, 
355                                                 bool& aValidScript)
356 {
357   TCollection_AsciiString aScript;
358   Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
359   
360   if(aDoc.IsNull()) return TCollection_AsciiString("def RebuildData(theStudy): pass\n");
361  
362   aScript = "import geompy\n\n";
363   aScript += "def RebuildData(theStudy):";
364   
365   Handle(TDataStd_TreeNode) aNode, aRoot;
366   Handle(GEOM_Function) aFunction;
367   TColStd_MapOfTransient aMap;
368
369   if(aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
370     TDataStd_ChildNodeIterator Itr(aRoot);
371     for(; Itr.More(); Itr.Next()) {
372       aNode = Itr.Value();
373       aFunction = GEOM_Function::GetFunction(aNode->Label());
374       if(aFunction.IsNull()) {
375         cout << "Null function !!!!" << endl;
376         continue;
377       }
378       ProcessFunction(aFunction, aScript, aMap);
379     }
380   }
381
382   aScript += "\n\tpass\n";
383   aValidScript = true;
384
385   
386   Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
387   Standard_Integer aLen = aSeq->Length(), objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
388   Resource_DataMapOfAsciiStringAsciiString aNames;
389
390   //Replace entries by the names
391   TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("geomObj_");
392   if(aLen == 0) anUpdatedScript = aScript;
393
394   for(Standard_Integer i = 1; i <= aLen; i+=2) {
395     anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i)-1);
396     anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i+1));
397     if(theObjectNames.IsBound(anEntry)) {
398       aName = theObjectNames.Find(anEntry); 
399     }
400     else {
401       aName = aBaseName + TCollection_AsciiString(++objectCounter);
402       while(theObjectNames.IsBound(aName)) aName = aBaseName + TCollection_AsciiString(++objectCounter);
403     }
404
405     anUpdatedScript += aName;
406     aNames.Bind(aName, "1");
407     aStart = aSeq->Value(i+1) + 1;
408   }
409  
410   //Add final part of the script
411   if(aSeq->Value(aLen) < aScriptLength)  anUpdatedScript += aScript.SubString(aSeq->Value(aLen)+1, aScriptLength);
412  
413   return anUpdatedScript;
414 }
415
416
417 //===========================================================================
418 //                     Internal functions
419 //===========================================================================
420 void ProcessFunction(Handle(GEOM_Function)& theFunction, 
421                      TCollection_AsciiString& theScript,
422                      TColStd_MapOfTransient& theProcessed)
423 {
424   if(theFunction.IsNull() || theProcessed.Contains(theFunction)) return;
425
426 /*
427   TDF_LabelSequence aSeq;
428   theFunction->GetDependency(aSeq);
429   Standard_Integer aLen = aSeq.Length();
430   for(Standard_Integer i = 1; i<= aLen; i++) {
431     Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aSeq.Value(i));
432     if(aFunction.IsNull()) continue;
433     ProcessFunction(aFunction, theScript, theProcessed);
434   }
435 */
436
437   TCollection_AsciiString aDescr = theFunction->GetDescription();
438   if(aDescr.Length() == 0) {
439     //cout << "Warning: the function has no description" << endl;
440     return;
441   }
442   //Check if its internal function which doesn't requires dumping
443   if(aDescr == "None") return;
444
445   theScript += "\n\t";
446   theScript += aDescr;
447  
448   theProcessed.Add(theFunction);
449   return;
450 }
451
452 //=============================================================================
453 /*!
454  *  FindEntries: Returns a sequence of start/end positions of entries in the string
455  */
456 //=============================================================================
457 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString)
458 {
459   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
460   Standard_Integer aLen = theString.Length();
461   Standard_Boolean isFound = Standard_False;
462
463   char* arr = theString.ToCString();
464   Standard_Integer i = 0, j;
465
466   while(i < aLen) {
467     int c = (int)arr[i];
468     j = i+1;
469     if(c >= 48 && c <= 57) { //Is digit?
470  
471       isFound = Standard_False;
472       while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
473         c = (int)arr[j++];  
474         if(c == 58) isFound = Standard_True;
475       }
476       
477       if(isFound) {
478         aSeq->Append(i+1); // +1 because AsciiString starts from 1
479         aSeq->Append(j-1);
480       }
481     }
482      
483     i = j;
484   }
485
486   return aSeq;
487 }