1 // Copyright (C) 2007-2012 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
24 #pragma warning( disable:4786 )
27 #include "GEOM_Engine.hxx"
29 #include "GEOM_Solver.hxx"
30 #include "GEOM_Function.hxx"
31 #include "GEOM_ISubShape.hxx"
32 #include "GEOM_SubShapeDriver.hxx"
33 #include "GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient.hxx"
34 #include "GEOM_PythonDump.hxx"
36 #include <Basics_OCCTVersion.hxx>
38 #include "utilities.h"
40 #include <Basics_Utils.hxx>
42 #include <TDF_Tool.hxx>
43 #include <TDF_Data.hxx>
44 #include <TDF_Reference.hxx>
45 #include <TDF_LabelSequence.hxx>
46 #include <TDataStd_Integer.hxx>
47 #include <TDataStd_ChildNodeIterator.hxx>
48 #include <TFunction_Driver.hxx>
49 #include <TFunction_DriverTable.hxx>
50 #include <TDataStd_ByteArray.hxx>
51 #include <TDataStd_UAttribute.hxx>
52 #include <TDF_ChildIterator.hxx>
53 #include <TDataStd_Comment.hxx>
56 #include <TopTools_IndexedMapOfShape.hxx>
58 #include <TCollection_AsciiString.hxx>
59 #include <TCollection_ExtendedString.hxx>
60 #include <TColStd_SequenceOfAsciiString.hxx>
61 #include <TColStd_MapOfTransient.hxx>
62 #include <TColStd_HSequenceOfInteger.hxx>
64 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
65 #include <TColStd_HArray1OfByte.hxx>
66 #include <TColStd_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
68 #include <TDataStd_HArray1OfByte.hxx>
69 #include <Interface_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
72 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
76 #include <Standard_Failure.hxx>
77 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
82 #define O_SQR_BRACKET '['
83 #define C_SQR_BRACKET ']'
84 #define PY_NULL "None"
87 static int MYDEBUG = 0;
89 static int MYDEBUG = 0;
92 typedef std::map< TCollection_AsciiString, TCollection_AsciiString > TSting2StringMap;
93 typedef std::map< TCollection_AsciiString, TObjectData > TSting2ObjDataMap;
94 typedef std::map< TCollection_AsciiString, TObjectData* > TSting2ObjDataPtrMap;
96 static GEOM_Engine* TheEngine = NULL;
99 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_Object)& theObject)
101 TCollection_AsciiString anID(theObject->GetDocID()), anEntry;
102 TDF_Tool::Entry(theObject->GetEntry(), anEntry);
103 anID+=(TCollection_AsciiString("_")+anEntry);
107 static TCollection_AsciiString BuildID(Standard_Integer theDocID, char* theEntry)
109 TCollection_AsciiString anID(theDocID);
110 anID+=(TCollection_AsciiString("_")+theEntry);
114 static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
116 TCollection_AsciiString aDocID = theID.Token("_");
117 if(aDocID.Length() < 1) return -1;
118 return aDocID.IntegerValue();
121 bool ProcessFunction(Handle(GEOM_Function)& theFunction,
122 TCollection_AsciiString& theScript,
123 TCollection_AsciiString& theAfterScript,
124 const TVariablesList& theVariables,
125 const bool theIsPublished,
126 TDF_LabelMap& theProcessed,
127 std::set<TCollection_AsciiString>& theIgnoreObjs,
128 bool& theIsDumpCollected);
130 void ReplaceVariables(TCollection_AsciiString& theCommand,
131 const TVariablesList& theVariables);
133 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString);
135 void ReplaceEntriesByNames (TCollection_AsciiString& theScript,
136 TSting2ObjDataMap& aEntry2ObjData,
137 const bool theIsPublished,
138 TColStd_SequenceOfAsciiString& theObjListToPublish,
139 Standard_Integer& objectCounter,
140 Resource_DataMapOfAsciiStringAsciiString& aNameToEntry);
142 void AddObjectColors (int theDocID,
143 TCollection_AsciiString& theScript,
144 const TSting2ObjDataMap& theEntry2ObjData);
146 void AddTextures (int theDocID, TCollection_AsciiString& theScript);
148 void PublishObject (TObjectData& theObjectData,
149 TSting2ObjDataMap& theEntry2ObjData,
150 const TSting2ObjDataPtrMap& theStEntry2ObjDataPtr,
151 Resource_DataMapOfAsciiStringAsciiString& theNameToEntry,
152 std::map< int, TCollection_AsciiString >& theEntryToCmdMap,
153 std::set<TCollection_AsciiString>& theMapOfPublished);
157 //================================================================================
159 * \brief Fix up the name of python variable
161 //================================================================================
163 void healPyName( TCollection_AsciiString& pyName,
164 const TCollection_AsciiString& anEntry,
165 Resource_DataMapOfAsciiStringAsciiString& aNameToEntry)
167 const TCollection_AsciiString allowedChars
168 ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
170 if ( pyName.IsIntegerValue() ) { // pyName must not start with a digit
171 pyName.Insert( 1, 'a' );
173 int p, p2=1; // replace not allowed chars
174 while ((p = pyName.FirstLocationNotInSet(allowedChars, p2, pyName.Length()))) {
175 pyName.SetValue(p, '_');
178 if ( aNameToEntry.IsBound( pyName ) && anEntry != aNameToEntry( pyName ))
179 { // diff objects have same name - make a new name by appending a digit
180 TCollection_AsciiString aName2;
181 Standard_Integer i = 0;
183 aName2 = pyName + "_" + ++i;
184 } while ( aNameToEntry.IsBound( aName2 ) && anEntry != aNameToEntry( aName2 ));
190 //=======================================================================
191 //function : GetTextureGUID
193 //=======================================================================
194 const Standard_GUID& GEOM_Engine::GetTextureGUID()
196 static Standard_GUID anID("FF1BBB01-5D14-4df2-980B-3A668264EA17");
200 //=============================================================================
204 //=============================================================================
205 GEOM_Engine* GEOM_Engine::GetEngine() { return TheEngine; }
207 //=============================================================================
211 //=============================================================================
212 void GEOM_Engine::SetEngine(GEOM_Engine* theEngine) { TheEngine = theEngine; }
214 //=============================================================================
218 //=============================================================================
219 GEOM_Engine::GEOM_Engine()
221 TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver());
223 _OCAFApp = new GEOM_Application();
230 GEOM_Engine::~GEOM_Engine()
232 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It(_objects);
233 std::list< Handle(GEOM_Object) > objs;
234 for(; It.More(); It.Next())
235 objs.push_back( Handle(GEOM_Object)::DownCast(It.Value()) );
236 std::list< Handle(GEOM_Object) >::iterator objit;
237 for(objit = objs.begin(); objit != objs.end(); ++objit)
238 RemoveObject(*objit);
240 //Close all documents not closed
241 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
242 TColStd_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument);
244 Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument);
246 for (; anItr.More(); anItr.Next())
249 _mapIDDocument.Clear();
253 //=============================================================================
257 //=============================================================================
258 Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID, bool force)
260 Handle(TDocStd_Document) aDoc;
261 if(_mapIDDocument.IsBound(theDocID)) {
262 aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
265 _OCAFApp->NewDocument("SALOME_GEOM", aDoc);
266 aDoc->SetUndoLimit(_UndoLimit);
267 _mapIDDocument.Bind(theDocID, aDoc);
268 TDataStd_Integer::Set(aDoc->Main(), theDocID);
273 //=============================================================================
277 //=============================================================================
278 int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument)
280 if (theDocument.IsNull()) return -1;
281 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
282 TColStd_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument);
284 Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument);
286 for (; anItr.More(); anItr.Next())
287 if (anItr.Value() == theDocument) return anItr.Key();
292 //=============================================================================
296 //=============================================================================
297 Handle(GEOM_Object) GEOM_Engine::GetObject(int theDocID, char* theEntry, bool force)
299 Handle(GEOM_Object) anObject;
301 TCollection_AsciiString anID = BuildID(theDocID, theEntry);
303 if (_objects.IsBound(anID)) {
304 anObject = Handle(GEOM_Object)::DownCast(_objects(anID));
307 Handle(TDocStd_Document) aDoc = GetDocument(theDocID, force);
308 if ( !aDoc.IsNull()) {
310 TDF_Tool::Label(aDoc->Main().Data(), theEntry, aLabel, Standard_True);
311 anObject = new GEOM_Object(aLabel);
312 _objects.Bind(anID, anObject);
319 //=============================================================================
323 //=============================================================================
324 Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
326 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
327 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
329 // NPAL18604: use existing label to decrease memory usage,
330 // if this label has been freed (object deleted)
331 bool useExisting = false;
333 if (_freeLabels.find(theDocID) != _freeLabels.end()) {
334 std::list<TDF_Label>& aFreeLabels = _freeLabels[theDocID];
335 if (!aFreeLabels.empty()) {
337 aChild = aFreeLabels.front();
338 aFreeLabels.pop_front();
343 aChild = TDF_TagSource::NewChild(aDoc->Main());
346 Handle(GEOM_Object) anObject = new GEOM_Object(aChild, theType);
348 //Put an object in the map of created objects
349 TCollection_AsciiString anID = BuildIDFromObject(anObject);
350 if(_objects.IsBound(anID)) _objects.UnBind(anID);
351 _objects.Bind(anID, anObject);
356 //=============================================================================
360 //=============================================================================
361 Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape,
362 Handle(TColStd_HArray1OfInteger) theIndices,
363 bool isStandaloneOperation)
365 if (theMainShape.IsNull() || theIndices.IsNull()) return NULL;
367 Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID());
368 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
370 // NPAL18604: use existing label to decrease memory usage,
371 // if this label has been freed (object deleted)
372 bool useExisting = false;
374 int aDocID = theMainShape->GetDocID();
375 if (_freeLabels.find(aDocID) != _freeLabels.end()) {
376 std::list<TDF_Label>& aFreeLabels = _freeLabels[aDocID];
377 if (!aFreeLabels.empty()) {
379 aChild = aFreeLabels.front();
380 aFreeLabels.pop_front();
385 aChild = TDF_TagSource::NewChild(aDoc->Main());
388 Handle(GEOM_Function) aMainShape = theMainShape->GetLastFunction();
389 Handle(GEOM_Object) anObject = new GEOM_Object (aChild, 28); //28 is SUBSHAPE type
390 Handle(GEOM_Function) aFunction = anObject->AddFunction(GEOM_Object::GetSubShapeID(), 1);
392 GEOM_ISubShape aSSI (aFunction);
393 aSSI.SetMainShape(aMainShape);
394 aSSI.SetIndices(theIndices);
397 #if OCC_VERSION_LARGE > 0x06010000
400 GEOM_Solver aSolver (GEOM_Engine::GetEngine());
401 if (!aSolver.ComputeFunction(aFunction)) {
402 MESSAGE("GEOM_Engine::AddSubShape Error: Can't build a sub shape");
406 catch (Standard_Failure) {
407 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
408 MESSAGE("GEOM_Engine::AddSubShape Error: " << aFail->GetMessageString());
412 // Put an object in the map of created objects
413 TCollection_AsciiString anID = BuildIDFromObject(anObject);
414 if (_objects.IsBound(anID)) _objects.UnBind(anID);
415 _objects.Bind(anID, anObject);
417 // Put this sub-shape in the list of sub-shapes of theMainShape
418 aMainShape->AddSubShapeReference(aFunction);
420 GEOM::TPythonDump pd (aFunction);
422 if (isStandaloneOperation) {
423 pd << anObject << " = geompy.GetSubShape(" << theMainShape << ", [";
424 Standard_Integer i = theIndices->Lower(), up = theIndices->Upper();
425 for (; i <= up - 1; i++) {
426 pd << theIndices->Value(i) << ", ";
428 pd << theIndices->Value(up) << "])";
436 //=============================================================================
440 //=============================================================================
441 bool GEOM_Engine::RemoveObject(Handle(GEOM_Object) theObject)
443 if (theObject.IsNull()) return false;
445 int aDocID = theObject->GetDocID();
446 if(!_mapIDDocument.IsBound(aDocID))
447 return false; // document is closed...
449 //Remove an object from the map of available objects
450 TCollection_AsciiString anID = BuildIDFromObject(theObject);
451 if (_objects.IsBound(anID)) _objects.UnBind(anID);
453 // If sub-shape, remove it from the list of sub-shapes of its main shape
454 if (!theObject->IsMainShape()) {
455 Handle(GEOM_Function) aFunction = theObject->GetFunction(1);
456 GEOM_ISubShape aSSI (aFunction);
457 Handle(GEOM_Function) aMainShape = aSSI.GetMainShape();
458 //If main shape is not null, then remove
459 if(!aMainShape.IsNull())
460 aMainShape->RemoveSubShapeReference(aFunction);
463 int nb = theObject->GetNbFunctions();
464 Handle(TDataStd_TreeNode) aNode;
465 for (int i = 1; i <= nb; i++) {
466 Handle(GEOM_Function) aFunction = theObject->GetFunction(i);
467 if (aFunction->GetEntry().FindAttribute(GEOM_Function::GetFunctionTreeID(), aNode))
471 TDF_Label aLabel = theObject->GetEntry();
472 aLabel.ForgetAllAttributes(Standard_True);
474 // Remember the label to reuse it then
475 std::list<TDF_Label>& aFreeLabels = _freeLabels[aDocID];
476 aFreeLabels.push_back(aLabel);
483 //=============================================================================
487 //=============================================================================
488 void GEOM_Engine::Undo(int theDocID)
490 GetDocument(theDocID)->Undo();
493 //=============================================================================
497 //=============================================================================
498 void GEOM_Engine::Redo(int theDocID)
500 GetDocument(theDocID)->Redo();
503 //=============================================================================
507 //=============================================================================
508 bool GEOM_Engine::Save(int theDocID, char* theFileName)
510 if(!_mapIDDocument.IsBound(theDocID)) return false;
511 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
513 _OCAFApp->SaveAs(aDoc, theFileName);
518 //=============================================================================
522 //=============================================================================
523 bool GEOM_Engine::Load(int theDocID, char* theFileName)
525 Handle(TDocStd_Document) aDoc;
526 #if OCC_VERSION_LARGE > 0x06050100 // For OCCT6.5.2 and higher
527 if (_OCAFApp->Open(theFileName, aDoc) != PCDM_RS_OK) {
529 if (_OCAFApp->Open(theFileName, aDoc) != CDF_RS_OK) {
534 aDoc->SetUndoLimit(_UndoLimit);
536 if(_mapIDDocument.IsBound(theDocID)) _mapIDDocument.UnBind(theDocID);
537 _mapIDDocument.Bind(theDocID, aDoc);
539 TDataStd_Integer::Set(aDoc->Main(), theDocID);
544 //=============================================================================
548 //=============================================================================
549 void GEOM_Engine::Close(int theDocID)
551 if (_mapIDDocument.IsBound(theDocID)) {
552 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
554 //Remove all GEOM Objects associated to the given document
555 TColStd_SequenceOfAsciiString aSeq;
556 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It (_objects);
557 for (; It.More(); It.Next()) {
558 TCollection_AsciiString anObjID (It.Key());
559 Standard_Integer anID = ExtractDocID(anObjID);
560 if (theDocID == anID) aSeq.Append(It.Key());
562 for (Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
564 // Forget free labels for this document
565 TFreeLabelsList::iterator anIt = _freeLabels.find(theDocID);
566 if (anIt != _freeLabels.end()) {
567 _freeLabels.erase(anIt);
570 _mapIDDocument.UnBind(theDocID);
571 _OCAFApp->Close(aDoc);
576 //=============================================================================
580 //=============================================================================
581 TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
582 std::vector<TObjectData>& theObjectData,
583 TVariablesList theVariables,
588 // Set "C" numeric locale to save numbers correctly
589 Kernel_Utils::Localizer loc;
591 TCollection_AsciiString aScript;
592 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
596 TCollection_AsciiString anEmptyScript;
598 anEmptyScript = "def RebuildData(theStudy): pass\n";
599 return anEmptyScript;
602 aScript = "import GEOM\n";
603 aScript += "import geompy\n";
604 aScript += "import math\n";
605 aScript += "import SALOMEDS\n\n";
607 aScript += "def RebuildData(theStudy):";
609 aScript += "\n\tgeompy.init_geom(theStudy)\n";
611 AddTextures(theDocID, aScript);
613 Standard_Integer posToInsertGlobalVars = aScript.Length() + 1;
615 // a map containing copies of TObjectData from theObjectData
616 TSting2ObjDataMap aEntry2ObjData;
617 // contains pointers to TObjectData of either aEntry2ObjData or theObjectData; the latter
618 // occures when several StudyEntries correspond to one Entry
619 TSting2ObjDataPtrMap aStEntry2ObjDataPtr;
621 //Resource_DataMapOfAsciiStringAsciiString aEntry2StEntry, aStEntry2Entry, theObjectNames;
622 for (unsigned i = 0; i < theObjectData.size(); ++i )
624 TObjectData& data = theObjectData[i];
625 // look for an object by entry
627 TDF_Tool::Label( aDoc->GetData(), data._entry, L );
628 if ( L.IsNull() ) continue;
629 Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
631 if ( !obj.IsNull() ) {
632 TSting2ObjDataMap::iterator ent2Data =
633 aEntry2ObjData.insert( std::make_pair( data._entry, data )).first;
635 if ( ent2Data->second._studyEntry == data._studyEntry ) // Entry encounters 1st time
636 aStEntry2ObjDataPtr.insert( std::make_pair( data._studyEntry, & ent2Data->second ));
638 aStEntry2ObjDataPtr.insert( std::make_pair( data._studyEntry, & data ));
642 // collect objects entries to be published
643 TColStd_SequenceOfAsciiString aObjListToPublish;
645 // iterates on functions till critical (that requiers publication of objects)
646 Handle(TDataStd_TreeNode) aNode, aRoot;
647 Handle(GEOM_Function) aFunction;
648 TDF_LabelMap aCheckedFuncMap;
649 std::set< TCollection_AsciiString > anIgnoreObjMap;
651 TCollection_AsciiString aFuncScript;
653 // Mantis issue 0020768
654 Standard_Integer objectCounter = 0;
655 Resource_DataMapOfAsciiStringAsciiString aNameToEntry;
657 if (aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
658 TDataStd_ChildNodeIterator Itr(aRoot);
659 for (; Itr.More(); Itr.Next()) {
661 aFunction = GEOM_Function::GetFunction(aNode->Label());
662 if (aFunction.IsNull()) {
663 MESSAGE ( "Null function !!!!" );
666 bool isDumpCollected = false;
667 TCollection_AsciiString aCurScript, anAfterScript;
668 if (!ProcessFunction(aFunction, aCurScript, anAfterScript, theVariables,
669 isPublished, aCheckedFuncMap, anIgnoreObjMap,
672 // add function description before dump
673 if (!aCurScript.IsEmpty())
674 aFuncScript += aCurScript;
675 if (isDumpCollected ) {
676 // Replace entries by the names
677 ReplaceEntriesByNames( aFuncScript, aEntry2ObjData, isPublished,
678 aObjListToPublish, objectCounter, aNameToEntry );
680 // publish collected objects
681 std::map< int, TCollection_AsciiString > anEntryToCmdMap; // sort publishing commands by study entry
682 int i = 1, n = aObjListToPublish.Length();
683 for ( ; i <= n; i++ )
685 const TCollection_AsciiString& aEntry = aObjListToPublish.Value(i);
686 PublishObject( aEntry2ObjData[aEntry], aEntry2ObjData, aStEntry2ObjDataPtr,
687 aNameToEntry, anEntryToCmdMap, anIgnoreObjMap );
689 // add publishing commands to the script
690 std::map< int, TCollection_AsciiString >::iterator anEntryToCmd = anEntryToCmdMap.begin();
691 for ( ; anEntryToCmd != anEntryToCmdMap.end(); ++anEntryToCmd )
692 aFuncScript += anEntryToCmd->second;
694 // PTv, 0020001 add result objects from RestoreGivenSubShapes into ignore list,
695 // because they will be published during command execution
696 int indx = anAfterScript.Search( "RestoreGivenSubShapes" );
698 indx = anAfterScript.Search( "RestoreSubShapes" );
700 TCollection_AsciiString aSubStr = anAfterScript.SubString(1, indx);
701 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aSubStr);
702 i = 1, n = aSeq->Length();
703 for ( ; i <= n; i+=2) {
704 TCollection_AsciiString anEntry =
705 aSubStr.SubString(aSeq->Value(i), aSeq->Value(i+1));
706 anIgnoreObjMap.insert(anEntry.ToCString());
710 aObjListToPublish.Clear();
711 aScript += aFuncScript;
714 aFuncScript += anAfterScript;
718 // Replace entries by the names
719 aObjListToPublish.Clear();
720 ReplaceEntriesByNames( aFuncScript, aEntry2ObjData, isPublished, aObjListToPublish,
721 objectCounter, aNameToEntry );
723 aScript += aFuncScript;
726 AddObjectColors( theDocID, aScript, aEntry2ObjData );
728 // Make script to publish in study
729 TSting2ObjDataPtrMap::iterator aStEntry2ObjDataPtrIt;
732 std::map< int, TCollection_AsciiString > anEntryToCmdMap; // sort publishing commands by object entry
734 for (aStEntry2ObjDataPtrIt = aStEntry2ObjDataPtr.begin();
735 aStEntry2ObjDataPtrIt != aStEntry2ObjDataPtr.end();
736 ++aStEntry2ObjDataPtrIt)
738 TObjectData* data = aStEntry2ObjDataPtrIt->second;
739 if ( anIgnoreObjMap.count( data->_entry ))
740 continue; // should not be dumped
741 PublishObject( *data, aEntry2ObjData, aStEntry2ObjDataPtr,
742 aNameToEntry, anEntryToCmdMap, anIgnoreObjMap );
744 // add publishing commands to the script
745 std::map< int, TCollection_AsciiString >::iterator anEntryToCmd = anEntryToCmdMap.begin();
746 for ( ; anEntryToCmd != anEntryToCmdMap.end(); ++anEntryToCmd )
747 aScript += anEntryToCmd->second;
750 //RNV: issue 16219: EDF PAL 469: "RemoveFromStudy" Function
751 //Add unpublish command if need
752 TCollection_AsciiString unpublishCmd("\n");
754 unpublishCmd += "\t";
755 unpublishCmd += "geompy.hideInStudy(";
757 for (aStEntry2ObjDataPtrIt = aStEntry2ObjDataPtr.begin();
758 aStEntry2ObjDataPtrIt != aStEntry2ObjDataPtr.end();
759 ++aStEntry2ObjDataPtrIt)
761 TObjectData* data = aStEntry2ObjDataPtrIt->second;
762 if ( data->_unpublished && !data->_pyName.IsEmpty() ) {
763 aScript += unpublishCmd + data->_pyName + ")";
767 //aScript += "\n\tpass\n";
771 // fill _studyEntry2NameMap and build globalVars
772 TCollection_AsciiString globalVars;
773 _studyEntry2NameMap.Clear();
774 for (aStEntry2ObjDataPtrIt = aStEntry2ObjDataPtr.begin();
775 aStEntry2ObjDataPtrIt != aStEntry2ObjDataPtr.end();
776 ++aStEntry2ObjDataPtrIt)
778 const TCollection_AsciiString& studyEntry = aStEntry2ObjDataPtrIt->first;
779 const TObjectData* data = aStEntry2ObjDataPtrIt->second;
780 _studyEntry2NameMap.Bind ( studyEntry, data->_pyName );
781 if ( !globalVars.IsEmpty() )
783 globalVars += data->_pyName;
785 if ( isMultiFile && !globalVars.IsEmpty() ) {
786 globalVars.Insert( 1, "\n\tglobal " );
787 aScript.Insert( posToInsertGlobalVars, globalVars );
793 //=======================================================================
794 //function : GetDumpName
796 //=======================================================================
798 const char* GEOM_Engine::GetDumpName (const char* theStudyEntry) const
800 if ( _studyEntry2NameMap.IsBound( (char*)theStudyEntry ))
801 return _studyEntry2NameMap( (char*)theStudyEntry ).ToCString();
806 //=======================================================================
807 //function : GetAllDumpNames
809 //=======================================================================
811 Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
813 Handle(TColStd_HSequenceOfAsciiString) aRetSeq = new TColStd_HSequenceOfAsciiString;
815 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString it (_studyEntry2NameMap);
816 for (; it.More(); it.Next()) {
817 aRetSeq->Append(it.Value());
823 #define TEXTURE_LABEL_ID 1
824 #define TEXTURE_LABEL_FILE 2
825 #define TEXTURE_LABEL_WIDTH 3
826 #define TEXTURE_LABEL_HEIGHT 4
827 #define TEXTURE_LABEL_DATA 5
829 int GEOM_Engine::addTexture(int theDocID, int theWidth, int theHeight,
830 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
831 const Handle(TColStd_HArray1OfByte)& theTexture,
833 const Handle(TDataStd_HArray1OfByte)& theTexture,
835 const TCollection_AsciiString& theFileName)
837 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
838 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
840 // NPAL18604: use existing label to decrease memory usage,
841 // if this label has been freed (object deleted)
842 bool useExisting = false;
844 if (_freeLabels.find(theDocID) != _freeLabels.end()) {
845 std::list<TDF_Label>& aFreeLabels = _freeLabels[theDocID];
846 if (!aFreeLabels.empty()) {
848 aChild = aFreeLabels.front();
849 aFreeLabels.pop_front();
854 aChild = TDF_TagSource::NewChild(aDoc->Main());
857 aChild.ForgetAllAttributes(Standard_True);
858 Handle(TDataStd_TreeNode) node;
859 if ( !aChild.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), node ) )
860 node = TDataStd_TreeNode::Set(aChild);
861 TDataStd_UAttribute::Set(aChild, GetTextureGUID());
863 static int aTextureID = 0;
865 TDataStd_Integer::Set(aChild.FindChild(TEXTURE_LABEL_ID), ++aTextureID);
866 TDataStd_Comment::Set(aChild.FindChild(TEXTURE_LABEL_FILE), theFileName);
867 TDataStd_Integer::Set(aChild.FindChild(TEXTURE_LABEL_WIDTH), theWidth);
868 TDataStd_Integer::Set(aChild.FindChild(TEXTURE_LABEL_HEIGHT), theHeight);
870 Handle(TDataStd_ByteArray) anAttr =
871 TDataStd_ByteArray::Set(aChild.FindChild(TEXTURE_LABEL_DATA),
872 theTexture.IsNull() ? 0 : theTexture->Lower(),
873 theTexture.IsNull() ? 0 : theTexture->Upper());
874 anAttr->ChangeArray(theTexture);
879 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
880 Handle(TColStd_HArray1OfByte) GEOM_Engine::getTexture(int theDocID, int theTextureID,
882 Handle(TDataStd_HArray1OfByte) GEOM_Engine::getTexture(int theDocID, int theTextureID,
884 int& theWidth, int& theHeight,
885 TCollection_AsciiString& theFileName)
887 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
888 Handle(TColStd_HArray1OfByte) anArray;
890 Handle(TDataStd_HArray1OfByte) anArray;
892 theWidth = theHeight = 0;
894 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
896 TDF_ChildIterator anIterator(aDoc->Main(), Standard_True);
898 for (; anIterator.More() && !found; anIterator.Next()) {
899 TDF_Label aTextureLabel = anIterator.Value();
900 if (aTextureLabel.IsAttribute( GetTextureGUID())) {
901 TDF_Label anIDLabel = aTextureLabel.FindChild(TEXTURE_LABEL_ID, Standard_False);
902 Handle(TDataStd_Integer) anIdAttr;
903 if(!anIDLabel.IsNull() && anIDLabel.FindAttribute(TDataStd_Integer::GetID(), anIdAttr) &&
904 anIdAttr->Get() == theTextureID) {
905 TDF_Label aFileLabel = aTextureLabel.FindChild(TEXTURE_LABEL_FILE, Standard_False);
906 TDF_Label aWidthLabel = aTextureLabel.FindChild(TEXTURE_LABEL_WIDTH, Standard_False);
907 TDF_Label aHeightLabel = aTextureLabel.FindChild(TEXTURE_LABEL_HEIGHT, Standard_False);
908 TDF_Label aDataLabel = aTextureLabel.FindChild(TEXTURE_LABEL_DATA, Standard_False);
909 Handle(TDataStd_Integer) aWidthAttr, aHeightAttr;
910 Handle(TDataStd_ByteArray) aTextureAttr;
911 Handle(TDataStd_Comment) aFileAttr;
912 if (!aWidthLabel.IsNull() && aWidthLabel.FindAttribute(TDataStd_Integer::GetID(), aWidthAttr) &&
913 !aHeightLabel.IsNull() && aHeightLabel.FindAttribute(TDataStd_Integer::GetID(), aHeightAttr) &&
914 !aDataLabel.IsNull() && aDataLabel.FindAttribute(TDataStd_ByteArray::GetID(), aTextureAttr)) {
915 theWidth = aWidthAttr->Get();
916 theHeight = aHeightAttr->Get();
917 anArray = aTextureAttr->InternalArray();
919 if (!aFileLabel.IsNull() && aFileLabel.FindAttribute(TDataStd_Comment::GetID(), aFileAttr))
920 theFileName = aFileAttr->Get();
928 std::list<int> GEOM_Engine::getAllTextures(int theDocID)
930 std::list<int> id_list;
932 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
934 TDF_ChildIterator anIterator(aDoc->Main(), Standard_True);
935 for (; anIterator.More(); anIterator.Next()) {
936 TDF_Label aTextureLabel = anIterator.Value();
937 if (aTextureLabel.IsAttribute( GetTextureGUID())) {
938 TDF_Label anIDLabel = aTextureLabel.FindChild(TEXTURE_LABEL_ID, Standard_False);
939 Handle(TDataStd_Integer) anIdAttr;
940 if(!anIDLabel.IsNull() && anIDLabel.FindAttribute(TDataStd_Integer::GetID(), anIdAttr))
941 id_list.push_back((int)anIdAttr->Get());
947 //===========================================================================
948 // Internal functions
949 //===========================================================================
951 //=============================================================================
953 * ProcessFunction: Dump fucntion description into script
955 //=============================================================================
956 bool ProcessFunction(Handle(GEOM_Function)& theFunction,
957 TCollection_AsciiString& theScript,
958 TCollection_AsciiString& theAfterScript,
959 const TVariablesList& theVariables,
960 const bool theIsPublished,
961 TDF_LabelMap& theProcessed,
962 std::set<TCollection_AsciiString>& theIgnoreObjs,
963 bool& theIsDumpCollected)
965 theIsDumpCollected = false;
966 if (theFunction.IsNull()) return false;
968 if (theProcessed.Contains(theFunction->GetEntry())) return false;
970 // pass functions, that depends on nonexisting ones
971 bool doNotProcess = false;
972 TDF_LabelSequence aSeq;
973 theFunction->GetDependency(aSeq);
974 Standard_Integer aLen = aSeq.Length();
975 for (Standard_Integer i = 1; i <= aLen && !doNotProcess; i++) {
976 TDF_Label aRefLabel = aSeq.Value(i);
977 Handle(TDF_Reference) aRef;
978 if (!aRefLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
982 if (aRef.IsNull() || aRef->Get().IsNull()) {
986 Handle(TDataStd_TreeNode) aT;
987 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
991 TDF_Label aDepLabel = aT->Label();
992 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aDepLabel);
994 if (aFunction.IsNull()) doNotProcess = true;
995 else if (!theProcessed.Contains(aDepLabel)) doNotProcess = true;
1002 TCollection_AsciiString anObjEntry;
1003 TDF_Tool::Entry(theFunction->GetOwnerEntry(), anObjEntry);
1004 theIgnoreObjs.insert(anObjEntry);
1007 theProcessed.Add(theFunction->GetEntry());
1009 TCollection_AsciiString aDescr = theFunction->GetDescription();
1010 if(aDescr.Length() == 0) return false;
1012 //Check if its internal function which doesn't requires dumping
1013 if(aDescr == "None") return false;
1015 // 0020001 PTv, check for critical functions, which require dump of objects
1018 // currently, there is only one function "RestoreGivenSubShapes",
1019 // later this check could be replaced by iterations on list of such functions
1020 if (aDescr.Search( "RestoreGivenSubShapes" ) != -1)
1021 theIsDumpCollected = true;
1022 else if (aDescr.Search( "RestoreSubShapes" ) != -1)
1023 theIsDumpCollected = true;
1026 //Replace parameter by notebook variables
1027 ReplaceVariables(aDescr,theVariables);
1028 if ( theIsDumpCollected ) {
1030 bool isBefore = true;
1031 TCollection_AsciiString aSubStr = aDescr.Token("\n\t", i++);
1032 while (!aSubStr.IsEmpty()) {
1034 aSubStr.Search( "RestoreGivenSubShapes" ) == -1 &&
1035 aSubStr.Search( "RestoreSubShapes" ) == -1)
1036 theScript += TCollection_AsciiString("\n\t") + aSubStr;
1038 theAfterScript += TCollection_AsciiString("\n\t") + aSubStr;
1039 aSubStr = aDescr.Token("\n\t", i++);
1043 theScript += "\n\t";
1044 theScript += aDescr;
1049 //=============================================================================
1051 * FindEntries: Returns a sequence of start/end positions of entries in the string
1053 //=============================================================================
1054 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString)
1056 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
1057 Standard_Integer aLen = theString.Length();
1058 Standard_Boolean isFound = Standard_False;
1060 const char* arr = theString.ToCString();
1061 Standard_Integer i = 0, j;
1064 int c = (int)arr[i];
1066 if(c >= 48 && c <= 57) { //Is digit?
1068 isFound = Standard_False;
1069 while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
1071 if(c == 58) isFound = Standard_True;
1074 if(isFound && arr[j-2] != 58) { // last char should be a diggit
1075 aSeq->Append(i+1); // +1 because AsciiString starts from 1
1086 //=============================================================================
1088 * ReplaceVariables: Replace parameters of the function by variales from
1091 //=============================================================================
1092 void ReplaceVariables(TCollection_AsciiString& theCommand,
1093 const TVariablesList& theVariables)
1096 cout<<"Command : "<<theCommand<<endl;
1099 cout<<"All Entries:"<<endl;
1100 TVariablesList::const_iterator it = theVariables.begin();
1101 for(;it != theVariables.end();it++)
1102 cout<<"\t'"<<(*it).first<<"'"<<endl;
1105 //Additional case - multi-row commands
1106 int aCommandIndex = 1;
1107 while( aCommandIndex < 10 ) { // tmp check
1108 TCollection_AsciiString aCommand = theCommand.Token("\n",aCommandIndex);
1109 if( aCommand.Length() == 0 )
1113 cout<<"Sub-command : "<<aCommand<<endl;
1115 Standard_Integer aStartCommandPos = theCommand.Location(aCommand,1,theCommand.Length());
1116 Standard_Integer aEndCommandPos = aStartCommandPos + aCommand.Length();
1118 //Get Entry of the result object
1119 TCollection_AsciiString anEntry;
1120 if( aCommand.Search("=") != -1 ) // command returns an object
1121 anEntry = aCommand.Token("=",1);
1122 else { // command modifies the object
1123 if (int aStartEntryPos = aCommand.Location(1,'(',1,aCommand.Length()))
1124 if (int aEndEntryPos = aCommand.Location(1,',',aStartEntryPos,aCommand.Length()))
1125 anEntry = aCommand.SubString(aStartEntryPos+1, aEndEntryPos-1);
1127 //Remove white spaces
1128 anEntry.RightAdjust();
1129 anEntry.LeftAdjust();
1131 cout<<"Result entry : '" <<anEntry<<"'"<<endl;
1133 if ( anEntry.IsEmpty() ) {
1138 //Check if result is list of entries - enough to get the first entry in this case
1140 if( anEntry.Value( 1 ) == O_SQR_BRACKET && anEntry.Value( anEntry.Length() ) == C_SQR_BRACKET ) {
1141 while(anEntry.Location(aNbEntries,COMMA,1,anEntry.Length()))
1143 TCollection_AsciiString aSeparator(COMMA);
1144 anEntry = anEntry.Token(aSeparator.ToCString(),1);
1145 anEntry.Remove( 1, 1 );
1146 anEntry.RightAdjust();
1147 anEntry.LeftAdjust();
1149 cout<<"Sub-entry : '" <<anEntry<<"'"<<endl;
1152 //Find variables used for object construction
1153 ObjectStates* aStates = 0;
1154 TVariablesList::const_iterator it = theVariables.find(anEntry);
1155 if( it != theVariables.end() )
1156 aStates = (*it).second;
1160 cout<<"Valiables list empty!!!"<<endl;
1165 TState aVariables = aStates->GetCurrectState();
1168 cout<<"Variables from SObject:"<<endl;
1169 for (int i = 0; i < aVariables.size();i++)
1170 cout<<"\t Variable["<<i<<"] = "<<aVariables[i].myVariable<<endl;
1173 //Calculate total number of parameters
1174 Standard_Integer aTotalNbParams = 1;
1175 while(aCommand.Location(aTotalNbParams,COMMA,1,aCommand.Length()))
1179 cout<<"aTotalNbParams = "<<aTotalNbParams<<endl;
1181 Standard_Integer aFirstParam = aNbEntries;
1183 //Replace parameters by variables
1184 Standard_Integer aStartPos = 0;
1185 Standard_Integer aEndPos = 0;
1187 TCollection_AsciiString aVar, aReplacedVar;
1188 for(Standard_Integer i=aFirstParam;i <= aTotalNbParams;i++) {
1189 //Replace first parameter (bettwen '(' character and first ',' character)
1190 if(i == aFirstParam)
1192 aStartPos = aCommand.Location(O_BRACKET, 1, aCommand.Length()) + 1;
1193 if(aTotalNbParams - aNbEntries > 0 )
1194 aEndPos = aCommand.Location(aFirstParam, COMMA, 1, aCommand.Length());
1196 aEndPos = aCommand.Location(C_BRACKET, 1, aCommand.Length());
1198 //Replace last parameter (bettwen ',' character and ')' character)
1199 else if(i == aTotalNbParams)
1201 aStartPos = aCommand.Location(i-1, COMMA, 1, aCommand.Length()) + 2;
1202 aEndPos = aCommand.Location(C_BRACKET, aStartPos , aCommand.Length());
1204 //Replace other parameters (bettwen two ',' characters)
1205 else if(i != aFirstParam && i != aTotalNbParams )
1207 aStartPos = aCommand.Location(i-1, COMMA, 1, aCommand.Length()) + 2;
1208 aEndPos = aCommand.Location(i, COMMA, 1, aCommand.Length());
1211 if( aCommand.Value( aStartPos ) == O_SQR_BRACKET )
1213 if( aCommand.Value( aEndPos-1 ) == C_SQR_BRACKET )
1215 if ( aStartPos == aEndPos )
1216 continue; // PAL20889: for "[]"
1219 cout<<"aStartPos = "<<aStartPos<<", aEndPos = "<<aEndPos<<endl;
1221 aVar = aCommand.SubString(aStartPos, aEndPos-1);
1226 cout<<"Variable: '"<< aVar <<"'"<<endl;
1228 // specific case for sketcher
1229 if(aVar.Location( TCollection_AsciiString("Sketcher:"), 1, aVar.Length() ) != 0) {
1230 Standard_Integer aNbSections = 1;
1231 while( aVar.Location( aNbSections, ':', 1, aVar.Length() ) )
1235 int aStartSectionPos = 0, aEndSectionPos = 0;
1236 TCollection_AsciiString aSection, aReplacedSection;
1237 for(Standard_Integer aSectionIndex = 1; aSectionIndex <= aNbSections; aSectionIndex++) {
1238 aStartSectionPos = aVar.Location( aSectionIndex, ':', 1, aVar.Length() ) + 1;
1239 if( aSectionIndex != aNbSections )
1240 aEndSectionPos = aVar.Location( aSectionIndex + 1, ':', 1, aVar.Length() );
1242 aEndSectionPos = aVar.Length();
1244 aSection = aVar.SubString(aStartSectionPos, aEndSectionPos-1);
1246 cout<<"aSection: "<<aSection<<endl;
1248 Standard_Integer aNbParams = 1;
1249 while( aSection.Location( aNbParams, ' ', 1, aSection.Length() ) )
1253 int aStartParamPos = 0, aEndParamPos = 0;
1254 TCollection_AsciiString aParameter, aReplacedParameter;
1255 for(Standard_Integer aParamIndex = 1; aParamIndex <= aNbParams; aParamIndex++) {
1256 aStartParamPos = aSection.Location( aParamIndex, ' ', 1, aSection.Length() ) + 1;
1257 if( aParamIndex != aNbParams )
1258 aEndParamPos = aSection.Location( aParamIndex + 1, ' ', 1, aSection.Length() );
1260 aEndParamPos = aSection.Length() + 1;
1263 cout<<"aParamIndex: "<<aParamIndex<<" aStartParamPos: " <<aStartParamPos<<" aEndParamPos: "<<aEndParamPos<<endl;
1265 if ( aStartParamPos == aEndParamPos)
1268 aParameter = aSection.SubString(aStartParamPos, aEndParamPos-1);
1270 cout<<"aParameter: "<<aParameter<<endl;
1272 if(iVar >= aVariables.size())
1275 aReplacedParameter = aVariables[iVar].myVariable;
1276 if(aReplacedParameter.IsEmpty()) {
1281 if(aVariables[iVar].isVariable) {
1282 aReplacedParameter.InsertBefore(1,"'");
1283 aReplacedParameter.InsertAfter(aReplacedParameter.Length(),"'");
1287 cout<<"aSection before : "<<aSection<<endl;
1288 aSection.Remove(aStartParamPos, aEndParamPos - aStartParamPos);
1289 aSection.Insert(aStartParamPos, aReplacedParameter);
1291 cout<<"aSection after : "<<aSection<<endl<<endl;
1295 cout<<"aVar before : "<<aVar<<endl;
1296 aVar.Remove(aStartSectionPos, aEndSectionPos - aStartSectionPos);
1297 aVar.Insert(aStartSectionPos, aSection);
1299 cout<<"aVar after : "<<aVar<<endl<<endl;
1303 cout<<"aCommand before : "<<aCommand<<endl;
1304 aCommand.Remove(aStartPos, aEndPos - aStartPos);
1305 aCommand.Insert(aStartPos, aVar);
1307 cout<<"aCommand after : "<<aCommand<<endl;
1310 } // end of specific case for sketcher
1312 //If parameter is entry or 'None', skip it
1313 if(theVariables.find(aVar) != theVariables.end() || aVar.Search(":") != -1 || aVar == PY_NULL)
1316 if(iVar >= aVariables.size())
1319 aReplacedVar = aVariables[iVar].myVariable;
1320 if(aReplacedVar.IsEmpty()) {
1325 if(aVariables[iVar].isVariable) {
1326 aReplacedVar.InsertBefore(1,"\"");
1327 aReplacedVar.InsertAfter(aReplacedVar.Length(),"\"");
1330 aCommand.Remove(aStartPos, aEndPos - aStartPos);
1331 aCommand.Insert(aStartPos, aReplacedVar);
1335 theCommand.Remove(aStartCommandPos, aEndCommandPos - aStartCommandPos);
1336 theCommand.Insert(aStartCommandPos, aCommand);
1340 aStates->IncrementState();
1344 cout<<"Command : "<<theCommand<<endl;
1347 //=============================================================================
1349 * ReplaceEntriesByNames: Replace object entries by their names
1351 //=============================================================================
1352 void ReplaceEntriesByNames (TCollection_AsciiString& theScript,
1353 TSting2ObjDataMap& aEntry2ObjData,
1354 const bool theIsPublished,
1355 TColStd_SequenceOfAsciiString& theObjListToPublish,
1356 Standard_Integer& objectCounter,
1357 Resource_DataMapOfAsciiStringAsciiString& aNameToEntry)
1359 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(theScript);
1360 Standard_Integer aLen = aSeq->Length(), aStart = 1, aScriptLength = theScript.Length();
1362 //Replace entries by the names
1363 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("geomObj_"),
1364 allowedChars ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
1365 if (aLen == 0) anUpdatedScript = theScript;
1367 for (Standard_Integer i = 1; i <= aLen; i+=2) {
1368 anUpdatedScript += theScript.SubString(aStart, aSeq->Value(i)-1);
1369 anEntry = theScript.SubString(aSeq->Value(i), aSeq->Value(i+1));
1370 theObjListToPublish.Append( anEntry );
1372 TObjectData& data = aEntry2ObjData[ anEntry ];
1373 if ( data._pyName.IsEmpty() ) { // encounted for the 1st time
1374 if ( !data._name.IsEmpty() ) { // published object
1375 data._pyName = data._name;
1376 healPyName( data._pyName, anEntry, aNameToEntry);
1380 data._pyName = aBaseName + TCollection_AsciiString(++objectCounter);
1381 } while(aNameToEntry.IsBound(data._pyName));
1385 aNameToEntry.Bind(data._pyName, anEntry); // to detect same name of diff objects
1387 anUpdatedScript += data._pyName;
1388 aStart = aSeq->Value(i+1) + 1;
1391 //Add final part of the script
1392 if (aLen && aSeq->Value(aLen) < aScriptLength)
1393 anUpdatedScript += theScript.SubString(aSeq->Value(aLen)+1, aScriptLength); // mkr : IPAL11865
1395 theScript = anUpdatedScript;
1398 //=============================================================================
1400 * AddObjectColors: Add color to objects
1402 //=============================================================================
1403 void AddObjectColors (int theDocID,
1404 TCollection_AsciiString& theScript,
1405 const TSting2ObjDataMap& theEntry2ObjData)
1407 GEOM_Engine* engine = GEOM_Engine::GetEngine();
1408 Handle(TDocStd_Document) aDoc = engine->GetDocument(theDocID);
1410 TSting2ObjDataMap::const_iterator anEntryToNameIt;
1411 for (anEntryToNameIt = theEntry2ObjData.begin();
1412 anEntryToNameIt!= theEntry2ObjData.end();
1415 const TCollection_AsciiString& aEntry = anEntryToNameIt->first;
1416 const TCollection_AsciiString& aName = anEntryToNameIt->second._pyName;
1419 TDF_Tool::Label( aDoc->GetData(), aEntry, L );
1423 Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
1427 bool anAutoColor = obj->GetAutoColor();
1430 TCollection_AsciiString aCommand( "\n\t" );
1431 aCommand += aName + ".SetAutoColor(1)";
1432 theScript += aCommand.ToCString();
1435 GEOM_Object::Color aColor = obj->GetColor();
1436 if ( aColor.R >= 0 && aColor.G >= 0 && aColor.B >= 0 )
1438 TCollection_AsciiString aCommand( "\n\t" );
1439 aCommand += aName + ".SetColor(SALOMEDS.Color(" + aColor.R + "," + aColor.G + "," + aColor.B + "))";
1440 theScript += aCommand.ToCString();
1443 Aspect_TypeOfMarker aMarkerType = obj->GetMarkerType();
1444 if (aMarkerType >= Aspect_TOM_POINT && aMarkerType < Aspect_TOM_USERDEFINED) {
1445 TCollection_AsciiString aCommand( "\n\t" );
1446 aCommand += aName + ".SetMarkerStd(";
1447 switch (aMarkerType) {
1448 case Aspect_TOM_POINT: aCommand += "GEOM.MT_POINT"; break;
1449 case Aspect_TOM_PLUS: aCommand += "GEOM.MT_PLUS"; break;
1450 case Aspect_TOM_STAR: aCommand += "GEOM.MT_STAR"; break;
1451 case Aspect_TOM_O: aCommand += "GEOM.MT_O"; break;
1452 case Aspect_TOM_X: aCommand += "GEOM.MT_X"; break;
1453 case Aspect_TOM_O_POINT: aCommand += "GEOM.MT_O_POINT"; break;
1454 case Aspect_TOM_O_PLUS: aCommand += "GEOM.MT_O_PLUS"; break;
1455 case Aspect_TOM_O_STAR: aCommand += "GEOM.MT_O_STAR"; break;
1456 case Aspect_TOM_O_X: aCommand += "GEOM.MT_O_X"; break;
1457 case Aspect_TOM_BALL: aCommand += "GEOM.MT_BALL"; break;
1458 case Aspect_TOM_RING1: aCommand += "GEOM.MT_RING1"; break;
1459 case Aspect_TOM_RING2: aCommand += "GEOM.MT_RING2"; break;
1460 case Aspect_TOM_RING3: aCommand += "GEOM.MT_RING3"; break;
1461 default: aCommand += "GEOM.MT_NONE"; break; // just for completeness, should not get here
1464 int aSize = (int)( obj->GetMarkerSize()/0.5 ) - 1;
1466 case 1: aCommand += "GEOM.MS_10"; break;
1467 case 2: aCommand += "GEOM.MS_15"; break;
1468 case 3: aCommand += "GEOM.MS_20"; break;
1469 case 4: aCommand += "GEOM.MS_25"; break;
1470 case 5: aCommand += "GEOM.MS_30"; break;
1471 case 6: aCommand += "GEOM.MS_35"; break;
1472 case 7: aCommand += "GEOM.MS_40"; break;
1473 case 8: aCommand += "GEOM.MS_45"; break;
1474 case 9: aCommand += "GEOM.MS_50"; break;
1475 case 10: aCommand += "GEOM.MS_55"; break;
1476 case 11: aCommand += "GEOM.MS_60"; break;
1477 case 12: aCommand += "GEOM.MS_65"; break;
1478 case 13: aCommand += "GEOM.MS_70"; break;
1479 default: aCommand += "GEOM.MS_NONE"; break;
1482 theScript += aCommand.ToCString();
1484 else if (aMarkerType == Aspect_TOM_USERDEFINED) {
1485 int aMarkerTextureID = obj->GetMarkerTexture();
1486 if (aMarkerTextureID >= 0) {
1487 TCollection_AsciiString aCommand( "\n\t" );
1488 aCommand += aName + ".SetMarkerTexture(texture_map[";
1489 aCommand += aMarkerTextureID;
1491 theScript += aCommand.ToCString();
1497 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
1498 static TCollection_AsciiString pack_data (const Handle(TColStd_HArray1OfByte)& aData)
1500 static TCollection_AsciiString pack_data (const Handle(TDataStd_HArray1OfByte)& aData)
1503 TCollection_AsciiString stream;
1504 if (!aData.IsNull()) {
1505 for (Standard_Integer i = aData->Lower(); i <= aData->Upper(); i++) {
1506 Standard_Byte byte = aData->Value(i);
1507 TCollection_AsciiString strByte = "";
1508 for (int j = 0; j < 8; j++)
1509 strByte.Prepend((byte & (1<<j)) ? "1" : "0");
1516 void AddTextures (int theDocID, TCollection_AsciiString& theScript)
1518 GEOM_Engine* engine = GEOM_Engine::GetEngine();
1519 std::list<int> allTextures = engine->getAllTextures(theDocID);
1520 std::list<int>::const_iterator it;
1522 if (allTextures.size() > 0) {
1523 theScript += "\n\ttexture_map = {}\n";
1525 for (it = allTextures.begin(); it != allTextures.end(); ++it) {
1526 if (*it <= 0) continue;
1527 Standard_Integer aWidth, aHeight;
1528 TCollection_AsciiString aFileName;
1529 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
1530 Handle(TColStd_HArray1OfByte) aTexture =
1532 Handle(TDataStd_HArray1OfByte) aTexture =
1534 engine->getTexture(theDocID, *it, aWidth, aHeight, aFileName);
1535 if (aWidth > 0 && aHeight > 0 && !aTexture.IsNull() && aTexture->Length() > 0 ) {
1536 TCollection_AsciiString aCommand = "\n\t";
1537 aCommand += "texture_map["; aCommand += *it; aCommand += "] = ";
1538 if (aFileName != "" ) {
1539 aCommand += "geompy.LoadTexture(\"";
1540 aCommand += aFileName.ToCString();
1544 aCommand += "geompy.AddTexture(";
1545 aCommand += aWidth; aCommand += ", "; aCommand += aHeight; aCommand += ", \"";
1546 aCommand += pack_data(aTexture);
1549 theScript += aCommand;
1556 //=============================================================================
1558 * PublishObject: publish object in study script
1560 //=============================================================================
1561 void PublishObject (TObjectData& theObjectData,
1562 TSting2ObjDataMap& theEntry2ObjData,
1563 const TSting2ObjDataPtrMap& theStEntry2ObjDataPtr,
1564 Resource_DataMapOfAsciiStringAsciiString& theNameToEntry,
1565 std::map< int, TCollection_AsciiString >& theEntryToCmdMap,
1566 std::set< TCollection_AsciiString>& theIgnoreMap)
1568 if ( theObjectData._studyEntry.IsEmpty() )
1569 return; // was not published
1570 if ( theIgnoreMap.count( theObjectData._entry ) )
1571 return; // not to publish
1573 TCollection_AsciiString aCommand("\n\tgeompy.");
1575 // find a father entry
1576 TObjectData* aFatherData = 0;
1577 TCollection_AsciiString aFatherStudyEntry =
1578 theObjectData._studyEntry.SubString( 1, theObjectData._studyEntry.SearchFromEnd(":") - 1 );
1579 TSting2ObjDataPtrMap::const_iterator stEntry2DataPtr =
1580 theStEntry2ObjDataPtr.find( aFatherStudyEntry );
1581 if ( stEntry2DataPtr != theStEntry2ObjDataPtr.end() )
1582 aFatherData = stEntry2DataPtr->second;
1584 const int geomObjDepth = 3;
1586 // treat multiply published object
1587 if ( theObjectData._pyName.IsEmpty() )
1589 TObjectData& data0 = theEntry2ObjData[ theObjectData._entry ];
1590 if ( data0._pyName.IsEmpty() ) return; // something wrong
1592 theObjectData._pyName = theObjectData._name;
1593 healPyName( theObjectData._pyName, theObjectData._entry, theNameToEntry);
1595 TCollection_AsciiString aCreationCommand("\n\t");
1596 aCreationCommand += theObjectData._pyName + " = " + data0._pyName;
1598 // store aCreationCommand before publishing commands
1599 int tag = theObjectData._entry.Token( ":", geomObjDepth ).IntegerValue();
1600 theEntryToCmdMap.insert( std::make_pair( tag + 2*theEntry2ObjData.size(), aCreationCommand ));
1604 if ( aFatherData && !aFatherData->_pyName.IsEmpty() ) {
1605 aCommand += "addToStudyInFather( ";
1606 aCommand += aFatherData->_pyName + ", ";
1609 aCommand += "addToStudy( ";
1611 aCommand += theObjectData._pyName + ", '" + theObjectData._name + "' )";
1613 // bind a command to the study entry
1614 int tag = theObjectData._entry.Token( ":", geomObjDepth ).IntegerValue();
1615 theEntryToCmdMap.insert( std::make_pair( tag, aCommand ));
1617 theObjectData._studyEntry.Clear(); // not to publish any more
1620 //================================================================================
1622 * \brief Constructor
1624 //================================================================================
1625 ObjectStates::ObjectStates()
1630 //================================================================================
1634 //================================================================================
1635 ObjectStates::~ObjectStates()
1639 //================================================================================
1641 * \brief Return current object state
1642 * \retval state - Object state (vector of notebook variable)
1644 //================================================================================
1645 TState ObjectStates::GetCurrectState() const
1647 if(_states.size() > _dumpstate)
1648 return _states[_dumpstate];
1652 //================================================================================
1654 * \brief Add new object state
1655 * \param theState - Object state (vector of notebook variable)
1657 //================================================================================
1658 void ObjectStates::AddState(const TState &theState)
1660 _states.push_back(theState);
1663 //================================================================================
1665 * \brief Increment object state
1667 //================================================================================
1668 void ObjectStates::IncrementState()