2 #pragma warning( disable:4786 )
5 #include "GEOM_Engine.hxx"
7 #include "GEOM_Solver.hxx"
8 #include "GEOM_Function.hxx"
9 #include "GEOM_ISubShape.hxx"
10 #include "GEOM_SubShapeDriver.hxx"
11 #include "GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient.hxx"
12 #include "GEOM_PythonDump.hxx"
14 #include "utilities.h"
16 #include <TDF_Tool.hxx>
17 #include <TDF_Data.hxx>
18 #include <TDF_LabelSequence.hxx>
19 #include <TDataStd_Integer.hxx>
20 #include <TDataStd_ChildNodeIterator.hxx>
21 #include <TFunction_Driver.hxx>
22 #include <TFunction_DriverTable.hxx>
25 #include <TopTools_IndexedMapOfShape.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <TCollection_ExtendedString.hxx>
29 #include <TColStd_SequenceOfAsciiString.hxx>
30 #include <TColStd_MapOfTransient.hxx>
31 #include <TColStd_HSequenceOfInteger.hxx>
33 #include <Interface_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
34 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
39 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
41 static GEOM_Engine* TheEngine = NULL;
43 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_Object)& theObject)
45 TCollection_AsciiString anID(theObject->GetDocID()), anEntry;
46 TDF_Tool::Entry(theObject->GetEntry(), anEntry);
47 anID+=(TCollection_AsciiString("_")+anEntry);
51 static TCollection_AsciiString BuildID(Standard_Integer theDocID, char* theEntry)
53 TCollection_AsciiString anID(theDocID);
54 anID+=(TCollection_AsciiString("_")+theEntry);
58 static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
60 TCollection_AsciiString aDocID = theID.Token("_");
61 if(aDocID.Length() < 1) return -1;
62 return aDocID.IntegerValue();
65 void ProcessFunction(Handle(GEOM_Function)& theFunction,
66 TCollection_AsciiString& theScript,
67 TColStd_MapOfTransient& theProcessed);
69 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString);
71 //=============================================================================
75 //=============================================================================
76 GEOM_Engine* GEOM_Engine::GetEngine() { return TheEngine; }
79 //=============================================================================
83 //=============================================================================
84 void GEOM_Engine::SetEngine(GEOM_Engine* theEngine) { TheEngine = theEngine; }
86 //=============================================================================
90 //=============================================================================
91 GEOM_Engine::GEOM_Engine()
93 TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver());
95 _OCAFApp = new GEOM_Application();
99 //=============================================================================
103 //=============================================================================
104 Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID)
106 Handle(TDocStd_Document) aDoc;
107 if(!_mapIDDocument.IsBound(theDocID)) {
108 _OCAFApp->NewDocument("SALOME_GEOM", aDoc);
109 aDoc->SetUndoLimit(_UndoLimit);
110 _mapIDDocument.Bind(theDocID, aDoc);
111 TDataStd_Integer::Set(aDoc->Main(), theDocID);
114 return Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
117 //=============================================================================
121 //=============================================================================
122 int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument)
124 if(theDocument.IsNull()) return -1;
125 for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
126 if(anItr.Value() == theDocument) return anItr.Key();
132 //=============================================================================
136 //=============================================================================
137 Handle(GEOM_Object) GEOM_Engine::GetObject(int theDocID, char* theEntry)
139 TCollection_AsciiString anID = BuildID(theDocID, theEntry);
140 if(_objects.IsBound(anID)) return Handle(GEOM_Object)::DownCast(_objects(anID));
143 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
144 TDF_Tool::Label(aDoc->Main().Data(), theEntry, aLabel, Standard_True);
145 Handle(GEOM_Object) anObject = new GEOM_Object(aLabel);
147 _objects.Bind(anID, anObject);
152 //=============================================================================
156 //=============================================================================
157 Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
159 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
160 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
162 TDF_Label aChild = TDF_TagSource::NewChild(aDoc->Main());
163 Handle(GEOM_Object) anObject = new GEOM_Object(aChild, theType);
165 //Put an object in the map of created objects
166 TCollection_AsciiString anID = BuildIDFromObject(anObject);
167 if(_objects.IsBound(anID)) _objects.UnBind(anID);
168 _objects.Bind(anID, anObject);
173 //=============================================================================
177 //=============================================================================
178 Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape,
179 Handle(TColStd_HArray1OfInteger) theIndices,
180 bool isStandaloneOperation)
182 if(theMainShape.IsNull() || theIndices.IsNull()) return NULL;
184 Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID());
185 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
187 TDF_Label aChild = TDF_TagSource::NewChild(aDoc->Main());
189 Handle(GEOM_Function) aMainShape = theMainShape->GetLastFunction();
190 Handle(GEOM_Object) anObject = new GEOM_Object(aChild, 28); //28 is SUBSHAPE type
191 Handle(GEOM_Function) aFunction = anObject->AddFunction(GEOM_Object::GetSubShapeID(), 1);
193 GEOM_ISubShape aSSI(aFunction);
194 aSSI.SetMainShape(aMainShape);
195 aSSI.SetIndices(theIndices);
198 GEOM_Solver aSolver (GEOM_Engine::GetEngine());
199 if (!aSolver.ComputeFunction(aFunction)) {
200 MESSAGE("GEOM_Engine::AddSubShape Error: Can't build a sub shape");
204 catch (Standard_Failure) {
205 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
206 MESSAGE("GEOM_Engine::AddSubShape Error: " << aFail->GetMessageString());
210 //Put an object in the map of created objects
211 TCollection_AsciiString anID = BuildIDFromObject(anObject);
212 if(_objects.IsBound(anID)) _objects.UnBind(anID);
213 _objects.Bind(anID, anObject);
215 GEOM::TPythonDump pd (aFunction);
217 if (isStandaloneOperation) {
218 pd << anObject << " = geompy.GetSubShape(" << theMainShape << ", [";
219 Standard_Integer i = theIndices->Lower(), up = theIndices->Upper();
220 for (; i <= up - 1; i++) {
221 pd << theIndices->Value(i) << ", ";
223 pd << theIndices->Value(up) << "])";
231 //=============================================================================
235 //=============================================================================
236 bool GEOM_Engine::RemoveObject(Handle(GEOM_Object) theObject)
238 if(!theObject) return false;
240 //Remove an object from the map of available objects
241 TCollection_AsciiString anID = BuildIDFromObject(theObject);
242 if(_objects.IsBound(anID)) _objects.UnBind(anID);
244 int nb = theObject->GetNbFunctions();
245 Handle(TDataStd_TreeNode) aNode;
246 for(int i = 1; i<=nb; i++) {
247 Handle(GEOM_Function) aFunction = theObject->GetFunction(i);
248 if(aFunction->GetEntry().FindAttribute(GEOM_Function::GetFunctionTreeID(), aNode))
252 TDF_Label aLabel = theObject->GetEntry();
253 aLabel.ForgetAllAttributes(Standard_True);
260 //=============================================================================
264 //=============================================================================
265 void GEOM_Engine::Undo(int theDocID)
267 GetDocument(theDocID)->Undo();
270 //=============================================================================
274 //=============================================================================
275 void GEOM_Engine::Redo(int theDocID)
277 GetDocument(theDocID)->Redo();
280 //=============================================================================
284 //=============================================================================
285 bool GEOM_Engine::Save(int theDocID, char* theFileName)
287 if(!_mapIDDocument.IsBound(theDocID)) return false;
288 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
290 _OCAFApp->SaveAs(aDoc, theFileName);
295 //=============================================================================
299 //=============================================================================
300 bool GEOM_Engine::Load(int theDocID, char* theFileName)
302 Handle(TDocStd_Document) aDoc;
303 if(_OCAFApp->Open(theFileName, aDoc) != CDF_RS_OK) {
307 aDoc->SetUndoLimit(_UndoLimit);
309 if(_mapIDDocument.IsBound(theDocID)) _mapIDDocument.UnBind(theDocID);
310 _mapIDDocument.Bind(theDocID, aDoc);
312 TDataStd_Integer::Set(aDoc->Main(), theDocID);
314 _OCAFApp->SaveAs(aDoc, "/dn05/salome/srn/Test.sdg");
319 //=============================================================================
323 //=============================================================================
324 void GEOM_Engine::Close(int theDocID)
326 if(_mapIDDocument.IsBound(theDocID)) {
327 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
329 //Remove all GEOM Objects associated to the given document
330 TColStd_SequenceOfAsciiString aSeq;
331 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It(_objects);
332 for(; It.More(); It.Next()) {
333 TCollection_AsciiString anObjID(It.Key());
334 Standard_Integer anID = ExtractDocID(anObjID);
335 if(theDocID == anID) aSeq.Append(It.Key());
337 for(Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
339 _mapIDDocument.UnBind(theDocID);
340 _OCAFApp->Close(aDoc);
345 //=============================================================================
349 //=============================================================================
350 TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
351 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
355 TCollection_AsciiString aScript;
356 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
358 if(aDoc.IsNull()) return TCollection_AsciiString("def RebuildData(theStudy): pass\n");
360 aScript = "import geompy\n";
361 aScript += "import math\n\n";
362 aScript += "def RebuildData(theStudy):";
363 aScript += "\n\tgeompy.init_geom(theStudy)";
365 Standard_Integer posToInertGlobalVars = aScript.Length() + 1;
367 Handle(TDataStd_TreeNode) aNode, aRoot;
368 Handle(GEOM_Function) aFunction;
369 TColStd_MapOfTransient aMap;
371 if(aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
372 TDataStd_ChildNodeIterator Itr(aRoot);
373 for(; Itr.More(); Itr.Next()) {
375 aFunction = GEOM_Function::GetFunction(aNode->Label());
376 if(aFunction.IsNull()) {
377 cout << "Null function !!!!" << endl;
380 ProcessFunction(aFunction, aScript, aMap);
384 Resource_DataMapOfAsciiStringAsciiString aEntry2StEntry, aStEntry2Entry;
385 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString anEntryToNameIt;
386 // build maps entry <-> studyEntry
387 for (anEntryToNameIt.Initialize( theObjectNames );
388 anEntryToNameIt.More();
389 anEntryToNameIt.Next())
391 const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
392 // look for an object by entry
394 TDF_Tool::Label( aDoc->GetData(), aEntry, L );
395 if ( L.IsNull() ) continue;
396 Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
398 if ( !obj.IsNull() ) {
399 TCollection_AsciiString aStudyEntry (obj->GetAuxData());
400 aEntry2StEntry.Bind( aEntry, aStudyEntry);
401 aStEntry2Entry.Bind( aStudyEntry, aEntry );
405 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
406 Standard_Integer aLen = aSeq->Length(), objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
407 Resource_DataMapOfAsciiStringAsciiString aNameToEntry, anEntryToBadName;
409 //Replace entries by the names
410 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("geomObj_"),
411 allowedChars ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
412 if(aLen == 0) anUpdatedScript = aScript;
414 for(Standard_Integer i = 1; i <= aLen; i+=2) {
415 anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i)-1);
416 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i+1));
417 if(theObjectNames.IsBound(anEntry)) {
418 aName = theObjectNames.Find(anEntry);
419 // check validity of aName
420 bool isValidName = true;
421 if ( aName.IsIntegerValue() ) { // aName must not start with a digit
422 aName.Insert( 1, 'a' );
425 int p, p2=1; // replace not allowed chars
426 while ((p = aName.FirstLocationNotInSet(allowedChars, p2, aName.Length()))) {
427 aName.SetValue(p, '_');
431 if ( aNameToEntry.IsBound( aName ) && anEntry != aNameToEntry( aName ))
432 { // diff objects have same name - make a new name by appending a digit
433 TCollection_AsciiString aName2;
434 Standard_Integer i = 0;
436 aName2 = aName + "_" + ++i;
437 } while ( aNameToEntry.IsBound( aName2 ) && anEntry != aNameToEntry( aName2 ));
441 if ( !isValidName ) {
443 anEntryToBadName.Bind( anEntry, theObjectNames.Find(anEntry) );
444 theObjectNames( anEntry ) = aName;
449 aName = aBaseName + TCollection_AsciiString(++objectCounter);
450 } while(aNameToEntry.IsBound(aName));
451 theObjectNames.Bind(anEntry, aName);
453 aNameToEntry.Bind(aName, anEntry); // to detect same name of diff objects
455 anUpdatedScript += aName;
456 aStart = aSeq->Value(i+1) + 1;
459 //Add final part of the script
460 if(aSeq->Value(aLen) < aScriptLength) anUpdatedScript += aScript.SubString(aSeq->Value(aLen)+1, aScriptLength);
462 // Make script to publish in study
465 map< int, string > anEntryToCommandMap; // sort publishing commands by object entry
466 for (anEntryToNameIt.Initialize( theObjectNames );
467 anEntryToNameIt.More();
468 anEntryToNameIt.Next())
470 const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
471 const TCollection_AsciiString& aName = anEntryToNameIt.Value();
472 if ( !aEntry2StEntry.IsBound( aEntry ))
473 continue; // was not published
474 TCollection_AsciiString aCommand("\n\tgeompy."), aFatherEntry;
476 // find a father entry
477 const TCollection_AsciiString& aStudyEntry = aEntry2StEntry( aEntry );
478 TCollection_AsciiString aFatherStudyEntry =
479 aStudyEntry.SubString( 1, aStudyEntry.SearchFromEnd(":") - 1 );
480 if ( aStEntry2Entry.IsBound( aFatherStudyEntry ))
481 aFatherEntry = aStEntry2Entry( aFatherStudyEntry );
484 if ( !aFatherEntry.IsEmpty() && theObjectNames.IsBound( aFatherEntry )) {
485 aCommand += "addToStudyInFather( ";
486 aCommand += theObjectNames( aFatherEntry ) + ", ";
489 aCommand += "addToStudy( ";
490 if ( anEntryToBadName.IsBound( aEntry ))
491 aCommand += aName + ", \"" + anEntryToBadName( aEntry ) + "\" )";
493 aCommand += aName + ", \"" + aName + "\" )";
495 // bind a command to the last digit of the entry
497 aEntry.SubString( aEntry.SearchFromEnd(":")+1, aEntry.Length() ).IntegerValue();
498 anEntryToCommandMap.insert( make_pair( tag, aCommand.ToCString() ));
501 // add publishing commands to the script
502 map< int, string >::iterator anEntryToCommand = anEntryToCommandMap.begin();
503 for ( ; anEntryToCommand != anEntryToCommandMap.end(); ++anEntryToCommand ) {
504 anUpdatedScript += (char*)anEntryToCommand->second.c_str();
508 anUpdatedScript += "\n\tpass\n";
511 // fill _studyEntry2NameMap and build globalVars
512 TCollection_AsciiString globalVars;
513 _studyEntry2NameMap.Clear();
514 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString aStEntryToEntryIt;
515 for (aStEntryToEntryIt.Initialize( aStEntry2Entry );
516 aStEntryToEntryIt.More();
517 aStEntryToEntryIt.Next() )
519 const TCollection_AsciiString & name = theObjectNames( aStEntryToEntryIt.Value() );
520 _studyEntry2NameMap.Bind (aStEntryToEntryIt.Key(), name );
521 if ( !globalVars.IsEmpty() )
525 if ( !globalVars.IsEmpty() ) {
526 globalVars.Insert( 1, "\n\tglobal " );
527 anUpdatedScript.Insert( posToInertGlobalVars, globalVars );
530 return anUpdatedScript;
533 //=======================================================================
534 //function : GetDumpName
536 //=======================================================================
538 const char* GEOM_Engine::GetDumpName (const char* theStudyEntry) const
540 if ( _studyEntry2NameMap.IsBound( (char*)theStudyEntry ))
541 return _studyEntry2NameMap( (char*)theStudyEntry ).ToCString();
546 //=======================================================================
547 //function : GetAllDumpNames
549 //=======================================================================
551 Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
553 Handle(TColStd_HSequenceOfAsciiString) aRetSeq = new TColStd_HSequenceOfAsciiString;
555 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString it (_studyEntry2NameMap);
556 for (; it.More(); it.Next()) {
557 aRetSeq->Append(it.Value());
564 //===========================================================================
565 // Internal functions
566 //===========================================================================
567 void ProcessFunction(Handle(GEOM_Function)& theFunction,
568 TCollection_AsciiString& theScript,
569 TColStd_MapOfTransient& theProcessed)
571 if(theFunction.IsNull() || theProcessed.Contains(theFunction)) return;
574 TDF_LabelSequence aSeq;
575 theFunction->GetDependency(aSeq);
576 Standard_Integer aLen = aSeq.Length();
577 for(Standard_Integer i = 1; i<= aLen; i++) {
578 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aSeq.Value(i));
579 if(aFunction.IsNull()) continue;
580 ProcessFunction(aFunction, theScript, theProcessed);
584 TCollection_AsciiString aDescr = theFunction->GetDescription();
585 if(aDescr.Length() == 0) {
586 //cout << "Warning: the function has no description" << endl;
589 //Check if its internal function which doesn't requires dumping
590 if(aDescr == "None") return;
595 theProcessed.Add(theFunction);
599 //=============================================================================
601 * FindEntries: Returns a sequence of start/end positions of entries in the string
603 //=============================================================================
604 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString)
606 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
607 Standard_Integer aLen = theString.Length();
608 Standard_Boolean isFound = Standard_False;
610 char* arr = theString.ToCString();
611 Standard_Integer i = 0, j;
616 if(c >= 48 && c <= 57) { //Is digit?
618 isFound = Standard_False;
619 while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
621 if(c == 58) isFound = Standard_True;
624 if(isFound && arr[j-2] != 58) { // last char should be a diggit
625 aSeq->Append(i+1); // +1 because AsciiString starts from 1