X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FGEOM%2FGEOM_Engine.cxx;h=13aea9601b606aade95657920fd67df2db75b5ff;hb=14e1a694c4cf249fe205a39c099482bc5b28a6e5;hp=1293fd8ed24f81f6adf1059cf83ccb8b3f76a038;hpb=4b84d5ef39e8bc3247c4d4fa9e1fddfac2431254;p=modules%2Fgeom.git diff --git a/src/GEOM/GEOM_Engine.cxx b/src/GEOM/GEOM_Engine.cxx index 1293fd8ed..13aea9601 100644 --- a/src/GEOM/GEOM_Engine.cxx +++ b/src/GEOM/GEOM_Engine.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2016 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 @@ -64,13 +63,13 @@ #include +#if OCC_VERSION_LARGE < 0x07050000 #include +#endif -#if OCC_VERSION_LARGE > 0x07000000 #include #include #include -#endif #include @@ -84,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; @@ -109,7 +110,7 @@ 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); @@ -149,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 @@ -215,11 +336,9 @@ GEOM_Engine::GEOM_Engine() TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver()); _OCAFApp = new GEOM_Application(); -#if OCC_VERSION_LARGE > 0x07000000 _OCAFApp->DefineFormat("SALOME_GEOM", "GEOM Document Version 1.0", "sgd", new StdDrivers_DocumentRetrievalDriver, 0); BinDrivers::DefineFormat(_OCAFApp); -#endif _UndoLimit = 0; } @@ -253,11 +372,7 @@ Handle(TDocStd_Document) GEOM_Engine::GetDocument(bool force) aDoc = _document; } else if (force) { -#if OCC_VERSION_MAJOR > 6 _OCAFApp->NewDocument("BinOcaf", aDoc); -#else - _OCAFApp->NewDocument("SALOME_GEOM", aDoc); -#endif aDoc->SetUndoLimit(_UndoLimit); _document = aDoc; } @@ -393,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; } @@ -435,6 +549,10 @@ bool GEOM_Engine::RemoveObject(Handle(GEOM_BaseObject)& theObject) 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)) { @@ -463,14 +581,13 @@ 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 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(); @@ -510,9 +627,13 @@ bool GEOM_Engine::Save(const char* theFileName) { if(!_document) return false; - _OCAFApp->SaveAs(_document, 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; } //============================================================================= @@ -522,17 +643,20 @@ bool GEOM_Engine::Save(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; } -#if OCC_VERSION_MAJOR > 6 // Replace old document format by the new one. if (aDoc->StorageFormat().IsEqual("SALOME_GEOM")) { aDoc->ChangeStorageFormat("BinOcaf"); } -#endif aDoc->SetUndoLimit(_UndoLimit); @@ -611,7 +735,7 @@ TCollection_AsciiString GEOM_Engine::DumpPython(std::vector& theObj // 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; @@ -811,6 +935,11 @@ TCollection_AsciiString GEOM_Engine::DumpPython(std::vector& theObj 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; } @@ -1073,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; } @@ -1145,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 ) ) { @@ -1263,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); } @@ -1284,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; @@ -1475,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); @@ -1780,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 @@ -1816,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); @@ -1824,7 +1950,7 @@ 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) { @@ -1838,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 @@ -1872,7 +2033,7 @@ ObjectStates::~ObjectStates() //================================================================================ TState ObjectStates::GetCurrectState() const { - if(_states.size() > _dumpstate) + if((int)_states.size() > _dumpstate) return _states[_dumpstate]; return TState(); }