#coin

import cadquery as cq
import math

# Parameters
t = 10.0  # Thickness of the disc
d = 50    # Diameter of the disc
ds = 16   # Diameter of the scallops
n = 8     # Number of scallops
p = 60    # Pitch circle diameter for scallops
f = 1.8   #fillet radius

sd = 300   # diameter of sphere for recess
################################

dm = d-t
rm=dm/2
cw=cq.Workplane("XY")

torus =cq.Workplane("top").center(20,0).circle(t/2).revolve(360/n,(-rm,1,0),(-rm,0,0)) #segment of torus

angle = 180-((360/n)*0.5) # Angle for the initial scallop
x = p / 2 * math.cos(angle)      # X coordinate of the scallop
y = p / 2 * math.sin(angle)      # Y coordinate of the scallop
scallop = cw.circle(ds/2).extrude(t*2).translate((x,y,-t))  # Create a scallop

part= torus.cut(scallop)
part=part.faces(">X").fillet(f)  #part of the fished torus


ring=[]

# put all the parts into a ring
for i in range(n):
    angle = (2 * math.pi / n) * i  # Angle for each part
    x = d / 2 * math.cos(angle)      # X coordinate of the part
    y = d / 2 * math.sin(angle)      # Y coordinate of the part
    seg = part.rotate((0,0,0),(0,0,1),i*360/n)  # Create each part of torus
    ring.append(seg)  # Store the part in the list

torus=ring[0]

for obj in ring[1:]:
    torus = torus.union(obj)

#angularTolerance = 0.001  # in degrees, controls smoothness of arcs
#linearTolerance = 0.01  # controls tessellation for straight lines

cent= cw.circle(rm).extrude(t).translate((0,0,-t/2)).rotate((0,0,0),(0,0,1),33)

coin_done=torus.union(cent)

top_curve= cw.sphere(radius=sd/2).rotate((0,0,0),(1,0,0),90)
top_recess  = top_curve.translate((0,0,((sd+t)/2)-1))
bot_recess = top_curve.translate((0,0,-(((sd+t)/2)-1)))

coin_done = coin_done.cut(top_recess).cut(bot_recess)

coin_done=coin_done.cut(cw.text( "RW",10,2).translate((0,0,(t-3)/2)))
show_object(coin_done)

cq.exporters.export(coin_done, 'coin_done.stl',tolerance=0.001)
cq.exporters.export(coin_done, 'coin_done.step')
