2 * Utility to convert overlapping Excellon drill hits into drill slots.
3 * Copyright © 2018, 2021, 2023 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
29 #include <localcharset.h>
32 #include <CNearTree.h>
45 #define _(x) (gettext(x))
47 static const char *progname = "slotifier";
48 static unsigned verbose;
50 static void print_version(void)
52 const char *copysign = copyright_symbol(locale_charset());
55 printf("Copyright %s 2023 Nick Bowler.\n", copysign);
56 puts("License GPLv3+: GNU GPL version 3 or any later version.");
57 puts("This is free software: you are free to change and redistribute it.");
58 puts("There is NO WARRANTY, to the extent permitted by law.");
61 static void print_usage(FILE *f)
63 fprintf(f, _("Usage: %s [options] [-o filename] filename\n"), progname);
65 fprintf(f, _("Try %s --help for more information.\n"),
69 static void print_help(const struct option *lopts)
71 const struct option *opt;
75 puts(_("This is \"slotifier\": a tool to convert overlapping drill hits in Excellon\n"
76 "drill files to G85 drill slots."));
80 for (opt = lopts; opt->val; opt++) {
81 struct lopt_help help;
83 if (!lopt_get_help(opt, &help))
86 help_print_option(opt, help.arg, help.desc, 20);
90 puts(_("For more information, see the slotifier(1) man page."));
94 * TRANSLATORS: Please add *another line* indicating where users should
95 * report translation bugs.
97 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
100 static void init_i18n(void)
102 setlocale(LC_ALL, "");
103 bindtextdomain(PACKAGE, LOCALEDIR);
107 static CNearTreeHandle build_search_tree(gerbv_image_t *drill)
112 if (CNearTreeCreate(&t, 2, CNEARTREE_TYPE_DOUBLE|CNEARTREE_NORM_L2))
115 /* Build a search tree from all the holes. */
116 for (x = drill->netlist; x; x = x->next) {
117 double xy[2] = { x->start_x, x->start_y };
119 assert(x->aperture >= 0 && x->aperture < APERTURE_MAX);
121 /* Skip things that aren't drill hits */
122 if (!drill->aperture[x->aperture])
125 /* Holes are marked as "flashing"; otherwise it's an existing
127 if (x->aperture_state != GERBV_APERTURE_STATE_FLASH)
130 if (CNearTreeInsert(t, xy, x)) {
136 if (CNearTreeCompleteDelayedInsert(t)) {
144 static gerbv_aperture_type_t tool_type(gerbv_image_t *drill, int aperture)
146 gerbv_aperture_t *tool = drill->aperture[abs(aperture)];
151 static double tool_radius(gerbv_image_t *drill, int aperture)
153 gerbv_aperture_t *tool = drill->aperture[abs(aperture)];
155 /* Half a mil slop to decisively include points on boundary. */
156 return tool->parameter[0] / 2.0 + 0.0005;
159 static int holes_overlap(gerbv_image_t *drill, gerbv_net_t *a, gerbv_net_t *b)
161 double d = hypot(a->start_x - b->start_x, a->start_y - b->start_y);
163 return tool_radius(drill, a->aperture) >= d
164 || tool_radius(drill, b->aperture) >= d;
167 static int combine_holes(gerbv_image_t *drill, gerbv_net_t *hole,
170 CVectorHandle group, tmp;
171 int biggest_tool, ret = -1;
176 * Since we consider holes in order of decreasing size, the initial hole
177 * considered is by definition the biggest one we will find in a group.
179 biggest_r = tool_radius(drill, (biggest_tool = hole->aperture));
181 if (CVectorCreate(&group, sizeof (gerbv_net_t *), 10)) {
182 fprintf(stderr, _("%s: failed to allocate memory\n"), progname);
186 if (CVectorCreate(&tmp, sizeof (void *), 10)) {
187 fprintf(stderr, _("%s: failed to allocate memory\n"), progname);
193 * Breadth-first nearest neighbour search of holes. We negate the
194 * aperture to indicate which holes have been previously visited.
196 CVectorAddElement(group, &hole);
197 hole->aperture = -hole->aperture;
199 for (i = 0; i < CVectorSize(group); i++) {
202 CVectorGetElement(group, &hole, i);
204 assert(tool_type(drill, hole->aperture) == GERBV_APTYPE_CIRCLE);
205 assert(tool_radius(drill, hole->aperture) <= biggest_r);
207 xy[0] = hole->start_x; xy[1] = hole->start_y;
208 if (CNearTreeFindInSphere(t, biggest_r, 0, tmp, xy, 1) != 0) {
209 /* We should always should find at least one hole! */
210 fprintf(stderr, _("%s: fatal error searching holes\n"),
215 for (j = 0; j < CVectorSize(tmp); j++) {
216 /* I don't know why, but CNearTree returns a list
217 * of pointers to its internal copies of pointers
218 * to the objects in the tree. So we need this
219 * double indirection to get the actual hole. */
220 gerbv_net_t *newhole;
223 CVectorGetElement(tmp, &p, j);
224 newhole = *(void **)p;
226 if (newhole->aperture < 0)
227 continue; /* already visited */
229 if (holes_overlap(drill, hole, newhole)) {
230 CVectorAddElement(group, &newhole);
231 newhole->aperture = -newhole->aperture;
236 /* Compare each pair of matched points to find the longest slot. */
237 if (CVectorSize(group) > 1) {
238 int biggest_slot = 0;
241 for (i = 0; i < CVectorSize(group) - 1; i++) {
242 CVectorGetElement(group, &hole, i);
243 for (j = i+1; j < CVectorSize(group); j++) {
247 CVectorGetElement(group, &h2, j);
248 newlen = hypot(hole->start_x - h2->start_x,
249 hole->start_y - h2->start_y);
251 if (newlen > bestlen) {
252 hole->stop_x = h2->start_x;
253 hole->stop_y = h2->stop_y;
260 CVectorGetElement(group, &hole, biggest_slot);
261 hole->aperture = biggest_tool;
262 hole->aperture_state = GERBV_APERTURE_STATE_ON;
265 const char *fmt = ngettext(
266 "%s: merged %zu hole into slot (%.4f,%.4f)-(%.4f,%.4f)\n",
267 "%s: merged %zu holes into slot (%.4f,%.4f)-(%.4f,%.4f)\n",
270 fprintf(stderr, fmt, progname, CVectorSize(group),
271 hole->start_x, hole->start_y,
272 hole->stop_x, hole->stop_y);
275 /* The only hole we found was our original one, restore
277 CVectorGetElement(group, &hole, 0);
279 assert(hole->aperture < 0);
280 hole->aperture = -hole->aperture;
283 fprintf(stderr, _("%s: hole at (%.4f,%.4f) not merged\n"),
284 progname, hole->start_x, hole->start_y);
296 * Order two holes by hole diameter.
298 static gerbv_image_t *hsc_drill_data;
299 static int hole_size_cmp(const void *a_, const void *b_)
301 gerbv_net_t * const *a = a_, * const *b = b_;
302 gerbv_aperture_t *ta, *tb;
304 ta = hsc_drill_data->aperture[abs(a[0]->aperture)];
305 assert(ta->type == GERBV_APTYPE_CIRCLE);
307 tb = hsc_drill_data->aperture[abs(b[0]->aperture)];
308 assert(tb->type == GERBV_APTYPE_CIRCLE);
310 if (ta->parameter[0] > tb->parameter[0])
312 if (ta->parameter[0] < tb->parameter[0])
317 static void sort_holes_by_size(gerbv_image_t *drill, CVectorHandle work)
319 hsc_drill_data = drill;
320 qsort(work->array, work->size, work->elementsize, hole_size_cmp);
323 static int slotify(gerbv_image_t *drill)
325 CVectorHandle holes, work;
330 t = build_search_tree(drill);
332 fprintf(stderr, _("%s: failed to build search tree\n"),
337 CNearTreeObjects(t, &holes);
341 if (CVectorCreate(&work, sizeof (gerbv_net_t *), CVectorSize(holes))) {
342 fprintf(stderr, _("%s: failed to allocate memory\n"), progname);
346 memcpy(work->array, holes->array, holes->size * holes->elementsize);
347 work->size = holes->size;
348 sort_holes_by_size(drill, work);
350 for (i = 0; i < CVectorSize(work); i++) {
353 CVectorGetElement(work, &hole, i);
354 /* Skip holes we've already looked at */
355 if (hole->aperture < 0)
357 if (hole->aperture_state == GERBV_APERTURE_STATE_ON)
361 fprintf(stderr, _("%s: checking hole at (%.4f,%.4f) for overlaps\n"),
362 progname, hole->start_x, hole->start_y);
365 if (combine_holes(drill, hole, t) < 0) {
371 /* Clean out any holes we don't need anymore */
372 for (i = 0; i < CVectorSize(holes); i++) {
375 CVectorGetElement(holes, &hole, i);
376 if (hole->aperture < 0)
377 gerbv_image_delete_net(hole);
386 static int do_cmdline(int argc, char **argv, const char **outfile)
388 const char *sopts = SOPT_STRING;
391 XTRA_PACKED_LOPTS(lopts);
396 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
417 fprintf(stderr, _("%s: error: must specify a filename\n"),
423 if (optind + 1 < argc) {
424 fprintf(stderr, _("%s: error: excess command-line arguments\n"),
433 int main(int argc, char **argv)
435 const char *outfile = "/dev/stdout";
437 gerbv_image_t *drill;
441 switch (do_cmdline(argc, argv, &outfile)) {
442 case -1: return EXIT_FAILURE;
443 case 1: return EXIT_SUCCESS;
446 gp = gerbv_create_project();
450 gerbv_open_layer_from_filename(gp, argv[optind]);
456 drill = gp->file[0]->image;
457 if (drill->layertype != GERBV_LAYERTYPE_DRILL) {
458 fprintf(stderr, _("%s: %s: error: not a drill file\n"),
459 progname, argv[optind]);
464 if (slotify(drill) != 0) {
469 if (!gerbv_export_drill_file_from_image(outfile, drill, NULL))
472 gerbv_destroy_project(gp);