|Home > LDraw > Tutorial: Parts authoring|
|This article was published in
BrickJournal, Issue 7, Volume 2 • Sept./Oct. 2009
Many people build models virtually with editors like MLCad, Leo, SR 3D or Bricksmith but just a handful know how to create a part for these programs to use or what’s needed before a part can show up in the preview window of your preferred building editor. Here’s a crash course in LDraw part authoring and learn that you don’t have to be a math guru or hack your way through a jungle of ones or zeros.
|Authoring an LDraw part is like playing with LEGO bricks where loads of single pieces are going to model a complex shape with a tiny difference: instead of thousands of different bricks you only have five basic elements, but you can arrange them at any angle and in every size. But before you learn how LDraw parts authors bake them into a simple Plate 1x1 or a complex Bionicle claw, let me first guide you through an elementary class in geometry.
You’ll surely recognize the first set of three numbers (also known as a “triple”) are used to indicate the first vertex and have figured out that the second and third triples are the other vertices of the filled triangle. The very first number called “line type” tells the programs that in this case they are dealing with a triangle and that three triples have to be calculated. The line types range from 0 to 5 and the LDraw File Format Version 1.0.0 lists them as:
|Coding with MLCad. A simple part like a 3004.dat - Brick 1x2 can be assembled in MLCad without much effort just like building a model out of bricks. The first step is defining the origin of the part. This is usually the top from where the studs rise, but checking back with already existing parts helps a lot. To get visual feedback I use a special helper part I have written. I also take to heart a hint given by Niels Bugge in his Reviewing-Parts tutorial: ”a good way to arrange a part is much like an onion is structured, in a logical sequence with the innermost objects first and the rest of the file slowly growing out from there.“|
|[Screenshot of MLCad showing the black Helper setting the part’s Origin and the vertices of
the green quadrilateral. Select Edit -> Modify... from the menu bar or double click the selected
element in the parts list pane to edit the coordinates of the elements]
|The bottom rim, 24 LDUs below the origin, is made of four quadrilaterals (the CADjargon uses “quad” for abbreviation) arranged in a way to avoid a so-called T-Junction. Most authors also color the single sections to better distinguish them and assign the usual LDraw main color number “16” only at the very end. At this stage I would also add line types 2, the edges, to highlight the borders, but they are already included in the primitive I’m going to use. Primitives are the most powerful elements in the LDraw system. Think of them as a collection of highly reusable components made of lines, triangles and quads, which can be incorporated into several parts.|
|[A range of official LDraw primitives. More primitives not yet certifi ed can be found at the LDraw Parts Tracker]|
|There are two dimensional primitives like discs and rings, 3D components such as cones, boxes and cylinders, and LEGO speci fic primitives for special purposes. The most prominent among the latter are inner and outer studs, but there are also Technic connectors, click-lock hinges, axles and all sorts of clips. For the inner cavity I use a box5.dat, a cuboid with 5 faces and all edges except the top, which can be found in the LDraw\p\ directory. Properly rotated and resized primitives save a lot of work. The tricky thing is that although their code looks like as if there were four triples of coordinates it is actually driven by a “top left 3x3 matrix of a standard 4x4 homogeneous transformation matrix“. Sound complicated? It is! Let me quote from The Matrix movie, “Unfortunately, no one can be told what the Matrix is. You have to see it for yourself.” A very generic explanation would be:|
|[Screenshot of MLCad showing the dialog to manipulate the coordinates
and the transformation matrix of a primitive or sub-file]
|For a deeper insight there are plenty of sources available on the Internet but a bit of trial and error and analyzing other people’s parts and code also does the trick. Select the primitive in one of MLCad’s panes and chose Enter Pos. + Rot... from the contextual menu or click the Keyboard icon in the Transformation toolbar. Playing around with the values in the popping up Position & orientation dialog might gets you some clues too.|
|[Brick 1x2 showing its components]|
|Time to add the missing pieces: the outer shell (another box5), the two top studs and the inner Stud3.dat and “color” them with the LDraw main color number “16”, which allows to be substituted with other colors in a building editor. Some documentation using the line type “0” also doesn’t hurt. The final code will look like this:
0 Brick 1 x 2
0 Name: 3004.dat
0 // Bottom rim
4 16 20 24 10 16 24 6 -16 24 6 -20 24 10
4 16 -20 24 10 -16 24 6 -16 24 -6 -20 24 -10
4 16 -20 24 -10 -16 24 -6 16 24 -6 20 24 -10
4 16 20 24 -10 16 24 -6 16 24 6 20 24 10
0 // Inner box
1 16 0 24 0 16 0 0 0 -20 0 0 0 6 box5.dat
0 // Outer box
1 16 0 24 0 20 0 0 0 -24 0 0 0 10 box5.dat
0 // Inner Stud
1 16 0 4 0 1 0 0 0 -5 0 0 0 1 stud3.dat
0 // Top Studs
1 16 10 0 0 1 0 0 0 1 0 0 0 1 stud.dat
1 16 -10 0 0 1 0 0 0 1 0 0 0 1 stud.dat
Opening the official 3004.dat file in the LDraw\parts\ directory with MLCad or a text editor will show the same content differing only in the sequence of the code lines. At this stage I would save the work and perform some additional tweaks and checks in LDDP LDraw Design Pad, which will be described in more detail in the article “Pattern it”.
Links to Tools and Their Documentation:
|Home | LDraw | MLCad | LSynth | LDView | Instructions | Wallpapers | Space | Western | Miscellaneous | About me | Sitemap | Contact|
|All content is provided as is, with no warranty stated or implied regarding the quality or accuracy of any content on or off this site.
LEGO® is a trademark of the LEGO Group of companies which does not sponsor, authorize or endorse this site.