forked from cho45/kicad-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch.ts
119 lines (94 loc) · 4.26 KB
/
sketch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//#!tsc && NODE_PATH=dist/src node dist/sketch.js
import { Transform } from "kicad_common";
import { CanvasPlotter, SVGPlotter } from "kicad_plotter";
import { SchPlotter } from "kicad_sch_plotter";
import { Library } from "kicad_lib";
import { Schematic } from "kicad_sch";
import * as fs from "fs";
function ensure<T>(arg: T | null | undefined): T {
if (!arg) throw "arg is falsy";
return arg;
}
{ // do sch
// const lib = Library.load(fs.readFileSync('../keyboard-schematic/Root-cache.lib', 'utf-8'));
// const sch = Schematic.load(fs.readFileSync('../keyboard-schematic/Root.sch', 'utf-8'));
// const lib = Library.load(fs.readFileSync('../keyboard-schematic/KeyModule-L-cache.lib', 'utf-8'));
// const sch = Schematic.load(fs.readFileSync('../keyboard-schematic/KeyModule-L.sch', 'utf-8'));
const lib = Library.load(fs.readFileSync('../keyboard-schematic/Root-cache.lib', 'utf-8'));
const sch = Schematic.load(fs.readFileSync('../keyboard-schematic/_keymodule_l.sch', 'utf-8'));
// const lib = Library.load(fs.readFileSync('../schematic-test/schematic-test-cache.lib', 'utf-8'));
// const sch = Schematic.load(fs.readFileSync('../schematic-test/file59827D42.sch', 'utf-8'));
// const sch = Schematic.load(fs.readFileSync('../schematic-test/schematic-test.sch', 'utf-8'));
//
// const lib = Library.load(fs.readFileSync('tmp/TinyFPGA-A-Programmer-cache.lib', 'utf-8'));
// const sch = Schematic.load(fs.readFileSync('tmp/TinyFPGA-A-Programmer.sch', 'utf-8'));
console.log(sch);
console.log(lib.findByName("GND"));
const MAX_WIDTH = 1920 * 2;
const MAX_HEIGHT = 1080 * 2;
const scale = Math.min(MAX_WIDTH / sch.descr.width, MAX_HEIGHT / sch.descr.height);
const Canvas = require('canvas');
const canvas = Canvas.createCanvas ? Canvas.createCanvas(sch.descr.width * scale, sch.descr.height * scale) : new Canvas(sch.descr.width * scale, sch.descr.height * scale);
const ctx = canvas.getContext('2d');
console.log(scale, canvas);
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.translate(0, 0);
ctx.scale(scale, scale);
const plotter = new CanvasPlotter(ctx);
// plotter.translate(-sch.descr.width, 0);
// plotter.scale(-1, 1);
// plotter.plotLibComponent(lib.findByName("RJ45"), 1, 1, { x: 500, y: 500 }, new Transform(0, 1, 1, 0));
plotter.startPlot();
new SchPlotter(plotter).plotSchematic(sch, [ lib ]);
plotter.endPlot();
const out = fs.createWriteStream('text.png'), stream = canvas.pngStream();
stream.on('data', function (chunk: any) {
out.write(chunk);
});
stream.on('end', function(){
console.log('saved png');
});
const svgPlotter = new SVGPlotter();
// svgPlotter.translate(-sch.descr.width, 0);
// svgPlotter.scale(-1, 1);
plotter.pageInfo = sch.descr.pageInfo;
plotter.startPlot();
new SchPlotter(svgPlotter).plotSchematic(sch, [ lib ]);
plotter.endPlot();
let dpi = 72; // 72 dpi == 72000 dot/mil
// sch.descr.{width,height} is mil
// 1000mil = 1inch = 72dot
fs.writeFileSync("text.svg", svgPlotter.output);
}
/*
//{
// const content = fs.readFileSync('/Library/Application Support/kicad/library/74xx.lib', 'utf-8')
// const lib = Library.load(content);
// const component = lib.findByName("74LS00");
// console.log(component);
// console.log(component.draw);
//};
const Canvas = require('canvas');
const canvas = new Canvas(2000, 2000);
const ctx = canvas.getContext('2d');
const content = fs.readFileSync('../keyboard-schematic/Root-cache.lib', 'utf-8')
const lib = Library.load(content);
// const component = lib.findByName("INDUCTOR");
// const component = lib.findByName("JUMPER3");
// const component = lib.findByName("LED_RGB");
// const component = lib.findByName("Led_x2");
const plotter = new CanvasPlotter(ctx);
plotter.plotLibComponent(ensure(lib.findByName("RJ45")), 1, 1, new Transform(0, 1, 1, 0, 1000, 1000));
// plotter.plotLibComponent(ensure(lib.findByName("RJ45")), 1, 1, new Transform().translate(1000, 1000));
const out = fs.createWriteStream('text.png'), stream = canvas.pngStream();
stream.on('data', function (chunk: any) {
out.write(chunk);
});
stream.on('end', function(){
console.log('saved png');
});
//const svgPlotter = new SVGPlotter();
//svgPlotter.plotLibComponent(ensure(lib.findByName("RJ45")), 1, 1, new Transform(0, 1, 1, 0).translate(1000, 1000));
//fs.writeFileSync("text.svg", svgPlotter.output);
//*/