Skip to content

SVG Import

sketch.svg parses SVG paths, lines, circles, rectangles, polylines, and polygons into normal sketch entities.

ts
import { readFileSync } from "node:fs";
import { defineModel, length, sketch } from "onshape-cadscript";

const badge = readFileSync(new URL("./badge.svg", import.meta.url), "utf8");

export default defineModel({
  name: "svg-keychain",
  units: "mm",
  parameters: {},
  async *build(cad) {
    const profile = yield* cad.sketch({
      id: "badge-profile",
      plane: cad.top,
      entities: [sketch.svg("badge", badge, { translate: [-25, 12] })],
    });
    yield* cad.extrude({ id: "badge", profile, depth: length(3) });
  },
});

The SVG Y axis is flipped into CAD's Y-up coordinate system. Apply scale and translate explicitly. Convert quadratic curves and elliptical arcs to cubic paths before import in v0.1.

Because import returns the shared sketch AST, local preview and Onshape receive the same normalized geometry.

Unofficial community project. Not affiliated with Onshape or PTC.