# Draper: Drapes objects onto terrain, setting them down on two feet and rotat - Pastebin.com ← Current code
Imgur: The magic of the Internet ← I’m using it for draping long (100’) solar panel racks across the terrain. These racks have several legs (“piles”), and the outermost piles are around 15’ from the outer edge of the rack, thus the “$feet_from_edge = 189 # INCHES from outside of component to middle of first pile”.
Code only works for rotating about the red axis (east/west) because solar panel racking like this typically is oriented north/south. I haven’t touched the code since last week but if I recall, it looks at the object like a rectangular bounding box, and finds the lowest side (lowest z), then the outermost edges on the green axis (perpendicular to the red axis), and the middle. Those points are called the two “feet” (moved inward by $feet_from_edge if non-zero). The code lowers the components down onto the terrain and rotates it about the lower center along the red axis, such that the end result is that the two “feet” are touching the ground. A non-working part of the code that I haven’t gotten working is a “negative allowed clearance”, such that after setting the component on the ground, it checks along a line between the two feet to see if any terrain goes up INTO the object more than X feet/inches, and if so, moves the whole component up to compensate. The operations are all performed as one so you can just do “undo” to restore it.
The code only works for multiple components when you select a bunch of components that are NOT in a group. I spent far too much time trying to get it working with groups and nested groups and it just kept having issues so I gave up.
Ideally someone would fix this up to allow the user to select the axis (red or green, or solar north/south or solar east/west, or a custom axis), or to establish FOUR feet (north/south/east/west, etc) and to put all four on the ground. Other features that would be handy would be allowing this to work on groups or nested groups, not just a selection of multiple components. Perhaps a GUI and integrating it as a plugin. If someone goes that far, credit for the initial coding would be appreciated.
(Note: Once all of the components have been “draped”, you can’t easily undo it some time after the fact. I have code that I haven’t shared that tries to “undrape” them by lifting the components up 100’ above the terrain and unrotating but it just doesn’t work and I don’t have time to get it working right now.)
To test:
- Get a geolocated area with some varied terrain, or make a terrain mesh.
- Make a bunch of rectangles that are long in the north/south direction. (I have only tested the code on components, so you may need to make each rectangle an instance of a component)
- Select all of the components (do NOT group them)
- Run the code in the Ruby console (be sure to adjust the $feet_from_edge to 0, or whatever you want)
Please let me know what you think! I hope to make some time to make it into a plugin myself, but I won’t be able to get to it for a while.