-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_dxf_polygons.ulp
307 lines (248 loc) · 7.96 KB
/
import_dxf_polygons.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
/**********************************************************************
** **
** Filename: import_dxf_polygons.ulp **
** **
** Author: Tim Ruetz **
** tim at caiaq.de **
** **
** This ULP imports polylines and splines from DXF files **
** Use it for importing vectorized logos, fonts etc. **
** Arcs, circles, splines, curves are not supported yet. **
** Since this is a very simple, rudimentary script it just uses **
** straight lines as approximation. **
** Please use a vector graphics editor to edit/refine you DXF file: **
** - add as many as needed vertexes to the shape **
** - convert all curves to straight lines **
** - offset traces to inner side to compensate pen width **
** - no negative shapes are supported by eagle, so create openings **
** to connect negative shapes to outside (see example DXF) **
** **
***********************************************************************
** **
** Legal issues: This program is provided as it is. Without any **
** warranties of any kind of data lose or damages. **
** **
** Feel free to modify and improve this program and let me know. **
** **
** Version: 0.1 **
** Date: 14.08.2008 **
** **
***********************************************************************
*/
#usage "<b>Simple DXF Polyline Import V0.1</b>\n"
"<p>"
"Imports and scales (only!) POLYLINE and SPLINE entries in DXF files."
"Splines are not drawn as splines but as stright lines!"
"<author>Author: Tim Ruetz ([email protected])</author>"
string dxf_filename;
int dxf_len;
int i, j;
string dxf_filedata[];
int dxf_code;
string dxf_value;
int state = 0;
real vertex_x;
real vertex_y;
real vertex_x_buf;
real vertex_y_buf;
real scale = 1.0;
real pen_width = 0.2; // mm
real angle = 0.0; // degrees
real rot_s; // rotation sinus
real rot_c; // rotation cosinus
int mirror_flag = 0; // 1=mirror
real xmin, xmax;
real ymin, ymax;
real width_orig;
real height_orig;
real w, h;
string parse_msg = "";
string l;
string cmd;
string script_out;
int layer_cnt;
int layer_sel=0;
string layer_list[];
int pen_sel = 1;
string pen_list[] = { "0.1", "0.2", "0.3", "0.4", "0.5", "1.0", "1.5", "2.0" };
void find_layers()
{
if (library) {
layer_cnt=0;
library(L) {
L.layers(LA) {
if (LA.visible)
{
if ((LA.number < 17 || LA.number > 19) && (LA.number < 23 || LA.number > 25) && LA.number != 28)
{
if (LA.number == 94) layer_sel = layer_cnt;
sprintf(layer_list[layer_cnt++], "%3d - %s", LA.number, LA.name);
}
}
}
}
}
if (board) {
layer_cnt=0;
board(B) {
B.layers(LA) {
if (LA.visible)
sprintf(layer_list[layer_cnt++], "%3d - %s", LA.number, LA.name);
}
}
}
if (schematic) {
layer_cnt=0;
schematic(S) {
S.layers(LA) {
if (LA.visible)
{
if (LA.number == 94) layer_sel = layer_cnt;
sprintf(layer_list[layer_cnt++], "%3d - %s", LA.number, LA.name);
}
}
}
}
}
void update_script()
{
script_out = "";
// script_out += "SET UNDO_LOG OFF;\n";
script_out += "GRID MM;\n";
script_out += "SET WIDTH "+pen_list[pen_sel]+";\n";
script_out += "LAYER "+strsub(layer_list[layer_sel],0, 3)+";\n";
script_out += "SET WIRE_BEND 2;\n";
script_out += cmd;
// script_out += "SET UNDO_LOG ON;\n";
}
void parse_dxf()
{
string cmd_temp = "";
int vertexes = 0;
cmd = "";
xmin = ymin = 100000.0;
xmax = ymax = -100000.0;
rot_s = sin(angle / 180.0 * PI);
rot_c = cos(angle / 180.0 * PI);
for (i=0; i<dxf_len; i+=2)
{
dxf_code = strtol(dxf_filedata[i]);
dxf_value = dxf_filedata[i+1];
if (dxf_code == 0)
{
if (state > 2 && dxf_value != "VERTEX")
{
if (vertexes > 2) // only draw polygons with at least 3 vertexes
{
cmd += cmd_temp+";\n";
}
state = 0;
cmd_temp="";
vertexes = 0;
}
if (dxf_value == "POLYLINE")
state = 1;
if (dxf_value == "VERTEX" && state==1)
{
cmd_temp += "POLYGON";
state=2;
}
if (dxf_value == "SPLINE")
{
cmd_temp += "POLYGON";
state = 2;
}
}
if (state >= 2)
{
if (dxf_code == 10)
vertex_x = strtod(dxf_value) * scale;
if (dxf_code == 20)
{
state = 3;
vertex_y = strtod(dxf_value) * scale;
if (vertex_x > xmax) xmax = vertex_x;
if (vertex_x < xmin) xmin = vertex_x;
if (vertex_y > ymax) ymax = vertex_y;
if (vertex_y < ymin) ymin = vertex_y;
if (vertex_x != vertex_x_buf || vertex_y != vertex_y_buf)
{
vertex_x_buf = vertex_x;
vertex_y_buf = vertex_y;
if (mirror_flag)
vertex_x = -vertex_x;
sprintf(l, " (%5.3f %5.3f)", (vertex_x * rot_c - vertex_y * rot_s), (vertex_y * rot_c + vertex_x * rot_s));
cmd_temp += l;
vertexes++;
}
}
}
}
sprintf(parse_msg, "Width: %5.3f mm\nHeight: %5.3f mm\n\nLeft: %5.3f mm\nRight: %5.3f mm\n\nTop: %5.3f mm\nBottom: %5.3f mm\n\n", xmax-xmin, ymax-ymin, xmin, xmax, ymin, ymax);
if (scale==1.0) // at least the first time
{
w = width_orig = xmax-xmin;
h = height_orig = ymax-ymin;
}
update_script();
}
//
// main()
//
find_layers();
//dxf_filename="/temp/test.dxf";
dxf_filename = dlgFileOpen("DXF file to import", ".", "DXF files (*.dxf);;All files (*)");
if (!dxf_filename)
exit (0);
dxf_len = fileread(dxf_filedata, dxf_filename);
if (dxf_len<1)
exit(0);
parse_dxf();
// dialog
int result = dlgDialog("Simple DXF Polyline Import") {
dlgTabWidget {
dlgTabPage("Settings")
{
dlgGridLayout {
dlgCell(0, 0) dlgLabel("Scale");
dlgCell(0, 1) dlgRealEdit(scale, 0.0, 999.0);
dlgCell(0, 2) dlgPushButton("+Rescale") parse_dxf();
dlgCell(1, 0) dlgLabel("Scale to width [mm]");
dlgCell(1, 1) dlgRealEdit(w, 0.1, 9999.0);
dlgCell(1, 2) dlgPushButton("+Scale to width") { scale = w/width_orig; h=scale*height_orig; parse_dxf(); }
dlgCell(2, 0) dlgLabel("Scale to height [mm]");
dlgCell(2, 1) dlgRealEdit(h, 0.1, 9999.0);
dlgCell(2, 2) dlgPushButton("+Scale to height") { scale = h/height_orig; w=scale*width_orig; parse_dxf(); }
dlgCell(3, 0) dlgLabel("Info");
dlgCell(3, 1, 3, 2) dlgTextView(parse_msg);;
dlgCell(4, 0) dlgLabel("Import to layer");
dlgCell(4, 1, 4, 2) dlgComboBox(layer_list, layer_sel) update_script();
dlgCell(5, 0) dlgLabel("Pen width [mm]");
dlgCell(5, 1, 5, 2) dlgComboBox(pen_list, pen_sel) update_script();
dlgCell(6, 0) dlgLabel("Angle (ccw)");
dlgCell(6, 1) dlgRealEdit(angle, 0.0, 360.0) parse_dxf();
dlgCell(6, 2) dlgCheckBox("Mirror", mirror_flag) parse_dxf();
}
}
dlgTabPage("Generated Script")
{
dlgTextView(script_out);
}
}
dlgGridLayout {
dlgCell(0, 0) dlgPushButton("-Cancel") dlgReject();
dlgCell(0, 3) {
dlgPushButton("+Execute")
{
parse_dxf();
dlgAccept();
}
}
}
};
if (result == 0)
exit(0);
else
{
exit(script_out);
}