gerbv  2.10.1-dev~93f1b5
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 #define dprintf \
43  if (DEBUG) \
44  printf
45 #define round(x) floor(x + 0.5)
46 
47 gboolean
49  const gchar* filename, gerbv_image_t* inputImage, gerbv_user_transformation_t* transform
50 ) {
51  FILE* fd;
52  GArray* apertureTable = g_array_new(FALSE, FALSE, sizeof(int));
53  gerbv_net_t* currentNet;
54 
55  /* force gerbv to output decimals as dots (not commas for other locales) */
56  setlocale(LC_NUMERIC, "C");
57 
58  if ((fd = g_fopen(filename, "w")) == NULL) {
59  GERB_COMPILE_ERROR(_("Can't open file for writing: %s"), filename);
60 
61  return FALSE;
62  }
63 
64  /* duplicate the image, cleaning it in the process */
65  gerbv_image_t* image = gerbv_image_duplicate_image(inputImage, transform);
66 
67  /* write header info */
68  fprintf(
69  fd,
70  "IMF_PBL_V1.0\r\n"
71  "\r\n"
72  "; Please change this parameters to your needs\r\n"
73  "; Don't forget to change the drill depth to\r\n"
74  "; your PCB thickness\r\n"
75  "\r\n"
76  "; The normal movement velocity in 1/1000 mm/s\r\n"
77  "VEL 5000\r\n"
78  "\r\n"
79  "; The fast movement velocity in 1/1000 mm/s\r\n"
80  "FASTVEL 10000\r\n"
81  "; The safety height in mm over the PCB for movement\r\n"
82  "\r\n"
83  "DRILLDEF S2000\r\n"
84  "\r\n"
85  "; Drill velocity of downwards movement in 1/1000 mm/s\r\n"
86  "\r\n"
87  "DRILLDEF V1000\r\n"
88  "\r\n"
89  "; The drill depth in 1/1000 mm\r\n"
90  "\r\n"
91  "DRILLDEF D1800 ; 1.5mm material + 0.3mm break through\r\n"
92  "\r\n"
93  "; DO NOT CHANGE THESE PARAMETERS!!\r\n"
94  "\r\n"
95  "DRILLDEF C1 P0\r\n"
96  "\r\n"
97  );
98 
99  /* define all apertures */
100  gerbv_aperture_t* currentAperture;
101 
102  /* the image should already have been cleaned by a duplicate_image call, so we can safely
103  assume the aperture range is correct */
104  for (int i = APERTURE_MIN; i < APERTURE_MAX; i++) {
105  currentAperture = image->aperture[i];
106 
107  if (!currentAperture)
108  continue;
109 
110  switch (currentAperture->type) {
111  case GERBV_APTYPE_CIRCLE:
112  /* add the "approved" aperture to our valid list */
113  fprintf(fd, "; TOOL %d - Diameter %1.3f mm\r\n", i + 1, COORD2MMS(currentAperture->parameter[0]));
114  g_array_append_val(apertureTable, i);
115 
116  break;
117  default: 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; currentNet = currentNet->next) {
131 
132  if (currentNet->aperture != currentAperture)
133  continue;
134 
135  switch (currentNet->aperture_state) {
137  {
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", xVal, yVal);
143  break;
144  }
145  default:
146  GERB_COMPILE_WARNING(
147  _("Skipped to export of unsupported state %d "
148  "interpolation \"%s\""),
149  currentNet->aperture_state, gerbv_interpolation_name(currentNet->interpolation)
150  );
151  }
152  }
153  }
154  g_array_free(apertureTable, TRUE);
155  /* write footer */
156  fprintf(fd, "PROGEND\r\n");
157  gerbv_destroy_image(image);
158  fclose(fd);
159 
160  /* return to the default locale */
161  setlocale(LC_NUMERIC, "");
162  return TRUE;
163 }
Contains basic defines.
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: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
const char * gerbv_interpolation_name(gerbv_interpolation_t interp)
Return string name of gerbv_interpolation_t interpolation.
Definition: gerbv.c:97
The main header file for the libgerbv library.
@ 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
struct gerbv_net * next
Definition: gerbv.h:677
gerbv_interpolation_t interpolation
Definition: gerbv.h:675
int aperture
Definition: gerbv.h:673