UNPKG

@bitbybit-dev/occt-worker

Version:

Bit By Bit Developers CAD algorithms using OpenCascade Technology kernel adapted for WebWorker

109 lines (108 loc) 4.21 kB
import { ReservedFunctions, CACHE_THRESHOLD } from "./constants"; /** * CommandHandlers provides specialized handling for reserved functions. * * These functions have custom logic that differs from the standard * cache-and-execute flow used by most OCCT operations. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export const CommandHandlers = { /** * Handles shape to mesh conversion. * Resolves the shape from cache and converts it to mesh data for rendering. */ [ReservedFunctions.SHAPE_TO_MESH]: (inputs, context) => { const resolvedInputs = context.shapeResolver.resolveShapeReferences(inputs); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = context.openCascade.shapeToMesh(resolvedInputs); return { handled: true, result }; }, /** * Handles multiple shapes to meshes conversion. * Resolves all shapes from cache and converts them to mesh data. */ [ReservedFunctions.SHAPES_TO_MESHES]: (inputs, context) => { const shapesInput = inputs.shapes; if (!shapesInput || !Array.isArray(shapesInput) || shapesInput.length === 0) { throw new Error("No shapes detected"); } const resolvedInputs = context.shapeResolver.resolveShapeReferences(inputs); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = context.openCascade.shapesToMeshes(resolvedInputs); return { handled: true, result }; }, /** * Handles single shape deletion from cache. */ [ReservedFunctions.DELETE_SHAPE]: (inputs, context) => { const shapeRef = inputs.shape; context.cacheHelper.cleanCacheForHash(shapeRef.hash); return { handled: true, result: {} }; }, /** * Handles multiple shapes deletion from cache. */ [ReservedFunctions.DELETE_SHAPES]: (inputs, context) => { const shapesInput = inputs.shapes; shapesInput.forEach(shape => context.cacheHelper.cleanCacheForHash(shape.hash)); return { handled: true, result: {} }; }, /** * Handles the start of a new run. * Cleans cache if threshold is exceeded. */ [ReservedFunctions.STARTED_THE_RUN]: (_inputs, context) => { if (Object.keys(context.cacheHelper.usedHashes).length > CACHE_THRESHOLD) { context.cacheHelper.cleanAllCache(); } return { handled: true, result: {} }; }, /** * Handles full cache cleanup. */ [ReservedFunctions.CLEAN_ALL_CACHE]: (_inputs, context) => { context.cacheHelper.cleanAllCache(); return { handled: true, result: {} }; }, /** * Handles adding OpenCascade dependencies/plugins. * Dependencies are stored and added to plugins when available. */ [ReservedFunctions.ADD_OC]: (inputs, context) => { var _a; if ((_a = context.openCascade) === null || _a === void 0 ? void 0 : _a.plugins) { Object.keys(inputs).forEach(key => { context.openCascade.plugins.dependencies[key] = inputs[key]; }); } else { Object.keys(inputs).forEach(key => { context.addPendingDependency(key, inputs[key]); }); } return { handled: true, result: undefined }; }, /** * Handles saving a shape to STEP format. * Resolves the shape from cache and exports it. */ [ReservedFunctions.SAVE_SHAPE_STEP]: (inputs, context) => { const resolvedInputs = context.shapeResolver.resolveShapeReferences(inputs); // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = context.openCascade.io.saveShapeSTEP(resolvedInputs); return { handled: true, result }; }, }; /** * Gets the command handler for a given function name. * Returns undefined if no special handler exists for the function. */ export function getCommandHandler(functionName) { return CommandHandlers[functionName]; } /** * Checks if a function name has a special command handler. */ export function hasCommandHandler(functionName) { return functionName in CommandHandlers; }