#include "Utils_ExceptHandlers.hxx"
#include "utilities.h"
-#include "GEOM_Object_i.hh"
#include "GEOM_Object.hxx"
#include "GEOM_Function.hxx"
#include "GEOM_ISubShape.hxx"
#include <TopExp.hxx>
#include <TopTools_SequenceOfShape.hxx>
#include <OSD.hxx>
+#include <TDataStd_ChildNodeIterator.hxx>
+#include <TDocStd_Owner.hxx>
+#include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
#include <SALOMEDS_Tool.hxx>
#include <SALOMEDS_wrap.hxx>
}
//=======================================================================
-// function :
-// purpose :
+// function : GetDependencyTree
+// purpose : Collects dependencies of the given objects from other ones
//=======================================================================
SALOMEDS::TMPFile* GEOM_Gen_i::GetDependencyTree( SALOMEDS::Study_ptr theStudy,
- const GEOM::string_array& theObjectIORs ) {
+ const GEOM::string_array& theObjectEntries ) {
// fill in the tree structure
GEOMUtils::TreeModel tree;
- // foreach( QString ior, theObjectIORs ) {
- std::string ior;
- for ( int i = 0; i < theObjectIORs.length(); i++ ) {
- ior = theObjectIORs[i].in();
- GEOM::GEOM_Object_ptr anObj = GetIORFromString( ior.c_str() );
+ std::string entry;
+ for ( int i = 0; i < theObjectEntries.length(); i++ ) {
+ // process objects one-by-one
+ entry = theObjectEntries[i].in();
+ GEOM::GEOM_BaseObject_var anObj = GetObject( theStudy->StudyId(), entry.c_str() );
+ if ( anObj->_is_nil() )
+ continue;
+ std::map< std::string, std::set<std::string> > passedEntries;
GEOMUtils::LevelsList upLevelList;
- getUpwardDependency( anObj, upLevelList );
+ // get objects from which current one depends on recursively
+ getUpwardDependency( anObj, upLevelList, passedEntries );
GEOMUtils::LevelsList downLevelList;
- getDownwardDependency( theStudy, anObj, downLevelList );
- tree.insert( std::pair<std::string, std::pair<GEOMUtils::LevelsList,GEOMUtils::LevelsList> >(ior, std::pair<GEOMUtils::LevelsList,GEOMUtils::LevelsList>( upLevelList, downLevelList ) ) );
+ // get objects that depends on current one recursively
+ getDownwardDependency( anObj, downLevelList, passedEntries );
+ tree.insert( std::pair<std::string, std::pair<GEOMUtils::LevelsList,GEOMUtils::LevelsList> >(entry, std::pair<GEOMUtils::LevelsList,GEOMUtils::LevelsList>( upLevelList, downLevelList ) ) );
}
// translation the tree into string
std::string treeStr;
GEOMUtils::ConvertTreeToString( tree, treeStr );
+ // put string into stream
char* aBuffer = (char*)CORBA::string_dup(treeStr.c_str());
int aBufferSize = strlen((char*)aBuffer);
}
//=======================================================================
-// function :
-// purpose :
+// function : getUpwardDependency
+// purpose : Collects the entries of objects on that the given one depends
//=======================================================================
void GEOM_Gen_i::getUpwardDependency( GEOM::GEOM_BaseObject_ptr gbo,
GEOMUtils::LevelsList &upLevelList,
+ std::map< std::string, std::set<std::string> > &passedEntries,
int level ) {
- std::string aGboIOR = GetStringFromIOR(GEOM::GEOM_Object::_narrow(gbo));
+ std::string aGboEntry = gbo->GetEntry();
+ GEOMUtils::NodeLinks anEntries;
+ GEOMUtils::LevelInfo aLevelMap;
+ if ( level > 0 ) {
+ if ( level-1 >= upLevelList.size() ) {
+ // create a new map
+ upLevelList.push_back( aLevelMap );
+ } else {
+ // get the existent map
+ aLevelMap = upLevelList.at(level-1);
+ if ( aLevelMap.count( aGboEntry ) > 0 ) {
+ anEntries = aLevelMap[ aGboEntry ];
+ }
+ }
+ }
+ // get objects on that the current one depends
GEOM::ListOfGBO_var depList = gbo->GetDependency();
+ std::string aDepEntry;
for( int j = 0; j < depList->length(); j++ ) {
+ if ( depList[j]->_is_nil() )
+ continue;
+ aDepEntry = depList[j]->GetEntry();
+ if ( passedEntries.count( aGboEntry ) > 0 &&
+ passedEntries[aGboEntry].count( aDepEntry ) > 0 ) {
+ //avoid checking the passed objects
+ continue;
+ }
+ passedEntries[aGboEntry].insert( aDepEntry );
if ( level > 0 ) {
- GEOMUtils::NodeLinks anIORs;
- GEOMUtils::LevelInfo aLevelMap;
- if ( level-1 >= upLevelList.size() ) {
- upLevelList.push_back( aLevelMap );
- } else {
- aLevelMap = upLevelList.at(level-1);
- if ( aLevelMap.count( aGboIOR ) > 0 )
- anIORs = aLevelMap[ aGboIOR ];
- }
- anIORs.push_back( GetStringFromIOR(GEOM::GEOM_Object::_narrow(depList[j])) );
- aLevelMap.insert( std::pair<std::string, GEOMUtils::NodeLinks>(aGboIOR, anIORs) );
+ anEntries.push_back( aDepEntry );
}
- getUpwardDependency(depList[j], upLevelList, level++);
+ // get dependencies recursively
+ getUpwardDependency(depList[j], upLevelList, passedEntries, level+1);
+ }
+ if ( level > 0 ) {
+ aLevelMap.insert( std::pair<std::string, GEOMUtils::NodeLinks>(aGboEntry, anEntries) );
+ upLevelList[level-1] = aLevelMap;
}
}
//=======================================================================
-// function :
-// purpose :
+// function : getDownwardDependency
+// purpose : Collects the entries of objects that depends on the given one
//=======================================================================
-void GEOM_Gen_i::getDownwardDependency( SALOMEDS::Study_ptr theStudy,
- GEOM::GEOM_BaseObject_ptr gbo,
+void GEOM_Gen_i::getDownwardDependency( GEOM::GEOM_BaseObject_ptr gbo,
GEOMUtils::LevelsList &downLevelList,
+ std::map< std::string, std::set<std::string> > &passedEntries,
int level ) {
- SALOMEDS::SComponent_var comp = theStudy->FindComponent("GEOM");
- if ( !comp )
- return;
+ std::string aGboEntry = gbo->GetEntry();
+ Handle(TDocStd_Document) aDoc = GEOM_Engine::GetEngine()->GetDocument(gbo->GetStudyID());
+ Handle(TDataStd_TreeNode) aNode, aRoot;
+ Handle(GEOM_Function) aFunction;
+ if (aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
+ // go through the whole OCAF tree
+ TDataStd_ChildNodeIterator Itr( aRoot );
+ for (; Itr.More(); Itr.Next()) {
+ aNode = Itr.Value();
+ aFunction = GEOM_Function::GetFunction(aNode->Label());
+ if (aFunction.IsNull()) {
+ continue;
+ }
+ TDF_Label aLabel = aFunction->GetOwnerEntry();
+ if(aLabel.IsNull()) continue;
+ TCollection_AsciiString anEntry;
+ TDF_Tool::Entry(aLabel, anEntry);
+ GEOM::GEOM_BaseObject_var geomObj = GetObject( gbo->GetStudyID(), anEntry.ToCString() );
+ if( CORBA::is_nil( geomObj ) )
+ continue;
+ // get dependencies for current object in the tree
+ GEOM::ListOfGBO_var depList = geomObj->GetDependency();
+ if( depList->length() == 0 )
+ continue;
+ std::string aGoEntry = geomObj->GetEntry();
+ // go through dependencies of current object to check whether it depends on the given object
+ for( int i = 0; i < depList->length(); i++ ) {
+ if ( depList[i]->_is_nil() )
+ continue;
+ if ( depList[i]->_is_equivalent( gbo ) ) {
+ // yes, the current object depends on the given object
+ if ( passedEntries.count( aGoEntry ) > 0 &&
+ passedEntries[aGoEntry].count( aGboEntry ) > 0 ) {
+ //avoid checking the passed objects
+ continue;
+ }
+ passedEntries[aGoEntry].insert( aGboEntry );
+ GEOMUtils::NodeLinks anEntries;
+ GEOMUtils::LevelInfo aLevelMap;
+ anEntries.push_back( aGboEntry );
+ if ( level >= downLevelList.size() ) {
+ downLevelList.push_back( aLevelMap );
+ } else {
+ aLevelMap = downLevelList.at(level);
+ if ( aLevelMap.count( aGoEntry ) > 0 ) {
+ anEntries = aLevelMap[ aGoEntry ];
+ }
+ }
+ aLevelMap.insert( std::pair<std::string, GEOMUtils::NodeLinks>(aGoEntry, anEntries) );
+ downLevelList[level] = aLevelMap;
+ // get dependencies of the current object recursively
+ getDownwardDependency(geomObj, downLevelList, passedEntries, level+1);
+ break;
+ }
+ }
+ }
+ }
+}
- SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator( comp );
- for ( it->InitEx( true ); it->More(); it->Next() ) {
- SALOMEDS::SObject_var child = it->Value();
- CORBA::Object_var corbaObj = child->GetObject();
- GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
- if( CORBA::is_nil( geomObj ) )
- continue;
+//==============================================================================
+// function : GetEntriesToCleanStudy
+// purpose : Fills 3 lists that is used to clean study of redundant objects
+//==============================================================================
+void GEOM_Gen_i::GetEntriesToCleanStudy(SALOMEDS::Study_ptr theStudy,
+ GEOM::string_array& theSelectedEntries,
+ GEOM::string_array& theParentEntries,
+ GEOM::string_array& theSubEntries,
+ GEOM::string_array& theOtherEntries)
+{
+ std::set<std::string> aSelected, aParents, aChildren, anOthers;
+ for ( int i = 0; i < theSelectedEntries.length(); i++ ) {
+ aSelected.insert( CORBA::string_dup( theSelectedEntries[i] ) );
+ }
- GEOM::ListOfGBO_var depList = geomObj->GetDependency();
- if( depList->length() == 0 )
- continue;
- std::string aGoIOR = GetStringFromIOR( geomObj );
-
- for( int i = 0; i < depList->length(); i++ ) {
- if ( depList[i]->IsSame( gbo ) ) {
- GEOMUtils::NodeLinks anIORs;
- GEOMUtils::LevelInfo aLevelMap;
- if ( level >= downLevelList.size() ) {
- aLevelMap = GEOMUtils::LevelInfo();
- downLevelList.push_back( aLevelMap );
- } else {
- aLevelMap = downLevelList.at(level);
- if ( aLevelMap.count( aGoIOR ) > 0 )
- anIORs = aLevelMap[ aGoIOR ];
+ Handle(TDocStd_Document) aDoc = GEOM_Engine::GetEngine()->GetDocument(theStudy->StudyId());
+ Handle(TDataStd_TreeNode) aNode, aRoot;
+ Handle(GEOM_Function) aFunction;
+ if (aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
+ // go through the whole OCAF tree
+ TDF_Label aLabel;
+ std::string anEntry;
+ TCollection_AsciiString anAsciiEntry;
+ TDataStd_ChildNodeIterator Itr( aRoot );
+ for (; Itr.More(); Itr.Next()) {
+ aNode = Itr.Value();
+ aFunction = GEOM_Function::GetFunction(aNode->Label());
+ if (aFunction.IsNull()) {
+ continue;
+ }
+ aLabel = aFunction->GetOwnerEntry();
+ if(aLabel.IsNull())
+ continue;
+ TDF_Tool::Entry(aLabel, anAsciiEntry);
+ anEntry = anAsciiEntry.ToCString();
+ GEOM::GEOM_BaseObject_var geomObj = GetObject( theStudy->StudyId(), anEntry.c_str() );
+ if( CORBA::is_nil( geomObj ) )
+ continue;
+
+ if ( aSelected.count( anEntry ) > 0 &&
+ aParents.count( anEntry ) == 0 ) {
+ getParentDependencies( geomObj, aSelected, aParents, aChildren, anOthers );
+ } else if ( aParents.count( anEntry ) == 0 &&
+ aChildren.count( anEntry ) == 0 ) {
+ anOthers.insert( geomObj->GetEntry() );
+ }
+ }
+
+ std::set<std::string>::iterator it;
+ std::set<std::string>::iterator foundIt;
+ TCollection_AsciiString stringIOR;
+ GEOM::GEOM_Object_var geomObj;
+
+ // filling list of sub-objects
+ for ( it = aSelected.begin(); it != aSelected.end(); ++it ) {
+ Handle(GEOM_BaseObject) handle_object = _impl->GetObject( theStudy->StudyId(), (*it).c_str(), false);
+ if ( handle_object.IsNull() )
+ continue;
+
+ Handle(GEOM_Function) aShapeFun = handle_object->GetFunction(1);
+ if ( aShapeFun.IsNull() || !aShapeFun->HasSubShapeReferences() )
+ continue;
+
+ const TDataStd_ListOfExtendedString& aListEntries = aShapeFun->GetSubShapeReferences();
+ if ( aListEntries.IsEmpty() )
+ continue;
+
+ TDataStd_ListIteratorOfListOfExtendedString anIt (aListEntries);
+ for ( ; anIt.More(); anIt.Next() ) {
+ TCollection_ExtendedString aSubEntry = anIt.Value();
+ Standard_Integer aStrLen = aSubEntry.LengthOfCString();
+ char* aSubEntryStr = new char[aStrLen+1];
+ aSubEntry.ToUTF8CString( aSubEntryStr );
+ foundIt = aParents.find( aSubEntryStr );
+ if ( foundIt == aParents.end() ) { // add to sub-objects if it is not in parents list
+ aChildren.insert( aSubEntryStr );
+ foundIt = anOthers.find( aSubEntryStr );
+ if ( foundIt != anOthers.end() )
+ anOthers.erase( foundIt );
}
- anIORs.push_back( GetStringFromIOR(GEOM::GEOM_Object::_narrow(depList[i])));
- aLevelMap.insert( std::pair<std::string, GEOMUtils::NodeLinks>(aGoIOR, anIORs) );
}
}
- getDownwardDependency(theStudy, geomObj, downLevelList, level++);
+
+ // if some selected object is not a main shape,
+ // we move it's main shapes into 'selected' list,
+ // because they could not be modified anyhow.
+ std::set<std::string> aToBeInSelected;
+ for ( it = aSelected.begin(); it != aSelected.end(); ++it ) {
+ Handle(GEOM_BaseObject) handle_object = _impl->GetObject( theStudy->StudyId(), (*it).c_str(), false);
+ if ( handle_object.IsNull() )
+ continue;
+
+ stringIOR = handle_object->GetIOR();
+ if ( !stringIOR.Length() > 1 )
+ continue;
+
+ geomObj = GetIORFromString( stringIOR.ToCString() );
+ while ( !geomObj->IsMainShape() ) {
+ geomObj = geomObj->GetMainShape();
+ anEntry = geomObj->GetEntry();
+
+ foundIt = aParents.find( anEntry );
+ if ( foundIt != aParents.end() )
+ aParents.erase( foundIt );
+
+ foundIt = aChildren.find( anEntry );
+ if ( foundIt != aChildren.end() )
+ aChildren.erase( foundIt );
+
+ foundIt = anOthers.find( anEntry );
+ if ( foundIt != anOthers.end() )
+ anOthers.erase( foundIt );
+
+ aToBeInSelected.insert( anEntry );
+ }
+ }
+ aSelected.insert( aToBeInSelected.begin(), aToBeInSelected.end() );
+
+ // fill the CORBA arrays with values from sets
+ int i;
+ theSelectedEntries.length( aSelected.size() );
+ for ( i = 0, it = aSelected.begin(); it != aSelected.end(); ++it, i++ )
+ theSelectedEntries[i] = CORBA::string_dup( (*it).c_str() );
+ theParentEntries.length( aParents.size() );
+ for ( i = 0, it = aParents.begin(); it != aParents.end(); ++it, i++ )
+ theParentEntries[i] = CORBA::string_dup( (*it).c_str() );
+ theSubEntries.length( aChildren.size() );
+ for ( i = 0, it = aChildren.begin(); it != aChildren.end(); ++it, i++ )
+ theSubEntries[i] = CORBA::string_dup( (*it).c_str() );
+ theOtherEntries.length( anOthers.size() );
+ for ( i = 0, it = anOthers.begin(); it != anOthers.end(); ++it, i++ )
+ theOtherEntries[i] = CORBA::string_dup( (*it).c_str() );
+ }
+}
+
+void GEOM_Gen_i::getParentDependencies(GEOM::GEOM_BaseObject_ptr geomObj,
+ std::set<std::string>& aSelected,
+ std::set<std::string>& aParents,
+ std::set<std::string>& aChildren,
+ std::set<std::string>& anOthers)
+{
+ std::string anEntry = geomObj->GetEntry();
+ if ( aSelected.count( anEntry ) == 0 ) {
+ aParents.insert( anEntry );
+ std::set<std::string>::iterator it;
+ it = aChildren.find( anEntry );
+ if ( it != aChildren.end() )
+ aChildren.erase( it );
+ it = anOthers.find( anEntry );
+ if ( it != anOthers.end() )
+ anOthers.erase( it );
+ }
+ // get dependencies for current object in the tree
+ GEOM::ListOfGBO_var depList = geomObj->GetDependency();
+ if( depList->length() == 0 )
+ return;
+ // go through dependencies of current object to check whether it depends on the given object
+ std::string aDepEntry;
+ for( int i = 0; i < depList->length(); i++ ) {
+ aDepEntry = depList[i]->GetEntry();
+ if ( depList[i]->_is_nil() ||
+ aDepEntry == anEntry || // skip self-depending
+ aSelected.count( aDepEntry ) > 0 || // skip selected objects
+ aParents.count( aDepEntry ) > 0 // skip already processed objects
+ )
+ continue;
+ getParentDependencies( depList[i], aSelected, aParents, aChildren, anOthers );
}
}