|
| 1 | +--- |
| 2 | +title: LDraw to OpenSCAD Tool |
| 3 | +tags: [lego ldraw, lego technic, lego robotics, lego rcx, lego mindstorms, python programming, openscad, lego cad, robotics cad, 3d cad, cad, cad-software] |
| 4 | +date: 2018-03-02 |
| 5 | +thumbnail: content/2018/03/02-ldraw-to-scad/openscad-adder-subtractor-buggy.png |
| 6 | +--- |
| 7 | +I wanted to look at some of my older Lego robotics projects and work again, and perhaps render or interact with them in a different way. I've been using OpenSCAD for a while, and I wanted to see if I could convert some of my LDraw files to OpenSCAD. This was also related to adding Lego assemblies to non-Lego robots for some modular robot designs. |
| 8 | + |
| 9 | +I wanted to also go further, and animate my Lego builds. |
| 10 | + |
| 11 | +I build a Python based tool to manage this, and I'm quite pleased with the results. |
| 12 | + |
| 13 | +## Getting it |
| 14 | + |
| 15 | +The project is all Open source, on [GitHub](https://github.com/orionrobots/ldraw-to-scad). It requires Python3 to run. |
| 16 | + |
| 17 | +You will also need an LDraw library, and OpenSCAD installed. |
| 18 | + |
| 19 | +## What features does it have? |
| 20 | + |
| 21 | +This converts the LDRaw model to an OpenSCAD model. |
| 22 | +The submodules and parts become OpenSCAD modules. The comments are also converted to OpenSCAD comments. |
| 23 | +Colour files are also supported. |
| 24 | + |
| 25 | +It's not quick, as it does need to go through the library of parts. It's also not perfect, as it doesn't handle all the LDraw features. |
| 26 | + |
| 27 | +## Do you have screenshots? |
| 28 | + |
| 29 | +Yes, a few models that have come from the Orionrobots archives are here, in OpenSCAD! These are before the colour fixes. |
| 30 | + |
| 31 | +### 4744 Lego Brick |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +The first thing was to get a single Lego brick working. In the LDraw world this is already multiple files, making use of the primitives. This is a simple brick, but it's a good test. |
| 36 | + |
| 37 | +The code shows the named primitives and subparts being sensible named modules in the OpenSCAD code, and the LDraw comments being preserved as OpenSCAD comments. |
| 38 | + |
| 39 | +### RCX Double sensor pad |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +This model was a simple two sided bumper for a Lego RCX based robot buggy. It contained multiple elements, so was a good test over the single brick. |
| 44 | + |
| 45 | +It is using a reference colour here, as the colour files are not yet fully implemented. It's clear from the render that the parts, including features like studs are represented. |
| 46 | + |
| 47 | +### Lego Adder Subtractor Buggy |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +This is the [Adder Subtractor buggy](wiki/adder_subtractor_drive.html), a Lego RCX based robot that uses the adder subtractor drive for steering - it mechanically eliminates some of the motor differences, so the robot can drive straight. |
| 52 | + |
| 53 | +This complex demonstration is a larger assembly with multiple LDraw parts files, a multi-part-data or MPD file. It took 26 seconds to render in OpenSCAD, as it's quite a large file. |
| 54 | + |
| 55 | +It may be possible to optimise the output and flatten some of the primitives in a later version - but at this point, it is all like-for-like. The OpenSCAD file has 38k lines of code! |
| 56 | + |
| 57 | +## Ideas for improvement |
| 58 | + |
| 59 | +We could consider substituting primitives for OpenSCAD primitives, like stud curves that are approximated by lines being substituted for real curves. |
| 60 | + |
| 61 | +Eg, for the 4-4cyli ldraw primitive something like this: |
| 62 | + |
| 63 | +```openscad |
| 64 | +$fn=20; |
| 65 | +rotate([-90, 0, 0]) |
| 66 | +{ |
| 67 | + render(convexity = 2) difference() { |
| 68 | + cylinder(1, r=1, [0, 0, 0], center=true); |
| 69 | + cylinder(1.2, r=0.9, [0, 0, 0], center=true); |
| 70 | + }; |
| 71 | +}; |
| 72 | +``` |
| 73 | + |
| 74 | +This needs testing, and makes the project more complex. |
| 75 | + |
| 76 | +## What's next? |
| 77 | + |
| 78 | +This project was a bit of a test, and for me to specifically get an animation working. I don't currently have long term plans, however, members of the github community have taken an interest in this, and I am happy to support them improving this tool! |
| 79 | + |
| 80 | +However, I've also found a [Blender export/importer for LDraw](https://github.com/TobyLobster/ImportLDraw), which I will look at next as it may be a good fit for me getting animations. |
| 81 | + |
| 82 | + |
| 83 | +## Helpful links for building this |
| 84 | + |
| 85 | +- [The OpenSCAD cheatsheet](https://openscad.org/cheatsheet/) incredibly helpful when building this. OpenSCAD is a very interesting CAD system, especially for programmers. This LDraw-to-scad project is effectively a transpiler for it. |
| 86 | +- [The LDraw file format](https://www.ldraw.org/article/218.html) - this is the file format I am converting from. |
| 87 | +- [LDraw multi-part files](https://www.ldraw.org/article/47.html) - The multi part files are a further specification. |
| 88 | +- [OpenSCAD Includes](https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Include_Statement) - this may be a way to reduce the duplication. Currently it generates a single big file. It could generate a library. |
0 commit comments