def read_cadfile(self, fileName, disp=True): print(fileName) base_dir = os.path.dirname(fileName) basename = os.path.basename(fileName) rootname, extname = os.path.splitext(fileName) if extname in [".stp", ".step"]: shpe = read_step_file(fileName) elif extname in [".igs", ".iges"]: shpe = read_iges_file(fileName) elif extname in [".stl"]: shpe = read_stl_file(fileName) elif extname in [".brep"]: shpe = TopoDS_Shape() builder = BRep_Builder() breptools_Read(shpe, fileName, builder) elif extname in [".geo"]: stlfile = self.import_geofile(fileName, 0.1) shpe = read_stl_file(stlfile) else: print("Incorrect file index") # sys.exit(0) if disp == True: self.display.DisplayShape(shpe, update=True) return shpe
def test_rule_motion_00(self): gcode = "G21 G94\n" gcode += "G54\n" gcode += "M6 T1 G43 H1\n" motions = [] for i in range(3): builder = BRep_Builder() shape = TopoDS_Shape() assert breptools_Read(shape, './input/tip_{}.brep'.format(i + 1), builder) tip_edge = OCCUtils.Topo(shape).edges().__next__() print(OCCUtils.Topo(shape).number_of_edges()) builder = BRep_Builder() shape = TopoDS_Shape() assert breptools_Read(shape, './input/shank_{}.brep'.format(i + 1), builder) shank_edge = OCCUtils.Topo(shape).edges().__next__() print(OCCUtils.Topo(shape).number_of_edges(), '\n') rm = HackerCAD.RuledMotion(tip_edge, shank_edge) motions.append(rm) pp = HackerCAD.PostProcessor() gcode += pp.process(motions) gcode += "M2\n" f = open("./output/bla.ngc", "w") f.write(gcode) f.close()
def load_shape(self, filename): self.shape_name = filename.split('/')[-1].split('.')[0] print(self.shape_name) self.shape = TopoDS_Shape() builder = BRep_Builder() breptools_Read(self.shape, filename, builder) cnt = 0 for face in TopologyExplorer(self.shape).faces(): self.face_ais[face] = display.DisplayShape(face) self.face_id[face] = cnt cnt += 1 self.face_label = [-1] * cnt
def move_to_box(iname, cname, wname, visualize=False): """Move foam to periodic box. Works on BREP files. Information about physical volumes is lost. Args: iname (str): input filename cname (str): output filename with cells wname (str): output filename with walls visualize (bool): show picture of foam morphology in box if True """ cells = TopoDS_Shape() builder = BRep_Builder() breptools_Read(cells, iname, builder) texp = TopologyExplorer(cells) solids = list(texp.solids()) cells = TopoDS_Compound() builder.MakeCompound(cells) box = BRepPrimAPI_MakeBox(gp_Pnt(1, -1, -1), 3, 3, 3).Shape() vec = gp_Vec(-1, 0, 0) solids = slice_and_move(solids, box, vec) box = BRepPrimAPI_MakeBox(gp_Pnt(-3, -1, -1), 3, 3, 3).Shape() vec = gp_Vec(1, 0, 0) solids = slice_and_move(solids, box, vec) box = BRepPrimAPI_MakeBox(gp_Pnt(-1, 1, -1), 3, 3, 3).Shape() vec = gp_Vec(0, -1, 0) solids = slice_and_move(solids, box, vec) box = BRepPrimAPI_MakeBox(gp_Pnt(-1, -3, -1), 3, 3, 3).Shape() vec = gp_Vec(0, 1, 0) solids = slice_and_move(solids, box, vec) box = BRepPrimAPI_MakeBox(gp_Pnt(-1, -1, 1), 3, 3, 3).Shape() vec = gp_Vec(0, 0, -1) solids = slice_and_move(solids, box, vec) box = BRepPrimAPI_MakeBox(gp_Pnt(-1, -1, -3), 3, 3, 3).Shape() vec = gp_Vec(0, 0, 1) solids = slice_and_move(solids, box, vec) create_compound(solids, cells, builder) breptools_Write(cells, cname) if visualize: display, start_display, _, _ = init_display() display.DisplayShape(cells, update=True) start_display() box = BRepPrimAPI_MakeBox(gp_Pnt(0, 0, 0), 1, 1, 1).Shape() walls = BRepAlgoAPI_Cut(box, cells).Shape() breptools_Write(walls, wname) if visualize: display, start_display, _, _ = init_display() display.DisplayShape(walls, update=True) start_display()
def test_adaptive(self): gcode = "G21 G94\n" gcode += "G54\n" gcode += "M6 T1 G43 H1\n" hex_flats = TopoDS_Shape() builder = BRep_Builder() assert breptools_Read(hex_flats, './inputs/hex_flats.brep', builder) t = OCCUtils.Topo(hex_flats) num_faces = 0 for face_i in t.faces(): print("face") a2d = HackerCAD.Adaptive2d(face_i) a2d.tool_diameter = 25.4 / 32.0 a2d.helix_diameter = a2d.tool_diameter * 0.35 a2d.depth = 5.0 a2d.helix_angle = 0.5 a2d.flip_ax_dir() wire = a2d.compute() breptools_Write(wire, "output/hex_flat_{}.brep".format(num_faces)) num_faces += 1 pp = HackerCAD.PostProcessor() gcode += pp.process(a2d.motions) gcode += "M2\n" f = open("./output/bla.ngc", "w") f.write(gcode) f.close()
def compute_cylinder(i): shape = TopoDS_Shape() builder = BRep_Builder() breptools_Read(shape, './face_pass_{}.brep'.format(i), builder) face = OCCUtils.Topo(shape).faces().__next__() iksolver = IKClient("127.0.0.1",8826) ttg = TurningToolpathGenerator(face,iksolver) #ttg.pitch = 1.0 ttg.v_initial_extension = 0 ttg.cutting_angle = 5 ttg.create_target_vis_edges = True ttg.makeHelixOnCyl() ttg.generate_tool_targets() ttg.write_target_edges("./pass_{}_targets.brep".format(i)) ttg.write_gcode("./pass_{}.ngc".format(i))
def test_001(self): shape = TopoDS_Shape() builder = BRep_Builder() assert breptools_Read(shape, "./input/cylinder.brep", builder) face = OCCUtils.Topo(shape).faces().__next__() ct = HackerCAD.CylindricalTurning(face) edge = ct.compute() breptools_Write(edge, "output/path.brep")
def test_adaptive(self): square_pacman = TopoDS_Shape() builder = BRep_Builder() assert breptools_Read(square_pacman, './inputs/square_pacman.brep', builder) t = OCCUtils.Topo(square_pacman) square_pacman = t.faces().__next__() hacker_a2d = HackerCAD.Adaptive2d(square_pacman) hacker_a2d.step_over_factor = 0.9 hacker_a2d.z_lift_distance = 0.0 wire = hacker_a2d.compute() breptools_Write(wire, "output/adaptive.brep")
def read_brep(brep_filepath): """ This function writes a 3D model into brep format. Parameters ---------- brep_filepath : str The file path of the brep file. Returns ------- occtopology : OCCtopology Geometries read from the brep. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex """ from OCC.Core.BRepTools import breptools_Read from OCC.Core.TopoDS import TopoDS_Shape from OCC.Core.BRep import BRep_Builder shape = TopoDS_Shape() builder = BRep_Builder() breptools_Read(shape, brep_filepath, builder) return shape
def get_brep(): cylinder_head = TopoDS_Shape() builder = BRep_Builder() breptools_Read(cylinder_head, './core_example/cylinder_head.brep', builder) return cylinder_head
##the Free Software Foundation, either version 3 of the License, or ##(at your option) any later version. ## ##pythonOCC is distributed in the hope that it will be useful, ##but WITHOUT ANY WARRANTY; without even the implied warranty of ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##GNU Lesser General Public License for more details. ## ##You should have received a copy of the GNU Lesser General Public License ##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. from OCC.Display.WebGl import threejs_renderer from OCC.Core.BRep import BRep_Builder from OCC.Core.TopoDS import TopoDS_Shape from OCC.Core.BRepTools import breptools_Read from OCC.Extend.TopologyUtils import TopologyExplorer # loads brep shape cylinder_head = TopoDS_Shape() builder = BRep_Builder() breptools_Read(cylinder_head, 'models/cylinder_head.brep', builder) # render cylinder head in x3dom my_renderer = threejs_renderer.ThreejsRenderer() all_faces = TopologyExplorer(cylinder_head).faces() # display each face for face in all_faces: my_renderer.DisplayShape(face) my_renderer.render()
##You should have received a copy of the GNU Lesser General Public License ##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. import sys from OCC.Display.SimpleGui import init_display from OCC.Core.Graphic3d import Graphic3d_RenderingParams from OCC.Core.BRepTools import breptools_Read from OCC.Core.TopoDS import TopoDS_Shape from OCC.Core.BRep import BRep_Builder display, start_display, add_menu, add_function_to_menu = init_display() # loads the motor model motor_c = TopoDS_Shape() builder = BRep_Builder() breptools_Read(motor_c, '../assets/models/Motor-c.brep', builder) display.DisplayShape(motor_c, update=True) def perspective(event=None): display.SetPerspectiveProjection() display.FitAll() def orthographic(event=None): display.SetOrthographicProjection() display.FitAll() def anaglyph_red_cyan(event=None): display.SetAnaglyphMode(Graphic3d_RenderingParams.Anaglyph_RedCyan_Simple)
def read_file(self): r"""Read the BREP file and stores the result in a TopoDS_Shape""" shape = TopoDS_Shape() builder = BRep_Builder() breptools_Read(shape, self._filename, builder) self._shape = shape
def get_brep(): cylinder_head = TopoDS_Shape() builder = BRep_Builder() breptools_Read(cylinder_head, '../assets/models/cylinder_head.brep', builder) return cylinder_head
## ##This file is part of pythonOCC. ## ##pythonOCC is free software: you can redistribute it and/or modify ##it under the terms of the GNU Lesser General Public License as published by ##the Free Software Foundation, either version 3 of the License, or ##(at your option) any later version. ## ##pythonOCC is distributed in the hope that it will be useful, ##but WITHOUT ANY WARRANTY; without even the implied warranty of ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##GNU Lesser General Public License for more details. ## ##You should have received a copy of the GNU Lesser General Public License ##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. from OCC.Display.WebGl import x3dom_renderer from OCC.Core.BRep import BRep_Builder from OCC.Core.TopoDS import TopoDS_Shape from OCC.Core.BRepTools import breptools_Read # loads brep shape cylinder_head = TopoDS_Shape() builder = BRep_Builder() breptools_Read(cylinder_head, '../assets/models/cylinder_head.brep', builder) # render cylinder head in x3dom my_renderer = x3dom_renderer.X3DomRenderer() my_renderer.DisplayShape(cylinder_head) my_renderer.render()
import sys from OCC.Core.gp import gp_Vec from OCC.Core.Graphic3d import Graphic3d_ClipPlane from OCC.Core.Quantity import Quantity_Color, Quantity_TOC_RGB from OCC.Core.BRepTools import breptools_Read from OCC.Core.TopoDS import TopoDS_Shape from OCC.Core.BRep import BRep_Builder from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display() cylinder_head = TopoDS_Shape() builder = BRep_Builder() breptools_Read(cylinder_head, 'cylinder_head.brep', builder) ais_shp = display.DisplayShape(cylinder_head)[0] # clip plane number one, by default xOy clip_plane_1 = Graphic3d_ClipPlane() # set hatch on clip_plane_1.SetCapping(True) clip_plane_1.SetCappingHatch(True) # off by default, user will have to enable it clip_plane_1.SetOn(False) # set clip plane color aMat = clip_plane_1.CappingMaterial()
I = 0.001 J = 0.001 K = 0.001 # tool target direction U = 0.00 V = 0.00 W = 1.00 x,y,z,a,b = ik_client.solve((I,J,K,U,V,W),1e-6,False) """ ## Load the face for the first pass that was created earlier in FreeCAD shape = TopoDS_Shape() builder = BRep_Builder() breptools_Read(shape, './pass_2_surface.brep', builder) face = OCCUtils.Topo(shape).faces().__next__() ## Higher level use of the ik_client # The inverse kinematics solver gets invoked may times by TurningToolpathGenerator. ttg = TurningToolpathGenerator(face, ik_client) ttg.ball_radius = 25.4 / 16 ttg.cutting_angle = 5 ttg.inside = False # False -> cut outside ttg.reverse_helix = True ttg.initial_extension = 1 ttg.output_offset = [0, 0, 0, 90, 0] ttg.create_target_vis_edges = True ttg.makeHelixOnCyl() ttg.generate_tool_targets()