# 挖洞與切除

``````from math import cos, sin, pi

r1 = 10
r2 = 8
r3 = 1
n = 12
thickness = 1

a_step = 2 * pi / n

points = [(r2 * cos(i * a_step), r2 * sin(i * a_step)) for i in range(n)]

plate = (cq.Workplane()
.circle(r1)
.pushPoints(points)
.circle(r3)
.extrude(thickness)
)
``````

``````import cadquery as cq

r1 = 10
r2 = 8
r3 = 1
n = 12
thickness = 1

plate = (cq.Workplane()
.circle(r1)
.polarArray(r2, 0, 360, n)
.circle(r3)
.extrude(thickness)
)
``````

``````import cadquery as cq

r1 = 10
r2 = 8
r3 = 1
n = 12
thickness = 1

plate = (cq.Workplane()
.circle(r1)
.extrude(thickness)
)

sticks = (cq.Workplane()
.polarArray(r2, 0, 360, n)
.circle(r3)
.extrude(thickness)
)

result = plate - sticks

show_object(result)
``````

``````import cadquery as cq

r1 = 10
r2 = 8
r3 = 1
n = 12
thickness = 1

plate = (cq.Workplane()
.circle(r1)
.extrude(thickness)
.faces('>Z')               # 選擇 Z 正方向最遠的面
.workplane()               # 建立一個工作平面
.polarArray(r2, 0, 360, n) # 位置資訊
.circle(r3)                # 建立 Wire
.cutThruAll()              # 切穿
)
``````

`cutThruAll` 會利用尚未被處理的 `Wire` 來切穿實體，另一個跟切除有關的方式是 `cutBlind`，它就像是把 `Wire` 拿來 `extrude`，擠出後的結果會被用來切除實體，例如：

``````import cadquery as cq

r1 = 10
r2 = 8
r3 = 1
n = 12
thickness = 1

plate = (cq.Workplane()
.circle(r1)
.extrude(thickness)
.polarArray(r2, 0, 360, n)
.circle(r3)
.cutBlind(thickness)
)
``````

``````import cadquery as cq

r1 = 10
r2 = 8
r3 = 1
n = 12
thickness = 1

plate = (cq.Workplane()
.circle(r1)
.extrude(thickness)
.faces('>Z')
.workplane()
.polarArray(r2, 0, 360, n)
.hole(r3 * 2) # 指定直徑
)
``````

`Workplane` 還有兩個挖洞的方法，可以用來埋頭孔，通常作為鎖螺絲後可以藏起螺絲頭的孔，`cboreHole` 可以建立圓柱形的平底孔，例如，將上例的 `hole(r3 * 2)` 改為 `cboreHole(r3, r3 * 2, thickness / 2)`，可以看到：

`cskHole` 可以用來建立圓錐形孔，例如將上例的 `hole(r3 * 2)` 改為 `cskHole(r3, r3 * 2, 45, thickness / 42)`，可以看到：

``````import cadquery as cq

r1 = 10
r2 = 1
n = 12
thickness = 1

plate = (cq.Workplane()
.circle(r1)
.extrude(thickness)
.faces('>Z')
.workplane()
.polarArray(r1, 0, 360, n)
.hole(r2 * 2)
)
``````

``````import cadquery as cq

w = 1
half_w = w / 2

box = (cq.Workplane()
.box(w, w, w)
.faces('+X')
.workplane()
.hole(half_w)
.faces('+Y')
.workplane()
.center(half_w, 0)
.hole(half_w)
.faces('+Z')
.workplane()
.center(0, -half_w)
.hole(half_w)
)
``````

``````import cadquery as cq

w = 1

cylinder = (cq.Workplane()
.circle(w / 4)
.extrude(w)
.translate((0, 0, -w / 2))
)

cylinders = (cylinder +
cylinder.rotate((0, 0, 0), (1, 0, 0), 90) +
cylinder.rotate((0, 0, 0), (0, 1, 0), 90)
)

box = cq.Workplane().box(w, w, w) - cylinders

show_object(box)
``````