gerbv  2.6A
export-drill.c
Go to the documentation of this file.
1 /*
2  * gEDA - GNU Electronic Design Automation
3  * This file is a part of gerbv.
4  *
5  * Copyright (C) 2008 Julian Lamb
6  *
7  * $Id$
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
22  */
23 
29 #include "gerbv.h"
30 
31 #include <math.h>
32 #include <locale.h>
33 #include <glib/gstdio.h>
34 
35 #include "common.h"
36 
37 #define dprintf if(DEBUG) printf
38 
39 #define round(x) floor(x+0.5)
40 
41 gboolean
42 gerbv_export_drill_file_from_image (gchar *filename, gerbv_image_t *inputImage,
43  gerbv_user_transformation_t *transform) {
44  FILE *fd;
45  GArray *apertureTable = g_array_new(FALSE, FALSE, sizeof(int));
46  gerbv_net_t *net;
47 
48  /* force gerbv to output decimals as dots (not commas for other locales) */
49  setlocale(LC_NUMERIC, "C");
50 
51  if ((fd = g_fopen(filename, "w")) == NULL) {
52  GERB_MESSAGE(_("Can't open file for writing: %s"), filename);
53  return FALSE;
54  }
55 
56  /* duplicate the image, cleaning it in the process */
57  gerbv_image_t *image = gerbv_image_duplicate_image (inputImage, transform);
58 
59  /* write header info */
60  fprintf(fd, "M48\n");
61  fprintf(fd, "INCH,TZ\n");
62 
63  /* define all apertures */
64  gerbv_aperture_t *aperture;
65  gint i;
66 
67  /* the image should already have been cleaned by a duplicate_image call, so we can safely
68  assume the aperture range is correct */
69  for (i = APERTURE_MIN; i < APERTURE_MAX; i++) {
70  aperture = image->aperture[i];
71 
72  if (!aperture)
73  continue;
74 
75  switch (aperture->type) {
77  fprintf(fd, "T%dC%1.3f\n", i, aperture->parameter[0]);
78  /* add the "approved" aperture to our valid list */
79  g_array_append_val(apertureTable, i);
80  break;
81  default:
82  break;
83  }
84  }
85 
86  fprintf(fd, "%%\n");
87  /* write rest of image */
88 
89  for (i = 0; i < apertureTable->len; i++) {
90  int aperture_idx = g_array_index(apertureTable, int, i);
91 
92  /* write tool change */
93  fprintf(fd, "T%d\n", aperture_idx);
94 
95  /* run through all nets and look for drills using this aperture */
96  for (net = image->netlist; net; net = net->next) {
97  if (net->aperture != aperture_idx)
98  continue;
99 
100  switch (net->aperture_state) {
102  fprintf(fd, "X%06ldY%06ld\n",
103  (long)round(net->stop_x * 10000.0),
104  (long)round(net->stop_y * 10000.0));
105  break;
106  case GERBV_APERTURE_STATE_ON: /* Cut slot */
107  fprintf(fd, "X%06ldY%06ldG85X%06ldY%06ld\n",
108  (long)round(net->start_x * 10000.0),
109  (long)round(net->start_y * 10000.0),
110  (long)round(net->stop_x * 10000.0),
111  (long)round(net->stop_y * 10000.0));
112  break;
113  default:
114  break;
115  }
116  }
117  }
118  g_array_free (apertureTable, TRUE);
119 
120  /* write footer */
121  fprintf(fd, "M30\n\n");
122  gerbv_destroy_image(image);
123  fclose(fd);
124 
125  /* return to the default locale */
126  setlocale(LC_NUMERIC, "");
127  return TRUE;
128 }