[PYTHON] Create 3D printer data (STL file) using CadQuery

Introduction

This article introduces how to create 3D printer data (STL file) using CadQuery. CadQuery is a python library that allows you to create 3D models.

image0.jpeg

Trigger

I bought a 3D printer to expand the range of electronic work. I haven't used the 3D CAD tool itself, so after a lot of research, CadQuery and FreeCad were easy for me to use. (This time, only CadQuery)

About CadQuery

The following is the official information. There are extremely few articles in Japanese.

information

Install / start

There are multiple installation methods, but this time it's the easiest This is done by downloading CQ-editor.

From CQ-editor "Installation"-Download from "RC1 Windows".

Unzip "CQ-editor-0.1RC1-win64.zip" and execute "CQ-editor.exe" to start it.

Create a cube

① Code creation

Paste the following code into the editor section ①.

test.py


import cadquery as cq

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

② Code execution

Press the play button to execute the code. A cube is created.

0001.PNG

Make a hook with a tacking hole

Create the photo hooks listed at the top of the page.

Writing code

test.py


# This is a CadQuery script
import cadquery as cq
import itertools

#Definition of length and thickness
X1 = 40.0
X2 = 2.0
X3 = 2.0
Y1 = 40.0
Y2 = 2.0
Y3 = 10.0
Z1 = 40.0

#Described in the xy plane
result0 = (cq.Workplane("XY")
           .moveTo(0,0)
           .lineTo(X1,0)
           .lineTo(X1,Y1)
           .lineTo(X1-X2,Y1)
           .lineTo(X1-X2,Y2)
           .lineTo(X3,Y2)
           .lineTo(X3,Y3)
           .lineTo(0,Y3)
           .close())

#Extrude in the z-axis direction
result = result0.extrude(Z1)

#Tacking hole
#Definition of holes in the yz and xz planes
HOLE_Xs = [9*1, 9*3, 9*5, 9*7, 9*9, 9*11]
HOLE_Ys = [9*1, 9*3, 9*5, 9*7, 9*9, 9*11]
HOLE = 5.0

#Drilling
for hole_crd in itertools.product(HOLE_Xs, HOLE_Ys):
    result = result.faces(">X").workplane().rect(hole_crd[0], hole_crd[1], forConstruction=True).vertices().hole(HOLE)
    result = result.faces(">Y").workplane().rect(hole_crd[0], hole_crd[1], forConstruction=True).vertices().hole(HOLE)

#Displaying objects
show_object(result)

Linking with the code of the xy plane

The coordinate relationship between the definition of length and thickness is as follows. 0002.PNG

Code execution

You can run the code to create a model with hooks.

0003.PNG

Output STL file

After selecting the object, select "Tools"-"Export as STL".

0004.PNG

Specify a file name and save.

After that, print with a 3D printer.

image0.jpeg

at the end

It is very helpful to code the 3D model.

Recommended Posts

Create 3D printer data (STL file) using CadQuery
Create a dummy data file
Instantly create a diagram of 2D data using python's matplotlib
Create document classification data quickly using NLTK
Create a GIF file using Pillow in Python
Create a binary data parser using Kaitai Struct
Create wav file from GLSL shader using python3
Create interactive 3D graphs on JupyterLab using matplotlib
Create a MIDI file in Python using pretty_midi
Create a cylinder with open3d + STL file output
Create a data collection bot in Python using Selenium
Collectively register data in Firestore using csv file in Python
Create dummy data using Python's NumPy and Faker packages
Create an image file using PIL (Python Imaging Library).
Create your first GDSII file in Python using gdspy
I tried reading data from a file using Node.js.
Create a 2d CAD file ".dxf" with python [ezdxf]