IfcOpenShell - building a tree and select by ray

I followed the idea in the docs about building a geometry tree and selecting by ray.
However for some reason the following code doesn't work:

import multiprocessing
import ifcopenshell
import ifcopenshell.geom
import logging
logging.getLogger().setLevel(logging.DEBUG)

ifc_file = ifcopenshell.open(r'tests\models\cube.ifc')
tree = ifcopenshell.geom.tree()
settings = ifcopenshell.geom.settings()
settings.logger = logging.getLogger()
iterator = ifcopenshell.geom.iterator(settings, ifc_file, multiprocessing.cpu_count())
i = 0
origin = (0., 0., 0.)
direction = (1., 0., 0.)
length = 50.

if iterator.initialize():
    while True:
        # Use triangulation to build a BVH tree
        tree.add_element(iterator.get())
        i += 1
        if not iterator.next():
            break

    logging.debug(f"{i} elements added to tree.")    
    results = tree.select_ray(origin, direction, length)
    print(f"Number of results: {len(results)}")
    for result in results:
        print(ifc_file.by_id(result.instance.id()))

There aren't any errors, my results is just empty.
However, when I don't use the iterator, like this:

import multiprocessing
import ifcopenshell
import ifcopenshell.geom
import logging
logging.getLogger().setLevel(logging.DEBUG)

ifc_file = ifcopenshell.open(r'tests\models\cube.ifc')
tree = ifcopenshell.geom.tree()
settings = ifcopenshell.geom.settings()
settings.logger = logging.getLogger()
iterator = ifcopenshell.geom.iterator(settings, ifc_file, multiprocessing.cpu_count())
i = 0
origin = (0., 0., 0.)
direction = (1., 0., 0.)
length = 50.

mytree = ifcopenshell.geom.tree(ifc_file, settings)

results = mytree.select_ray(origin, direction, length)

for result in results:
    print(ifc_file.by_id(result.instance.id()))

Then I get the results I expect. So I'm pretty sure I'm using the iterator wrong.
Any advice much appreciated, thanks.

Comments

  • Note to my future self, using tree.add_element(iterator.get_native()) works.

Sign In or Register to comment.