jscad-electronics 0.0.35 → 0.0.37

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/vanilla.js CHANGED
@@ -1478,7 +1478,6 @@ var VSSOP = ({
1478
1478
  // lib/Footprinter3d.tsx
1479
1479
  var Footprinter3d = ({ footprint }) => {
1480
1480
  const fpJson = fp.string(footprint).json();
1481
- console.log(footprint, fpJson);
1482
1481
  switch (fpJson.fn) {
1483
1482
  case "dip":
1484
1483
  return /* @__PURE__ */ jsx(Dip, { numPins: fpJson.num_pins, pitch: fpJson.p, bodyWidth: fpJson.w });
@@ -1638,17 +1637,17 @@ function renderNode(node, colorCtx) {
1638
1637
  if (!isVNode(node)) return [];
1639
1638
  const { type, props, children } = node;
1640
1639
  if (type === Fragment) {
1641
- return children.flatMap((c) => renderNode(c, colorCtx));
1640
+ return (children ?? []).flatMap((c) => renderNode(c, colorCtx));
1642
1641
  }
1643
1642
  if (type === Colorize) {
1644
1643
  const newColor = props?.color;
1645
- return children.flatMap((c) => renderNode(c, newColor ?? colorCtx));
1644
+ return (children ?? []).flatMap((c) => renderNode(c, newColor ?? colorCtx));
1646
1645
  }
1647
1646
  if (type === Translate) {
1648
1647
  const off = toVec3(
1649
1648
  props?.offset ?? { x: props?.x, y: props?.y, z: props?.z }
1650
1649
  );
1651
- const geoms = children.flatMap((c) => renderNode(c, colorCtx));
1650
+ const geoms = (children ?? []).flatMap((c) => renderNode(c, colorCtx));
1652
1651
  return geoms.map(({ geom, color }) => ({
1653
1652
  geom: transforms.translate(off, geom),
1654
1653
  color: color ?? colorCtx
@@ -1664,7 +1663,7 @@ function renderNode(node, colorCtx) {
1664
1663
  degToRad(props?.y ?? 0),
1665
1664
  degToRad(props?.z ?? 0)
1666
1665
  ];
1667
- const geoms = children.flatMap((c) => renderNode(c, colorCtx));
1666
+ const geoms = (children ?? []).flatMap((c) => renderNode(c, colorCtx));
1668
1667
  return geoms.map(({ geom, color }) => ({
1669
1668
  geom: transforms.rotateZ(
1670
1669
  rot[2],
@@ -1674,7 +1673,7 @@ function renderNode(node, colorCtx) {
1674
1673
  }));
1675
1674
  }
1676
1675
  if (type === Union || type === Subtract || type === Hull) {
1677
- const geoms = children.flatMap((c) => renderNode(c, colorCtx)).map((g) => g.geom);
1676
+ const geoms = (children ?? []).flatMap((c) => renderNode(c, colorCtx)).map((g) => g.geom);
1678
1677
  if (geoms.length === 0) return [];
1679
1678
  let geom;
1680
1679
  if (type === Union) geom = booleans.union(geoms);
@@ -1689,7 +1688,7 @@ function renderNode(node, colorCtx) {
1689
1688
  return [{ geom: g2, color: colorCtx ?? props?.color }];
1690
1689
  }
1691
1690
  if (type === ExtrudeLinear) {
1692
- const geoms2 = children.flatMap((c) => renderNode(c, colorCtx)).map((g) => g.geom);
1691
+ const geoms2 = (children ?? []).flatMap((c) => renderNode(c, colorCtx)).map((g) => g.geom);
1693
1692
  if (geoms2.length === 0) return [];
1694
1693
  const base2 = geoms2.length > 1 ? booleans.union(geoms2) : geoms2[0];
1695
1694
  const height10 = props?.height ?? props?.h ?? 1;
@@ -1729,19 +1728,117 @@ function renderNode(node, colorCtx) {
1729
1728
  const out = type(props ?? {});
1730
1729
  return renderNode(out, colorCtx);
1731
1730
  }
1732
- return children.flatMap((c) => renderNode(c, colorCtx));
1731
+ return (children ?? []).flatMap((c) => renderNode(c, colorCtx));
1733
1732
  }
1734
1733
  function render(root) {
1735
1734
  const geometries2 = renderNode(root);
1736
1735
  return { geometries: geometries2 };
1737
1736
  }
1738
1737
 
1738
+ // lib/vanilla/convertCSGToThreeGeom.ts
1739
+ import {
1740
+ BufferAttribute,
1741
+ BufferGeometry,
1742
+ Matrix4
1743
+ } from "three";
1744
+ function convertCSGToThreeGeom(csg) {
1745
+ if (csg.polygons) {
1746
+ const vertices = [];
1747
+ const indices = [];
1748
+ const colors = [];
1749
+ let idx = 0;
1750
+ for (const polygon of csg.polygons) {
1751
+ for (const vertex of polygon.vertices) {
1752
+ vertex.index = idx;
1753
+ vertices.push(vertex[0], vertex[1], vertex[2]);
1754
+ if (csg.color && csg.color.length >= 3) {
1755
+ colors.push(csg.color[0], csg.color[1], csg.color[2]);
1756
+ } else {
1757
+ colors.push(1, 1, 1);
1758
+ }
1759
+ idx++;
1760
+ }
1761
+ const first = polygon.vertices[0].index;
1762
+ for (let i = 2; i < polygon.vertices.length; i++) {
1763
+ const second = polygon.vertices[i - 1].index;
1764
+ const third = polygon.vertices[i].index;
1765
+ indices.push(first, second, third);
1766
+ }
1767
+ }
1768
+ const geo = new BufferGeometry();
1769
+ geo.setAttribute(
1770
+ "position",
1771
+ new BufferAttribute(new Float32Array(vertices), 3)
1772
+ );
1773
+ geo.setIndex(indices);
1774
+ if (colors.length > 0) {
1775
+ geo.setAttribute(
1776
+ "color",
1777
+ new BufferAttribute(new Float32Array(colors), 3)
1778
+ );
1779
+ }
1780
+ if (csg.transforms) {
1781
+ const transforms2 = new Matrix4();
1782
+ transforms2.fromArray(csg.transforms);
1783
+ geo.applyMatrix4(transforms2);
1784
+ }
1785
+ geo.computeVertexNormals();
1786
+ return geo;
1787
+ }
1788
+ if (csg.sides) {
1789
+ const vertices = [];
1790
+ const colors = [];
1791
+ for (const side of csg.sides) {
1792
+ vertices.push(side[0][0], side[0][1], 0);
1793
+ if (csg.color && csg.color.length >= 3) {
1794
+ colors.push(csg.color[0], csg.color[1], csg.color[2]);
1795
+ } else {
1796
+ colors.push(1, 1, 1);
1797
+ }
1798
+ }
1799
+ const geo = new BufferGeometry();
1800
+ geo.setAttribute(
1801
+ "position",
1802
+ new BufferAttribute(new Float32Array(vertices), 3)
1803
+ );
1804
+ if (colors.length > 0) {
1805
+ geo.setAttribute(
1806
+ "color",
1807
+ new BufferAttribute(new Float32Array(colors), 3)
1808
+ );
1809
+ }
1810
+ if (csg.transforms) {
1811
+ const transforms2 = new Matrix4();
1812
+ transforms2.fromArray(csg.transforms);
1813
+ geo.applyMatrix4(transforms2);
1814
+ }
1815
+ return geo;
1816
+ }
1817
+ console.error("Invalid CSG object: neither polygons nor sides found");
1818
+ return new BufferGeometry();
1819
+ }
1820
+
1739
1821
  // lib/vanilla/index.ts
1740
1822
  function getJscadModelForFootprint(footprint) {
1741
1823
  const vnode = h(Footprinter3d, { footprint });
1742
1824
  return render(vnode);
1743
1825
  }
1826
+ function createJSCADRenderer(_jscad) {
1827
+ function createJSCADRoot(container) {
1828
+ return {
1829
+ render(element) {
1830
+ const { geometries: geometries2 } = render(element);
1831
+ container.splice(0, container.length, ...geometries2);
1832
+ }
1833
+ };
1834
+ }
1835
+ return { createJSCADRoot };
1836
+ }
1744
1837
  export {
1745
- getJscadModelForFootprint
1838
+ Fragment,
1839
+ convertCSGToThreeGeom,
1840
+ createJSCADRenderer,
1841
+ getJscadModelForFootprint,
1842
+ h
1746
1843
  };
1747
1844
  //# sourceMappingURL=vanilla.js.map