classcad
npm install @buerli.io/classcad
This library connects Buerli with ClassCAD, the CAD backend. Buerli/classcad in particular functions as a bridge between the Javascript client and ClassCAD, allowing you to pick specific interfaces (SocketIO, WASM, ...).
Note: In order to observe Javascript conventions the ClassCAD API is unwrapped in Buerli. Where normally each function would return an object with a
result
property, and amessages
property for errors, all ClassCAD API functions in Buerli return the result directly and throw errors as exceptions that can be caught with try/catch.
init
Initializes Buerli with the given factory and options. This function is typically called once at the start of your application to set up the Buerli environment. It tells Buerli where ClassCAD is running and how to connect to it.
Declaration
(source: function, options?: PARTIAL<{
theme: {
primary: string,
secondary: string,
dark: string,
highlightedGeom: string,
hoveredGeom: string,
},
config: {
geometry: {
disabled?: boolean,
meshes?: ElementConfig & {
wireframe?: boolean,
depthWrite?: boolean,
},
edges?: ElementConfig,
points?: ElementConfig,
},
},
}>): null
type ElementConfig = {
hidden?: boolean,
color?: any,
opacity?: number,
};
Params
Name | Type | Default | Description |
---|---|---|---|
source | function | Factory function (WASM, SOCKETIO) | |
options | object | Optional configuration options for Buerli |
Returns null
Example
import { init, WASMClient } from '@buerli.io/classcad'
// Get your key from https://www.classcad.ch/user
const appKey = '...'
init((drawingID) => new WASMClient(drawingID, { appKey }))
WASMClient
Initializes ClassCAD WASM, meaning ClassCAD will run in the browser using WebAssembly.
Declaration
(drawingID: number, options: object): object
Params
Name | Type | Default | Description |
---|---|---|---|
drawingID | number | The drawingID you receive from init() | |
options | object | Configuration |
Returns object
Example
import { init, WASMClient } from '@buerli.io/classcad'
// Get your key from https://www.classcad.ch/user
const appKey = '...'
init((drawingID) => new WASMClient(drawingID, { appKey }))
SocketIOClient
Initializes the ClassCAD SocketIO, meaning ClassCAD will run on a server and communicate with your application via Socket.IO.
Declaration
(url: string, drawingID: number): object
Params
Name | Type | Default | Description |
---|---|---|---|
url | string | URL | |
drawingID | number | The drawingID you receive from init() |
Returns object
Example
import { init, SocketIOClient } from '@buerli.io/classcad'
// Run your ClassCAD via Docker, or natively
init((drawingID) => new SocketIOClient('https://127.0.0.1/9091'))
ClassCAD
This class manages a ClassCAD connection. It allows you to connect to a live drawing and carry out operations on it. You can use this class to create, modify, and query geometries in ClassCAD.
Example
import { ClassCAD } from '@buerli.io/classcad'
const drawing = new ClassCAD()
await drawing.connect()
const api = drawing.api.v1
// Create a part
const part = await api.part.create({ name: 'Flange' })
// Create a parametric box in it
const box = await api.part.box({ id: part })
Properties
.api
The API object that provides methods to interact with the ClassCAD API.
Note: The ClassCAD API fundamentally is is based on server round-trips and must function the same between REST, SocketIO, WASM, and other interfaces and languages. This means that the API is asynchronous. All functions typically return a promise that yields an object with a
result
property, and amessages
property, which contains errors and warnings. The raw javascript ClassCAD API would be something like this:
const { result: part, messages } = await api.part.create({ name: 'Flange' })
if (messages.length > 0) {
throw new Error(messages)
}
const { result: box, messages } = await api.part.box({ id: part })
if (messages.length > 0) {
throw new Error(messages)
}
Note: With the
.api
property of this class the API has been unwrapped for a more conventional usage in Javascript. This means that all API functions yield the result directly and throw errors as exceptions that can be caught with try/catch. Warnings will be logged to the console.
try {
const part = await api.part.create({ name: 'Flange' })
const box = await api.part.box({ id: part })
} catch (error) {
console.error('An error occured:', error)
}
.drawingId
The unique identifier (a number) for the drawing session.
Functions
.connect(drawingName?: string)
Creates a new drawing and its Socket client and connects it to the server. It will connect to the URL provided in the init
function that has to be called beforehand.
Returns Promise<DrawingID | null>
.disconnect()
Disconnects the drawing from ClassCAD and resets the drawing.
Returns void
.undo(state?: string)
Undo the last operation or if state isn't empty, load the state with name = state.
Returns void
.redo(state?: string)
Redo the last undo or if state isn't empty, load the state with name = state.
Returns void
.createBufferGeometry(objectId?: ObjectID|ObjectID[])
Creates a THREEJS buffer geometry of any product (part or assembly).
Returns Promise<BufferGeometry[] | undefined>
.createScene(objectId?: ObjectID|ObjectID[], options?: { meshPerGeometry?: boolean; structureOnly?: boolean })
Creates a THREEJS scene of any product (part or assembly).
Returns Promise<{ scene: Scene; nodes: { [key: string]: Object3D }; materials: { [key: string]: Material } }>
.createThreeShape(owner: ObjectID, shape: THREE.Shape)
Creates a curve from THREEJS shapes.
Returns Promise<object>
.createPolyline(owner: ObjectID, fPts: { point: THREE.Vector3; radius: number }[], close = true)
Creates a Polyline2D.
Returns Promise<object>
.selectGeometry(types: GrT[], count?: number)
Halts execution until a specific geometry is selected by the user.
Returns Promise<InteractionInfo[]>
compression
A collection of helpers for compressing and decompressing data, particularly useful for handling large datasets or binary data in a more efficient manner. You would typically use this whenever buerli needs to load or import binary data, such as when importing a STEP file or other large files.
.encodeToBase64(data: ArrayBuffer | Uint8Array)
Encodes binary data into a Base64 string. This is useful for transmitting binary data over text-based protocols or storing it in text formats.
Example
const part = await api.part.create({ name: 'Part' })
const buffer = await fetch(fileURL).then(res => res.arrayBuffer())
const data = compression.encodeToBase64(buffer)
const step = await api.part.importFeature({ id: part, data, format: 'stp', encoding: 'base64' })
.decodeFromBase64(base64: string)
Decodes a Base64 string back into binary data.