[PYTHON] [Note] CAD query sample code

Introduction

This article is a collection of sample code for CadQuery, a Python module.

Please refer to Creating 3D printer data (STL file) using CadQuery for the installation method.

[box] Draw a rectangular parallelepiped

--Create a rectangular parallelepiped of x: 2mm, y: 2mm, z: 0.5mm

test.py


import cadquery as cq

result = cq.Workplane("front").box(2.0, 2.0, 0.5)
show_object(result)

image.png

[translate] Move a rectangular parallelepiped by specifying coordinates

--Create a rectangular parallelepiped of x: 2mm, y: 2mm, z: 0.5mm --Move the center coordinates of the rectangular parallelepiped 2 mm in the x direction, 3 mm in the y direction, and 1 mm in the z direction.

test.py


import cadquery as cq

result = cq.Workplane("front").box(2.0, 2.0, 0.5)
result = result.translate((2, 3, 1))
show_object(result)

image.png

[hole] Make a hole in the rectangular parallelepiped

--Create a rectangular parallelepiped of x: 80mm, y: 60mm, z: 10mm --Drill a hole with a diameter of 22m in a rectangular parallelepiped

test.py


import cadquery as cq

length = 80.0
height = 60.0
thickness = 10.0
center_hole_dia = 22.0

result = (cq.Workplane("XY").box(length, height, thickness)
    .faces(">Z").workplane().hole(center_hole_dia))

show_object(result)

image.png

[cboreHole] Make a screw hole in a rectangular parallelepiped

--Create a rectangular parallelepiped of x: 80mm, y: 60mm, z: 10mm

test.py


import cadquery as cq

length = 80.0
height = 60.0
thickness = 10.0

result = (cq.Workplane(cq.Plane.XY()).box(length, height, thickness))
result = result.faces(">Z").workplane().rect(70.0, 50.0, forConstruction=True).vertices().cboreHole(3.0, 4.0, 2.0, depth=None)

show_object(result)

image.png

[union] Fusion of rectangular parallelepipeds

--Create two rectangular parallelepipeds (r1, r2) of x: 2mm, y: 2mm, z: 0.5mm --Move r1 1mm in the x direction and 1mm in the y direction --Fusion of r1 and r2

test.py


import cadquery as cq

r1 = cq.Workplane("front").box(2.0, 2.0, 0.5)
r1 = r1.translate((1, 1, 0))
r2 = cq.Workplane("front").box(2.0, 2.0, 0.5)
r2 = r2.translate((0, 0, 0))

r1 = r1.union(r2)

show_object(r1)

image.png

[cut] Cut a rectangular parallelepiped

--Create two rectangular parallelepipeds (r1, r2) of x: 2mm, y: 2mm, z: 0.5mm --Move r1 1mm in the x direction and 1mm in the y direction --Cut the part from r1 to r2

test.py


import cadquery as cq

r1 = cq.Workplane("front").box(2.0, 2.0, 0.5)
r1 = r1.translate((1, 1, 0))
r2 = cq.Workplane("front").box(2.0, 2.0, 0.5)
r2 = r2.translate((0, 0, 0))

r1 = r1.cut(r2)

show_object(r1)

image.png

[circle] Make a cylinder

--Create a cylinder with a diameter of 2 mm and a height of 3 mm

test.py


import cadquery as cq

result = cq.Workplane("front").circle(2).extrude(3)
show_object(result)

image.png

Recommended Posts

[Note] CAD query sample code
Spark Dataframe Sample Code Collection
[Python] Sample code for Python grammar
A small sample note of list_head
Python parallel / parallel processing sample code summary
A note for writing Python-like code