gerbv
export-isel-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) 2014 Florian Hirschberg
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 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include <glib.h>
35 #include <math.h>
36 
37 #include <glib/gstdio.h>
38 #include "gerbv.h"
39 #include "common.h"
40 
41 /* DEBUG printing. #define DEBUG 1 in config.h to use this fcn. */
42 #undef DPRINTF
43 #define DPRINTF(...) do { if (DEBUG) printf(__VA_ARGS__); } while (0)
44 #define round(x) floor(x+0.5)
45 
46 gboolean
47 gerbv_export_isel_drill_file_from_image (const gchar *filename, gerbv_image_t *inputImage,
48  gerbv_user_transformation_t *transform) {
49  FILE *fd;
50  GArray *apertureTable = g_array_new(FALSE,FALSE,sizeof(int));
51  gerbv_net_t *currentNet;
52 
53  /* force gerbv to output decimals as dots (not commas for other locales) */
54  setlocale(LC_NUMERIC, "C");
55 
56  if ((fd = g_fopen(filename, "w")) == NULL) {
57  GERB_COMPILE_ERROR(_("Can't open file for writing: %s"),
58  filename);
59 
60  return FALSE;
61  }
62 
63  /* duplicate the image, cleaning it in the process */
64  gerbv_image_t *image = gerbv_image_duplicate_image (inputImage, transform);
65 
66  /* write header info */
67  fprintf(fd,
68  "IMF_PBL_V1.0\r\n"
69  "\r\n"
70  "; Please change this parameters to your needs\r\n"
71  "; Don't forget to change the drill depth to\r\n"
72  "; your PCB thickness\r\n"
73  "\r\n"
74  "; The normal movement velocity in 1/1000 mm/s\r\n"
75  "VEL 5000\r\n"
76  "\r\n"
77  "; The fast movement velocity in 1/1000 mm/s\r\n"
78  "FASTVEL 10000\r\n"
79  "; The safety height in mm over the PCB for movement\r\n"
80  "\r\n"
81  "DRILLDEF S2000\r\n"
82  "\r\n"
83  "; Drill velocity of downwards movement in 1/1000 mm/s\r\n"
84  "\r\n"
85  "DRILLDEF V1000\r\n"
86  "\r\n"
87  "; The drill depth in 1/1000 mm\r\n"
88  "\r\n"
89  "DRILLDEF D1800 ; 1.5mm material + 0.3mm break through\r\n"
90  "\r\n"
91  "; DO NOT CHANGE THESE PARAMETERS!!\r\n"
92  "\r\n"
93  "DRILLDEF C1 P0\r\n"
94  "\r\n");
95 
96  /* define all apertures */
97  gerbv_aperture_t *currentAperture;
98 
99  /* the image should already have been cleaned by a duplicate_image call, so we can safely
100  assume the aperture range is correct */
101  for (int i=APERTURE_MIN; i<APERTURE_MAX; i++) {
102  currentAperture = image->aperture[i];
103 
104  if (!currentAperture)
105  continue;
106 
107  switch (currentAperture->type) {
108  case GERBV_APTYPE_CIRCLE:
109  /* add the "approved" aperture to our valid list */
110  fprintf(fd, "; TOOL %d - Diameter %1.3f mm\r\n",
111  i + 1,
112  COORD2MMS(currentAperture->parameter[0]));
113  g_array_append_val (apertureTable, i);
114 
115  break;
116  default:
117  break;
118  }
119  }
120 
121  /* write rest of image */
122 
123  for (guint i=0; i<apertureTable->len; i++) {
124  int currentAperture=g_array_index (apertureTable, int, i);
125 
126  /* write tool change */
127  fprintf(fd, "GETTOOL %d\r\n",currentAperture+1);
128 
129  /* run through all nets and look for drills using this aperture */
130  for (currentNet = image->netlist; currentNet;
131  currentNet = currentNet->next) {
132 
133  if (currentNet->aperture != currentAperture)
134  continue;
135 
136  switch (currentNet->aperture_state) {
138  long xVal,yVal;
139 
140  xVal = (long) round(COORD2MMS(currentNet->stop_x)*1e3);
141  yVal = (long) round(COORD2MMS(currentNet->stop_y)*1e3);
142  fprintf(fd, "DRILL X%06ld Y%06ld\r\n",
143  xVal, yVal);
144  break;
145  }
146  default:
147  GERB_COMPILE_WARNING(
148  _("Skipped to export of unsupported state %d "
149  "interpolation \"%s\""),
150  currentNet->aperture_state,
152  currentNet->interpolation));
153  }
154  }
155  }
156  g_array_free (apertureTable, TRUE);
157  /* write footer */
158  fprintf(fd, "PROGEND\r\n");
159  gerbv_destroy_image (image);
160  fclose(fd);
161 
162  /* return to the default locale */
163  setlocale(LC_NUMERIC, "");
164  return TRUE;
165 }
gboolean gerbv_export_isel_drill_file_from_image(const gchar *filename, gerbv_image_t *inputImage, gerbv_user_transformation_t *transform)
Export an image to a new file in ISEL NCP drill format.
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:920
const char * gerbv_interpolation_name(gerbv_interpolation_t interp)
Return string name of gerbv_interpolation_t interpolation.
Definition: gerbv.c:115
The main header file for the libgerbv library.
@ 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
struct gerbv_net * next
Definition: gerbv.h:666
gerbv_interpolation_t interpolation
Definition: gerbv.h:664
int aperture
Definition: gerbv.h:662