1 #include "GEOM_Engine.hxx"
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"
11 #include <Interface_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
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>
22 #include <TopTools_IndexedMapOfShape.hxx>
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>
33 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
35 static GEOM_Engine* TheEngine = NULL;
37 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_Object)& theObject)
39 TCollection_AsciiString anID(theObject->GetDocID()), anEntry;
40 TDF_Tool::Entry(theObject->GetEntry(), anEntry);
41 anID+=(TCollection_AsciiString("_")+anEntry);
45 static TCollection_AsciiString BuildID(Standard_Integer theDocID, char* theEntry)
47 TCollection_AsciiString anID(theDocID);
48 anID+=(TCollection_AsciiString("_")+theEntry);
52 static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
54 TCollection_AsciiString aDocID = theID.Token("_");
55 if(aDocID.Length() < 1) return -1;
56 return aDocID.IntegerValue();
59 void ProcessFunction(Handle(GEOM_Function)& theFunction,
60 TCollection_AsciiString& theScript,
61 TColStd_MapOfTransient& theProcessed);
63 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString);
65 //=============================================================================
69 //=============================================================================
70 GEOM_Engine* GEOM_Engine::GetEngine() { return TheEngine; }
73 //=============================================================================
77 //=============================================================================
78 void GEOM_Engine::SetEngine(GEOM_Engine* theEngine) { TheEngine = theEngine; }
80 //=============================================================================
84 //=============================================================================
85 GEOM_Engine::GEOM_Engine()
87 TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver());
89 _OCAFApp = new GEOM_Application();
93 //=============================================================================
97 //=============================================================================
98 Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID)
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);
108 return Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
111 //=============================================================================
115 //=============================================================================
116 int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument)
118 if(theDocument.IsNull()) return -1;
119 for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
120 if(anItr.Value() == theDocument) return anItr.Key();
126 //=============================================================================
130 //=============================================================================
131 Handle(GEOM_Object) GEOM_Engine::GetObject(int theDocID, char* theEntry)
133 TCollection_AsciiString anID = BuildID(theDocID, theEntry);
134 if(_objects.IsBound(anID)) return Handle(GEOM_Object)::DownCast(_objects(anID));
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);
141 _objects.Bind(anID, anObject);
146 //=============================================================================
150 //=============================================================================
151 Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
153 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
154 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
156 TDF_Label aChild = TDF_TagSource::NewChild(aDoc->Main());
157 Handle(GEOM_Object) anObject = new GEOM_Object(aChild, theType);
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);
167 //=============================================================================
171 //=============================================================================
172 Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape,
173 Handle(TColStd_HArray1OfInteger) theIndices,
174 bool isStandaloneOperation)
176 if(theMainShape.IsNull() || theIndices.IsNull()) return NULL;
178 Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID());
179 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
181 TDF_Label aChild = TDF_TagSource::NewChild(aDoc->Main());
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);
187 GEOM_ISubShape aSSI(aFunction);
188 aSSI.SetMainShape(aMainShape);
189 aSSI.SetIndices(theIndices);
192 GEOM_Solver aSolver (GEOM_Engine::GetEngine());
193 if (!aSolver.ComputeFunction(aFunction)) {
194 MESSAGE("GEOM_Engine::AddSubShape Error: Can't build a sub shape");
198 catch (Standard_Failure) {
199 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
200 MESSAGE("GEOM_Engine::AddSubShape Error: " << aFail->GetMessageString());
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);
209 TCollection_AsciiString aDescr("");
211 if(isStandaloneOperation) {
212 TCollection_AsciiString anEntry;
213 TDF_Tool::Entry(anObject->GetEntry(), anEntry);
215 aDescr += " = geom.AddSubShape(";
216 TDF_Tool::Entry(theMainShape->GetEntry(), anEntry);
217 aDescr += (anEntry+", ");
219 for(Standard_Integer i=theIndices->Lower(); i<=theIndices->Upper(); i++) {
220 aDescr += (TCollection_AsciiString(theIndices->Value(i))+", ");
222 aDescr.Trunc(aDescr.Length()-1);
226 TCollection_AsciiString aDescr("None");
228 aFunction->SetDescription(aDescr);
233 //=============================================================================
237 //=============================================================================
238 bool GEOM_Engine::RemoveObject(Handle(GEOM_Object) theObject)
240 if(!theObject) return false;
242 //Remove an object from the map of available objects
243 TCollection_AsciiString anID = BuildIDFromObject(theObject);
244 if(_objects.IsBound(anID)) _objects.UnBind(anID);
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))
254 TDF_Label aLabel = theObject->GetEntry();
255 aLabel.ForgetAllAttributes(Standard_True);
262 //=============================================================================
266 //=============================================================================
267 void GEOM_Engine::Undo(int theDocID)
269 GetDocument(theDocID)->Undo();
272 //=============================================================================
276 //=============================================================================
277 void GEOM_Engine::Redo(int theDocID)
279 GetDocument(theDocID)->Redo();
282 //=============================================================================
286 //=============================================================================
287 bool GEOM_Engine::Save(int theDocID, char* theFileName)
289 if(!_mapIDDocument.IsBound(theDocID)) return false;
290 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
292 _OCAFApp->SaveAs(aDoc, theFileName);
297 //=============================================================================
301 //=============================================================================
302 bool GEOM_Engine::Load(int theDocID, char* theFileName)
304 Handle(TDocStd_Document) aDoc;
305 if(_OCAFApp->Open(theFileName, aDoc) != CDF_RS_OK) {
309 aDoc->SetUndoLimit(_UndoLimit);
311 if(_mapIDDocument.IsBound(theDocID)) _mapIDDocument.UnBind(theDocID);
312 _mapIDDocument.Bind(theDocID, aDoc);
314 TDataStd_Integer::Set(aDoc->Main(), theDocID);
316 _OCAFApp->SaveAs(aDoc, "/dn05/salome/srn/Test.sdg");
321 //=============================================================================
325 //=============================================================================
326 void GEOM_Engine::Close(int theDocID)
328 if(_mapIDDocument.IsBound(theDocID)) {
329 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
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());
339 for(Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
341 _mapIDDocument.UnBind(theDocID);
342 _OCAFApp->Close(aDoc);
347 //=============================================================================
351 //=============================================================================
352 TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
353 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
357 TCollection_AsciiString aScript;
358 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
360 if(aDoc.IsNull()) return TCollection_AsciiString("def RebuildData(theStudy): pass\n");
362 aScript = "import geompy\n\n";
363 aScript += "def RebuildData(theStudy):";
365 Handle(TDataStd_TreeNode) aNode, aRoot;
366 Handle(GEOM_Function) aFunction;
367 TColStd_MapOfTransient aMap;
369 if(aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
370 TDataStd_ChildNodeIterator Itr(aRoot);
371 for(; Itr.More(); Itr.Next()) {
373 aFunction = GEOM_Function::GetFunction(aNode->Label());
374 if(aFunction.IsNull()) {
375 cout << "Null function !!!!" << endl;
378 ProcessFunction(aFunction, aScript, aMap);
382 aScript += "\n\tpass\n";
386 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
387 Standard_Integer aLen = aSeq->Length(), objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
388 Resource_DataMapOfAsciiStringAsciiString aNames;
390 //Replace entries by the names
391 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("geomObj_");
392 if(aLen == 0) anUpdatedScript = aScript;
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);
401 aName = aBaseName + TCollection_AsciiString(++objectCounter);
402 while(theObjectNames.IsBound(aName)) aName = aBaseName + TCollection_AsciiString(++objectCounter);
405 anUpdatedScript += aName;
406 aNames.Bind(aName, "1");
407 aStart = aSeq->Value(i+1) + 1;
410 //Add final part of the script
411 if(aSeq->Value(aLen) < aScriptLength) anUpdatedScript += aScript.SubString(aSeq->Value(aLen)+1, aScriptLength);
413 return anUpdatedScript;
417 //===========================================================================
418 // Internal functions
419 //===========================================================================
420 void ProcessFunction(Handle(GEOM_Function)& theFunction,
421 TCollection_AsciiString& theScript,
422 TColStd_MapOfTransient& theProcessed)
424 if(theFunction.IsNull() || theProcessed.Contains(theFunction)) return;
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);
437 TCollection_AsciiString aDescr = theFunction->GetDescription();
438 if(aDescr.Length() == 0) {
439 //cout << "Warning: the function has no description" << endl;
442 //Check if its internal function which doesn't requires dumping
443 if(aDescr == "None") return;
448 theProcessed.Add(theFunction);
452 //=============================================================================
454 * FindEntries: Returns a sequence of start/end positions of entries in the string
456 //=============================================================================
457 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString)
459 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
460 Standard_Integer aLen = theString.Length();
461 Standard_Boolean isFound = Standard_False;
463 char* arr = theString.ToCString();
464 Standard_Integer i = 0, j;
469 if(c >= 48 && c <= 57) { //Is digit?
471 isFound = Standard_False;
472 while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
474 if(c == 58) isFound = Standard_True;
478 aSeq->Append(i+1); // +1 because AsciiString starts from 1