LDrawParser

A package for parsing LDraw™ files. As stated on the LDraw website, LDraw is an open standard for LEGO CAD programs that enables the creation of virtual LEGO models. This package allows you to parse LDraw™ files into a Julia data structure.

Manual Outline

Example usage:

using LDrawParser
path = "/path/to/ldraw_file.mpd" # or .ldr
model = parse_ldraw_file(path)
model.models
model.parts

Installation

Module

To install the module, run the following command in the Julia REPL:

] add LDrawParser

Parts Library

It is recommended to download the LDraw parts library to get full use out of your models. Functionality still exists without the parts library, including parsing the build steps, but individual part geometry is recommended for most applications. The parts library can be downloaded from LDraw™ Parts Library. Place the unzipped library in your desired path. The default path assumed by LDrawParser is joinpath(homedir(), "Documents/ldraw"). It is recommended to download the complete library (~80 MB zipped, ~450 MB unzipped).

If you did not place the parts library in the default path, you can change the path LDrawParser uses by the set_part_library_dir! command. For example, if you placed the parts library in the assets directory, you can run the following command in the Julia REPL:

using LDrawParser
set_part_library_dir!("assets/ldraw")

Usage

A handful of models are provided in the assets directory. In this example, we are reading in the tractor model and populating the part geometry. We then use the change_coordinate_system! function to change the coordinate system from the LDraw system to the standard right-handed system and scale the model by 0.5.

filename = joinpath(dirname(dirname(pathof(LDrawParser))), "assets", "tractor.mpd")
model = parse_ldraw_file(filename)

# Populate the part geometry
populate_part_geometry!(model)

# Change the coordiante system from the LDraw system to the standard right-handed system and scale model by 0.5
LDrawParser.change_coordinate_system!(model, ldraw_base_transform(), 0.5)

Functions

LDrawParser.COMMAND_CODEType
COMMAND_CODE

The line type of a line is the first number on the line. The line types are:

  1. META # 0 !<META command> <additional parameters>
  2. SUBFILEREF # 1 <colour> x y z a b c d e f g h i <file>
  3. LINE # 2 <colour> x1 y1 z1 x2 y2 z2
  4. TRIANGLE # 3 <colour> x1 y1 z1 x2 y2 z2 x3 y3 z3
  5. QUADRILATERAL # 4 <colour> x1 y1 z1 x2 y2 z2 x3 y3 z3 x4 y4 z4
  6. OPTIONAL_LINE # 5 <colour> x1 y1 z1 x2 y2 z2 x3 y3 z3 x4 y4 z4
source
LDrawParser.DATModelType
DATModel

Encodes the raw geometry of a LDraw part stored in a .dat file. It is possible to avoid populating the geometry fields, which is useful for large models or models that use parts from the LDraw library.

source
LDrawParser.FILE_TYPEType
FILE_TYPE

All LDraw files carry the LDR (default), DAT or MPD extension.

Official Parts Part | Subpart | Primitive | 8Primitive | 48Primitive | Shortcut Unofficial Parts UnofficialPart| UnofficialSubpart | UnofficialPrimitive | Unofficial8Primitive | Unofficial48Primitive | UnofficialShortcut

The file type is usually prefaced in one of the following ways 0 !LDRAWORG <type> (qualifier(s)) (update-tag) 0 LDRAWORG <type> update-tag 0 Official LCAD <type> update-tag 0 Unofficial <type> 0 Un-official <type>

source
LDrawParser.META_COMMANDType
META_COMMAND

0 !<META command> <additional parameters>

  • ! is used to positively identify this as a META command. (Note: A few official meta commands do not start with a ! in order to preserve backwards compatibility, however, all new official META commands must start with a ! and it is strongly recommended that new unofficial meta-commands also start with a !)
  • <META command> is any string in all caps
  • <additional parameters> is any string. Note that if a META command does not require any additional parameter, none should be given.
source
LDrawParser.MPDModelType
MPDModel

The MPD model stores the information contained in a .mpd or .ldr file. This includes a submodel tree (stored implicitly in a dictionary that maps model_name to SubModelPlan) and a part list. The first model in MPDModel.models is the main model. All the following are submodels of that model and/or each other.

source
LDrawParser.SubFileRefType
SubFileRef

Represents a sub-file reference from an LDraw file. Encodes the placement of a part or submodel.

source
LDrawParser.change_coordinate_system!Function
change_coordinate_system!(model::MPDModel, T=ldraw_base_transform(), scale=1.0; ignore_rotation_determinant=false)

Transform the coordinate system of the entire model using transform T and scale scale.

source
LDrawParser.find_part_fileFunction
find_part_file(name,library=get_part_library_dir())

Try to find a file with name name, and return that file's path if found.

source
LDrawParser.get_part_library_dirMethod
get_part_library_dir()

Return the path to the LDraw part library. This is the directory that contains the parts, primitives, and subparts directories.

source
LDrawParser.ldraw_base_frameMethod
ldraw_base_frame()

Returns a rotation matrix that defines the base LDraw coordinate system. LDraw uses a right-handed co-ordinate system where -Y is "up"

source
LDrawParser.parse_ldraw_fileMethod
parse_ldraw_file(filename)
parse_ldraw_file(filename; sneaky_parts::Set{String}=preprocess_ldraw_file(filename),
ignore_rotation_determinant::Bool=false)

Parse an LDraw file and return an MPDModel. Files can be .mpd or .ldr files.

Keyword Arguments:

  • sneaky_parts::Set{String}: A set of part names that are masquerading as submodels.

Often not needed to include this argument, as it is automatically generated by preprocess_ldraw_file. (default: preprocess_ldraw_file(filename))

  • ignore_rotation_determinant::Bool: If true, ignore the determinant of the

rotation matrix. Some parts have rotation matrices with negative determinants. This does not affect rendering, but can cause issues with other operations. (default: false)

source
LDrawParser.populate_part_geometry!Function
populate_part_geometry!(model,frontier=Set(collect(part_keys(model))))

Load all geometry into model.parts. Loading is recursive, so that geometry will be loaded through arbitrary levels of nested subparts until finally being stored in each atomic part that is referenced by the main model(s).

source
LDrawParser.read_meta_line!Function
read_meta_line(model,state,line)

Modifies the model and parser_state based on a META command. For example, the FILE meta command indicates the beginning of a new file, so this creates a new active model into which subsequent building steps will be placed. The STEP meta command indicates the end of the current step, which prompts the parser to close the current build step and begin a new one.

source
LDrawParser.set_part_library_dir!Method
set_part_library_dir!(path)

Set the path to the LDraw part library. This is the directory that contains the parts, primitives, and subparts directories.

source
This documentation is not for the latest stable release, but for either the development version or an older release.
Click here to go to the documentation for the latest stable release.