circuit-json-to-gltf 0.0.139 → 0.0.141

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -178,7 +178,7 @@ interface GLTFExportOptions {
178
178
  includeCustomExtensions?: boolean;
179
179
  }
180
180
  interface CircuitTo3DOptions {
181
- /** Override folding for parallel, non-overlapping bends. Undefined follows CAD is_on_folded_board. */
181
+ /** Override folding for supported PCB bend regions. Undefined follows CAD is_on_folded_board. */
182
182
  foldPcbs?: boolean;
183
183
  pcbColor?: Color;
184
184
  boardSideColor?: Color;
package/dist/index.js CHANGED
@@ -13847,7 +13847,93 @@ function polygonsTouch(a, b) {
13847
13847
  }
13848
13848
  return false;
13849
13849
  }
13850
+ function polygonContainsPolygon(boundary, polygon3) {
13851
+ if (polygon3.some((p) => !pointInPolygon(p, boundary))) return false;
13852
+ for (let i = 0; i < polygon3.length; i++) {
13853
+ const a = polygon3[i], b = polygon3[(i + 1) % polygon3.length];
13854
+ const dx = b.x - a.x, dy = b.y - a.y;
13855
+ const lengthSquared = dx * dx + dy * dy;
13856
+ if (lengthSquared < EPS * EPS) continue;
13857
+ const crossings = [0, 1];
13858
+ for (let j = 0; j < boundary.length; j++) {
13859
+ const c = boundary[j], d = boundary[(j + 1) % boundary.length];
13860
+ const ex = d.x - c.x, ey = d.y - c.y;
13861
+ const denominator = dx * ey - dy * ex;
13862
+ const cx = c.x - a.x, cy = c.y - a.y;
13863
+ if (Math.abs(denominator) < EPS) {
13864
+ if (Math.abs(cx * dy - cy * dx) > EPS) continue;
13865
+ for (const p of [c, d]) {
13866
+ const t = ((p.x - a.x) * dx + (p.y - a.y) * dy) / lengthSquared;
13867
+ if (t > 0 && t < 1) crossings.push(t);
13868
+ }
13869
+ } else {
13870
+ const t = (cx * ey - cy * ex) / denominator;
13871
+ const u = (cx * dy - cy * dx) / denominator;
13872
+ if (t > 0 && t < 1 && u >= -EPS && u <= 1 + EPS) crossings.push(t);
13873
+ }
13874
+ }
13875
+ crossings.sort((a2, b2) => a2 - b2);
13876
+ for (let j = 1; j < crossings.length; j++) {
13877
+ const t = (crossings[j - 1] + crossings[j]) / 2;
13878
+ if (!pointInPolygon({ x: a.x + t * dx, y: a.y + t * dy }, boundary))
13879
+ return false;
13880
+ }
13881
+ }
13882
+ return true;
13883
+ }
13850
13884
  var EPS2 = 1e-7;
13885
+ function orderPcbBends(bends) {
13886
+ const sorted = [...bends].sort(
13887
+ (a, b) => a.start - b.start || a.nx - b.nx || a.ny - b.ny || a.id.localeCompare(b.id)
13888
+ );
13889
+ const parents = /* @__PURE__ */ new Map();
13890
+ const overlap = (bend) => {
13891
+ throw new PcbFoldError({
13892
+ code: "overlapping_bend_zones",
13893
+ message: "Overlapping PCB bend zones or incompatible moving regions are not supported",
13894
+ bendId: bend.id
13895
+ });
13896
+ };
13897
+ for (let i = 0; i < sorted.length; i++)
13898
+ for (let j = i + 1; j < sorted.length; j++) {
13899
+ const a = sorted[i], b = sorted[j];
13900
+ if (a.movingOutline && b.movingOutline) {
13901
+ if (!polygonsTouch(a.movingOutline, b.movingOutline)) continue;
13902
+ const aContainsB = polygonContainsPolygon(
13903
+ a.movingOutline,
13904
+ b.movingOutline
13905
+ );
13906
+ const bContainsA = polygonContainsPolygon(
13907
+ b.movingOutline,
13908
+ a.movingOutline
13909
+ );
13910
+ if (aContainsB === bContainsA) overlap(b);
13911
+ const parent = aContainsB ? a : b, child = aContainsB ? b : a;
13912
+ if (child.movingOutline.some(
13913
+ (p) => p.x * parent.nx + p.y * parent.ny < parent.end - EPS2
13914
+ ))
13915
+ overlap(child);
13916
+ parents.set(child, [...parents.get(child) ?? [], parent]);
13917
+ } else {
13918
+ if (Math.abs(a.nx - b.nx) > EPS2 || Math.abs(a.ny - b.ny) > EPS2)
13919
+ throw new PcbFoldError({
13920
+ code: "nonparallel_bends",
13921
+ message: "Nonparallel PCB bends or bends with opposite moving directions require a board outline to resolve their moving regions"
13922
+ });
13923
+ if (b.start < a.end - EPS2) overlap(b);
13924
+ parents.set(b, [...parents.get(b) ?? [], a]);
13925
+ }
13926
+ }
13927
+ const ordered = [], visited = /* @__PURE__ */ new Set();
13928
+ const visit = (bend) => {
13929
+ if (visited.has(bend)) return;
13930
+ for (const parent of parents.get(bend) ?? []) visit(parent);
13931
+ visited.add(bend);
13932
+ ordered.push(bend);
13933
+ };
13934
+ for (const bend of sorted) visit(bend);
13935
+ return ordered;
13936
+ }
13851
13937
  function createPcbFold(records, thickness, { outline } = {}) {
13852
13938
  if (!Number.isFinite(thickness) || thickness < 0)
13853
13939
  throw new Error("Board thickness must be finite and nonnegative");
@@ -13908,27 +13994,8 @@ function createPcbFold(records, thickness, { outline } = {}) {
13908
13994
  });
13909
13995
  return geometry;
13910
13996
  }).filter((b) => Math.abs(b.angle) > EPS2);
13911
- const first = bends[0];
13912
- if (first && bends.some(
13913
- (b) => Math.abs(b.nx - first.nx) > EPS2 || Math.abs(b.ny - first.ny) > EPS2
13914
- )) {
13915
- throw new PcbFoldError({
13916
- code: "nonparallel_bends",
13917
- message: "Folded PCB rendering currently requires parallel bends with the same moving direction"
13918
- });
13919
- }
13920
- bends.sort((a, b) => a.start - b.start);
13921
- for (let i = 0; i < bends.length; i++)
13922
- for (let j = i + 1; j < bends.length; j++) {
13923
- const a = bends[i], b = bends[j];
13924
- if (b.start < a.end - EPS2 && (!a.movingOutline || !b.movingOutline || polygonsTouch(a.movingOutline, b.movingOutline)))
13925
- throw new PcbFoldError({
13926
- code: "overlapping_bend_zones",
13927
- message: "Overlapping PCB bend zones are not supported",
13928
- bendId: b.id
13929
- });
13930
- }
13931
- const distalFirst = [...bends].reverse();
13997
+ const proximalFirst = orderPcbBends(bends);
13998
+ const distalFirst = [...proximalFirst].reverse();
13932
13999
  const transform = ({
13933
14000
  p,
13934
14001
  anchor,
@@ -13977,7 +14044,7 @@ function createPcbFold(records, thickness, { outline } = {}) {
13977
14044
  return { x: dot2(axes2[0]), y: dot2(axes2[1]), z: dot2(axes2[2]) };
13978
14045
  };
13979
14046
  return {
13980
- bends,
14047
+ bends: proximalFirst,
13981
14048
  inversePoint: (p, anchor) => inverse({ p, anchor, direction: false }),
13982
14049
  inverseDirection: (p, anchor) => inverse({ p, anchor, direction: true }),
13983
14050
  point: (p, anchor = p) => transform({ p, anchor, direction: false }),
@@ -17755,6 +17822,7 @@ var DEFAULT_GREEN_SOLDER_MASK = {
17755
17822
  backgroundColor: "#0f4f30",
17756
17823
  solderMaskWithCopperColor: "#17613b"
17757
17824
  };
17825
+ var DEFAULT_FLEX_AMBER = "#cc9c33";
17758
17826
  var BOARD_COLOR_PRESETS = {
17759
17827
  green: DEFAULT_GREEN_SOLDER_MASK.backgroundColor,
17760
17828
  red: "#8b1e1e",
@@ -17803,12 +17871,12 @@ var parseColor = (value) => {
17803
17871
  if (resolved.startsWith("#")) return parseHexColor(resolved);
17804
17872
  return parseRgbColor(resolved);
17805
17873
  };
17874
+ var getSpecifiedColor = (value) => !value?.trim() || value.trim().toLowerCase() === "not_specified" ? void 0 : value;
17806
17875
  var normalizeColor = (value) => {
17807
- if (!value || value.trim().toLowerCase() === "not_specified") {
17808
- return void 0;
17809
- }
17810
- const parsed = parseColor(value);
17811
- return parsed ? toHex(parsed) : value.trim();
17876
+ const specified = getSpecifiedColor(value);
17877
+ if (!specified) return void 0;
17878
+ const parsed = parseColor(specified);
17879
+ return parsed ? toHex(parsed) : specified.trim();
17812
17880
  };
17813
17881
  var mix = (color, target, amount) => color.map(
17814
17882
  (channel, index) => clampByte(channel + (target[index] - channel) * amount)
@@ -17851,9 +17919,15 @@ function getBoardColorPalette(circuitJson, overrides = {}) {
17851
17919
  (element) => element.type === "pcb_board"
17852
17920
  );
17853
17921
  const legacyBoard = board;
17854
- const solderMaskColor = overrides.solderMaskColor ?? board?.solder_mask_color ?? legacyBoard?.soldermask_color;
17922
+ const solderMaskColor = getSpecifiedColor(overrides.solderMaskColor) ?? normalizeColor(board?.solder_mask_color) ?? normalizeColor(legacyBoard?.soldermask_color);
17855
17923
  const silkscreenColor = overrides.silkscreenColor ?? board?.silkscreen_color;
17856
17924
  if (!solderMaskColor) {
17925
+ if (board?.material === "flex") {
17926
+ return {
17927
+ ...deriveBoardColorPalette(DEFAULT_FLEX_AMBER, silkscreenColor),
17928
+ boardSideColor: DEFAULT_FLEX_AMBER
17929
+ };
17930
+ }
17857
17931
  return { silkscreenColor: normalizeColor(silkscreenColor) };
17858
17932
  }
17859
17933
  return deriveBoardColorPalette(solderMaskColor, silkscreenColor);
@@ -17862,8 +17936,9 @@ function getBoardColorPalette(circuitJson, overrides = {}) {
17862
17936
  // lib/converters/board-renderer.ts
17863
17937
  import { convertCircuitJsonToPcbSvg } from "circuit-to-svg";
17864
17938
  async function renderBoardLayer(circuitJson, options) {
17939
+ const backgroundOverride = getSpecifiedColor(options.backgroundColor);
17865
17940
  const palette = getBoardColorPalette(circuitJson, {
17866
- solderMaskColor: options.backgroundColor,
17941
+ solderMaskColor: backgroundOverride,
17867
17942
  silkscreenColor: options.silkscreenColor
17868
17943
  });
17869
17944
  const {
@@ -17873,7 +17948,7 @@ async function renderBoardLayer(circuitJson, options) {
17873
17948
  drillColor = "rgba(0,0,0,0.5)",
17874
17949
  showPcbNotes = false
17875
17950
  } = options;
17876
- const backgroundColor = options.backgroundColor ?? palette.backgroundColor ?? DEFAULT_GREEN_SOLDER_MASK.backgroundColor;
17951
+ const backgroundColor = backgroundOverride ?? palette.backgroundColor ?? DEFAULT_GREEN_SOLDER_MASK.backgroundColor;
17877
17952
  const silkscreenColor = options.silkscreenColor ?? palette.silkscreenColor ?? "#ffffff";
17878
17953
  const solderMaskWithCopperColor = options.solderMaskWithCopperColor ?? palette.solderMaskWithCopperColor ?? DEFAULT_GREEN_SOLDER_MASK.solderMaskWithCopperColor;
17879
17954
  const svg = convertCircuitJsonToPcbSvg(circuitJson, {
@@ -17968,11 +18043,12 @@ async function renderBoardTextures(circuitJson, {
17968
18043
  drillColor,
17969
18044
  showPcbNotes = false
17970
18045
  }) {
18046
+ const backgroundOverride = getSpecifiedColor(backgroundColor);
17971
18047
  const palette = getBoardColorPalette(circuitJson, {
17972
- solderMaskColor: backgroundColor,
18048
+ solderMaskColor: backgroundOverride,
17973
18049
  silkscreenColor
17974
18050
  });
17975
- const resolvedBackgroundColor = backgroundColor ?? palette.backgroundColor ?? DEFAULT_GREEN_SOLDER_MASK.backgroundColor;
18051
+ const resolvedBackgroundColor = backgroundOverride ?? palette.backgroundColor ?? DEFAULT_GREEN_SOLDER_MASK.backgroundColor;
17976
18052
  const resolvedSilkscreenColor = silkscreenColor ?? palette.silkscreenColor ?? "#ffffff";
17977
18053
  const resolvedSolderMaskWithCopperColor = solderMaskWithCopperColor ?? palette.solderMaskWithCopperColor ?? DEFAULT_GREEN_SOLDER_MASK.solderMaskWithCopperColor;
17978
18054
  const top = await renderBoardLayer(circuitJson, {
@@ -18019,7 +18095,6 @@ function convertCadSizeToSceneSize(size) {
18019
18095
  }
18020
18096
  async function convertCircuitJsonTo3D(inputCircuitJson, options = {}) {
18021
18097
  const {
18022
- pcbColor = "rgba(0,140,0,0.8)",
18023
18098
  boardSideColor,
18024
18099
  componentColor = "rgba(128,128,128,0.5)",
18025
18100
  copperColor = "#C87B4B",
@@ -18047,11 +18122,12 @@ async function convertCircuitJsonTo3D(inputCircuitJson, options = {}) {
18047
18122
  );
18048
18123
  const db = cju(circuitJson);
18049
18124
  const boxes = [];
18125
+ const pcbColorOverride = typeof options.pcbColor === "string" ? getSpecifiedColor(options.pcbColor) : options.pcbColor;
18050
18126
  const palette = getBoardColorPalette(circuitJson, {
18051
- solderMaskColor: options.pcbColor !== void 0 ? colorToCssString(options.pcbColor) : void 0,
18127
+ solderMaskColor: pcbColorOverride !== void 0 ? colorToCssString(pcbColorOverride) : void 0,
18052
18128
  silkscreenColor
18053
18129
  });
18054
- const resolvedPcbColor = options.pcbColor ?? palette.backgroundColor ?? pcbColor;
18130
+ const resolvedPcbColor = pcbColorOverride ?? palette.backgroundColor ?? "rgba(0,140,0,0.8)";
18055
18131
  const resolvedBoardSideColor = boardSideColor ?? palette.boardSideColor;
18056
18132
  const boardTextureColors = {
18057
18133
  backgroundColor: palette.backgroundColor,
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "circuit-json-to-gltf",
3
3
  "main": "dist/index.js",
4
4
  "type": "module",
5
- "version": "0.0.139",
5
+ "version": "0.0.141",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/tscircuit/circuit-json-to-gltf.git"
@@ -30,7 +30,7 @@
30
30
  "occt-import-js": "^0.0.23"
31
31
  },
32
32
  "devDependencies": {
33
- "@tscircuit/flex-utils": "https://jscdn.tscircuit.com/@tscircuit/flex-utils/0.0.7.tgz",
33
+ "@tscircuit/flex-utils": "https://jscdn.tscircuit.com/@tscircuit/flex-utils/0.0.8.tgz",
34
34
  "@biomejs/biome": "^2.1.4",
35
35
  "@google/model-viewer": "^4.1.0",
36
36
  "@napi-rs/canvas": "^1.0.9",