def read_geom(ifc_path, ObjTypes): """Function to read elements from a ifc file Args: ifc_path (str): Full or relative path of the ifc file ObjTypes (list): IfcTypes of the elements want to get info back Returns: dict: Dictionary containing dictionaries with the vertices, edges and tag of each element, using the globally unique id """ ifc_file = ifcopenshell.open(ifc_path) settings = geom.settings() settings.set(settings.USE_WORLD_COORDS, True) objects_geometry = {} for ObjType in ObjTypes: ios_vertices = {} ios_edges = {} ios_elements = {} for ifc_entity in ifc_file.by_type( ObjType): # Retrieves all the elements of the given type shape = geom.create_shape( settings, ifc_entity) # Convert the ifc into a mesh ios_vertices[ifc_entity[0]] = shape.geometry.verts ios_edges[ifc_entity[0]] = shape.geometry.edges ios_elements[ifc_entity[0]] = ifc_entity.Name objects_geometry[ObjType] = { "vertices": ios_vertices, "edges": ios_edges, "elements": ios_elements } return objects_geometry
def display_geom(ifc_path, base_class="IfcProduct"): """Display geom in from file in FreeCAD""" ifc_file = ifcopenshell.open(ifc_path) # Define settings settings = geom.settings() settings.set(settings.USE_BREP_DATA, True) # Allow you to embed FreeCAD in python https://www.freecadweb.org/wiki/Embedding_FreeCAD FreeCADGui.showMainWindow() doc = FreeCAD.newDocument() parts = [ convert_geom(ifc_entity, doc, settings) for ifc_entity in ifc_file.by_type(base_class) ] # Set Draw Style to display mesh edges. Orient view and fit to wall FreeCADGui.runCommand("Std_DrawStyle", 1) for part in parts: FreeCADGui.Selection.addSelection(part) FreeCADGui.activeView().viewIsometric() FreeCADGui.SendMsgToActiveView("ViewSelection") FreeCADGui.exec_loop()
def runExportFuncs(): settings = geom.settings() settings.set(settings.USE_WORLD_COORDS, True) f = ifcopenshell.open("tmpIfcFile.ifc") product_li = [] faces_li = [] k = 0 for product in f.by_type("IfcProduct"): # print(product.is_a()) all_obj_verts = [] all_obj_faces = [] vertex_li = [] try: shape = ifcopenshell.geom.create_shape(settings, product) geo = shape.geometry obj_verts = procVerts(geo.verts) obj_faces = procFaces(obj_verts, geo.faces) all_obj_verts.append(obj_verts) all_obj_faces.append(obj_faces) except Exception as e: # print('error --> ', e) pass for faces in all_obj_faces: for obj in faces: for item in obj: vertex = {'x': item[0], 'y': item[1], 'z': item[2]} vertex_li.append(vertex) k += 1 prod = {"product_name": product.is_a(), "vertices": vertex_li} product_li.append(prod) return product_li
def toggleMesh(self, checked=False): "turns mesh display on/off" if not FreeCAD.ActiveDocument: FreeCAD.newDocument() if FreeCAD.ActiveDocument: if checked: if self.mesh: self.mesh.ViewObject.show() else: import importIFC s = importIFC.getScaling(self.ifc) s *= 1000 # ifcopenshell outputs its meshes in metres trf = None if s != 1: trf = FreeCAD.Matrix() trf.scale(s, s, s) basemesh = Mesh.Mesh() import ifcopenshell from ifcopenshell import geom s = geom.settings() s.set(s.USE_WORLD_COORDS, True) for product in self.products: try: m = geom.create_shape(s, product) g = m.geometry v = g.verts f = g.faces verts = [ FreeCAD.Vector(v[i:i + 3]) for i in range(0, len(v), 3) ] faces = [ tuple(f[i:i + 3]) for i in range(0, len(f), 3) ] omesh = Mesh.Mesh((verts, faces)) if trf: omesh.transform(trf) self.omeshes[product.id()] = omesh basemesh.addMesh(omesh) except: pass self.mesh = FreeCAD.ActiveDocument.addObject( "Mesh::Feature", "IFCMesh") self.mesh.Mesh = basemesh self.mesh.ViewObject.Transparency = 85 FreeCAD.ActiveDocument.recompute() FreeCADGui.Selection.clearSelection() FreeCADGui.Selection.addSelection(self.mesh) FreeCADGui.SendMsgToActiveView("ViewSelection") else: if self.mesh: self.mesh.ViewObject.hide() if self.currentmesh: self.currentmesh.ViewObject.hide()
def read_geom(ifc_path): ifc_file = ifcopenshell.open(ifc_path) settings = geom.settings() for ifc_entity in ifc_file.by_type("IfcWall"): shape = geom.create_shape(settings, ifc_entity) # ios stands for IfcOpenShell ios_vertices = shape.geometry.verts ios_edges = shape.geometry.edges ios_faces = shape.geometry.faces # IfcOpenShell store vertices in a single tuple, same for edges and faces print(ios_vertices) print(ios_edges) print(ios_faces) """ Above will result in : (0.0, 0.0, 0.0, 0.0, 0.0, 3.0, 10.0, 0.0, 0.0, 10.0, 0.0, 3.0, 10.0, 0.2, 0.0, 10.0, 0.2, 3.0, 0.0, 0.2, 0.0, 0.0, 0.2, 3.0) (0, 1, 1, 3, 0, 2, 2, 3, 2, 3, 2, 4, 3, 5, 4, 5, 4, 5, 5, 7, 4, 6, 6, 7, 6, 7, 0, 6, 1, 7, 0, 1, 0, 6, 0, 2, 4, 6, 2, 4, 1, 7, 1, 3, 5, 7, 3, 5) (1, 0, 3, 3, 0, 2, 3, 2, 4, 5, 3, 4, 5, 4, 7, 7, 4, 6, 7, 6, 0, 1, 7, 0, 0, 6, 2, 2, 6, 4, 3, 7, 1, 5, 7, 3) """ print(set(ios_edges)) print(set(ios_faces)) """ Above will result in : {0, 1, 2, 3, 4, 5, 6, 7} {0, 1, 2, 3, 4, 5, 6, 7} """ # Let's parse it and prepare it for FreeCAD import vertices = [ FreeCAD.Vector(ios_vertices[i:i + 3]) for i in range(0, len(ios_vertices), 3) ] edges = [ios_edges[i:i + 2] for i in range(0, len(ios_edges), 2)] faces = [ tuple(ios_faces[i:i + 3]) for i in range(0, len(ios_faces), 3) ] print( f"This {ifc_entity.is_a()} has been defined by {len(vertices)} vertices, {len(edges)} edges and {len(faces)} faces" ) print(vertices) print(edges) print(faces) """ Above will result in : This IfcWall has been defined by 8 vertices, 24 edges and 12 faces [(0.0, 0.0, 0.0), (0.0, 0.0, 3.0), (10.0, 0.0, 0.0), (10.0, 0.0, 3.0), (10.0, 0.2, 0.0), (10.0, 0.2, 3.0), (0.0, 0.2, 0.0), (0.0, 0.2, 3.0)] [(0, 1), (1, 3), (0, 2), (2, 3), (2, 3), (2, 4), (3, 5), (4, 5), (4, 5), (5, 7), (4, 6), (6, 7), (6, 7), (0, 6), (1, 7), (0, 1), (0, 6), (0, 2), (4, 6), (2, 4), (1, 7), (1, 3), (5, 7), (3, 5)] [(1, 0, 3), (3, 0, 2), (3, 2, 4), (5, 3, 4), (5, 4, 7), (7, 4, 6), (7, 6, 0), (1, 7, 0), (0, 6, 2), (2, 6, 4), (3, 7, 1), (5, 7, 3)] """ return {"vertices": vertices, "edges": edges, "faces": faces}
def toggleMesh(self, checked=False): "turns mesh display on/off" if FreeCAD.ActiveDocument: if checked: if self.mesh: self.mesh.ViewObject.show() else: basemesh = Mesh.Mesh() import ifcopenshell from ifcopenshell import geom s = geom.settings() s.set(s.USE_WORLD_COORDS, True) for product in self.products: try: m = geom.create_shape(s, product) g = m.geometry v = g.verts f = g.faces verts = [ FreeCAD.Vector(v[i:i + 3]) for i in range(0, len(v), 3) ] faces = [ tuple(f[i:i + 3]) for i in range(0, len(f), 3) ] omesh = Mesh.Mesh((verts, faces)) self.omeshes[product.id()] = omesh basemesh.addMesh(omesh) except: pass self.mesh = FreeCAD.ActiveDocument.addObject( "Mesh::Feature", "IFCMesh") self.mesh.Mesh = basemesh self.mesh.ViewObject.Transparency = 85 FreeCAD.ActiveDocument.recompute() FreeCADGui.Selection.clearSelection() FreeCADGui.Selection.addSelection(self.mesh) FreeCADGui.SendMsgToActiveView("ViewSelection") else: if self.mesh: self.mesh.ViewObject.hide()
import ifcopenshell as ifc from ifcopenshell import geom settings = geom.settings() settings.set(settings.USE_PYTHON_OPENCASCADE, True) def getWallShapesFromIfc(fileName): f = ifc.open(fileName) # Get a list of all walls in the file walls = f.by_type("IfcWall") # Create a list of wall representation shapes wall_shapes = [] for wall in walls: # print("doing stuff") shape = geom.create_shape(settings, wall).geometry if shape: # print("doing stuff22") wall_shapes.append((wall, shape)) return wall_shapes def getSlabShapesFromIfc(fileName): f = ifc.open(fileName) # Get a list of all walls in the file slabs = f.by_type("IfcSlab") # Create a list of wall representation shapes slab_shapes = []
# code for debuging and implementation of rebar ifc importer # code to copy for rebar import # may be there are some HACK s # there have been takes place renamming and moving methods and class # ************************************************************************************************ # ifc import into rebar2 objects ***************************************************************** # ************************************************************************************************ # ************************************************************************************************ # use new rebar2 module import ifcopenshell, rebar2, Part, Draft, os import lattice2Placement, lattice2JoinArrays, lattice2Executer from FreeCAD import Vector as vec from ifcopenshell.geom import settings prefs = settings() prefs.set(prefs.USE_BREP_DATA, True) prefs.set(prefs.USE_WORLD_COORDS, True) prefs.set(prefs.INCLUDE_CURVES, True) prefs.set(prefs.EXCLUDE_SOLIDS_AND_SURFACES, True) path_to_rebar2 = rebar2.__file__.rstrip(os.path.basename(rebar2.__file__)) ifcfile = path_to_rebar2 + "example_01_two_stirrups.ifc" # imports :-) # ifcfile = path_to_rebar2 + "example_02_channel_foundation.ifc" # imports :-) # ifcfile = path_to_rebar2 + "example_03_crane_foundation.ifc" # does import crap, was exported with old Allplan exporter # ifcfile = path_to_rebar2 + "example_04_vat.ifc" # imports :-) f = ifcopenshell.open(ifcfile) for rebar in f.by_type("IfcReinforcingBar"): ifc_shape_representation = rebar.Representation.Representations[0] ifc_swept_disk_solid = ifc_shape_representation.Items[