1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #pragma warning( disable:4786 )
24 #include "GEOM_Engine.hxx"
26 #include "GEOM_Solver.hxx"
27 #include "GEOM_Function.hxx"
28 #include "GEOM_ISubShape.hxx"
29 #include "GEOM_SubShapeDriver.hxx"
30 #include "GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient.hxx"
31 #include "GEOM_PythonDump.hxx"
33 #include "utilities.h"
35 #include <TDF_Tool.hxx>
36 #include <TDF_Data.hxx>
37 #include <TDF_LabelSequence.hxx>
38 #include <TDataStd_Integer.hxx>
39 #include <TDataStd_ChildNodeIterator.hxx>
40 #include <TFunction_Driver.hxx>
41 #include <TFunction_DriverTable.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
46 #include <TCollection_AsciiString.hxx>
47 #include <TCollection_ExtendedString.hxx>
48 #include <TColStd_SequenceOfAsciiString.hxx>
49 #include <TColStd_MapOfTransient.hxx>
50 #include <TColStd_HSequenceOfInteger.hxx>
53 #include <Interface_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
54 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
59 #include <Standard_Failure.hxx>
60 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
67 static int MYDEBUG = 0;
69 static int MYDEBUG = 0;
72 static GEOM_Engine* TheEngine = NULL;
74 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_Object)& theObject)
76 TCollection_AsciiString anID(theObject->GetDocID()), anEntry;
77 TDF_Tool::Entry(theObject->GetEntry(), anEntry);
78 anID+=(TCollection_AsciiString("_")+anEntry);
82 static TCollection_AsciiString BuildID(Standard_Integer theDocID, char* theEntry)
84 TCollection_AsciiString anID(theDocID);
85 anID+=(TCollection_AsciiString("_")+theEntry);
89 static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
91 TCollection_AsciiString aDocID = theID.Token("_");
92 if(aDocID.Length() < 1) return -1;
93 return aDocID.IntegerValue();
96 void ProcessFunction(Handle(GEOM_Function)& theFunction,
97 TCollection_AsciiString& theScript,
98 Resource_DataMapOfAsciiStringAsciiString& theVariableNames,
99 TColStd_HSequenceOfAsciiString& theStudyVariables,
100 TColStd_MapOfTransient& theProcessed);
102 void ReplaceVariables(TCollection_AsciiString& theCommand,
103 Resource_DataMapOfAsciiStringAsciiString& theVariableNames,
104 TColStd_HSequenceOfAsciiString& theStudyVariables);
108 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString);
111 //=============================================================================
115 //=============================================================================
116 GEOM_Engine* GEOM_Engine::GetEngine() { return TheEngine; }
119 //=============================================================================
123 //=============================================================================
124 void GEOM_Engine::SetEngine(GEOM_Engine* theEngine) { TheEngine = theEngine; }
126 //=============================================================================
130 //=============================================================================
131 GEOM_Engine::GEOM_Engine()
133 TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver());
135 _OCAFApp = new GEOM_Application();
142 GEOM_Engine::~GEOM_Engine()
144 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It(_objects);
145 for(; It.More(); It.Next())
147 RemoveObject(Handle(GEOM_Object)::DownCast(It.Value()));
150 //Close all documents not closed
151 for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
154 _mapIDDocument.Clear();
158 //=============================================================================
162 //=============================================================================
163 Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID)
165 Handle(TDocStd_Document) aDoc;
166 if(!_mapIDDocument.IsBound(theDocID)) {
167 _OCAFApp->NewDocument("SALOME_GEOM", aDoc);
168 aDoc->SetUndoLimit(_UndoLimit);
169 _mapIDDocument.Bind(theDocID, aDoc);
170 TDataStd_Integer::Set(aDoc->Main(), theDocID);
173 return Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
176 //=============================================================================
180 //=============================================================================
181 int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument)
183 if(theDocument.IsNull()) return -1;
184 for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
185 if(anItr.Value() == theDocument) return anItr.Key();
191 //=============================================================================
195 //=============================================================================
196 Handle(GEOM_Object) GEOM_Engine::GetObject(int theDocID, char* theEntry)
198 TCollection_AsciiString anID = BuildID(theDocID, theEntry);
199 if(_objects.IsBound(anID)) return Handle(GEOM_Object)::DownCast(_objects(anID));
202 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
203 TDF_Tool::Label(aDoc->Main().Data(), theEntry, aLabel, Standard_True);
204 Handle(GEOM_Object) anObject = new GEOM_Object(aLabel);
206 _objects.Bind(anID, anObject);
211 //=============================================================================
215 //=============================================================================
216 Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
218 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
219 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
221 // NPAL18604: use existing label to decrease memory usage,
222 // if this label has been freed (object deleted)
223 bool useExisting = false;
225 if (!_lastCleared.IsNull()) {
226 if (_lastCleared.Root() == aDoc->Main().Root()) {
228 aChild = _lastCleared;
229 _lastCleared.Nullify();
234 aChild = TDF_TagSource::NewChild(aDoc->Main());
237 Handle(GEOM_Object) anObject = new GEOM_Object(aChild, theType);
239 //Put an object in the map of created objects
240 TCollection_AsciiString anID = BuildIDFromObject(anObject);
241 if(_objects.IsBound(anID)) _objects.UnBind(anID);
242 _objects.Bind(anID, anObject);
247 //=============================================================================
251 //=============================================================================
252 Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape,
253 Handle(TColStd_HArray1OfInteger) theIndices,
254 bool isStandaloneOperation)
256 if(theMainShape.IsNull() || theIndices.IsNull()) return NULL;
258 Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID());
259 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
261 // NPAL18604: use existing label to decrease memory usage,
262 // if this label has been freed (object deleted)
263 bool useExisting = false;
265 if (!_lastCleared.IsNull()) {
266 if (_lastCleared.Root() == aDoc->Main().Root()) {
268 aChild = _lastCleared;
269 _lastCleared.Nullify();
274 aChild = TDF_TagSource::NewChild(aDoc->Main());
277 Handle(GEOM_Function) aMainShape = theMainShape->GetLastFunction();
278 Handle(GEOM_Object) anObject = new GEOM_Object(aChild, 28); //28 is SUBSHAPE type
279 Handle(GEOM_Function) aFunction = anObject->AddFunction(GEOM_Object::GetSubShapeID(), 1);
281 GEOM_ISubShape aSSI(aFunction);
282 aSSI.SetMainShape(aMainShape);
283 aSSI.SetIndices(theIndices);
286 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
289 GEOM_Solver aSolver (GEOM_Engine::GetEngine());
290 if (!aSolver.ComputeFunction(aFunction)) {
291 MESSAGE("GEOM_Engine::AddSubShape Error: Can't build a sub shape");
295 catch (Standard_Failure) {
296 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
297 MESSAGE("GEOM_Engine::AddSubShape Error: " << aFail->GetMessageString());
301 //Put an object in the map of created objects
302 TCollection_AsciiString anID = BuildIDFromObject(anObject);
303 if(_objects.IsBound(anID)) _objects.UnBind(anID);
304 _objects.Bind(anID, anObject);
306 GEOM::TPythonDump pd (aFunction);
308 if (isStandaloneOperation) {
309 pd << anObject << " = geompy.GetSubShape(" << theMainShape << ", [";
310 Standard_Integer i = theIndices->Lower(), up = theIndices->Upper();
311 for (; i <= up - 1; i++) {
312 pd << theIndices->Value(i) << ", ";
314 pd << theIndices->Value(up) << "])";
322 //=============================================================================
326 //=============================================================================
327 bool GEOM_Engine::RemoveObject(Handle(GEOM_Object) theObject)
329 if (!theObject) return false;
331 //Remove an object from the map of available objects
332 TCollection_AsciiString anID = BuildIDFromObject(theObject);
333 if (_objects.IsBound(anID)) _objects.UnBind(anID);
335 int nb = theObject->GetNbFunctions();
336 Handle(TDataStd_TreeNode) aNode;
337 for (int i = 1; i<=nb; i++) {
338 Handle(GEOM_Function) aFunction = theObject->GetFunction(i);
339 if (aFunction->GetEntry().FindAttribute(GEOM_Function::GetFunctionTreeID(), aNode))
343 TDF_Label aLabel = theObject->GetEntry();
344 aLabel.ForgetAllAttributes(Standard_True);
345 _lastCleared = aLabel;
352 //=============================================================================
356 //=============================================================================
357 void GEOM_Engine::Undo(int theDocID)
359 GetDocument(theDocID)->Undo();
362 //=============================================================================
366 //=============================================================================
367 void GEOM_Engine::Redo(int theDocID)
369 GetDocument(theDocID)->Redo();
372 //=============================================================================
376 //=============================================================================
377 bool GEOM_Engine::Save(int theDocID, char* theFileName)
379 if(!_mapIDDocument.IsBound(theDocID)) return false;
380 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
382 _OCAFApp->SaveAs(aDoc, theFileName);
387 //=============================================================================
391 //=============================================================================
392 bool GEOM_Engine::Load(int theDocID, char* theFileName)
394 Handle(TDocStd_Document) aDoc;
395 if(_OCAFApp->Open(theFileName, aDoc) != CDF_RS_OK) {
399 aDoc->SetUndoLimit(_UndoLimit);
401 if(_mapIDDocument.IsBound(theDocID)) _mapIDDocument.UnBind(theDocID);
402 _mapIDDocument.Bind(theDocID, aDoc);
404 TDataStd_Integer::Set(aDoc->Main(), theDocID);
409 //=============================================================================
413 //=============================================================================
414 void GEOM_Engine::Close(int theDocID)
416 if (_mapIDDocument.IsBound(theDocID)) {
417 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
419 //Remove all GEOM Objects associated to the given document
420 TColStd_SequenceOfAsciiString aSeq;
421 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It (_objects);
422 for (; It.More(); It.Next()) {
423 TCollection_AsciiString anObjID (It.Key());
424 Standard_Integer anID = ExtractDocID(anObjID);
425 if (theDocID == anID) aSeq.Append(It.Key());
427 for (Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
429 _lastCleared.Nullify();
431 _mapIDDocument.UnBind(theDocID);
432 _OCAFApp->Close(aDoc);
437 //=============================================================================
441 //=============================================================================
442 TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
443 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
444 Resource_DataMapOfAsciiStringAsciiString& theVariableNames,
445 TColStd_HSequenceOfAsciiString& theStudyVariables,
449 TCollection_AsciiString aScript;
450 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
452 if (aDoc.IsNull()) return TCollection_AsciiString("def RebuildData(theStudy): pass\n");
454 aScript = "import geompy\n";
455 aScript += "import math\n";
456 aScript += "import SALOMEDS\n\n";
457 aScript += "def RebuildData(theStudy):";
458 aScript += "\n\tgeompy.init_geom(theStudy)";
460 Standard_Integer posToInertGlobalVars = aScript.Length() + 1;
462 Handle(TDataStd_TreeNode) aNode, aRoot;
463 Handle(GEOM_Function) aFunction;
464 TColStd_MapOfTransient aMap;
466 if (aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
467 TDataStd_ChildNodeIterator Itr(aRoot);
468 for (; Itr.More(); Itr.Next()) {
470 aFunction = GEOM_Function::GetFunction(aNode->Label());
471 if (aFunction.IsNull()) {
472 MESSAGE ( "Null function !!!!" );
475 ProcessFunction(aFunction, aScript, theVariableNames, theStudyVariables ,aMap);
479 Resource_DataMapOfAsciiStringAsciiString aEntry2StEntry, aStEntry2Entry;
480 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString anEntryToNameIt;
481 // build maps entry <-> studyEntry
482 for (anEntryToNameIt.Initialize( theObjectNames );
483 anEntryToNameIt.More();
484 anEntryToNameIt.Next())
486 const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
487 // look for an object by entry
489 TDF_Tool::Label( aDoc->GetData(), aEntry, L );
490 if ( L.IsNull() ) continue;
491 Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
493 if ( !obj.IsNull() ) {
494 TCollection_AsciiString aStudyEntry (obj->GetAuxData());
495 aEntry2StEntry.Bind( aEntry, aStudyEntry);
496 aStEntry2Entry.Bind( aStudyEntry, aEntry );
500 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
501 Standard_Integer aLen = aSeq->Length(), objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
502 Resource_DataMapOfAsciiStringAsciiString aNameToEntry, anEntryToBadName;
504 //Replace entries by the names
505 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("geomObj_"),
506 allowedChars ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
507 if (aLen == 0) anUpdatedScript = aScript;
509 for (Standard_Integer i = 1; i <= aLen; i+=2) {
510 anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i)-1);
511 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i+1));
512 if (theObjectNames.IsBound(anEntry)) {
513 aName = theObjectNames.Find(anEntry);
514 // check validity of aName
515 bool isValidName = true;
516 if ( aName.IsIntegerValue() ) { // aName must not start with a digit
517 aName.Insert( 1, 'a' );
520 int p, p2=1; // replace not allowed chars
521 while ((p = aName.FirstLocationNotInSet(allowedChars, p2, aName.Length()))) {
522 aName.SetValue(p, '_');
526 if ( aNameToEntry.IsBound( aName ) && anEntry != aNameToEntry( aName ))
527 { // diff objects have same name - make a new name by appending a digit
528 TCollection_AsciiString aName2;
529 Standard_Integer i = 0;
531 aName2 = aName + "_" + ++i;
532 } while ( aNameToEntry.IsBound( aName2 ) && anEntry != aNameToEntry( aName2 ));
536 if ( !isValidName ) {
538 anEntryToBadName.Bind( anEntry, theObjectNames.Find(anEntry) );
539 theObjectNames( anEntry ) = aName;
544 aName = aBaseName + TCollection_AsciiString(++objectCounter);
545 } while(aNameToEntry.IsBound(aName));
546 theObjectNames.Bind(anEntry, aName);
548 aNameToEntry.Bind(aName, anEntry); // to detect same name of diff objects
550 anUpdatedScript += aName;
551 aStart = aSeq->Value(i+1) + 1;
554 //Add final part of the script
555 if (aLen && aSeq->Value(aLen) < aScriptLength)
556 anUpdatedScript += aScript.SubString(aSeq->Value(aLen)+1, aScriptLength); // mkr : IPAL11865
559 for (anEntryToNameIt.Initialize( theObjectNames );
560 anEntryToNameIt.More();
561 anEntryToNameIt.Next())
563 const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
564 const TCollection_AsciiString& aName = anEntryToNameIt.Value();
567 TDF_Tool::Label( aDoc->GetData(), aEntry, L );
571 Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
575 bool anAutoColor = obj->GetAutoColor();
578 TCollection_AsciiString aCommand( "\n\t" );
579 aCommand += aName + ".SetAutoColor(1)";
580 anUpdatedScript += aCommand.ToCString();
583 SALOMEDS::Color aColor = obj->GetColor();
584 if ( aColor.R > 0 || aColor.G > 0 || aColor.B > 0 )
586 TCollection_AsciiString aCommand( "\n\t" );
587 aCommand += aName + ".SetColor(SALOMEDS.Color(" + aColor.R + "," + aColor.G + "," + aColor.B + "))";
588 anUpdatedScript += aCommand.ToCString();
592 // Make script to publish in study
595 std::map< int, std::string > anEntryToCommandMap; // sort publishing commands by object entry
596 for (anEntryToNameIt.Initialize( theObjectNames );
597 anEntryToNameIt.More();
598 anEntryToNameIt.Next())
600 const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
601 const TCollection_AsciiString& aName = anEntryToNameIt.Value();
602 if ( !aEntry2StEntry.IsBound( aEntry ))
603 continue; // was not published
604 TCollection_AsciiString aCommand("\n\tgeompy."), aFatherEntry;
606 // find a father entry
607 const TCollection_AsciiString& aStudyEntry = aEntry2StEntry( aEntry );
608 TCollection_AsciiString aFatherStudyEntry =
609 aStudyEntry.SubString( 1, aStudyEntry.SearchFromEnd(":") - 1 );
610 if ( aStEntry2Entry.IsBound( aFatherStudyEntry ))
611 aFatherEntry = aStEntry2Entry( aFatherStudyEntry );
614 if ( !aFatherEntry.IsEmpty() && theObjectNames.IsBound( aFatherEntry )) {
615 aCommand += "addToStudyInFather( ";
616 aCommand += theObjectNames( aFatherEntry ) + ", ";
619 aCommand += "addToStudy( ";
620 if ( anEntryToBadName.IsBound( aEntry ))
621 aCommand += aName + ", \"" + anEntryToBadName( aEntry ) + "\" )";
623 aCommand += aName + ", \"" + aName + "\" )";
625 // bind a command to the last digit of the entry
627 aEntry.SubString( aEntry.SearchFromEnd(":")+1, aEntry.Length() ).IntegerValue();
628 anEntryToCommandMap.insert( std::make_pair( tag, aCommand.ToCString() ));
631 // add publishing commands to the script
632 std::map< int, std::string >::iterator anEntryToCommand = anEntryToCommandMap.begin();
633 for ( ; anEntryToCommand != anEntryToCommandMap.end(); ++anEntryToCommand ) {
634 anUpdatedScript += (char*)anEntryToCommand->second.c_str();
638 //anUpdatedScript += "\n\tpass\n";
639 anUpdatedScript += "\n";
642 // fill _studyEntry2NameMap and build globalVars
643 TCollection_AsciiString globalVars;
644 _studyEntry2NameMap.Clear();
645 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString aStEntryToEntryIt;
646 for (aStEntryToEntryIt.Initialize( aStEntry2Entry );
647 aStEntryToEntryIt.More();
648 aStEntryToEntryIt.Next() )
650 const TCollection_AsciiString & name = theObjectNames( aStEntryToEntryIt.Value() );
651 _studyEntry2NameMap.Bind (aStEntryToEntryIt.Key(), name );
652 if ( !globalVars.IsEmpty() )
656 if ( !globalVars.IsEmpty() ) {
657 globalVars.Insert( 1, "\n\tglobal " );
658 anUpdatedScript.Insert( posToInertGlobalVars, globalVars );
661 return anUpdatedScript;
664 //=======================================================================
665 //function : GetDumpName
667 //=======================================================================
669 const char* GEOM_Engine::GetDumpName (const char* theStudyEntry) const
671 if ( _studyEntry2NameMap.IsBound( (char*)theStudyEntry ))
672 return _studyEntry2NameMap( (char*)theStudyEntry ).ToCString();
677 //=======================================================================
678 //function : GetAllDumpNames
680 //=======================================================================
682 Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
684 Handle(TColStd_HSequenceOfAsciiString) aRetSeq = new TColStd_HSequenceOfAsciiString;
686 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString it (_studyEntry2NameMap);
687 for (; it.More(); it.Next()) {
688 aRetSeq->Append(it.Value());
695 //===========================================================================
696 // Internal functions
697 //===========================================================================
698 void ProcessFunction(Handle(GEOM_Function)& theFunction,
699 TCollection_AsciiString& theScript,
700 Resource_DataMapOfAsciiStringAsciiString& theVariableNames,
701 TColStd_HSequenceOfAsciiString& theStudyVariables,
702 TColStd_MapOfTransient& theProcessed)
704 if(theFunction.IsNull() || theProcessed.Contains(theFunction)) return;
707 TDF_LabelSequence aSeq;
708 theFunction->GetDependency(aSeq);
709 Standard_Integer aLen = aSeq.Length();
710 for(Standard_Integer i = 1; i<= aLen; i++) {
711 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aSeq.Value(i));
712 if(aFunction.IsNull()) continue;
713 ProcessFunction(aFunction, theScript, theProcessed);
717 TCollection_AsciiString aDescr = theFunction->GetDescription();
718 if(aDescr.Length() == 0) {
719 //cout << "Warning: the function has no description" << endl;
722 //Check if its internal function which doesn't requires dumping
723 if(aDescr == "None") return;
725 //Replace parameter by notebook variables
726 ReplaceVariables(aDescr,theVariableNames,theStudyVariables);
731 theProcessed.Add(theFunction);
735 //=============================================================================
737 * FindEntries: Returns a sequence of start/end positions of entries in the string
739 //=============================================================================
740 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString)
742 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
743 Standard_Integer aLen = theString.Length();
744 Standard_Boolean isFound = Standard_False;
746 const char* arr = theString.ToCString();
747 Standard_Integer i = 0, j;
752 if(c >= 48 && c <= 57) { //Is digit?
754 isFound = Standard_False;
755 while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
757 if(c == 58) isFound = Standard_True;
760 if(isFound && arr[j-2] != 58) { // last char should be a diggit
761 aSeq->Append(i+1); // +1 because AsciiString starts from 1
772 //=============================================================================
774 * ReplaceVariables: Replace parameters of the function by variales from
777 //=============================================================================
778 void ReplaceVariables(TCollection_AsciiString& theCommand,
779 Resource_DataMapOfAsciiStringAsciiString& theVariableNames,
780 TColStd_HSequenceOfAsciiString& theStudyVariables)
782 //Get Entry of the result object
783 TCollection_AsciiString anEntry = theCommand.Token("=",1);
785 //Remove white spaces
786 anEntry.RightAdjust();
787 anEntry.LeftAdjust();
789 cout<<"Result entry : '" <<anEntry<<"'"<<endl;
791 //Find variables used for object construction
792 TCollection_AsciiString aVariables;
793 if(theVariableNames.IsBound(anEntry))
794 aVariables = theVariableNames.Find(anEntry);
796 if(aVariables.IsEmpty()) {
798 cout<<"Valiables list empty!!!"<<endl;
803 cout<<"Variables : '" <<aVariables<<"'"<<endl;
805 //Calculate number of variables, that mast be replaced
806 Standard_Integer aNbReplacedParams = 1,aPos;
807 TColStd_HSequenceOfInteger aPositions;
808 while(aPos = aVariables.Location(aNbReplacedParams,':',1,aVariables.Length())) {
809 aPositions.Append(aPos);
814 cout<<"Number of replaced variables : " <<aNbReplacedParams<<endl;
818 for(Standard_Integer i = 1;i<=aPositions.Length();i++)
819 cout<<"Positions ["<<i<<"] = "<<aPositions.Value(i)<<endl;
822 //Calculate total number of parameter
823 Standard_Integer aTotalNbParams = 1;
824 while(theCommand.Location(aTotalNbParams,COMMA,1,theCommand.Length()))
829 cout<<"Total Number of parameters : " <<aTotalNbParams<<endl;
831 //Get Variables names
832 TColStd_SequenceOfAsciiString aVarSeq;
833 TCollection_AsciiString aVar;
834 Standard_Integer i = 1;
835 while(i <= aNbReplacedParams){
838 aVar = (aPositions.Value(i) == 1) ? TCollection_AsciiString() : aVariables.SubString(1, aPositions.Value(i)-1);
839 else if(i == aNbReplacedParams) {
840 Standard_Integer aLen = aVariables.Length();
841 Standard_Integer aPos = aPositions.Value(i-1);
842 aVar = (aPos == aLen) ? TCollection_AsciiString() : aVariables.SubString(aPos+1, aLen);
845 Standard_Integer aPrevPos = aPositions.Value(i-1);
846 Standard_Integer aCurrentPos = aPositions.Value(i);
847 aVar = (aCurrentPos - aPrevPos == 1) ? TCollection_AsciiString() : aVariables.SubString(aPrevPos+1, aCurrentPos-1);
850 cout<<"Variable ["<<i<<"] = '"<<aVar<<"'"<<endl;
852 //Add current varibale
853 aVarSeq.Append(aVar);
857 //Replace parameters by variables
858 Standard_Integer aStartPos = 0;
859 Standard_Integer aEndPos = 0;
860 Standard_Integer iVar = 1;
861 for(Standard_Integer i=1;i <= aTotalNbParams;i++) {
863 //Replace first parameter (bettwen '(' character and first ',' character)
866 aStartPos = theCommand.Location(O_BRACKET, 1, theCommand.Length()) + 1;
867 aEndPos = theCommand.Location(COMMA, 1, theCommand.Length());
869 //Replace last parameter (bettwen ',' character and ')' character)
870 else if(i == aVarSeq.Length())
872 aStartPos = theCommand.Location(i-1, COMMA, 1, theCommand.Length()) + 2;
873 aEndPos = theCommand.Location(C_BRACKET, 1, theCommand.Length());
875 //Replace other parameters (bettwen two ',' characters)
876 else if(i != 1 && i != aVarSeq.Length())
878 aStartPos = theCommand.Location(i-1, COMMA, 1, theCommand.Length()) + 2;
879 aEndPos = theCommand.Location(i, COMMA, 1, theCommand.Length());
882 aVar = aVarSeq.Value(iVar);
884 //If parameter is entry, skip it
887 if(theVariableNames.IsBound(aVar))
897 for(Standard_Integer i = 1; i <= theStudyVariables.Length();i++)
898 if(theStudyVariables.Value(i).IsEqual(aVar)) {
903 aVar.InsertBefore(1,"\"");
904 aVar.InsertAfter(aVar.Length(),"\"");
907 theCommand.Remove(aStartPos, aEndPos - aStartPos);
908 theCommand.Insert(aStartPos,aVar);