Exemple #1
0
def get_geom(ifc_elem, settings):
    from ifcopenshell.geom.occ_utils import shape_tuple
    from OCC.Core import BRepTools
    from OCC.Core.TopoDS import TopoDS_Compound

    try:
        pdct_shape = ifcopenshell.geom.create_shape(settings, inst=ifc_elem)
    except RuntimeError:
        print(f'unable to parse ifc_elem "{ifc_elem}"')
        return

    if isinstance(pdct_shape, shape_tuple):
        shape = pdct_shape[1]
    else:
        shape = pdct_shape.solid

    if type(shape) is not TopoDS_Compound:
        brep_data = pdct_shape.solid.brep_data
        ss = BRepTools.BRepTools_ShapeSet()
        ss.ReadFromString(brep_data)
        nb_shapes = ss.NbShapes()
        occ_shape = ss.Shape(nb_shapes)
    else:
        occ_shape = shape
    return occ_shape
Exemple #2
0
def create_shape_from_serialization(brep_object):
    brep_data, occ_shape, styles = None, None, ()

    is_product_shape = True
    try:
        brep_data = brep_object.geometry.brep_data
        styles = brep_object.geometry.surface_styles
    except BaseException:
        try:
            brep_data = brep_object.brep_data
            styles = brep_object.surface_styles
            is_product_shape = False
        except BaseException:
            pass

    styles = tuple(styles[i:i + 4] for i in range(0, len(styles), 4))

    if not brep_data:
        return shape_tuple(brep_object, None, styles)

    try:
        ss = BRepTools.BRepTools_ShapeSet()
        ss.ReadFromString(brep_data)
        occ_shape = ss.Shape(ss.NbShapes())
    except BaseException:
        pass

    if is_product_shape:
        return shape_tuple(brep_object, occ_shape, styles)
    else:
        return occ_shape
Exemple #3
0
 def draw_polyline(self, element, position):
     classes = self.get_classes(element["raw"], position)
     exp = BRepTools.BRepTools_WireExplorer(element["geometry"])
     points = []
     while exp.More():
         point = BRep.BRep_Tool.Pnt(exp.CurrentVertex())
         points.append((point.X() * self.scale, -point.Y() * self.scale))
         exp.Next()
     self.svg.add(self.svg.polyline(points=points,
                                    class_=" ".join(classes)))
    def build_new_face(self, face, zpos, product):
        exp = TopExp.TopExp_Explorer(face, TopAbs.TopAbs_WIRE)
        while exp.More():
            wireexp = BRepTools.BRepTools_WireExplorer(
                topods.Wire(exp.Current()))
            new_wire_builder = BRepBuilderAPI.BRepBuilderAPI_MakeWire()
            first_vertex = None
            previous_vertex = None
            while wireexp.More():
                current_vertex = wireexp.CurrentVertex()
                current_point = BRep.BRep_Tool.Pnt(current_vertex)
                # Dodgy technique to squash in Z axis
                current_point.SetZ(zpos)
                current_vertex = BRepBuilderAPI.BRepBuilderAPI_MakeVertex(
                    current_point).Vertex()
                if not first_vertex:
                    first_vertex = current_vertex
                if not previous_vertex:
                    previous_vertex = current_vertex
                else:
                    try:
                        new_wire_builder.Add(
                            topods.Edge(
                                BRepBuilderAPI.BRepBuilderAPI_MakeEdge(
                                    previous_vertex, current_vertex).Edge()))
                        previous_vertex = current_vertex
                    except:
                        pass
                wireexp.Next()

                # make last edge
                if not wireexp.More():
                    try:
                        new_wire_builder.Add(
                            topods.Edge(
                                BRepBuilderAPI.BRepBuilderAPI_MakeEdge(
                                    current_vertex, first_vertex).Edge()))
                    except:
                        pass
            try:
                new_wire = new_wire_builder.Wire()
                new_face = BRepBuilderAPI.BRepBuilderAPI_MakeFace(
                    new_wire).Face()
                self.background_elements.append({
                    'raw': product,
                    'geometry': new_wire,
                    'geometry_face': new_face,
                    'type': 'polygon',
                    'z': zpos
                })
            except:
                #print('Could not build face')
                pass
            exp.Next()
Exemple #5
0
    def _ifc_geom_to_shape(self, ifc_geom):
        from OCC.Core import BRepTools
        from OCC.Core.TopoDS import TopoDS_Compound

        if type(ifc_geom) is TopoDS_Compound:
            geom = ifc_geom
        elif type(ifc_geom.solid) is not TopoDS_Compound:
            brep_data = ifc_geom.solid.brep_data
            ss = BRepTools.BRepTools_ShapeSet()
            ss.ReadFromString(brep_data)
            nb_shapes = ss.NbShapes()
            geom = ss.Shape(nb_shapes)
        else:
            geom = ifc_geom.solid
        return geom
def do_cut(process_data):
    global_id, shape, section, trsf_data = process_data

    axis = gp.gp_Ax2(
        gp.gp_Pnt(trsf_data['top_left_corner'][0],
                  trsf_data['top_left_corner'][1],
                  trsf_data['top_left_corner'][2]),
        gp.gp_Dir(trsf_data['projection'][0], trsf_data['projection'][1],
                  trsf_data['projection'][2]),
        gp.gp_Dir(trsf_data['x_axis'][0], trsf_data['x_axis'][1],
                  trsf_data['x_axis'][2]))
    source = gp.gp_Ax3(axis)
    destination = gp.gp_Ax3(gp.gp_Pnt(0, 0, 0), gp.gp_Dir(0, 0, -1),
                            gp.gp_Dir(1, 0, 0))
    transformation = gp.gp_Trsf()
    transformation.SetDisplacement(source, destination)

    cut_polygons = []
    section = BRepAlgoAPI.BRepAlgoAPI_Section(section, shape).Shape()
    section_edges = get_booleaned_edges(section)
    if len(section_edges) <= 0:
        return cut_polygons
    wires = connect_edges_into_wires(section_edges)
    for i in range(wires.Length()):
        wire_shape = wires.Value(i + 1)

        transformed_wire = BRepBuilderAPI.BRepBuilderAPI_Transform(
            wire_shape, transformation)
        wire_shape = transformed_wire.Shape()

        wire = topods.Wire(wire_shape)
        face = BRepBuilderAPI.BRepBuilderAPI_MakeFace(wire).Face()

        points = []
        exp = BRepTools.BRepTools_WireExplorer(wire)
        while exp.More():
            point = BRep.BRep_Tool.Pnt(exp.CurrentVertex())
            points.append((point.X(), -point.Y()))
            exp.Next()
        cut_polygons.append({
            'global_id': global_id,
            'metadata': {},
            'points': points
        })
    return cut_polygons
Exemple #7
0
def serialize_shape(shape):
    shapes = BRepTools.BRepTools_ShapeSet()
    shapes.Add(shape)
    return shapes.WriteToString()