forked from robertstarr/ulp_user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dxfimport.ulp
411 lines (366 loc) · 13 KB
/
dxfimport.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#usage "<b>DXFimport</b> - v1.01<p>\n"
"This ULP can import DXF files that include polylines.\n"
"Browse to the DXF file, setup your origins, input units, and linewidth then click convert.\n"
"The ULP will then show you the script in case you want to change the layer the input will be written to.\n"
"Tested in both version 5 and version 6.\n"
"Written by Jorge Garcia of Cadsoft Computer, heavily based off of Hank Wallace's DXF2SCR program.\n"
"With special thanks to Robert Starr and Alfred Zaffran."
/* Below I have retained Mr. Wallace's original "licensing" statement for two reasons:
1. As a sign of respect for the original author of the code from which this ULP was
developed and without which it would have not been possible to develop this ULP.
2. This is quite possibly the funniest licensing statement any one will ever read.
Officially though, I will license this under the MIT open software license copied below:
Copyright (c) 2012 Newark, Premier Farnell DBA Cadsoft Computer
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.
Jorge Garcia
03-Mar-2012
*/
/* DXF2SCR.C
Program that crunches a DXF file with circles, arcs and lines into an
Eagle script file, useful for translating board outlines.
Hank Wallace 20-Mar-03
Revision 20-Mar-03
There are two licensing options for this program:
1. If you are a fan of the GNU license, and you think you
understand it, and you think it is reasonable, and you believe
that 'free to download' means 'high quality' or 'low maintenance
cost', and you are a computer geek who cannot get a date who
measures the value of his/her existence by the number of
marginally useful command line options you can stuff into one
program on a lonely Friday night, and you write C source code in
such a manner that there are more #define'd constants and
conditional compilation directives than there are actual C
source statements, and you attempt to make your programs so
'portable' that they runs on no machine whatsoever without
modification, then you have no license to use this software. Get
a life.
2. If you one of the other 6 billion people on planet Earth,
this program source code and executable is free for your use
without restriction, but NO WARRANTY OR SUPPORT IS GIVEN.
This program compiles under Microsoft's command line compiler,
version 12.00.
*/
string Version = "v1.01";
string ConfigFileName = "dxfimport.cfg";
string Help = "<b>DXFimport "+Version+ "</b>\n <p>This ULP will import a DXF file into EAGLE. Use the browse button to find the DXF file, make sure to set the units to whatever units are used by the DXF file. Set the width, Xorg, and Yorg in whatever units you selected. Then click the convert button, the generated script will be shown for editing, once it's adjusted to your liking click Run.</p>";
string dxf(string dxffile, int units, real xorigin, real yorigin, real lwidth)
{
string s[]; //This string array will store the DXF file
string out, temp; //out is the script as generated by this function temp is a place holder
int
i, //index variable for xa and ya
p, //dummy variable for for loop
m, //dummy variable for main while loop
nlines, //number of lines in the text file
state,
lines,
arcs,
circles,
polylines, //NEW
linewidth=1;
real x,y,x1,y1,x2,y2,x3,y3,r,theta1,theta3,xorg,yorg,xa[],ya[],s_factor;
//xa and ya arrays hold the possible vertexes of a polyline
//This code sets up the scale factor based on wether the input file is metric or inches
if (units == 1)
{
s_factor = 39.370079; //Convert mm to mils
}
else
{
s_factor = 1000; //Convert in to mils
}
linewidth = lwidth * s_factor;
xorg = xorigin * s_factor;
yorg = yorigin * s_factor;
out += "# dxfimport generated script file.\n";
out += "Grid mil 1 off;\n";
out += "Set Wire_Bend 2;\n";
out += "Layer Dimension;\n";
out += "Change Font Vector;\n";
state = 0;
lines = arcs = circles = polylines = 0;
nlines = fileread(s, dxffile);
for (m = 0;m < nlines;m++)
{
switch (state)
{
case 0: // scanning for SECTION
if (strstr(s[m],"SECTION") != -1)
{
state=1;
}
break;
case 1: // scanning for ENTITIES
if (strstr(s[m],"ENDSEC") != -1)
{
state=0;
}
if (strstr(s[m],"ENTITIES") != -1)
{
state=2;
}
break;
case 2: // scanning for LINE, ARC, CIRCLE or POLYLINE
if (strstr(s[m],"ENDSEC") != -1)
{
state=0;
}
if (strstr(s[m],"LINE") != -1 && strstr(s[m],"POLYLINE") == -1)
{
state=3;
lines++;
}
if (strstr(s[m],"ARC") != -1)
{
state=4;
arcs++;
}
if (strstr(s[m],"CIRCLE") != -1)
{
state=5;
circles++;
}
if (strstr(s[m],"POLYLINE") != -1)
{
i=0;
state=6;
polylines++;
}
break;
case 3: // absorbing LINE
// LINE 10, 20, 30 (start point), 11, 21, 31 (end point).
if (strstr(s[m], " 10") != -1)
{
x=s_factor*strtod(s[m+1]);
}
if (strstr(s[m], " 20") != -1)
{
y=s_factor*strtod(s[m+1]);
}
if (strstr(s[m], " 11") != -1)
{
x1=s_factor*strtod(s[m+1]);
}
if (strstr(s[m], " 21") != -1)
{
y1=s_factor*strtod(s[m+1]);
sprintf(temp, "Wire %d (%0.2f %0.2f) (%0.2f %0.2f);\n",
linewidth,x+xorg,y+yorg,x1+xorg,y1+yorg);
out += temp; //Write to script
state=2;
}
break;
case 4: // absorbing ARC
// ARC 10, 20, 30 (center), 40 (radius), 50 (start angle), 51 (end
if (strstr(s[m], " 10") != -1)
{
x=s_factor*strtod(s[m+1]);
}
if (strstr(s[m], " 20") != -1)
{
y=s_factor*strtod(s[m+1]);
}
if (strstr(s[m], " 40") != -1)
{
r=s_factor*strtod(s[m+1]);
}
if (strstr(s[m], " 50") != -1)
{
theta1=strtod(s[m+1]);
theta1*=(PI/180);
}
if (strstr(s[m], " 51") != -1)
{
theta3=strtod(s[m+1]);
theta3*=(PI/180);
// compute Eagle arc parameters from DXF arc params
x1=r*cos(theta1)+x;
y1=r*sin(theta1)+y;
x2=x1-2*r*cos(theta1);
y2=y1-2*r*sin(theta1);
x3=r*cos(theta3)+x;
y3=r*sin(theta3)+y;
sprintf(temp, "Arc CCW %d (%0.2f %0.2f) (%0.2f %0.2f) (%0.2f %0.2f);\n",
linewidth,x1+xorg,y1+yorg,x2+xorg,y2+yorg,x3+xorg,y3+yorg);
out += temp;
state=2;
}
break;
case 5: // absorbing CIRCLE
// CIRCLE 10, 20, 30 (center), 40 (radius).
if (strstr(s[m], " 10") != -1)
{
x=s_factor*strtod(s[m+1]);
}
if (strstr(s[m], " 20") != -1)
{
y=s_factor*strtod(s[m+1]);
}
if (strstr(s[m], " 40") != -1)
{
y1=s_factor*strtod(s[m+1]);
sprintf(temp, "Circle %d (%0.2f %0.2f) (%0.2f %0.2f);\n",
linewidth,x+xorg,y+yorg,x+xorg,y+y1+yorg);
out += temp;
state=2;
}
break;
case 6: // absorbing POLYLINE -NEW
// VERTEX
if (strstr(s[m], "VERTEX") != -1)
{
state=7;
}
if (strstr(s[m], "SEQEND") != -1)
{
for (p=0; p < i-1;p++)
{
sprintf(temp, "Wire %d (%0.2f %0.2f) (%0.2f %0.2f);\n",
linewidth,xa[p]+xorg,ya[p]+yorg,xa[p+1]+xorg,ya[p+1]+yorg);
out += temp;
}
//n points give n-1 lines that's why there's an i-1 in the conditional
state=2;
}
break;
case 7: // absorbing VERTEX -NEW
// 10 (x-value) 20 (y-value)
if (strstr(s[m], " 10") != -1)
{
xa[i] = s_factor*strtod(s[m+1]);
}
if (strstr(s[m], " 20") != -1)
{
ya[i] = s_factor*strtod(s[m+1]);
i++; //The x coordinate is always first so I can increment here.
state=6; //Look for the next vertex of the polyline
}
break;
}
}
sprintf(temp,"Window Fit;\n");
out += temp;
sprintf(temp,"# lines=%d, arcs=%d, circles=%d, polylines=%d\n",lines,arcs,circles,polylines);
out += temp;
return out;
}
//GUI code
int in_unit = 0;
string script;
string fileName;
real Xorg=0.0, Yorg=0.0, lwidth=0.001;
// 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;
}
// load the last config settings saved
void LoadConfigSettings()
{
string a[];
string rf[];
int nrf = fileglob(rf, filedir(argv[0])+ConfigFileName);
if (nrf)
nrf = fileread(a, rf[0]);
if (nrf == 5)
{
int i = 0;
fileName = a[i++];
Xorg = strtod(a[i++]);
Yorg = strtod(a[i++]);
lwidth = strtod(a[i++]);
in_unit = strtol(a[i++]);
}
}
void SaveConfigSettings()
{
// save the config settings
output(filedir(argv[0])+ConfigFileName, "wt")
{
printf("%s\n", fileName);
printf("%f\n", Xorg);
printf("%f\n", Yorg);
printf("%f\n", lwidth);
printf("%d\n", in_unit);
}
}
void runscript(string scr) //Write script file and run it
{
string filepath = get_project_path() + "dxfimp.scr";
SaveConfigSettings();
output(filepath,"wt")
{
printf(scr);
}
exit("SCRIPT '" + filepath + "'");
}
void checkscript(void) //This allows the user to inspect the script and make changes
{
script = dxf(fileName, in_unit, Xorg, Yorg, lwidth);
int Result = dlgDialog("Run Script")
{
dlgTextEdit(script);
dlgHBoxLayout
{
dlgPushButton("Run") runscript(script);
dlgPushButton("Cancel") dlgReject();
}
};
}
int Result = dlgDialog("DXFIMPORT - "+Version)
{
// load the last filename config settings
LoadConfigSettings();
dlgHBoxLayout
{
//dlgStretch(1);
dlgLabel(Help);
//dlgStretch(1);
}
dlgHBoxLayout
{
dlgLabel("File &name:");
dlgStringEdit(fileName);
dlgPushButton("Bro&wse")
{
fileName = dlgFileOpen("Select a file", fileName, "*.dxf");
}
}
dlgHBoxLayout
{
dlgGroup("Input Units")
{
dlgRadioButton("Imperial(in)", in_unit); //in_unit = 0
dlgRadioButton("Metric(mm)", in_unit); //in_unit = 1
}
dlgGridLayout
{
dlgCell(0, 0) dlgLabel("Xorg");
dlgCell(1, 0) dlgLabel("Yorg");
dlgCell(0, 1) dlgRealEdit(Xorg);
dlgCell(1, 1) dlgRealEdit(Yorg);
dlgCell(2, 0) dlgLabel("Width");
dlgCell(2, 1) dlgRealEdit(lwidth);
dlgCell(1, 2) dlgPushButton("OK") checkscript();
dlgCell(2, 2) dlgPushButton("-Cancel") dlgReject();
}
}
};