Bryan Cole a écrit : > Hi, > Hi Bryan,
> I've started experimenting with TopExp_Explorer in order extract > sub-shapes from Shapes. When I try to store the results of the iteration > in a python list, all but the last extracted shapes become invalid. It > seems that TopExp_Explorer is re-using some part of the data structure > it outputs as it iterates, so keeping references to the shapes it > generates doesn't work. > > Here's an example: > > from OCC import TopExp, BRepPrimAPI, TopAbs, TopoDS > > box = BRepPrimAPI.BRepPrimAPI_MakeBox(10., 20., 30.) > ex = TopExp.TopExp_Explorer(box.Shape(), TopAbs.TopAbs_EDGE) > > results = [] > > while ex.More(): > edge = TopoDS.TopoDS().Edge(ex.Current()) > print "is null?", bool(edge.IsNull()) > results.append(edge) > ex.Next() > > for edge in results: > print "null now?", bool(edge.IsNull()) > > Inside the while loop, none of the shapes are Null. Once the iteration > is complete, all the edges become Null. > At the end of the iteration, e.g. when ex.More() is False, the TopExp_Explorer instance is cleared (with the Clear() method). Then all the references to subshapes are lost and your list contains NULL elements. > What's the correct way to store a references to sub-Shapes? I guess I > need a shallow copy of the shape-object returned by Explorer.Current() > but I can't figure how to achieve this. > You just have to ReInit() the TopExp_Explorer instance at the end of the loop. Here is the corrected code: from OCC import TopExp, BRepPrimAPI, TopAbs, TopoDS box = BRepPrimAPI.BRepPrimAPI_MakeBox(10., 20., 30.) ex = TopExp.TopExp_Explorer(box.Shape(), TopAbs.TopAbs_EDGE) results = [] while ex.More(): edge = TopoDS.TopoDS().Edge(ex.Current()) print "is null?", bool(edge.IsNull()) results.append(edge) ex.Next() ex.ReInit() #to avoid loosing reference to objects found in the loop for edge in results: print "null now?", bool(edge.IsNull()) > cheers, > Cheers, > Bryan > Thomas _______________________________________________ Pythonocc-users mailing list Pythonocc-users@gna.org https://mail.gna.org/listinfo/pythonocc-users