gerbv  2.10.1-dev~93f1b5
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 #define dprintf \
37  if (DEBUG) \
38  printf
39 
40 #define round(x) floor(x + 0.5)
41 
42 gboolean
44  const gchar* filename, gerbv_image_t* inputImage, gerbv_user_transformation_t* transform
45 ) {
46  FILE* fd;
47  GArray* apertureTable = g_array_new(FALSE, FALSE, sizeof(int));
48  gerbv_net_t* net;
49 
50  /* force gerbv to output decimals as dots (not commas for other locales) */
51  setlocale(LC_NUMERIC, "C");
52 
53  if ((fd = g_fopen(filename, "w")) == NULL) {
54  GERB_COMPILE_ERROR(_("Can't open file for writing: %s"), filename);
55  return FALSE;
56  }
57 
58  /* duplicate the image, cleaning it in the process */
59  gerbv_image_t* image = gerbv_image_duplicate_image(inputImage, transform);
60 
61  /* write header info */
62  fprintf(fd, "M48\n");
63  fprintf(fd, "INCH,TZ\n");
64 
65  /* define all apertures */
66  gerbv_aperture_t* aperture;
67 
68  /* the image should already have been cleaned by a duplicate_image call, so we can safely
69  assume the aperture range is correct */
70  for (int i = APERTURE_MIN; i < APERTURE_MAX; i++) {
71  aperture = image->aperture[i];
72 
73  if (!aperture)
74  continue;
75 
76  switch (aperture->type) {
78  fprintf(fd, "T%dC%1.3f\n", i, aperture->parameter[0]);
79  /* add the "approved" aperture to our valid list */
80  g_array_append_val(apertureTable, i);
81  break;
82  default: 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(
103  fd, "X%06ldY%06ld\n", (long)round(net->stop_x * 10000.0), (long)round(net->stop_y * 10000.0)
104  );
105  break;
106  case GERBV_APERTURE_STATE_ON: /* Cut slot */
107  fprintf(
108  fd, "X%06ldY%06ldG85X%06ldY%06ld\n", (long)round(net->start_x * 10000.0),
109  (long)round(net->start_y * 10000.0), (long)round(net->stop_x * 10000.0),
110  (long)round(net->stop_y * 10000.0)
111  );
112  break;
113  default: break;
114  }
115  }
116  }
117  g_array_free(apertureTable, TRUE);
118 
119  /* write footer */
120  fprintf(fd, "M30\n\n");
121  gerbv_destroy_image(image);
122  fclose(fd);
123 
124  /* return to the default locale */
125  setlocale(LC_NUMERIC, "");
126  return TRUE;
127 }
Contains basic defines.
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:43
void gerbv_destroy_image(gerbv_image_t *image)
Free an image structure.
Definition: gerb_image.c:104
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:815
The main header file for the libgerbv library.
@ GERBV_APERTURE_STATE_ON
Definition: gerbv.h:172
@ GERBV_APERTURE_STATE_FLASH
Definition: gerbv.h:173
@ GERBV_APTYPE_CIRCLE
Definition: gerbv.h:152
gerbv_net_t * netlist
Definition: gerbv.h:739
gerbv_aperture_t * aperture[APERTURE_MAX]
Definition: gerbv.h:733
double stop_y
Definition: gerbv.h:671
gerbv_aperture_state_t aperture_state
Definition: gerbv.h:674
double stop_x
Definition: gerbv.h:670
double start_x
Definition: gerbv.h:668
struct gerbv_net * next
Definition: gerbv.h:677
double start_y
Definition: gerbv.h:669
int aperture
Definition: gerbv.h:673