Holly-Wood.it  
    Brick 1 x 3
Brick 2 x 2 Corner    
  Brick 1 x 1  
 
 
  Home > LDraw > Tutorial: Parts authoring  
    Brick 2x4
  This article was published in
BrickJournal, Issue 7, Volume 2 • Sept./Oct. 2009
 
      Brick 1x2

 

Bits 'n' Pieces

 
 

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.
 
Brick 3x3 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.


Three dimensional Cartesian coordinate [Three dimensional Cartesian coordinate
system in a isometric projection]

The Cartesian coordinate system and beyond. Got one of those fancy gadgets where you can hammer in some GPS coordinates instead of your mother-inlaw’s home address? Add a third figure and you’ve defined the location of your first point in the three dimensional LDraw universe. Starting from a de fined point called “origin” where all values are zero, you move the point (the mathematical terms is “vertex”) on all three axes gaining “length”, “width” and “height”. Since mathematicians are known to be lazy people, they use x, y, z instead and the vertex in the picture would be de fined as “20 -32 -40” because LDraw uses a right-handed coordinate system where -Y is “up”. When I started authoring LDraw parts back in 2002, I didn’t remember much math but I recalled that I need at least two vertices to define a straight line and three vertices to describe the simplest surface: the triangle! (OK, the code-gods down here at LDraw.org would argue that a disc is even simpler, but since the LDraw system knows nothing about “Area = r²·π” I think it’s OK.) Opening an LDraw file in a text editor, you would see thousands of numbers piling up row after row and if the triangle shown in the image would be part of it, its code would look like this:

Coordinates of a triangle in a 3D system
[Coordinates of a triangle in a 3D system]

3   6    20 -32 -40    -40.13 -24.4 0.5    40 -48.2 0.52

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:
  • 0: Comment or META Command
  • 1: Sub- file reference
  • 2: Line
  • 3: Triangle
  • 4: Quadrilateral
  • 5: Optional Line
These are the “bricks” an author has at his or her disposal, and that’s all that’s needed to build the brick 1x2 I’m going to author. The explanation for one entry in the line is missing, right? The number in second position represents the color of the part – according to the LDraw color table ”6” is Old Light Gray.
 
     
 
A Brick 1x2 with LDU measurements [A Brick 1x2 with LDU measurements]
Preliminaries. The first step in reengineering a brick into its CAD format is measuring the real part with a caliper or a micrometer screw and converting the millimeters or inches into LDraw’s own measuring units: LDraw Units (LDUs) where 1 LDU corresponds to 0.4 mm or 0.016 inch. A handy tool to use for these conversions is LDCalc, an LDraw Calculator written by Michael Heidemann. Some people measure all lengths in one rush and draw a rough sketch before they start working, while others proceed step-by-step and go back and forth between the real part and its digital counterpart. There are even more variations when it comes down to tools and the ways authors code a part. Some use commercial CAD tools like AutoCAD, SolidWorks or Maya, while others are happy with the available LDraw tools and editors like MLCad or LDDP, whereas very nerdy hardcore part authors program their own tools or hack an OpenOffice Calc spreadsheet.
Brick 1x1  
  Brick 1x1
   
     
  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
  [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]
 
     
Brick 1x2       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
  [A range of official LDraw primitives. More primitives not yet certifi ed can be found at the LDraw Parts Tracker]  
    Brick 1x2
  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:  
     
  LDraw matrix
     
       Brick 1x3
  • The first triple (0 24 0) are the coordinates - where the primitive is located in reference to the origin
  • The values in the transformation matrix define the overall size
  • The distribution of the numbers over the 9 available positions and whether they are positive or negative rotates and/or distorts the part
 
     
  Screenshot of MLCad showing the dialog to manipulate the coordinates
  [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 Brick 1x6
  [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
Brick 2x4
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
0

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”.
 
     



Brick 3x2 with Hole       

Facts & Figures

The LDraw Parts Library, managed by Parts Tracker Administrator Chris Dee, contains 2854 parts plus some 2205 parts currently in the queue for review and certification. So far 88 authors have contributed to the Library – some just one part other hundreds. You will find the first 3001.dat - Brick 2x4 made by James Jessiman as well as part shapes in the LDraw file format coming from the LEGO® Group through the LEGO Universe Team. The 2008-01 Update - a complete re-issue of the official library with the primary purpose of aligning the library with the Contributor Agreement and to standardizes the file headers - has been downloaded over 12000 times in the three months following its release.
Final thoughts. While most novice part authors succeed in relatively short time authoring a shape, it sometimes takes weeks or months to learn about the restrictions for certified parts: vertex sequences, BFC, conditional lines, header specification, precision, format... Since “LDrawers” prefer writing a part over writing a tutorial, the only way is learning by doing. I learned some tricks only after I had submitted my parts to the Parts Tracker for certification and they got commented on by senior authors and reviewers. Some complain that today’s entrance bar is too high, that all the restrictions, the perfectionism and non-documented tribal-knowledge scare people away. I guess the reason why there are only a handful of authors adding new parts to the library is the complexity of today’s LEGO bricks. Simple shapes, like the 1x1 slope which requires no more than half an hour to author and would be a perfect playground for newbies, are rare these days (and tend to get ‘whipped up’ in short order by advanced part authors who want to use the new part). Instead, the LEGO company puts out tons of curvy free-formed parts every year, requiring thousands of lines of code which keep even the most experienced authors busy for weeks. The “3947.dat - baseplate 32 x 32 with craters” for example is made of 5500 triangles, and it took me 3 months to get the shape right.

Links to Tools and Their Documentation:
top  
        Brick 1 x 3
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.