@bitbybit-dev/occt-worker
Version:
Bit By Bit Developers CAD algorithms using OpenCascade Technology kernel adapted for WebWorker
106 lines (105 loc) • 3.54 kB
JavaScript
/**
* Constants for OCCT worker communication and shape handling.
* These constants define the protocol between the main thread and the worker.
*/
/**
* Shape reference type identifier used in serialized shape objects.
* When shapes are passed between main thread and worker, they are serialized
* as objects with this type identifier and a hash reference.
*/
export const SHAPE_TYPE_IDENTIFIER = "occ-shape";
/**
* Entity reference type identifier used in serialized OCCT objects that are not shapes.
* This includes assembly documents, handles, and other OCCT objects.
* When these objects are passed between main thread and worker, they are serialized
* as objects with this type identifier and a hash reference.
*/
export const ENTITY_TYPE_IDENTIFIER = "occ-entity";
/**
* Worker lifecycle and status messages.
*/
export const WorkerMessages = {
/** Sent when the worker has completed initialization */
INITIALIZED: "occ-initialised",
/** Sent when the worker is processing a request */
BUSY: "busy",
};
/**
* Reserved function names that have special handling in the worker.
* These functions bypass the standard caching mechanism and have custom logic.
*/
export const ReservedFunctions = {
/** Convert a single shape to mesh data for rendering */
SHAPE_TO_MESH: "shapeToMesh",
/** Convert multiple shapes to mesh data for rendering */
SHAPES_TO_MESHES: "shapesToMeshes",
/** Delete a single shape from cache */
DELETE_SHAPE: "deleteShape",
/** Delete multiple shapes from cache */
DELETE_SHAPES: "deleteShapes",
/** Signal that a new run has started (used for cache cleanup) */
STARTED_THE_RUN: "startedTheRun",
/** Clean all cached shapes */
CLEAN_ALL_CACHE: "cleanAllCache",
/** Add OpenCascade dependencies/plugins */
ADD_OC: "addOc",
/** Save shape to STEP file format */
SAVE_SHAPE_STEP: "saveShapeSTEP",
};
/**
* Set of function names that should not go through the standard caching flow.
* These functions either handle their own caching or don't need caching.
*/
export const NON_CACHEABLE_FUNCTIONS = new Set([
ReservedFunctions.SHAPE_TO_MESH,
ReservedFunctions.SHAPES_TO_MESHES,
ReservedFunctions.DELETE_SHAPE,
ReservedFunctions.DELETE_SHAPES,
ReservedFunctions.STARTED_THE_RUN,
ReservedFunctions.CLEAN_ALL_CACHE,
ReservedFunctions.ADD_OC,
ReservedFunctions.SAVE_SHAPE_STEP,
]);
/**
* Maximum number of cached hashes before triggering a full cache cleanup.
* This prevents memory issues from accumulating too many cached shapes.
*/
export const CACHE_THRESHOLD = 10000;
/**
* Type guard to check if a value is a ShapeReference.
*/
export function isShapeReference(value) {
return (value !== null &&
typeof value === "object" &&
"type" in value &&
"hash" in value &&
value.type === SHAPE_TYPE_IDENTIFIER);
}
/**
* Creates a shape reference object for serialization.
*/
export function createShapeReference(hash) {
return {
type: SHAPE_TYPE_IDENTIFIER,
hash,
};
}
/**
* Type guard to check if a value is an EntityReference.
*/
export function isEntityReference(value) {
return (value !== null &&
typeof value === "object" &&
"type" in value &&
"hash" in value &&
value.type === ENTITY_TYPE_IDENTIFIER);
}
/**
* Creates an entity reference object for serialization.
*/
export function createEntityReference(hash) {
return {
type: ENTITY_TYPE_IDENTIFIER,
hash,
};
}