Skip to content

Instantly share code, notes, and snippets.

@timolaine
Created March 11, 2025 11:39
Show Gist options
  • Select an option

  • Save timolaine/4258d2e631fc75e061557ef7bba83527 to your computer and use it in GitHub Desktop.

Select an option

Save timolaine/4258d2e631fc75e061557ef7bba83527 to your computer and use it in GitHub Desktop.
Arranges selected elements alphabetically into a neat grid in #jArchi. The number of columns is user configurable.
/*
* Mapify
* Author: Timo Laine (timoroso.com)
* Arranges selected elements alphabetically into a neat grid.
* The number of columns is user configurable.
*/
// Get Archi's Preference Store
const archiPrefs = Java.type('com.archimatetool.editor.ArchiPlugin').PREFERENCES;
var fixedColumns = window.prompt("How many columns you want in the map? (Zero for automatic.)", "0");
var selection = $(selection);
if (selection.length === 0) {
console.log("No elements selected.");
} else {
// Get the size of the first element as reference
const firstElement = selection.first();
const elementWidth = firstElement.bounds.width;
const elementHeight = firstElement.bounds.height;
// Sort elements alphabetically by name
let sortedElements = selection.toArray().sort((a, b) => a.name.localeCompare(b.name));
// Define the aspect ratio (16:10) and grid size;
const aspectRatio = 16 / 10;
const gridSize = archiPrefs.getInt('gridSize');
const totalElements = selection.length;
// Calculate the best row count based on the aspect ratio
let columns = fixedColumns > 0 ? fixedColumns : Math.ceil(Math.sqrt(totalElements * aspectRatio));
let rows = Math.ceil(totalElements / columns);
// Starting position
let startX = firstElement.bounds.x;
let startY = firstElement.bounds.y;
// Arrange elements in a grid
var index = 0;
sortedElements.forEach((element) => {
let row = Math.floor(index / columns);
let col = index % columns;
let newX = startX + col * elementWidth + col * gridSize;
let newY = startY + row * elementHeight + row * gridSize;
element.bounds = { x: newX, y: newY };
index++;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment