Files
FreeCADWasmDev/web/src/openscad-format-facade.ts
wangdequan 9a7c4f0335 Save FreeCAD Web workspace sources and user validation evidence
IFC native binding remains incomplete; preserve accurate test results.
2026-09-15 22:01:58 +08:00

45 lines
3.7 KiB
TypeScript

import { FacadeHandlerError, FacadeRequest, FacadeResponse, FacadeSerialQueue, createFailure, createRequest, isFacadeRequest } from "./facade-envelope.js";
import type { OpenScadKernelModule } from "./openscad-inspect-facade.js";
export const OPENSCAD_IMPORT_DXF_SCHEMA = "freecad-web-openscad/import-dxf/v1" as const;
export const OPENSCAD_IMPORT_DXF_COMMAND = "openscad.importDxf" as const;
export interface OpenScadImportDxfPayload { schema: typeof OPENSCAD_IMPORT_DXF_SCHEMA; module: "OpenSCAD"; documentId: string; path: string; layer?: string; }
export interface OpenScadImportDxfResult { schema: typeof OPENSCAD_IMPORT_DXF_SCHEMA; module: "OpenSCAD"; documentId: string; documentHandle: string; createdDocument: boolean; objectNames: string[]; objectCount: number; faceCount: number; recomputed: true; sourceEntry: string; resourcesReleased: true; }
export type OpenScadImportDxfRequest = FacadeRequest<OpenScadImportDxfPayload>;
export function createOpenScadImportDxfRequest(requestId: string, payload: OpenScadImportDxfPayload): OpenScadImportDxfRequest {
return createRequest(requestId, OPENSCAD_IMPORT_DXF_COMMAND, payload, payload.documentId);
}
export function isOpenScadImportDxfRequest(value: unknown): value is OpenScadImportDxfRequest {
if (!isFacadeRequest(value)) return false;
const request = value as OpenScadImportDxfRequest;
const payload = request.payload;
return request.command === OPENSCAD_IMPORT_DXF_COMMAND && payload?.schema === OPENSCAD_IMPORT_DXF_SCHEMA && payload?.module === "OpenSCAD"
&& typeof payload.documentId === "string" && payload.documentId.length > 0 && typeof payload.path === "string" && payload.path.length > 0
&& (payload.layer === undefined || typeof payload.layer === "string");
}
export async function dispatchOpenScadImportDxf(queue: FacadeSerialQueue, request: OpenScadImportDxfRequest, kernelSource: OpenScadKernelModule | Promise<OpenScadKernelModule>): Promise<FacadeResponse<OpenScadImportDxfResult>> {
const requestId = request.requestId;
if (!isOpenScadImportDxfRequest(request)) return createFailure(requestId, "invalid_request", "OpenSCAD DXF import envelope is invalid");
return queue.dispatch(request, async (payload, signal) => {
if (signal.aborted) throw new FacadeHandlerError("cancelled", "request was cancelled");
const kernel = await kernelSource;
const raw = String(kernel.ccall("fc_web_dispatch_json", "string", ["string"], [JSON.stringify({ cmd: OPENSCAD_IMPORT_DXF_COMMAND, args: payload })]));
let reply: Record<string, unknown>;
try { reply = JSON.parse(raw) as Record<string, unknown>; } catch { throw new FacadeHandlerError("native_protocol_error", "OpenSCAD native dispatcher returned invalid JSON"); }
if (reply.ok !== true) throw new FacadeHandlerError(String(reply.code || "import_failed"), String(reply.error || "OpenSCAD DXF import failed"));
if (reply.schema !== OPENSCAD_IMPORT_DXF_SCHEMA || reply.module !== "OpenSCAD" || reply.documentId !== payload.documentId
|| typeof reply.documentHandle !== "string" || !reply.documentHandle || typeof reply.createdDocument !== "boolean"
|| !Array.isArray(reply.objectNames) || reply.objectNames.some((name) => typeof name !== "string" || !name)
|| reply.objectCount !== reply.objectNames.length || typeof reply.faceCount !== "number" || !Number.isInteger(reply.faceCount) || reply.faceCount < 0
|| reply.faceCount !== reply.objectCount || typeof reply.sourceEntry !== "string" || reply.sourceEntry.length === 0
|| reply.recomputed !== true || reply.resourcesReleased !== true) {
throw new FacadeHandlerError("native_protocol_error", "OpenSCAD DXF import returned an invalid result schema");
}
return reply as unknown as OpenScadImportDxfResult;
});
}