mcneel / rhino3dm

Libraries based on OpenNURBS with a RhinoCommon style
MIT License
598 stars 135 forks source link

Infinite iteration #500

Closed sbaer closed 1 year ago

sbaer commented 1 year ago
import rhino3dm

model = rhino3dm.File3dm.Read("Cube.3dm")
for geo in model.Objects:
    obj = geo.Geometry
    if obj.ObjectType != rhino3dm.ObjectType.Brep:
        continue
    n = len(obj.Faces)
    print(n)
    for f in obj.Faces:
        print(f)

for last for loop listed above will end up in an infinite printing out of None. This is because python expects an exception to be thrown for stopping iteration. We need to dig into routines that look like

BND_BrepFace* BND_BrepFaceList::GetFace(int i)
{
  ON_BrepFace* face = m_brep->Face(i);
  if (nullptr == face)
    return nullptr;
  return new BND_BrepFace(face, &m_component_reference);
}

and change them to

BND_BrepFace* BND_BrepFaceList::GetFace(int i)
{
#if defined(ON_PYTHON_COMPILE)
  if (i >= Count())
    throw pybind11::index_error();
#endif

  ON_BrepFace* face = m_brep->Face(i);
  if (nullptr == face)
    return nullptr;
  return new BND_BrepFace(face, &m_component_reference);
}