-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare-coil.ulp
263 lines (244 loc) · 9.46 KB
/
square-coil.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
/*
MIT License
Copyright (c) 2021 ngammarano
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#usage "This ULP places a square coil in a board.<p>"
"This ULP must be started from a board.<p>"
"The parameters of the coil are:<ul>"
"<li>the number of turns 'n'</li>"
"<li>the wire width 'W' in mm</li>"
"<li>the distance between wires 'S' in mm</li>"
"<li>the internal square length 'din' in mm</li></ul>"
"A via is placed to connect top and bottom layers of the coil as shown in the image. You must specify:<ul>"
"<li>the via diameter in mm</li>"
"<li>the via drill diameter in mm</li></ul>"
"The starting point of the square coil is the lower left corner of the top layer of the coil. The coordinates you want this point to be must be specified:<ul>"
"<li>the horizontal offset x in mm</li>"
"<li>the vertical offset y in mm</li></ul>"
"The signal name of the coil (top, bottom layers of the coil and the via) must also be specified.<p>"
"<author>Author: <a href='https://github.com/ngammarano/square-coil'> Nicolas Gammarano</a></author>";
/**
* Parameters of the dialog box.
*/
int n = 5; // number of turns
real w = 2; // wire width in mm
real s = 0.5; // distance between wires in mm
real din = 5; // internal square length in mm
real diameter = 1.5; // diameter of the vias in mm
real drill = 1.0; // drill diameter of the vias in mm
int layer = 1; // layer number
real offsetX = 0; // horizontal offset in mm
real offsetY = 0; // vertical offset in mm
string signalName = "V_COIL"; // signal name
/**
* Variables to execute a command.
*/
string cmd = "";
string strCmd;
/**
* Function executed when pressing "OK" in the dialog box.
*/
void doit(void) {
// Change grid to mm.
cmd += "grid mm;\n";
// Change layer to 1 (top).
cmd += "layer 1;\n";
// Start drawing the polygon (width: 0).
cmd += "polygon " + signalName + " 0\n";
// Length of the outer square.
real dout = din + 2*(n*w + (n-1)*s);
// Coordinates of the current point.
real x = offsetX;
real y = offsetY;
// Place the first point.
sprintf(strCmd, "(%4.2f %4.2f)\n", x, y);
cmd += strCmd;
// First half of the square coil.
// Currently changing the x coordinate (1) or the y coordinate (0).
int changeX = 1;
// Currently adding (1) or subtracting (-1) the corresponding coordinate.
int sign = 1;
int signCounter = 0;
// Distance of the current segment (d = dout - m*(w+s)).
int m = 0;
int mCounter = - 1;
// Point index.
int index;
for (index = 0; index < 4*n; index++) {
if (changeX == 1) {
x = x + sign*(dout - m*(w+s));
changeX = 0;
} else {
y = y + sign*(dout - m*(w+s));
changeX = 1;
};
signCounter++;
if (signCounter >= 2) {
signCounter = 0;
sign = -sign;
};
mCounter++;
if (mCounter >= 2) {
mCounter = 0;
m++;
};
sprintf(strCmd, "(%4.2f %4.2f)\n", x, y);
cmd += strCmd;
};
// Inner endpoint of the wire.
x = x + w;
sprintf(strCmd, "(%4.2f %4.2f)\n", x, y);
cmd += strCmd;
y = y + din - s;
sprintf(strCmd, "(%4.2f %4.2f)\n", x, y);
cmd += strCmd;
// Second half of the square coil.
// Currently changing the x coordinate (1) or the y coordinate (0).
changeX = 1;
// Currently adding (1) or subtracting (-1) the corresponding coordinate.
sign = 1;
signCounter = 1;
// Distance of the current segment (d = din + m*(w+s)).
m = 0;
mCounter = 0;
// Point index.
for (index = 0; index < 4*n-2; index++) {
if (changeX == 1) {
x = x + sign*(din + m*(w+s));
changeX = 0;
} else {
y = y + sign*(din + m*(w+s));
changeX = 1;
};
signCounter++;
if (signCounter >= 2) {
signCounter = 0;
sign = -sign;
};
mCounter++;
if (mCounter >= 2) {
mCounter = 0;
m++;
};
sprintf(strCmd, "(%4.2f %4.2f)\n", x, y);
cmd += strCmd;
};
x = x - (din + m*(w+s) - s);
sprintf(strCmd, "(%4.2f %4.2f)\n", x, y);
cmd += strCmd;
y = y - w;
sprintf(strCmd, "(%4.2f %4.2f)\n", x, y);
cmd += strCmd;
// Final point of the polygon (same as starting point).
sprintf(strCmd, "(%4.2f %4.2f);\n", offsetX, offsetY);
cmd += strCmd;
// Change layer to 16 (bottom).
cmd += "layer 16;\n";
// Start drawing the polygon (width: 0).
cmd += "polygon " + signalName + " 0\n";
x = offsetX - s;
y = offsetY;
sprintf(strCmd, "(%4.2f %4.2f)\n", x, y);
cmd += strCmd;
y = y + n*(w+s);
sprintf(strCmd, "(%4.2f %4.2f)\n", x, y);
cmd += strCmd;
x = x + n*(w+s);
sprintf(strCmd, "(%4.2f %4.2f)\n", x, y);
cmd += strCmd;
y = y + w;
sprintf(strCmd, "(%4.2f %4.2f)\n", x, y);
cmd += strCmd;
x = x - n*(w+s) - w;
sprintf(strCmd, "(%4.2f %4.2f)\n", x, y);
cmd += strCmd;
y = y - n*(w+s) - w;
sprintf(strCmd, "(%4.2f %4.2f)\n", x, y);
cmd += strCmd;
x = x + w;
sprintf(strCmd, "(%4.2f %4.2f);\n", x, y);
cmd += strCmd;
// Change layer to 1 (top).
cmd += "layer 1;\n";
// Change the drill diameter.
sprintf(strCmd, "change drill %4.2f;\n", drill);
cmd += strCmd;
// Place a via to connect top with bottom.
cmd += "via '" + signalName + "' ";
sprintf(strCmd, "%4.2f round 1-16 (%4.2f %4.2f);\n", diameter, offsetX + (n-1)*(w+s)+w/2, offsetY + n*(w+s)+w/2);
cmd += strCmd;
exit(cmd);
}
/**
* Dialog box.
*/
dlgDialog("Copper square coil") {
dlgHBoxLayout {
dlgVBoxLayout {
dlgGridLayout {
dlgCell(1, 1) dlgLabel("<hr>");
dlgCell(1, 2) dlgLabel("<hr>");
dlgCell(2, 1) dlgLabel("Turns 'n'"); // number of turns
dlgCell(2, 2) dlgIntEdit(n, 1, 100);
dlgCell(3, 1) dlgLabel("Wire width 'W'"); // track width
dlgCell(3, 2) dlgRealEdit(w, 0.01, 25.0);
dlgCell(4, 1) dlgLabel("Distance between wires 'S'"); // distance between tracks
dlgCell(4, 2) dlgRealEdit(s, 0.01, 25.0);
dlgCell(5, 1) dlgLabel("Internal square length 'din'"); // internal square length
dlgCell(5, 2) dlgRealEdit(din, 0.01, 25.0);
dlgCell(6, 1) dlgLabel("<hr>");
dlgCell(6, 2) dlgLabel("<hr>");
dlgCell(7, 1) dlgLabel("Via diameter"); // via diameter
dlgCell(7, 2) dlgRealEdit(diameter, 0.1, 10.0);
dlgCell(8, 1) dlgLabel("Via drill diameter"); // via drill diameter
dlgCell(8, 2) dlgRealEdit(drill, 0.1, 10.0);
dlgCell(9, 1) dlgLabel("<hr>");
dlgCell(9, 2) dlgLabel("<hr>");
dlgCell(10, 1) dlgLabel("Offset x"); // place with horizontal offset
dlgCell(10, 2) dlgRealEdit(offsetX);
dlgCell(11, 1) dlgLabel("Offset y"); // place with vertical offset
dlgCell(11, 2) dlgRealEdit(offsetY);
dlgCell(12, 1) dlgLabel("<hr>");
dlgCell(12, 2) dlgLabel("<hr>");
dlgCell(13, 1) dlgLabel("Signal name"); // signal name
dlgCell(13, 2) dlgStringEdit(signalName);
dlgCell(14, 1) dlgLabel("<hr>");
dlgCell(14, 2) dlgLabel("<hr>");
}
dlgStretch(1);
}
dlgVBoxLayout {
dlgLabel("<img src=square-coil-ulp.bmp>");
}
}
dlgLabel("<qt>All measures are in <b>mm</b>.</qt>");
dlgHBoxLayout {
dlgPushButton("+&OK") {
dlgAccept();
doit();
}
dlgPushButton("-&Cancel") {
dlgReject();
exit(0);
}
dlgStretch(1);
dlgPushButton("&Help") {
dlgMessageBox(usage, "Ok");
}
}
};