* rework export svg/pdf/ps functionality to export images with 1:1 scale. Previously...
[geda-gerbv.git] / src / export-drill.c
blob1ec348b9770bb7b8239f958bb6f62815e819bc7f
1 /*
2 * gEDA - GNU Electronic Design Automation
3 * This file is a part of gerbv.
5 * Copyright (C) 2008 Julian Lamb
7 * $Id$
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.
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.
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
24 /** \file export-drill.c
25 \brief Functions for exporting gerbv images to Excellon drill files
26 \ingroup libgerbv
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #include <glib.h>
34 #include <math.h>
36 #include <glib/gstdio.h>
37 #include "gerbv.h"
39 /* DEBUG printing. #define DEBUG 1 in config.h to use this fcn. */
40 #define dprintf if(DEBUG) printf
41 #define round(x) floor(x+0.5)
43 gboolean
44 gerbv_export_drill_file_from_image (gchar *filename, gerbv_image_t *inputImage,
45 gerbv_user_transformation_t *transform) {
46 FILE *fd;
47 GArray *apertureTable = g_array_new(FALSE,FALSE,sizeof(int));
48 gerbv_net_t *currentNet;
50 if ((fd = g_fopen(filename, "w")) == NULL) {
51 GERB_MESSAGE("Can't open file for writing: %s\n", filename);
52 return FALSE;
55 /* duplicate the image, cleaning it in the process */
56 gerbv_image_t *image = gerbv_image_duplicate_image (inputImage, transform);
58 /* write header info */
59 fprintf(fd, "M48\n");
60 fprintf(fd, "INCH,TZ\n");
62 /* define all apertures */
63 gerbv_aperture_t *currentAperture;
64 gint i;
66 /* the image should already have been cleaned by a duplicate_image call, so we can safely
67 assume the aperture range is correct */
68 for (i=APERTURE_MIN; i<APERTURE_MAX; i++) {
69 currentAperture = image->aperture[i];
71 if (!currentAperture)
72 continue;
74 switch (currentAperture->type) {
75 case GERBV_APTYPE_CIRCLE:
76 fprintf(fd, "T%dC%1.3f\n",i,currentAperture->parameter[0]);
77 /* add the "approved" aperture to our valid list */
78 g_array_append_val (apertureTable, i);
79 break;
80 default:
81 break;
85 fprintf(fd, "%%\n");
86 /* write rest of image */
88 for (i=0; i<apertureTable->len; i++) {
89 int currentAperture=g_array_index (apertureTable, int, i);
91 /* write tool change */
92 fprintf(fd, "T%d\n",currentAperture);
94 /* run through all nets and look for drills using this aperture */
95 for (currentNet = image->netlist; currentNet; currentNet = currentNet->next){
96 if ((currentNet->aperture == currentAperture)&&(currentNet->aperture_state == GERBV_APERTURE_STATE_FLASH)) {
97 long xVal,yVal;
98 xVal = (long) round(currentNet->stop_x * 10000.0);
99 yVal = (long) round(currentNet->stop_y * 10000.0);
100 fprintf(fd, "X%06ldY%06ld\n",xVal,yVal);
104 g_array_free (apertureTable, TRUE);
105 /* write footer */
106 fprintf(fd, "M30\n\n");
107 gerbv_destroy_image (image);
108 fclose(fd);
109 return TRUE;