import cadquery as cq
import cadkit as ck

# Parameters
BUSHING_OD = 30.0       # Outer diameter of the main cylinder
BUSHING_L = 40.0        # Length of the main cylinder part
FLANGE_OD = 40.0        # Diameter of the flange (бурт)
FLANGE_T = 5.0         # Thickness of the flange
BORE_D = 20.0           # Through-hole diameter

# The total height of the part is BUSHING_L + FLANGE_T
# We build it starting from z=0.
# The flange is at the bottom (z=0 to z=FLANGE_T)
# The cylinder is on top (z=FLANGE_T to z=FLANGE_T + BUSHING_L)

# Create the flange
flange = (
    cq.Workplane("XY")
    .circle(FLANGE_OD / 2)
    .extrude(FLANGE_T)
)

# Create the main cylinder body
body = (
    cq.Workplane("XY")
    .workplane(offset=FLANGE_T)
    .circle(BUSHING_OD / 2)
    .extrude(BUSHING_L)
)

# Union the two parts
part = flange.union(body)

# Cut the through-hole
# We use a cylinder centered on Z axis from z=0 to total height
hole = (
    cq.Workplane("XY")
    .circle(BORE_D / 2)
    .extrude(FLANGE_T + BUSHING_L)
)

final_bushing = part.cut(hole)

# Assembly for final result
assy = cq.Assembly()
assy.add(final_bushing, name="bushing_with_flange", color=cq.Color(0.7, 0.7, 0.7))

result = assy
