Skip to content

Instantly share code, notes, and snippets.

@kargeor
Created March 14, 2021 03:40
Show Gist options
  • Save kargeor/8a7444b68ed164409fb5935d375f5a77 to your computer and use it in GitHub Desktop.
Save kargeor/8a7444b68ed164409fb5935d375f5a77 to your computer and use it in GitHub Desktop.
EasyEDA add wires for all pins
function getRandomId() {
return "gge" + Math.round(Math.random() * 10000000);
}
const ids = api('getSelectedIds');
const result = api('getSource', {type:'json'});
function addWire(x1, y1, x2, y2) {
gId = getRandomId();
result.wire[gId] = {
gId,
pointArr: [{x:x1,y:y1}, {x:x2,y:y2}],
locked: '0',
strokeColor: '#008800',
strokeWidth: '1',
strokeStyle: '0',
fillColor: 'none',
};
}
if (!(ids in result.schlib)) {
alert("Please select object");
return;
}
const o = result.schlib[ids];
const allX = Object
.keys(o.pin)
.map(p => o.pin[p].configure.x);
const minX = Math.min(...allX);
const maxX = Math.max(...allX);
Object.keys(o.pin).forEach(p => {
const x = o.pin[p].configure.x;
const y = o.pin[p].configure.y;
let xx = x;
if (x == minX) xx -= 50;
else if (x == maxX) xx += 50;
else return;
addWire(x, y, xx, y);
});
api('applySource', {source: result, createNew: false});
@turbobabr
Copy link

Unfortunately, you script fails in case schematic document doesn't contain any wires yet, which is quite common when people test unknown scripts/plugins using blank document.

function addWire(x1, y1, x2, y2) {
  gId = getRandomId();
  result.wire = result.wire || {}; // ADDED LINE: Consider adding this line to initialize `wire` dictionary before accessing in.
  result.wire[gId] = {
    gId,
    pointArr: [{x:x1,y:y1}, {x:x2,y:y2}],
    locked: '0',
    strokeColor: '#008800',
    strokeWidth: '1',
    strokeStyle: '0',
    fillColor: 'none',
  };
}

Thank you for the script by the way! It inspired me to build a similar one that automatically adds wires and net ports with pre-initialized nets. :)

@kargeor
Copy link
Author

kargeor commented Mar 14, 2021

Thanks. I plan to also add the ability to name the "wires". Please share your script too.

Any idea how to generate new ids? I did random and it worked, but is there a better approach?

@xsrf
Copy link

xsrf commented Mar 14, 2021

The id generator is not accessible by scripts :/ But you can use the createShape API and just omit the gId. EasyEDA will generate one for you:

api('createShape',{
	shapeType:'wire',
	"jsonCache": {
		pointArr: [{x:10,y:10}, {x:20,y:20}],
		locked: '0',
		strokeColor: '#008800',
		strokeWidth: '1',
		strokeStyle: '0',
		fillColor: 'none',
	}
});

By using this, together with api('getShape',{id: id}) you also don't need to edit/apply the source (which resets the editors view).

Edit: Also, to iterate over multiple selected components use api('getSelectedIds').split(',')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment