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>
33 #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 = "(C)";
53 void *convsign = NULL;
58 convsign = str_iconv("\xc2\xa9", "UTF-8", locale_charset());
63 printf("Copyright %s 2023 Nick Bowler.\n", copysign);
64 puts("License GPLv3+: GNU GPL version 3 or any later version.");
65 puts("This is free software: you are free to change and redistribute it.");
66 puts("There is NO WARRANTY, to the extent permitted by law.");
71 static void print_usage(FILE *f)
73 fprintf(f, _("Usage: %s [options] [-o filename] filename\n"), progname);
75 fprintf(f, _("Try %s --help for more information.\n"),
79 static void print_help(const struct option *lopts)
81 const struct option *opt;
85 puts(_("This is \"slotifier\": a tool to convert overlapping drill hits in Excellon\n"
86 "drill files to G85 drill slots."));
90 for (opt = lopts; opt->val; opt++) {
91 struct lopt_help help;
93 if (!lopt_get_help(opt, &help))
96 help_print_option(opt, help.arg, help.desc, 20);
100 puts(_("For more information, see the slotifier(1) man page."));
104 * TRANSLATORS: Please add *another line* indicating where users should
105 * report translation bugs.
107 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
110 static void init_i18n(void)
112 setlocale(LC_ALL, "");
113 bindtextdomain(PACKAGE, LOCALEDIR);
117 static CNearTreeHandle build_search_tree(gerbv_image_t *drill)
122 if (CNearTreeCreate(&t, 2, CNEARTREE_TYPE_DOUBLE|CNEARTREE_NORM_L2))
125 /* Build a search tree from all the holes. */
126 for (x = drill->netlist; x; x = x->next) {
127 double xy[2] = { x->start_x, x->start_y };
129 assert(x->aperture >= 0 && x->aperture < APERTURE_MAX);
131 /* Skip things that aren't drill hits */
132 if (!drill->aperture[x->aperture])
135 /* Holes are marked as "flashing"; otherwise it's an existing
137 if (x->aperture_state != GERBV_APERTURE_STATE_FLASH)
140 if (CNearTreeInsert(t, xy, x)) {
146 if (CNearTreeCompleteDelayedInsert(t)) {
154 static gerbv_aperture_type_t tool_type(gerbv_image_t *drill, int aperture)
156 gerbv_aperture_t *tool = drill->aperture[abs(aperture)];
161 static double tool_radius(gerbv_image_t *drill, int aperture)
163 gerbv_aperture_t *tool = drill->aperture[abs(aperture)];
165 /* Half a mil slop to decisively include points on boundary. */
166 return tool->parameter[0] / 2.0 + 0.0005;
169 static int holes_overlap(gerbv_image_t *drill, gerbv_net_t *a, gerbv_net_t *b)
171 double d = hypot(a->start_x - b->start_x, a->start_y - b->start_y);
173 return tool_radius(drill, a->aperture) >= d
174 || tool_radius(drill, b->aperture) >= d;
177 static int combine_holes(gerbv_image_t *drill, gerbv_net_t *hole,
180 CVectorHandle group, tmp;
181 int biggest_tool, ret = -1;
186 * Since we consider holes in order of decreasing size, the initial hole
187 * considered is by definition the biggest one we will find in a group.
189 biggest_r = tool_radius(drill, (biggest_tool = hole->aperture));
191 if (CVectorCreate(&group, sizeof (gerbv_net_t *), 10)) {
192 fprintf(stderr, _("%s: failed to allocate memory\n"), progname);
196 if (CVectorCreate(&tmp, sizeof (void *), 10)) {
197 fprintf(stderr, _("%s: failed to allocate memory\n"), progname);
203 * Breadth-first nearest neighbour search of holes. We negate the
204 * aperture to indicate which holes have been previously visited.
206 CVectorAddElement(group, &hole);
207 hole->aperture = -hole->aperture;
209 for (i = 0; i < CVectorSize(group); i++) {
212 CVectorGetElement(group, &hole, i);
214 assert(tool_type(drill, hole->aperture) == GERBV_APTYPE_CIRCLE);
215 assert(tool_radius(drill, hole->aperture) <= biggest_r);
217 xy[0] = hole->start_x; xy[1] = hole->start_y;
218 if (CNearTreeFindInSphere(t, biggest_r, 0, tmp, xy, 1) != 0) {
219 /* We should always should find at least one hole! */
220 fprintf(stderr, _("%s: fatal error searching holes\n"),
225 for (j = 0; j < CVectorSize(tmp); j++) {
226 /* I don't know why, but CNearTree returns a list
227 * of pointers to its internal copies of pointers
228 * to the objects in the tree. So we need this
229 * double indirection to get the actual hole. */
230 gerbv_net_t *newhole;
233 CVectorGetElement(tmp, &p, j);
234 newhole = *(void **)p;
236 if (newhole->aperture < 0)
237 continue; /* already visited */
239 if (holes_overlap(drill, hole, newhole)) {
240 CVectorAddElement(group, &newhole);
241 newhole->aperture = -newhole->aperture;
246 /* Compare each pair of matched points to find the longest slot. */
247 if (CVectorSize(group) > 1) {
248 int biggest_slot = 0;
251 for (i = 0; i < CVectorSize(group) - 1; i++) {
252 CVectorGetElement(group, &hole, i);
253 for (j = i+1; j < CVectorSize(group); j++) {
257 CVectorGetElement(group, &h2, j);
258 newlen = hypot(hole->start_x - h2->start_x,
259 hole->start_y - h2->start_y);
261 if (newlen > bestlen) {
262 hole->stop_x = h2->start_x;
263 hole->stop_y = h2->stop_y;
270 CVectorGetElement(group, &hole, biggest_slot);
271 hole->aperture = biggest_tool;
272 hole->aperture_state = GERBV_APERTURE_STATE_ON;
275 const char *fmt = ngettext(
276 "%s: merged %zu hole into slot (%.4f,%.4f)-(%.4f,%.4f)\n",
277 "%s: merged %zu holes into slot (%.4f,%.4f)-(%.4f,%.4f)\n",
280 fprintf(stderr, fmt, progname, CVectorSize(group),
281 hole->start_x, hole->start_y,
282 hole->stop_x, hole->stop_y);
285 /* The only hole we found was our original one, restore
287 CVectorGetElement(group, &hole, 0);
289 assert(hole->aperture < 0);
290 hole->aperture = -hole->aperture;
293 fprintf(stderr, _("%s: hole at (%.4f,%.4f) not merged\n"),
294 progname, hole->start_x, hole->start_y);
306 * Order two holes by hole diameter.
308 static gerbv_image_t *hsc_drill_data;
309 static int hole_size_cmp(const void *a_, const void *b_)
311 gerbv_net_t * const *a = a_, * const *b = b_;
312 gerbv_aperture_t *ta, *tb;
314 ta = hsc_drill_data->aperture[abs(a[0]->aperture)];
315 assert(ta->type == GERBV_APTYPE_CIRCLE);
317 tb = hsc_drill_data->aperture[abs(b[0]->aperture)];
318 assert(tb->type == GERBV_APTYPE_CIRCLE);
320 if (ta->parameter[0] > tb->parameter[0])
322 if (ta->parameter[0] < tb->parameter[0])
327 static void sort_holes_by_size(gerbv_image_t *drill, CVectorHandle work)
329 hsc_drill_data = drill;
330 qsort(work->array, work->size, work->elementsize, hole_size_cmp);
333 static int slotify(gerbv_image_t *drill)
335 CVectorHandle holes, work;
340 t = build_search_tree(drill);
342 fprintf(stderr, _("%s: failed to build search tree\n"),
347 CNearTreeObjects(t, &holes);
351 if (CVectorCreate(&work, sizeof (gerbv_net_t *), CVectorSize(holes))) {
352 fprintf(stderr, _("%s: failed to allocate memory\n"), progname);
356 memcpy(work->array, holes->array, holes->size * holes->elementsize);
357 work->size = holes->size;
358 sort_holes_by_size(drill, work);
360 for (i = 0; i < CVectorSize(work); i++) {
363 CVectorGetElement(work, &hole, i);
364 /* Skip holes we've already looked at */
365 if (hole->aperture < 0)
367 if (hole->aperture_state == GERBV_APERTURE_STATE_ON)
371 fprintf(stderr, _("%s: checking hole at (%.4f,%.4f) for overlaps\n"),
372 progname, hole->start_x, hole->start_y);
375 if (combine_holes(drill, hole, t) < 0) {
381 /* Clean out any holes we don't need anymore */
382 for (i = 0; i < CVectorSize(holes); i++) {
385 CVectorGetElement(holes, &hole, i);
386 if (hole->aperture < 0)
387 gerbv_image_delete_net(hole);
396 static int do_cmdline(int argc, char **argv, const char **outfile)
398 const char *sopts = SOPT_STRING;
401 XTRA_PACKED_LOPTS(lopts);
406 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
427 fprintf(stderr, _("%s: error: must specify a filename\n"),
433 if (optind + 1 < argc) {
434 fprintf(stderr, _("%s: error: excess command-line arguments\n"),
443 int main(int argc, char **argv)
445 const char *outfile = "/dev/stdout";
447 gerbv_image_t *drill;
451 switch (do_cmdline(argc, argv, &outfile)) {
452 case -1: return EXIT_FAILURE;
453 case 1: return EXIT_SUCCESS;
456 gp = gerbv_create_project();
460 gerbv_open_layer_from_filename(gp, argv[optind]);
466 drill = gp->file[0]->image;
467 if (drill->layertype != GERBV_LAYERTYPE_DRILL) {
468 fprintf(stderr, _("%s: %s: error: not a drill file\n"),
469 progname, argv[optind]);
474 if (slotify(drill) != 0) {
479 if (!gerbv_export_drill_file_from_image(outfile, drill, NULL))
482 gerbv_destroy_project(gp);