def split_compound(compound): all_faces = get_faces(compound) planar_faces = list(filter(lambda x: Face(x).is_planar(), all_faces)) p1, v1 = gp_Pnt(50, 50, 25), gp_Vec(0, 0, -1) fc1 = make_face(gp_Pln(p1, vec_to_dir(v1)), -1000, 1000, -1000, 1000) # limited, not infinite plane bo = BOPAlgo_Builder() bo.AddArgument(copy.deepcopy(compound)) # bo.AddArgument(fc1) # display.DisplayShape(fc1, transparency=0.7) for f in planar_faces: gprop = BRepGProp_Face(f) normal_point = gp_Pnt(0, 0, 0) normal_vec = gp_Vec(0, 0, 0) gprop.Normal(0, 0, normal_point, normal_vec) big_face = make_face(gp_Pln(normal_point, vec_to_dir(normal_vec)), -1000, 1000, -1000, 1000) # limited, not infinite plane bo.AddArgument(big_face) # display.DisplayShape(big_face, transparency=0.7) bo.Perform() # print("error status: {}".format(bo.ErrorStatus())) top = Topo(bo.Shape()) result = [s for s in top.solids()] return result
def reflect(p0, v0, ax): ray = gp_Lin(p0, vec_to_dir(v0)) intersection = IntAna_IntConicQuad(ray, gp_Pln(ax), precision_Angular(), precision_Confusion()) p1 = intersection.Point(1) vx, vy = gp_Vec(1, 0, 0), gp_Vec(0, 1, 0) handle = Geom_Plane(ax) handle.D1(0.5, 0.5, gp_Pnt(), vx, vy) vz = vx.Crossed(vy) v1 = v0.Mirrored(gp_Ax2(p1, vec_to_dir(vz))) return p1, v1
def reflect(p0, v0, face): h_surf = BRep_Tool.Surface(face) ray = Geom_Line(gp_Lin(p0, vec_to_dir(v0))) uvw = GeomAPI_IntCS(ray.GetHandle(), h_surf).Parameters(1) u, v, w = uvw p1, vx, vy = gp_Pnt(), gp_Vec(), gp_Vec() GeomLProp_SurfaceTool.D1(h_surf, u, v, p1, vx, vy) vz = vx.Crossed(vy) vx.Normalize() vy.Normalize() vz.Normalize() v1 = v0.Mirrored(gp_Ax2(p1, vec_to_dir(vz))) return p1, v1
def __init__(self): plotocc.__init__(self) self.axs = gp_Ax3() p_array = TColgp_Array1OfPnt(1, 100) for idx, pt in enumerate(np.linspace(-2.0, 2.0, 100)): pnt = gp_Pnt(300 * np.sin(pt), 100 * np.cos(3 * pt), 0) p_array.SetValue(idx + 1, pnt) api = GeomAPI_PointsToBSpline(p_array) self.curv = api.Curve() print(self.curv) api = BRepOffsetAPI_ThruSections() api.SetSmoothing(True) num_list = [ 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 4, 4, 4 ] p1, v1, v2 = gp_Pnt(), gp_Vec(), gp_Vec() for idx, pt in enumerate(np.linspace(0.0, 0.5, len(num_list))): GeomLProp_CurveTool.D2(self.curv, pt, p1, v1, v2) v3 = v1.Crossed(v2) axis = gp_Ax3(p1, vec_to_dir(v1), vec_to_dir(v2)) poly = self.make_PolyWire(num=num_list[idx], radi=20, axs=axis) api.AddWire(poly) self.show_axs_pln(axis, scale=10) self.display.DisplayShape(poly) api.Build() self.display.DisplayShape(api.Shape()) write_step_file(api.Shape(), "./tmp/ThruPipe_Hex.stp") api = BRepOffsetAPI_ThruSections() # api.SetSmoothing(True) p1, v1, v2 = gp_Pnt(), gp_Vec(), gp_Vec() for idx, pt in enumerate(np.linspace(0.55, 1.0, 20)): GeomLProp_CurveTool.D2(self.curv, pt, p1, v1, v2) v3 = v1.Crossed(v2) axis = gp_Ax3(p1, vec_to_dir(v1), vec_to_dir(v2)) shft = 90 * pt poly = self.make_Ellip(rxy=[15, 10], shft=shft, axs=axis) api.AddWire(poly) self.display.DisplayShape(poly) api.Build() self.display.DisplayShape(api.Shape()) write_step_file(api.Shape(), "./tmp/ThruPipe_Ellip.stp")
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 run_beam_face(self, beam0=gp_Ax3(), shpe=TopoDS_Shape(), tr=0): v0 = dir_to_vec(beam0.Direction()) v1 = dir_to_vec(beam0.XDirection()) p0 = beam0.Location() lin = gp_Lin(beam0.Axis()) api = BRepIntCurveSurface_Inter() api.Init(shpe, lin, 1.0E-9) dst = np.inf num = 0 sxy = p0 uvw = [0, 0, 0] fce = None while api.More(): p1 = api.Pnt() dst1 = p0.Distance(p1) if dst1 < dst and api.W() > 1.0E-6: dst = dst1 uvw = [api.U(), api.V(), api.W()] sxy = api.Pnt() fce = api.Face() api.Next() else: api.Next() print(*uvw) u, v, w = uvw surf = BRepAdaptor_Surface(fce) prop = BRepLProp_SLProps(surf, u, v, 2, 1.0E-9) p1, vx, vy = prop.Value(), prop.D1U(), prop.D1V() vz = vx.Crossed(vy) if vz.Dot(v0) > 0: vz.Reverse() vx.Normalize() vy.Normalize() vz.Normalize() beam1 = gp_Ax3(p1, vec_to_dir(v0.Reversed()), vec_to_dir(v1.Reversed())) norm1 = gp_Ax3(p1, vec_to_dir(vz), vec_to_dir(vx)) if tr == 0: beam1.Mirror(norm1.Ax2()) if beam1.Direction().Dot(norm1.Direction()) < 0: beam1.ZReverse() elif tr == 1: beam1.ZReverse() return beam1
def GO_Prop(self, s=0): h_ini_wave = BRep_Tool.Surface(self.ini.wave) vz, v1, v2, r1, r2 = axs_curvature(h_ini_wave, 0.5, 0.5) r1_z = r1 + s / 2 r2_z = r2 + s / 2 ini_wave_axs = self.ini.beam.Translated( dir_to_vec(self.ini.beam.Direction()).Scaled(s / 2)) ini_wave_axs.SetXDirection(vec_to_dir(v1)) ini_wave = wavefront([r1_z, r2_z], ini_wave_axs) self.display.DisplayShape(ini_wave) r1_z = r1 + s r2_z = r2 + s self.ini_wave_axs = self.ini.beam.Translated( dir_to_vec(self.ini.beam.Direction()).Scaled(s)) self.ini_wave_axs.SetXDirection(vec_to_dir(v1)) self.ini_wave = wavefront([r1_z, r2_z], self.ini_wave_axs) self.display.DisplayShape(self.ini_wave)
def reflect(beam, face, axs=gp_Ax3()): p0, v0 = beam.Location(), dir_to_vec(beam.Direction()) h_surf = BRep_Tool.Surface(face) ray = Geom_Line(gp_Lin(p0, vec_to_dir(v0))) if GeomAPI_IntCS(ray, h_surf).NbPoints() == 0: print("Out of Surface", axs.Location()) pln = make_plane(axs.Location(), dir_to_vec(axs.Direction()), 500, -500, 500, -500) h_surf = BRep_Tool.Surface(pln) GeomAPI_IntCS(ray, h_surf).IsDone() uvw = GeomAPI_IntCS(ray, h_surf).Parameters(1) u, v, w = uvw p1, vx, vy = gp_Pnt(), gp_Vec(), gp_Vec() GeomLProp_SurfaceTool.D1(h_surf, u, v, p1, vx, vy) vz = vx.Crossed(vy) vx.Normalize() vy.Normalize() vz.Normalize() v1 = v0.Mirrored(gp_Ax2(p1, vec_to_dir(vz), vec_to_dir(vx))) return gp_Ax3(p1, vec_to_dir(v1), beam.XDirection().Reversed())
def Reflect(self, beam=gp_Ax3(), surf=make_plane()): h_surf = BRep_Tool.Surface(surf) ray = Geom_Line(beam.Location(), beam.Direction()) if GeomAPI_IntCS(ray, h_surf).NbPoints() == 0: beam_v1 = beam else: GeomAPI_IntCS(ray, h_surf).IsDone() uvw = GeomAPI_IntCS(ray, h_surf).Parameters(1) u, v, w = uvw p1, vx, vy = gp_Pnt(), gp_Vec(), gp_Vec() GeomLProp_SurfaceTool.D1(h_surf, u, v, p1, vx, vy) vz = vx.Crossed(vy) vx.Normalize() vy.Normalize() vz.Normalize() norm = gp_Ax3(p1, vec_to_dir(vz), vec_to_dir(vx)) beam_v0 = beam beam_v0.SetLocation(p1) beam_v1 = beam_v0.Mirrored(norm.Ax2()) beam_v1.XReverse() return beam_v1
def check_ground(self): if len(self.pts) != 1: ray = spl_2pnt(self.pts[-2], self.pts[-1]) vec = gp_Vec(self.pts[-2], self.pts[-1]) self.p_trce.Perform(ray, self.h_surf) if self.p_trce.NbPoints() != 0 and vec.Z() < 0: print() uvw = self.p_trce.Parameters(1) u, v, w = uvw p0, v0 = gp_Pnt(), gp_Vec() GeomLProp_CurveTool.D1(ray, w, p0, v0) p1, vx, vy = gp_Pnt(), gp_Vec(), gp_Vec() GeomLProp_SurfaceTool.D1(self.h_surf, u, v, p1, vx, vy) vz = vx.Crossed(vy) self.pts[-1] = p0 print(self.r.t, w, p0) print(v0) print(gp_Vec(self.pts[-2], self.pts[-1])) norm = gp_Ax3(p1, vec_to_dir(vz), vec_to_dir(vx)) v1 = v0 v1.Mirror(norm.Ax2()) self.r.set_initial_value(self.gen_condition(p0, v1), self.r.t + self.dt)
def Reflect(self): h_surf = BRep_Tool.Surface(self.tar.srf) g_line = gp_Lin(self.ini.beam.Location(), self.ini.beam.Direction()) ray = Geom_Line(g_line) if GeomAPI_IntCS(ray, h_surf).NbPoints(): self.tar.beam = reflect(self.ini.beam, self.tar.srf) else: pln = make_plane(self.tar.axs.Location(), dir_to_vec(self.tar.axs.Direction()), 500, -500, 500, -500) h_surf = BRep_Tool.Surface(pln) self.tar.beam = reflect(self.ini.beam, pln) print(self.ini.beam.Location()) print(self.tar.beam.Location()) GeomAPI_IntCS(ray, h_surf).IsDone() uvw = GeomAPI_IntCS(ray, h_surf).Parameters(1) u, v, w = uvw print(u, v, w) vz, v1, v2, r1, r2 = axs_curvature(h_surf, u, v) tar_surf_axs = gp_Ax3(self.tar.beam.Location(), vec_to_dir(vz), vec_to_dir(v1)) tar_surf = wavefront([r1, r2], tar_surf_axs) self.display.DisplayShape(tar_surf, color="BLUE") self.display.DisplayShape(axs_pln(tar_surf_axs)) self.GO_Prop(w) h_tar_wave = BRep_Tool.Surface(self.ini_wave) vz, v1, v2, r1, r2 = axs_curvature(h_tar_wave, u, v) tar_wave_axs = self.tar.beam.Translated(gp_Vec(0, 0, 0)) tar_wave_axs.SetXDirection(vec_to_dir(v1)) self.tar.wave = wavefront([r1, r2], tar_wave_axs) self.display.DisplayShape(self.tar.wave, color="RED") self.display.DisplayShape(axs_pln(tar_wave_axs))
def reflect_beam(self, beam0=gp_Ax3(), tr=0): v0 = dir_to_vec(beam0.Direction()) v1 = dir_to_vec(beam0.XDirection()) surf = BRep_Tool.Surface(self.surf) ray = Geom_Line(beam0.Axis()) uvw = GeomAPI_IntCS(ray, surf).Parameters(1) u, v, w = uvw p1, vx, vy = gp_Pnt(), gp_Vec(), gp_Vec() GeomLProp_SurfaceTool.D1(surf, u, v, p1, vx, vy) vz = vx.Crossed(vy) if vz.Dot(v0) > 0: vz.Reverse() vx.Normalize() vy.Normalize() vz.Normalize() self.beam = gp_Ax3(p1, vec_to_dir(v0.Reversed()), vec_to_dir(v1.Reversed())) self.norm = gp_Ax3(p1, vec_to_dir(vz), vec_to_dir(vx)) if tr == 0: self.beam.Mirror(self.norm.Ax2()) if self.beam.Direction().Dot(self.norm.Direction()) < 0: self.beam.ZReverse() elif tr == 1: self.beam.ZReverse()
def reflect_b2(self, num=1): h_surf = BRep_Tool.Surface(self.b2) ray = Geom_Line(self.beam.Axis()) self.RayTrace.Perform(ray, h_surf) if self.RayTrace.NbPoints() == 0: beam = self.beam else: self.num += 1 uvw = self.RayTrace.Parameters(num) u, v, w = uvw p1, vx, vy = gp_Pnt(), gp_Vec(), gp_Vec() GeomLProp_SurfaceTool.D1(h_surf, u, v, p1, vx, vy) vz = vx.Crossed(vy).Reversed() norm = gp_Ax3(p1, vec_to_dir(vz), vec_to_dir(vx)) self.show_axs_pln(norm, scale=10) beam = self.beam beam.SetLocation(p1) beam.SetDirection(beam.Direction().Reversed()) beam.Mirror(norm.Axis()) print(self.num, self.b2, p1) self.pts.append(p1) # self.beam.XReverse() # self.beam.Mirror(norm.Ax2()) return beam
def __init__(self): plotocc.__init__(self) self.axs = gp_Ax3() self.circle = Geom_Circle(gp_Circ(self.axs.Ax2(), 100)) p0, v1, v2 = gp_Pnt(), gp_Vec(), gp_Vec() GeomLProp_CurveTool.D2(self.circle, 0, p0, v1, v2) self.poly_axs = gp_Ax3(p0, vec_to_dir(v1)) for num in range(4, 9): self.poly = self.make_PolyWire( num=num, radi=20.0, axs=self.poly_axs) self.base = self.make_Thru(50) self.display.DisplayShape( self.base, transparency=0.7, color="BLUE") write_step_file(self.base, self.tmpdir + "ThruSurf_{:d}.stp".format(num))
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 reflect_axs2(beam, surf, axs=gp_Ax3(), indx=1): p0, v0 = beam.Location(), dir_to_vec(beam.Direction()) h_surf = BRep_Tool.Surface(surf) ray = Geom_Line(gp_Lin(p0, vec_to_dir(v0))) if GeomAPI_IntCS(ray, h_surf).NbPoints() == 0: return beam, beam, None elif GeomAPI_IntCS(ray, h_surf).NbPoints() == 1: return beam, beam, None GeomAPI_IntCS(ray, h_surf).IsDone() u, v, w = GeomAPI_IntCS(ray, h_surf).Parameters(indx) p1, vx, vy = gp_Pnt(), gp_Vec(), gp_Vec() GeomLProp_SurfaceTool.D1(h_surf, u, v, p1, vx, vy) vz = vx.Crossed(vy) vx.Normalize() vy.Normalize() vz.Normalize() v1 = v0.Mirrored(gp_Ax2(p1, vec_to_dir(vz), vec_to_dir(vx))) norm_ax = gp_Ax3(p1, vec_to_dir(vz), vec_to_dir(vx)) beam_ax = gp_Ax3(p1, vec_to_dir(v1), beam.XDirection().Reversed()) return beam_ax, norm_ax, 1
def surf_trf(pnt, vec, face): axs = gp_Ax3(pnt, vec_to_dir(vec)) trf = gp_Trsf() trf.SetTransformation(axs, gp_Ax3()) srf = face.Moved(TopLoc_Location(trf)) return srf
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(): num += 1 props = GProp_GProps() brepgprop_LinearProperties(exp.Current(), props) vol = props.Mass() print(vol)
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)
from OCC.Core.gp import gp_Pln from OCC.Core.gp import gp_Pnt, gp_Vec from OCC.Core.TopoDS import TopoDS_Compound from OCC.Core.BOPAlgo import BOPAlgo_MakerVolume, BOPAlgo_Builder from OCC.Core.BRep import BRep_Builder from OCCUtils.Topology import Topo from OCCUtils.Construct import make_box, make_face from OCCUtils.Construct import vec_to_dir if __name__ == "__main__": display, start_display, add_menu, add_function_to_menu = init_display() box = make_box(200, 200, 200) p1, v1 = gp_Pnt(50, 50, 50), gp_Vec(0, 0, -1) fc1 = make_face(gp_Pln(p1, vec_to_dir(v1)), -1000, 1000, -1000, 1000) # limited, not infinite plane p2, v2 = gp_Pnt(50, 50, 50), gp_Vec(0, 1, -1) fc2 = make_face(gp_Pln(p2, vec_to_dir(v2)), -1000, 1000, -1000, 1000) # limited, not infinite plane p3, v3 = gp_Pnt(50, 50, 25), gp_Vec(1, 1, -1) fc3 = make_face(gp_Pln(p3, vec_to_dir(v3)), -1000, 1000, -1000, 1000) # limited, not infinite plane bo = BOPAlgo_Builder() bo.AddArgument(box) bo.AddArgument(fc1) bo.AddArgument(fc2) bo.AddArgument(fc3)