import cadquery as cq
import cadkit as ck

# Parameters for DN50 Flat Flange (Standard-like dimensions)
OD = 140.0              # Outer diameter (mm)
THICKNESS = 16.0        # Flange thickness (mm)
BORE_D = 57.0           # Central bore diameter (mm)
BOLT_CIRCLE_D = 110.0   # Bolt circle diameter (mm)
BOLT_HOLE_D = 18.0      # Diameter of bolt holes (mm)
BOLT_COUNT = 4          # Number of bolt holes
CHAMFER_R = 1.5         # Edge chamfer (mm)

# Using cadkit.flange for a verified standard part implementation
# Parameters: od, bore, thickness, bolt_circle, bolts, bolt_d
flange = ck.flange(
    od=OD, 
    bore=BORE_D, 
    thickness=THICKNESS, 
    bolt_circle=BOLT_CIRCLE_D, 
    bolts=BOLT_COUNT, 
    bolt_d=BOLT_HOLE_D
)

# Refine the geometry for manufacturability
# Apply chamfers to the top and bottom outer edges and the bore
flange = (
    flange.edges(">Z").chamfer(CHAMFER_R)
    .edges("<Z").chamfer(CHAMFER_R)
    .edges(">>Z").chamfer(CHAMFER_R) # Inner bore top
    .edges("<<Z").chamfer(CHAMFER_R) # Inner bore bottom
)

# Create an assembly for the final result
assy = cq.Assembly()
assy.add(flange, name="flange_dn50", color=cq.Color(0.7, 0.7, 0.75))

result = assy
