Added theAllSubChildren flag to the TObj_OcafObjectIterator to iterate all sub-children.
By default it still iterates only the first level of children.
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();
}
{
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();
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)
};
#include <TObj_Model.hxx>
#include <TObj_Object.hxx>
#include <TObj_ObjectIterator.hxx>
+#include <TObj_OcafObjectIterator.hxx>
#include <TObj_TModel.hxx>
#include <TObj_TNameContainer.hxx>
#include <TObjDRAW.hxx>
}
//=======================================================================
-//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() )
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++ )
{
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);
--- /dev/null
+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'"
+ }
+}