Files
FreeCADWasmDev/web/tools/line-course/lib/build.mjs

160 lines
8.0 KiB
JavaScript

// Shared UI building blocks for the production-line course drivers.
import { closeTask, openGroup, openPartMenu, removeObject, renameSelected,
selectTreeLabel, simpleCopy, treeLabels } from "../../carrier-course/lib/ui-actions.mjs";
export const hex = color => `#${color.map(value =>
Math.round(Math.min(1, Math.max(0, value)) * 255).toString(16).padStart(2, "0")).join("")}`;
export function createBuilder({ page, rec, logStep }) {
const labels = () => treeLabels(page);
const pause = ms => page.waitForTimeout(ms);
const applyLabel = async (from, to) => {
await selectTreeLabel(page, from);
const input = page.locator('input[aria-label="Object Label"]').first();
await input.fill(to);
await input.press("Enter");
await pause(700);
};
const place = async (label, part) => {
await selectTreeLabel(page, label);
await rec.clickSelector(page, 'button:has-text("Position")');
await page.locator('input[aria-label="Position X"]').first().fill(String(part.position[0]));
await page.locator('input[aria-label="Position Y"]').first().fill(String(part.position[1]));
await page.locator('input[aria-label="Position Z"]').first().fill(String(part.position[2]));
const [ax, ay, az] = part.rotation.axis;
await page.locator('input[aria-label="Rotation axis X"]').first().fill(String(ax));
await page.locator('input[aria-label="Rotation axis Y"]').first().fill(String(ay));
await page.locator('input[aria-label="Rotation axis Z"]').first().fill(String(az));
await page.locator('input[aria-label="Rotation angle"]').first().fill(String(part.rotation.angleDeg));
await rec.clickSelector(page, 'button[aria-label="应用位置与旋转"]');
await pause(1600);
await closeTask(page);
};
const setColor = async (label, color) => {
await selectTreeLabel(page, label);
const input = page.locator('input[aria-label="Object Color"]').first();
if (await input.count()) {
await input.fill(hex(color));
await input.dispatchEvent("change");
await pause(500);
}
};
const createBox = async (label, dims, localPosition, rotation) => {
const before = await labels();
await openPartMenu(page, rec);
await openGroup(page, "Primitives");
await rec.clickSelector(page, 'button[data-command="Part_Box"]');
await pause(800);
let created = null;
for (let attempt = 0; attempt < 20 && !created; attempt++) {
await pause(300);
created = (await labels()).find(item => !before.includes(item)) ?? null;
}
if (!created) throw new Error(`box ${label} not created`);
await selectTreeLabel(page, created);
await pause(300);
await page.locator('input[aria-label="Length"]').first().fill(String(dims[0]));
await page.locator('input[aria-label="Width"]').first().fill(String(dims[1]));
await page.locator('input[aria-label="Height"]').first().fill(String(dims[2]));
await rec.clickSelector(page, 'button:has-text("应用尺寸并重算")');
await pause(1400);
await closeTask(page);
await applyLabel(created, label);
if (localPosition) {
await selectTreeLabel(page, label);
await rec.clickSelector(page, 'button:has-text("Position")');
await page.locator('input[aria-label="Position X"]').first().fill(String(localPosition[0]));
await page.locator('input[aria-label="Position Y"]').first().fill(String(localPosition[1]));
await page.locator('input[aria-label="Position Z"]').first().fill(String(localPosition[2]));
if (rotation) {
await page.locator('input[aria-label="Rotation axis X"]').first().fill(String(rotation.axis[0]));
await page.locator('input[aria-label="Rotation axis Y"]').first().fill(String(rotation.axis[1]));
await page.locator('input[aria-label="Rotation axis Z"]').first().fill(String(rotation.axis[2]));
await page.locator('input[aria-label="Rotation angle"]').first().fill(String(rotation.angleDeg));
}
await rec.clickSelector(page, 'button[aria-label="应用位置与旋转"]');
await pause(900);
await closeTask(page);
}
logStep?.({ step: "box", label, dims, localPosition });
return label;
};
const createCylinder = async (label, radius, height, localPosition, rotation) => {
const before = await labels();
await openPartMenu(page, rec);
await openGroup(page, "Primitives");
await rec.clickSelector(page, 'button[data-command="Part_Cylinder"]');
await pause(800);
let created = null;
for (let attempt = 0; attempt < 20 && !created; attempt++) {
await pause(300);
created = (await labels()).find(item => !before.includes(item)) ?? null;
}
if (!created) throw new Error(`cylinder ${label} not created`);
await selectTreeLabel(page, created);
await applyLabel(created, label);
await selectTreeLabel(page, label);
for (const [name, value] of [["Property Radius", radius], ["Property Height", height]]) {
const input = page.locator(`input[aria-label="${name}"]`).first();
if (await input.count()) { await input.fill(String(value)); await input.press("Tab"); await pause(600); }
}
await closeTask(page);
if (localPosition) {
await selectTreeLabel(page, label);
await rec.clickSelector(page, 'button:has-text("Position")');
await page.locator('input[aria-label="Position X"]').first().fill(String(localPosition[0]));
await page.locator('input[aria-label="Position Y"]').first().fill(String(localPosition[1]));
await page.locator('input[aria-label="Position Z"]').first().fill(String(localPosition[2]));
if (rotation) {
await page.locator('input[aria-label="Rotation axis X"]').first().fill(String(rotation.axis[0]));
await page.locator('input[aria-label="Rotation axis Y"]').first().fill(String(rotation.axis[1]));
await page.locator('input[aria-label="Rotation axis Z"]').first().fill(String(rotation.axis[2]));
await page.locator('input[aria-label="Rotation angle"]').first().fill(String(rotation.angleDeg));
}
await rec.clickSelector(page, 'button[aria-label="应用位置与旋转"]');
await pause(900);
await closeTask(page);
}
logStep?.({ step: "cylinder", label, radius, height, localPosition });
return label;
};
const compoundCopy = async (sources, name, baseLabels) => {
await openPartMenu(page, rec);
const group = page.locator("details.part-submenu").filter({ has: page.locator("summary", { hasText: /^Compound$/ }) }).first();
if (!(await group.evaluate(element => element.open))) { await group.locator(":scope > summary").click(); await pause(300); }
await rec.clickSelector(page, '.part-command-menu button[data-command="Part_Compound"]');
const task = page.getByRole("region", { name: "Compound tools" });
await task.waitFor({ state: "visible", timeout: 30000 });
for (const source of sources) {
const box = task.locator("label").filter({ hasText: source }).first().locator('input[type="checkbox"]').first();
if (!(await box.isChecked())) await box.click();
await pause(250);
}
await task.getByRole("button", { name: "Create compound feature", exact: true }).click();
await pause(1400);
await closeTask(page);
const after = await labels();
const compound = after.find(label => !baseLabels.includes(label) && !sources.includes(label)) ?? null;
if (!compound) throw new Error(`compound not created: ${JSON.stringify(after)}`);
await selectTreeLabel(page, compound);
await simpleCopy({ page, rec, source: compound });
const copied = await labels();
const copy = copied.find(label => label !== compound && label.startsWith(compound)) ?? null;
if (!copy) throw new Error(`compound copy not found: ${JSON.stringify(copied)}`);
await selectTreeLabel(page, copy);
await renameSelected(page, name);
await removeObject(page, rec, compound);
for (const source of sources) await removeObject(page, rec, source);
logStep?.({ step: "compound", compound, copy, name, labels: await labels() });
return name;
};
return { labels, pause, place, setColor, createBox, createCylinder, compoundCopy, applyLabel };
}