Skip to content

Commit

Permalink
feat: initial schematic support - wires
Browse files Browse the repository at this point in the history
  • Loading branch information
karinch committed Jan 29, 2020
1 parent ad242be commit 950e160
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/easyeda-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// Generated by https://quicktype.io

export interface IEasyEDASchematicCollection {
schematics: Array<{ dataStr: string }>;
}

export interface IEasyEDASchematic {
head: Head;
canvas: string;
shape: string[];
BBox: BBox;
colors: {};
}

export interface IEasyEDABoard {
head: Head;
canvas: string;
Expand Down
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import * as fs from 'fs';
import { convertBoard } from './board';
import { convertSchematic } from './schematic';

if (process.argv.length < 3) {
console.error(`Usage: ${process.argv[1]} <input.json> [output.kicad_pcb]`);
Expand All @@ -15,9 +16,10 @@ if (process.argv[2] === '-v') {
}

const input = JSON.parse(fs.readFileSync(process.argv[2], 'utf-8'));
const output = input.docType === '5' ? convertSchematic(input) : convertBoard(input);
const outputFile = process.argv[3];
if (outputFile && outputFile !== '-') {
fs.writeFileSync(outputFile, convertBoard(input));
fs.writeFileSync(outputFile, output);
} else {
process.stdout.write(convertBoard(input));
process.stdout.write(output);
}
60 changes: 60 additions & 0 deletions src/schematic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Doc: https://docs.easyeda.com/en/DocumentFormat/2-EasyEDA-Schematic-File-Format/index.html
import { IEasyEDASchematic, IEasyEDASchematicCollection } from './easyeda-types';

function kiUnits(value: string | number) {
if (typeof value === 'string') {
value = parseFloat(value);
}
return value * 11;
}

function flatten<T>(arr: T[]) {
return [].concat(...arr);
}

function convertWire(args: string[]) {
const [points, strokeColor, strokeWidth, strokeStyle, fillColor, id, locked] = args;
const coordList = points.split(' ');
const result = [];
for (let i = 0; i < coordList.length - 2; i += 2) {
result.push('Wire Wire Line');
result.push(
' ' +
coordList
.slice(i, i + 4)
.map(kiUnits)
.join(' ')
);
}
return result;
}

function convertShape(shape: string) {
const [type, ...args] = shape.split('~');
switch (type) {
case 'W':
return convertWire(args);
default:
console.warn(`Warning: unsupported shape ${type}`);
return [];
}
}

export function convertSchematic(input: IEasyEDASchematicCollection) {
const { schematics } = input;
if (schematics.length !== 1) {
console.warn(`Found ${schematics.length} schematics, converting only the first one.`);
}

const schematic = JSON.parse(schematics[0].dataStr) as IEasyEDASchematic;
const shapeArray = schematic.shape;
const shapeResult = flatten(shapeArray.map(convertShape)).join('\n');

return `
EESchema Schematic File Version 4
EELAYER 30 0
EELAYER END
${shapeResult}
$EndSCHEMATC
`.trim();
}

0 comments on commit 950e160

Please sign in to comment.