forked from robertstarr/ulp_user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod-dup-place.ulp
296 lines (238 loc) · 7.55 KB
/
mod-dup-place.ulp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#usage "<b>Duplicate the placement of a module</b>\n"
"<p>"
"This program duplicates the board placement of a module from a source hierarchical module. "
"The ULP displays and allows the user to select the source module placement to duplicate "
"from and desitination module is duplicated with X/Y offsets currently set.<p>"
"<author>http://www.bobstarr.net</author>"
//////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2014, Robert E. Starr (http://www.bobstarr.net)
//
// REVISION HISTORY:
//
// v1.00 - 12/11/2014 (RES) - Initial Release
//
//////////////////////////////////////////////////////////////////////////////
//
// THIS PROGRAM IS PROVIDED AS IS AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESSED OR IMPLIED. IF YOU DON'T LIKE IT, DON'T USE IT!
//
//////////////////////////////////////////////////////////////////////////////
string Version = "1.00";
string fileName;
string configFilename = "mod-dup-place.cfg";
// Variables below are stored/recalled from config file
real offsetX = 0.0;
real offsetY = 0.0;
int fScriptExec = 1; // execute script automatically after generating
int nSelSrc, nSrcCnt;
int nSelDest, nDestCnt;
int nIDCount;
string strSrc, strSrcMod[];
string strDest, strDestMod[];
//////////////////////////////////////////////////////////////////////////////
void LoadConfigSettings()
{
string a[];
int n;
// Should be 5 lines of data in config file
if ((n = fileread(a, filedir(argv[0])+configFilename)) == 3)
{
int i = 0;
offsetX = strtod(a[i++]);
offsetY = strtod(a[i++]);
fScriptExec = strtol(a[i++]);
}
}
void SaveConfigSettings()
{
// save the config settings
output(filedir(argv[0])+configFilename, "wt")
{
printf("%f\n", offsetX);
printf("%f\n", offsetY);
printf("%d\n", fScriptExec);
}
}
int MoveModule(UL_BOARD B)
{
int i;
int cnt;
real xpos[], ypos[], angle[];
string pid[], mirror[];
cnt = 0;
/* First collect x/y, angle and mirror data for the source module */
printf("#\n# Duplicate module '%s' placement from '%s'\n#\n", strDest, strSrc);
B.elements(E)
{
string key[];
if (strsplit(key, E.name, ':') == 2)
{
if (key[0] == strSrc)
{
real x, y;
switch (B.grid.unit)
{
case GRID_UNIT_MIC:
x = u2mic(E.y);
y = u2mic(E.y);
break;
case GRID_UNIT_MIL:
x = u2mil(E.x);
y = u2mil(E.y);
break;
case GRID_UNIT_MM:
x = u2mm (E.x);
y = u2mm (E.y);
break;
case GRID_UNIT_INCH:
x = u2inch(E.x);
y = u2inch(E.y);
break;
}
xpos[cnt] = x;
ypos[cnt] = y;
angle[cnt] = E.angle;
pid[cnt] = key[1];
if (E.mirror)
mirror[cnt] = key[0];
/* printf("# %s|%f|%f|%f\n", E.name, x, y, E.angle); */
++cnt;
}
}
}
/* Now move the destination module to match the source placement with an offset */
for (i=0; i < cnt; i++)
{
string name;
sprintf(name, "%s:%s", strDest, pid[i]);
printf("MOVE %s (%5.4f %5.4f);\n", name, xpos[i] + offsetX, ypos[i] + offsetY);
printf("ROTATE =R%f %s;\n", angle[i], name);
if (mirror[i])
printf("MIRROR %s;\n", name);
}
return cnt;
}
void OnComboChangeSource()
{
strSrc = strSrcMod[nSelSrc];
}
void OnComboChangeDest()
{
strDest = strDestMod[nSelDest];
}
int MainAppDialog()
{
int nResult = dlgDialog("Module Duplicate Placement")
{
string title = "Module Duplicate Placement v" + Version;
dlgVBoxLayout
{
dlgSpacing(5);
dlgGroup(title)
{
dlgVBoxLayout
{
dlgGroup("Source/Destination")
{
dlgGridLayout
{
dlgCell(1,0) dlgLabel("Source module to duplicate placement from");
dlgCell(1,1) dlgComboBox(strSrcMod, nSelSrc) OnComboChangeSource();
dlgCell(2,0) dlgLabel("Destination module to place with offset");
dlgCell(2,1) dlgComboBox(strDestMod, nSelDest) OnComboChangeDest();
}
}
dlgGridLayout
{
dlgCell(1,0) dlgLabel("X-Offset");
dlgCell(1,1) dlgRealEdit(offsetX);
dlgCell(2,0) dlgLabel("Y-Offset");
dlgCell(2,1) dlgRealEdit(offsetY);
}
}
}
}
dlgHBoxLayout
{
dlgPushButton("OK") dlgAccept();
dlgPushButton("-Cancel") dlgReject();
dlgCheckBox("&Execute Script", fScriptExec);
}
};
return nResult;
}
//////////////////////////////////////////////////////////////////////////////
// Script Entry Point
if (board) board(B)
{
LoadConfigSettings();
project.schematic(S)
{
S.allparts(P)
{
// Module instances
if (P.modulepart)
{
P.modulepart.instances(I)
{
if (P.device.package)
{
string key[];
if (strsplit(key, P.name, ':') == 2)
{
/* Collect unique module names */
if (!lookup(strSrcMod, key[0], 0))
{
strSrcMod[nSrcCnt++] = key[0];
strDestMod[nDestCnt++] = key[0];
}
}
}
}
}
}
}
if (nDestCnt < 2)
{
dlgMessageBox("\n Must have at least two module instances \n");
exit(0);
}
nSelSrc = 0;
strSrc = strSrcMod[nSelSrc];
nSelDest = 1;
strDest = strDestMod[nSelDest];
if (MainAppDialog() == 0)
{
SaveConfigSettings();
exit(0);
}
fileName = filesetext(B.name, "_ModDupPlace.scr");
output(fileName)
{
printf("#\n# Generated by mod-dup-place.ulp\n#\n");
printf("# '%s at %s;\n", B.name, t2string(time()));
printf("# '%s;\n#\n", EAGLE_SIGNATURE);
printf ("GRID ");
if (B.grid.unit == GRID_UNIT_MIC)
printf ("MIC\n");
else if (B.grid.unit == GRID_UNIT_MM)
printf ("MM\n");
else if (B.grid.unit == GRID_UNIT_MIL)
printf ("MIL\n");
else
printf ("INCH\n");
int cnt = 0;
cnt = MoveModule(B);
SaveConfigSettings();
if (fScriptExec && cnt)
exit("SCRIPT '" + fileName + "';");
else
exit(0);
}
}
else
{
dlgMessageBox("\n Start this ULP in a Board \n");
exit (0);
}