def __init__(self, crv, srf): super(IntersectCurveSurface, self).__init__(crv, srf) # Perform # OCC intersection. csi = GeomAPI_IntCS(crv.object, srf.object) results = [] for i in range(1, csi.NbPoints() + 1): u, v, t = csi.Parameters(i, 0., 0., 0.) pc = crv.eval(t) ps = srf.eval(u, v) pi = mean([pc, ps], axis=0) pi = Point(*pi) results.append([(t, u, v), pi]) npts = len(results) self._set_results(npts, results)
class DBall(plotocc): def __init__(self): plotocc.__init__(self) self.b1 = self.get_face(gen_ellipsoid(rxyz=[100, 100, 105])) self.b2 = self.get_face(gen_ellipsoid(rxyz=[210, 210, 210])) self.beam = gp_Ax3(gp_Pnt(0, 150, 0), gp_Dir(1, 1.5, 0)) print(self.b1) h_surf = BRep_Tool.Surface(self.b2) ray = Geom_Line(self.beam.Axis()) self.RayTrace = GeomAPI_IntCS(ray, h_surf) print(self.RayTrace.NbPoints()) self.num = 0 self.pts = [self.beam.Location()] for i in range(5): self.beam = self.reflect_b1(num=1) self.beam = self.reflect_b1(num=2) self.beam = self.reflect_b2(num=1) self.beam = self.reflect_b2(num=2) def beam_run(self): for i in range(3): self.beam = self.reflect_b1() self.beam = self.reflect_b2() def get_face(self, sol): top_api = Topo(sol) print(top_api.number_of_faces()) for face in top_api.faces(): sol_face = face return sol_face def reflect_b1(self, num=1): h_surf = BRep_Tool.Surface(self.b1) 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) 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.Ax2()) print(self.num, self.b1, p1) self.pts.append(p1) # self.beam.XReverse() # self.beam.Mirror(norm.Ax2()) return beam 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 reflect(self, 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 export_file(self): builder = BRep_Builder() compound = TopoDS_Compound() builder.MakeCompound(compound) builder.Add(compound, self.b1) builder.Add(compound, self.b2) builder.Add(compound, make_polygon(self.pts)) self.export_stp(compound) def display_ball(self): #self.show_vec(self.beam, scale=50) self.display.DisplayShape(self.b1, transparency=0.7, color="RED") self.display.DisplayShape(self.b2, transparency=0.7, color="BLUE") self.display.DisplayShape(self.beam.Location()) self.show_axs_pln(scale=100) self.show_axs_pln(self.beam, scale=10) self.show_pts(self.pts) self.show() self.export_file()
class HexPlane(plotocc): 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) def make_PolyPlane(self, num=6, radi=1.0, shft=0.0, axs=gp_Ax3()): lxy = radi - 0.1 pnts = [] angl = 360 / num for i in range(num): thet = np.deg2rad(i * angl) + np.deg2rad(shft) x, y = radi * np.sin(thet), radi * np.cos(thet) pnts.append(gp_Pnt(x, y, 0)) pnts.append(pnts[0]) poly = make_polygon(pnts) brep = BRepBuilderAPI_MakeFace(gp_Pln(), poly) brep.Add(poly) face = brep.Face() face.Location(set_loc(gp_Ax3(), axs)) return face def make_PolySurf(self, num=6, radi=1.0, shft=0.0, axs=gp_Ax3()): lxy = radi - 0.1 pnts = [] angl = 360 / num for i in range(num): thet = np.deg2rad(i * angl) + np.deg2rad(shft) x, y = radi * np.sin(thet), radi * np.cos(thet) pnts.append(gp_Pnt(x, y, 0)) pnts.append(pnts[0]) poly = make_polygon(pnts) proj = BRepProj_Projection(poly, self.surf, gp_Pnt(0, 0, 10)) # print(proj.Current()) brep = BRepBuilderAPI_MakeFace(self.surf, poly) # brep.Add(poly) face = brep.Face() face.Location(set_loc(gp_Ax3(), axs)) return face def export_file(self): write_step_file(self.compound, self.tempname + ".stp") def display_Plane(self): self.display.DisplayShape(self.beam_line) self.display.DisplayShape(self.compound) self.show_axs_pln(scale=1.0) self.show()
class Particle(plotocc): def __init__(self, pnt=gp_Pnt(-10, 0, -10), vec=gp_Vec(10, 20, 100)): plotocc.__init__(self) self.solver = ode(self.newton).set_integrator('dopri5') self.initial_conditions = self.gen_condition(pnt, vec) self.m = 10.0 self.k = 0.0 ray = spl_2pnt() px = np.linspace(-1, 1, 50) * 750 py = np.linspace(-1, 1, 50) * 750 mesh = np.meshgrid(px, py) surf = mesh[0]**2 / 500 + mesh[1]**2 / 750 self.grd_axs = gp_Ax3(gp_Pnt(0, 0, 0), gp_Dir(-0.25, 0, 1)) #self.grd = pln_for_axs(self.grd_axs, [500, 500]) self.grd = spl_face(*mesh, surf) self.h_surf = BRep_Tool.Surface(self.grd) self.p_trce = GeomAPI_IntCS(ray, self.h_surf) self.pts, self.vel = [], [] def gen_condition(self, pnt=gp_Pnt(), vec=gp_Vec(0, 0, 1)): x, y, z = pnt.X(), pnt.Y(), pnt.Z() u, v, w = vec.X(), vec.Y(), vec.Z() return np.array([x, y, z, u, v, w]) def newton(self, t, Y): """ Computes the derivative of the state vector y according to the equation of motion: Y is the state vector (x, y, z, u, v, w) === (position, velocity). returns dY/dt. """ px, py, pz = Y[0], Y[1], Y[2] vx, vy, vz = Y[3], Y[4], Y[5] alpha = -self.k / self.m ux, uy, uz = alpha * vx, alpha * vy, alpha * vz #uy += -0.5 * py #uz -= cnt.g / (pz) uz -= cnt.g return np.array([vx, vy, vz, ux, uy, uz]) def compute_trajectory(self, t0=0.0, t1=10.0): self.r = ode(self.newton).set_integrator('dopri5') self.r.set_initial_value(self.initial_conditions, t0) positions = [] self.dt = 0.5 self.pts, self.vel = [], [] while self.r.successful() and self.r.t < t1: txt = "{:03.2f} / {:03.2f} ".format(self.r.t, t1) txt += "| {:03.2f} {:03.2f} {:03.2f} ".format(*self.r.y[:3]) txt += "| {:03.2f} {:03.2f} {:03.2f} ".format(*self.r.y[3:]) sys.stdout.write("\r" + txt) sys.stdout.flush() self.pts.append(gp_Pnt(*self.r.y[:3])) self.vel.append(gp_Vec(*self.r.y[3:])) self.check_ground() self.r.integrate(self.r.t + self.dt) print() poly = make_polygon(self.pts) self.display.DisplayShape(poly) for pnt in self.pts[::10]: self.display.DisplayShape(pnt) self.display.DisplayShape(self.grd, transparency=0.8, color="BLUE") 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)
from OCC.Core.TColgp import TColgp_Array1OfPnt from OCCUtils.Construct import make_plane from particle.base import spl_curv_pts if __name__ == '__main__': p0 = gp_Pnt(10, 10, 50) p1 = gp_Pnt(-20, -20, -50) pts, crv = spl_curv_pts([p0, p1]) pln = make_plane(vec_normal=gp_Vec(0, 1, 1)) h_surf = BRep_Tool.Surface(pln) api = GeomAPI_IntCS(crv, h_surf) print(api.IsDone()) print(api.NbSegments()) uvw = api.Parameters(1) u, v, w = uvw pnt_crv = gp_Pnt() pnt_1st = gp_Pnt() pnt_2nd = gp_Pnt() pnt_srf = gp_Pnt() GeomLProp_CurveTool.Value(crv, w, pnt_crv) GeomLProp_CurveTool.Value(crv, 0, pnt_1st) GeomLProp_CurveTool.Value(crv, 1, pnt_2nd) GeomLProp_SurfaceTool.Value(h_surf, u, v, pnt_srf) print(uvw) print(pnt_crv) print(pnt_1st) print(pnt_2nd) print(pnt_srf)