UNPKG

@bitbybit-dev/occt-worker

Version:

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

108 lines (107 loc) 4.67 kB
import { CacheHelper } from "./cache-helper"; /** * ShapeResolver handles the recursive resolution of shape and document references in input objects. * * When shapes or documents are passed from the main thread to the worker, they are serialized as * reference objects containing a hash. This class recursively traverses any * data structure to find and replace these references with the actual cached objects. * * This solves the limitation of only resolving shapes at the top level of inputs, * allowing shapes and documents to be nested at any depth within the input structure. */ export declare class ShapeResolver { private readonly cacheHelper; constructor(cacheHelper: CacheHelper); /** * Recursively resolves all shape and document references in the given value. * * @param value - Any value that may contain shape/document references at any nesting level * @returns The value with all references replaced by actual cached objects * @throws Error if a reference hash is not found in cache */ resolveShapeReferences<T>(value: T): T; /** * Internal recursive resolution method. * Handles all cases: primitives, shape references, document references, arrays, and objects. * * NOTE: This method is synchronous. File/Blob objects that slip through from the main thread * cannot be converted here (would require async). They should be converted on the API side * using prepareStepData() before being sent to the worker. */ private resolveRecursively; /** * Retrieves an object from the cache by its hash. * * @param hash - The hash identifier of the cached object * @param type - The type of object ("shape" or "entity") * @returns The cached object * @throws Error if the object is not found in cache */ private resolveFromCache; } /** * ResultSerializer handles the conversion of OCCT shapes and documents back to serializable references. * * When returning results from the worker, actual shape/document objects cannot be passed directly * to the main thread. Instead, they are cached and a reference is returned. * * This class provides methods to serialize various result types: * - Single shapes -> ShapeReference * - Assembly documents -> DocumentReference * - Arrays of shapes * - ObjectDefinition structures (compound shapes with associated data) * - Arbitrary nested structures containing shapes/documents * - Non-shape values (passed through unchanged) * * The serialization is **recursive**, meaning shapes and documents nested at any depth within * objects or arrays will be properly converted to references. */ export declare class ResultSerializer { private readonly cacheHelper; constructor(cacheHelper: CacheHelper); /** * Serializes a result for transmission back to the main thread. * Recursively traverses the result to find and serialize all OCCT shapes and documents. * * @param result - The result from an OCCT operation * @returns A serializable version with references instead of actual objects */ serializeResult(result: unknown): unknown; /** * Recursively serializes a value, converting OCCT objects to references. */ private serializeRecursively; /** * Type guard to check if value is an ObjectDefinition. * ObjectDefinition has compound, data, and shapes properties. */ private isObjectDefinition; /** * Serializes an ObjectDefinition structure. * Converts the compound and individual shapes to references while preserving data. */ private serializeObjectDefinition; } /** * FunctionPathResolver handles calling functions on nested objects by path. * * Instead of hardcoded path depth checks, this resolver can handle any depth * of nesting in the OpenCascade service object. * * @example * // Calling "shapes.wire.createCircleWire" * resolver.callFunction(openCascade, "shapes.wire.createCircleWire", inputs) * // Equivalent to: openCascade.shapes.wire.createCircleWire(inputs) */ export declare class FunctionPathResolver { /** * Resolves a function path and calls it with the provided inputs. * * @param root - The root object (OpenCascade service) * @param functionPath - Dot-separated path to the function (e.g., "shapes.wire.createCircleWire") * @param inputs - The inputs to pass to the function * @returns The result of calling the function * @throws Error if the path cannot be resolved or the function doesn't exist */ callFunction(root: unknown, functionPath: string, inputs: unknown): unknown; }