/* https://www.roomle.com/t/cp/ */
var doNotFreshload;
/*
Uncomment following to pre-init doNotFreshload variable to make the current configuration reload with updated components, without loading the configuration with the last component only.
*/
//doNotFreshload = true;
/* Uncomment following to never reload current configuration */
//doNotFreshload = false;
(async function () {
const prefix = 'http://127.0.0.1:5500/<REPOSITORY FOLDER>/<CATALOGUE>/components';
const urls = [
'component1.json'
, 'component2.json'
// Because we added /**/ comment at the end of this, and we put commas at the beginning of the lines, you can easily comment out components without deleting them from the snippet. You will then load component2.json if you comment it out like this. Watch out for dependencies. Therefore, keep the components in this snippet sorted in a way that component on the next line has all dependencies already loaded!
/*
, 'component3.json'
, 'root_component.json'
/**/
];
const responses = await Promise.all(urls.map((url) => fetch(prefix + url)))
const components = await Promise.all(responses.map((response) => response.text()));
let lastComponentId = '';
components.forEach((component) => {
// Parse the JSON to object. You can do whatever you want with it in this snippet later. The Regex escapes newlines in the component, making JSON with multiple lines valid. Note: Windows use /r/n as line ends. This is also handled.
let jsonComponent = JSON.parse(component.replace(/((\/\/|#).*)?\r?\n|\r/g, ''));
// Store the last componentId to load it at the end in doNotFreshload.
lastComponentId = jsonComponent.id;
// Undefined by the API, but you can leave debugGeometry in the script. This injects what you have in the debugGeometry at the end of the actual geometry.
if (jsonComponent.debugGeometry) {
jsonComponent.geometry = jsonComponent.geometry.concat(jsonComponent.debugGeometry);
}
// Inject text feature of the loader snippet. Will inject a 3D floating text in the scene that can display any information you want.
if (jsonComponent.geometry) {
const foundDebugTextCalls = jsonComponent.geometry.match(/(?<=#INJECTTEXT)(.*?)(?=#ENDINJECTTEXT)/g);
if (foundDebugTextCalls) {
let internalId = 'DEBUG_TEST_INJECTED_SUBCOMPONENT_';
let debugTextInjectionNumber = 0;
foundDebugTextCalls.forEach((debugtext) => {
console.log(debugtext);
let split = debugtext.split(';');
let data = {
"condition": split[0],
"x": split[1],
"y": split[2],
"z": split[3],
"text": split[4]
};
debugTextInjectionNumber++;
if (!jsonComponent.subComponents) { jsonComponent.subComponents = []; }
jsonComponent.subComponents.push(
{ "internalId": internalId + debugTextInjectionNumber, "componentId": "isdt:text", "assignments": { "text": data.text } }
);
jsonComponent.geometry = jsonComponent.geometry.concat
(
"if (" + data.condition + ") {" +
"SubComponent(" + (internalId + debugTextInjectionNumber) + ");" +
"MoveMatrixBy(Vector3f{" + data.x + "," + data.y + "," + data.z + "});" +
"}"
);
});
}
}
// For IDM-imported components: Place the idmFeature key number at the beginning of the label in every language. Makes working with IDM components easier.
if (jsonComponent.parameters) {
jsonComponent.parameters.forEach(p => {
if (p.key) {
if (p.key.includes("idmFeature")) {
var x = p.key.substring(10, p.key.length);
for (var k in p.labels) {
p.labels[k] = x + " : " + p.labels[k];
}
// Show value object values at beginning of every label
if (p.valueObjects) {
p.valueObjects.forEach(vo => {
if (vo.value && vo.labels) {
var val = vo.value;
for (var k in vo.labels) {
vo.labels[k] = val + " : " + vo.labels[k];
}
}
});
}
}
}
});
}
// Deactivate parameter groups. Also delete possible children's group, otherwise they do not show.
jsonComponent.parameterGroups = null;
if (jsonComponent.possibleChildren) {
jsonComponent.possibleChildren.forEach(pc => {
pc.group = null;
});
}
// Stringify the object and feed it to the configurator.
RoomleConfigurator._plannerKernelAccess._kernelInstance.loadComponentDefinition(10, JSON.stringify(jsonComponent));
// If you see this log, the current component passed. If you read componentId component1.json in the console, error is in component2.json, which did not reach to here.
console.log(`loaded ${lastComponentId} into kernel`);
});
// if false or uninitialized
if (doNotFreshload) {
var previous_configuration = await RoomleConfigurator.getCurrentConfiguration();
RoomleConfigurator.loadConfiguration(previous_configuration);
console.log(`reloading previous config`);
} else {
// if doNotFreshload is not yet inited, load the component with the lastComponentId.
RoomleConfigurator.loadConfiguration(`{"componentId":"${lastComponentId}"}`);
// initialize in order to reload previous config next time
doNotFreshload = true;
console.log(`freshloaded: {"componentId":"${lastComponentId}"}`);
}
}());