Remove elements from IFC in Python
Hi! I am currently writing my master thesis and need some help with handling IFC files. I wish to manipulate a m IFC file so that it removes all the elements that are not load bearing (say foreksample every element besides IfcBeam and IfcColum). Then I need to read this IFC file in Grasshopper by using the plugin GeometryGym.
I have managed to write a code that removes everything except IfcBeam and IfcColumn in Python, but when I tried reading it in Grasshopper there are no values in the file. However, the new file works in Solibri.
Here is my Python code:
f = ifcopenshell.open("My_file.ifc")
beams = f.by_type("IfcBeam")
for instance in f:
if not instance.is_a("IfcBeam") and not instance.is_a("IfcColumn"):
Does anyone have any experience with this and know what the problem may be?
Thank you so much in advance :D
Any chance you are able to share your .ifc file?
Of course, here it is! @Coen
Loads perfect in BIMVision and BlenderBIM too
Unfortunelaty I have no Rhino with Grasshopper, maybe you could try to ask here?
@Elisemunch you should be using the root.remove_product api function: https://blenderbim.org/docs-python/autoapi/ifcopenshell/api/root/remove_product/index.html
Removing an element is not as simple as you might think - there are many relationships that needs to be considered. For example, are associated properties, materials, profiles, etc also correctly removed?
I guess also ping @geometrygym .
Another approach FYI is to use the ExtractElements IfcPatch which effectively does the opposite, it "copies the element you choose to a new IFC model" instead of deleting the ones you don't want.
Are you sure that the file you posted is the one with elements removed? I see no reference of IfcOpenShell in the file to identify that this might have been post processed. I had a user share a file directly with me last week, and the file was missing IfcProject. This object is mandatory and our tools will not look for orphaned objects. It would explain the behaviour you report. If you are using a similar process, check open the file in a text editor and check the IfcProject is retained.
I agree with Dion, I have implemented removal of elements in my developments, but I'd advise to filter the objects to retain and duplicate them into the target file. Geometry Gym plugins can facilitate this as an alternative to IfcOpenShell.
Note that solibri visualizes all products in the file, ignoring whether it is orphaned or not from the spatial structure. I personally would prefer if Solibri did behave this way.
Thank you so much to everyone who answered! I am looking into your comments now and it is very clear that i missed some important things in my first code. I will keep you updated :D
@Moult I am looking into extracting the elements by using IfcPatch like you recommended, but keep getting the error "No module named "ifcpatch"". Have tried "import ifcpatch", to pip install, and googling of course, but cant get it to work. Do you know how I can fix this?
same here ... I do use this very very often ... I do not remove elements but use ifcpatch to create a new ifc.
see https://community.osarch.org/discussion/comment/14747/#Comment_14747 how to call ifcpatch
you need to adapt the directories to your ones ...
Thank you @bernd !