1 // Copyright (C) 2007-2010 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 "utilities.h"
38 #include <Basics_Utils.hxx>
40 #include <TDF_Tool.hxx>
41 #include <TDF_Data.hxx>
42 #include <TDF_Reference.hxx>
43 #include <TDF_LabelSequence.hxx>
44 #include <TDataStd_Integer.hxx>
45 #include <TDataStd_ChildNodeIterator.hxx>
46 #include <TFunction_Driver.hxx>
47 #include <TFunction_DriverTable.hxx>
48 #include <TDataStd_HArray1OfByte.hxx>
49 #include <TDataStd_ByteArray.hxx>
50 #include <TDataStd_UAttribute.hxx>
51 #include <TDF_ChildIterator.hxx>
52 #include <TDataStd_Comment.hxx>
55 #include <TopTools_IndexedMapOfShape.hxx>
57 #include <TCollection_AsciiString.hxx>
58 #include <TCollection_ExtendedString.hxx>
59 #include <TColStd_SequenceOfAsciiString.hxx>
60 #include <TColStd_MapOfTransient.hxx>
61 #include <TColStd_HSequenceOfInteger.hxx>
63 #include <Interface_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
64 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
68 #include <Standard_Failure.hxx>
69 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
74 #define O_SQR_BRACKET '['
75 #define C_SQR_BRACKET ']'
76 #define PY_NULL "None"
79 static int MYDEBUG = 0;
81 static int MYDEBUG = 0;
84 typedef std::map< TCollection_AsciiString, TCollection_AsciiString > TSting2StringMap;
85 typedef std::map< TCollection_AsciiString, TObjectData > TSting2ObjDataMap;
86 typedef std::map< TCollection_AsciiString, TObjectData* > TSting2ObjDataPtrMap;
88 static GEOM_Engine* TheEngine = NULL;
91 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_Object)& theObject)
93 TCollection_AsciiString anID(theObject->GetDocID()), anEntry;
94 TDF_Tool::Entry(theObject->GetEntry(), anEntry);
95 anID+=(TCollection_AsciiString("_")+anEntry);
99 static TCollection_AsciiString BuildID(Standard_Integer theDocID, char* theEntry)
101 TCollection_AsciiString anID(theDocID);
102 anID+=(TCollection_AsciiString("_")+theEntry);
106 static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
108 TCollection_AsciiString aDocID = theID.Token("_");
109 if(aDocID.Length() < 1) return -1;
110 return aDocID.IntegerValue();
113 bool ProcessFunction(Handle(GEOM_Function)& theFunction,
114 TCollection_AsciiString& theScript,
115 TCollection_AsciiString& theAfterScript,
116 const TVariablesList& theVariables,
117 const bool theIsPublished,
118 TDF_LabelMap& theProcessed,
119 std::set<TCollection_AsciiString>& theIgnoreObjs,
120 bool& theIsDumpCollected);
122 void ReplaceVariables(TCollection_AsciiString& theCommand,
123 const TVariablesList& theVariables);
125 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString);
127 void ReplaceEntriesByNames (TCollection_AsciiString& theScript,
128 TSting2ObjDataMap& aEntry2ObjData,
129 const bool theIsPublished,
130 TColStd_SequenceOfAsciiString& theObjListToPublish,
131 Standard_Integer& objectCounter,
132 Resource_DataMapOfAsciiStringAsciiString& aNameToEntry);
134 void AddObjectColors (int theDocID,
135 TCollection_AsciiString& theScript,
136 const TSting2ObjDataMap& theEntry2ObjData);
138 void AddTextures (int theDocID, TCollection_AsciiString& theScript);
140 void PublishObject (TObjectData& theObjectData,
141 TSting2ObjDataMap& theEntry2ObjData,
142 const TSting2ObjDataPtrMap& theStEntry2ObjDataPtr,
143 Resource_DataMapOfAsciiStringAsciiString& theNameToEntry,
144 TSting2StringMap& theEntryToCmdMap,
145 std::set<TCollection_AsciiString>& theMapOfPublished);
149 //================================================================================
151 * \brief Fix up the name of python variable
153 //================================================================================
155 void healPyName( TCollection_AsciiString& pyName,
156 const TCollection_AsciiString& anEntry,
157 Resource_DataMapOfAsciiStringAsciiString& aNameToEntry)
159 const TCollection_AsciiString allowedChars
160 ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
162 if ( pyName.IsIntegerValue() ) { // pyName must not start with a digit
163 pyName.Insert( 1, 'a' );
165 int p, p2=1; // replace not allowed chars
166 while ((p = pyName.FirstLocationNotInSet(allowedChars, p2, pyName.Length()))) {
167 pyName.SetValue(p, '_');
170 if ( aNameToEntry.IsBound( pyName ) && anEntry != aNameToEntry( pyName ))
171 { // diff objects have same name - make a new name by appending a digit
172 TCollection_AsciiString aName2;
173 Standard_Integer i = 0;
175 aName2 = pyName + "_" + ++i;
176 } while ( aNameToEntry.IsBound( aName2 ) && anEntry != aNameToEntry( aName2 ));
182 //=======================================================================
183 //function : GetTextureGUID
185 //=======================================================================
186 const Standard_GUID& GEOM_Engine::GetTextureGUID()
188 static Standard_GUID anID("FF1BBB01-5D14-4df2-980B-3A668264EA17");
192 //=============================================================================
196 //=============================================================================
197 GEOM_Engine* GEOM_Engine::GetEngine() { return TheEngine; }
199 //=============================================================================
203 //=============================================================================
204 void GEOM_Engine::SetEngine(GEOM_Engine* theEngine) { TheEngine = theEngine; }
206 //=============================================================================
210 //=============================================================================
211 GEOM_Engine::GEOM_Engine()
213 TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver());
215 _OCAFApp = new GEOM_Application();
222 GEOM_Engine::~GEOM_Engine()
224 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It(_objects);
225 std::list< Handle(GEOM_Object) > objs;
226 for(; It.More(); It.Next())
227 objs.push_back( Handle(GEOM_Object)::DownCast(It.Value()) );
228 std::list< Handle(GEOM_Object) >::iterator objit;
229 for(objit = objs.begin(); objit != objs.end(); ++objit)
230 RemoveObject(*objit);
232 //Close all documents not closed
233 for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
236 _mapIDDocument.Clear();
240 //=============================================================================
244 //=============================================================================
245 Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID, bool force)
247 Handle(TDocStd_Document) aDoc;
248 if(_mapIDDocument.IsBound(theDocID)) {
249 aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
252 _OCAFApp->NewDocument("SALOME_GEOM", aDoc);
253 aDoc->SetUndoLimit(_UndoLimit);
254 _mapIDDocument.Bind(theDocID, aDoc);
255 TDataStd_Integer::Set(aDoc->Main(), theDocID);
260 //=============================================================================
264 //=============================================================================
265 int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument)
267 if(theDocument.IsNull()) return -1;
268 for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
269 if(anItr.Value() == theDocument) return anItr.Key();
275 //=============================================================================
279 //=============================================================================
280 Handle(GEOM_Object) GEOM_Engine::GetObject(int theDocID, char* theEntry, bool force)
282 Handle(GEOM_Object) anObject;
284 TCollection_AsciiString anID = BuildID(theDocID, theEntry);
286 if (_objects.IsBound(anID)) {
287 anObject = Handle(GEOM_Object)::DownCast(_objects(anID));
290 Handle(TDocStd_Document) aDoc = GetDocument(theDocID, force);
291 if ( !aDoc.IsNull()) {
293 TDF_Tool::Label(aDoc->Main().Data(), theEntry, aLabel, Standard_True);
294 anObject = new GEOM_Object(aLabel);
295 _objects.Bind(anID, anObject);
302 //=============================================================================
306 //=============================================================================
307 Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
309 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
310 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
312 // NPAL18604: use existing label to decrease memory usage,
313 // if this label has been freed (object deleted)
314 bool useExisting = false;
316 if (_freeLabels.find(theDocID) != _freeLabels.end()) {
317 std::list<TDF_Label>& aFreeLabels = _freeLabels[theDocID];
318 if (!aFreeLabels.empty()) {
320 aChild = aFreeLabels.front();
321 aFreeLabels.pop_front();
326 aChild = TDF_TagSource::NewChild(aDoc->Main());
329 Handle(GEOM_Object) anObject = new GEOM_Object(aChild, theType);
331 //Put an object in the map of created objects
332 TCollection_AsciiString anID = BuildIDFromObject(anObject);
333 if(_objects.IsBound(anID)) _objects.UnBind(anID);
334 _objects.Bind(anID, anObject);
339 //=============================================================================
343 //=============================================================================
344 Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape,
345 Handle(TColStd_HArray1OfInteger) theIndices,
346 bool isStandaloneOperation)
348 if (theMainShape.IsNull() || theIndices.IsNull()) return NULL;
350 Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID());
351 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
353 // NPAL18604: use existing label to decrease memory usage,
354 // if this label has been freed (object deleted)
355 bool useExisting = false;
357 int aDocID = theMainShape->GetDocID();
358 if (_freeLabels.find(aDocID) != _freeLabels.end()) {
359 std::list<TDF_Label>& aFreeLabels = _freeLabels[aDocID];
360 if (!aFreeLabels.empty()) {
362 aChild = aFreeLabels.front();
363 aFreeLabels.pop_front();
368 aChild = TDF_TagSource::NewChild(aDoc->Main());
371 Handle(GEOM_Function) aMainShape = theMainShape->GetLastFunction();
372 Handle(GEOM_Object) anObject = new GEOM_Object (aChild, 28); //28 is SUBSHAPE type
373 Handle(GEOM_Function) aFunction = anObject->AddFunction(GEOM_Object::GetSubShapeID(), 1);
375 GEOM_ISubShape aSSI (aFunction);
376 aSSI.SetMainShape(aMainShape);
377 aSSI.SetIndices(theIndices);
380 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
383 GEOM_Solver aSolver (GEOM_Engine::GetEngine());
384 if (!aSolver.ComputeFunction(aFunction)) {
385 MESSAGE("GEOM_Engine::AddSubShape Error: Can't build a sub shape");
389 catch (Standard_Failure) {
390 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
391 MESSAGE("GEOM_Engine::AddSubShape Error: " << aFail->GetMessageString());
395 // Put an object in the map of created objects
396 TCollection_AsciiString anID = BuildIDFromObject(anObject);
397 if (_objects.IsBound(anID)) _objects.UnBind(anID);
398 _objects.Bind(anID, anObject);
400 // Put this subshape in the list of subshapes of theMainShape
401 aMainShape->AddSubShapeReference(aFunction);
403 GEOM::TPythonDump pd (aFunction);
405 if (isStandaloneOperation) {
406 pd << anObject << " = geompy.GetSubShape(" << theMainShape << ", [";
407 Standard_Integer i = theIndices->Lower(), up = theIndices->Upper();
408 for (; i <= up - 1; i++) {
409 pd << theIndices->Value(i) << ", ";
411 pd << theIndices->Value(up) << "])";
419 //=============================================================================
423 //=============================================================================
424 bool GEOM_Engine::RemoveObject(Handle(GEOM_Object) theObject)
426 if (theObject.IsNull()) return false;
428 int aDocID = theObject->GetDocID();
429 if(!_mapIDDocument.IsBound(aDocID))
430 return false; // document is closed...
432 //Remove an object from the map of available objects
433 TCollection_AsciiString anID = BuildIDFromObject(theObject);
434 if (_objects.IsBound(anID)) _objects.UnBind(anID);
436 // If subshape, remove it from the list of subshapes of its main shape
437 if (!theObject->IsMainShape()) {
438 Handle(GEOM_Function) aFunction = theObject->GetFunction(1);
439 GEOM_ISubShape aSSI (aFunction);
440 Handle(GEOM_Function) aMainShape = aSSI.GetMainShape();
441 //If main shape is not null, then remove
442 if(!aMainShape.IsNull())
443 aMainShape->RemoveSubShapeReference(aFunction);
446 int nb = theObject->GetNbFunctions();
447 Handle(TDataStd_TreeNode) aNode;
448 for (int i = 1; i <= nb; i++) {
449 Handle(GEOM_Function) aFunction = theObject->GetFunction(i);
450 if (aFunction->GetEntry().FindAttribute(GEOM_Function::GetFunctionTreeID(), aNode))
454 TDF_Label aLabel = theObject->GetEntry();
455 aLabel.ForgetAllAttributes(Standard_True);
457 // Remember the label to reuse it then
458 std::list<TDF_Label>& aFreeLabels = _freeLabels[aDocID];
459 aFreeLabels.push_back(aLabel);
466 //=============================================================================
470 //=============================================================================
471 void GEOM_Engine::Undo(int theDocID)
473 GetDocument(theDocID)->Undo();
476 //=============================================================================
480 //=============================================================================
481 void GEOM_Engine::Redo(int theDocID)
483 GetDocument(theDocID)->Redo();
486 //=============================================================================
490 //=============================================================================
491 bool GEOM_Engine::Save(int theDocID, char* theFileName)
493 if(!_mapIDDocument.IsBound(theDocID)) return false;
494 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
496 _OCAFApp->SaveAs(aDoc, theFileName);
501 //=============================================================================
505 //=============================================================================
506 bool GEOM_Engine::Load(int theDocID, char* theFileName)
508 Handle(TDocStd_Document) aDoc;
509 if(_OCAFApp->Open(theFileName, aDoc) != CDF_RS_OK) {
513 aDoc->SetUndoLimit(_UndoLimit);
515 if(_mapIDDocument.IsBound(theDocID)) _mapIDDocument.UnBind(theDocID);
516 _mapIDDocument.Bind(theDocID, aDoc);
518 TDataStd_Integer::Set(aDoc->Main(), theDocID);
523 //=============================================================================
527 //=============================================================================
528 void GEOM_Engine::Close(int theDocID)
530 if (_mapIDDocument.IsBound(theDocID)) {
531 Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
533 //Remove all GEOM Objects associated to the given document
534 TColStd_SequenceOfAsciiString aSeq;
535 GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It (_objects);
536 for (; It.More(); It.Next()) {
537 TCollection_AsciiString anObjID (It.Key());
538 Standard_Integer anID = ExtractDocID(anObjID);
539 if (theDocID == anID) aSeq.Append(It.Key());
541 for (Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
543 // Forget free labels for this document
544 TFreeLabelsList::iterator anIt = _freeLabels.find(theDocID);
545 if (anIt != _freeLabels.end()) {
546 _freeLabels.erase(anIt);
549 _mapIDDocument.UnBind(theDocID);
550 _OCAFApp->Close(aDoc);
555 //=============================================================================
559 //=============================================================================
560 TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
561 std::vector<TObjectData>& theObjectData,
562 TVariablesList theVariables,
566 // Set "C" numeric locale to save numbers correctly
567 Kernel_Utils::Localizer loc;
569 TCollection_AsciiString aScript;
570 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
572 if (aDoc.IsNull()) return TCollection_AsciiString("def RebuildData(theStudy): pass\n");
574 aScript = "import GEOM\n";
575 aScript += "import geompy\n";
576 aScript += "import math\n";
577 aScript += "import SALOMEDS\n\n";
578 aScript += "def RebuildData(theStudy):";
579 aScript += "\n\tgeompy.init_geom(theStudy)\n";
581 AddTextures(theDocID, aScript);
583 Standard_Integer posToInsertGlobalVars = aScript.Length() + 1;
585 // a map containing copies of TObjectData from theObjectData
586 TSting2ObjDataMap aEntry2ObjData;
587 // contains pointers to TObjectData of either aEntry2ObjData or theObjectData; the latter
588 // occures when several StudyEntries correspond to one Entry
589 TSting2ObjDataPtrMap aStEntry2ObjDataPtr;
591 //Resource_DataMapOfAsciiStringAsciiString aEntry2StEntry, aStEntry2Entry, theObjectNames;
592 for (unsigned i = 0; i < theObjectData.size(); ++i )
594 TObjectData& data = theObjectData[i];
595 // look for an object by entry
597 TDF_Tool::Label( aDoc->GetData(), data._entry, L );
598 if ( L.IsNull() ) continue;
599 Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
601 if ( !obj.IsNull() ) {
602 TSting2ObjDataMap::iterator ent2Data =
603 aEntry2ObjData.insert( std::make_pair( data._entry, data )).first;
605 if ( ent2Data->second._studyEntry == data._studyEntry ) // Entry encounters 1st time
606 aStEntry2ObjDataPtr.insert( std::make_pair( data._studyEntry, & ent2Data->second ));
608 aStEntry2ObjDataPtr.insert( std::make_pair( data._studyEntry, & data ));
612 // collect objects entries to be published
613 TColStd_SequenceOfAsciiString aObjListToPublish;
615 // iterates on functions till critical (that requiers publication of objects)
616 Handle(TDataStd_TreeNode) aNode, aRoot;
617 Handle(GEOM_Function) aFunction;
618 TDF_LabelMap aCheckedFuncMap;
619 std::set< TCollection_AsciiString > anIgnoreObjMap;
621 TCollection_AsciiString aFuncScript;
623 // Mantis issue 0020768
624 Standard_Integer objectCounter = 0;
625 Resource_DataMapOfAsciiStringAsciiString aNameToEntry;
627 if (aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
628 TDataStd_ChildNodeIterator Itr(aRoot);
629 for (; Itr.More(); Itr.Next()) {
631 aFunction = GEOM_Function::GetFunction(aNode->Label());
632 if (aFunction.IsNull()) {
633 MESSAGE ( "Null function !!!!" );
636 bool isDumpCollected = false;
637 TCollection_AsciiString aCurScript, anAfterScript;
638 if (!ProcessFunction(aFunction, aCurScript, anAfterScript, theVariables,
639 isPublished, aCheckedFuncMap, anIgnoreObjMap,
642 // add function description before dump
643 if (!aCurScript.IsEmpty())
644 aFuncScript += aCurScript;
645 if (isDumpCollected ) {
646 // Replace entries by the names
647 ReplaceEntriesByNames( aFuncScript, aEntry2ObjData, isPublished,
648 aObjListToPublish, objectCounter, aNameToEntry );
650 // publish collected objects
651 TSting2StringMap anEntryToCmdMap; // sort publishing commands by study entry
652 int i = 1, n = aObjListToPublish.Length();
653 for ( ; i <= n; i++ )
655 const TCollection_AsciiString& aEntry = aObjListToPublish.Value(i);
656 PublishObject( aEntry2ObjData[aEntry], aEntry2ObjData, aStEntry2ObjDataPtr,
657 aNameToEntry, anEntryToCmdMap, anIgnoreObjMap );
659 // add publishing commands to the script
660 TSting2StringMap::iterator anEntryToCmd = anEntryToCmdMap.begin();
661 for ( ; anEntryToCmd != anEntryToCmdMap.end(); ++anEntryToCmd )
662 aFuncScript += anEntryToCmd->second;
664 // PTv, 0020001 add result objects from RestoreGivenSubShapes into ignore list,
665 // because they will be published during command execution
666 int indx = anAfterScript.Search( "RestoreGivenSubShapes" );
668 indx = anAfterScript.Search( "RestoreSubShapes" );
670 TCollection_AsciiString aSubStr = anAfterScript.SubString(1, indx);
671 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aSubStr);
672 i = 1, n = aSeq->Length();
673 for ( ; i <= n; i+=2) {
674 TCollection_AsciiString anEntry =
675 aSubStr.SubString(aSeq->Value(i), aSeq->Value(i+1));
676 anIgnoreObjMap.insert(anEntry.ToCString());
680 aObjListToPublish.Clear();
681 aScript += aFuncScript;
684 aFuncScript += anAfterScript;
688 // Replace entries by the names
689 aObjListToPublish.Clear();
690 ReplaceEntriesByNames( aFuncScript, aEntry2ObjData, isPublished, aObjListToPublish,
691 objectCounter, aNameToEntry );
693 aScript += aFuncScript;
696 AddObjectColors( theDocID, aScript, aEntry2ObjData );
698 // Make script to publish in study
699 TSting2ObjDataPtrMap::iterator aStEntry2ObjDataPtrIt;
702 TSting2StringMap anEntryToCmdMap; // sort publishing commands by object entry
704 for (aStEntry2ObjDataPtrIt = aStEntry2ObjDataPtr.begin();
705 aStEntry2ObjDataPtrIt != aStEntry2ObjDataPtr.end();
706 ++aStEntry2ObjDataPtrIt)
708 TObjectData* data = aStEntry2ObjDataPtrIt->second;
709 if ( anIgnoreObjMap.count( data->_entry ))
710 continue; // should not be dumped
711 PublishObject( *data, aEntry2ObjData, aStEntry2ObjDataPtr,
712 aNameToEntry, anEntryToCmdMap, anIgnoreObjMap );
714 // add publishing commands to the script
715 TSting2StringMap::iterator anEntryToCmd = anEntryToCmdMap.begin();
716 for ( ; anEntryToCmd != anEntryToCmdMap.end(); ++anEntryToCmd )
717 aScript += anEntryToCmd->second;
720 //aScript += "\n\tpass\n";
724 // fill _studyEntry2NameMap and build globalVars
725 TCollection_AsciiString globalVars;
726 _studyEntry2NameMap.Clear();
727 for (aStEntry2ObjDataPtrIt = aStEntry2ObjDataPtr.begin();
728 aStEntry2ObjDataPtrIt != aStEntry2ObjDataPtr.end();
729 ++aStEntry2ObjDataPtrIt)
731 const TCollection_AsciiString& studyEntry = aStEntry2ObjDataPtrIt->first;
732 const TObjectData* data = aStEntry2ObjDataPtrIt->second;
733 _studyEntry2NameMap.Bind ( studyEntry, data->_pyName );
734 if ( !globalVars.IsEmpty() )
736 globalVars += data->_pyName;
738 if ( !globalVars.IsEmpty() ) {
739 globalVars.Insert( 1, "\n\tglobal " );
740 aScript.Insert( posToInsertGlobalVars, globalVars );
746 //=======================================================================
747 //function : GetDumpName
749 //=======================================================================
751 const char* GEOM_Engine::GetDumpName (const char* theStudyEntry) const
753 if ( _studyEntry2NameMap.IsBound( (char*)theStudyEntry ))
754 return _studyEntry2NameMap( (char*)theStudyEntry ).ToCString();
759 //=======================================================================
760 //function : GetAllDumpNames
762 //=======================================================================
764 Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
766 Handle(TColStd_HSequenceOfAsciiString) aRetSeq = new TColStd_HSequenceOfAsciiString;
768 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString it (_studyEntry2NameMap);
769 for (; it.More(); it.Next()) {
770 aRetSeq->Append(it.Value());
776 #define TEXTURE_LABEL_ID 1
777 #define TEXTURE_LABEL_FILE 2
778 #define TEXTURE_LABEL_WIDTH 3
779 #define TEXTURE_LABEL_HEIGHT 4
780 #define TEXTURE_LABEL_DATA 5
782 int GEOM_Engine::addTexture(int theDocID, int theWidth, int theHeight,
783 const Handle(TDataStd_HArray1OfByte)& theTexture,
784 const TCollection_AsciiString& theFileName)
786 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
787 Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
789 // NPAL18604: use existing label to decrease memory usage,
790 // if this label has been freed (object deleted)
791 bool useExisting = false;
793 if (_freeLabels.find(theDocID) != _freeLabels.end()) {
794 std::list<TDF_Label>& aFreeLabels = _freeLabels[theDocID];
795 if (!aFreeLabels.empty()) {
797 aChild = aFreeLabels.front();
798 aFreeLabels.pop_front();
803 aChild = TDF_TagSource::NewChild(aDoc->Main());
806 aChild.ForgetAllAttributes(Standard_True);
807 Handle(TDataStd_TreeNode) node;
808 if ( !aChild.FindAttribute(TDataStd_TreeNode::GetDefaultTreeID(), node ) )
809 node = TDataStd_TreeNode::Set(aChild);
810 TDataStd_UAttribute::Set(aChild, GetTextureGUID());
812 static int aTextureID = 0;
814 TDataStd_Integer::Set(aChild.FindChild(TEXTURE_LABEL_ID), ++aTextureID);
815 TDataStd_Comment::Set(aChild.FindChild(TEXTURE_LABEL_FILE), theFileName);
816 TDataStd_Integer::Set(aChild.FindChild(TEXTURE_LABEL_WIDTH), theWidth);
817 TDataStd_Integer::Set(aChild.FindChild(TEXTURE_LABEL_HEIGHT), theHeight);
819 Handle(TDataStd_ByteArray) anAttr =
820 TDataStd_ByteArray::Set(aChild.FindChild(TEXTURE_LABEL_DATA),
821 theTexture.IsNull() ? 0 : theTexture->Lower(),
822 theTexture.IsNull() ? 0 : theTexture->Upper());
823 anAttr->ChangeArray(theTexture);
828 Handle(TDataStd_HArray1OfByte) GEOM_Engine::getTexture(int theDocID, int theTextureID,
829 int& theWidth, int& theHeight,
830 TCollection_AsciiString& theFileName)
832 Handle(TDataStd_HArray1OfByte) anArray;
833 theWidth = theHeight = 0;
835 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
837 TDF_ChildIterator anIterator(aDoc->Main(), Standard_True);
839 for (; anIterator.More() && !found; anIterator.Next()) {
840 TDF_Label aTextureLabel = anIterator.Value();
841 if (aTextureLabel.IsAttribute( GetTextureGUID())) {
842 TDF_Label anIDLabel = aTextureLabel.FindChild(TEXTURE_LABEL_ID, Standard_False);
843 Handle(TDataStd_Integer) anIdAttr;
844 if(!anIDLabel.IsNull() && anIDLabel.FindAttribute(TDataStd_Integer::GetID(), anIdAttr) &&
845 anIdAttr->Get() == theTextureID) {
846 TDF_Label aFileLabel = aTextureLabel.FindChild(TEXTURE_LABEL_FILE, Standard_False);
847 TDF_Label aWidthLabel = aTextureLabel.FindChild(TEXTURE_LABEL_WIDTH, Standard_False);
848 TDF_Label aHeightLabel = aTextureLabel.FindChild(TEXTURE_LABEL_HEIGHT, Standard_False);
849 TDF_Label aDataLabel = aTextureLabel.FindChild(TEXTURE_LABEL_DATA, Standard_False);
850 Handle(TDataStd_Integer) aWidthAttr, aHeightAttr;
851 Handle(TDataStd_ByteArray) aTextureAttr;
852 Handle(TDataStd_Comment) aFileAttr;
853 if (!aWidthLabel.IsNull() && aWidthLabel.FindAttribute(TDataStd_Integer::GetID(), aWidthAttr) &&
854 !aHeightLabel.IsNull() && aHeightLabel.FindAttribute(TDataStd_Integer::GetID(), aHeightAttr) &&
855 !aDataLabel.IsNull() && aDataLabel.FindAttribute(TDataStd_ByteArray::GetID(), aTextureAttr)) {
856 theWidth = aWidthAttr->Get();
857 theHeight = aHeightAttr->Get();
858 anArray = aTextureAttr->InternalArray();
860 if (!aFileLabel.IsNull() && aFileLabel.FindAttribute(TDataStd_Comment::GetID(), aFileAttr))
861 theFileName = aFileAttr->Get();
869 std::list<int> GEOM_Engine::getAllTextures(int theDocID)
871 std::list<int> id_list;
873 Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
875 TDF_ChildIterator anIterator(aDoc->Main(), Standard_True);
876 for (; anIterator.More(); anIterator.Next()) {
877 TDF_Label aTextureLabel = anIterator.Value();
878 if (aTextureLabel.IsAttribute( GetTextureGUID())) {
879 TDF_Label anIDLabel = aTextureLabel.FindChild(TEXTURE_LABEL_ID, Standard_False);
880 Handle(TDataStd_Integer) anIdAttr;
881 if(!anIDLabel.IsNull() && anIDLabel.FindAttribute(TDataStd_Integer::GetID(), anIdAttr))
882 id_list.push_back((int)anIdAttr->Get());
888 //===========================================================================
889 // Internal functions
890 //===========================================================================
892 //=============================================================================
894 * ProcessFunction: Dump fucntion description into script
896 //=============================================================================
897 bool ProcessFunction(Handle(GEOM_Function)& theFunction,
898 TCollection_AsciiString& theScript,
899 TCollection_AsciiString& theAfterScript,
900 const TVariablesList& theVariables,
901 const bool theIsPublished,
902 TDF_LabelMap& theProcessed,
903 std::set<TCollection_AsciiString>& theIgnoreObjs,
904 bool& theIsDumpCollected)
906 theIsDumpCollected = false;
907 if (theFunction.IsNull()) return false;
909 if (theProcessed.Contains(theFunction->GetEntry())) return false;
911 // pass functions, that depends on nonexisting ones
912 bool doNotProcess = false;
913 TDF_LabelSequence aSeq;
914 theFunction->GetDependency(aSeq);
915 Standard_Integer aLen = aSeq.Length();
916 for (Standard_Integer i = 1; i <= aLen && !doNotProcess; i++) {
917 TDF_Label aRefLabel = aSeq.Value(i);
918 Handle(TDF_Reference) aRef;
919 if (!aRefLabel.FindAttribute(TDF_Reference::GetID(), aRef)) {
923 if (aRef.IsNull() || aRef->Get().IsNull()) {
927 Handle(TDataStd_TreeNode) aT;
928 if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) {
932 TDF_Label aDepLabel = aT->Label();
933 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aDepLabel);
935 if (aFunction.IsNull()) doNotProcess = true;
936 else if (!theProcessed.Contains(aDepLabel)) doNotProcess = true;
943 TCollection_AsciiString anObjEntry;
944 TDF_Tool::Entry(theFunction->GetOwnerEntry(), anObjEntry);
945 theIgnoreObjs.insert(anObjEntry);
948 theProcessed.Add(theFunction->GetEntry());
950 TCollection_AsciiString aDescr = theFunction->GetDescription();
951 if(aDescr.Length() == 0) return false;
953 //Check if its internal function which doesn't requires dumping
954 if(aDescr == "None") return false;
956 // 0020001 PTv, check for critical functions, which require dump of objects
959 // currently, there is only one function "RestoreGivenSubShapes",
960 // later this check could be replaced by iterations on list of such functions
961 if (aDescr.Search( "RestoreGivenSubShapes" ) != -1)
962 theIsDumpCollected = true;
963 else if (aDescr.Search( "RestoreSubShapes" ) != -1)
964 theIsDumpCollected = true;
967 //Replace parameter by notebook variables
968 ReplaceVariables(aDescr,theVariables);
969 if ( theIsDumpCollected ) {
971 bool isBefore = true;
972 TCollection_AsciiString aSubStr = aDescr.Token("\n\t", i++);
973 while (!aSubStr.IsEmpty()) {
975 aSubStr.Search( "RestoreGivenSubShapes" ) == -1 &&
976 aSubStr.Search( "RestoreSubShapes" ) == -1)
977 theScript += TCollection_AsciiString("\n\t") + aSubStr;
979 theAfterScript += TCollection_AsciiString("\n\t") + aSubStr;
980 aSubStr = aDescr.Token("\n\t", i++);
990 //=============================================================================
992 * FindEntries: Returns a sequence of start/end positions of entries in the string
994 //=============================================================================
995 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString)
997 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
998 Standard_Integer aLen = theString.Length();
999 Standard_Boolean isFound = Standard_False;
1001 const char* arr = theString.ToCString();
1002 Standard_Integer i = 0, j;
1005 int c = (int)arr[i];
1007 if(c >= 48 && c <= 57) { //Is digit?
1009 isFound = Standard_False;
1010 while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
1012 if(c == 58) isFound = Standard_True;
1015 if(isFound && arr[j-2] != 58) { // last char should be a diggit
1016 aSeq->Append(i+1); // +1 because AsciiString starts from 1
1027 //=============================================================================
1029 * ReplaceVariables: Replace parameters of the function by variales from
1032 //=============================================================================
1033 void ReplaceVariables(TCollection_AsciiString& theCommand,
1034 const TVariablesList& theVariables)
1037 cout<<"Command : "<<theCommand<<endl;
1040 cout<<"All Entries:"<<endl;
1041 TVariablesList::const_iterator it = theVariables.begin();
1042 for(;it != theVariables.end();it++)
1043 cout<<"\t'"<<(*it).first<<"'"<<endl;
1046 //Additional case - multi-row commands
1047 int aCommandIndex = 1;
1048 while( aCommandIndex < 10 ) { // tmp check
1049 TCollection_AsciiString aCommand = theCommand.Token("\n",aCommandIndex);
1050 if( aCommand.Length() == 0 )
1054 cout<<"Sub-command : "<<aCommand<<endl;
1056 Standard_Integer aStartCommandPos = theCommand.Location(aCommand,1,theCommand.Length());
1057 Standard_Integer aEndCommandPos = aStartCommandPos + aCommand.Length();
1059 //Get Entry of the result object
1060 TCollection_AsciiString anEntry;
1061 if( aCommand.Search("=") != -1 ) // command returns an object
1062 anEntry = aCommand.Token("=",1);
1063 else { // command modifies the object
1064 if (int aStartEntryPos = aCommand.Location(1,'(',1,aCommand.Length()))
1065 if (int aEndEntryPos = aCommand.Location(1,',',aStartEntryPos,aCommand.Length()))
1066 anEntry = aCommand.SubString(aStartEntryPos+1, aEndEntryPos-1);
1068 //Remove white spaces
1069 anEntry.RightAdjust();
1070 anEntry.LeftAdjust();
1072 cout<<"Result entry : '" <<anEntry<<"'"<<endl;
1074 if ( anEntry.IsEmpty() ) {
1079 //Check if result is list of entries - enough to get the first entry in this case
1081 if( anEntry.Value( 1 ) == O_SQR_BRACKET && anEntry.Value( anEntry.Length() ) == C_SQR_BRACKET ) {
1082 while(anEntry.Location(aNbEntries,COMMA,1,anEntry.Length()))
1084 TCollection_AsciiString aSeparator(COMMA);
1085 anEntry = anEntry.Token(aSeparator.ToCString(),1);
1086 anEntry.Remove( 1, 1 );
1087 anEntry.RightAdjust();
1088 anEntry.LeftAdjust();
1090 cout<<"Sub-entry : '" <<anEntry<<"'"<<endl;
1093 //Find variables used for object construction
1094 ObjectStates* aStates = 0;
1095 TVariablesList::const_iterator it = theVariables.find(anEntry);
1096 if( it != theVariables.end() )
1097 aStates = (*it).second;
1101 cout<<"Valiables list empty!!!"<<endl;
1106 TState aVariables = aStates->GetCurrectState();
1109 cout<<"Variables from SObject:"<<endl;
1110 for (int i = 0; i < aVariables.size();i++)
1111 cout<<"\t Variable["<<i<<"] = "<<aVariables[i].myVariable<<endl;
1114 //Calculate total number of parameters
1115 Standard_Integer aTotalNbParams = 1;
1116 while(aCommand.Location(aTotalNbParams,COMMA,1,aCommand.Length()))
1120 cout<<"aTotalNbParams = "<<aTotalNbParams<<endl;
1122 Standard_Integer aFirstParam = aNbEntries;
1124 //Replace parameters by variables
1125 Standard_Integer aStartPos = 0;
1126 Standard_Integer aEndPos = 0;
1128 TCollection_AsciiString aVar, aReplacedVar;
1129 for(Standard_Integer i=aFirstParam;i <= aTotalNbParams;i++) {
1130 //Replace first parameter (bettwen '(' character and first ',' character)
1131 if(i == aFirstParam)
1133 aStartPos = aCommand.Location(O_BRACKET, 1, aCommand.Length()) + 1;
1134 if(aTotalNbParams - aNbEntries > 0 )
1135 aEndPos = aCommand.Location(aFirstParam, COMMA, 1, aCommand.Length());
1137 aEndPos = aCommand.Location(C_BRACKET, 1, aCommand.Length());
1139 //Replace last parameter (bettwen ',' character and ')' character)
1140 else if(i == aTotalNbParams)
1142 aStartPos = aCommand.Location(i-1, COMMA, 1, aCommand.Length()) + 2;
1143 aEndPos = aCommand.Location(C_BRACKET, 1, aCommand.Length());
1145 //Replace other parameters (bettwen two ',' characters)
1146 else if(i != aFirstParam && i != aTotalNbParams )
1148 aStartPos = aCommand.Location(i-1, COMMA, 1, aCommand.Length()) + 2;
1149 aEndPos = aCommand.Location(i, COMMA, 1, aCommand.Length());
1152 if( aCommand.Value( aStartPos ) == O_SQR_BRACKET )
1154 if( aCommand.Value( aEndPos-1 ) == C_SQR_BRACKET )
1156 if ( aStartPos == aEndPos )
1157 continue; // PAL20889: for "[]"
1160 cout<<"aStartPos = "<<aStartPos<<", aEndPos = "<<aEndPos<<endl;
1162 aVar = aCommand.SubString(aStartPos, aEndPos-1);
1167 cout<<"Variable: '"<< aVar <<"'"<<endl;
1169 // specific case for sketcher
1170 if(aVar.Location( TCollection_AsciiString("Sketcher:"), 1, aVar.Length() ) != 0) {
1171 Standard_Integer aNbSections = 1;
1172 while( aVar.Location( aNbSections, ':', 1, aVar.Length() ) )
1176 int aStartSectionPos = 0, aEndSectionPos = 0;
1177 TCollection_AsciiString aSection, aReplacedSection;
1178 for(Standard_Integer aSectionIndex = 1; aSectionIndex <= aNbSections; aSectionIndex++) {
1179 aStartSectionPos = aVar.Location( aSectionIndex, ':', 1, aVar.Length() ) + 1;
1180 if( aSectionIndex != aNbSections )
1181 aEndSectionPos = aVar.Location( aSectionIndex + 1, ':', 1, aVar.Length() );
1183 aEndSectionPos = aVar.Length();
1185 aSection = aVar.SubString(aStartSectionPos, aEndSectionPos-1);
1187 cout<<"aSection: "<<aSection<<endl;
1189 Standard_Integer aNbParams = 1;
1190 while( aSection.Location( aNbParams, ' ', 1, aSection.Length() ) )
1194 int aStartParamPos = 0, aEndParamPos = 0;
1195 TCollection_AsciiString aParameter, aReplacedParameter;
1196 for(Standard_Integer aParamIndex = 1; aParamIndex <= aNbParams; aParamIndex++) {
1197 aStartParamPos = aSection.Location( aParamIndex, ' ', 1, aSection.Length() ) + 1;
1198 if( aParamIndex != aNbParams )
1199 aEndParamPos = aSection.Location( aParamIndex + 1, ' ', 1, aSection.Length() );
1201 aEndParamPos = aSection.Length() + 1;
1203 aParameter = aSection.SubString(aStartParamPos, aEndParamPos-1);
1205 cout<<"aParameter: "<<aParameter<<endl;
1207 if(iVar >= aVariables.size())
1210 aReplacedParameter = aVariables[iVar].myVariable;
1211 if(aReplacedParameter.IsEmpty()) {
1216 if(aVariables[iVar].isVariable) {
1217 aReplacedParameter.InsertBefore(1,"'");
1218 aReplacedParameter.InsertAfter(aReplacedParameter.Length(),"'");
1222 cout<<"aSection before : "<<aSection<<endl;
1223 aSection.Remove(aStartParamPos, aEndParamPos - aStartParamPos);
1224 aSection.Insert(aStartParamPos, aReplacedParameter);
1226 cout<<"aSection after : "<<aSection<<endl<<endl;
1230 cout<<"aVar before : "<<aVar<<endl;
1231 aVar.Remove(aStartSectionPos, aEndSectionPos - aStartSectionPos);
1232 aVar.Insert(aStartSectionPos, aSection);
1234 cout<<"aVar after : "<<aVar<<endl<<endl;
1238 cout<<"aCommand before : "<<aCommand<<endl;
1239 aCommand.Remove(aStartPos, aEndPos - aStartPos);
1240 aCommand.Insert(aStartPos, aVar);
1242 cout<<"aCommand after : "<<aCommand<<endl;
1245 } // end of specific case for sketcher
1247 //If parameter is entry or 'None', skip it
1248 if(theVariables.find(aVar) != theVariables.end() || aVar.Search(":") != -1 || aVar == PY_NULL)
1251 if(iVar >= aVariables.size())
1254 aReplacedVar = aVariables[iVar].myVariable;
1255 if(aReplacedVar.IsEmpty()) {
1260 if(aVariables[iVar].isVariable) {
1261 aReplacedVar.InsertBefore(1,"\"");
1262 aReplacedVar.InsertAfter(aReplacedVar.Length(),"\"");
1265 aCommand.Remove(aStartPos, aEndPos - aStartPos);
1266 aCommand.Insert(aStartPos, aReplacedVar);
1270 theCommand.Remove(aStartCommandPos, aEndCommandPos - aStartCommandPos);
1271 theCommand.Insert(aStartCommandPos, aCommand);
1275 aStates->IncrementState();
1279 cout<<"Command : "<<theCommand<<endl;
1282 //=============================================================================
1284 * ReplaceEntriesByNames: Replace object entries by their names
1286 //=============================================================================
1287 void ReplaceEntriesByNames (TCollection_AsciiString& theScript,
1288 TSting2ObjDataMap& aEntry2ObjData,
1289 const bool theIsPublished,
1290 TColStd_SequenceOfAsciiString& theObjListToPublish,
1291 Standard_Integer& objectCounter,
1292 Resource_DataMapOfAsciiStringAsciiString& aNameToEntry)
1294 Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(theScript);
1295 Standard_Integer aLen = aSeq->Length(), aStart = 1, aScriptLength = theScript.Length();
1297 //Replace entries by the names
1298 TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("geomObj_"),
1299 allowedChars ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
1300 if (aLen == 0) anUpdatedScript = theScript;
1302 for (Standard_Integer i = 1; i <= aLen; i+=2) {
1303 anUpdatedScript += theScript.SubString(aStart, aSeq->Value(i)-1);
1304 anEntry = theScript.SubString(aSeq->Value(i), aSeq->Value(i+1));
1305 theObjListToPublish.Append( anEntry );
1307 TObjectData& data = aEntry2ObjData[ anEntry ];
1308 if ( !data._name.IsEmpty() ) { // published object
1309 if ( data._pyName.IsEmpty() ) { // encounted for the 1st time
1310 data._pyName = data._name;
1311 healPyName( data._pyName, anEntry, aNameToEntry);
1316 data._pyName = aBaseName + TCollection_AsciiString(++objectCounter);
1317 } while(aNameToEntry.IsBound(data._pyName));
1319 aNameToEntry.Bind(data._pyName, anEntry); // to detect same name of diff objects
1321 anUpdatedScript += data._pyName;
1322 aStart = aSeq->Value(i+1) + 1;
1325 //Add final part of the script
1326 if (aLen && aSeq->Value(aLen) < aScriptLength)
1327 anUpdatedScript += theScript.SubString(aSeq->Value(aLen)+1, aScriptLength); // mkr : IPAL11865
1329 theScript = anUpdatedScript;
1332 //=============================================================================
1334 * AddObjectColors: Add color to objects
1336 //=============================================================================
1337 void AddObjectColors (int theDocID,
1338 TCollection_AsciiString& theScript,
1339 const TSting2ObjDataMap& theEntry2ObjData)
1341 GEOM_Engine* engine = GEOM_Engine::GetEngine();
1342 Handle(TDocStd_Document) aDoc = engine->GetDocument(theDocID);
1344 TSting2ObjDataMap::const_iterator anEntryToNameIt;
1345 for (anEntryToNameIt = theEntry2ObjData.begin();
1346 anEntryToNameIt!= theEntry2ObjData.end();
1349 const TCollection_AsciiString& aEntry = anEntryToNameIt->first;
1350 const TCollection_AsciiString& aName = anEntryToNameIt->second._pyName;
1353 TDF_Tool::Label( aDoc->GetData(), aEntry, L );
1357 Handle(GEOM_Object) obj = GEOM_Object::GetObject( L );
1361 bool anAutoColor = obj->GetAutoColor();
1364 TCollection_AsciiString aCommand( "\n\t" );
1365 aCommand += aName + ".SetAutoColor(1)";
1366 theScript += aCommand.ToCString();
1369 GEOM_Object::Color aColor = obj->GetColor();
1370 if ( aColor.R >= 0 && aColor.G >= 0 && aColor.B >= 0 )
1372 TCollection_AsciiString aCommand( "\n\t" );
1373 aCommand += aName + ".SetColor(SALOMEDS.Color(" + aColor.R + "," + aColor.G + "," + aColor.B + "))";
1374 theScript += aCommand.ToCString();
1377 Aspect_TypeOfMarker aMarkerType = obj->GetMarkerType();
1378 if (aMarkerType >= Aspect_TOM_POINT && aMarkerType < Aspect_TOM_USERDEFINED) {
1379 TCollection_AsciiString aCommand( "\n\t" );
1380 aCommand += aName + ".SetMarkerStd(";
1381 switch (aMarkerType) {
1382 case Aspect_TOM_POINT: aCommand += "GEOM.MT_POINT"; break;
1383 case Aspect_TOM_PLUS: aCommand += "GEOM.MT_PLUS"; break;
1384 case Aspect_TOM_STAR: aCommand += "GEOM.MT_STAR"; break;
1385 case Aspect_TOM_O: aCommand += "GEOM.MT_O"; break;
1386 case Aspect_TOM_X: aCommand += "GEOM.MT_X"; break;
1387 case Aspect_TOM_O_POINT: aCommand += "GEOM.MT_O_POINT"; break;
1388 case Aspect_TOM_O_PLUS: aCommand += "GEOM.MT_O_PLUS"; break;
1389 case Aspect_TOM_O_STAR: aCommand += "GEOM.MT_O_STAR"; break;
1390 case Aspect_TOM_O_X: aCommand += "GEOM.MT_O_X"; break;
1391 case Aspect_TOM_BALL: aCommand += "GEOM.MT_BALL"; break;
1392 case Aspect_TOM_RING1: aCommand += "GEOM.MT_RING1"; break;
1393 case Aspect_TOM_RING2: aCommand += "GEOM.MT_RING2"; break;
1394 case Aspect_TOM_RING3: aCommand += "GEOM.MT_RING3"; break;
1395 default: aCommand += "GEOM.MT_NONE"; break; // just for completeness, should not get here
1398 int aSize = (int)( obj->GetMarkerSize()/0.5 ) - 1;
1400 case 1: aCommand += "GEOM.MS_10"; break;
1401 case 2: aCommand += "GEOM.MS_15"; break;
1402 case 3: aCommand += "GEOM.MS_20"; break;
1403 case 4: aCommand += "GEOM.MS_25"; break;
1404 case 5: aCommand += "GEOM.MS_30"; break;
1405 case 6: aCommand += "GEOM.MS_35"; break;
1406 case 7: aCommand += "GEOM.MS_40"; break;
1407 case 8: aCommand += "GEOM.MS_45"; break;
1408 case 9: aCommand += "GEOM.MS_50"; break;
1409 case 10: aCommand += "GEOM.MS_55"; break;
1410 case 11: aCommand += "GEOM.MS_60"; break;
1411 case 12: aCommand += "GEOM.MS_65"; break;
1412 case 13: aCommand += "GEOM.MS_70"; break;
1413 default: aCommand += "GEOM.MS_NONE"; break;
1416 theScript += aCommand.ToCString();
1418 else if (aMarkerType == Aspect_TOM_USERDEFINED) {
1419 int aMarkerTextureID = obj->GetMarkerTexture();
1420 if (aMarkerTextureID >= 0) {
1421 TCollection_AsciiString aCommand( "\n\t" );
1422 aCommand += aName + ".SetMarkerTexture(texture_map[";
1423 aCommand += aMarkerTextureID;
1425 theScript += aCommand.ToCString();
1431 static TCollection_AsciiString pack_data(const Handle(TDataStd_HArray1OfByte)& aData )
1433 TCollection_AsciiString stream;
1434 if (!aData.IsNull()) {
1435 for (Standard_Integer i = aData->Lower(); i <= aData->Upper(); i++) {
1436 Standard_Byte byte = aData->Value(i);
1437 TCollection_AsciiString strByte = "";
1438 for (int j = 0; j < 8; j++)
1439 strByte.Prepend((byte & (1<<j)) ? "1" : "0");
1446 void AddTextures (int theDocID, TCollection_AsciiString& theScript)
1448 GEOM_Engine* engine = GEOM_Engine::GetEngine();
1449 std::list<int> allTextures = engine->getAllTextures(theDocID);
1450 std::list<int>::const_iterator it;
1452 if (allTextures.size() > 0) {
1453 theScript += "\n\ttexture_map = {}\n";
1455 for (it = allTextures.begin(); it != allTextures.end(); ++it) {
1456 if (*it <= 0) continue;
1457 Standard_Integer aWidth, aHeight;
1458 TCollection_AsciiString aFileName;
1459 Handle(TDataStd_HArray1OfByte) aTexture = engine->getTexture(theDocID, *it, aWidth, aHeight, aFileName);
1460 if (aWidth > 0 && aHeight > 0 && !aTexture.IsNull() && aTexture->Length() > 0 ) {
1461 TCollection_AsciiString aCommand = "\n\t";
1462 aCommand += "texture_map["; aCommand += *it; aCommand += "] = ";
1463 if (aFileName != "" ) {
1464 aCommand += "geompy.LoadTexture(\"";
1465 aCommand += aFileName.ToCString();
1469 aCommand += "geompy.AddTexture(";
1470 aCommand += aWidth; aCommand += ", "; aCommand += aHeight; aCommand += ", \"";
1471 aCommand += pack_data(aTexture);
1474 theScript += aCommand;
1481 //=============================================================================
1483 * PublishObject: publish object in study script
1485 //=============================================================================
1486 void PublishObject (TObjectData& theObjectData,
1487 TSting2ObjDataMap& theEntry2ObjData,
1488 const TSting2ObjDataPtrMap& theStEntry2ObjDataPtr,
1489 Resource_DataMapOfAsciiStringAsciiString& theNameToEntry,
1490 TSting2StringMap& theEntryToCmdMap,
1491 std::set< TCollection_AsciiString>& theIgnoreMap)
1493 if ( theObjectData._studyEntry.IsEmpty() )
1494 return; // was not published
1495 if ( theIgnoreMap.count( theObjectData._entry ) )
1496 return; // not to publish
1498 TCollection_AsciiString aCommand("\n\tgeompy.");
1500 // find a father entry
1501 TObjectData* aFatherData = 0;
1502 TCollection_AsciiString aFatherStudyEntry =
1503 theObjectData._studyEntry.SubString( 1, theObjectData._studyEntry.SearchFromEnd(":") - 1 );
1504 TSting2ObjDataPtrMap::const_iterator stEntry2DataPtr =
1505 theStEntry2ObjDataPtr.find( aFatherStudyEntry );
1506 if ( stEntry2DataPtr != theStEntry2ObjDataPtr.end() )
1507 aFatherData = stEntry2DataPtr->second;
1509 // treat multiply published object
1510 if ( theObjectData._pyName.IsEmpty() )
1512 TObjectData& data0 = theEntry2ObjData[ theObjectData._entry ];
1513 if ( data0._pyName.IsEmpty() ) return; // something wrong
1515 theObjectData._pyName = theObjectData._name;
1516 healPyName( theObjectData._pyName, theObjectData._entry, theNameToEntry);
1518 TCollection_AsciiString aCreationCommand("\n\t");
1519 aCreationCommand += theObjectData._pyName + " = " + data0._pyName;
1521 // store aCreationCommand before publishing commands
1522 TCollection_AsciiString mapKey(" ");
1523 mapKey += theObjectData._studyEntry;
1524 theEntryToCmdMap.insert( std::make_pair( mapKey, aCreationCommand ));
1528 if ( aFatherData && !aFatherData->_pyName.IsEmpty() ) {
1529 aCommand += "addToStudyInFather( ";
1530 aCommand += aFatherData->_pyName + ", ";
1533 aCommand += "addToStudy( ";
1535 aCommand += theObjectData._pyName + ", '" + theObjectData._name + "' )";
1537 // bind a command to the study entry
1538 theEntryToCmdMap.insert( std::make_pair( theObjectData._studyEntry, aCommand ));
1540 theObjectData._studyEntry.Clear(); // not to publish any more
1543 //================================================================================
1545 * \brief Constructor
1547 //================================================================================
1548 ObjectStates::ObjectStates()
1553 //================================================================================
1557 //================================================================================
1558 ObjectStates::~ObjectStates()
1562 //================================================================================
1564 * \brief Return current object state
1565 * \retval state - Object state (vector of notebook variable)
1567 //================================================================================
1568 TState ObjectStates::GetCurrectState() const
1570 if(_states.size() > _dumpstate)
1571 return _states[_dumpstate];
1575 //================================================================================
1577 * \brief Add new object state
1578 * \param theState - Object state (vector of notebook variable)
1580 //================================================================================
1581 void ObjectStates::AddState(const TState &theState)
1583 _states.push_back(theState);
1586 //================================================================================
1588 * \brief Increment object state
1590 //================================================================================
1591 void ObjectStates::IncrementState()