File: cam2image.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 (556 lines) | stat: -rw-r--r-- 16,883 bytes parent folder | download | duplicates (5)
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#usage "<b>Convert a CAM job to a script to export IMAGE data</b>"
       "<p>"
       "<nobr>Usage: RUN cam2image [ <i>filename monochrome resolution image type</i>]"
       "<p>"
       "Tip: Assign a funktion key with"
       "<br>"
       "<tt>ASSIGN Shift+Ctrl+Alt+I 'run cam2image;';</tt>"
       "<br>"
       "or <tt>ASSIGN Shift+Ctrl+Alt+I 'run cam2image myjob.cam monochrome 150 .bmp;';</tt>"
       "<br>"
       "or <tt>ASSIGN Shift+Ctrl+Alt+I 'run cam2image myjob.cam color 300 .tif;';</tt>"
       "<p>"
       "<author>Author: support@cadsoft.de</author></nobr>"

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

// Revision 1. 14.02.2005 Exdendet with .tif format alf@cadsoft.de
// Revision 2. 23.02.2005 Use correct file name in CAM-Job-Section alf@cadsoft.de


// CAM-Job token
string tok_job = "[CAM Processor Job]";
string tok_Descrition = "Description=";
string description;
string descriptionLine[];
int    descrCnt;
string tok_Section = "Section=";
string tok_Sec_n   = "[Sec_";
string section[];
string tok_Name    = "Name=";
string sec_Name[];
string tok_Prompt  = "Prompt=";
string sec_Prompt[];
string tok_Out_put = "Output=";
string sec_Out_put[];

string file_name;

string tok_Sheet   = "Sheet=";
int    sec_Sheet[];
int sheetprint = 0;  // 0 = All, 1 = From To, 2 = This section defined, 3 = Actual

int sheetprint_from[];
int sheetprint_to[];
string actualsheet;

string tok_Layers  = "Layers=";
string sec_Layers[];
string sec_usedlayer[];
int    absolutUsedLayer = 0;

string lines[];
int    nLines;
string s;
string nu;
int    sx = 0;

int    lVisible[];
int    useLayer[];
string lNames[] = { " " };
int    lastsheet = 0;
string lastSH;

int    Resolution = 75; // DPI
string monochrome = "";
int    sel_monochrome = 0;


string filetype[] = {
                     ".bmp", // Windows-Bitmap-Datei
                     ".png", // Portable-Network-Graphics-Datei
                     ".pbm", // Portable-Bitmap-Datei
                     ".pgm", // Portable-Grayscale-Bitmap-Datei
                     ".ppm", // Portable-Pixelmap-Datei
                     ".xbm", // X-Bitmap-Datei
                     ".xpm", // X-Pixmap-Datei
                     ".tif"  // Tiff-Datei
                    };

int    selfiletype = 0;


string help = usage+ "<qt><b>This ULP converts a CAM Job to a Export IMAGE!</b><br>\n" +
              "If the layer list is empty after starting this ULP,<br>" +
              "you did not start it from the proper editor window (SCH/BRD).<br>" +
              "CAM jobs for the layout have to be started from the Layout Editor, " +
              "jobs for a schematic from the Schematic Editor. " +
              "CAM Jobs can be edited and saved by the CAM processor only.</qt>";

string ulp_path ;
char bkslash = '/';
int pos = strrchr(argv[0], bkslash);
if (pos >= 0) ulp_path = strsub(argv[0], 0, pos + 1);

string cmd = "";
int test = 0;
int Result = 0;

// File handling
int n = 0;
string text;
string CAMfileName;
int nBytes;
int align;
string CAMfile;

string Eagle ="<img src=eagle.bmp>";

// *** functions ***

void editsec(int sec) {
   string num;
   sprintf(num, "%d", sec);
   dlgDialog("Section Editor") {
      dlgLabel("Section " + num);
      dlgHBoxLayout {
         dlgPushButton("+OK") dlgAccept();
         dlgPushButton("-Cancel") dlgReject();
         dlgStretch(1);
         }
      };
   return;
}

void commandPrint(string sectab) {
   string h;
   string Path      = filedir(sectab);
   string File_Name = filesetext(filename(sectab), "");
   string Extension = fileext(sectab);
   string expfilename;

   if (Path) {   // check filename in CAM-Job *** 23.02.2005  alf@cadsoft.de
      if (!File_Name) {
         expfilename = Path + filesetext(filename(file_name), "") + Extension;
      }
      else {
         expfilename = Path + File_Name + Extension;
      }
   }
   else {
     expfilename = file_name + Extension;
   }
   sprintf(h, "EXPORT IMAGE '%s%s' %s %d;\n", expfilename, filetype[selfiletype], monochrome, Resolution);
   cmd += h;
   return;
   }

void viewDescript(void) {
   string ds;
   for (int n = 1 ; n < descrCnt; n++ ) {
      if (ds[0] = ' ') {
         ds = descriptionLine[n];
         ds[0] = ' ';
         descriptionLine[n] = ds;
         }
      }
   descriptionLine[0] = "<qt>" + descriptionLine[0] + "</qt>";
   ds = strjoin(descriptionLine, '\n');
   dlgMessageBox(ds, "OK");
   return;
}

void readCam(void) {
   sx = 0;
   for (int sn = 0; sn < nLines; sn++) {
      if( strstr(lines[sn], tok_Descrition) == 0) {
         description = strsub(lines[sn], strlen(tok_Descrition) + 1, strlen(lines[sn]) - (strlen(tok_Descrition)+2) );
         descrCnt = strsplit (descriptionLine, description, '\\');
         }

      if( strstr(lines[sn], tok_Sec_n) == 0) {
         sx = strtol( strsub(lines[sn], strstr(lines[sn], "_") + 1) );
         sprintf(nu, "%d", sx);
         }

      if( strstr(lines[sn], tok_Name      ) == 0) {;
         sec_Name[sx] = strsub(lines[sn], strlen(tok_Name) + 1, strlen(lines[sn]) - (strlen(tok_Name)+2) );
         sheetprint_from[sx] = 1;
         }

      if( strstr(lines[sn], tok_Prompt    ) == 0) {;
         sec_Prompt[sx] = strsub(lines[sn], strlen(tok_Prompt) + 1, strlen(lines[sn]) - (strlen(tok_Prompt)+2) );
         }

      if( strstr(lines[sn], tok_Out_put   ) == 0) {
         sec_Out_put[sx] = strsub(lines[sn], strlen(tok_Out_put) + 1, strlen(lines[sn]) - (strlen(tok_Out_put)+2) );
         }

      if( strstr(lines[sn], tok_Sheet ) == 0) {
         sec_Sheet[sx] = strtod( strsub(lines[sn], strlen(tok_Sheet) , strlen(lines[sn]) - (strlen(tok_Sheet) ) ) );
         }

      // n Layers (max 255)
      if( strstr(lines[sn], tok_Layers    ) == 0) {
         sec_Layers[sx] = strsub(lines[sn], strlen(tok_Layers) + 2, strlen(lines[sn]) - (strlen(tok_Layers)+3) );
         // hier noch die Layer
         }
      }
   return;
}

// main
int nt = 0;
do {
  if(strlwr(argv[4]) == filetype[nt]) {
    selfiletype = nt;
    break;
  }
  nt++;
} while(filetype[nt]);

if (argv[3]) Resolution = strtol(argv[3]);

if (strupr(argv[2]) == "MONOCHROME") sel_monochrome = 1;

if (argv[1]) {
   CAMfileName = argv[1];
   string dir = filedir(CAMfileName);
   if (filedir(CAMfileName)) ;
   else CAMfileName = path_cam[0] + "/" + argv[1];
   }
else {
   CAMfileName = dlgFileOpen("Select CAM File", path_cam[0]+"/*.cam", "*.*");
   }

if (CAMfileName) {
   nLines = fileread(lines, CAMfileName);
   readCam();
   }
else exit (0);

if(lines[0] != tok_job) {
   dlgMessageBox(CAMfileName + "\nis not a EAGLE CAM-Job\n" + lines[0], "OK");
   exit (0);
   }

if (schematic) {
   schematic(S) {
      file_name = filesetext(S.name, "");
      if (sheet) sheet(SH) sprintf(actualsheet, "%d", SH.number);
      }
   schematic(S) {
      S.sheets(SH) {
         if (lastsheet < SH.number) lastsheet = SH.number;
         }
      S.layers(L) {
         lNames[L.number] = L.name;
         lVisible[L.number] = L.visible;
         useLayer[L.number] = L.used;
         }
      }
   }

if (board) {
   board(B) {
      file_name = filesetext(B.name, "");
      B.layers(L) {
         lNames[L.number] = L.name;
         lVisible[L.number] = L.visible;
         useLayer[L.number] = L.used;
         }
      }
   }


// tabs menue
Result = dlgDialog("CAM-Job to EXPORT Image-Command") {
   //Define a container for tab pages
   dlgTabWidget {
      int tpn=1;
      sheetprint_to[tpn] = lastsheet;
      while (sec_Name[tpn]) {
         dlgTabPage(sec_Name[tpn]) {
            dlgLabel("<b>JOB Name</b>: " + CAMfileName);
            dlgSpacing(10);
            dlgStretch(0);
            dlgHBoxLayout {
               dlgStretch(0);
               dlgSpacing(10);
               dlgLabel(Eagle);
               dlgSpacing(10);
               if (board) {
                  dlgVBoxLayout {
                     dlgGroup("File name/suffix") dlgLabel(sec_Out_put[tpn]);
                     dlgStretch(1);
                     }
                  }
               dlgStretch(0);
               dlgHBoxLayout {
                  dlgStretch(0);
                  dlgVBoxLayout {
                     if (schematic) {
                        dlgStretch(0);
                        dlgGroup("Sheet") {
                           dlgVBoxLayout {
                              dlgHBoxLayout {
                                 dlgRadioButton("&All ", sheetprint);
                                 dlgLabel(" ");
                                 dlgStretch(1);
                                 }
                              dlgHBoxLayout {
                                 dlgRadioButton("&From ", sheetprint);
                                 dlgLabel("S&heet ");
                                 dlgIntEdit(sheetprint_from[tpn], 1, lastsheet);
                                 dlgLabel(" &to ");
                                 dlgIntEdit(sheetprint_to[tpn], sheetprint_from[tpn], lastsheet);
                                 dlgStretch(1);
                                 }
                              dlgHBoxLayout {
                                 dlgRadioButton("&# ", sheetprint);
                                 string sl;
                                 sprintf(sl, "%d/%d", sec_Sheet[tpn], lastsheet);
                                 dlgLabel(sl + " (def. in section)");
                                 dlgStretch(1);
                                 }
                              dlgHBoxLayout {
                                 dlgRadioButton("Actua&l ", sheetprint);
                                 dlgLabel(actualsheet);
                                 dlgStretch(1);
                                 }
                              }
                           }
                        }        // if schematic
                     dlgStretch(1);
                     }
                  // *** Layer list to print ***
                  int Seleclayer;
                  string layer[] ;
                  int n = 0;
                  int ln = strsplit(layer, sec_Layers[tpn], ' ');
                  for (int x = 0; x < ln; x++) {
                     int num = strtod(layer[x]);
                     if (useLayer[num]) {
                        if(schematic) {
                           if (num >= 90) {
                              sprintf(sec_usedlayer[n], "%3s %s", layer[x], lNames[num]);
                              n++;
                              }
                           }
                        if(board) {
                           if (num < 90 || num > 100) {
                              sprintf(sec_usedlayer[n], "%3s %s", layer[x], lNames[num]);
                              n++;
                              }
                           }
                        }
                     }
                  sec_usedlayer[n] = "";  // clear last+1
                  absolutUsedLayer = n;
                  dlgStretch(0);
                  dlgSpacing(10);
                  dlgVBoxLayout {
                     if (absolutUsedLayer) {
                        dlgHBoxLayout { dlgSpacing(100); }
                        dlgLabel("Printed layers");
                        dlgListBox(sec_usedlayer, Seleclayer);
                        }
                     else {
                        dlgLabel("<img src=warning.bmp>");
                        dlgLabel("no Layers selected\nin this CAM-Job/Section!\nLoad a correct Job.");
                        }
                     }
                  dlgSpacing(10);
                  }
               dlgStretch(1);
               }
            dlgStretch(1);
            tpn++;
            }
         }                 // ************ End of TAB | SHEET ***********
      dlgTabPage("&Help") {
         dlgSpacing(10);
         dlgHBoxLayout {
            dlgSpacing(10);
            dlgVBoxLayout {
               dlgHBoxLayout {
                  dlgLabel(Eagle);
                  dlgSpacing(10);
                  dlgVBoxLayout {
                     dlgLabel(EAGLE_SIGNATURE);
                     dlgSpacing(10);
                     if (description) {
                        dlgHBoxLayout {
                           dlgPushButton("&Show Job Description") viewDescript();
                           dlgStretch(1);
                           }
                        }
                     else dlgLabel("CAM Job description is EMPTY");
                     }
                  dlgStretch(1);
                  }
                  dlgSpacing(10);
               dlgLabel(help);
               dlgStretch(1);
               }
            }
         }
      }                // ************ End of all TABs ***********


   dlgHBoxLayout {
     dlgLabel("FileType");
     dlgComboBox(filetype, selfiletype);
     dlgSpacing(15);
     dlgCheckBox("Monochrome", sel_monochrome);
     dlgSpacing(15);
     dlgLabel("Resolution DPI");
     dlgIntEdit(Resolution, 50, 600);
     dlgStretch(1);
   }
   dlgHBoxLayout {
      dlgStretch(0);
      dlgPushButton("+OK") dlgAccept();
      dlgStretch(0);
      dlgPushButton("-Cancel") dlgReject();
      dlgStretch(1);
      dlgPushButton("&Load CAM-File") {
         CAMfileName = dlgFileOpen("select a File", path_cam[0]+"/*.cam", "");
         if (CAMfileName) {
            exit ("run '" + filesetext(argv[0], "")  + "' '" + CAMfileName + "';\n");
            }
         }
      dlgStretch(1);
      }
   };

if (Result == 0) exit (0);
if (sel_monochrome) monochrome = "MONOCHROME";
else monochrome = "";

if (test) cmd = "# generatet by " + argv[0] + " from " + CAMfileName + "\n";

for (int tpn = 1; tpn <= sx; tpn++) {
   string layer[] ;
   int n = 0;
   int ln = strsplit(layer, sec_Layers[tpn], ' ');
   if (test) cmd += "# Section: " + sec_Name[tpn] + "\n";
   if(board) {
      cmd += "SET DISPLAY_MODE REAL;\n";
      cmd += "RATSNEST;\n";
      cmd += "DISPLAY NONE ";

      for (int x = 0; x < ln; x++) {
         int num = strtod(layer[x]);
         if (num < 90 || num > 100) {
            if (useLayer[num]) {
               sprintf(s, " %3s", layer[x]);
               cmd += s;
               if(num == 21) cmd += " -23 -25 -27 -51";
               if(num == 22) cmd += " -24 -26 -28 -52";
               n++;
               }
            }
         }
      cmd += ";\n";
      commandPrint(sec_Out_put[tpn]);
      }

   if(schematic) {
      cmd += "DISPLAY NONE ";
      for (int x = 0; x < ln; x++) {
         int num = strtod(layer[x]);
         if (useLayer[num]) {
            if (num >= 90) {
               sprintf(s, " %3s", layer[x]);
               cmd += s;
               n++;
               }
            }
         }
      cmd += ";\n";
      string sh;
      string shn;
      switch (sheetprint) {
         case 0 : schematic(S) {
                     S.sheets(SH) {
                        sprintf(sh, "EDIT '.s%d';\n", SH.number);
                        cmd += sh;
                        sprintf(shn, "_s%d", SH.number);
                        commandPrint(shn);
                        }
                     }
                  break;

         case 1 : for (int prn = sheetprint_from[tpn]; prn <= sheetprint_to[tpn]; prn++) {
                     sprintf(sh, "EDIT '.s%d';\n", prn);
                     cmd += sh;
                     sprintf(shn, "_s%d", prn);
                     commandPrint(shn);
                     }
                  break;

         case 2 : sprintf(sh, "EDIT '.s%d';\n", sec_Sheet[tpn]);
                  cmd += sh;
                  sprintf(shn, "_s%d", sec_Sheet[tpn]);
                  commandPrint(shn);
                  break;

         case 3 : sprintf(sh, "EDIT '.s%s';\n", actualsheet);
                  cmd += sh;
                  sprintf(shn, ".s%s", actualsheet);
                  commandPrint(shn);
                  break;
         }
      }
   }

cmd += "DISPLAY ";
if(board) {
   for(int l = 1; l < 90; l++) {
      if (useLayer[l]) {
         if (lVisible[l]) {
            sprintf(s, " %d", l);
            cmd += s;
            }
         else {
            sprintf(s, " -%d", l);
            cmd += s;
            }
         }
      }
   for(l = 100; l < 256; l++) {
      if (useLayer[l]) {
         if (lVisible[l]) {
            sprintf(s, " %d", l);
            cmd += s;
            }
         else {
            sprintf(s, " -%d", l);
            cmd += s;
            }
         }
      }
   cmd += ";\n";
   }

if(schematic) {
   for(int l = 91; l < 256; l++) {
      if (useLayer[l]) {
         if (lVisible[l]) {
            sprintf(s, " %d", l);
            cmd += s;
            }
         else {
            sprintf(s, " -%d", l);
            cmd += s;
            }
         }
      }
   cmd += ";\n";
   sprintf(s, "EDIT '.s%s';\n", actualsheet);
   cmd += s;
   }

if (test) { dlgMessageBox(cmd, "OK", "Cancel");  exit (0);}
exit (cmd);