3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[備忘録]CADqueryサンプルコード

Last updated at Posted at 2020-08-05

はじめに

本記事は、PythonモジュールであるCadQueryのサンプルコード集です。

導入方法は、CadQueryを使って、3Dプリンタデータ(STLファイル)を作成するを参照ください。

※適宜更新予定です。

[box]直方体をかく

  • 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]直方体を座標指定で移動する

  • x:2mm, y:2mm, z:0.5mmの直方体を作成
  • 直方体の中心座標をx方向に2mm, y方向に3mm, z方向に1mm移動
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]直方体に穴をあける

  • x:80mm, y:60mm, z:10mmの直方体を作成
  • 直径22mの穴を直方体にあける
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

[hole]直方体に穴をあける(座標指定)

  • x:80mm, y:60mm, z:10mmの直方体を作成
  • 中心に対し(10, 20)をずらすした直径16mの穴を直方体にあける
test.py
import cadquery as cq

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

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

show_object(result)

image.png

[cboreHole]直方体にネジ穴をあける

  • x:80mm, y:60mm, z:10mmの直方体を作成
  • faces(">Z")
    • Z方向の一番大きい面を取得するため指定
  • rect(70.0, 50.0, forConstruction=True)
    • 70.0 x 50.0の大きさを指定
    • forConstruction=Trueは、ワイヤー参照
    • ※forConstruction=Falseは、パーツジオメトリ用
  • vertices()
    • 頂点を選択
  • cboreHole(3.0, 4.0, 2.0, depth=None)
    • 3.0mmのネジ穴
    • 4.0mmのネジ頭穴
    • 2.0mmは、ネジ頭の深さ
    • depthで、ネジ穴の深さ。None指定で貫通
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]直方体を融合する

  • x:2mm, y:2mm, z:0.5mmの2つの直方体(r1, r2)を作成
  • r1をx方向に1mm, y方向に1mm移動
  • r1と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]直方体をカットする

  • x:2mm, y:2mm, z:0.5mmの2つの直方体(r1, r2)を作成
  • r1をx方向に1mm, y方向に1mm移動
  • r1から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]円柱をつくる

  • 直径2mm, 高さ:3mmの円柱を作成
test.py
import cadquery as cq

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

image.png

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?