diff --git a/src/TObj/TObj_OcafObjectIterator.cxx b/src/TObj/TObj_OcafObjectIterator.cxx index 91dfbe24d0..2e1a3634cd 100644 --- a/src/TObj/TObj_OcafObjectIterator.cxx +++ b/src/TObj/TObj_OcafObjectIterator.cxx @@ -29,8 +29,10 @@ IMPLEMENT_STANDARD_RTTIEXT(TObj_OcafObjectIterator,TObj_LabelIterator) TObj_OcafObjectIterator::TObj_OcafObjectIterator (const TDF_Label& theLabel, const Handle(Standard_Type)& theType, - const Standard_Boolean theRecursive) - : TObj_LabelIterator( theLabel, theRecursive ), myType( theType ) + const Standard_Boolean theRecursive, + const Standard_Boolean theAllSubChildren) + : TObj_LabelIterator (theLabel, theRecursive), + myType (theType), myAllSubChildren (theAllSubChildren) { MakeStep(); } @@ -46,14 +48,14 @@ void TObj_OcafObjectIterator::MakeStep() { TDF_Label L = myIterator.Value(); Handle(TObj_Object) anObject; - if(TObj_Object::GetObj(L,anObject)) + if(TObj_Object::GetObj (L, anObject)) { - if (myType.IsNull() || anObject->IsKind( myType )) + if (myType.IsNull() || anObject->IsKind (myType)) { myObject = anObject; myNode = L; } - myIterator.NextBrother(); + myAllSubChildren ? myIterator.Next() : myIterator.NextBrother(); } else myIterator.Next(); diff --git a/src/TObj/TObj_OcafObjectIterator.hxx b/src/TObj/TObj_OcafObjectIterator.hxx index ee82041882..ce0320ae03 100644 --- a/src/TObj/TObj_OcafObjectIterator.hxx +++ b/src/TObj/TObj_OcafObjectIterator.hxx @@ -27,35 +27,28 @@ class TObj_OcafObjectIterator : public TObj_LabelIterator { - public: - /** - * Constructor - */ - - //! Creates the iterator on objects in the sub labels of theLabel - //! theType narrows a variety of iterated objects +public: + //! Creates the iterator on TObj objects on the sub-labels of theLabel. + //! @param theLabel start label for searching + //! @param theType type of the found objects, or all types if Null + //! @param theRecursive search children recursively, not only on sub-labels of theLabel + //! @param theAllSubChildren do not stop at the first level of children, but search for sub-children too Standard_EXPORT TObj_OcafObjectIterator (const TDF_Label& theLabel, const Handle(Standard_Type)& theType = NULL, - const Standard_Boolean theRecursive = Standard_False); - - protected: - /* - * Internal methods - */ + const Standard_Boolean theRecursive = Standard_False, + const Standard_Boolean theAllSubChildren = Standard_False); +protected: //! Shift iterator to the next object virtual Standard_EXPORT void MakeStep() Standard_OVERRIDE; - protected: - /** - * fields - */ - +protected: Handle(Standard_Type) myType; //!< type of objects to iterate on + Standard_Boolean myAllSubChildren; //!< to iterate all sub-children, do not stop on the first level - public: - //! CASCADE RTTI +public: + //! CASCADE RTTI DEFINE_STANDARD_RTTIEXT(TObj_OcafObjectIterator,TObj_LabelIterator) }; diff --git a/src/TObjDRAW/TObjDRAW.cxx b/src/TObjDRAW/TObjDRAW.cxx index 9c6482ba43..da25d2bc97 100644 --- a/src/TObjDRAW/TObjDRAW.cxx +++ b/src/TObjDRAW/TObjDRAW.cxx @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -419,12 +420,17 @@ static Standard_Integer addChild (Draw_Interpretor& di, Standard_Integer argc, c } //======================================================================= -//function : getChild +//function : getChildren //purpose : //======================================================================= -static Standard_Integer getChild (Draw_Interpretor& di, Standard_Integer argc, const char** argv) +static Standard_Integer getChildren (Draw_Interpretor& di, Standard_Integer argc, const char** argv) { - if (argc < 3) {di<<"Use "<< argv[0] << "DocName ObjName\n";return 1;} + if (argc < 3) + { + di << "Use " << argv[0] << "DocName ObjName [-all]\n"; + di << " -all: recurse to list children of all levels\n"; + return 1; + } Handle(TObjDRAW_Object) tObj = getObjByName( argv[1], argv[2] ); if ( tObj.IsNull() ) @@ -432,7 +438,12 @@ static Standard_Integer getChild (Draw_Interpretor& di, Standard_Integer argc, c di << "Error: Object " << argv[2] << " not found\n"; return 1; } - Handle(TObj_ObjectIterator) anItr = tObj->GetChildren(); + + bool aGetSubs = (argc > 3 && ! strcasecmp (argv[3], "-all")); + Handle(TObj_ObjectIterator) anItr = aGetSubs ? + new TObj_OcafObjectIterator(tObj->GetChildLabel(), NULL, Standard_True, Standard_True) : + tObj->GetChildren(); + int i = 0; for ( ; anItr->More(); anItr->Next(), i++ ) { @@ -519,8 +530,8 @@ void TObjDRAW::Init(Draw_Interpretor& di) di.Add ("TObjAddChild","DocName ObjName chldName \t: Add child object to indicated object", __FILE__, addChild, g); - di.Add ("TObjGetChildren","DocName ObjName \t: Returns list of children objects", - __FILE__, getChild, g); + di.Add ("TObjGetChildren","DocName ObjName [-all]\t: Returns list of children objects (-all to recurse)", + __FILE__, getChildren, g); di.Add("TObjHasModifications", "DocName ObjName \t: Returns status of modification of the object (if object has been modified 1, otherwise 0)", __FILE__, hasModifications, g); diff --git a/tests/bugs/caf/bug31323 b/tests/bugs/caf/bug31323 new file mode 100644 index 0000000000..091552ce58 --- /dev/null +++ b/tests/bugs/caf/bug31323 @@ -0,0 +1,36 @@ +puts "============" +puts "0031323: OCAF, TObj - TObj_OcafObjectIterator does not go recursively to children if type argument is used" +puts "============" +puts "" + +pload TOBJ QAcommands + +# create document with object and 3 levels of sub-objects +TObjNew TD1 +TObjAddObj TD1 obj +TObjAddChild TD1 obj sub1 +TObjAddChild TD1 obj sub2 +TObjAddChild TD1 sub1 sub11 +TObjAddChild TD1 sub1 sub12 +TObjAddChild TD1 sub1 sub13 +TObjAddChild TD1 sub12 sub121 + +# Check iteration without sub-children +set flat_list [TObjGetChildren TD1 obj] +if {[llength $flat_list] != 2} { + puts "Error : there must be two elements for the childrens only iteration, but got '$flat_list'" +} else { + if {[lsort $flat_list] != "sub1 sub2"} { + puts "Error : not all elements found in the flat list iteration. Must be 'sub1 sub2', but got '$flat_list'" + } +} + +# Check iteration with all sub-children +set all_subs [TObjGetChildren TD1 obj -all] +if {[llength $all_subs] != 6} { + puts "Error : there must be six elements for the all levels of childrens iteration, but got '$all_subs'" +} else { + if {[lsort $all_subs] != "sub1 sub11 sub12 sub121 sub13 sub2"} { + puts "Error : not all elements found in the flat list iteration. Must be 'sub1 sub11 sub12 sub121 sub13 sub2', but got '$all_subs'" + } +}