Skip to content

Instantly share code, notes, and snippets.

@mattexdee
Forked from schultzcole/drawings-to-walls.js
Last active October 20, 2023 11:14
Show Gist options
  • Save mattexdee/19f5ae486ad00c33dd3d1508278bd2c9 to your computer and use it in GitHub Desktop.
Save mattexdee/19f5ae486ad00c33dd3d1508278bd2c9 to your computer and use it in GitHub Desktop.
A macro script to convert 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, strokeWidth } = drawing.data;
const points = [
[0 + (strokeWidth / 2), 0 + (strokeWidth / 2)],
[width - (strokeWidth / 2), 0 + (strokeWidth / 2)],
[width - (strokeWidth / 2), height - (strokeWidth / 2)],
[0 + (strokeWidth / 2), height - (strokeWidth / 2)],
[0 + (strokeWidth / 2), 0 + (strokeWidth / 2)]
];
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 θ = Math.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.createEmbeddedDocuments("Wall", newWalls);
canvas.walls.activate();
} else {
ui.notifications.error("No drawings selected!");
}
@KageJittai
Copy link

This macro no longer works on Foundry v10

@mrjackphil
Copy link

@KageJittai Had the same problem. Reworked script a little bit:
https://gist.github.com/mrjackphil/49a435432458f7fcf7844a405dae0d59

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment