diff --git a/packages/shared/cadmium-api.ts b/packages/shared/cadmium-api.ts new file mode 100644 index 00000000..9ea7a4d3 --- /dev/null +++ b/packages/shared/cadmium-api.ts @@ -0,0 +1,51 @@ +import { Direction, IDType, Message, MessageResult, Mode, Plane, PlaneDescription, ProjectRename } from "cadmium"; +import { sendWasmMessage } from "./projectUtils"; + +export function projectRename(new_name: string): MessageResult { + const message: Message = { ProjectRename: { new_name } } + return sendWasmMessage(message) +} +export function workbenchRename(workbench_id: IDType, new_name: string): MessageResult { + const message: Message = { WorkbenchRename: { id: workbench_id, inner: { new_name } } } + return sendWasmMessage(message) +} +export function workbenchPointAdd(workbench_id: IDType, x: number, y: number, z: number): MessageResult { + const message: Message = { WorkbenchPointAdd: { id: workbench_id, inner: { x, y, z } } } + return sendWasmMessage(message) +} +export function workbenchPlaneAdd(workbench_id: IDType, plane: Plane, width: number, height: number): MessageResult { + const message: Message = { WorkbenchPlaneAdd: { id: workbench_id, inner: { plane, width, height } } } + return sendWasmMessage(message) +} +export function workbenchSketchAdd(workbench_id: IDType, plane_description: PlaneDescription): MessageResult { + const message: Message = { WorkbenchSketchAdd: { id: workbench_id, inner: { plane_description } } } + return sendWasmMessage(message) +} +export function workbenchPointUpdate(workbench_id: IDType, point_id: IDType, x: number, y: number, z: number): MessageResult { + const message: Message = { WorkbenchPointUpdate: { id: workbench_id, inner: { id: point_id, inner: { x, y, z } } } } + return sendWasmMessage(message) +} +export function sketchAddPoint(workbench_id: IDType, sketch_id: IDType, x: number, y: number, z: number): MessageResult { + const message: Message = { SketchAddPoint: { id: workbench_id, inner: { id: sketch_id, inner: { x, y, z } } } } + return sendWasmMessage(message) +} +export function sketchAddArc(workbench_id: IDType, sketch_id: IDType, center: IDType, radius: number, clockwise: boolean, start_angle: number, end_angle: number): MessageResult { + const message: Message = { SketchAddArc: { id: workbench_id, inner: { id: sketch_id, inner: { center, radius, clockwise, start_angle, end_angle } } } } + return sendWasmMessage(message) +} +export function sketchAddCircle(workbench_id: IDType, sketch_id: IDType, center: IDType, radius: number): MessageResult { + const message: Message = { SketchAddCircle: { id: workbench_id, inner: { id: sketch_id, inner: { center, radius } } } } + return sendWasmMessage(message) +} +export function sketchAddLine(workbench_id: IDType, sketch_id: IDType, start: IDType, end: IDType): MessageResult { + const message: Message = { SketchAddLine: { id: workbench_id, inner: { id: sketch_id, inner: { start, end } } } } + return sendWasmMessage(message) +} +export function sketchDeletePrimitive(workbench_id: IDType, sketch_id: IDType, primitive_id: IDType): MessageResult { + const message: Message = { SketchDeletePrimitive: { id: workbench_id, inner: { id: sketch_id, inner: { id: primitive_id } } } } + return sendWasmMessage(message) +} +export function solidExtrusionAdd(workbench_id: IDType, sketch_id: IDType, faces: Face[], length: number, offset: number, direction: Direction, mode: Mode): MessageResult { + const message: Message = { SolidExtrusionAdd: { id: workbench_id, inner: { sketch_id, faces, length, offset, direction, mode } } } + return sendWasmMessage(message) +} diff --git a/packages/shared/projectUtils.ts b/packages/shared/projectUtils.ts index 5f9229e8..2f412106 100644 --- a/packages/shared/projectUtils.ts +++ b/packages/shared/projectUtils.ts @@ -24,7 +24,7 @@ import type { WithTarget, WorkBench } from "./types" -import type { Realization as WasmRealization, Message, Primitive, StepData, Workbench } from "cadmium" +import type { Realization as WasmRealization, Message, Primitive, StepData, Workbench, MessageResult } from "cadmium" import { isMessage } from "./typeGuards" // import { isDevelopment } from "../+layout" @@ -54,7 +54,7 @@ export function arraysEqual(a: any[], b: any[]) { return true } -function sendWasmMessage(message: Message) { +export function sendWasmMessage(message: Message): MessageResult { let wp = get(wasmProject) log("[sendWasmMessage] sending message:", message) let result = wp.send_message(message)