Build LEGO models with LDraw and LeoCAD

Tutorial – LDraw and LeoCAD

Article from Issue 268/2023
Author(s):

LDraw and LeoCAD help you become a virtual LEGO architect.

For decades, young and old alike have enjoyed the well-known bricks by Danish toy manufacturer LEGO. The success story began in 1932 with wooden toys. In 1949 the first plastic bricks went into production, and since 1958 they have been available in the form that remains popular. Today, LEGO is far more than just a children's toy; there are also many adult LEGO fans. LDraw and LeoCAD help you plan your own LEGO models on your PC.

Before you get started, there are two things you need to do. First, install LDraw [1] on your system. This is an open standard for LEGO CAD programs. The tool comes with 3D models of the bricks and a file format for the notation of the models you build with the bricks. Download the complete brick list [2] and unzip the ZIP file to the ~/ldraw/ folder or another directory. The bricks included in LDraw are constantly updated.

The second step is to install a LEGO CAD program, which you use to turn the virtual bricks into digital models. Of the various programs that use the LDraw standard [3], we chose the current 21.06 version of LeoCAD [4] for this article. For the sake of simplicity, it is a good idea to download the AppImage variant [5], which you can launch directly without installing. Alternatively, pick up the source code from GitHub and compile the program yourself [6].

Digital Bricks

LDraw provides information about the available LEGO bricks and the models, while LeoCAD displays them graphically and lets you edit them. LDraw stores the information in simple text files: .dat files describe the individual bricks. They are located in the ~/ldraw/parts/ directory or in the parts/ folder in the path in which you unpacked the LDraw archive. For each brick there is a separate file, each with the part number.

In the models/ subdirectory, you will find two examples of models in LDraw: a rudimentary pyramid and a car. Files with models end with .ldr. Listing 1 shows an example. The first three lines contain the title, file name, and author's name. Each line starting with a 1 represents a brick. The number following it determines the color of the brick, and the remaining numbers indicate the position and rotation. At the end of each line there is the file with information about the brick in question. The individual steps for assembling a model are separated by lines containing 0 STEP. This means that a model can be described in a simple text file.

Listing 1

Tux.ldr

0 Tux
0 Name: Tux.ldr
0 Author: Daniel Tibi
1 14 -10 -8 -10 1 0 0 0 1 0 0 0 1 49673.dat
1 14 10 -8 -10 1 0 0 0 1 0 0 0 1 49673.dat
1 0 0 -8 10 1 0 0 0 1 0 0 0 1 28653.dat
0 STEP
1 0 0 -16 0 1 0 0 0 1 0 0 0 1 3022.dat
0 STEP
1 15 0 -40 -10 1 0 0 0 1 0 0 0 1 3660b.dat
1 0 0 -40 10 -1 0 0 0 1 0 0 0 -1 6227.dat
0 STEP
1 15 0 -48 -20 1 0 0 0 1 0 0 0 1 94148.dat
1 0 0 -48 10 1 0 0 0 1 0 0 0 1 28653.dat
0 STEP
1 0 10 -72 10 0 0 -1 0 1 0 1 0 0 4286.dat
1 0 -10 -72 10 0 0 1 0 1 0 -1 0 0 4286.dat
0 STEP
1 15 0 -72 -10 1 0 0 0 1 0 0 0 1 6227.dat
0 STEP
1 0 0 -80 0 1 0 0 0 1 0 0 0 1 94148.dat
0 STEP
1 14 0 -88 -20 1 0 0 0 1 0 0 0 1 33909.dat
1 0 0 -88 10 1 0 0 0 1 0 0 0 1 6225.dat
0 STEP
1 0 0 -112 10 1 0 0 0 1 0 0 0 1 3004.dat
0 STEP
1 0 -10 -112 -10 1 0 0 0 1 0 0 0 1 4070.dat
1 0 10 -112 -10 1 0 0 0 1 0 0 0 1 4070.dat
0 STEP
1 15 -10 -100 -22 0.866025 0 -0.5 -0.5 0 -0.866025 0 1 0 10238.dat
1 15 10 -100 -22 0.866026 0 -0.5 -0.5 0 -0.866026 0 1 0 10238.dat
0 STEP
1 0 0 -120 0 1 0 0 0 1 0 0 0 1 3068b.dat

If you just entered the models in files as abstract numeric sequences, the build would not be much fun. This is where LeoCAD comes in. It outputs the models graphically and lets you edit your creations. This is how the model in Figure 1 was created from the text file in Listing 1.

Figure 1: LeoCAD's graphical representation of Tux in bricks.

Virtual Building Fun

When you launch LeoCAD for the first time, it needs information on where the LDraw files for each LEGO brick are stored. LeoCAD itself comes with a small selection of bricks, but this would severely limit your creativity. In the View | Settings menu, open a dialog and go to the General tab. Then, in the Parts Library field, enter the path to the folder where you previously unzipped the LDraw files, for example, ~/ldraw/.

Now you get started with the virtual build. Below the menubar and toolbar, the LeoCAD window is divided into two areas. On the left you will find what is initially an empty base plate. The plate grows automatically as soon as you place bricks close to one of its edges. Top right you will see a ball that you can rotate with the mouse. This lets you move the model in any direction to view it from all sides. Pressing H switches back to the original view. You can press + to zoom into the model and - to zoom out.

On the right side of the window, LeoCAD displays a list of all available bricks at the top, followed by an overview of all colors underneath. For a better overview, simply filter the selection. At startup time, the filter defaults to Brick. You can search for bricks using the search field below. Either type in the part number you are looking for (e.g.,, 3002 for a brick with two-by-two studs) or search for the size of a brick. To do this, enter something like 2 x 2. Pay attention to the spaces; otherwise you will not see any results. In addition, you can find a specific brick by typing its name, such as slope brick. Use Insert to integrate the appropriate brick into the model or drag it to the desired position with the mouse.

Now you need to insert the brick correctly. To do this, click on the brick to select it. LeoCAD marks the selected brick with six arrows (Figure 2). The three straight arrows let you move the brick back and forth, left and right, and up and down. Alternatively, you can use the arrow keys and the Page Up and Page Down keys. The three curved arrows are used to rotate the brick on all three axes. To do this, use the same keys as you used for moving again, but this time in combination with the Shift key.

Figure 2: Align bricks in all directions using the six arrows.

During a build, you will often use single bricks or a certain arrangement several times. Instead of reinstalling and aligning each brick individually, you can simply select the brick in question (or the respective combination if you hold down Ctrl) in the model and then duplicate the selection using Ctrl+D. Then move the cloned elements to the desired location.

Up to this point, your model is still gray – high time to add some color. You can see the available colors bottom right in the window. As soon as you click on one of the colors, the bricks in the selection list on the right are painted in that color. If you want to color bricks that have already been installed, first select the desired color and then select the bricks in question. Then select the Part | Paint Selected menu item to finally color the bricks.

Build Instructions

If you want to rebuild your virtual model later with real bricks, it makes sense to follow the step-by-step instructions. To do this, insert a new build step at appropriate points when assembling the virtual model by pressing Alt+Right arrow. The File | Instructions menu item displays detailed instructions for the finished model, recording the bricks required for each step of the build (Figure 3).

Figure 3: The instructions show you how to assemble Tux in 12 steps from bricks.

A list of all bricks used in a model is provided by the Parts Used tab from the Submodel | Properties menu item (Figure 4). Switch to the Summary tab and you will see the size of the model. This gives you an idea of the dimensions the model will have when built with real bricks.

Figure 4: Parts Used lists all the bricks for a project; switch to Summary to see the model's size.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News