def get_transform(self): d = self.declaration result = gp_Trsf() #: TODO: Order matters... how to configure it??? if d.operations: for op in d.operations: t = gp_Trsf() if isinstance(op, Translate): t.SetTranslation(gp_Vec(op.x, op.y, op.z)) elif isinstance(op, Rotate): t.SetRotation( gp_Ax1(gp_Pnt(*op.point), gp_Dir(*op.direction)), op.angle) elif isinstance(op, Mirror): Ax = gp_Ax2 if op.plane else gp_Ax1 t.SetMirror(Ax(gp_Pnt(*op.point), gp_Dir(op.x, op.y, op.z))) elif isinstance(op, Scale): t.SetScale(gp_Pnt(*op.point), op.s) result.Multiply(t) else: axis = gp_Ax3() axis.SetDirection(d.direction.proxy) result.SetTransformation(axis) result.SetTranslationPart(gp_Vec(*d.position)) if d.rotation: t = gp_Trsf() t.SetRotation(gp_Ax1(d.position.proxy, d.direction.proxy), d.rotation) result.Multiply(t) return result
def get_transform(self): """ Create a transform which rotates the default axis to align with the normal given by the position Returns ------- transform: gp_Trsf """ d = self.declaration # Move to position and align along direction axis t = gp_Trsf() if d.direction.is_parallel(DZ): t.SetRotation(AZ, d.direction.angle(DZ) + d.rotation) else: d1 = d.direction.cross(DZ) axis = gp_Ax1(gp_Pnt(0, 0, 0), d1.proxy) t.SetRotation(axis, d.direction.angle(DZ)) # Apply the rotation an reverse any rotation added in sign = 1 if d1.y >= 0 else -1 angle = d.rotation + sign * d1.angle(DX) if angle: rot = gp_Trsf() rot.SetRotation(AZ, angle) t.Multiply(rot) t.SetTranslationPart(gp_Vec(*d.position)) return t
def rotating_cube_2_axis(event=None): display.EraseAll() ais_boxshp = build_shape() ax1 = gp_Ax1(gp_Pnt(0., 0., 0.), gp_Dir(0., 0., 1.)) ax2 = gp_Ax1(gp_Pnt(0., 0., 0.), gp_Dir(0., 1., 0.)) a_cube_trsf = gp_Trsf() a_cube_trsf2 = gp_Trsf() angle = 0.0 tA = time.time() n_rotations = 200 for i in range(n_rotations): a_cube_trsf.SetRotation(ax1, angle) a_cube_trsf2.SetRotation(ax2, angle) aCubeToploc = TopLoc_Location(a_cube_trsf * a_cube_trsf2) display.Context.SetLocation(ais_boxshp, aCubeToploc) display.Context.UpdateCurrentViewer() angle += 2 * pi / n_rotations print("%i rotations took %f" % (n_rotations, time.time() - tA))
def add_feature(base): """Add a "feature" to a shape. In this case we drill a hole through it.""" feature_diameter = 0.8 feature_origin = gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)) feature_maker = BRepFeat_MakeCylindricalHole() feature_maker.Init(base, feature_origin) feature_maker.Build() feature_maker.Perform(feature_diameter / 2.0) shape = feature_maker.Shape() return shape
def mirror_pnt_dir(brep, pnt, direction, copy=False): ''' @param brep: @param line: ''' trns = gp_Trsf() trns.SetMirror(gp_Ax1(pnt, direction)) brep_trns = BRepBuilderAPI_Transform(brep, trns, copy) with assert_isdone(brep_trns, 'could not produce mirror'): brep_trns.Build() return brep_trns.Shape()
def make_ellipse(p, rx, ry, rotate=0, direction=Z_DIR): """ gp_Elips doesn't allow minor > major so swap and rotate instead if that's the case. """ c = gp_Pnt(*p) if ry > rx: rx, ry = ry, rx # Swap rotate += pi/2 # This only works when rotate == 0 ellipse = gp_Elips(gp_Ax2(c, direction), rx, ry) ellipse.Rotate(gp_Ax1(c, direction), rotate) return ellipse
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 update_shape(self, change=None): d = self.declaration if d.shape: shape = coerce_shape(d.shape) copy = True else: shape = self.get_shape().shape copy = False #: Build arguments args = [shape, gp_Ax1(d.position.proxy, d.direction.proxy)] if d.angle: args.append(d.angle) args.append(copy) revol = BRepPrimAPI_MakeRevol(*args) self.shape = revol.Shape()
def revolved_cut(base): # Define 7 points face_points = TColgp_Array1OfPnt(1, 7) face_inner_radius = 0.6 pts = [ gp_Pnt(face_inner_radius - 0.05, 0.0, -0.05), gp_Pnt(face_inner_radius - 0.10, 0.0, -0.025), gp_Pnt(face_inner_radius - 0.10, 0.0, 0.025), gp_Pnt(face_inner_radius + 0.10, 0.0, 0.025), gp_Pnt(face_inner_radius + 0.10, 0.0, -0.025), gp_Pnt(face_inner_radius + 0.05, 0.0, -0.05), gp_Pnt(face_inner_radius - 0.05, 0.0, -0.05), ] for n, i in enumerate(pts): face_points.SetValue(n + 1, i) # Use these points to create edges and add these edges to a wire hexwire = BRepBuilderAPI_MakeWire() for i in range(1, 7): hexedge = BRepBuilderAPI_MakeEdge(face_points.Value(i), face_points.Value(i + 1)).Edge() hexwire.Add(hexedge) # Turn the wire into a 6 sided face hexface = BRepBuilderAPI_MakeFace(hexwire.Wire()).Face() # Revolve the face around an axis revolve_axis = gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)) revolved_shape = BRepPrimAPI_MakeRevol(hexface, revolve_axis).Shape() # Move the generated shape move = gp_Trsf() move.SetTranslation(gp_Pnt(0, 0, 0), gp_Pnt(0, 0, sin(0.5))) moved_shape = BRepBuilderAPI_Transform(revolved_shape, move, False).Shape() # Remove the revolved shape cut = BRepAlgoAPI_Cut(base, moved_shape).Shape() return cut
def revolved_shape(): """ demonstrate how to create a revolved shape from an edge adapted from algotopia.com's opencascade_basic tutorial: http://www.algotopia.com/contents/opencascade/opencascade_basic """ face_inner_radius = 0.6 # point to create an edge from edg_points = [ gp_Pnt(face_inner_radius - 0.05, 0.0, -0.05), gp_Pnt(face_inner_radius - 0.10, 0.0, -0.025), gp_Pnt(face_inner_radius - 0.10, 0.0, 0.025), gp_Pnt(face_inner_radius + 0.10, 0.0, 0.025), gp_Pnt(face_inner_radius + 0.10, 0.0, -0.025), gp_Pnt(face_inner_radius + 0.05, 0.0, -0.05), gp_Pnt(face_inner_radius - 0.05, 0.0, -0.05), ] # aggregate edges in wire hexwire = BRepBuilderAPI_MakeWire() for i in range(6): hexedge = BRepBuilderAPI_MakeEdge(edg_points[i], edg_points[i + 1]).Edge() hexwire.Add(hexedge) hexwire_wire = hexwire.Wire() # face from wire hexface = BRepBuilderAPI_MakeFace(hexwire_wire).Face() revolve_axis = gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)) # create revolved shape revolved_shape_ = BRepPrimAPI_MakeRevol(hexface, revolve_axis, math.radians(90.)).Shape() # render wire & revolved shape display.DisplayShape([revolved_shape_, hexwire_wire]) display.FitAll() start_display()
from OCCT.Geom import Geom_BezierCurve, Geom_BSplineCurve from OCCT.gp import ( gp_Dir, gp_Pnt, gp_Circ, gp_Elips, gp_Ax1, gp_Ax2, gp_Trsf, gp_Ax3, gp_Vec ) from OCCT.TColgp import TColgp_Array1OfPnt from OCCT.TopoDS import TopoDS_Shape, TopoDS_Compound#, topods from .occ_shape import OccShape from ..draw import ProxySvg from declaracad.core.utils import log Z_DIR = gp_Dir(0, 0, 1) NEG_Z_DIR = gp_Dir(0, 0, -1) Z_AXIS = gp_Ax1(gp_Pnt(0, 0, 0), Z_DIR) UNITS = { 'in': 90.0, 'pt': 1.25, 'px': 1, 'mm': 3.5433070866, 'cm': 35.433070866, 'm': 3543.3070866, 'km': 3543307.0866, 'pc': 15.0, 'yd': 3240, 'ft': 1080 } # From simplepath.py's parsePath by Aaron Spike, [email protected] PATHDEFS = { 'M': ['L', 2, [float, float], ['x', 'y']], 'L': ['L', 2, [float, float], ['x', 'y']], 'H': ['H', 1, [float], ['x']], 'V': ['V', 1, [float], ['y']], 'C': ['C', 6, [float, float, float, float, float, float], ['x', 'y', 'x', 'y', 'x', 'y']],
TopTools_IndexedDataMapOfShapeListOfShape) from ..shape import (ProxyShape, ProxyFace, ProxyBox, ProxyCone, ProxyCylinder, ProxyHalfSpace, ProxyPrism, ProxySphere, ProxyWedge, ProxyTorus, ProxyRevol, ProxyRawShape, ProxyLoadShape, BBox, Shape, coerce_point, coerce_direction) from declaracad.core.utils import log DX = gp_Dir(1, 0, 0) DXN = gp_Dir(-1, 0, 0) DY = gp_Dir(0, 1, 0) DYN = gp_Dir(0, -1, 0) DZ = gp_Dir(0, 0, 1) DZN = gp_Dir(0, 0, -1) AX = gp_Ax1() AX.SetDirection(gp.DX_()) AY = gp_Ax1() AY.SetDirection(gp.DY_()) AZ = gp_Ax1() AZ.SetDirection(gp.DZ_()) def coerce_axis(value): pos, dir, rotation = value axis = gp_Ax2(pos.proxy, dir.proxy) axis.Rotate(axis.Axis(), rotation) return axis def coerce_shape(shape):