2 * gEDA - GNU Electronic Design Automation
3 * This file is a part of gerbv.
5 * Copyright (C) 2008 Julian Lamb
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
37 #include <glib/gstdio.h>
40 /* DEBUG printing. #define DEBUG 1 in config.h to use this fcn. */
41 #define dprintf if(DEBUG) printf
42 #define round(x) floor(x+0.5)
45 gerbv_export_drill_file_from_image (gchar
*filename
, gerbv_image_t
*inputImage
,
46 gerbv_user_transformation_t
*transform
) {
48 GArray
*apertureTable
= g_array_new(FALSE
,FALSE
,sizeof(int));
49 gerbv_net_t
*currentNet
;
51 // force gerbv to output decimals as dots (not commas for other locales)
52 setlocale(LC_NUMERIC
, "C");
54 if ((fd
= g_fopen(filename
, "w")) == NULL
) {
55 GERB_MESSAGE("Can't open file for writing: %s\n", filename
);
59 /* duplicate the image, cleaning it in the process */
60 gerbv_image_t
*image
= gerbv_image_duplicate_image (inputImage
, transform
);
62 /* write header info */
64 fprintf(fd
, "INCH,TZ\n");
66 /* define all apertures */
67 gerbv_aperture_t
*currentAperture
;
70 /* the image should already have been cleaned by a duplicate_image call, so we can safely
71 assume the aperture range is correct */
72 for (i
=APERTURE_MIN
; i
<APERTURE_MAX
; i
++) {
73 currentAperture
= image
->aperture
[i
];
78 switch (currentAperture
->type
) {
79 case GERBV_APTYPE_CIRCLE
:
80 fprintf(fd
, "T%dC%1.3f\n",i
,currentAperture
->parameter
[0]);
81 /* add the "approved" aperture to our valid list */
82 g_array_append_val (apertureTable
, i
);
90 /* write rest of image */
92 for (i
=0; i
<apertureTable
->len
; i
++) {
93 int currentAperture
=g_array_index (apertureTable
, int, i
);
95 /* write tool change */
96 fprintf(fd
, "T%d\n",currentAperture
);
98 /* run through all nets and look for drills using this aperture */
99 for (currentNet
= image
->netlist
; currentNet
; currentNet
= currentNet
->next
){
100 if ((currentNet
->aperture
== currentAperture
)&&(currentNet
->aperture_state
== GERBV_APERTURE_STATE_FLASH
)) {
102 xVal
= (long) round(currentNet
->stop_x
* 10000.0);
103 yVal
= (long) round(currentNet
->stop_y
* 10000.0);
104 fprintf(fd
, "X%06ldY%06ld\n",xVal
,yVal
);
108 g_array_free (apertureTable
, TRUE
);
110 fprintf(fd
, "M30\n\n");
111 gerbv_destroy_image (image
);
114 // return to the default locale
115 setlocale(LC_NUMERIC
, "");