File: cmd-snap-board.ulp

package info (click to toggle)
eagle 4.16-5
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 36,508 kB
  • sloc: sh: 82; makefile: 32
file content (205 lines) | stat: -rw-r--r-- 5,955 bytes parent folder | download | duplicates (3)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#usage "<b>Snap objects in a board</b>\n"
       "<p>"
       "Snaps components, wires and vias of the current "
       "board to a given grid. "
       "If 'Show script' is checked, you can edit the MOVE commands "
       "before they are executed. So you are able to exclude certain "
       "elements from the snap procedure."
       "<p>"
       "<author>Author: support@cadsoft.de</author>"

// THIS PROGRAM IS PROVIDED AS IS AND WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED

real    GridDist = 50.0;
enum    {unitINCH, unitMIL, unitMM, unitMIC};
string  h, cmd = "";
int     unit = unitMIL;   // predefined unit, can be changed to unitMM, unitINCH, unitMIC
int     show_script = 1;  // predefined value for button "Show script"
int     Result;
string  Status = "";

// get project path, if in board or schematic, otherwise library path
string get_project_path() {
string s = "", p = "";;
  if (library)  { library(L)  s = L.name;}
  if (board)    { board(B)     s = B.name;}
  if (schematic){ schematic(S) s = S.name;}
  char c = '/';
  int pos = strrchr(s, c);
  if (pos >= 0) {
     p = strsub(s, 0, pos + 1);
     }
  return p;
}

real u2unit(int u) {
  if (unit == unitMIL)
     return u2mil(u);
  if (unit == unitMM)
     return u2mm(u);
  if (unit == unitINCH)
     return u2inch(u);
  if (unit == unitMIC)
     return u2mic(u);
}

real snap(int n) {  // returns next grid point
  return round(u2unit(n) / GridDist)   * GridDist;
}

int n = 0, x[], y[], l[], UsedLayers[];

int isNew(int X, int Y, int L) {
  for (int i = 0; i < n; i++) {
      if (x[i] == X && y[i] == Y && (l[i] == L || l[i] == LAYER_VIAS ))
         return 0;
      }
  return 1;
}

void Move(int Layer) {
  if (UsedLayers[Layer]) {
     h = ""; sprintf(h, "DISPLAY NONE %d;\n", Layer); cmd += h;
     // Snap the signal wires and vias:
     for (int i = 0; i < n; i++) {
         if (l[i] == Layer && (u2unit(x[i]) != snap(x[i]) || u2unit(y[i]) != snap(y[i]))) {
            h = ""; sprintf(h, "MOVE (%f %f) (%f %f);\n", u2unit(x[i]), u2unit(y[i]), snap(x[i]), snap(y[i])); cmd += h;
            }
         }
     }
}

void snap_to_grid (void) {
  board(B) {
    // Collect all (unique!) signal wire and via coordinates:
    B.signals(S) {
      Status = "Signal: "+S.name; dlgRedisplay();
      S.vias(V) {
        UsedLayers[LAYER_VIAS] = 1;
        x[n] = V.x;
        y[n] = V.y;
        l[n] = LAYER_VIAS;
        n++;
        }
      S.wires(W) {
        UsedLayers[W.layer] = 1;
        if (isNew(W.x1, W.y1, W.layer)) {
           x[n] = W.x1;
           y[n] = W.y1;
           l[n] = W.layer;
           n++;
           }
        if (isNew(W.x2, W.y2, W.layer)) {
           x[n] = W.x2;
           y[n] = W.y2;
           l[n] = W.layer;
           n++;
           }
        }
      }

    // Remember the active layers:
    int ActiveLayers[];
    B.layers(L) {
      ActiveLayers[L.number] = L.visible;
      }

    if (unit == unitMIL)  {
       h = ""; sprintf(h, "GRID MIL FINEST;\n");  cmd += h;
       }
    if (unit == unitMM)   {
       h = ""; sprintf(h, "GRID MM FINEST;\n");   cmd += h;
       }
    if (unit == unitINCH) {
       h = ""; sprintf(h, "GRID INCH FINEST;\n"); cmd += h;
       }
    if (unit == unitMIC)  {
       h = ""; sprintf(h, "GRID MIC FINEST;\n");  cmd += h;
       }

    // Go through the used layers (this avoids problems with wires on different
    // layers that are selected at the same coordinates):
    for (int u = LAYER_TOP; u <= LAYER_BOTTOM; u++)
        Move(u);
    Move(LAYER_VIAS);

    // Reactivate the active layers:
    h = ""; sprintf(h, "DISPLAY"); cmd += h;
    for (int j = 1; j < 256; j++)
        if (ActiveLayers[j]) {
           h = ""; sprintf(h, " %d", j); cmd += h;
           }
    h = ""; sprintf(h, ";\n"); cmd += h;

    // Snap the elements:
    B.elements(E) {
      Status = "Element: "+E.name; dlgRedisplay();
      if (u2unit(E.x) != snap(E.x) || u2unit(E.y) != snap(E.y)) {
         if (show_script) {
            h = ""; sprintf(h, "SHOW %s;\n", E.name); cmd += h;
            }
         h = ""; sprintf(h, "MOVE %s (%f %f);\n", E.name, snap(E.x), snap(E.y)); cmd += h;
         }
      }

    h = ""; sprintf(h, "GRID LAST;\n"); cmd += h;
    }
  }

//---- main ----------------------------------------------------------------------

if (!board) {
   dlgMessageBox(usage + "<hr><b>ERROR: No board!</b><p>\nThis program can only work in the board editor.");
   exit(1);
   }

dlgDialog("Snap Packages/Wires/Vias") {
  dlgHBoxLayout {
  dlgHBoxLayout {
      dlgGroup("Unit") {
        dlgRadioButton("&inch", unit);
        dlgRadioButton("&mil",  unit);
        dlgRadioButton("&mm",   unit);
        dlgRadioButton("&mic",  unit);
        dlgSpacing(20);
        dlgLabel("Snap grid ");
        dlgRealEdit(GridDist, 0.0001, 1000);
        }
      }
    dlgSpacing(10);
    dlgVBoxLayout {
      dlgSpacing(10);
      dlgCheckBox("&Show script", show_script);
      dlgLabel(Status, 1);
      dlgHBoxLayout {
        dlgPushButton("+&Snap")   {Status = "Busy...";
                                   dlgRedisplay();
                                   snap_to_grid();
                                   dlgAccept();
                                   }
        dlgPushButton("-&Cancel") exit(0);
        }
      dlgSpacing(7);
      }
    }
  };

Result = 1;
if (show_script) {
   Result = dlgDialog("Edit Commands") {
   dlgVBoxLayout {
     dlgLabel("Edit only if you are sure what you do!");
     dlgTextEdit(cmd);
        }
   dlgHBoxLayout {
        dlgPushButton("+Execute")   {snap_to_grid(); dlgAccept();}
        dlgPushButton("-Quit") dlgReject();
        dlgPushButton("+Save") {
                               string dest = dlgFileSave("Save Script File", get_project_path()+"snap.scr", "*.scr");
                               if (dest != "") output(dest, "wt") {printf(cmd);}
                               }
        }
     };
   }
if (!Result) exit(0);
exit(cmd);