gerbv
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 <glib/gstdio.h>
33 
34 #include "common.h"
35 
36 #undef DPRINTF
37 #define DPRINTF(...) do { if (DEBUG) printf(__VA_ARGS__); } while (0)
38 
39 #define round(x) floor(x+0.5)
40 
41 gboolean
42 gerbv_export_drill_file_from_image (const 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_COMPILE_ERROR( _("Can't open file for writing: %s"),
53  filename);
54  return FALSE;
55  }
56 
57  /* duplicate the image, cleaning it in the process */
58  gerbv_image_t *image = gerbv_image_duplicate_image (inputImage, transform);
59 
60  /* write header info */
61  fprintf(fd, "M48\n");
62  fprintf(fd, "INCH,TZ\n");
63 
64  /* define all apertures */
65  gerbv_aperture_t *aperture;
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 (int 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 (guint 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 }
gboolean gerbv_export_drill_file_from_image(const gchar *filename, gerbv_image_t *inputImage, gerbv_user_transformation_t *transform)
Export an image to a new file in Excellon drill format.
Definition: export-drill.c:42
void gerbv_destroy_image(gerbv_image_t *image)
Free an image structure.
Definition: gerb_image.c:106
gerbv_image_t * gerbv_image_duplicate_image(gerbv_image_t *sourceImage, gerbv_user_transformation_t *transform)
Duplicate an existing image and return the new copy.
Definition: gerb_image.c:909
The main header file for the libgerbv library.
@ GERBV_APERTURE_STATE_ON
Definition: gerbv.h:179
@ GERBV_APERTURE_STATE_FLASH
Definition: gerbv.h:180
@ GERBV_APTYPE_CIRCLE
Definition: gerbv.h:160
gerbv_net_t * netlist
Definition: gerbv.h:729
gerbv_aperture_t * aperture[APERTURE_MAX]
Definition: gerbv.h:723
double stop_y
Definition: gerbv.h:660
gerbv_aperture_state_t aperture_state
Definition: gerbv.h:663
double stop_x
Definition: gerbv.h:659
double start_x
Definition: gerbv.h:657
struct gerbv_net * next
Definition: gerbv.h:666
double start_y
Definition: gerbv.h:658
int aperture
Definition: gerbv.h:662