def init_base(self, shape): self.base = shape self.base_vol = self.cal_vol(self.base) self.splitter = BOPAlgo_Splitter() self.splitter.AddArgument(self.base) print(self.cal_vol(self.base))
def __init__(self, touch=False, file=False): dispocc.__init__(self, touch=touch) self.prop = GProp_GProps() self.base = make_box(100, 100, 100) self.base_vol = self.cal_vol(self.base) self.splitter = BOPAlgo_Splitter() self.splitter.AddArgument(self.base) print(self.cal_vol(self.base))
def __init__(self): plotocc.__init__(self) self.prop = GProp_GProps() self.base = make_box(100, 100, 100) self.base_vol = self.cal_vol(self.base) self.splitter = BOPAlgo_Splitter() self.splitter.AddArgument(self.base) print(self.cal_vol(self.base)) from OCC.Display.qtDisplay import qtViewer3d self.app = self.get_app() self.wi = self.app.topLevelWidgets()[0] self.vi = self.wi.findChild(qtViewer3d, "qt_viewer_3d") self.on_select()
def make_lens1(body, face1, face2, face3): splitter = BOPAlgo_Splitter() splitter.AddArgument(body) splitter.AddTool(face1) splitter.AddTool(face2) splitter.AddTool(face3) splitter.Perform() print(splitter.Arguments()) print(splitter.ShapesSD()) exp = TopExp_Explorer(splitter.Shape(), TopAbs_SOLID) shp = [] while exp.More(): print(exp.Current()) shp.append(exp.Current()) exp.Next() return shp
def split_edge_with_face(event=None): display.EraseAll() p0 = gp_Pnt() vnorm = gp_Dir(1, 0, 0) pln = gp_Pln(p0, vnorm) face = BRepBuilderAPI_MakeFace(pln, -10, 10, -10, 10).Face() p1 = gp_Pnt(0, 0, 15) p2 = gp_Pnt(0, 0, -15) edge = BRepBuilderAPI_MakeEdge(p1, p2).Edge() # Initialize splitter splitter = BOPAlgo_Splitter() # Add the edge as an argument and the face as a tool. This will split # the edge with the face. splitter.AddArgument(edge) splitter.AddTool(face) splitter.Perform() edges = [] exp = TopExp_Explorer(splitter.Shape(), TopAbs_EDGE) while exp.More(): edges.append(exp.Current()) exp.Next() print('Number of edges in split shape: ', len(edges)) display.DisplayShape(edges[0], color='red') display.DisplayShape(edges[1], color='green') display.DisplayShape(edges[2], color='yellow') display.FitAll()
def split_face_with_edge(event=None): display.EraseAll() p0 = gp_Pnt() vnorm = gp_Dir(1, 0, 0) pln = gp_Pln(p0, vnorm) face = BRepBuilderAPI_MakeFace(pln, -10, 10, -10, 10).Face() p1 = gp_Pnt(0, 0, 15) p2 = gp_Pnt(0, 0, -15) edge = BRepBuilderAPI_MakeEdge(p1, p2).Edge() # Initialize splitter splitter = BOPAlgo_Splitter() # Add the face as an argument and the edge as a tool. This will split # the face with the edge. splitter.AddArgument(face) splitter.AddTool(edge) splitter.Perform() display.DisplayShape(splitter.Shape()) display.FitAll()
py = np.linspace(-1, 1, 200) * 110 / 2 mesh = np.meshgrid(px, py) dae_data = uiuc_dae_data() obj = plotocc() axs = gp_Ax3() ax1 = gp_Ax3(gp_Pnt(0, 0, -10), axs.Direction()) vec = gp_Vec(gp_Pnt(0, 0, -10), gp_Pnt(0, 0, 10)) face = obj.make_EllipWire(rxy=[50.0, 50.0], axs=ax1, skin=0) body = BRepPrimAPI_MakePrism(face, vec).Shape() data1 = (mesh[0]**2 / 750 + mesh[1]**2 / 750) + 6.0 data2 = (mesh[0]**2 / 1000 + mesh[1]**2 / 1000) - 6.0 face1 = spl_face(*mesh, data1, axs=axs) face2 = spl_face(*mesh, data2, axs=axs) splitter = BOPAlgo_Splitter() splitter.AddArgument(body) splitter.AddTool(face1) splitter.AddTool(face2) splitter.Perform() print(splitter.Arguments()) print(splitter.ShapesSD()) exp = TopExp_Explorer(splitter.Shape(), TopAbs_SOLID) shp = [] while exp.More(): print(exp.Current()) shp.append(exp.Current()) exp.Next() obj.show_axs_pln(axs, scale=20) obj.display.DisplayShape(face1, transparency=0.9, color="RED")
def slice(shape): # keep track of time for perf init_time = time.time() # get the axis-aligned bounding box and dimensions of shape center, [dx, dy, dz], box_shp = get_aligned_boundingbox(shape) # get maximum z coordinate z_max = center.Z() + dz / 2 # slice height in mm slice_height = 2 # 0.3 # number of shells (offsets) num_shells = 3 # distance between offsets shell_thickness = 0.4 # list of slicing plane heights # z_list = [i * slice_height for i in range(int(z_max / slice_height) + 1)] # splitter function splitter = BOPAlgo_Splitter() splitter.SetRunParallel(True) splitter.SetFuzzyValue(0.001) splitter.AddArgument(shape) # loop over slicing planes planes = {} for z in arange(0, z_max, slice_height): # create slicing plane plane = gp_Pln(gp_Pnt(0., 0., z), gp.DZ()) face = BRepBuilderAPI_MakeFace(plane).Shape() # add slicing plane (face) to dict planes[z] = face # add slicing plane to splitter algo splitter.AddTool(face) # split the shape splitter.Perform() # record the time spent split_time = time.time() # get the solid slices from the result exp = TopExp_Explorer(splitter.Shape(), TopAbs_SOLID) faces = [] while exp.More(): faces.extend(find_faces(exp.Current())) exp.Next() search_time = time.time() # display.EraseAll() # offset wires print('offsetting wires') wires = [] # loop over faces for f in faces: # display.DisplayShape(f, update=False) # make offsets of all wires in face # wires.append(make_offsets(f, num_shells, shell_thickness)) z = TopExp_Explorer(f, TopAbs_WIRE) while z.More(): wires.append(z.Current()) z.Next() offset_time = time.time() print('displaying offsets') for w in wires: wire_gcode(w) # update viewer when all is added: display.Repaint() current_time = time.time() print('Splitting time: ', split_time - init_time) print('Searching time: ', search_time - split_time) print('Offsetting time: ', offset_time - split_time) print('Display time: ', current_time - offset_time) print('Total time: ', current_time - init_time) viewer = get_viewer() viewer.sig_topods_selected.connect(on_select) start_display()
class CovExp(plotocc): def __init__(self): plotocc.__init__(self) self.prop = GProp_GProps() self.base = make_box(100, 100, 100) self.base_vol = self.cal_vol(self.base) self.splitter = BOPAlgo_Splitter() self.splitter.AddArgument(self.base) print(self.cal_vol(self.base)) from OCC.Display.qtDisplay import qtViewer3d self.app = self.get_app() self.wi = self.app.topLevelWidgets()[0] self.vi = self.wi.findChild(qtViewer3d, "qt_viewer_3d") self.on_select() def get_app(self): app = QApplication.instance() #app = qApp # checks if QApplication already exists if not app: app = QApplication(sys.argv) return app def on_select(self): self.vi.sig_topods_selected.connect(self._on_select) def _on_select(self, shapes): """ Parameters ---------- shape : TopoDS_Shape """ for shape in shapes: self.DumpTop(shape) def DumpTop(self, shape, level=0): """ Print the details of an object from the top down """ brt = BRep_Tool() s = shape.ShapeType() if s == TopAbs_VERTEX: pnt = brt.Pnt(topods_Vertex(shape)) dmp = " " * level dmp += "%s - " % get_type_as_string(shape) dmp += "%.5e %.5e %.5e" % (pnt.X(), pnt.Y(), pnt.Z()) print(dmp) else: dmp = " " * level dmp += get_type_as_string(shape) print(dmp) it = TopoDS_Iterator(shape) while it.More(): shp = it.Value() it.Next() self.DumpTop(shp, level + 1) def ShowDisplay(self): colors = ["BLUE", "RED", "GREEN", "YELLOW", "BLACK", "WHITE"] num = 0 sol_exp = TopExp_Explorer(self.splitter.Shape(), TopAbs_SOLID) while sol_exp.More(): num += 1 self.display.DisplayShape(sol_exp.Current(), color=colors[num % len(colors)], transparency=0.5) sol_exp.Next() self.display.FitAll() self.start_display() def fileout(self, dirname="./shp/"): num = 0 stp_file = dirname + "shp_{:04d}.stp".format(num) write_step_file(self.base, stp_file) sol_exp = TopExp_Explorer(self.splitter.Shape(), TopAbs_SOLID) while sol_exp.More(): num += 1 stp_file = dirname + "shp_{:04d}.stp".format(num) write_step_file(sol_exp.Current(), stp_file) sol_exp.Next() def split_run(self, num=5): for i in range(num): pnt = gp_Pnt(*np.random.rand(3) * 100) vec = gp_Vec(*np.random.randn(3)) pln = gp_Pln(pnt, vec_to_dir(vec)) fce = make_face(pln, -10000, 10000, -10000, 10000) self.splitter.AddTool(fce) self.splitter.Perform() def cal_len(self, shp=TopoDS_Shape()): brepgprop_LinearProperties(shp, self.prop) return self.prop.Mass() def cal_are(self, shp=TopoDS_Shape()): brepgprop_SurfaceProperties(shp, self.prop) return self.prop.Mass() def cal_vol(self, shp=TopoDS_Shape()): brepgprop_VolumeProperties(shp, self.prop) return self.prop.Mass() def prop_edge(self, edge=TopoDS_Edge()): edge_adaptor = BRepAdaptor_Curve(edge) edge_line = edge_adaptor.Line() return edge_line def face_init(self, face=TopoDS_Face()): self.face_num += 1 self.tmp_face = face self.tmp_plan = self.pln_on_face(self.tmp_face) self.tmp_axis = self.tmp_plan.Position() print(self.tmp_axis) if self.show == True: self.display.DisplayMessage(self.tmp_axis.Location(), "{:04d}".format(self.face_num)) pass def pln_on_face(self, face=TopoDS_Face()): face_adaptor = BRepAdaptor_Surface(face) face_trf = face_adaptor.Trsf() face_pln = face_adaptor.Plane() #face_dir = face_adaptor.Direction() face_umin = face_adaptor.FirstUParameter() face_vmin = face_adaptor.FirstVParameter() face_umax = face_adaptor.LastUParameter() face_vmax = face_adaptor.LastVParameter() face_u = (face_umax + face_umin) / 2 face_v = (face_vmax + face_vmin) / 2 face_pnt = face_adaptor.Value(face_u, face_v) return face_pln def face_expand(self, face=TopoDS_Face()): print(face) find_edge = LocOpe_FindEdges(self.tmp_face, face) find_edge.InitIterator() while find_edge.More(): if face not in self.face_cnt: edge = find_edge.EdgeTo() line = self.prop_edge(edge) plan = self.pln_on_face(face) plan_axs = plan.Position() line_axs = line.Position() print(self.tmp_axis.Axis()) print(plan.Position().Axis()) #print(self.cal_len(edge), self.cal_are(face)) new_face = self.face_rotate(face, line_axs) #self.face_tranfer(face, plan.Axis()) plan = self.pln_on_face(face) print(face, self.cal_are(face), plan) print(plan, plan.Axis()) self.face_cnt.append(face) find_edge.Next() # self.face_init(face) def face_tranfer(self, face=TopoDS_Face(), axs=gp_Ax1()): axs_3 = gp_Ax3(axs.Location(), axs.Direction()) trf = gp_Trsf() trf.SetTransformation(axs_3, self.tmp_axs3) loc_face = TopLoc_Location(trf) face.Location(loc_face) return face def face_rotate(self, face=TopoDS_Face(), axs=gp_Ax1()): plan = self.pln_on_face(face) plan_axs = plan.Position() pln_angle = self.tmp_axis.Angle(plan_axs) ref_angle = self.tmp_axis.Direction().AngleWithRef( plan_axs.Direction(), axs.Direction()) print(np.rad2deg(pln_angle), np.rad2deg(ref_angle)) trf = gp_Trsf() if np.abs(ref_angle) >= np.pi / 2: trf.SetRotation(axs, -ref_angle) elif 0 < ref_angle < np.pi / 2: trf.SetRotation(axs, np.pi - ref_angle) elif -np.pi / 2 < ref_angle < 0: trf.SetRotation(axs, -ref_angle - np.pi) else: trf.SetRotation(axs, -ref_angle) #trf.SetTransformation(axs3.Rotated(axs, angle), axs3) loc_face = TopLoc_Location(trf) new_face = face.Located(loc_face) # self.sol_builder.Add(new_face) self.face_lst.Append(new_face) # face.Location(loc_face) if self.show == True: self.display.DisplayShape(new_face) return new_face def prop_soild(self, sol=TopoDS_Solid()): self.sol_builder = TopoDS_Builder() sol_exp = TopExp_Explorer(sol, TopAbs_FACE) sol_top = TopologyExplorer(sol) #print(self.cal_vol(sol), self.base_vol) print(sol_top.number_of_faces()) self.face_lst = TopTools_ListOfShape() self.face_cnt = [] self.face_num = 0 self.face_init(sol_exp.Current()) #self.sol_builder.Add(sol, sol_exp.Current()) sol_exp.Next() while sol_exp.More(): face = sol_exp.Current() self.face_expand(face) sol_exp.Next() if self.file == True: stp_file = "./shp/shp_{:04d}.stp".format(self.sol_num) write_step_file(sol, stp_file) stp_file = "./shp/shp_{:04d}_exp.stp".format(self.sol_num) new_shpe = TopoDS_Compound() self.sol_builder.Add(gp_Pnt()) # self.sol_builder.MakeCompSolid(new_shpe) #write_step_file(new_shpe, stp_file) # if self.show == True: # self.display.DisplayShape(self.face_cnt) """self.face_init(face) sol_exp = TopExp_Explorer(sol, TopAbs_FACE) while sol_exp.More(): face = sol_exp.Current() self.face_expand(face) #self.face_init(sol_exp.Current()) sol_exp.Next()""" def prop_solids(self): sol_exp = TopExp_Explorer(self.splitter.Shape(), TopAbs_SOLID) self.sol_num = 0 while sol_exp.More(): self.sol_num += 1 self.prop_soild(sol_exp.Current()) sol_exp.Next()
from OCC.Core.TopTools import TopTools_ListOfShape from OCC.Core.TopExp import TopExp_Explorer from OCC.Core.GProp import GProp_GProps from OCC.Extend.DataExchange import write_step_file, write_stl_file from OCCUtils.Topology import Topo from OCCUtils.Construct import make_box, make_face from OCCUtils.Construct import vec_to_dir if __name__ == "__main__": num = 0 box = make_box(100, 100, 100) stpname = "{}/shp_{:04d}.stp".format("./shp/", num) write_step_file(box, stpname) splitter = BOPAlgo_Splitter() splitter.AddArgument(box) for i in range(7): pnt = gp_Pnt(*np.random.rand(3) * 100) vec = gp_Vec(*np.random.randn(3)) pln = gp_Pln(pnt, vec_to_dir(vec)) fce = make_face(pln, -1000, 1000, -1000, 1000) splitter.AddTool(fce) splitter.Perform() #shp_list = splitter.Generated() exp = TopExp_Explorer(splitter.Shape(), TopAbs_SOLID) shp = [] while exp.More():
class CovExp(dispocc): def __init__(self, touch=False, file=False): dispocc.__init__(self, touch=touch) self.prop = GProp_GProps() self.base = make_box(100, 100, 100) self.base_vol = self.cal_vol(self.base) self.splitter = BOPAlgo_Splitter() self.splitter.AddArgument(self.base) print(self.cal_vol(self.base)) def init_base(self, shape): self.base = shape self.base_vol = self.cal_vol(self.base) self.splitter = BOPAlgo_Splitter() self.splitter.AddArgument(self.base) print(self.cal_vol(self.base)) def fileout(self, dirname="./shp/"): num = 0 stp_file = dirname + "shp_{:04d}.stp".format(num) write_step_file(self.base, stp_file) sol_exp = TopExp_Explorer(self.splitter.Shape(), TopAbs_SOLID) while sol_exp.More(): num += 1 stp_file = dirname + "shp_{:04d}.stp".format(num) write_step_file(sol_exp.Current(), stp_file) sol_exp.Next() def split_run(self, num=5): for i in range(num): pnt = gp_Pnt(*np.random.rand(3) * 100) vec = gp_Vec(*np.random.randn(3)) pln = gp_Pln(pnt, vec_to_dir(vec)) fce = make_face(pln, -10000, 10000, -10000, 10000) self.splitter.AddTool(fce) self.splitter.Perform() def cal_len(self, shp=TopoDS_Shape()): brepgprop_LinearProperties(shp, self.prop) return self.prop.Mass() def cal_are(self, shp=TopoDS_Shape()): brepgprop_SurfaceProperties(shp, self.prop) return self.prop.Mass() def cal_vol(self, shp=TopoDS_Shape()): brepgprop_VolumeProperties(shp, self.prop) return self.prop.Mass() def prop_edge(self, edge=TopoDS_Edge()): edge_adaptor = BRepAdaptor_Curve(edge) edge_line = edge_adaptor.Line() return edge_line def face_tranfer(self, face=TopoDS_Face(), axs=gp_Ax1()): axs_3 = gp_Ax3(axs.Location(), axs.Direction()) trf = gp_Trsf() trf.SetTransformation(axs_3, self.tmp_axs3) loc_face = TopLoc_Location(trf) face.Location(loc_face) return face def pln_on_face(self, face=TopoDS_Face()): face_adaptor = BRepAdaptor_Surface(face) face_trf = face_adaptor.Trsf() face_pln = face_adaptor.Plane() #face_dir = face_adaptor.Direction() face_umin = face_adaptor.FirstUParameter() face_vmin = face_adaptor.FirstVParameter() face_umax = face_adaptor.LastUParameter() face_vmax = face_adaptor.LastVParameter() face_u = (face_umax + face_umin) / 2 face_v = (face_vmax + face_vmin) / 2 face_pnt = face_adaptor.Value(face_u, face_v) face_pln.SetLocation(face_pnt) return face_pln def face_expand(self, face=TopoDS_Face()): plan = self.pln_on_face(face) find_edge = LocOpe_FindEdges(self.tmp_face, face) find_edge.InitIterator() edge_n = 0 while find_edge.More(): edge = find_edge.EdgeTo() line = self.prop_edge(edge) e_curve, u0, u1 = BRep_Tool.Curve(edge) p = e_curve.Value((u0 + u1) / 2) i = (edge_n + self.tmp_face_n) % len(self.colors) self.display.DisplayShape(edge, color=self.colors[i]) self.display.DisplayMessage( p, "Face{:d}-Edge{:d}".format(self.tmp_face_n, edge_n)) plan_axs = plan.Position() line_axs = line.Position() line_axs.SetLocation(p) print() print("Face: {:d}, Edge: {:d}".format(self.tmp_face_n, edge_n)) print(self.tmp_axis.Axis()) print(plan.Position().Axis()) #print(self.cal_len(edge), self.cal_are(face)) self.face_rotate(face, line_axs) #self.face_tranfer(face, plan.Axis()) plan = self.pln_on_face(face) print(face, self.cal_are(face), plan) print(plan, plan.Axis()) find_edge.Next() edge_n += 1 def face_rotate(self, face=TopoDS_Face(), axs=gp_Ax1()): plan = self.pln_on_face(face) plan_axs = plan.Position() self.display.DisplayShape(plan_axs.Location()) v0 = dir_to_vec(self.tmp_axis.Direction()) v1 = dir_to_vec(plan_axs.Direction()) print(v0.Dot(v1)) lin_vec = gp_Vec(axs.Location(), plan_axs.Location()) edg_circl = Geom_Circle( gp_Circ( gp_Ax2(axs.Location(), axs.Direction(), vec_to_dir(lin_vec)), 5)) rim_u0, rim_u1 = edg_circl.FirstParameter(), edg_circl.LastParameter() rim_p0 = edg_circl.Value(rim_u0) pln_angle = self.tmp_axis.Angle(plan_axs) ref_angle = self.tmp_axis.Direction().AngleWithRef( plan_axs.Direction(), axs.Direction()) print(np.rad2deg(pln_angle), np.rad2deg(ref_angle)) rim_u2 = -ref_angle rim_p2 = edg_circl.Value(rim_u2) rim_angle = Geom_TrimmedCurve(edg_circl, rim_u0, rim_u2) trf = gp_Trsf() #trf.SetRotation(axs, 2*np.pi - ref_angle) if np.abs(ref_angle) >= np.pi / 2: trf.SetRotation(axs, -ref_angle) elif 0 < ref_angle < np.pi / 2: trf.SetRotation(axs, np.pi - ref_angle) elif -np.pi / 2 < ref_angle < 0: trf.SetRotation(axs, -ref_angle - np.pi) else: trf.SetRotation(axs, -ref_angle) #trf.SetTransformation(axs3.Rotated(axs, angle), axs3) loc_face = TopLoc_Location(trf) new_face = face.Moved(loc_face) self.display.DisplayShape(new_face, transparency=0.5) self.display.DisplayShape(rim_angle) self.display.DisplayShape(rim_p0) self.display.DisplayShape(rim_p2) return new_face def face_init(self, face=TopoDS_Face()): self.tmp_face = face self.tmp_plan = self.pln_on_face(self.tmp_face) self.tmp_axis = self.tmp_plan.Position() self.tmp_face_n = 0 self.show_axs_pln(self.tmp_axis, scale=20, name="Fix-Face") self.display.DisplayShape(self.tmp_face, color="RED") def prop_soild(self, sol=TopoDS_Solid()): sol_exp = TopExp_Explorer(sol, TopAbs_FACE) sol_top = TopologyExplorer(sol) print() print(sol, self.cal_vol(sol)) print(sol_top.number_of_faces()) self.face_init(sol_exp.Current()) sol_exp.Next() while sol_exp.More(): face = sol_exp.Current() self.face_expand(face) sol_exp.Next() self.tmp_face_n += 1 """self.face_init(face) sol_exp = TopExp_Explorer(sol, TopAbs_FACE) while sol_exp.More(): face = sol_exp.Current() self.face_expand(face) #self.face_init(sol_exp.Current()) sol_exp.Next()""" def prop_solids(self): sol_exp = TopExp_Explorer(self.splitter.Shape(), TopAbs_SOLID) while sol_exp.More(): self.prop_soild(sol_exp.Current()) sol_exp.Next() def show_split_solid(self): colors = ["BLUE", "RED", "GREEN", "YELLOW", "BLACK", "WHITE"] num = 0 sol_exp = TopExp_Explorer(self.splitter.Shape(), TopAbs_SOLID) while sol_exp.More(): num += 1 self.display.DisplayShape(sol_exp.Current(), color=colors[num % len(colors)], transparency=0.5) sol_exp.Next() self.ShowOCC()