def make_axis_triedron(self): self.x_axis = AIS_Axis( Geom_Line(gp_Lin(gp_Pnt(0, 0, 0), gp_Dir(gp_XYZ(1, 0, 0))))) self.y_axis = AIS_Axis( Geom_Line(gp_Lin(gp_Pnt(0, 0, 0), gp_Dir(gp_XYZ(0, 1, 0))))) self.z_axis = AIS_Axis( Geom_Line(gp_Lin(gp_Pnt(0, 0, 0), gp_Dir(gp_XYZ(0, 0, 1))))) self.x_axis.SetColor(Quantity_Color(1, 0, 0, Quantity_TOC_RGB)) self.y_axis.SetColor(Quantity_Color(0, 1, 0, Quantity_TOC_RGB)) self.z_axis.SetColor(Quantity_Color(0, 0, 1, Quantity_TOC_RGB))
def to_Geom_Line(self): return Geom_Line( gp_Lin( gp_Pnt(0, 0, 0), gp_Dir( gp_XYZ(self._coords[0], self._coords[1], self._coords[2]))))
def calculate_circumcenter_2d_candidates(c_3d, n, face): def array_from_inter(inter, i): u = inter.UParameter(i) v = inter.VParameter(i) return np.array((u, v)) center_line = gp_Lin(gp_Pnt(c_3d[0], c_3d[1], c_3d[2]), gp_Dir(n[0], n[1], n[2])) inter = IntCurvesFace_Intersector(face, 0.0001) inter.Perform(center_line, -float('inf'), float('inf')) if inter.IsDone(): occ_offset = 1 # occ indices start at 1 c_2d_candidates = [ array_from_inter(inter, i + occ_offset) for i in range(inter.NbPnt()) ] if face.Orientation() == TopAbs_REVERSED: for c_2d in c_2d_candidates: # reverse u axis for reversed faces c_2d[0] = reverse_u(c_2d[0], face) return c_2d_candidates else: raise Exception( 'calculate_surface_circumcenter() error - intersector not done' )
def raycast(self, shape, point): raycast = IntCurvesFace.IntCurvesFace_ShapeIntersector() raycast.Load(shape, 0.01) line = gp.gp_Lin( gp.gp_Pnt(float(point[0]), float(point[1]), float(point[2])), gp.gp_Dir(0, 0, -1)) raycast.Perform(line, 0, self.section_box['z']) return raycast.NbPnt() != 0
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) self.tar.beam = reflect(self.ini.beam, pln)
def calculate_center_line(scdt, delta_index): vertices, _, triangles = scdt i0, i1, i2 = triangles[delta_index] a = vertices[i0].XYZ_vec3() b = vertices[i1].XYZ_vec3() c = vertices[i2].XYZ_vec3() cc = calculate_circumcenter(scdt, delta_index) n = normalize(np.cross(b - a, c - a)) return cc, gp_Lin(gp_Pnt(cc[0], cc[1], cc[2]), gp_Dir(n[0], n[1], n[2]))
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 raycast_at_projection_dir(self, shape, point): raycast = IntCurvesFace.IntCurvesFace_ShapeIntersector() raycast.Load(shape, 0.01) line = gp.gp_Lin( gp.gp_Pnt(float(point[0]), float(point[1]), float(point[2])), gp.gp_Dir(self.section_box['projection'][0], self.section_box['projection'][1], self.section_box['projection'][2])) raycast.Perform(line, 0, self.section_box['z']) if raycast.NbPnt() != 0: # The smaller WParameter is the closer z-index # Should be the first return {'face': raycast.Face(1), 'z': raycast.WParameter(1)}
def test_hash_eq_operator(self) -> None: """test that the == wrapper is ok""" # test Standard s = Standard_Transient() s2 = Standard_Transient() self.assertFalse(s == s2) self.assertTrue(s == s) # test list.index, that uses __eq__ method p1 = gp_Pnt(0.0, 0.0, 0.0) line = gp_Lin(p1, gp_Dir(1.0, 0.0, 0.0)) items = [p1, line] res = items.index(line) self.assertEqual(res, 1.0)
def test_hash_eq_operator(self): ''' test that the == wrapper is ok ''' # test Standard s = Standard_Transient() s2 = Standard_Transient() self.assertFalse(s == s2) self.assertTrue(s == s) # test list.index, that uses __eq__ method p1 = gp_Pnt(0., 0., 0.) line = gp_Lin(p1, gp_Dir(1., 0., 0.)) items = [p1, line] res = items.index(line) self.assertEqual(res, 1.)
def edge(event=None): # The blud edge BlueEdge = BRepBuilderAPI_MakeEdge(gp_Pnt(-80, -50, -20), gp_Pnt(-30, -60, -60)) V1 = BRepBuilderAPI_MakeVertex(gp_Pnt(-20, 10, -30)) V2 = BRepBuilderAPI_MakeVertex(gp_Pnt(10, 7, -25)) YellowEdge = BRepBuilderAPI_MakeEdge(V1.Vertex(), V2.Vertex()) #The white edge line = gp_Lin(gp_Ax1(gp_Pnt(10, 10, 10), gp_Dir(1, 0, 0))) WhiteEdge = BRepBuilderAPI_MakeEdge(line, -20, 10) #The red edge Elips = gp_Elips(gp_Ax2(gp_Pnt(10, 0, 0), gp_Dir(1, 1, 1)), 60, 30) RedEdge = BRepBuilderAPI_MakeEdge(Elips, 0, math.pi/2) # The green edge and the both extreme vertex P1 = gp_Pnt(-15, 200, 10) P2 = gp_Pnt(5, 204, 0) P3 = gp_Pnt(15, 200, 0) P4 = gp_Pnt(-15, 20, 15) P5 = gp_Pnt(-5, 20, 0) P6 = gp_Pnt(15, 20, 0) P7 = gp_Pnt(24, 120, 0) P8 = gp_Pnt(-24, 120, 12.5) array = TColgp_Array1OfPnt(1, 8) array.SetValue(1, P1) array.SetValue(2, P2) array.SetValue(3, P3) array.SetValue(4, P4) array.SetValue(5, P5) array.SetValue(6, P6) array.SetValue(7, P7) array.SetValue(8, P8) curve = Geom_BezierCurve(array) ME = BRepBuilderAPI_MakeEdge(curve) GreenEdge = ME V3 = ME.Vertex1() V4 = ME.Vertex2() display.DisplayColoredShape(BlueEdge.Edge(), 'BLUE') display.DisplayShape(V1.Vertex()) display.DisplayShape(V2.Vertex()) display.DisplayColoredShape(WhiteEdge.Edge(), 'WHITE') display.DisplayColoredShape(YellowEdge.Edge(), 'YELLOW') display.DisplayColoredShape(RedEdge.Edge(), 'RED') display.DisplayColoredShape(GreenEdge.Edge(), 'GREEN') display.DisplayShape(V3) display.DisplayShape(V4, update=True)
def test_hash_eq_operator(self): ''' test that the == wrapper is ok ''' # test Standard h1 = Handle_Standard_Transient() s = Standard_Transient() h2 = s.GetHandle() self.assertTrue(h1 == h1) self.assertFalse(h1 == h2) self.assertFalse(h1 == 10) self.assertTrue(h2 == s) # test list.index, that uses __eq__ method p1 = gp_Pnt(0., 0., 0.) line = gp_Lin(p1, gp_Dir(1., 0., 0.)) items = [p1, line] res = items.index(line) self.assertEqual(res, 1.)
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 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 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 calc_intersection(terrain_intersection_curves, edges_coords, edges_dir): """ This script calculates the intersection of the building edges to the terrain, :param terrain_intersection_curves: :param edges_coords: :param edges_dir: :return: intersecting points, intersecting faces """ building_line = gp_Lin( gp_Ax1(gp_Pnt(edges_coords[0], edges_coords[1], edges_coords[2]), gp_Dir(edges_dir[0], edges_dir[1], edges_dir[2]))) terrain_intersection_curves.PerformNearest(building_line, 0.0, float("+inf")) if terrain_intersection_curves.IsDone(): npts = terrain_intersection_curves.NbPnt() if npts != 0: return terrain_intersection_curves.Pnt( 1), terrain_intersection_curves.Face(1) else: return None, None else: return None, None
def intersect_shape_with_ptdir(occtopology, pypt, pydir): """ This function projects a point in a direction and calculates the at which point does the point intersects the OCCtopology. Parameters ---------- occtopology : OCCtopology The OCCtopology to be projected on. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex pypt : tuple of floats The point to be projected. A pypt is a tuple that documents the xyz coordinates of a pt e.g. (x,y,z) pydir : tuple of floats The direction of the point to be projected. A pydir is a tuple that documents the xyz vector of a dir e.g. (x,y,z) Returns ------- intersection point : pypt The point in which the projected point intersect the OCCtopology. If None means there is no intersection. intersection face : OCCface The OCCface in which the projected point hits. If None means there is no intersection. """ occ_line = gp_Lin( gp_Ax1(gp_Pnt(pypt[0], pypt[1], pypt[2]), gp_Dir(pydir[0], pydir[1], pydir[2]))) shape_inter = IntCurvesFace_ShapeIntersector() shape_inter.Load(occtopology, 1e-6) shape_inter.PerformNearest(occ_line, 0.0, float("+inf")) if shape_inter.IsDone(): npts = shape_inter.NbPnt() if npts != 0: return modify.occpt_2_pypt(shape_inter.Pnt(1)), shape_inter.Face(1) else: return None, None else: return None, None
def compas_line_to_occ_line(self: Line) -> gp_Lin: """Convert a COMPAS line to an OCC line. Parameters ---------- self : :class:`~compas.geometry.Line` The COMPAS line to convert. Returns ------- ``gp_Lin`` Examples -------- >>> from compas.geometry import Line >>> Line.to_occ = compas_line_to_occ_line >>> line = Line([0, 0, 0], [1, 0, 0]) >>> line.to_occ() <class 'gp_Lin'> """ return gp_Lin(compas_point_to_occ_point(self.start), compas_vector_to_occ_direction(self.direction))
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 proj_pnt_pln(self, pnt, surf, axs=gp_Ax3()): lin = gp_Lin(pnt, axs.Direction()) sxy = GeomAPI_IntCS(Geom_Line(lin), BRep_Tool.Surface(surf)).Point(1) return sxy
def Prj_pnt_to_face(axs, pnt, face): lin = gp_Lin(pnt, axs.Direction()) sxy = GeomAPI_IntCS(Geom_Line(lin).GetHandle(), BRep_Tool.Surface(face)).Point(1) return sxy
def viewline(self, x, y): Xv, Yv, Zv, Vx, Vy, Vz = self.View.ConvertWithProj(x, y) return gp_Lin(gp_Pnt(Xv, Yv, Zv), gp_Dir(Vx, Vy, Vz))
def doscreen_impl(model, path, size, yaw=None, pitch=None, triedron=True): scn = Scene() try: mmm = model if isinstance(mmm, evalcache.LazyObject): mmm = mmm.unlazy() c = zencad.default_color() if REVERSE_COLOR: c = (c[2], c[1], c[0]) scn.add(mmm, c) except: for m in model: if isinstance(m, (tuple, list)): c = m[1] m = m[0] else: c = zencad.default_color() if REVERSE_COLOR: c = (c[2], c[1], c[0]) mod = m if isinstance(mod, evalcache.LazyObject): mod = mod.unlazy() if isinstance(mod, zencad.util.point3): c = Color(1, 0, 0) if REVERSE_COLOR: c = (c[2], c[1], c[0]) scn.add(mod, c) else: scn.add(mod, c) #viewer = scn.viewer # if triedron: # Always add triedron # viewer.set_triedron_axes() #view = viewer.create_view() # view.set_triedron(False) #view.set_virtual_window(size[0], size[1]) if yaw is None: yaw = math.pi * (7 / 16) + math.pi / 2 if pitch is None: pitch = math.pi * -0.15 render = OffscreenRenderer(path, size) for i in scn.interactives: render.Context.Display(i.ais_object, True) i.bind_context(render.Context) if triedron: x_axis = AIS_Axis( Geom_Line(gp_Lin(gp_Pnt(0, 0, 0), gp_Dir(gp_XYZ(1, 0, 0))))) y_axis = AIS_Axis( Geom_Line(gp_Lin(gp_Pnt(0, 0, 0), gp_Dir(gp_XYZ(0, 1, 0))))) z_axis = AIS_Axis( Geom_Line(gp_Lin(gp_Pnt(0, 0, 0), gp_Dir(gp_XYZ(0, 0, 1))))) x_axis.SetColor(Quantity_Color(1, 0, 0, Quantity_TOC_RGB)) y_axis.SetColor(Quantity_Color(0, 1, 0, Quantity_TOC_RGB)) z_axis.SetColor(Quantity_Color(0, 0, 1, Quantity_TOC_RGB)) render.Context.Display(x_axis, True) render.Context.Display(y_axis, True) render.Context.Display(z_axis, True) render.View.Camera().SetDirection(gp_Dir( math.cos(pitch) * math.cos(yaw), math.cos(pitch) * math.sin(yaw), math.sin(pitch), )) render.View.Camera().SetUp(gp_Dir(0, 0, 1)) render.View.FitAll(0.07) render.DoIt()
from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Lin, gp_Ax2, gp_Pln ORIGIN = gp_Pnt(0, 0, 0) DIR_X = gp_Dir(1, 0, 0) DIR_Y = gp_Dir(0, 1, 0) DIR_Z = gp_Dir(0, 0, 1) LINE_X = gp_Lin(ORIGIN, DIR_X) LINE_Y = gp_Lin(ORIGIN, DIR_Y) LINE_Z = gp_Lin(ORIGIN, DIR_Z) AX_XZ = gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 1, 0), gp_Dir(0, 0, 1)) AX_YZ = gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(1, 0, 0), gp_Dir(0, 0, 1)) PL_XZ = gp_Pln(ORIGIN, DIR_Y) PL_YZ = gp_Pln(ORIGIN, DIR_X)