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);
317 //=============================================================================
321 //=============================================================================
322 void GEOM_Engine::Close(int theDocID)
324 if(_mapIDDocument.IsBound(theDocID)) {
325 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
327 //Remove all GEOM Objects associated to the given document
328 TColStd_SequenceOfAsciiString aSeq;
329 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It(_objects);
330 for(; It.More(); It.Next()) {
331 TCollection_AsciiString anObjID(It.Key());
332 Standard_Integer anID = ExtractDocID(anObjID);
333 if(theDocID == anID) aSeq.Append(It.Key());
335 for(Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
337 _mapIDDocument.UnBind(theDocID);
338 _OCAFApp->Close(aDoc);
343 //=============================================================================
347 //=============================================================================
348 TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
349 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
353 TCollection_AsciiString aScript;
354 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
356 if(aDoc.IsNull()) return TCollection_AsciiString("def RebuildData(theStudy): pass\n");
358 aScript = "import geompy\n";
359 aScript += "import math\n\n";
360 aScript += "def RebuildData(theStudy):";
361 aScript += "\n\tgeompy.init_geom(theStudy)";
363 Standard_Integer posToInertGlobalVars = aScript.Length() + 1;
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 Resource_DataMapOfAsciiStringAsciiString aEntry2StEntry, aStEntry2Entry;
383 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString anEntryToNameIt;
384 // build maps entry <-> studyEntry
385 for (anEntryToNameIt.Initialize( theObjectNames );
386 anEntryToNameIt.More();
387 anEntryToNameIt.Next())
389 const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
390 // look for an object by entry
392 TDF_Tool::Label( aDoc->GetData(), aEntry, L );
393 if ( L.IsNull() ) continue;
394 Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
396 if ( !obj.IsNull() ) {
397 TCollection_AsciiString aStudyEntry (obj->GetAuxData());
398 aEntry2StEntry.Bind( aEntry, aStudyEntry);
399 aStEntry2Entry.Bind( aStudyEntry, aEntry );
403 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
404 Standard_Integer aLen = aSeq->Length(), objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
405 Resource_DataMapOfAsciiStringAsciiString aNameToEntry, anEntryToBadName;
407 //Replace entries by the names
408 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("geomObj_"),
409 allowedChars ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
410 if(aLen == 0) anUpdatedScript = aScript;
412 for(Standard_Integer i = 1; i <= aLen; i+=2) {
413 anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i)-1);
414 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i+1));
415 if(theObjectNames.IsBound(anEntry)) {
416 aName = theObjectNames.Find(anEntry);
417 // check validity of aName
418 bool isValidName = true;
419 if ( aName.IsIntegerValue() ) { // aName must not start with a digit
420 aName.Insert( 1, 'a' );
423 int p, p2=1; // replace not allowed chars
424 while ((p = aName.FirstLocationNotInSet(allowedChars, p2, aName.Length()))) {
425 aName.SetValue(p, '_');
429 if ( aNameToEntry.IsBound( aName ) && anEntry != aNameToEntry( aName ))
430 { // diff objects have same name - make a new name by appending a digit
431 TCollection_AsciiString aName2;
432 Standard_Integer i = 0;
434 aName2 = aName + "_" + ++i;
435 } while ( aNameToEntry.IsBound( aName2 ) && anEntry != aNameToEntry( aName2 ));
439 if ( !isValidName ) {
441 anEntryToBadName.Bind( anEntry, theObjectNames.Find(anEntry) );
442 theObjectNames( anEntry ) = aName;
447 aName = aBaseName + TCollection_AsciiString(++objectCounter);
448 } while(aNameToEntry.IsBound(aName));
449 theObjectNames.Bind(anEntry, aName);
451 aNameToEntry.Bind(aName, anEntry); // to detect same name of diff objects
453 anUpdatedScript += aName;
454 aStart = aSeq->Value(i+1) + 1;
457 //Add final part of the script
458 if(aSeq->Value(aLen) < aScriptLength) anUpdatedScript += aScript.SubString(aSeq->Value(aLen)+1, aScriptLength);
460 // Make script to publish in study
463 map< int, string > anEntryToCommandMap; // sort publishing commands by object entry
464 for (anEntryToNameIt.Initialize( theObjectNames );
465 anEntryToNameIt.More();
466 anEntryToNameIt.Next())
468 const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
469 const TCollection_AsciiString& aName = anEntryToNameIt.Value();
470 if ( !aEntry2StEntry.IsBound( aEntry ))
471 continue; // was not published
472 TCollection_AsciiString aCommand("\n\tgeompy."), aFatherEntry;
474 // find a father entry
475 const TCollection_AsciiString& aStudyEntry = aEntry2StEntry( aEntry );
476 TCollection_AsciiString aFatherStudyEntry =
477 aStudyEntry.SubString( 1, aStudyEntry.SearchFromEnd(":") - 1 );
478 if ( aStEntry2Entry.IsBound( aFatherStudyEntry ))
479 aFatherEntry = aStEntry2Entry( aFatherStudyEntry );
482 if ( !aFatherEntry.IsEmpty() && theObjectNames.IsBound( aFatherEntry )) {
483 aCommand += "addToStudyInFather( ";
484 aCommand += theObjectNames( aFatherEntry ) + ", ";
487 aCommand += "addToStudy( ";
488 if ( anEntryToBadName.IsBound( aEntry ))
489 aCommand += aName + ", \"" + anEntryToBadName( aEntry ) + "\" )";
491 aCommand += aName + ", \"" + aName + "\" )";
493 // bind a command to the last digit of the entry
495 aEntry.SubString( aEntry.SearchFromEnd(":")+1, aEntry.Length() ).IntegerValue();
496 anEntryToCommandMap.insert( make_pair( tag, aCommand.ToCString() ));
499 // add publishing commands to the script
500 map< int, string >::iterator anEntryToCommand = anEntryToCommandMap.begin();
501 for ( ; anEntryToCommand != anEntryToCommandMap.end(); ++anEntryToCommand ) {
502 anUpdatedScript += (char*)anEntryToCommand->second.c_str();
506 anUpdatedScript += "\n\tpass\n";
509 // fill _studyEntry2NameMap and build globalVars
510 TCollection_AsciiString globalVars;
511 _studyEntry2NameMap.Clear();
512 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString aStEntryToEntryIt;
513 for (aStEntryToEntryIt.Initialize( aStEntry2Entry );
514 aStEntryToEntryIt.More();
515 aStEntryToEntryIt.Next() )
517 const TCollection_AsciiString & name = theObjectNames( aStEntryToEntryIt.Value() );
518 _studyEntry2NameMap.Bind (aStEntryToEntryIt.Key(), name );
519 if ( !globalVars.IsEmpty() )
523 if ( !globalVars.IsEmpty() ) {
524 globalVars.Insert( 1, "\n\tglobal " );
525 anUpdatedScript.Insert( posToInertGlobalVars, globalVars );
528 return anUpdatedScript;
531 //=======================================================================
532 //function : GetDumpName
534 //=======================================================================
536 const char* GEOM_Engine::GetDumpName (const char* theStudyEntry) const
538 if ( _studyEntry2NameMap.IsBound( (char*)theStudyEntry ))
539 return _studyEntry2NameMap( (char*)theStudyEntry ).ToCString();
544 //=======================================================================
545 //function : GetAllDumpNames
547 //=======================================================================
549 Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
551 Handle(TColStd_HSequenceOfAsciiString) aRetSeq = new TColStd_HSequenceOfAsciiString;
553 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString it (_studyEntry2NameMap);
554 for (; it.More(); it.Next()) {
555 aRetSeq->Append(it.Value());
562 //===========================================================================
563 // Internal functions
564 //===========================================================================
565 void ProcessFunction(Handle(GEOM_Function)& theFunction,
566 TCollection_AsciiString& theScript,
567 TColStd_MapOfTransient& theProcessed)
569 if(theFunction.IsNull() || theProcessed.Contains(theFunction)) return;
572 TDF_LabelSequence aSeq;
573 theFunction->GetDependency(aSeq);
574 Standard_Integer aLen = aSeq.Length();
575 for(Standard_Integer i = 1; i<= aLen; i++) {
576 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aSeq.Value(i));
577 if(aFunction.IsNull()) continue;
578 ProcessFunction(aFunction, theScript, theProcessed);
582 TCollection_AsciiString aDescr = theFunction->GetDescription();
583 if(aDescr.Length() == 0) {
584 //cout << "Warning: the function has no description" << endl;
587 //Check if its internal function which doesn't requires dumping
588 if(aDescr == "None") return;
593 theProcessed.Add(theFunction);
597 //=============================================================================
599 * FindEntries: Returns a sequence of start/end positions of entries in the string
601 //=============================================================================
602 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString)
604 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
605 Standard_Integer aLen = theString.Length();
606 Standard_Boolean isFound = Standard_False;
608 char* arr = theString.ToCString();
609 Standard_Integer i = 0, j;
614 if(c >= 48 && c <= 57) { //Is digit?
616 isFound = Standard_False;
617 while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
619 if(c == 58) isFound = Standard_True;
622 if(isFound && arr[j-2] != 58) { // last char should be a diggit
623 aSeq->Append(i+1); // +1 because AsciiString starts from 1