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>
60 #include <Standard_Failure.hxx>
61 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
66 #define PY_NULL "None"
69 static int MYDEBUG = 0;
71 static int MYDEBUG = 0;
74 static GEOM_Engine* TheEngine = NULL;
78 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_Object)& theObject)
80 TCollection_AsciiString anID(theObject->GetDocID()), anEntry;
81 TDF_Tool::Entry(theObject->GetEntry(), anEntry);
82 anID+=(TCollection_AsciiString("_")+anEntry);
86 static TCollection_AsciiString BuildID(Standard_Integer theDocID, char* theEntry)
88 TCollection_AsciiString anID(theDocID);
89 anID+=(TCollection_AsciiString("_")+theEntry);
93 static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
95 TCollection_AsciiString aDocID = theID.Token("_");
96 if(aDocID.Length() < 1) return -1;
97 return aDocID.IntegerValue();
100 void ProcessFunction(Handle(GEOM_Function)& theFunction,
101 TCollection_AsciiString& theScript,
102 TVariablesList theVariables,
103 TColStd_MapOfTransient& theProcessed);
105 void ReplaceVariables(TCollection_AsciiString& theCommand,
106 TVariablesList theVariables);
110 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString);
113 //=============================================================================
117 //=============================================================================
118 GEOM_Engine* GEOM_Engine::GetEngine() { return TheEngine; }
121 //=============================================================================
125 //=============================================================================
126 void GEOM_Engine::SetEngine(GEOM_Engine* theEngine) { TheEngine = theEngine; }
128 //=============================================================================
132 //=============================================================================
133 GEOM_Engine::GEOM_Engine()
135 TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver());
137 _OCAFApp = new GEOM_Application();
144 GEOM_Engine::~GEOM_Engine()
146 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It(_objects);
147 for(; It.More(); It.Next())
149 RemoveObject(Handle(GEOM_Object)::DownCast(It.Value()));
152 //Close all documents not closed
153 for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
156 _mapIDDocument.Clear();
160 //=============================================================================
164 //=============================================================================
165 Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID)
167 Handle(TDocStd_Document) aDoc;
168 if(!_mapIDDocument.IsBound(theDocID)) {
169 _OCAFApp->NewDocument("SALOME_GEOM", aDoc);
170 aDoc->SetUndoLimit(_UndoLimit);
171 _mapIDDocument.Bind(theDocID, aDoc);
172 TDataStd_Integer::Set(aDoc->Main(), theDocID);
175 return Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
178 //=============================================================================
182 //=============================================================================
183 int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument)
185 if(theDocument.IsNull()) return -1;
186 for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
187 if(anItr.Value() == theDocument) return anItr.Key();
193 //=============================================================================
197 //=============================================================================
198 Handle(GEOM_Object) GEOM_Engine::GetObject(int theDocID, char* theEntry)
200 TCollection_AsciiString anID = BuildID(theDocID, theEntry);
201 if(_objects.IsBound(anID)) return Handle(GEOM_Object)::DownCast(_objects(anID));
204 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
205 TDF_Tool::Label(aDoc->Main().Data(), theEntry, aLabel, Standard_True);
206 Handle(GEOM_Object) anObject = new GEOM_Object(aLabel);
208 _objects.Bind(anID, anObject);
213 //=============================================================================
217 //=============================================================================
218 Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
220 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
221 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
223 // NPAL18604: use existing label to decrease memory usage,
224 // if this label has been freed (object deleted)
225 bool useExisting = false;
227 if (!_lastCleared.IsNull()) {
228 if (_lastCleared.Root() == aDoc->Main().Root()) {
230 aChild = _lastCleared;
231 _lastCleared.Nullify();
236 aChild = TDF_TagSource::NewChild(aDoc->Main());
239 Handle(GEOM_Object) anObject = new GEOM_Object(aChild, theType);
241 //Put an object in the map of created objects
242 TCollection_AsciiString anID = BuildIDFromObject(anObject);
243 if(_objects.IsBound(anID)) _objects.UnBind(anID);
244 _objects.Bind(anID, anObject);
249 //=============================================================================
253 //=============================================================================
254 Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape,
255 Handle(TColStd_HArray1OfInteger) theIndices,
256 bool isStandaloneOperation)
258 if(theMainShape.IsNull() || theIndices.IsNull()) return NULL;
260 Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID());
261 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
263 // NPAL18604: use existing label to decrease memory usage,
264 // if this label has been freed (object deleted)
265 bool useExisting = false;
267 if (!_lastCleared.IsNull()) {
268 if (_lastCleared.Root() == aDoc->Main().Root()) {
270 aChild = _lastCleared;
271 _lastCleared.Nullify();
276 aChild = TDF_TagSource::NewChild(aDoc->Main());
279 Handle(GEOM_Function) aMainShape = theMainShape->GetLastFunction();
280 Handle(GEOM_Object) anObject = new GEOM_Object(aChild, 28); //28 is SUBSHAPE type
281 Handle(GEOM_Function) aFunction = anObject->AddFunction(GEOM_Object::GetSubShapeID(), 1);
283 GEOM_ISubShape aSSI(aFunction);
284 aSSI.SetMainShape(aMainShape);
285 aSSI.SetIndices(theIndices);
288 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
291 GEOM_Solver aSolver (GEOM_Engine::GetEngine());
292 if (!aSolver.ComputeFunction(aFunction)) {
293 MESSAGE("GEOM_Engine::AddSubShape Error: Can't build a sub shape");
297 catch (Standard_Failure) {
298 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
299 MESSAGE("GEOM_Engine::AddSubShape Error: " << aFail->GetMessageString());
303 //Put an object in the map of created objects
304 TCollection_AsciiString anID = BuildIDFromObject(anObject);
305 if(_objects.IsBound(anID)) _objects.UnBind(anID);
306 _objects.Bind(anID, anObject);
308 GEOM::TPythonDump pd (aFunction);
310 if (isStandaloneOperation) {
311 pd << anObject << " = geompy.GetSubShape(" << theMainShape << ", [";
312 Standard_Integer i = theIndices->Lower(), up = theIndices->Upper();
313 for (; i <= up - 1; i++) {
314 pd << theIndices->Value(i) << ", ";
316 pd << theIndices->Value(up) << "])";
324 //=============================================================================
328 //=============================================================================
329 bool GEOM_Engine::RemoveObject(Handle(GEOM_Object) theObject)
331 if (!theObject) return false;
333 //Remove an object from the map of available objects
334 TCollection_AsciiString anID = BuildIDFromObject(theObject);
335 if (_objects.IsBound(anID)) _objects.UnBind(anID);
337 int nb = theObject->GetNbFunctions();
338 Handle(TDataStd_TreeNode) aNode;
339 for (int i = 1; i<=nb; i++) {
340 Handle(GEOM_Function) aFunction = theObject->GetFunction(i);
341 if (aFunction->GetEntry().FindAttribute(GEOM_Function::GetFunctionTreeID(), aNode))
345 TDF_Label aLabel = theObject->GetEntry();
346 aLabel.ForgetAllAttributes(Standard_True);
347 _lastCleared = aLabel;
354 //=============================================================================
358 //=============================================================================
359 void GEOM_Engine::Undo(int theDocID)
361 GetDocument(theDocID)->Undo();
364 //=============================================================================
368 //=============================================================================
369 void GEOM_Engine::Redo(int theDocID)
371 GetDocument(theDocID)->Redo();
374 //=============================================================================
378 //=============================================================================
379 bool GEOM_Engine::Save(int theDocID, char* theFileName)
381 if(!_mapIDDocument.IsBound(theDocID)) return false;
382 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
384 _OCAFApp->SaveAs(aDoc, theFileName);
389 //=============================================================================
393 //=============================================================================
394 bool GEOM_Engine::Load(int theDocID, char* theFileName)
396 Handle(TDocStd_Document) aDoc;
397 if(_OCAFApp->Open(theFileName, aDoc) != CDF_RS_OK) {
401 aDoc->SetUndoLimit(_UndoLimit);
403 if(_mapIDDocument.IsBound(theDocID)) _mapIDDocument.UnBind(theDocID);
404 _mapIDDocument.Bind(theDocID, aDoc);
406 TDataStd_Integer::Set(aDoc->Main(), theDocID);
411 //=============================================================================
415 //=============================================================================
416 void GEOM_Engine::Close(int theDocID)
418 if (_mapIDDocument.IsBound(theDocID)) {
419 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
421 //Remove all GEOM Objects associated to the given document
422 TColStd_SequenceOfAsciiString aSeq;
423 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It (_objects);
424 for (; It.More(); It.Next()) {
425 TCollection_AsciiString anObjID (It.Key());
426 Standard_Integer anID = ExtractDocID(anObjID);
427 if (theDocID == anID) aSeq.Append(It.Key());
429 for (Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
431 _lastCleared.Nullify();
433 _mapIDDocument.UnBind(theDocID);
434 _OCAFApp->Close(aDoc);
439 //=============================================================================
443 //=============================================================================
444 TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
445 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
446 TVariablesList theVariables,
450 TCollection_AsciiString aScript;
451 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
453 if (aDoc.IsNull()) return TCollection_AsciiString("def RebuildData(theStudy): pass\n");
455 aScript = "import geompy\n";
456 aScript += "import math\n";
457 aScript += "import SALOMEDS\n\n";
458 aScript += "def RebuildData(theStudy):";
459 aScript += "\n\tgeompy.init_geom(theStudy)";
461 Standard_Integer posToInertGlobalVars = aScript.Length() + 1;
463 Handle(TDataStd_TreeNode) aNode, aRoot;
464 Handle(GEOM_Function) aFunction;
465 TColStd_MapOfTransient aMap;
467 if (aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
468 TDataStd_ChildNodeIterator Itr(aRoot);
469 for (; Itr.More(); Itr.Next()) {
471 aFunction = GEOM_Function::GetFunction(aNode->Label());
472 if (aFunction.IsNull()) {
473 MESSAGE ( "Null function !!!!" );
476 ProcessFunction(aFunction, aScript, theVariables,aMap);
480 Resource_DataMapOfAsciiStringAsciiString aEntry2StEntry, aStEntry2Entry;
481 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString anEntryToNameIt;
482 // build maps entry <-> studyEntry
483 for (anEntryToNameIt.Initialize( theObjectNames );
484 anEntryToNameIt.More();
485 anEntryToNameIt.Next())
487 const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
488 // look for an object by entry
490 TDF_Tool::Label( aDoc->GetData(), aEntry, L );
491 if ( L.IsNull() ) continue;
492 Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
494 if ( !obj.IsNull() ) {
495 TCollection_AsciiString aStudyEntry (obj->GetAuxData());
496 aEntry2StEntry.Bind( aEntry, aStudyEntry);
497 aStEntry2Entry.Bind( aStudyEntry, aEntry );
501 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
502 Standard_Integer aLen = aSeq->Length(), objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
503 Resource_DataMapOfAsciiStringAsciiString aNameToEntry, anEntryToBadName;
505 //Replace entries by the names
506 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("geomObj_"),
507 allowedChars ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
508 if (aLen == 0) anUpdatedScript = aScript;
510 for (Standard_Integer i = 1; i <= aLen; i+=2) {
511 anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i)-1);
512 anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i+1));
513 if (theObjectNames.IsBound(anEntry)) {
514 aName = theObjectNames.Find(anEntry);
515 // check validity of aName
516 bool isValidName = true;
517 if ( aName.IsIntegerValue() ) { // aName must not start with a digit
518 aName.Insert( 1, 'a' );
521 int p, p2=1; // replace not allowed chars
522 while ((p = aName.FirstLocationNotInSet(allowedChars, p2, aName.Length()))) {
523 aName.SetValue(p, '_');
527 if ( aNameToEntry.IsBound( aName ) && anEntry != aNameToEntry( aName ))
528 { // diff objects have same name - make a new name by appending a digit
529 TCollection_AsciiString aName2;
530 Standard_Integer i = 0;
532 aName2 = aName + "_" + ++i;
533 } while ( aNameToEntry.IsBound( aName2 ) && anEntry != aNameToEntry( aName2 ));
537 if ( !isValidName ) {
539 anEntryToBadName.Bind( anEntry, theObjectNames.Find(anEntry) );
540 theObjectNames( anEntry ) = aName;
545 aName = aBaseName + TCollection_AsciiString(++objectCounter);
546 } while(aNameToEntry.IsBound(aName));
547 theObjectNames.Bind(anEntry, aName);
549 aNameToEntry.Bind(aName, anEntry); // to detect same name of diff objects
551 anUpdatedScript += aName;
552 aStart = aSeq->Value(i+1) + 1;
555 //Add final part of the script
556 if (aLen && aSeq->Value(aLen) < aScriptLength)
557 anUpdatedScript += aScript.SubString(aSeq->Value(aLen)+1, aScriptLength); // mkr : IPAL11865
560 for (anEntryToNameIt.Initialize( theObjectNames );
561 anEntryToNameIt.More();
562 anEntryToNameIt.Next())
564 const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
565 const TCollection_AsciiString& aName = anEntryToNameIt.Value();
568 TDF_Tool::Label( aDoc->GetData(), aEntry, L );
572 Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
576 bool anAutoColor = obj->GetAutoColor();
579 TCollection_AsciiString aCommand( "\n\t" );
580 aCommand += aName + ".SetAutoColor(1)";
581 anUpdatedScript += aCommand.ToCString();
584 SALOMEDS::Color aColor = obj->GetColor();
585 if ( aColor.R > 0 || aColor.G > 0 || aColor.B > 0 )
587 TCollection_AsciiString aCommand( "\n\t" );
588 aCommand += aName + ".SetColor(SALOMEDS.Color(" + aColor.R + "," + aColor.G + "," + aColor.B + "))";
589 anUpdatedScript += aCommand.ToCString();
593 // Make script to publish in study
596 std::map< int, std::string > anEntryToCommandMap; // sort publishing commands by object entry
597 for (anEntryToNameIt.Initialize( theObjectNames );
598 anEntryToNameIt.More();
599 anEntryToNameIt.Next())
601 const TCollection_AsciiString& aEntry = anEntryToNameIt.Key();
602 const TCollection_AsciiString& aName = anEntryToNameIt.Value();
603 if ( !aEntry2StEntry.IsBound( aEntry ))
604 continue; // was not published
605 TCollection_AsciiString aCommand("\n\tgeompy."), aFatherEntry;
607 // find a father entry
608 const TCollection_AsciiString& aStudyEntry = aEntry2StEntry( aEntry );
609 TCollection_AsciiString aFatherStudyEntry =
610 aStudyEntry.SubString( 1, aStudyEntry.SearchFromEnd(":") - 1 );
611 if ( aStEntry2Entry.IsBound( aFatherStudyEntry ))
612 aFatherEntry = aStEntry2Entry( aFatherStudyEntry );
615 if ( !aFatherEntry.IsEmpty() && theObjectNames.IsBound( aFatherEntry )) {
616 aCommand += "addToStudyInFather( ";
617 aCommand += theObjectNames( aFatherEntry ) + ", ";
620 aCommand += "addToStudy( ";
621 if ( anEntryToBadName.IsBound( aEntry ))
622 aCommand += aName + ", \"" + anEntryToBadName( aEntry ) + "\" )";
624 aCommand += aName + ", \"" + aName + "\" )";
626 // bind a command to the last digit of the entry
628 aEntry.SubString( aEntry.SearchFromEnd(":")+1, aEntry.Length() ).IntegerValue();
629 anEntryToCommandMap.insert( std::make_pair( tag, aCommand.ToCString() ));
632 // add publishing commands to the script
633 std::map< int, std::string >::iterator anEntryToCommand = anEntryToCommandMap.begin();
634 for ( ; anEntryToCommand != anEntryToCommandMap.end(); ++anEntryToCommand ) {
635 anUpdatedScript += (char*)anEntryToCommand->second.c_str();
639 //anUpdatedScript += "\n\tpass\n";
640 anUpdatedScript += "\n";
643 // fill _studyEntry2NameMap and build globalVars
644 TCollection_AsciiString globalVars;
645 _studyEntry2NameMap.Clear();
646 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString aStEntryToEntryIt;
647 for (aStEntryToEntryIt.Initialize( aStEntry2Entry );
648 aStEntryToEntryIt.More();
649 aStEntryToEntryIt.Next() )
651 const TCollection_AsciiString & name = theObjectNames( aStEntryToEntryIt.Value() );
652 _studyEntry2NameMap.Bind (aStEntryToEntryIt.Key(), name );
653 if ( !globalVars.IsEmpty() )
657 if ( !globalVars.IsEmpty() ) {
658 globalVars.Insert( 1, "\n\tglobal " );
659 anUpdatedScript.Insert( posToInertGlobalVars, globalVars );
662 return anUpdatedScript;
665 //=======================================================================
666 //function : GetDumpName
668 //=======================================================================
670 const char* GEOM_Engine::GetDumpName (const char* theStudyEntry) const
672 if ( _studyEntry2NameMap.IsBound( (char*)theStudyEntry ))
673 return _studyEntry2NameMap( (char*)theStudyEntry ).ToCString();
678 //=======================================================================
679 //function : GetAllDumpNames
681 //=======================================================================
683 Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
685 Handle(TColStd_HSequenceOfAsciiString) aRetSeq = new TColStd_HSequenceOfAsciiString;
687 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString it (_studyEntry2NameMap);
688 for (; it.More(); it.Next()) {
689 aRetSeq->Append(it.Value());
696 //===========================================================================
697 // Internal functions
698 //===========================================================================
699 void ProcessFunction(Handle(GEOM_Function)& theFunction,
700 TCollection_AsciiString& theScript,
701 TVariablesList theVariables,
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,theVariables);
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 TVariablesList theVariables)
781 //Get Entry of the result object
782 TCollection_AsciiString anEntry = theCommand.Token("=",1);
785 cout<<"All Entries:"<<endl;
786 TVariablesList::const_iterator it = theVariables.begin();
787 for(;it != theVariables.end();it++)
788 cout<<"\t'"<<(*it).first<<"'"<<endl;
791 //Remove white spaces
792 anEntry.RightAdjust();
793 anEntry.LeftAdjust();
795 cout<<"Result entry : '" <<anEntry<<"'"<<endl;
797 //Find variables used for object construction
798 vector<TVariable> aVariables;
799 TVariablesList::const_iterator it = theVariables.find(anEntry);
800 if( it != theVariables.end() )
801 aVariables = (*it).second;
803 if(aVariables.empty()) {
805 cout<<"Valiables list empty!!!"<<endl;
810 cout<<"Variables from SObject:"<<endl;
811 for (int i = 0; i < aVariables.size();i++)
812 cout<<"\t Variable["<<i<<"]"<<aVariables[i].myVariable<<endl;
815 //Calculate total number of parameter
816 Standard_Integer aTotalNbParams = 1;
817 while(theCommand.Location(aTotalNbParams,COMMA,1,theCommand.Length()))
820 //Replace parameters by variables
821 Standard_Integer aStartPos = 0;
822 Standard_Integer aEndPos = 0;
824 TCollection_AsciiString aReplasedVar, aVar;
825 for(Standard_Integer i=1;i <= aTotalNbParams;i++) {
827 //Replace first parameter (bettwen '(' character and first ',' character)
830 aStartPos = theCommand.Location(O_BRACKET, 1, theCommand.Length()) + 1;
831 aEndPos = theCommand.Location(COMMA, 1, theCommand.Length());
833 //Replace last parameter (bettwen ',' character and ')' character)
834 else if(i == aTotalNbParams)
836 aStartPos = theCommand.Location(i-1, COMMA, 1, theCommand.Length()) + 2;
837 aEndPos = theCommand.Location(C_BRACKET, 1, theCommand.Length());
839 //Replace other parameters (bettwen two ',' characters)
840 else if(i != 1 && i != aTotalNbParams )
842 aStartPos = theCommand.Location(i-1, COMMA, 1, theCommand.Length()) + 2;
843 aEndPos = theCommand.Location(i, COMMA, 1, theCommand.Length());
845 aVar = theCommand.SubString(aStartPos, aEndPos-1);
847 cout<<"Current variable 1: '"<< aVar <<"'"<<endl;
853 cout<<"Current variable 2: '"<< aVar <<"'"<<endl;
855 //If parameter is entry or 'None', skip it
856 if(theVariables.find(aVar) != theVariables.end() || aVar == PY_NULL)
859 aReplasedVar = aVariables[iVar].myVariable;
862 if(aReplasedVar.IsEmpty()) {
867 if(aVariables[iVar].isVariable) {
868 aReplasedVar.InsertBefore(1,"\"");
869 aReplasedVar.InsertAfter(aReplasedVar.Length(),"\"");
872 theCommand.Remove(aStartPos, aEndPos - aStartPos);
873 theCommand.Insert(aStartPos,aReplasedVar);