# 實作 polyhedron

``````from cadquery import Vector, Edge, Wire, Solid, Shell, Face

def polyhedron(points, faces):

def _edges(vectors, face_indices):
leng_vertices = len(face_indices)
return (
Edge.makeLine(
vectors[face_indices[i]],
vectors[face_indices[(i + 1) % leng_vertices]]
)
for i in range(leng_vertices)
)

vectors = [Vector(*p) for p in points]

return Solid.makeSolid(
Shell.makeShell(
Face.makeFromWires(
Wire.assembleEdges(
_edges(vectors, face_indices)
)
)
for face_indices in faces
)
)
``````

``````# 結合方才的 polyhedron

points = (
(10, -10, 0),
(10, 10, 0),
(-10, 10, 0),
(-10, -10, 0),
(10, -10, 10),
(10, 10, 10),
(-10, 10, 10),
(-10, -10, 10)
)

faces = (
(0, 3, 2, 1),
(4, 5, 6, 7),
(0, 1, 5, 4),
(3, 7, 6, 2),
(0, 4, 7, 3),
(1, 2, 6, 5)
)

solid = polyhedron(points, faces)
show_object(solid)
``````

`faces` 只要共平面就可以了，例如，底下的金字塔混合了三角面與四邊形面：

``````# 結合方才的 polyhedron

points = (
# XY 平面上四個頂點
(10, 10, 0), (10, -10, 0), (-10, -10, 0), (-10, 10,0),
# 金字塔尖端
(0, 0, 10)
)

faces = (
# 底部的四邊形
(0, 1, 2, 3),
(4, 1, 0), (4, 2, 1), (4, 3, 2), (4, 0,3),  # 四個三角面
)

solid = polyhedron(points, faces)
show_object(solid)
``````

``````# 結合方才的 polyhedron

points = (
(5, -5, -5), (-5, 5, -5), (5, 5, 5), (-5, -5, 5)
)

faces = (
(0, 1, 2), (0, 3, 1), (1, 3, 2), (0, 2, 3)
)

solid = polyhedron(points, faces)
show_object(solid)
``````