@modular-circuit/transpiler 0.1.12 → 0.1.14

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.mts CHANGED
@@ -136,13 +136,13 @@ declare function convert_graph_to_design_block(ctx: ConvertGraphToKiCadInput): P
136
136
 
137
137
  declare const KICAD_SCH_FRAME: string;
138
138
  declare class KiCadProjectArchive {
139
- project_name: string;
140
- sheets: Record<string, string>;
139
+ private designBlock;
141
140
  kicad_sym?: string;
142
141
  fp_lib_table?: string;
143
142
  layout_frame?: string;
144
143
  pcb?: string;
145
144
  sym_lib_table?: string;
145
+ project_name: string;
146
146
  get sch_frame(): string;
147
147
  get kicad_prl(): {
148
148
  board: {
@@ -367,7 +367,7 @@ declare class KiCadProjectArchive {
367
367
  text_variables: {};
368
368
  };
369
369
  get main_sch_name(): string;
370
- constructor(project_name: string, sheets: Record<string, string>);
370
+ constructor(designBlock: DesignBlockContent);
371
371
  get_project_directive_file_name(ext: string): string;
372
372
  toZip(): Promise<Blob>;
373
373
  }
package/dist/index.d.ts CHANGED
@@ -136,13 +136,13 @@ declare function convert_graph_to_design_block(ctx: ConvertGraphToKiCadInput): P
136
136
 
137
137
  declare const KICAD_SCH_FRAME: string;
138
138
  declare class KiCadProjectArchive {
139
- project_name: string;
140
- sheets: Record<string, string>;
139
+ private designBlock;
141
140
  kicad_sym?: string;
142
141
  fp_lib_table?: string;
143
142
  layout_frame?: string;
144
143
  pcb?: string;
145
144
  sym_lib_table?: string;
145
+ project_name: string;
146
146
  get sch_frame(): string;
147
147
  get kicad_prl(): {
148
148
  board: {
@@ -367,7 +367,7 @@ declare class KiCadProjectArchive {
367
367
  text_variables: {};
368
368
  };
369
369
  get main_sch_name(): string;
370
- constructor(project_name: string, sheets: Record<string, string>);
370
+ constructor(designBlock: DesignBlockContent);
371
371
  get_project_directive_file_name(ext: string): string;
372
372
  toZip(): Promise<Blob>;
373
373
  }
package/dist/index.js CHANGED
@@ -2474,7 +2474,18 @@ var import_ir5 = require("@modular-circuit/ir");
2474
2474
  var import_perc = require("@modular-circuit/perc");
2475
2475
  var import_utils14 = require("@modular-circuit/utils");
2476
2476
  async function do_convert_graph_to_kicad_project(ctx) {
2477
- const { sheets } = await collect_sub_sheets(ctx.project.dependencies, ctx.module_resolver);
2477
+ const module_ids = [];
2478
+ for (const sch of Object.values(ctx.schematics)) {
2479
+ for (const mod of sch.blocks ?? []) {
2480
+ if (!(0, import_ir5.block_is_abstract)(mod) && (0, import_ir5.block_is_associated_with_concrete_module)(mod.type)) {
2481
+ module_ids.push(mod.type);
2482
+ }
2483
+ }
2484
+ }
2485
+ const { sheets } = {
2486
+ ...await collect_sub_sheets(ctx.project.dependencies, ctx.module_resolver),
2487
+ ...await collect_sub_sheets_by_id(module_ids, ctx.module_resolver)
2488
+ };
2478
2489
  const netlist = new GraphConverter({
2479
2490
  ...ctx
2480
2491
  }).convert_to_netlist();
@@ -2501,9 +2512,6 @@ async function convert_graph_to_sheets(ctx) {
2501
2512
  });
2502
2513
  }
2503
2514
 
2504
- // src/kicad/project/kicad_project_archive.ts
2505
- var import_utils16 = require("@modular-circuit/utils");
2506
-
2507
2515
  // src/kicad/project/kicad_prl.ts
2508
2516
  var kicad_prl = (prj_name) => ({
2509
2517
  board: {
@@ -2590,7 +2598,7 @@ var kicad_prl = (prj_name) => ({
2590
2598
  });
2591
2599
 
2592
2600
  // src/kicad/project/kicad_pro.ts
2593
- var import_utils15 = require("@modular-circuit/utils");
2601
+ var import_utils16 = require("@modular-circuit/utils");
2594
2602
  var kicad_pro = (project_name) => ({
2595
2603
  board: {
2596
2604
  "3dviewports": [],
@@ -2800,7 +2808,7 @@ var kicad_pro = (project_name) => ({
2800
2808
  version: 1
2801
2809
  },
2802
2810
  net_format_name: "",
2803
- page_layout_descr_file: `${project_name}.${import_utils15.DrawingSheetFileExtension}`,
2811
+ page_layout_descr_file: `${project_name}.${import_utils16.DrawingSheetFileExtension}`,
2804
2812
  plot_directory: "",
2805
2813
  spice_current_sheet_as_root: false,
2806
2814
  spice_external_command: 'spice "%I"',
@@ -2817,15 +2825,17 @@ var kicad_pro = (project_name) => ({
2817
2825
 
2818
2826
  // src/kicad/project/kicad_project_archive.ts
2819
2827
  var import_utils17 = require("@modular-circuit/utils");
2828
+ var import_jszip = __toESM(require("jszip"));
2820
2829
  var KICAD_SCH_FRAME = get_sch_default_drawing_sheet();
2821
2830
  var KiCadProjectArchive = class {
2822
- constructor(project_name, sheets) {
2823
- this.project_name = project_name;
2824
- this.sheets = sheets;
2825
- if (!(this.main_sch_name in this.sheets)) {
2826
- throw new Error(
2827
- `Expecting a main schematic file named ${this.main_sch_name} in the sheets : ${Object.keys(this.sheets)}`
2828
- );
2831
+ constructor(designBlock) {
2832
+ this.designBlock = designBlock;
2833
+ if (!this.designBlock.root) {
2834
+ throw new Error("No root sch file found");
2835
+ }
2836
+ this.project_name = this.designBlock.root.split(".")[0];
2837
+ if (!this.project_name) {
2838
+ throw new Error(`Cannot get project name from ${this.designBlock.root}`);
2829
2839
  }
2830
2840
  }
2831
2841
  get sch_frame() {
@@ -2843,33 +2853,82 @@ var KiCadProjectArchive = class {
2843
2853
  get_project_directive_file_name(ext) {
2844
2854
  return `${this.project_name}.${ext}`;
2845
2855
  }
2846
- toZip() {
2847
- return (0, import_utils16.zipFiles)({
2848
- [this.get_project_directive_file_name(import_utils17.ProjectLocalSettingsFileExtension)]: JSON.stringify(this.kicad_prl),
2849
- [this.get_project_directive_file_name(import_utils17.ProjectFileExtension)]: JSON.stringify(this.kicad_pro),
2850
- [this.get_project_directive_file_name(import_utils17.DrawingSheetFileExtension)]: this.sch_frame,
2851
- ...this.sheets
2852
- });
2856
+ async toZip() {
2857
+ const zip = new import_jszip.default();
2858
+ zip.file(
2859
+ this.get_project_directive_file_name(import_utils17.ProjectLocalSettingsFileExtension),
2860
+ JSON.stringify(this.kicad_prl)
2861
+ );
2862
+ zip.file(
2863
+ this.get_project_directive_file_name(import_utils17.ProjectFileExtension),
2864
+ JSON.stringify(this.kicad_pro)
2865
+ );
2866
+ zip.file(
2867
+ this.get_project_directive_file_name(import_utils17.DrawingSheetFileExtension),
2868
+ this.sch_frame
2869
+ );
2870
+ for (const schematic of this.designBlock.schematics) {
2871
+ zip.file(schematic.filename, schematic.content);
2872
+ }
2873
+ const fpLibTableContent = `(fp_lib_table
2874
+ (version 7)
2875
+ (lib (name "kicad_api_lib")(type "KiCad")(uri "\${KIPRJMOD}/kicad_api_libs/kicad_api_lib.pretty")(options "Added by KiCad IPC API")(descr ""))
2876
+ )`;
2877
+ zip.file("fp-lib-table", fpLibTableContent);
2878
+ let symLibTableContent = "(sym_lib_table\n (version 7)\n";
2879
+ const existing_components = /* @__PURE__ */ new Set();
2880
+ if (this.designBlock.components) {
2881
+ for (const component of this.designBlock.components) {
2882
+ const symFilename = component.symbol.filename;
2883
+ const componentName = symFilename.replace(".kicad_sym", "");
2884
+ if (existing_components.has(componentName)) {
2885
+ continue;
2886
+ }
2887
+ existing_components.add(componentName);
2888
+ symLibTableContent += ` (lib (name "${componentName}")(type "KiCad")(uri "\${KIPRJMOD}/kicad_api_libs/${symFilename}")(options "Added by KiCad IPC API")(descr ""))
2889
+ `;
2890
+ zip.file(`kicad_api_libs/${symFilename}`, component.symbol.content);
2891
+ if (component.footprints) {
2892
+ for (const fp of component.footprints) {
2893
+ const fpFilename = fp.footprint.filename;
2894
+ zip.file(
2895
+ `kicad_api_libs/kicad_api_lib.pretty/${fpFilename}`,
2896
+ fp.footprint.content
2897
+ );
2898
+ if (fp.models) {
2899
+ for (const model of fp.models) {
2900
+ zip.file(
2901
+ `kicad_api_libs/3d_models/${model.filename}`,
2902
+ model.content
2903
+ );
2904
+ }
2905
+ }
2906
+ }
2907
+ }
2908
+ }
2909
+ }
2910
+ symLibTableContent += ")";
2911
+ zip.file("sym-lib-table", symLibTableContent);
2912
+ if (this.designBlock.pcb?.pcb) {
2913
+ if (this.designBlock.pcb.pcb.content) {
2914
+ zip.file(this.designBlock.pcb.pcb.filename, this.designBlock.pcb.pcb.content);
2915
+ }
2916
+ }
2917
+ const blob = await zip.generateAsync({ type: "blob" });
2918
+ return blob;
2853
2919
  }
2854
2920
  };
2855
2921
 
2856
- // src/builder/graph_to_kicad/convert_graph_to_kicad_project.ts
2857
- async function convert_graph_to_kicad_project(ctx) {
2858
- const sheets = await convert_graph_to_sheets(ctx);
2859
- const kicad_project = new KiCadProjectArchive(ctx.project.name, {
2860
- ...sheets
2861
- });
2862
- return kicad_project.toZip();
2863
- }
2864
-
2865
2922
  // src/builder/graph_to_kicad/convert_graph_to_design_block.ts
2923
+ var import_utils18 = require("@modular-circuit/utils");
2866
2924
  async function convert_graph_to_design_block(ctx) {
2925
+ const root = ctx.project.main.replace(import_utils18.ModularCircuitFileExtension, import_utils18.KiCadSchematicFileExtension);
2867
2926
  const sheets = await convert_graph_to_sheets(ctx);
2868
- const kicad_project = new KiCadProjectArchive(ctx.project.name, {
2869
- ...sheets
2870
- });
2927
+ if (!(root in sheets)) {
2928
+ throw new Error(`Cannot find root sch file ${root}`);
2929
+ }
2871
2930
  const design_block = {
2872
- root: kicad_project.main_sch_name,
2931
+ root,
2873
2932
  pcb: null,
2874
2933
  components: [],
2875
2934
  schematics: Object.entries(sheets).map(([filename, content]) => {
@@ -2881,6 +2940,13 @@ async function convert_graph_to_design_block(ctx) {
2881
2940
  };
2882
2941
  return design_block;
2883
2942
  }
2943
+
2944
+ // src/builder/graph_to_kicad/convert_graph_to_kicad_project.ts
2945
+ async function convert_graph_to_kicad_project(ctx) {
2946
+ const design_block = await convert_graph_to_design_block(ctx);
2947
+ const kicad_project = new KiCadProjectArchive(design_block);
2948
+ return kicad_project.toZip();
2949
+ }
2884
2950
  // Annotate the CommonJS export names for ESM import in node:
2885
2951
  0 && (module.exports = {
2886
2952
  BLOCK_PIN_GAP,
package/dist/index.mjs CHANGED
@@ -2430,11 +2430,26 @@ var NetListConverter = class {
2430
2430
  };
2431
2431
 
2432
2432
  // src/builder/graph_to_kicad/convert_graph_to_sheets.ts
2433
- import { MODULAR_CIRCUIT_SCH_EXT as MODULAR_CIRCUIT_SCH_EXT2 } from "@modular-circuit/ir";
2433
+ import {
2434
+ block_is_abstract,
2435
+ block_is_associated_with_concrete_module,
2436
+ MODULAR_CIRCUIT_SCH_EXT as MODULAR_CIRCUIT_SCH_EXT2
2437
+ } from "@modular-circuit/ir";
2434
2438
  import { build_connection_graph } from "@modular-circuit/perc";
2435
2439
  import { KiCadSchematicFileExtension as KiCadSchematicFileExtension3 } from "@modular-circuit/utils";
2436
2440
  async function do_convert_graph_to_kicad_project(ctx) {
2437
- const { sheets } = await collect_sub_sheets(ctx.project.dependencies, ctx.module_resolver);
2441
+ const module_ids = [];
2442
+ for (const sch of Object.values(ctx.schematics)) {
2443
+ for (const mod of sch.blocks ?? []) {
2444
+ if (!block_is_abstract(mod) && block_is_associated_with_concrete_module(mod.type)) {
2445
+ module_ids.push(mod.type);
2446
+ }
2447
+ }
2448
+ }
2449
+ const { sheets } = {
2450
+ ...await collect_sub_sheets(ctx.project.dependencies, ctx.module_resolver),
2451
+ ...await collect_sub_sheets_by_id(module_ids, ctx.module_resolver)
2452
+ };
2438
2453
  const netlist = new GraphConverter({
2439
2454
  ...ctx
2440
2455
  }).convert_to_netlist();
@@ -2461,9 +2476,6 @@ async function convert_graph_to_sheets(ctx) {
2461
2476
  });
2462
2477
  }
2463
2478
 
2464
- // src/kicad/project/kicad_project_archive.ts
2465
- import { zipFiles } from "@modular-circuit/utils";
2466
-
2467
2479
  // src/kicad/project/kicad_prl.ts
2468
2480
  var kicad_prl = (prj_name) => ({
2469
2481
  board: {
@@ -2782,15 +2794,17 @@ import {
2782
2794
  ProjectFileExtension,
2783
2795
  ProjectLocalSettingsFileExtension
2784
2796
  } from "@modular-circuit/utils";
2797
+ import JSZip from "jszip";
2785
2798
  var KICAD_SCH_FRAME = get_sch_default_drawing_sheet();
2786
2799
  var KiCadProjectArchive = class {
2787
- constructor(project_name, sheets) {
2788
- this.project_name = project_name;
2789
- this.sheets = sheets;
2790
- if (!(this.main_sch_name in this.sheets)) {
2791
- throw new Error(
2792
- `Expecting a main schematic file named ${this.main_sch_name} in the sheets : ${Object.keys(this.sheets)}`
2793
- );
2800
+ constructor(designBlock) {
2801
+ this.designBlock = designBlock;
2802
+ if (!this.designBlock.root) {
2803
+ throw new Error("No root sch file found");
2804
+ }
2805
+ this.project_name = this.designBlock.root.split(".")[0];
2806
+ if (!this.project_name) {
2807
+ throw new Error(`Cannot get project name from ${this.designBlock.root}`);
2794
2808
  }
2795
2809
  }
2796
2810
  get sch_frame() {
@@ -2808,33 +2822,82 @@ var KiCadProjectArchive = class {
2808
2822
  get_project_directive_file_name(ext) {
2809
2823
  return `${this.project_name}.${ext}`;
2810
2824
  }
2811
- toZip() {
2812
- return zipFiles({
2813
- [this.get_project_directive_file_name(ProjectLocalSettingsFileExtension)]: JSON.stringify(this.kicad_prl),
2814
- [this.get_project_directive_file_name(ProjectFileExtension)]: JSON.stringify(this.kicad_pro),
2815
- [this.get_project_directive_file_name(DrawingSheetFileExtension2)]: this.sch_frame,
2816
- ...this.sheets
2817
- });
2825
+ async toZip() {
2826
+ const zip = new JSZip();
2827
+ zip.file(
2828
+ this.get_project_directive_file_name(ProjectLocalSettingsFileExtension),
2829
+ JSON.stringify(this.kicad_prl)
2830
+ );
2831
+ zip.file(
2832
+ this.get_project_directive_file_name(ProjectFileExtension),
2833
+ JSON.stringify(this.kicad_pro)
2834
+ );
2835
+ zip.file(
2836
+ this.get_project_directive_file_name(DrawingSheetFileExtension2),
2837
+ this.sch_frame
2838
+ );
2839
+ for (const schematic of this.designBlock.schematics) {
2840
+ zip.file(schematic.filename, schematic.content);
2841
+ }
2842
+ const fpLibTableContent = `(fp_lib_table
2843
+ (version 7)
2844
+ (lib (name "kicad_api_lib")(type "KiCad")(uri "\${KIPRJMOD}/kicad_api_libs/kicad_api_lib.pretty")(options "Added by KiCad IPC API")(descr ""))
2845
+ )`;
2846
+ zip.file("fp-lib-table", fpLibTableContent);
2847
+ let symLibTableContent = "(sym_lib_table\n (version 7)\n";
2848
+ const existing_components = /* @__PURE__ */ new Set();
2849
+ if (this.designBlock.components) {
2850
+ for (const component of this.designBlock.components) {
2851
+ const symFilename = component.symbol.filename;
2852
+ const componentName = symFilename.replace(".kicad_sym", "");
2853
+ if (existing_components.has(componentName)) {
2854
+ continue;
2855
+ }
2856
+ existing_components.add(componentName);
2857
+ symLibTableContent += ` (lib (name "${componentName}")(type "KiCad")(uri "\${KIPRJMOD}/kicad_api_libs/${symFilename}")(options "Added by KiCad IPC API")(descr ""))
2858
+ `;
2859
+ zip.file(`kicad_api_libs/${symFilename}`, component.symbol.content);
2860
+ if (component.footprints) {
2861
+ for (const fp of component.footprints) {
2862
+ const fpFilename = fp.footprint.filename;
2863
+ zip.file(
2864
+ `kicad_api_libs/kicad_api_lib.pretty/${fpFilename}`,
2865
+ fp.footprint.content
2866
+ );
2867
+ if (fp.models) {
2868
+ for (const model of fp.models) {
2869
+ zip.file(
2870
+ `kicad_api_libs/3d_models/${model.filename}`,
2871
+ model.content
2872
+ );
2873
+ }
2874
+ }
2875
+ }
2876
+ }
2877
+ }
2878
+ }
2879
+ symLibTableContent += ")";
2880
+ zip.file("sym-lib-table", symLibTableContent);
2881
+ if (this.designBlock.pcb?.pcb) {
2882
+ if (this.designBlock.pcb.pcb.content) {
2883
+ zip.file(this.designBlock.pcb.pcb.filename, this.designBlock.pcb.pcb.content);
2884
+ }
2885
+ }
2886
+ const blob = await zip.generateAsync({ type: "blob" });
2887
+ return blob;
2818
2888
  }
2819
2889
  };
2820
2890
 
2821
- // src/builder/graph_to_kicad/convert_graph_to_kicad_project.ts
2822
- async function convert_graph_to_kicad_project(ctx) {
2823
- const sheets = await convert_graph_to_sheets(ctx);
2824
- const kicad_project = new KiCadProjectArchive(ctx.project.name, {
2825
- ...sheets
2826
- });
2827
- return kicad_project.toZip();
2828
- }
2829
-
2830
2891
  // src/builder/graph_to_kicad/convert_graph_to_design_block.ts
2892
+ import { KiCadSchematicFileExtension as KiCadSchematicFileExtension5, ModularCircuitFileExtension } from "@modular-circuit/utils";
2831
2893
  async function convert_graph_to_design_block(ctx) {
2894
+ const root = ctx.project.main.replace(ModularCircuitFileExtension, KiCadSchematicFileExtension5);
2832
2895
  const sheets = await convert_graph_to_sheets(ctx);
2833
- const kicad_project = new KiCadProjectArchive(ctx.project.name, {
2834
- ...sheets
2835
- });
2896
+ if (!(root in sheets)) {
2897
+ throw new Error(`Cannot find root sch file ${root}`);
2898
+ }
2836
2899
  const design_block = {
2837
- root: kicad_project.main_sch_name,
2900
+ root,
2838
2901
  pcb: null,
2839
2902
  components: [],
2840
2903
  schematics: Object.entries(sheets).map(([filename, content]) => {
@@ -2846,6 +2909,13 @@ async function convert_graph_to_design_block(ctx) {
2846
2909
  };
2847
2910
  return design_block;
2848
2911
  }
2912
+
2913
+ // src/builder/graph_to_kicad/convert_graph_to_kicad_project.ts
2914
+ async function convert_graph_to_kicad_project(ctx) {
2915
+ const design_block = await convert_graph_to_design_block(ctx);
2916
+ const kicad_project = new KiCadProjectArchive(design_block);
2917
+ return kicad_project.toZip();
2918
+ }
2849
2919
  export {
2850
2920
  BLOCK_PIN_GAP,
2851
2921
  BLOCK_PIN_TB_MARGIN,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modular-circuit/transpiler",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "Intermediate representation of the modular circuit",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -30,8 +30,8 @@
30
30
  "js-base64": "^3.7.7",
31
31
  "jszip": "^3.10.1",
32
32
  "@modular-circuit/electronics-model": "0.1.1",
33
- "@modular-circuit/ir": "0.1.3",
34
- "@modular-circuit/utils": "0.1.4",
33
+ "@modular-circuit/ir": "0.1.4",
34
+ "@modular-circuit/utils": "0.1.5",
35
35
  "@modular-circuit/perc": "0.1.4"
36
36
  },
37
37
  "scripts": {