Exchange formats
File contents and reports from code
These functions work on caller-supplied data. They do not open files, download results or save a document. Keep the report with the result; a report object is not the file content.
| Function | Result and units | Failure boundary |
|---|---|---|
exportIGES(model, ids) | { iges, report }: iges is file text; the report declares mm, scale 1 and non-solid geometry | Requires a version-0 millimetre model with 1–16 objects and 1–16 distinct selected IDs; invalid model or selection is refused |
importIGES(text, options) | { request, source, model, metadata, entities, report }: native model/source data and import provenance, not a B-rep solid | Unsupported input throws IGESImportError with a diagnostic code; it is not a successful report with missing geometry |
writeDxf(drawing, options) | { text, bytes, report }: bytes is Uint8Array in Windows-1252; extents and chord distances use the drawing's units | Throws a typed refusal; use isDxfWriteError(error) to identify its code |
The bounded IGES importer requires sourceId, modelName, idPrefix, maximumEntities: 16 and maximumRecords: 8192. It accepts its supported millimetre profile without unit conversion. Its returned source is native model source, not the original IGES file text; request.iges retains that input text.
For DXF, write the returned bytes when you need the writer's file encoding. Encoding text as UTF-8 can produce different bytes for non-ASCII characters. In R12, report.r12 records conversions and whether units were omitted; the chord tolerance uses drawing units, not a fixed millimetre value.
import { writeDxf, isDxfWriteError, type DxfDocument } from '@barocss/cad';
// The caller supplies drawing data and handles file I/O.
export function prepareDxf(drawing: DxfDocument) {
try {
const result = writeDxf(drawing, { version: 'AC1015' });
return { bytes: result.bytes, report: result.report };
} catch (error: unknown) {
if (isDxfWriteError(error)) return { error: error.code };
throw error;
}
}STEP
Writing. An output's
exportStep(options), orexportStep(brep | breps, options), writes the exact B-rep as STEP AP214: planes, B-spline surfaces and curves. Its options:name: the product's name.timestamp: the header's time; the default is now.description: the header'sFILE_DESCRIPTIONentries. The default is'BaroCSS CAD part'; the studio adds its source revision.validationProperties: CAx-IF geometric validation properties (volume, area, centroid and bounding box), written by default so a reader can check what it imported.meshes, for mesh bodies:tessellated: AP242 triangulated faces, compact;planar: the triangles as planar faces, readable by any STEP reader, but large;auto(the default): planar up to 5,000 triangles a body, tessellated above.
Planar faces past 250,000 triangles a body are refused.
Reading.
importStep(text), or theimportfeature in a part, reads B-rep solids:- surfaces: planes, cylinders, cones, spheres and tori as exact rational patches, B-spline surfaces as they are, and surfaces of linear extrusion and of revolution;
- offset surfaces: one level of
OFFSET_SURFACEover directPLANE,CYLINDRICAL_SURFACE,SPHERICAL_SURFACE,CONICAL_SURFACE(including exact-zero identity and nonzero cone offsets away from apex collapse/crossing),TOROIDAL_SURFACE(away from collapse and spindle pinch), andB_SPLINE_SURFACE*bases. Analytic offsets certify boundary curves explicitly; NURBS offsets check curvature fold bounds and disclose fitted deviation inimportStepWithReport(...).offsetApproximations; - curves and periodic bounds: lines, circles and ellipses as rational arcs, B-spline curves (including exact domain splits and
TRIMMED_CURVEbounds),VERTEX_LOOPpole/apex bounds on singular carriers, seam-crossing holes, and once-round swept/revolution/periodic carriers.
A face that goes once round its axis is cut into seamless halves. Other offset bases, nested offsets, binary64 cone-offset underflows, pinched/collapsed offsets, non-singular
VERTEX_LOOPpinholes and separated boundary curves are explicitly refused by name (GEOMETRYfor proved boundary separation,CAPABILITY/INPUTotherwise).stepMeshes(text)reads a file of AP242 tessellated meshes only.Multi-level STEP assemblies. STEP import and export preserve multi-level assembly definitions, shared part instances, and rigid placement transforms across round-trips rather than flattening every occurrence into unlinked bodies.
Zero-RegExp Part 21 parsing. STEP real/point formatting and Part 21 token parsing run on a dedicated scanner with zero RegExp backtracking overhead for large assemblies.
const [body] = evaluateCadPart(plate).outputs;
const step = exportStep(body, { name: 'plate' }); // exact B-rep, AP214
const back = evaluateCadPart({ format: 'barocss-cad-part', version: 1, units: 'mm', parameters: {},
features: [{ id: 'i', kind: 'import', step }] }); // a STEP file as a feature
const [read] = back.outputs;
assert.ok(Math.abs(massPropertiesOf(read, 2.7).volume - massPropertiesOf(body, 2.7).volume) < 1e-6);
assert.equal(checkOutput(read).valid, true);With each: true, the import feature makes each solid in the file its own part (id/k), in file order.
IGES
exportIGES writes curves and surfaces as rational B-spline entities (126 and 128). importIGES reads the same profile: untransformed rational B-spline curves and surfaces in IGES 5.3. It gives typed diagnostics for anything else.
In the studio, IGES is for curves and surfaces you edit directly, not solids. Native IGES exchange is verified end-to-end across @barocss/cad, @barocss/cad-model, and @barocss/viewer with strict bounds validation and refusal diagnostics. See Files and exports.
STL
- Writing. An output's
exportStl({ segmentsPerQuarter })writes its display tessellation as binary STL, in millimetres.encodeBinaryStl(positions, indices)writes any triangle mesh. - Reading.
readStl(bytes)reads binary or ASCII STL triangles, andreadObjreads OBJ. Themeshfeature takes triangles as a faceted body.
STL carries triangles only: no units, colours, names or exact curves.
- Unit scaling and malformed refusal. Binary and ASCII STL readers support explicit unit scaling (
mm,cm,m,in) and refuse truncated buffers, non-finite coordinates, or malformed headers with typed diagnostics instead of silently dropping triangles. - Approximation disclosure. Exporting a curved B-rep to STL approximates exact analytic/NURBS faces as triangles; Studio surfaces triangle count and chordal approximation metadata alongside the export.
Omitting segmentsPerQuarter from exportStl uses 16. This dimensionless tessellation setting is not a millimetre tolerance or a guarantee of geometric error. It does not change the on-screen mesh; see STL export choices for the studio controls.
DXF
writeDxf(document, { version }) writes a 2D drawing:
The input is a plain document of layers, line types, text styles, blocks and entities.
The versions are DXF 2000 (
AC1015, the default) and R12 (AC1009).Same document, same file. The output has no time stamps, so the same document always gives the same bytes.
R12's limits. R12 has no ellipses, splines, hatches or multi-line text:
- polylines and text are rewritten exactly;
- ellipses and splines become polylines only within a chord tolerance you give;
- anything else is refused, typed.
The report counts every such change, so nothing is approximated silently.
The document may also supply dimensionStyle with all three fields: a finite positive textHeight in the drawing's units, textPlacement (above or center) and textOrientation (aligned or horizontal). Both versions write these dimension-style defaults. Omitting the object preserves the existing defaults and bytes. Invalid values throw a typed DXF write error. The fields do not rebuild anonymous dimension display blocks or guarantee an external editor's regenerated layout.
const sheet = { units: 'mm', entities: [
{ type: 'line', a: [0, 0], b: [60, 0] }, { type: 'line', a: [60, 0], b: [60, 40] },
{ type: 'line', a: [60, 40], b: [0, 40] }, { type: 'line', a: [0, 40], b: [0, 0] },
{ type: 'circle', center: [30, 20], radius: 4 },
] };
const a = writeDxf(sheet), b = writeDxf(sheet); // DXF 2000 (AC1015) by default
assert.equal(a.text, b.text); // no time stamps: the same document, the same file
assert.deepEqual(a.report.modelSpace, { LINE: 4, CIRCLE: 1 });
const r12 = writeDxf(sheet, { version: 'AC1009' }); // R12
assert.match(r12.text, /AC1009/);Sheet metal's flat pattern has its own DXF writer, flatPatternDxf. Reading a DXF sketch is the document model's job: see @barocss/cad-model.