def compas_mesh_to_occ_shell(mesh: Mesh) -> TopoDS_Shell: """Convert a general COMPAS mesh to an OCC shell. Parameters ---------- mesh : :class:`~compas.datastructures.Mesh` A COMPAS mesh data structure. Returns ------- TopoDS_Shell """ # https://github.com/tpaviot/pythonocc-demos/blob/master/examples/core_geometry_geomplate.py shell = TopoDS_Shell() builder = BRep_Builder() builder.MakeShell(shell) for face in mesh.faces(): points = mesh.face_coordinates(face) if len(points) == 3: builder.Add(shell, triangle_to_face(points)) elif len(points) == 4: builder.Add(shell, quad_to_face(points)) else: builder.Add(shell, ngon_to_face(points)) return shell
def __init__(self, faces): topods_shell = TopoDS_Shell() builder = BRep_Builder() builder.MakeShell(topods_shell) for face in faces: builder.Add(topods_shell, face.object) self._shell = Shell(topods_shell)
def compas_quadmesh_to_occ_shell(mesh: Mesh) -> TopoDS_Shell: """Convert a COMPAS quad mesh to an OCC shell. Parameters ---------- mesh : :class:`~compas.datastructures.Mesh` A COMPAS mesh data structure with quad faces. Returns ------- TopoDS_Shell Raises ------ AssertionError If the input mesh is not a quad mesh. """ assert mesh.is_quadmesh(), "The input mesh is not a quad mesh." shell = TopoDS_Shell() builder = BRep_Builder() builder.MakeShell(shell) for face in mesh.faces(): points = mesh.face_coordinates(face) builder.Add(shell, quad_to_face(points)) return shell
def from_polygons(cls, polygons: List[compas.geometry.Polygon]) -> 'BRep': """Construct a BRep from a set of polygons. Parameters ---------- polygons : list[:class:`~compas.geometry.Polygon`] Returns ------- :class:`~compas_occ.brep.BRep` """ shell = TopoDS_Shell() builder = BRep_Builder() builder.MakeShell(shell) for points in polygons: if len(points) == 3: builder.Add(shell, triangle_to_face(points)) elif len(points) == 4: builder.Add(shell, quad_to_face(points)) else: builder.Add(shell, ngon_to_face(points)) brep = BRep() brep.shape = shell return brep
def from_mesh(cls, mesh: compas.datastructures.Mesh) -> 'BRep': """Construct a BRep from a COMPAS mesh. Parameters ---------- mesh : :class:`~compas.datastructures.Mesh` Returns ------- :class:`~compas_occ.brep.BRep` """ shell = TopoDS_Shell() builder = BRep_Builder() builder.MakeShell(shell) for face in mesh.faces(): points = mesh.face_coordinates(face) if len(points) == 3: builder.Add(shell, triangle_to_face(points)) elif len(points) == 4: builder.Add(shell, quad_to_face(points)) else: builder.Add(shell, ngon_to_face(points)) brep = BRep() brep.shape = shell return brep
def main(): vertices = [gp_Pnt(p[0], p[1], p[2]) for p in mesh['vertices']] oFaces = [] builder = BRep_Builder() shell = TopoDS_Shell() builder.MakeShell(shell) for face in mesh['faces']: edges = [] face.reverse() for i in range(len(face)): cur = face[i] nxt = face[(i + 1) % len(face)] segment = GC_MakeSegment(vertices[cur], vertices[nxt]) edges.append(BRepBuilderAPI_MakeEdge(segment.Value())) wire = BRepBuilderAPI_MakeWire() for edge in edges: wire.Add(edge.Edge()) oFace = BRepBuilderAPI_MakeFace(wire.Wire()) builder.Add(shell, oFace.Shape()) write_stl_file(shell, "./cube_binding.stl")
def to_shell(self): """ Create a shell from the face. :return: The shell. :rtype: afem.topology.entities.Shell """ topods_shell = TopoDS_Shell() builder = BRep_Builder() builder.MakeShell(topods_shell) builder.Add(topods_shell, self.object) return Shell(topods_shell)
def by_face(face): """ Create a shell from a face. :param afem.topology.entities.Face face: The face. :return: The shell. :rtype: afem.topology.entities.Shell """ topods_shell = TopoDS_Shell() builder = BRep_Builder() builder.MakeShell(topods_shell) builder.Add(topods_shell, face.object) return Shell(topods_shell)
def test_check_shape(): r"""check_shape() tests""" # Null shapes should raise a ValueError with pytest.raises(ValueError): check_shape(TopoDS_Shape()) with pytest.raises(ValueError): check_shape(TopoDS_Shell()) builderapi_makeedge = BRepBuilderAPI_MakeEdge(gp_Pnt(), gp_Pnt(10, 10, 10)) shape = builderapi_makeedge.Shape() # a ValueError should be raised is check_shape() is not give # a TopoDS_Shape or subclass with pytest.raises(ValueError): check_shape(gp_Pnt()) with pytest.raises(ValueError): check_shape(builderapi_makeedge) # a TopoDS_Shape should pass the check without raising any exception check_shape(shape) # a subclass of shape should not raise any exception check_shape(Topo(shape, return_iter=False).edges[0])
from OCC.Core.GeomAbs import GeomAbs_C0 from compas.datastructures import Mesh from compas_view2.app import App tubemesh = Mesh.from_obj(compas.get('tubemesh.obj')) print(tubemesh.number_of_vertices()) print(tubemesh.number_of_faces()) # ============================================================================== # To OCC # ============================================================================== shell = TopoDS_Shell() builder = BRep_Builder() builder.MakeShell(shell) points = tubemesh.vertices_attributes('xyz') for face in tubemesh.faces(): brep = BRepFill_Filling() for u, v in tubemesh.face_halfedges(face): edge = BRepBuilderAPI_MakeEdge(gp_Pnt(*points[u]), gp_Pnt(*points[v])).Edge() brep.Add(edge, GeomAbs_C0, True) brep.Build()
def __init__(self): plotocc.__init__(self) self.compound = TopoDS_Compound() self.builder = BRep_Builder() self.builder.MakeCompound(self.compound) self.beam = gp_Ax3() self.beam.SetLocation(gp_Pnt(0.5, 0.5, 0.0)) self.beam.SetDirection(gp_Dir(0.0, 0.5, 1.0)) self.beam_line = line_from_axs(self.beam, length=20) self.builder.Add(self.compound, self.beam_line) ax = gp_Ax3(gp_Pnt(0, 0, 10), gp_Dir(0, 0, -1)) px = np.linspace(-1, 1, 10) * 10 py = np.linspace(-1, 1, 10) * 10 mesh = np.meshgrid(px, py) surf = mesh[0]**2 / 100 + mesh[1]**2 / 150 self.surf = spl_face(*mesh, surf, ax) self.surf_bound = self.make_PolySurf(radi=5, axs=ax) self.beam_glin = Geom_Line(self.beam.Location(), self.beam.Direction()) self.ics = GeomAPI_IntCS(self.beam_glin, BRep_Tool.Surface(self.surf)) print(self.ics.NbPoints()) # print(self.ics.Point(1)) self.ics = GeomAPI_IntCS(self.beam_glin, BRep_Tool.Surface(self.surf_bound)) print(self.ics.NbPoints()) #self.display.DisplayShape(self.surf, transparency=0.7) self.display.DisplayShape(self.surf_bound, transparency=0.7) self.plns = TopoDS_Shell() self.builder.MakeShell(self.plns) for ix in np.linspace(0, 1, 5): for iy in np.linspace(0, 1, 5): p1, vx, vy = gp_Pnt(), gp_Vec(), gp_Vec() GeomLProp_SurfaceTool.D1(BRep_Tool.Surface(self.surf), ix, iy, p1, vx, vy) vz = vx.Crossed(vy) axs = gp_Ax3(p1, vec_to_dir(vz), vec_to_dir(vx)) pln = self.make_PolyPlane(axs=axs, radi=2.5, shft=15.0) print(pln) self.builder.Add(self.compound, make_vertex(p1)) self.builder.Add(self.plns, pln) self.builder.Add(self.compound, self.plns) for face in Topo(self.plns).faces(): self.ics.Perform(self.beam_glin, BRep_Tool.Surface(face)) uvw = self.ics.Parameters(1) u, v, w = uvw p1, vx, vy = gp_Pnt(), gp_Vec(), gp_Vec() GeomLProp_SurfaceTool.D1(BRep_Tool.Surface(face), u, v, p1, vx, vy) vz = vx.Crossed(vy) if u > 0 and v > 0: print(u, v) print(p1) print(self.ics.Point(1)) self.display.DisplayShape(p1) self.display.DisplayShape(face, color="BLUE") else: print(u, v)