1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #pragma warning( disable:4786 )
26 #include "GEOM_Engine.hxx"
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"
35 #include "utilities.h"
37 #include <TDF_Tool.hxx>
38 #include <TDF_Data.hxx>
39 #include <TDF_Reference.hxx>
40 #include <TDF_LabelSequence.hxx>
41 #include <TDataStd_Integer.hxx>
42 #include <TDataStd_ChildNodeIterator.hxx>
43 #include <TFunction_Driver.hxx>
44 #include <TFunction_DriverTable.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <TCollection_AsciiString.hxx>
50 #include <TCollection_ExtendedString.hxx>
51 #include <TColStd_SequenceOfAsciiString.hxx>
52 #include <TColStd_MapOfTransient.hxx>
53 #include <TColStd_HSequenceOfInteger.hxx>
55 #include <Interface_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
56 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
63 #include <Standard_Failure.hxx>
64 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
69 #define O_SQR_BRACKET '['
70 #define C_SQR_BRACKET ']'
71 #define PY_NULL "None"
74 static int MYDEBUG = 0;
76 static int MYDEBUG = 0;
79 static GEOM_Engine* TheEngine = NULL;
83 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_Object)& theObject)
85 TCollection_AsciiString anID(theObject->GetDocID()), anEntry;
86 TDF_Tool::Entry(theObject->GetEntry(), anEntry);
87 anID+=(TCollection_AsciiString("_")+anEntry);
91 static TCollection_AsciiString BuildID(Standard_Integer theDocID, char* theEntry)
93 TCollection_AsciiString anID(theDocID);
94 anID+=(TCollection_AsciiString("_")+theEntry);
98 static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
100 TCollection_AsciiString aDocID = theID.Token("_");
101 if(aDocID.Length() < 1) return -1;
102 return aDocID.IntegerValue();
105 void ProcessFunction(Handle(GEOM_Function)& theFunction,
106 TCollection_AsciiString& theScript,
107 const TVariablesList& theVariables,
108 TDF_LabelMap& theProcessed,
109 std::set<std::string>& theDumpedObjs);
111 void ReplaceVariables(TCollection_AsciiString& theCommand,
112 const TVariablesList& theVariables);
116 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString);
119 //=============================================================================
123 //=============================================================================
124 GEOM_Engine* GEOM_Engine::GetEngine() { return TheEngine; }
126 //=============================================================================
130 //=============================================================================
131 void GEOM_Engine::SetEngine(GEOM_Engine* theEngine) { TheEngine = theEngine; }
133 //=============================================================================
137 //=============================================================================
138 GEOM_Engine::GEOM_Engine()
140 TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver());
142 _OCAFApp = new GEOM_Application();
149 GEOM_Engine::~GEOM_Engine()
151 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It(_objects);
152 for(; It.More(); It.Next())
154 RemoveObject(Handle(GEOM_Object)::DownCast(It.Value()));
157 //Close all documents not closed
158 for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
161 _mapIDDocument.Clear();
165 //=============================================================================
169 //=============================================================================
170 Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID)
172 Handle(TDocStd_Document) aDoc;
173 if(!_mapIDDocument.IsBound(theDocID)) {
174 _OCAFApp->NewDocument("SALOME_GEOM", aDoc);
175 aDoc->SetUndoLimit(_UndoLimit);
176 _mapIDDocument.Bind(theDocID, aDoc);
177 TDataStd_Integer::Set(aDoc->Main(), theDocID);
180 return Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
183 //=============================================================================
187 //=============================================================================
188 int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument)
190 if(theDocument.IsNull()) return -1;
191 for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
192 if(anItr.Value() == theDocument) return anItr.Key();
198 //=============================================================================
202 //=============================================================================
203 Handle(GEOM_Object) GEOM_Engine::GetObject(int theDocID, char* theEntry)
205 TCollection_AsciiString anID = BuildID(theDocID, theEntry);
206 if(_objects.IsBound(anID)) return Handle(GEOM_Object)::DownCast(_objects(anID));
209 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
210 TDF_Tool::Label(aDoc->Main().Data(), theEntry, aLabel, Standard_True);
211 Handle(GEOM_Object) anObject = new GEOM_Object(aLabel);
213 _objects.Bind(anID, anObject);
218 //=============================================================================
222 //=============================================================================
223 Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
225 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
226 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
228 // NPAL18604: use existing label to decrease memory usage,
229 // if this label has been freed (object deleted)
230 bool useExisting = false;
232 if (!_lastCleared.IsNull()) {
233 if (_lastCleared.Root() == aDoc->Main().Root()) {
235 aChild = _lastCleared;
236 _lastCleared.Nullify();
241 aChild = TDF_TagSource::NewChild(aDoc->Main());
244 Handle(GEOM_Object) anObject = new GEOM_Object(aChild, theType);
246 //Put an object in the map of created objects
247 TCollection_AsciiString anID = BuildIDFromObject(anObject);
248 if(_objects.IsBound(anID)) _objects.UnBind(anID);
249 _objects.Bind(anID, anObject);
254 //=============================================================================
258 //=============================================================================
259 Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape,
260 Handle(TColStd_HArray1OfInteger) theIndices,
261 bool isStandaloneOperation)
263 if(theMainShape.IsNull() || theIndices.IsNull()) return NULL;
265 Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID());
266 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
268 // NPAL18604: use existing label to decrease memory usage,
269 // if this label has been freed (object deleted)
270 bool useExisting = false;
272 if (!_lastCleared.IsNull()) {
273 if (_lastCleared.Root() == aDoc->Main().Root()) {
275 aChild = _lastCleared;
276 _lastCleared.Nullify();
281 aChild = TDF_TagSource::NewChild(aDoc->Main());
284 Handle(GEOM_Function) aMainShape = theMainShape->GetLastFunction();
285 Handle(GEOM_Object) anObject = new GEOM_Object(aChild, 28); //28 is SUBSHAPE type
286 Handle(GEOM_Function) aFunction = anObject->AddFunction(GEOM_Object::GetSubShapeID(), 1);
288 GEOM_ISubShape aSSI(aFunction);
289 aSSI.SetMainShape(aMainShape);
290 aSSI.SetIndices(theIndices);
293 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
296 GEOM_Solver aSolver (GEOM_Engine::GetEngine());
297 if (!aSolver.ComputeFunction(aFunction)) {
298 MESSAGE("GEOM_Engine::AddSubShape Error: Can't build a sub shape");
302 catch (Standard_Failure) {
303 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
304 MESSAGE("GEOM_Engine::AddSubShape Error: " << aFail->GetMessageString());
308 //Put an object in the map of created objects
309 TCollection_AsciiString anID = BuildIDFromObject(anObject);
310 if(_objects.IsBound(anID)) _objects.UnBind(anID);
311 _objects.Bind(anID, anObject);
313 GEOM::TPythonDump pd (aFunction);
315 if (isStandaloneOperation) {
316 pd << anObject << " = geompy.GetSubShape(" << theMainShape << ", [";
317 Standard_Integer i = theIndices->Lower(), up = theIndices->Upper();
318 for (; i <= up - 1; i++) {
319 pd << theIndices->Value(i) << ", ";
321 pd << theIndices->Value(up) << "])";
329 //=============================================================================
333 //=============================================================================
334 bool GEOM_Engine::RemoveObject(Handle(GEOM_Object) theObject)
336 if (!theObject) return false;
338 //Remove an object from the map of available objects
339 TCollection_AsciiString anID = BuildIDFromObject(theObject);
340 if (_objects.IsBound(anID)) _objects.UnBind(anID);
342 int nb = theObject->GetNbFunctions();
343 Handle(TDataStd_TreeNode) aNode;
344 for (int i = 1; i<=nb; i++) {
345 Handle(GEOM_Function) aFunction = theObject->GetFunction(i);
346 if (aFunction->GetEntry().FindAttribute(GEOM_Function::GetFunctionTreeID(), aNode))
350 TDF_Label aLabel = theObject->GetEntry();
351 aLabel.ForgetAllAttributes(Standard_True);
352 _lastCleared = aLabel;
359 //=============================================================================
363 //=============================================================================
364 void GEOM_Engine::Undo(int theDocID)
366 GetDocument(theDocID)->Undo();
369 //=============================================================================
373 //=============================================================================
374 void GEOM_Engine::Redo(int theDocID)
376 GetDocument(theDocID)->Redo();
379 //=============================================================================
383 //=============================================================================
384 bool GEOM_Engine::Save(int theDocID, char* theFileName)
386 if(!_mapIDDocument.IsBound(theDocID)) return false;
387 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
389 _OCAFApp->SaveAs(aDoc, theFileName);
394 //=============================================================================
398 //=============================================================================
399 bool GEOM_Engine::Load(int theDocID, char* theFileName)
401 Handle(TDocStd_Document) aDoc;
402 if(_OCAFApp->Open(theFileName, aDoc) != CDF_RS_OK) {
406 aDoc->SetUndoLimit(_UndoLimit);
408 if(_mapIDDocument.IsBound(theDocID)) _mapIDDocument.UnBind(theDocID);
409 _mapIDDocument.Bind(theDocID, aDoc);
411 TDataStd_Integer::Set(aDoc->Main(), theDocID);
416 //=============================================================================
420 //=============================================================================
421 void GEOM_Engine::Close(int theDocID)
423 if (_mapIDDocument.IsBound(theDocID)) {
424 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
426 //Remove all GEOM Objects associated to the given document
427 TColStd_SequenceOfAsciiString aSeq;
428 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It (_objects);
429 for (; It.More(); It.Next()) {
430 TCollection_AsciiString anObjID (It.Key());
431 Standard_Integer anID = ExtractDocID(anObjID);
432 if (theDocID == anID) aSeq.Append(It.Key());
434 for (Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
436 _lastCleared.Nullify();
438 _mapIDDocument.UnBind(theDocID);
439 _OCAFApp->Close(aDoc);
444 //=============================================================================
448 //=============================================================================
449 TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
450 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
451 TVariablesList theVariables,
455 TCollection_AsciiString aScript;
456 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
458 if (aDoc.IsNull()) return TCollection_AsciiString("def RebuildData(theStudy): pass\n");
460 aScript = "import geompy\n";
461 aScript += "import math\n";
462 aScript += "import SALOMEDS\n\n";
463 aScript += "def RebuildData(theStudy):";
464 aScript += "\n\tgeompy.init_geom(theStudy)";
466 Standard_Integer posToInsertGlobalVars = aScript.Length() + 1;
468 Handle(TDataStd_TreeNode) aNode, aRoot;
469 Handle(GEOM_Function) aFunction;
470 TDF_LabelMap aCheckedFuncMap;
471 std::set<std::string> anIgnoreObjMap;
473 if (aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
474 TDataStd_ChildNodeIterator Itr(aRoot);
475 for (; Itr.More(); Itr.Next()) {
477 aFunction = GEOM_Function::GetFunction(aNode->Label());
478 if (aFunction.IsNull()) {
479 MESSAGE ( "Null function !!!!" );
482 ProcessFunction(aFunction, aScript, theVariables, aCheckedFuncMap, anIgnoreObjMap);
486 Resource_DataMapOfAsciiStringAsciiString aEntry2StEntry, aStEntry2Entry;
487 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString anEntryToNameIt;
488 // build maps entry <-> studyEntry
489 for (anEntryToNameIt.Initialize( theObjectNames );
490 anEntryToNameIt.More();
491 anEntryToNameIt.Next())
493 const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
494 // look for an object by entry
496 TDF_Tool::Label( aDoc->GetData(), aEntry, L );
497 if ( L.IsNull() ) continue;
498 Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
500 if ( !obj.IsNull() ) {
501 TCollection_AsciiString aStudyEntry (obj->GetAuxData());
502 aEntry2StEntry.Bind( aEntry, aStudyEntry);
503 aStEntry2Entry.Bind( aStudyEntry, aEntry );
507 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
508 Standard_Integer aLen = aSeq->Length(), objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
509 Resource_DataMapOfAsciiStringAsciiString aNameToEntry, anEntryToBadName;
511 //Replace entries by the names
512 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("geomObj_"),
513 allowedChars ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
514 if (aLen == 0) anUpdatedScript = aScript;
516 for (Standard_Integer i = 1; i <= aLen; i+=2) {
517 anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i)-1);
518 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i+1));
519 if (theObjectNames.IsBound(anEntry)) {
520 aName = theObjectNames.Find(anEntry);
521 // check validity of aName
522 bool isValidName = true;
523 if ( aName.IsIntegerValue() ) { // aName must not start with a digit
524 aName.Insert( 1, 'a' );
527 int p, p2=1; // replace not allowed chars
528 while ((p = aName.FirstLocationNotInSet(allowedChars, p2, aName.Length()))) {
529 aName.SetValue(p, '_');
533 if ( aNameToEntry.IsBound( aName ) && anEntry != aNameToEntry( aName ))
534 { // diff objects have same name - make a new name by appending a digit
535 TCollection_AsciiString aName2;
536 Standard_Integer i = 0;
538 aName2 = aName + "_" + ++i;
539 } while ( aNameToEntry.IsBound( aName2 ) && anEntry != aNameToEntry( aName2 ));
543 if ( !isValidName ) {
545 anEntryToBadName.Bind( anEntry, theObjectNames.Find(anEntry) );
546 theObjectNames( anEntry ) = aName;
551 aName = aBaseName + TCollection_AsciiString(++objectCounter);
552 } while(aNameToEntry.IsBound(aName));
553 theObjectNames.Bind(anEntry, aName);
555 aNameToEntry.Bind(aName, anEntry); // to detect same name of diff objects
557 anUpdatedScript += aName;
558 aStart = aSeq->Value(i+1) + 1;
561 //Add final part of the script
562 if (aLen && aSeq->Value(aLen) < aScriptLength)
563 anUpdatedScript += aScript.SubString(aSeq->Value(aLen)+1, aScriptLength); // mkr : IPAL11865
566 for (anEntryToNameIt.Initialize( theObjectNames );
567 anEntryToNameIt.More();
568 anEntryToNameIt.Next())
570 const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
571 const TCollection_AsciiString& aName = anEntryToNameIt.Value();
574 TDF_Tool::Label( aDoc->GetData(), aEntry, L );
578 Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
582 bool anAutoColor = obj->GetAutoColor();
585 TCollection_AsciiString aCommand( "\n\t" );
586 aCommand += aName + ".SetAutoColor(1)";
587 anUpdatedScript += aCommand.ToCString();
590 SALOMEDS::Color aColor = obj->GetColor();
591 if ( aColor.R > 0 || aColor.G > 0 || aColor.B > 0 )
593 TCollection_AsciiString aCommand( "\n\t" );
594 aCommand += aName + ".SetColor(SALOMEDS.Color(" + aColor.R + "," + aColor.G + "," + aColor.B + "))";
595 anUpdatedScript += aCommand.ToCString();
599 // Make script to publish in study
602 std::map< int, std::string > anEntryToCommandMap; // sort publishing commands by object entry
603 for (anEntryToNameIt.Initialize( theObjectNames );
604 anEntryToNameIt.More();
605 anEntryToNameIt.Next())
607 const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
608 const TCollection_AsciiString& aName = anEntryToNameIt.Value();
609 if (anIgnoreObjMap.count(aEntry.ToCString()))
610 continue; // should not be dumped
611 if ( !aEntry2StEntry.IsBound( aEntry ))
612 continue; // was not published
613 TCollection_AsciiString aCommand("\n\tgeompy."), aFatherEntry;
615 // find a father entry
616 const TCollection_AsciiString& aStudyEntry = aEntry2StEntry( aEntry );
617 TCollection_AsciiString aFatherStudyEntry =
618 aStudyEntry.SubString( 1, aStudyEntry.SearchFromEnd(":") - 1 );
619 if ( aStEntry2Entry.IsBound( aFatherStudyEntry ))
620 aFatherEntry = aStEntry2Entry( aFatherStudyEntry );
623 if ( !aFatherEntry.IsEmpty() && theObjectNames.IsBound( aFatherEntry )) {
624 aCommand += "addToStudyInFather( ";
625 aCommand += theObjectNames( aFatherEntry ) + ", ";
628 aCommand += "addToStudy( ";
629 if ( anEntryToBadName.IsBound( aEntry ))
630 aCommand += aName + ", \"" + anEntryToBadName( aEntry ) + "\" )";
632 aCommand += aName + ", \"" + aName + "\" )";
634 // bind a command to the last digit of the entry
636 aEntry.SubString( aEntry.SearchFromEnd(":")+1, aEntry.Length() ).IntegerValue();
637 anEntryToCommandMap.insert( std::make_pair( tag, aCommand.ToCString() ));
640 // add publishing commands to the script
641 std::map< int, std::string >::iterator anEntryToCommand = anEntryToCommandMap.begin();
642 for ( ; anEntryToCommand != anEntryToCommandMap.end(); ++anEntryToCommand ) {
643 anUpdatedScript += (char*)anEntryToCommand->second.c_str();
647 //anUpdatedScript += "\n\tpass\n";
648 anUpdatedScript += "\n";
651 // fill _studyEntry2NameMap and build globalVars
652 TCollection_AsciiString globalVars;
653 _studyEntry2NameMap.Clear();
654 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString aStEntryToEntryIt;
655 for (aStEntryToEntryIt.Initialize( aStEntry2Entry );
656 aStEntryToEntryIt.More();
657 aStEntryToEntryIt.Next() )
659 const TCollection_AsciiString & name = theObjectNames( aStEntryToEntryIt.Value() );
660 _studyEntry2NameMap.Bind (aStEntryToEntryIt.Key(), name );
661 if ( !globalVars.IsEmpty() )
665 if ( !globalVars.IsEmpty() ) {
666 globalVars.Insert( 1, "\n\tglobal " );
667 anUpdatedScript.Insert( posToInsertGlobalVars, globalVars );
670 return anUpdatedScript;
673 //=======================================================================
674 //function : GetDumpName
676 //=======================================================================
678 const char* GEOM_Engine::GetDumpName (const char* theStudyEntry) const
680 if ( _studyEntry2NameMap.IsBound( (char*)theStudyEntry ))
681 return _studyEntry2NameMap( (char*)theStudyEntry ).ToCString();
686 //=======================================================================
687 //function : GetAllDumpNames
689 //=======================================================================
691 Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
693 Handle(TColStd_HSequenceOfAsciiString) aRetSeq = new TColStd_HSequenceOfAsciiString;
695 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString it (_studyEntry2NameMap);
696 for (; it.More(); it.Next()) {
697 aRetSeq->Append(it.Value());
704 //===========================================================================
705 // Internal functions
706 //===========================================================================
707 void ProcessFunction(Handle(GEOM_Function)& theFunction,
708 TCollection_AsciiString& theScript,
709 const TVariablesList& theVariables,
710 TDF_LabelMap& theProcessed,
711 std::set<std::string>& theIgnoreObjs)
713 if (theFunction.IsNull()) return;
715 if (theProcessed.Contains(theFunction->GetEntry())) return;
717 // pass functions, that depends on nonexisting ones
718 bool doNotProcess = false;
719 TDF_LabelSequence aSeq;
720 theFunction->GetDependency(aSeq);
721 Standard_Integer aLen = aSeq.Length();
722 for (Standard_Integer i = 1; i <= aLen && !doNotProcess; i++) {
723 TDF_Label aRefLabel = aSeq.Value(i);
724 Handle(TDF_Reference) aRef;
725 if (!aRefLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
729 if (aRef.IsNull() || aRef->Get().IsNull()) {
733 Handle(TDataStd_TreeNode) aT;
734 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
738 TDF_Label aDepLabel = aT->Label();
739 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aDepLabel);
741 if (aFunction.IsNull()) doNotProcess = true;
742 else if (!theProcessed.Contains(aDepLabel)) doNotProcess = true;
749 TCollection_AsciiString anObjEntry;
750 TDF_Tool::Entry(theFunction->GetOwnerEntry(), anObjEntry);
751 theIgnoreObjs.insert(anObjEntry.ToCString());
754 theProcessed.Add(theFunction->GetEntry());
756 TCollection_AsciiString aDescr = theFunction->GetDescription();
757 if(aDescr.Length() == 0) return;
759 //Check if its internal function which doesn't requires dumping
760 if(aDescr == "None") return;
762 //Replace parameter by notebook variables
763 ReplaceVariables(aDescr,theVariables);
768 //=============================================================================
770 * FindEntries: Returns a sequence of start/end positions of entries in the string
772 //=============================================================================
773 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString)
775 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
776 Standard_Integer aLen = theString.Length();
777 Standard_Boolean isFound = Standard_False;
779 const char* arr = theString.ToCString();
780 Standard_Integer i = 0, j;
785 if(c >= 48 && c <= 57) { //Is digit?
787 isFound = Standard_False;
788 while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
790 if(c == 58) isFound = Standard_True;
793 if(isFound && arr[j-2] != 58) { // last char should be a diggit
794 aSeq->Append(i+1); // +1 because AsciiString starts from 1
805 //=============================================================================
807 * ReplaceVariables: Replace parameters of the function by variales from
810 //=============================================================================
811 void ReplaceVariables(TCollection_AsciiString& theCommand,
812 const TVariablesList& theVariables)
815 cout<<"Command : "<<theCommand<<endl;
818 cout<<"All Entries:"<<endl;
819 TVariablesList::const_iterator it = theVariables.begin();
820 for(;it != theVariables.end();it++)
821 cout<<"\t'"<<(*it).first<<"'"<<endl;
824 //Additional case - multi-row commands
825 int aCommandIndex = 1;
826 while( aCommandIndex < 10 ) { // tmp check
827 TCollection_AsciiString aCommand = theCommand.Token("\n",aCommandIndex);
828 if( aCommand.Length() == 0 )
832 cout<<"Sub-command : "<<aCommand<<endl;
834 Standard_Integer aStartCommandPos = theCommand.Location(aCommand,1,theCommand.Length());
835 Standard_Integer aEndCommandPos = aStartCommandPos + aCommand.Length();
837 //Get Entry of the result object
838 TCollection_AsciiString anEntry;
839 if( aCommand.Search("=") != -1 ) // command returns an object
840 anEntry = aCommand.Token("=",1);
841 else { // command modifies the object
842 if (int aStartEntryPos = aCommand.Location(1,'(',1,aCommand.Length()))
843 if (int aEndEntryPos = aCommand.Location(1,',',aStartEntryPos,aCommand.Length()))
844 anEntry = aCommand.SubString(aStartEntryPos+1, aEndEntryPos-1);
846 //Remove white spaces
847 anEntry.RightAdjust();
848 anEntry.LeftAdjust();
850 cout<<"Result entry : '" <<anEntry<<"'"<<endl;
852 if ( anEntry.IsEmpty() ) {
857 //Check if result is list of entries - enough to get the first entry in this case
859 if( anEntry.Value( 1 ) == O_SQR_BRACKET && anEntry.Value( anEntry.Length() ) == C_SQR_BRACKET ) {
860 while(anEntry.Location(aNbEntries,COMMA,1,anEntry.Length()))
862 TCollection_AsciiString aSeparator(COMMA);
863 anEntry = anEntry.Token(aSeparator.ToCString(),1);
864 anEntry.Remove( 1, 1 );
865 anEntry.RightAdjust();
866 anEntry.LeftAdjust();
868 cout<<"Sub-entry : '" <<anEntry<<"'"<<endl;
871 //Find variables used for object construction
872 ObjectStates* aStates = 0;
873 TVariablesList::const_iterator it = theVariables.find(anEntry);
874 if( it != theVariables.end() )
875 aStates = (*it).second;
879 cout<<"Valiables list empty!!!"<<endl;
884 TState aVariables = aStates->GetCurrectState();
887 cout<<"Variables from SObject:"<<endl;
888 for (int i = 0; i < aVariables.size();i++)
889 cout<<"\t Variable["<<i<<"] = "<<aVariables[i].myVariable<<endl;
892 //Calculate total number of parameters
893 Standard_Integer aTotalNbParams = 1;
894 while(aCommand.Location(aTotalNbParams,COMMA,1,aCommand.Length()))
898 cout<<"aTotalNbParams = "<<aTotalNbParams<<endl;
900 Standard_Integer aFirstParam = aNbEntries;
902 //Replace parameters by variables
903 Standard_Integer aStartPos = 0;
904 Standard_Integer aEndPos = 0;
906 TCollection_AsciiString aVar, aReplacedVar;
907 for(Standard_Integer i=aFirstParam;i <= aTotalNbParams;i++) {
908 //Replace first parameter (bettwen '(' character and first ',' character)
911 aStartPos = aCommand.Location(O_BRACKET, 1, aCommand.Length()) + 1;
912 if(aTotalNbParams - aNbEntries > 0 )
913 aEndPos = aCommand.Location(aFirstParam, COMMA, 1, aCommand.Length());
915 aEndPos = aCommand.Location(C_BRACKET, 1, aCommand.Length());
917 //Replace last parameter (bettwen ',' character and ')' character)
918 else if(i == aTotalNbParams)
920 aStartPos = aCommand.Location(i-1, COMMA, 1, aCommand.Length()) + 2;
921 aEndPos = aCommand.Location(C_BRACKET, 1, aCommand.Length());
923 //Replace other parameters (bettwen two ',' characters)
924 else if(i != aFirstParam && i != aTotalNbParams )
926 aStartPos = aCommand.Location(i-1, COMMA, 1, aCommand.Length()) + 2;
927 aEndPos = aCommand.Location(i, COMMA, 1, aCommand.Length());
930 if( aCommand.Value( aStartPos ) == O_SQR_BRACKET )
932 if( aCommand.Value( aEndPos-1 ) == C_SQR_BRACKET )
934 if ( aStartPos == aEndPos )
935 continue; // PAL20889: for "[]"
938 cout<<"aStartPos = "<<aStartPos<<", aEndPos = "<<aEndPos<<endl;
940 aVar = aCommand.SubString(aStartPos, aEndPos-1);
945 cout<<"Variable: '"<< aVar <<"'"<<endl;
947 // specific case for sketcher
948 if(aVar.Location( TCollection_AsciiString("Sketcher:"), 1, aVar.Length() ) != 0) {
949 Standard_Integer aNbSections = 1;
950 while( aVar.Location( aNbSections, ':', 1, aVar.Length() ) )
954 int aStartSectionPos = 0, aEndSectionPos = 0;
955 TCollection_AsciiString aSection, aReplacedSection;
956 for(Standard_Integer aSectionIndex = 1; aSectionIndex <= aNbSections; aSectionIndex++) {
957 aStartSectionPos = aVar.Location( aSectionIndex, ':', 1, aVar.Length() ) + 1;
958 if( aSectionIndex != aNbSections )
959 aEndSectionPos = aVar.Location( aSectionIndex + 1, ':', 1, aVar.Length() );
961 aEndSectionPos = aVar.Length();
963 aSection = aVar.SubString(aStartSectionPos, aEndSectionPos-1);
965 cout<<"aSection: "<<aSection<<endl;
967 Standard_Integer aNbParams = 1;
968 while( aSection.Location( aNbParams, ' ', 1, aSection.Length() ) )
972 int aStartParamPos = 0, aEndParamPos = 0;
973 TCollection_AsciiString aParameter, aReplacedParameter;
974 for(Standard_Integer aParamIndex = 1; aParamIndex <= aNbParams; aParamIndex++) {
975 aStartParamPos = aSection.Location( aParamIndex, ' ', 1, aSection.Length() ) + 1;
976 if( aParamIndex != aNbParams )
977 aEndParamPos = aSection.Location( aParamIndex + 1, ' ', 1, aSection.Length() );
979 aEndParamPos = aSection.Length() + 1;
981 aParameter = aSection.SubString(aStartParamPos, aEndParamPos-1);
983 cout<<"aParameter: "<<aParameter<<endl;
985 if(iVar >= aVariables.size())
988 aReplacedParameter = aVariables[iVar].myVariable;
989 if(aReplacedParameter.IsEmpty()) {
994 if(aVariables[iVar].isVariable) {
995 aReplacedParameter.InsertBefore(1,"'");
996 aReplacedParameter.InsertAfter(aReplacedParameter.Length(),"'");
1000 cout<<"aSection before : "<<aSection<<endl;
1001 aSection.Remove(aStartParamPos, aEndParamPos - aStartParamPos);
1002 aSection.Insert(aStartParamPos, aReplacedParameter);
1004 cout<<"aSection after : "<<aSection<<endl<<endl;
1008 cout<<"aVar before : "<<aVar<<endl;
1009 aVar.Remove(aStartSectionPos, aEndSectionPos - aStartSectionPos);
1010 aVar.Insert(aStartSectionPos, aSection);
1012 cout<<"aVar after : "<<aVar<<endl<<endl;
1016 cout<<"aCommand before : "<<aCommand<<endl;
1017 aCommand.Remove(aStartPos, aEndPos - aStartPos);
1018 aCommand.Insert(aStartPos, aVar);
1020 cout<<"aCommand after : "<<aCommand<<endl;
1023 } // end of specific case for sketcher
1025 //If parameter is entry or 'None', skip it
1026 if(theVariables.find(aVar) != theVariables.end() || aVar.Search(":") != -1 || aVar == PY_NULL)
1029 if(iVar >= aVariables.size())
1032 aReplacedVar = aVariables[iVar].myVariable;
1033 if(aReplacedVar.IsEmpty()) {
1038 if(aVariables[iVar].isVariable) {
1039 aReplacedVar.InsertBefore(1,"\"");
1040 aReplacedVar.InsertAfter(aReplacedVar.Length(),"\"");
1043 aCommand.Remove(aStartPos, aEndPos - aStartPos);
1044 aCommand.Insert(aStartPos, aReplacedVar);
1048 theCommand.Remove(aStartCommandPos, aEndCommandPos - aStartCommandPos);
1049 theCommand.Insert(aStartCommandPos, aCommand);
1053 aStates->IncrementState();
1057 cout<<"Command : "<<theCommand<<endl;
1060 //================================================================================
1062 * \brief Constructor
1064 //================================================================================
1065 ObjectStates::ObjectStates()
1070 //================================================================================
1074 //================================================================================
1075 ObjectStates::~ObjectStates()
1079 //================================================================================
1081 * \brief Return current object state
1082 * \retval state - Object state (vector of notebook variable)
1084 //================================================================================
1085 TState ObjectStates::GetCurrectState() const
1087 if(_states.size() > _dumpstate)
1088 return _states[_dumpstate];
1092 //================================================================================
1094 * \brief Add new object state
1095 * \param theState - Object state (vector of notebook variable)
1097 //================================================================================
1098 void ObjectStates::AddState(const TState &theState)
1100 _states.push_back(theState);
1103 //================================================================================
1105 * \brief Increment object state
1107 //================================================================================
1108 void ObjectStates::IncrementState()