]> OCCT Git - occt.git/commitdiff
0031323: OCAF, TObj - TObj_OcafObjectIterator does not go recursively to children...
authormpv <mpv@opencascade.com>
Mon, 14 Sep 2020 13:49:38 +0000 (16:49 +0300)
committerbugmaster <bugmaster@opencascade.com>
Sun, 27 Sep 2020 09:00:06 +0000 (12:00 +0300)
Added theAllSubChildren flag to the TObj_OcafObjectIterator to iterate all sub-children.
By default it still iterates only the first level of children.

src/TObj/TObj_OcafObjectIterator.cxx
src/TObj/TObj_OcafObjectIterator.hxx
src/TObjDRAW/TObjDRAW.cxx
tests/bugs/caf/bug31323 [new file with mode: 0644]

index 91dfbe24d04e5a27d215b58698a19c936acb0332..2e1a3634cd725872464dd1f41b4d3d2b6d13681c 100644 (file)
@@ -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();
index ee820418828ad3a03a6d6a90614f6dc6a0ed71d0..ce0320ae0331fbcb3b8c8a1ad712652b877d5939 100644 (file)
 
 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)
 };
 
index 9c6482ba432b6349effba9eb5f9fe3c0c1005fe0..da25d2bc9753efeec1961efe8b48949cb28e42d5 100644 (file)
@@ -30,6 +30,7 @@
 #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>
@@ -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 (file)
index 0000000..091552c
--- /dev/null
@@ -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'"
+  }
+}