3D Cube
There are several ways to create a 3D cube. For build123d a cube will be called a box.
Extruded rectangle of lines
This example is a continuation of the rectangle of lines where we started by creating a rectangle out of lines, making a sketch (2D object) out of it and now as a final step we will extrude this 2D object to get a 3D object.
For most 3D primitives there is a direct way to create them.
Extruding is simple. We need a new builder "BuildPart()" and one new command "extrude".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 | # Import ocp_vscode which is an addon to view the 3D model in VScode
from ocp_vscode import *
# Import build123d library to be able to use build123 commands
from build123d import *
# Define an x and y value for the rectangle
x=20
y=10
# Start the Part builder which creates 3D part objects
with BuildPart() as RectangleAsPart:
# Start the Sketch builder which creates 2D sketch objects
with BuildSketch() as RectangleAsSketch:
# Start the Line Builder and define the name of the finished line object as RectangleOfLines
with BuildLine() as RectangleOfLines:
# Define first line
Line((0,0),(x,0))
# Define second line
Line((x,0),(x,y))
# Define third line
Line((x,y),(0,y))
# Define fourth and last line to close the rectangle shape
Line((0,y),(0,0))
# Create a face from your RectangleOfLines object. Lines must be closed to encompass a closed area.
make_face()
# Extrudes our 2D object to a hight of 4
extrude(amount=4)
# Show all created objects which is RectangleAsPart
show_all()
|
As you can see we added "BuildPart()" in line 11 and extrude in line 26. As extrude has a few more possible parameters we need to make sure build123d knows that we use the "amount" parameter. That is why we write amount=4 to extrude our rectangle to a hight of 4.
Making a 3D cube
Build123d also has a box command which will create a 3D cube directly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | # Import ocp_vscode which is an addon to view the 3D model in VScode
from ocp_vscode import *
# Import build123d library to be able to use build123 commands
from build123d import *
# Define an x and y value for the rectangle
x=20
y=10
z=4
# Start the Part builder which creates 3D part objects
with BuildPart() as RectangleAsPart:
# Creat a 3D cube
Box (x,y,z)
# Show all created objects which is RectangleAsPart
show_all()
|