lego ergo sum-LDraw-mode

For the impatient soul: Download the source right away: ldraw-mode.el. The source code is published under the GNU General Public License.

Table of contents

What is LDraw?
What is Emacs?
What is LDraw-mode?
lego ergo sum-LDraw-mode-What is LDraw?

What is LDraw?

LDraw is a freeware program which can create LEGO models in 3D. LDraw was created by James Jessiman. LDraw has proven a very popular program and format to model LEGO creations for display and download on the internet.

Obtaining and installing LDraw

Please refer to the documentation on This will tell you how to download the packages you need and how to install them. Instructions are provided for both Windows and GNU/Linux users.
lego ergo sum-LDraw-mode-What is Emacs?

What is Emacs?

"Emacs is the extensible, customizable, self-documenting real-time display editor."

Obtaining and installing Emacs

Emacs is free software, and can be used on many kinds of systems, including most UNIX-like operating systems, Windows, MacOS, Amiga, etc. You will need Emacs version 20.x to use LDraw-mode.

Windows users

If you're a Windows user, please look at for instructions on how to install Emacs.

GNU/Linux users

If you're using some UNIX-like operating system, chances are you already have Emacs installed. Check if you have version 20.1 or later by issuing this command:

% emacs --version

If you need to download and install Emacs, you can choose either to grab the full source from and compile it yourself, or you can get a precompiled package for your system. E.g., if you are using Red Hat Linux, you would typically grab an RPM package file. Looking at may be a good starting point.

If you are new to Emacs...

If you are new to Emacs, I advice you to read the documentation and tutorial on the "Help" pull down menu. If you are too impatient to do this, you may want to read about some major differences between Emacs and similar Windows applications.
lego ergo sum-LDraw-mode-What is LDraw-mode?

What is LDraw-mode?

LDraw-mode is a major mode for editing LDraw files in Emacs. It adds some functionality designed to make typical tasks easier. You will need to have Emacs version 20.x installed to use LDraw-mode.


Here's the source code for LDraw-mode: ldraw-mode.el. If you are a seasoned Emacs user, you can just grab it and get busy. I've written a few words on how to install it under the "INSTALL" heading in the source code.

If you are new to Emacs, you can read some step by step instructions on how to obtain and install LDraw-mode. I have divided it into two sections: One for Windows users and one for users of GNU/Linux or similar systems.

Please report any bugs, and issue any questions to me.
lego ergo sum-LDraw-mode-Documentation


In addition to this page, you can get some documentation by hitting C-h m when LDraw-mode is active, i.e. when editing an LDraw DAT file. If the *Help* buffer has a lot of text in it, search for "ldraw" to find the relevant portion.

You can also click on the indiviual ldraw-mode functions in the key binding section to learn more about them. Or you can obtain information about a specific key stroke by prepending it with C-h k. E.g., pressing C-h k C-c C-r gives you information about the function ldraw-insert-ring-primitive.
lego ergo sum-LDraw-mode-Features


So far, this is what I have implemented:

Font-lock patterns

Automatically colours the buffer with font-lock according to the contents. You'll need to turn on Emacs' font-lock facility first, obviously. Put something like (global-font-lock-mode 1) in your .emacs file. Here's a screenshot to give you an idea what it will look like:

Note that the line in which the cursor is located has a red warning font because it is syntactically wrong. This feature enables you to quickly discover errors.

Also note the three levels of inlining which has been performed. More on this further down.

Modeling bent flexible hoses with Bézier curves

Simple hoses

Flexible Technic axles

LDraw-mode has built in support for the flexible technic axles. Here's what you do to insert a bent flexible axle.

  1. Insert the end points where you want them. Here's an example. Note that the open ends of the end segments must point inwards in the direction of the flexible axle.
  2. Put the cursor on either of the lines containing the end points and press C-c C-b. LDraw-mode will prompt you for the length of the flexible part of the axle in LDraw units. I chose 180 below, which corresponds to a flexible axle with a total length of 11 studs (32199.dat).

Flexible hose, 8.5L

LDraw-mode can be used to model the 73590.DAT Hose Flexible 8.5L very easily too. Here's what you do.

  1. Insert the end points where you want them. Here's an example.

    You can also use the 752.DAT Hose Flexible End 1 x 1 x 2/3 without Tabs if you want the other version of the hose.

  2. Put the cursor on either of the lines containing the end points and press C-c C-b. That's all!

Technic Flex-System hoses

To model Technic Flex-System Hoses, follow this procedure:

  1. Insert the end points where you want them. Here's an example.
  2. Put the cursor on either of the lines containing the end points and press C-c C-b. You will be prompted for the length of the segment to insert. In the example below, I answered 82, which gave a total lenght of 122, including the two end segments.

Technic ribbed hoses

To model Technic ribbed hoses, follow this procedure:

  1. Insert the end points where you want them. Here's an example.
  2. Put the cursor on either of the lines containing the end points and press C-c C-b. You will be prompted for the number of notched segments to insert. Keeping in mind that there were two end segments present already, I answered 23 below, corresponding to a hose with a total of 25 notched segments.

Advanced usage of the Bézier tool

LDraw-mode can be used to model most other bent flexible hoses as well with Bézier curves. Click here to see information about the advanced usage of the function.

View parts

You can download and view an image of the part on the line currently being edited from the LUGNET partsref by hitting C-c C-p. This feature requires that you have William M. Perry's W3 installed.


Sub-models, parts and primitives can be inlined by hitting C-c C-i. This can be done recursively, so that parts contained in an inlined sub-model can themselves be inlined.

Searching for parts

If you want to search for a specific part, hit C-c C-s. This puts you in a buffer containing the PARTS.LST file. Press ENTER to select a part. If the line currently being edited was a part line, the selected part is substituted for the original one. The name of the part is also inserted into the kill-ring. If you don't want to select any parts, hit DEL or BACKSPC to exit the search buffer.

Inserting ring primitives

Sometimes I find it a bit difficult to know which ring primitive to use to cover a specific area. The function ldraw-insert-ring-primitive bound to C-h C-r prompts you for an inner and outer radius, and does the calculations for you.

If one ring fits the space you wanted, it is inserted into the buffer. If two overlapping rings are required, you are warned about this, and they are inserted. If no ring is narrow enough to fit the space you wanted, you are asked if you want the ring to match the inner or outer radius you requested. If you choose it to match the inner radius, the ring will be too wide and vice versa.

Fill out a surface between two curves

When modeling LEGO parts, I sometimes need to fill out a curved surface with polygons. This can be a bit tedious, besides, I find it hard to insert the type-5 conditional lines properly. The function ldraw-fill-in-surface bound to C-c C-f is designed to make this task easier.

The program assumes that you already have two curves made up of type-2 edge lines and that you want to fill in the surface between them. The two curves must have the same number of line segments, and they must both be connected. I.e., the first line in the first curve must have one coordinate in common with the second line in the first curve and so on.

The program looks at each line segment pair individually (the first segment in curve 1 and curve 2, then the second segment, and so on). If these four coordinates are all in the same plane, the program adds a quadilateral between the two line segments. If they are not on the same plane, two triangles are used in stead, and a type-5 conditional line is added between them. Type-5 conditional lines are also added between each quadilateral (or triangle pair). If at least one of the two curves are not cyclic, then type-2 edge lines are used in each end.

The program will prompt you for a colour to use for the surface, and uses the colour of the original type-2 edge lines for the type-2 and type-5 conditional lines it inserts.

It is probably best to explore this function by an example. Insert something like this into an LDraw-file:

 1 16 0 0 0 10 0 0 0 0 0 0 0 10 2-4edge.dat
 1 16 0 -5 0 10 0 0 0 0 0 0 0 10 2-4edge.dat

This is two half-circles with radius 10 each, one 5 units above the other.

Now, inline both primitives. That is, put the cursor on the first line above, hit C-c C-i and do the same with the next. This should give you two times eight type-2 edge lines.

Put the cursor on the first type-2 line. Set the mark, that is hit C-SPC. Move the cursor down to the first type-2 line inlined from the second primitive. That should be halfway down. Hit C-c C-f. This should generate a half cylinder between the two edge lines.

In this simple example, it would have been better to use a half cylinder primitive in the first place, of course, but this function is handy if you want to make a more complicated surface, such as, for example, a cone where the upper edge is oval and the lower is circular.



Here's a list of things I plan to add in the future:

lego ergo sum-LDraw-mode