@modular-circuit/transpiler 0.0.81 → 0.0.83

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.
@@ -1,5 +1,3 @@
1
1
  import type { VECTOR2 } from '@modular-circuit/electronics-model';
2
- export declare const calc_boxes_pos: (boxes: {
3
- [id: string]: VECTOR2;
4
- }) => Promise<Record<string, VECTOR2>>;
2
+ export declare const calc_boxes_pos: (boxes: Record<string, VECTOR2>) => Promise<Record<string, VECTOR2>>;
5
3
  //# sourceMappingURL=calc_boxes_pos.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"calc_boxes_pos.d.ts","sourceRoot":"","sources":["../../../src/converter/netlist_to_kicad/calc_boxes_pos.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oCAAoC,CAAA;AAQjE,eAAO,MAAM,cAAc,GAAU,OAAO;IAC1C,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAA;CACtB,qCA0CA,CAAA"}
1
+ {"version":3,"file":"calc_boxes_pos.d.ts","sourceRoot":"","sources":["../../../src/converter/netlist_to_kicad/calc_boxes_pos.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oCAAoC,CAAA;AAGjE,eAAO,MAAM,cAAc,GACzB,OAAO,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC7B,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAqDjC,CAAA"}
@@ -34,83 +34,49 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
34
34
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
35
  }
36
36
  };
37
- var __values = (this && this.__values) || function(o) {
38
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
39
- if (m) return m.call(o);
40
- if (o && typeof o.length === "number") return {
41
- next: function () {
42
- if (o && i >= o.length) o = void 0;
43
- return { value: o && o[i++], done: !o };
44
- }
45
- };
46
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
47
- };
48
- // @ts-expect-error
49
- import { loadYoga } from 'yoga-layout/load';
50
- import { DEFAULT_PAPER_SIZE } from '../../utils';
51
37
  import { GRID_SIZE } from './layout';
52
38
  export var calc_boxes_pos = function (boxes) { return __awaiter(void 0, void 0, void 0, function () {
53
- var children_map, Yoga, node, child_count, idList, idList_1, idList_1_1, id, box, child, width, height, result, idList_2, idList_2_1, id, child;
54
- var e_1, _a, e_2, _b;
55
- return __generator(this, function (_c) {
56
- switch (_c.label) {
57
- case 0:
58
- children_map = {};
59
- return [4 /*yield*/, loadYoga()];
60
- case 1:
61
- Yoga = _c.sent();
62
- node = Yoga.Node.create();
63
- node.setAlignContent(Yoga.ALIGN_CENTER);
64
- node.setWidth(DEFAULT_PAPER_SIZE.width / GRID_SIZE);
65
- node.setHeight(DEFAULT_PAPER_SIZE.height / GRID_SIZE);
66
- node.setJustifyContent(Yoga.JUSTIFY_SPACE_EVENLY);
67
- node.setPadding(Yoga.EDGE_ALL, 1);
68
- child_count = 0;
69
- idList = Object.keys(boxes);
70
- try {
71
- for (idList_1 = __values(idList), idList_1_1 = idList_1.next(); !idList_1_1.done; idList_1_1 = idList_1.next()) {
72
- id = idList_1_1.value;
73
- box = boxes[id];
74
- child = Yoga.Node.create();
75
- width = box.x / GRID_SIZE;
76
- height = box.y / GRID_SIZE;
77
- child.setWidth(width);
78
- child.setHeight(height);
79
- child.setMargin(Yoga.EDGE_ALL, 2);
80
- node.insertChild(child, child_count);
81
- children_map[id] = child;
82
- child_count++;
83
- }
84
- }
85
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
86
- finally {
87
- try {
88
- if (idList_1_1 && !idList_1_1.done && (_a = idList_1.return)) _a.call(idList_1);
89
- }
90
- finally { if (e_1) throw e_1.error; }
91
- }
92
- node.calculateLayout(undefined, undefined, Yoga.DIRECTION_LTR);
93
- result = {};
94
- try {
95
- for (idList_2 = __values(idList), idList_2_1 = idList_2.next(); !idList_2_1.done; idList_2_1 = idList_2.next()) {
96
- id = idList_2_1.value;
97
- child = children_map[id];
98
- result[id] = {
99
- x: child.getComputedLeft() * GRID_SIZE,
100
- y: child.getComputedTop() * GRID_SIZE,
101
- };
102
- child.free();
103
- }
104
- }
105
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
106
- finally {
107
- try {
108
- if (idList_2_1 && !idList_2_1.done && (_b = idList_2.return)) _b.call(idList_2);
109
- }
110
- finally { if (e_2) throw e_2.error; }
111
- }
112
- node.free();
113
- return [2 /*return*/, result];
114
- }
39
+ var idList, sizes, cols, rows, colWidths, rowHeights, margin, result;
40
+ return __generator(this, function (_a) {
41
+ idList = Object.keys(boxes);
42
+ if (idList.length === 0)
43
+ return [2 /*return*/, {}
44
+ // Convert sizes to logical units (grid space)
45
+ ];
46
+ sizes = idList.map(function (id) {
47
+ var box = boxes[id];
48
+ return {
49
+ id: id,
50
+ w: box.x / GRID_SIZE,
51
+ h: box.y / GRID_SIZE,
52
+ };
53
+ });
54
+ cols = Math.ceil(Math.sqrt(idList.length));
55
+ rows = Math.ceil(idList.length / cols);
56
+ colWidths = Array(cols).fill(0);
57
+ rowHeights = Array(rows).fill(0);
58
+ sizes.forEach(function (s, i) {
59
+ var row = Math.floor(i / cols);
60
+ var col = i % cols;
61
+ colWidths[col] = Math.max(colWidths[col], s.w);
62
+ rowHeights[row] = Math.max(rowHeights[row], s.h);
63
+ });
64
+ margin = 1 // in grid units
65
+ ;
66
+ result = {};
67
+ sizes.forEach(function (s, i) {
68
+ var row = Math.floor(i / cols);
69
+ var col = i % cols;
70
+ var prevColsWidth = colWidths.slice(0, col).reduce(function (a, b) { return a + b; }, 0);
71
+ var prevRowsHeight = rowHeights.slice(0, row).reduce(function (a, b) { return a + b; }, 0);
72
+ // Cell top-left position
73
+ var cellLeft = prevColsWidth + col * margin;
74
+ var cellTop = prevRowsHeight + row * margin;
75
+ // Center box inside cell
76
+ var x = (cellLeft + (colWidths[col] - s.w) / 2) * GRID_SIZE;
77
+ var y = (cellTop + (rowHeights[row] - s.h) / 2) * GRID_SIZE;
78
+ result[s.id] = { x: x, y: y };
79
+ });
80
+ return [2 /*return*/, result];
115
81
  });
116
82
  }); };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modular-circuit/transpiler",
3
- "version": "0.0.81",
3
+ "version": "0.0.83",
4
4
  "description": "Intermediate representation of the modular circuit",
5
5
  "main": "./build/index.js",
6
6
  "files": [
@@ -27,9 +27,9 @@
27
27
  "js-base64": "^3.7.7",
28
28
  "jszip": "^3.10.1",
29
29
  "yoga-layout": "^3.2.1",
30
+ "@modular-circuit/ir": "0.0.61",
30
31
  "@modular-circuit/electronics-model": "0.0.54",
31
32
  "@modular-circuit/utils": "0.0.40",
32
- "@modular-circuit/ir": "0.0.61",
33
33
  "@modular-circuit/perc": "0.0.61"
34
34
  },
35
35
  "scripts": {