main
John Lauer BFK458-12: model, chip artwork, hero image and datasheet facts 9bc5382
"""Kendrion INTORQ BFK458-12 N, basic module N with hand release, drawing-derived 3D model.

Source: Kendrion brochure "INTORQ BFK458 spring-applied brake", edition 2022.10, page 10
("Dimensions Basic module N (without adjustment ring)", size 12 row and the two views) and page 11
(size 12 heights). Printed values used: b 152, d 20 (bore, standard 20 or 25), d1 3xM6, d2 132,
d3 50, d6 150, d11 10, d12 13, d13 12, d14 4xM5 on d15 64, di 70.1, da 115, h 54.9, h2 2, h4 29.4,
h5 161, h7 23, h9 88, l 25, sLN 0.3, alpha 25 deg.
Measured off the page 10 section view (to scale: d6 150 mm = 742 px and h 54.9 mm = 270 px at 300 dpi,
4.94 px/mm on both axes): rotor 9.3 thick with 2.6 mm friction linings, armature plate 7.8 thick,
stator 37.5 thick, hub outside diameter 48.6, sleeve outside diameter 10, bolt projection 13.6 below the
mounting face, bolt heads sunk in the stator rear face.
Not modelled: the coil and its potting (inside, not visible), the hub teeth (drawn as a plain cylinder),
the cable channel cover, the 400 mm cable (a 60 mm stub is drawn).

Frame: Adom Z-up, millimetres in the STEP. The brake axis is Z. z = 0 is the mounting face (the rotor's
friction face that meets the motor end shield); the stator is towards +Z. The hand release lever points
to +Y, its handle end at h5 = 161 mm from the axis. Cable exit at the lower left, alpha = 25 deg.
"""
import math, sys
from OCP.gp import gp_Pnt, gp_Dir, gp_Ax2, gp_Vec, gp_Trsf, gp_Ax1
from OCP.BRepPrimAPI import BRepPrimAPI_MakeCylinder, BRepPrimAPI_MakeBox, BRepPrimAPI_MakeCone, BRepPrimAPI_MakeSphere
from OCP.BRepAlgoAPI import BRepAlgoAPI_Cut, BRepAlgoAPI_Fuse
from OCP.BRepBuilderAPI import BRepBuilderAPI_Transform, BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire, BRepBuilderAPI_MakeFace
from OCP.BRepOffsetAPI import BRepOffsetAPI_MakePipe
from OCP.BRepFilletAPI import BRepFilletAPI_MakeChamfer
from OCP.GC import GC_MakeArcOfCircle
from OCP.gp import gp_Circ
from OCP.TopExp import TopExp_Explorer
from OCP.TopAbs import TopAbs_EDGE
from OCP.TopoDS import TopoDS
from OCP.STEPCAFControl import STEPCAFControl_Writer
from OCP.TDocStd import TDocStd_Document
from OCP.XCAFDoc import XCAFDoc_DocumentTool, XCAFDoc_ColorType
from OCP.TCollection import TCollection_ExtendedString
from OCP.Quantity import Quantity_Color, Quantity_TOC_RGB
from OCP.TDataStd import TDataStd_Name
from OCP.STEPControl import STEPControl_AsIs
from OCP.TopLoc import TopLoc_Location
from OCP.Interface import Interface_Static

def cyl(r, z0, z1, x=0.0, y=0.0):
    return BRepPrimAPI_MakeCylinder(gp_Ax2(gp_Pnt(x, y, z0), gp_Dir(0, 0, 1)), r, z1 - z0).Shape()

def ring(r0, r1, z0, z1):
    return BRepAlgoAPI_Cut(cyl(r1, z0, z1), cyl(r0, z0 - 1, z1 + 1)).Shape()

def cut(a, *bs):
    for b in bs:
        a = BRepAlgoAPI_Cut(a, b).Shape()
    return a

def fuse(a, *bs):
    for b in bs:
        a = BRepAlgoAPI_Fuse(a, b).Shape()
    return a

def box(x0, y0, z0, x1, y1, z1):
    return BRepPrimAPI_MakeBox(gp_Pnt(x0, y0, z0), gp_Pnt(x1, y1, z1)).Shape()

def move(s, dx, dy, dz):
    t = gp_Trsf(); t.SetTranslation(gp_Vec(dx, dy, dz))
    return BRepBuilderAPI_Transform(s, t, True).Shape()

def rot_z(s, deg):
    t = gp_Trsf(); t.SetRotation(gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), math.radians(deg))
    return BRepBuilderAPI_Transform(s, t, True).Shape()

def pipe(points, r):
    """Circular section r swept along a polyline of 3D points (straight segments)."""
    mw = BRepBuilderAPI_MakeWire()
    for a, b in zip(points, points[1:]):
        mw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(*a), gp_Pnt(*b)).Edge())
    w = mw.Wire()
    p0, p1 = gp_Pnt(*points[0]), gp_Pnt(*points[1])
    d = gp_Dir(gp_Vec(p0, p1))
    prof = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(gp_Circ(gp_Ax2(p0, d), r)).Edge()).Wire()
    face = BRepBuilderAPI_MakeFace(prof).Face()
    mp = BRepOffsetAPI_MakePipe(w, face)
    return mp.Shape()

# ---- printed and measured values (mm) ----
D6 = 150.0; R6 = D6 / 2          # stator and armature outside diameter
H = 54.9                          # overall length, mounting face to stator rear face
D2 = 132.0                        # sleeve bolt circle, 3 x M6
DI, DA = 70.1, 115.0              # friction lining inside and outside diameters
D3 = 50.0                         # stator centre bore
BORE = 20.0                       # hub bore d (20 or 25 standard)
L_HUB, H2 = 25.0, 2.0             # hub length l, hub set back h2 from the mounting face
R_HUB = 48.6 / 2                  # hub outside, measured
T_ROT, T_LIN = 9.3, 2.6           # rotor thickness and each lining, measured
Z_ARM0, Z_ARM1 = 9.3, 17.1        # armature plate, measured
SLN = 0.3                         # rated air gap
Z_ST0 = Z_ARM1 + SLN              # stator front face
H4 = 29.4                         # mounting face to hand release lever axis
H5, H7, H9 = 161.0, 23.0, 88.0    # axis to handle end, handle length, axis to the bow top
D11, D12, D13 = 10.0, 13.0, 12.0  # lever rod, handle, lever base diameters
B = 152.0                         # width across the hand release lugs
BOLT_ANG = [90.0, 210.0, 330.0]   # sleeve bolts: one under the lever, two at the lower corners
ALPHA = 25.0                      # cable exit angle from the vertical

parts = []  # (name, shape, rgb)

# rotor: steel carrier reaching the hub teeth, two friction linings
carrier = ring(R_HUB, DA / 2, T_LIN, T_ROT - T_LIN)
lin_a = ring(DI / 2, DA / 2, 0.0, T_LIN)
lin_b = ring(DI / 2, DA / 2, T_ROT - T_LIN, T_ROT)
parts += [("Rotor carrier", carrier, (0.55, 0.56, 0.58)),
          ("Friction lining, mounting side", lin_a, (0.24, 0.21, 0.19)),
          ("Friction lining, armature side", lin_b, (0.24, 0.21, 0.19))]

# hub with DIN 6885 keyway for d 20 (b 6, t2 2.8)
hub = ring(BORE / 2, R_HUB, H2, H2 + L_HUB)
hub = cut(hub, box(-3.0, 0, H2 - 1, 3.0, BORE / 2 + 2.8, H2 + L_HUB + 1))
parts.append(("Hub, bore 20 mm with keyway", hub, (0.66, 0.67, 0.69)))

# armature plate with clearance holes for the sleeves
arm = ring(D3 / 2, R6, Z_ARM0, Z_ARM1)
for a in BOLT_ANG:
    x, y = D2 / 2 * math.cos(math.radians(a)), D2 / 2 * math.sin(math.radians(a))
    arm = cut(arm, cyl(5.6, Z_ARM0 - 1, Z_ARM1 + 1, x, y))
parts.append(("Armature plate", arm, (0.74, 0.75, 0.77)))

# stator (magnet body): centre bore, rear chamfer, 4 x M5 tapped holes on d15 64, bolt counterbores
st = ring(D3 / 2, R6, Z_ST0, H)
ch = BRepFilletAPI_MakeChamfer(st)
ex = TopExp_Explorer(st, TopAbs_EDGE)
while ex.More():
    e = TopoDS.Edge_s(ex.Current())
    from OCP.BRepAdaptor import BRepAdaptor_Curve
    c = BRepAdaptor_Curve(e)
    try:
        circ = c.Circle()
        if abs(circ.Radius() - R6) < 0.01 and abs(circ.Location().Z() - H) < 0.01:
            ch.Add(3.0, e)
    except Exception:
        pass
    ex.Next()
st = ch.Shape()
for a in (0, 90, 180, 270):
    x, y = 32 * math.cos(math.radians(a)), 32 * math.sin(math.radians(a))
    st = cut(st, cyl(2.1, H - 8, H + 1, x, y))
for a in BOLT_ANG:
    x, y = D2 / 2 * math.cos(math.radians(a)), D2 / 2 * math.sin(math.radians(a))
    st = cut(st, cyl(5.6, Z_ST0 - 1, H + 1, x, y))
parts.append(("Stator (magnet body)", st, (0.32, 0.33, 0.35)))

# sleeve bolts: sleeve from the mounting face to the stator, M6 shank below, socket head in the stator
bolts = None
for a in BOLT_ANG:
    x, y = D2 / 2 * math.cos(math.radians(a)), D2 / 2 * math.sin(math.radians(a))
    sl = ring(3.2, 5.0, 0.0, Z_ST0)
    sl = move(sl, x, y, 0)
    sh = cyl(3.0, -13.6, H - 8.5, x, y)
    hd = cut(cyl(5.0, H - 8.5, H - 2.5, x, y), cyl(2.5, H - 5.5, H, x, y))
    one = fuse(sl, sh, hd)
    bolts = one if bolts is None else fuse(bolts, one)
parts.append(("Sleeve bolts, 3 x M6", bolts, (0.80, 0.80, 0.82)))

# hand release: two lugs at 3 and 9 o'clock, a bow over the top, the lever at 12 o'clock
lugs = None
for sx in (-1, 1):
    lug = box(-4.0, -7.0, H4 - 7.0, 4.0, 7.0, H4 + 7.0)
    lug = move(lug, sx * (B / 2 - 4.0), 0, 0)
    lugs = lug if lugs is None else fuse(lugs, lug)
parts.append(("Hand release lugs", lugs, (0.32, 0.33, 0.35)))
rb = 2.6
# the bow: straight sides up from the lugs, then over the top to h9 from the axis
rb_c = B / 2 - 2.6
pts = [(-rb_c, 2.0, H4), (-rb_c, 30.0, H4)]
cy = 30.0
for k in range(1, 24):
    t = math.radians(180 - k * 180 / 24)
    pts.append((rb_c * math.cos(t), cy + (H9 - 4.0 - cy) * math.sin(t), H4))
pts.append((rb_c, 30.0, H4)); pts.append((rb_c, 2.0, H4))
bow = pipe(pts, rb)
parts.append(("Hand release bow", bow, (0.80, 0.80, 0.82)))
knuckle = BRepPrimAPI_MakeSphere(gp_Pnt(0, H9 - 1.0, H4), 6.5).Shape()
base = BRepPrimAPI_MakeCylinder(gp_Ax2(gp_Pnt(0, H9, H4), gp_Dir(0, 1, 0)), D13 / 2, 12.0).Shape()
rod = BRepPrimAPI_MakeCylinder(gp_Ax2(gp_Pnt(0, H9 + 12.0, H4), gp_Dir(0, 1, 0)), D11 / 2, H5 - H7 - H9 - 12.0).Shape()
lever = fuse(knuckle, base, rod)
parts.append(("Hand release lever", lever, (0.82, 0.83, 0.85)))
handle = BRepPrimAPI_MakeCone(gp_Ax2(gp_Pnt(0, H5 - H7, H4), gp_Dir(0, 1, 0)), 4.6, D12 / 2, H7 - D12 / 2).Shape()
cap = BRepPrimAPI_MakeSphere(gp_Pnt(0, H5 - D12 / 2, H4), D12 / 2).Shape()
handle = fuse(handle, cap)
parts.append(("Hand release handle", handle, (0.07, 0.07, 0.08)))

# coil cable: 60 mm stub leaving the stator rim at the lower left, alpha 25 deg from the vertical
ang = math.radians(270 - 30)
x0, y0 = (R6 - 2) * math.cos(ang), (R6 - 2) * math.sin(ang)
dx, dy = -math.sin(math.radians(ALPHA)), -math.cos(math.radians(ALPHA))
cable = pipe([(x0, y0, H4 + 6), (x0 + dx * 60, y0 + dy * 60, H4 + 6)], 3.0)
parts.append(("Coil cable (stub, 400 mm as supplied)", cable, (0.06, 0.06, 0.07)))

doc = TDocStd_Document(TCollection_ExtendedString("bfk458-12"))
stool = XCAFDoc_DocumentTool.ShapeTool_s(doc.Main()); ctool = XCAFDoc_DocumentTool.ColorTool_s(doc.Main())
asm = stool.NewShape(); TDataStd_Name.Set_s(asm, TCollection_ExtendedString("BFK458-12N"))
for name, shp, rgb in parts:
    lab = stool.AddShape(shp, False); TDataStd_Name.Set_s(lab, TCollection_ExtendedString(name))
    ctool.SetColor(lab, Quantity_Color(*rgb, Quantity_TOC_RGB), XCAFDoc_ColorType.XCAFDoc_ColorSurf)
    stool.AddComponent(asm, lab, TopLoc_Location())
stool.UpdateAssemblies()
Interface_Static.SetCVal_s("write.step.unit", "MM")
w = STEPCAFControl_Writer(); w.SetColorMode(True); w.SetNameMode(True)
w.Transfer(doc, STEPControl_AsIs); w.Write(sys.argv[1]); print("OK", sys.argv[1], len(parts), "parts")