X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FGEOM%2FGEOM_Engine.cxx;h=13aea9601b606aade95657920fd67df2db75b5ff;hb=14e1a694c4cf249fe205a39c099482bc5b28a6e5;hp=e7921bed45520072b77f5adaec1feb7d13c833ca;hpb=529bc70dc8dd73f61f7311f1a882eca5923b6e70;p=modules%2Fgeom.git diff --git a/src/GEOM/GEOM_Engine.cxx b/src/GEOM/GEOM_Engine.cxx index e7921bed4..13aea9601 100644 --- a/src/GEOM/GEOM_Engine.cxx +++ b/src/GEOM/GEOM_Engine.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE +// Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE // // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS @@ -34,11 +34,10 @@ #include "GEOM_SubShapeDriver.hxx" #include "Sketcher_Profile.hxx" -#include - #include "utilities.h" #include +#include #include #include @@ -62,10 +61,15 @@ #include #include -#include #include +#if OCC_VERSION_LARGE < 0x07050000 #include +#endif + +#include +#include +#include #include @@ -79,11 +83,13 @@ #define C_SQR_BRACKET ']' #define PY_NULL "None" -#ifdef _DEBUG_ -static int MYDEBUG = 0; -#else -static int MYDEBUG = 0; -#endif +// VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump +// Next macro, when defined, causes appearing of SubShapeAllIDs(), SubShapeAllSortedIDs(), GetSameIDs() +// and other such commands in Python dump. +// See also GEOMImpl_IShapesOperations.cxx. +// --------------------------------------- +// #define DUMP_SUBSHAPE_IDS +// --------------------------------------- typedef std::map< TCollection_AsciiString, TCollection_AsciiString > TSting2StringMap; typedef std::map< TCollection_AsciiString, TObjectData > TSting2ObjDataMap; @@ -95,31 +101,16 @@ static GEOM_Engine* TheEngine = NULL; static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_BaseObject)& theObject) { - TCollection_AsciiString anID(theObject->GetDocID()), anEntry; + TCollection_AsciiString anEntry; TDF_Tool::Entry(theObject->GetEntry(), anEntry); - anID+=(TCollection_AsciiString("_")+anEntry); - return anID; -} - -static TCollection_AsciiString BuildID(Standard_Integer theDocID, const char* theEntry) -{ - TCollection_AsciiString anID(theDocID); - anID+=(TCollection_AsciiString("_")+theEntry); - return anID; -} - -static Standard_Integer ExtractDocID(TCollection_AsciiString& theID) -{ - TCollection_AsciiString aDocID = theID.Token("_"); - if(aDocID.Length() < 1) return -1; - return aDocID.IntegerValue(); + return anEntry; } bool ProcessFunction(Handle(GEOM_Function)& theFunction, TCollection_AsciiString& theScript, TCollection_AsciiString& theAfterScript, const TVariablesList& theVariables, - const bool theIsPublished, + const bool /*theIsPublished*/, TDF_LabelMap& theProcessed, std::set& theIgnoreObjs, bool& theIsDumpCollected); @@ -141,11 +132,10 @@ void ReplaceEntriesByNames (TCollection_AsciiString& theScript, Standard_Integer& objectCounter, Resource_DataMapOfAsciiStringAsciiString& aNameToEntry); -void AddObjectColors (int theDocID, - TCollection_AsciiString& theScript, +void AddObjectColors (TCollection_AsciiString& theScript, const TSting2ObjDataMap& theEntry2ObjData); -void AddTextures (int theDocID, TCollection_AsciiString& theScript); +void AddTextures (TCollection_AsciiString& theScript); void PublishObject (TObjectData& theObjectData, TSting2ObjDataMap& theEntry2ObjData, @@ -160,6 +150,126 @@ static TCollection_AsciiString GetPublishCommands const TIntToListIntMap &theMapRefs, std::set< int > &thePublished); +void Prettify(TCollection_AsciiString& theScript); + +// Helper functions +namespace +{ + // Specifies a way to process a given function + enum FunctionProcessType { NOT_PROCESS, TO_PROCESS, UPDATE_DESCRIPTION }; + + // Starting string for an edge case where function was dumped from Python code + const Standard_CString funcFromPythonStartString = "from salome.geom.geomrepairadv"; + + + //================================================================================ + /*! + * \brief Checks if a description contents a function that was dumped from Python code + */ + //================================================================================ + bool IsFunctionSetFromPython(const TCollection_AsciiString& aDescr) + { + // TODO: make it more generic and not depended on geomrepairadv name + return aDescr.Search(funcFromPythonStartString) != -1; + } + + + //================================================================================ + /*! + * \brief Removes from description the part before specific import statement + */ + //================================================================================ + void UpdateFuncFromPythonDescription(TCollection_AsciiString& aDescr) + { + const Standard_Integer startStringPos = aDescr.Search(funcFromPythonStartString); + MESSAGE("Description should start from pos: " << startStringPos); + if (startStringPos == -1) + { + MESSAGE("Can't find a string:\n" << funcFromPythonStartString << " \nin func description!"); + return; + } + + // Remove everything from the beginning till the starting point. + // Index starts from 1 not 0! + aDescr.Remove(1, startStringPos - 1); + MESSAGE("Updated func description: " << aDescr); + } + + + //================================================================================ + /*! + * \brief Finds out how we should process a given function for Python dump + */ + //================================================================================ + FunctionProcessType GetFunctionProcessingType(const Handle(GEOM_Function)& theFunction, const TDF_LabelMap& theProcessed, const TCollection_AsciiString& aDescr) + { + MESSAGE("Start check function dependencies..."); + + TDF_LabelSequence aSeq; + theFunction->GetDependency(aSeq); + const Standard_Integer aLen = aSeq.Length(); + + for (Standard_Integer i = 1; i <= aLen; i++) { + TDF_Label aRefLabel = aSeq.Value(i); + Handle(TDF_Reference) aRef; + if (!aRefLabel.FindAttribute(TDF_Reference::GetID(), aRef)) { + MESSAGE("Can't find TDF_Reference::GetID() attribute. Do not process."); + return NOT_PROCESS; + } + + if (aRef.IsNull() || aRef->Get().IsNull()) { + MESSAGE("Reference to attribute is null. Do not process."); + return NOT_PROCESS; + } + + Handle(TDataStd_TreeNode) aT; + if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) { + MESSAGE("Can't find a tree node. Do not process."); + return NOT_PROCESS; + } + + TDF_Label aDepLabel = aT->Label(); + Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aDepLabel); + if (aFunction.IsNull()) { + MESSAGE("Function is null. Do not process." << aFunction->GetDescription()); + return NOT_PROCESS; + } + + if (!theProcessed.Contains(aDepLabel)) { + // Special case for function dumped from Python, because it's appended to + // description of other function that should be rejected early. + // TODO: it's not clear if we need to check every given function or + // checking on this level is enough. At this moment it's better to stay here + // for performance reason. + if (IsFunctionSetFromPython(aDescr)) { + MESSAGE("Function set from Python. Do process with updated description."); + return UPDATE_DESCRIPTION; + } + + MESSAGE("The dependency label is not in processed list. Do not process."); + return NOT_PROCESS; + } + } + + MESSAGE("OK. Do process the function."); + return TO_PROCESS; + } + + //================================================================================ + /*! + * \brief Adds function's object to ignored for Python dump output + */ + //================================================================================ + void AddFuncObjectToIgnored(const Handle(GEOM_Function)& theFunction, std::set& theIgnoreObjs) + { + TCollection_AsciiString anObjEntry; + TDF_Tool::Entry(theFunction->GetOwnerEntry(), anObjEntry); + theIgnoreObjs.insert(anObjEntry); + } + +} + + //================================================================================ /*! * \brief Fix up the name of python variable @@ -226,6 +336,9 @@ GEOM_Engine::GEOM_Engine() TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver()); _OCAFApp = new GEOM_Application(); + _OCAFApp->DefineFormat("SALOME_GEOM", "GEOM Document Version 1.0", "sgd", + new StdDrivers_DocumentRetrievalDriver, 0); + BinDrivers::DefineFormat(_OCAFApp); _UndoLimit = 0; } @@ -242,14 +355,8 @@ GEOM_Engine::~GEOM_Engine() for(objit = objs.begin(); objit != objs.end(); ++objit) RemoveObject(*objit); - //Close all documents not closed - TColStd_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument); - for (; anItr.More(); anItr.Next()) - { - Close(anItr.Key()); - anItr.Initialize( _mapIDDocument ); // anItr becomes invalid at _mapIDDocument.UnBind(docId) - } - _mapIDDocument.Clear(); + //Close document + Close(); _objects.Clear(); } @@ -258,53 +365,35 @@ GEOM_Engine::~GEOM_Engine() * GetDocument */ //============================================================================= -Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID, bool force) +Handle(TDocStd_Document) GEOM_Engine::GetDocument(bool force) { Handle(TDocStd_Document) aDoc; - if(_mapIDDocument.IsBound(theDocID)) { - aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID)); + if (_document) { + aDoc = _document; } else if (force) { - _OCAFApp->NewDocument("SALOME_GEOM", aDoc); + _OCAFApp->NewDocument("BinOcaf", aDoc); aDoc->SetUndoLimit(_UndoLimit); - _mapIDDocument.Bind(theDocID, aDoc); - TDataStd_Integer::Set(aDoc->Main(), theDocID); + _document = aDoc; } return aDoc; } -//============================================================================= -/*! - * GetDocID - */ -//============================================================================= -int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument) -{ - if (theDocument.IsNull()) return -1; - TColStd_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument); - for (; anItr.More(); anItr.Next()) - if (anItr.Value() == theDocument) return anItr.Key(); - - return -1; -} - //============================================================================= /*! * GetObject */ //============================================================================= -Handle(GEOM_BaseObject) GEOM_Engine::GetObject(int theDocID, const char* theEntry, bool force) +Handle(GEOM_BaseObject) GEOM_Engine::GetObject(const char* theEntry, bool force) { Handle(GEOM_BaseObject) anObject; - TCollection_AsciiString anID = BuildID(theDocID, theEntry); - - if (_objects.IsBound(anID)) { - anObject = Handle(GEOM_BaseObject)::DownCast(_objects(anID)); + if (_objects.IsBound(theEntry)) { + anObject = Handle(GEOM_BaseObject)::DownCast(_objects(theEntry)); } else if (force) { - Handle(TDocStd_Document) aDoc = GetDocument(theDocID, force); + Handle(TDocStd_Document) aDoc = GetDocument(force); if ( !aDoc.IsNull()) { TDF_Label aLabel; TDF_Tool::Label(aDoc->Main().Data(), theEntry, aLabel, Standard_True); @@ -315,7 +404,7 @@ Handle(GEOM_BaseObject) GEOM_Engine::GetObject(int theDocID, const char* theEntr case GEOM_FIELD_STEP_OBJTYPE: anObject = new GEOM_FieldStep(aLabel); break; default: anObject = new GEOM_Object (aLabel); } - _objects.Bind(anID, anObject); + _objects.Bind(theEntry, anObject); } } } @@ -329,22 +418,19 @@ Handle(GEOM_BaseObject) GEOM_Engine::GetObject(int theDocID, const char* theEntr */ //============================================================================= -Handle(GEOM_BaseObject) GEOM_Engine::AddBaseObject(int theDocID, int theType) +Handle(GEOM_BaseObject) GEOM_Engine::AddBaseObject(int theType) { - Handle(TDocStd_Document) aDoc = GetDocument(theDocID); + Handle(TDocStd_Document) aDoc = GetDocument(); Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main()); // NPAL18604: use existing label to decrease memory usage, // if this label has been freed (object deleted) bool useExisting = false; TDF_Label aChild; - if (_freeLabels.find(theDocID) != _freeLabels.end()) { - std::list& aFreeLabels = _freeLabels[theDocID]; - if (!aFreeLabels.empty()) { - useExisting = true; - aChild = aFreeLabels.front(); - aFreeLabels.pop_front(); - } + if (!_freeLabels.empty()) { + useExisting = true; + aChild = _freeLabels.front(); + _freeLabels.pop_front(); } if (!useExisting) { // create new label @@ -372,9 +458,9 @@ Handle(GEOM_BaseObject) GEOM_Engine::AddBaseObject(int theDocID, int theType) */ //================================================================================ -Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType) +Handle(GEOM_Object) GEOM_Engine::AddObject(int theType) { - return Handle(GEOM_Object)::DownCast( AddBaseObject(theDocID, theType) ); + return Handle(GEOM_Object)::DownCast( AddBaseObject(theType) ); } //============================================================================= @@ -389,21 +475,17 @@ Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) th { if (theMainShape.IsNull() || theIndices.IsNull()) return NULL; - Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID()); + Handle(TDocStd_Document) aDoc = GetDocument(); Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main()); // NPAL18604: use existing label to decrease memory usage, // if this label has been freed (object deleted) bool useExisting = false; - TDF_Label aChild; - int aDocID = theMainShape->GetDocID(); - if (_freeLabels.find(aDocID) != _freeLabels.end()) { - std::list& aFreeLabels = _freeLabels[aDocID]; - if (!aFreeLabels.empty()) { - useExisting = true; - aChild = aFreeLabels.front(); - aFreeLabels.pop_front(); - } + TDF_Label aChild;; + if (!_freeLabels.empty()) { + useExisting = true; + aChild = _freeLabels.front(); + _freeLabels.pop_front(); } if (!useExisting) { // create new label @@ -426,9 +508,8 @@ Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) th return NULL; } } - catch (Standard_Failure) { - Handle(Standard_Failure) aFail = Standard_Failure::Caught(); - MESSAGE("GEOM_Engine::AddSubShape Error: " << aFail->GetMessageString()); + catch (Standard_Failure& aFail) { + MESSAGE("GEOM_Engine::AddSubShape Error: " << aFail.GetMessageString()); return NULL; } @@ -465,10 +546,13 @@ bool GEOM_Engine::RemoveObject(Handle(GEOM_BaseObject)& theObject) { if (theObject.IsNull()) return false; - int aDocID = theObject->GetDocID(); - if(!_mapIDDocument.IsBound(aDocID)) + if(!_document) return false; // document is closed... + TDF_Label aLabel = theObject->GetEntry(); + if ( aLabel == aLabel.Root() ) + return false; // already removed object + //Remove an object from the map of available objects TCollection_AsciiString anID = BuildIDFromObject(theObject); if (_objects.IsBound(anID)) { @@ -497,20 +581,17 @@ bool GEOM_Engine::RemoveObject(Handle(GEOM_BaseObject)& theObject) aNode->Remove(); } - TDF_Label aLabel = theObject->GetEntry(); aLabel.ForgetAllAttributes(Standard_True); // Remember the label to reuse it then - std::list& aFreeLabels = _freeLabels[aDocID]; - if ( aFreeLabels.empty() || aFreeLabels.back() != aLabel ) - aFreeLabels.push_back(aLabel); + if ( _freeLabels.empty() || _freeLabels.back() != aLabel ) + _freeLabels.push_back(aLabel); - // we can't explicitely delete theObject. At least prevent its functioning + // we can't explicitly delete theObject. At least prevent its functioning // as an alive object when aLabel is reused for a new object theObject->_label = aLabel.Root(); theObject->_ior.Clear(); - theObject->_parameters.Clear(); - theObject->_docID = -1; + theObject->_parameters.Clear();; theObject.Nullify(); @@ -522,9 +603,9 @@ bool GEOM_Engine::RemoveObject(Handle(GEOM_BaseObject)& theObject) * Undo */ //============================================================================= -void GEOM_Engine::Undo(int theDocID) +void GEOM_Engine::Undo() { - GetDocument(theDocID)->Undo(); + GetDocument()->Undo(); } //============================================================================= @@ -532,9 +613,9 @@ void GEOM_Engine::Undo(int theDocID) * Redo */ //============================================================================= -void GEOM_Engine::Redo(int theDocID) +void GEOM_Engine::Redo() { - GetDocument(theDocID)->Redo(); + GetDocument()->Redo(); } //============================================================================= @@ -542,14 +623,17 @@ void GEOM_Engine::Redo(int theDocID) * Save */ //============================================================================= -bool GEOM_Engine::Save(int theDocID, const char* theFileName) +bool GEOM_Engine::Save(const char* theFileName) { - if(!_mapIDDocument.IsBound(theDocID)) return false; - Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID)); + if(!_document) return false; - _OCAFApp->SaveAs(aDoc, theFileName); +#if defined(WIN32) && defined(UNICODE) + std::wstring aFileName = Kernel_Utils::utf8_decode_s(theFileName); +#else + std::string aFileName = theFileName; +#endif - return true; + return _OCAFApp->SaveAs( _document, aFileName.c_str() ) == PCDM_SS_OK; } //============================================================================= @@ -557,19 +641,26 @@ bool GEOM_Engine::Save(int theDocID, const char* theFileName) * Load */ //============================================================================= -bool GEOM_Engine::Load(int theDocID, const char* theFileName) +bool GEOM_Engine::Load(const char* theFileName) { +#if defined(WIN32) && defined(UNICODE) + std::wstring aFileName = Kernel_Utils::utf8_decode_s(theFileName); +#else + std::string aFileName = theFileName; +#endif Handle(TDocStd_Document) aDoc; - if (_OCAFApp->Open(theFileName, aDoc) != PCDM_RS_OK) { + if (_OCAFApp->Open(aFileName.c_str(), aDoc) != PCDM_RS_OK) { return false; } - aDoc->SetUndoLimit(_UndoLimit); + // Replace old document format by the new one. + if (aDoc->StorageFormat().IsEqual("SALOME_GEOM")) { + aDoc->ChangeStorageFormat("BinOcaf"); + } - if(_mapIDDocument.IsBound(theDocID)) _mapIDDocument.UnBind(theDocID); - _mapIDDocument.Bind(theDocID, aDoc); + aDoc->SetUndoLimit(_UndoLimit); - TDataStd_Integer::Set(aDoc->Main(), theDocID); + _document = aDoc; return true; } @@ -579,30 +670,24 @@ bool GEOM_Engine::Load(int theDocID, const char* theFileName) * Close */ //============================================================================= -void GEOM_Engine::Close(int theDocID) +void GEOM_Engine::Close() { - if (_mapIDDocument.IsBound(theDocID)) { - Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID)); - - //Remove all GEOM Objects associated to the given document + if (_document) { + //Remove all GEOM Objects associated to the document TColStd_SequenceOfAsciiString aSeq; GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It (_objects); for (; It.More(); It.Next()) { - TCollection_AsciiString anObjID (It.Key()); - Standard_Integer anID = ExtractDocID(anObjID); - if (theDocID == anID) aSeq.Append(It.Key()); + aSeq.Append(It.Key()); } - for (Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i)); - - // Forget free labels for this document - TFreeLabelsList::iterator anIt = _freeLabels.find(theDocID); - if (anIt != _freeLabels.end()) { - _freeLabels.erase(anIt); + for (Standard_Integer i=1; i<=aSeq.Length(); i++) { + _objects.UnBind(aSeq.Value(i)); } - _mapIDDocument.UnBind(theDocID); - _OCAFApp->Close(aDoc); - aDoc.Nullify(); + // Forget free labels for document + _freeLabels.clear(); + + _OCAFApp->Close(_document); + _document.Nullify(); } } @@ -611,8 +696,7 @@ void GEOM_Engine::Close(int theDocID) * DumpPython */ //============================================================================= -TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID, - std::vector& theObjectData, +TCollection_AsciiString GEOM_Engine::DumpPython(std::vector& theObjectData, TVariablesList theVariables, bool isPublished, bool isMultiFile, @@ -622,33 +706,36 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID, Kernel_Utils::Localizer loc; TCollection_AsciiString aScript; - Handle(TDocStd_Document) aDoc = GetDocument(theDocID); + Handle(TDocStd_Document) aDoc = GetDocument(); if (aDoc.IsNull()) { TCollection_AsciiString anEmptyScript; if( isMultiFile ) - anEmptyScript = "def RebuildData(theStudy): pass\n"; + anEmptyScript = "def RebuildData(): pass\n"; return anEmptyScript; } + + if( isMultiFile ) + aScript = "import salome\n"; - aScript = "import GEOM\n"; + aScript += "import GEOM\n"; aScript += "from salome.geom import geomBuilder\n"; aScript += "import math\n"; aScript += "import SALOMEDS\n\n"; if( isMultiFile ) - aScript += "def RebuildData(theStudy):"; + aScript += "def RebuildData():"; - aScript += "\n\tgeompy = geomBuilder.New(theStudy)\n"; + aScript += "\n\tgeompy = geomBuilder.New()\n"; - AddTextures(theDocID, aScript); + AddTextures(aScript); Standard_Integer posToInsertGlobalVars = aScript.Length() + 1; // a map containing copies of TObjectData from theObjectData TSting2ObjDataMap aEntry2ObjData; // contains pointers to TObjectData of either aEntry2ObjData or theObjectData; the latter - // occures when several StudyEntries correspond to one Entry + // occurs when several StudyEntries correspond to one Entry TSting2ObjDataPtrMap aStEntry2ObjDataPtr; //Resource_DataMapOfAsciiStringAsciiString aEntry2StEntry, aStEntry2Entry, theObjectNames; @@ -777,7 +864,7 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID, aScript += aFuncScript; // ouv : NPAL12872 - AddObjectColors( theDocID, aScript, aEntry2ObjData ); + AddObjectColors(aScript, aEntry2ObjData ); // Make script to publish in study TSting2ObjDataPtrMap::iterator aStEntry2ObjDataPtrIt; @@ -848,6 +935,11 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID, aScript.Insert( posToInsertGlobalVars, globalVars ); } + // VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump +#ifndef DUMP_SUBSHAPE_IDS + Prettify(aScript); +#endif + return aScript; } @@ -887,24 +979,21 @@ Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const #define TEXTURE_LABEL_HEIGHT 4 #define TEXTURE_LABEL_DATA 5 -int GEOM_Engine::addTexture(int theDocID, int theWidth, int theHeight, +int GEOM_Engine::addTexture(int theWidth, int theHeight, const Handle(TColStd_HArray1OfByte)& theTexture, const TCollection_AsciiString& theFileName) { - Handle(TDocStd_Document) aDoc = GetDocument(theDocID); + Handle(TDocStd_Document) aDoc = GetDocument(); Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main()); // NPAL18604: use existing label to decrease memory usage, // if this label has been freed (object deleted) bool useExisting = false; TDF_Label aChild; - if (_freeLabels.find(theDocID) != _freeLabels.end()) { - std::list& aFreeLabels = _freeLabels[theDocID]; - if (!aFreeLabels.empty()) { - useExisting = true; - aChild = aFreeLabels.front(); - aFreeLabels.pop_front(); - } + if (!_freeLabels.empty()) { + useExisting = true; + aChild = _freeLabels.front(); + _freeLabels.pop_front(); } if (!useExisting) { // create new label @@ -933,14 +1022,14 @@ int GEOM_Engine::addTexture(int theDocID, int theWidth, int theHeight, return aTextureID; } -Handle(TColStd_HArray1OfByte) GEOM_Engine::getTexture(int theDocID, int theTextureID, +Handle(TColStd_HArray1OfByte) GEOM_Engine::getTexture(int theTextureID, int& theWidth, int& theHeight, TCollection_AsciiString& theFileName) { Handle(TColStd_HArray1OfByte) anArray; theWidth = theHeight = 0; - Handle(TDocStd_Document) aDoc = GetDocument(theDocID); + Handle(TDocStd_Document) aDoc = GetDocument(); TDF_ChildIterator anIterator(aDoc->Main(), Standard_True); bool found = false; @@ -974,11 +1063,11 @@ Handle(TColStd_HArray1OfByte) GEOM_Engine::getTexture(int theDocID, int theTextu return anArray; } -std::list GEOM_Engine::getAllTextures(int theDocID) +std::list GEOM_Engine::getAllTextures() { std::list id_list; - Handle(TDocStd_Document) aDoc = GetDocument(theDocID); + Handle(TDocStd_Document) aDoc = GetDocument(); TDF_ChildIterator anIterator(aDoc->Main(), Standard_True); for (; anIterator.More(); anIterator.Next()) { @@ -993,17 +1082,6 @@ std::list GEOM_Engine::getAllTextures(int theDocID) return id_list; } -void GEOM_Engine::DocumentModified(const int theDocId, const bool isModified) -{ - if (isModified) _mapModifiedDocs.Add(theDocId); - else _mapModifiedDocs.Remove(theDocId); -} - -bool GEOM_Engine::DocumentModified(const int theDocId) -{ - return _mapModifiedDocs.Contains(theDocId); -} - //=========================================================================== // Internal functions //=========================================================================== @@ -1124,62 +1202,62 @@ bool ProcessFunction(Handle(GEOM_Function)& theFunction, bool& theIsDumpCollected) { theIsDumpCollected = false; - if (theFunction.IsNull()) return false; - if (theProcessed.Contains(theFunction->GetEntry())) return false; + if (theFunction.IsNull()) { + MESSAGE("Can't process a null function! Return."); + return false; + } - // pass functions, that depends on nonexisting ones - bool doNotProcess = false; - TDF_LabelSequence aSeq; - theFunction->GetDependency(aSeq); - Standard_Integer aLen = aSeq.Length(); - for (Standard_Integer i = 1; i <= aLen && !doNotProcess; i++) { - TDF_Label aRefLabel = aSeq.Value(i); - Handle(TDF_Reference) aRef; - if (!aRefLabel.FindAttribute(TDF_Reference::GetID(), aRef)) { - doNotProcess = true; - } - else { - if (aRef.IsNull() || aRef->Get().IsNull()) { - doNotProcess = true; - } - else { - Handle(TDataStd_TreeNode) aT; - if (!TDataStd_TreeNode::Find(aRef->Get(), aT)) { - doNotProcess = true; - } - else { - TDF_Label aDepLabel = aT->Label(); - Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aDepLabel); + TCollection_AsciiString aDescr = theFunction->GetDescription(); + MESSAGE("The function description: " << aDescr); - if (aFunction.IsNull()) doNotProcess = true; - else if (!theProcessed.Contains(aDepLabel)) doNotProcess = true; - } - } - } - } + if (theProcessed.Contains(theFunction->GetEntry())) + return false; - if (doNotProcess) { - TCollection_AsciiString anObjEntry; - TDF_Tool::Entry(theFunction->GetOwnerEntry(), anObjEntry); - theIgnoreObjs.insert(anObjEntry); + // Check if a given function depends on nonexisting ones + const FunctionProcessType funcProcessType = GetFunctionProcessingType(theFunction, theProcessed, aDescr); + switch (funcProcessType) + { + case TO_PROCESS: + // Just process it + break; + + case NOT_PROCESS: + { + // We don't need this function and its object in a dump + AddFuncObjectToIgnored(theFunction, theIgnoreObjs); return false; } + + case UPDATE_DESCRIPTION: + // Edge case for a function that was dumped from Python. + // Get rid of the parent function description. + UpdateFuncFromPythonDescription(aDescr); + // A result object is already added by an algo script, then + // if we keep it in the dump it will be added twice on the script loading. + AddFuncObjectToIgnored(theFunction, theIgnoreObjs); + break; + + default: + MESSAGE("Wrong type of the function processing!" << funcProcessType); + break; + } + theProcessed.Add(theFunction->GetEntry()); - TCollection_AsciiString aDescr = theFunction->GetDescription(); - if(aDescr.Length() == 0) return false; + // Check the length only after its fucntion was added to the processed! + if(!aDescr.Length()) + return false; - //Check if its internal function which doesn't requires dumping - if(aDescr == "None") return false; + // Check if it's an internal function which doesn't require dumping + if(aDescr == "None") + return false; //Check the very specific case of RestoreShape function, //which is not dumped, but the result can be published by the user. //We do not publish such objects to decrease danger of dumped script failure. if(aDescr.Value(1) == '#') { - TCollection_AsciiString anObjEntry; - TDF_Tool::Entry(theFunction->GetOwnerEntry(), anObjEntry); - theIgnoreObjs.insert(anObjEntry); + AddFuncObjectToIgnored(theFunction, theIgnoreObjs); return false; } @@ -1196,6 +1274,9 @@ bool ProcessFunction(Handle(GEOM_Function)& theFunction, //Replace parameter by notebook variables ReplaceVariables(aDescr,theVariables); + // Check description, because we could lose entire command during variable processing + if (aDescr.IsEmpty()) + return false; //Process sketcher functions, replacing string command by calls to Sketcher interface if ( ( aDescr.Search( "MakeSketcherOnPlane" ) != -1 ) || ( aDescr.Search( "MakeSketcher" ) != -1 ) ) { @@ -1278,7 +1359,10 @@ void FillMapOfRef(const Handle(GEOM_Function) &theFunction, const int aRefTag = GetTag(anObjEntry); - theRefMap[anObjTag].push_back(aRefTag); + if (anObjTag != aRefTag) { + // Avoid making references for operations without copy. + theRefMap[anObjTag].push_back(aRefTag); + } } } } @@ -1311,7 +1395,7 @@ Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theStrin if(c == 58) isFound = Standard_True; } - if(isFound && arr[j-2] != 58) { // last char should be a diggit + if(isFound && arr[j-2] != 58) { // last char should be a digit aSeq->Append(i+1); // +1 because AsciiString starts from 1 aSeq->Append(j-1); } @@ -1332,14 +1416,13 @@ Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theStrin void ReplaceVariables(TCollection_AsciiString& theCommand, const TVariablesList& theVariables) { - if (MYDEBUG) - cout<<"Command : "<GetCurrectState(); - if(MYDEBUG) { - cout<<"Variables from SObject:"<= aVariables.size()) continue; @@ -1523,28 +1603,25 @@ void ReplaceVariables(TCollection_AsciiString& theCommand, aReplacedParameter.InsertAfter(aReplacedParameter.Length(),"'"); } - if(MYDEBUG) - cout<<"aSection before : "<IncrementState(); } - if (MYDEBUG) - cout<<"Command : "<healPyName( data._pyName, anEntry, aNameToEntry); @@ -1641,12 +1717,11 @@ void ReplaceEntriesByNames (TCollection_AsciiString& theScript, * AddObjectColors: Add color to objects */ //============================================================================= -void AddObjectColors (int theDocID, - TCollection_AsciiString& theScript, +void AddObjectColors (TCollection_AsciiString& theScript, const TSting2ObjDataMap& theEntry2ObjData) { GEOM_Engine* engine = GEOM_Engine::GetEngine(); - Handle(TDocStd_Document) aDoc = engine->GetDocument(theDocID); + Handle(TDocStd_Document) aDoc = engine->GetDocument(); TSting2ObjDataMap::const_iterator anEntryToNameIt; for (anEntryToNameIt = theEntry2ObjData.begin(); @@ -1750,10 +1825,10 @@ static TCollection_AsciiString pack_data (const Handle(TColStd_HArray1OfByte)& a return stream; } -void AddTextures (int theDocID, TCollection_AsciiString& theScript) +void AddTextures (TCollection_AsciiString& theScript) { GEOM_Engine* engine = GEOM_Engine::GetEngine(); - std::list allTextures = engine->getAllTextures(theDocID); + std::list allTextures = engine->getAllTextures(); std::list::const_iterator it; if (allTextures.size() > 0) { @@ -1764,7 +1839,7 @@ void AddTextures (int theDocID, TCollection_AsciiString& theScript) Standard_Integer aWidth, aHeight; TCollection_AsciiString aFileName; Handle(TColStd_HArray1OfByte) aTexture = - engine->getTexture(theDocID, *it, aWidth, aHeight, aFileName); + engine->getTexture(*it, aWidth, aHeight, aFileName); if (aWidth > 0 && aHeight > 0 && !aTexture.IsNull() && aTexture->Length() > 0 ) { TCollection_AsciiString aCommand = "\n\t"; aCommand += "texture_map["; aCommand += *it; aCommand += "] = "; @@ -1829,7 +1904,7 @@ void PublishObject (TObjectData& theObjectData, // store aCreationCommand before publishing commands int tag = GetTag(theObjectData._entry); - theEntryToCmdMap.insert( std::make_pair( tag + 2*theEntry2ObjData.size(), aCreationCommand )); + theEntryToCmdMap.insert( std::make_pair( tag + -2*theEntry2ObjData.size(), aCreationCommand )); } // make a command @@ -1865,6 +1940,8 @@ TCollection_AsciiString GetPublishCommands if (!thePublished.count(theTag)) { // This object is not published yet. + thePublished.insert(theTag); + std::map< int, TCollection_AsciiString >::const_iterator anIt = theEntryToCmdMap.find(theTag); @@ -1873,9 +1950,9 @@ TCollection_AsciiString GetPublishCommands TIntToListIntMap::const_iterator aRefIt = theMapRefs.find(theTag); if (aRefIt != theMapRefs.end()) { - // Recursively publish all references. + // Recursively publish all references. std::list< int >::const_iterator aRefTagIt = aRefIt->second.begin(); - + for(; aRefTagIt != aRefIt->second.end(); ++aRefTagIt) { const TCollection_AsciiString aRefCmd = GetPublishCommands (*aRefTagIt, theEntryToCmdMap, theMapRefs, thePublished); @@ -1887,13 +1964,48 @@ TCollection_AsciiString GetPublishCommands // Add the object command. aResult += anIt->second; } - - thePublished.insert(theTag); } return aResult; } +void Prettify(TCollection_AsciiString& theScript) +{ + TCollection_AsciiString output; + static std::list ToRemove; + if (ToRemove.empty()) { + ToRemove.push_back("geompy.SubShapeAllIDs"); + ToRemove.push_back("geompy.SubShapeAllSortedCentresIDs"); + ToRemove.push_back("geompy.SubShapeAllSortedIDs"); + ToRemove.push_back("geompy.GetFreeFacesIDs"); + ToRemove.push_back("geompy.GetShapesOnBoxIDs"); + ToRemove.push_back("geompy.GetShapesOnShapeIDs"); + ToRemove.push_back("geompy.GetShapesOnPlaneIDs"); + ToRemove.push_back("geompy.GetShapesOnPlaneWithLocationIDs"); + ToRemove.push_back("geompy.GetShapesOnCylinderIDs"); + ToRemove.push_back("geompy.GetShapesOnCylinderWithLocationIDs"); + ToRemove.push_back("geompy.GetShapesOnSphereIDs"); + ToRemove.push_back("geompy.GetShapesOnQuadrangleIDs"); + ToRemove.push_back("geompy.GetSameIDs"); + } + + int start = 1; + while (start <= theScript.Length()) { + int end = theScript.Location("\n", start, theScript.Length()); + if (end == -1) end = theScript.Length(); + TCollection_AsciiString line = theScript.SubString(start, end); + bool found = false; + for (std::list::const_iterator it = ToRemove.begin(); it != ToRemove.end() && !found; ++it) + found = line.Search( *it ) != -1; + if (!found) + output += line; + start = end + 1; + } + theScript = output; + + //OK @@@@@@@@@@@@@@@@@@@@@@@@@@@ +} + //================================================================================ /*! * \brief Constructor @@ -1921,7 +2033,7 @@ ObjectStates::~ObjectStates() //================================================================================ TState ObjectStates::GetCurrectState() const { - if(_states.size() > _dumpstate) + if((int)_states.size() > _dumpstate) return _states[_dumpstate]; return TState(); }