Use Networkx to manipulate an IFC file as a directed graph.
#graph.py
import ifcopenshell
import ifcgraph
import networkx as nx
import sys
fn = sys.argv[1]
ifc_file = ifcopenshell.open(fn)
G = ifcgraph.create_graph(ifc_file)
sorted = list(reversed(list(nx.topological_sort(G))))
SG = ifcgraph.get_subgraph(23946, G)
ifcgraph.draw_graph(SG)
python graph.py input.ifc
Output: