Skip to content

2D Rectangle

Rectangle of Lines

 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
# 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 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()

# Show all created objects which is only RectangleAsSketch
show_all()

This creates a 2D sketch and a new object RectangleAsSketch. This sketch can be extruded to get a 3D object.

See: 3D Cube

2D Rectangle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 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 Sketch builder which creates 2D sketch objects
with BuildSketch() as RectangleAsSketch:
  Rectangle(x,y)

# Show all created objects which is only RectangleAsSketch
show_all()

This gives the same sketch object as the one we created with RectangleOfLines. You might notice that we needed less information AND that this object will now be centered at the origin (x=0,y=0). All objects will normally be build by being centered as each object has an additional property called "alignt" which has as default the center option. So to make it truly equal we have to adjust it's placing.

Placing with "align"

 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

# Start the Sketch builder which creates 2D sketch objects
with BuildSketch() as RectangleAsSketch:
  # Create a Rectangle and change it's alignment property
  Rectangle(x,y,align=(Align.MIN,Align.MIN))

# Show all created objects which is only RectangleAsSketch
show_all()

The "align=" options you can choose from are:

  • Align.MIN
  • Align.MAX
  • Align.CENTER

You can use each of these options for one of the 2 paramters of the "align=" statement.

Placing with the position property of the object

Placing with the POS() command

Other options to create a rectangle or a edged 2D shape

Other ways to create a rectangle is to use the Rectangle, Polygon, RegularPolygon or Trapezoid.

See: Polygon

See: RegularPolygon

See: Trapezoid