Skip to content

Instantly share code, notes, and snippets.

@RaD
Created December 29, 2013 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RaD/8175866 to your computer and use it in GitHub Desktop.
Save RaD/8175866 to your computer and use it in GitHub Desktop.
Eagle CAD: Скрипт нормализации подписей компонентов на плате
#usage "<b>Snap silkscreen text on grid</b><hr/>\n"
"<author>Author: Ruslan Popov (ruslan.popov@gmail.com)</author>"
#require 6.05
string VERSION = "1.0";
int result = 0; // dialog result
string str; // temporary string
string cmd = "SET UNDO_LOG OFF;\n"; // script command to execute
int silk_screen_layers[] = {LAYER_TNAMES, LAYER_BNAMES};
enum {UNIT_MIL, UNIT_MM, UNIT_IN};
int curr_unit = UNIT_MM;
//
// Redisplays the layers that were visible when the ULP was started
//
void reset_visible(UL_BOARD B) {
sprintf(str, "DISP NONE;\nDISP ");
cmd += str;
B.layers(L) {
if (L.visible) {
sprintf(str, "%d ", L.number);
cmd += str;
}
}
cmd += ";\n";
}
//
// Snapping.
//
real snapping(int unit, int value, real step) {
real tmp = 0.0;
real multiplier = 0.0;
switch(unit) {
case UNIT_MIL:
tmp = u2mil(value);
break;
case UNIT_MM:
tmp = u2mm(value);
break;
case UNIT_IN:
tmp = u2inch(value);
break;
}
multiplier = 1.0 / step;
tmp = round(tmp * multiplier) / multiplier;
return tmp;
}
if (board) {
real grid_step = 0.025;
real grid_min = 0.0001;
real grid_max = 10;
string text_font[] = {"vector", "proportional", "fixed"};
int selected_font = 0;
string text_align[] = {"bottom left", "bottom center", "bottom right",
"center left", "center", "center right",
"top left", "top center", "top right"};
int selected_align = 4;
string text_size[] = {"0.3048", "0.4064", "0.6096", "0.8128", "1", "1.016",
"1.27", "1.4224", "1.6764", "1.778", "2.18", "2.54"};
int selected_size = 3;
int ratio_value = 18;
int ratio_min = 11;
int ratio_max = 50;
int snap_to_grid = 1;
result = dlgDialog("Normalize component texts") {
sprintf(str, "<qt>"+
"<h3>Normalize component texts v%s</h3>"+
"<p>by Ruslan Popov (ruslan.popov@gmail.com)</p>"+
"<p>This ULP normalizes the component names "+
"on the silkscreen layers to the specified values.</p>"+
"</qt>", VERSION);
dlgLabel(str);
// options
dlgHBoxLayout {
dlgGroup("Output units") {
dlgRadioButton("m&il", curr_unit) {
curr_unit = UNIT_MIL;
grid_step = 1;
}
dlgRadioButton("&mm", curr_unit) {
curr_unit = UNIT_MM;
grid_step = 0.025;
}
dlgRadioButton("&inches", curr_unit) {
curr_unit = UNIT_IN;
grid_step = 0.001;
}
}
dlgSpacing(20);
dlgGridLayout {
dlgCell(0, 0) dlgLabel("Font");
dlgCell(0, 1) dlgComboBox(text_font, selected_font) cmd += selected_font;
dlgCell(1, 0) dlgLabel("Align");
dlgCell(1, 1) dlgComboBox(text_align, selected_align) cmd += selected_align;
dlgCell(2, 0) dlgLabel("Size");
dlgCell(2, 1) dlgComboBox(text_size, selected_size) cmd += selected_size;
dlgCell(3, 0) dlgLabel("Ratio");
dlgCell(3, 1) dlgIntEdit(ratio_value, ratio_min, ratio_max);
dlgCell(4, 0) dlgLabel("Grid Step");
dlgCell(4, 1) dlgRealEdit(grid_step, grid_min, grid_max);
dlgCell(5, 0, 5, 1) dlgCheckBox("Snap to grid", snap_to_grid);
}
}
// buttons
dlgHBoxLayout {
dlgStretch(1);
dlgPushButton("+Normalize") dlgAccept();
dlgPushButton("-Cancel") dlgReject();
}
};
if (!result) exit(0);
switch(curr_unit) {
case UNIT_MIL:
sprintf(str, "GRID MIL %f;\n", grid_step);
break;
case UNIT_MM:
sprintf(str, "GRID MM %f;\n", grid_step);
break;
case UNIT_IN:
sprintf(str, "GRID INCH %f;\n", grid_step);
break;
}
cmd += str;
board(B) {
B.elements(E) {
if (E.package) {
if (!E.smashed) {
sprintf(str, "SMASH %s;\n", E.name);
cmd += str;
}
if (E.smashed["VALUE"]) {
sprintf(str, "DELETE %s>VALUE;\n", E.name);
cmd += str;
}
if (snap_to_grid) {
E.texts(T) {
sprintf(str, "MOVE %s>NAME (%f %f);\n", E.name,
snapping(curr_unit, T.x, grid_step),
snapping(curr_unit, T.y, grid_step));
cmd += str;
}
}
cmd += "\n";
}
}
sprintf(str, "DISPLAY NONE TNAMES BNAMES;\n");
cmd += str;
sprintf(str, "GROUP ALL; CHANGE FONT %s (>0 0);\n", text_font[selected_font]);
cmd += str;
sprintf(str, "GROUP ALL; CHANGE ALIGN %s (>0 0);\n", text_align[selected_align]);
cmd += str;
sprintf(str, "GROUP ALL; CHANGE SIZE %s (>0 0);\n", text_size[selected_size]);
cmd += str;
sprintf(str, "GROUP ALL; CHANGE RATIO %u (>0 0);\n", ratio_value);
cmd += str;
reset_visible(B);
}
cmd += "GRID LAST;\nSET UNDO_LOG ON;\n";
result = dlgDialog("Edit and execute script") {
dlgHBoxLayout {
dlgSpacing(500); // Force width of dialog to 500
}
dlgTextEdit(cmd);
dlgHBoxLayout {
dlgPushButton("+Execute") dlgAccept();
dlgPushButton("-Cancel") dlgReject();
}
};
// Execute script if it was accepted
if (!result)
exit(0);
else
exit(cmd); // run composed script
} else {
dlgMessageBox("Start this ULP on board only!", "OK");
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment