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 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 getSpaceShapesFromIfc(fileName): f = ifc.open(fileName) # Get a list of all walls in the file spaces = f.by_type("IfcSpace") # Create a list of wall representation shapes space_shapes = [] for space in spaces: shape = geom.create_shape(settings, space).geometry space_shapes.append((space, shape)) return space_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 = [] for slab in slabs: shape = geom.create_shape(settings, slab).geometry slab_shapes.append((slab, shape)) return slab_shapes
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 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 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()
def convert_brep_geom(ifc_entity, doc): """Convert ifc entity to a basic FreeCAD Part""" ios_shape = geom.create_shape(BREP_SETTINGS, ifc_entity) # occ stands for OpenCascade occ_shape = ios_shape.geometry.brep_data # Create FreeCAD shape from Open Cascade BREP fc_shape = Part.Shape() fc_shape.importBrepFromString(occ_shape) # Ifc lenght internal unit : meter. FreeCAD internal unit : mm. fc_shape.scale(SCALE) # Shape scale must be applied before shape placement else placement scale would be doubled fc_shape.Placement = ios_shape_to_fc_placement(ios_shape) # Add geometry to FreeCAD scenegraph (Coin) fc_part = doc.addObject("Part::Feature", "IfcPart") fc_part.Shape = fc_shape return fc_part
def convert_geom(ifc_entity, doc, settings): """Convert ifc entity to a basic FreeCAD Part""" shape = geom.create_shape(settings, ifc_entity) # occ stands for OpenCascade occ_shape = shape.geometry.brep_data # IfcOpenShell generate an Open Cascade BREP with open("IfcOpenShellSamples/brep_data", "w") as file: file.write(occ_shape) # Create FreeCAD shape from Open Cascade BREP fc_shape = Part.Shape() fc_shape.importBrepFromString(occ_shape) # Ifc lenght internal unit : meter. FreeCAD internal unit : mm. fc_shape.scale(1000) # Add geometry to FreeCAD scenegraph (Coin) fc_part = doc.addObject("Part::Feature", "IfcPart") fc_part.Shape = fc_shape return fc_part
ifc_file = ifcopenshell.open('ifcmodels/institute.ifc') settings = ifcopenshell.geom.settings() settings.set(settings.USE_WORLD_COORDS, True) mesh_dict={} for ifc_entity in ifc_file.by_type('IfcElement'): #iterating through every ifcelement if ifc_entity.Representation is None: #skipping elements that have no rep continue else: shape = geom.create_shape(settings, ifc_entity) verts,edges,faces = rawmeshfromshape(shape) mesh_dict[ifc_entity.GlobalId]={ 'verts':verts, 'edges':edges, 'faces':faces, 'type':ifc_entity.is_a(), } if ifc_entity.is_a('IfcDoor'): mesh_dict[ifc_entity.GlobalId].update({ 'operationtype':ifc_entity.IsTypedBy[0].RelatingType.OperationType, 'width':ifc_entity.OverallWidth, 'height':ifc_entity.OverallHeight,