Last active
May 5, 2022 13:31
-
-
Save schultzcole/d09503e099b930b256f06afe4f538467 to your computer and use it in GitHub Desktop.
A macro script to convert polygonal drawings to walls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author cole#9640 | |
* @version 2 | |
* Converts all selected rectangular and polygonal drawings to walls. | |
*/ | |
let drawings = canvas.drawings.controlled; | |
drawings = drawings.map(drawing => { | |
switch (drawing.data.type) { | |
case "f": | |
case "p": { | |
let { _id, points, rotation, x, y, width, height } = drawing.data; | |
return { id: _id, valid: true, points, rotation, x, y, width, height }; | |
} | |
case "r": { | |
let { _id, rotation, x, y, width, height } = drawing.data; | |
const points = [ | |
[0, 0], | |
[width, 0], | |
[width, height], | |
[0, height], | |
[0, 0], | |
]; | |
return { id: _id, valid: true, points, rotation, x, y, width, height }; | |
} | |
default: | |
return { id: drawing.data._id, valid: false }; | |
} | |
}).filter(drawing => { | |
if (!drawing.valid) { | |
ui.notifications.warn(`Drawing "${drawing.id}" is not a valid drawing type`); | |
return false; | |
} | |
return true; | |
}); | |
if (drawings.length) { | |
const newWalls = drawings.flatMap((drawing) => { | |
const { x, y, width, height } = drawing; | |
const xCenterOffset = width/2; | |
const yCenterOffset = height/2; | |
const θ = toRadians(drawing.rotation); | |
const cosθ = Math.cos(θ); | |
const sinθ = Math.sin(θ); | |
const points = drawing.points.map((point) => { | |
const offsetX = point[0] - xCenterOffset; | |
const offsetY = point[1] - yCenterOffset; | |
const rotatedX = (offsetX * cosθ - offsetY * sinθ); | |
const rotatedY = (offsetY * cosθ + offsetX * sinθ); | |
return [rotatedX + x + xCenterOffset, rotatedY + y + yCenterOffset]; | |
}); | |
return points | |
.slice(0, points.length - 1) | |
.map((point, i) => ({ c: point.concat(points[i + 1]) })); | |
}); | |
canvas.scene.createEmbeddedEntity("Wall", newWalls); | |
canvas.walls.activate(); | |
} else { | |
ui.notifications.error("No polygon drawings selected!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment