Index: polystitch.c =================================================================== --- polystitch.c (revision 16910) +++ polystitch.c (revision 16911) @@ -37,17 +37,12 @@ #include "obj_poly.h" #include "obj_poly_draw.h" -static pcb_poly_t *inner_poly, *outer_poly; -static pcb_layer_t *poly_layer; - -/* Given the X,Y, find the polygon and set inner_poly and poly_layer. */ -static void find_crosshair_poly(pcb_coord_t x, pcb_coord_t y) +/* Given the X,Y, find the inmost (closest) polygon */ +static pcb_poly_t *find_crosshair_poly(pcb_coord_t x, pcb_coord_t y) { double best = 0, dist; + pcb_poly_t *res = NULL; - inner_poly = NULL; - poly_layer = NULL; - PCB_POLY_VISIBLE_LOOP(PCB->Data); { /* layer, polygon */ @@ -57,9 +52,8 @@ pcb_coord_t dx = x - point->X; pcb_coord_t dy = y - point->Y; dist = (double)dx * dx + (double)dy * dy; - if (dist < best || inner_poly == NULL) { - inner_poly = polygon; - poly_layer = layer; + if ((dist < best) || (res == NULL)) { + res = polygon; best = dist; } } @@ -66,16 +60,17 @@ PCB_END_LOOP; } PCB_ENDALL_LOOP; - if (!inner_poly) { + + if (res == NULL) pcb_message(PCB_MSG_ERROR, "Cannot find any polygons"); - return; - } + + return res; } /* Set outer_poly to the enclosing poly. We assume there's only one. */ -static void find_enclosing_poly() +static pcb_poly_t *find_enclosing_poly(pcb_poly_t *inner_poly) { - outer_poly = NULL; + pcb_layer_t *poly_layer = inner_poly->parent.layer; PCB_POLY_LOOP(poly_layer); { @@ -84,20 +79,24 @@ if (polygon->BoundingBox.X1 <= inner_poly->BoundingBox.X1 && polygon->BoundingBox.X2 >= inner_poly->BoundingBox.X2 && polygon->BoundingBox.Y1 <= inner_poly->BoundingBox.Y1 && polygon->BoundingBox.Y2 >= inner_poly->BoundingBox.Y2) { - outer_poly = polygon; - return; + return polygon; } } PCB_END_LOOP; + pcb_message(PCB_MSG_ERROR, "Cannot find a polygon enclosing the one you selected"); + return NULL; } static int polystitch(int argc, const char **argv, pcb_coord_t x, pcb_coord_t y) { - find_crosshair_poly(x, y); + pcb_poly_t *inner_poly, *outer_poly; + + inner_poly = find_crosshair_poly(x, y); + if (inner_poly) { - find_enclosing_poly(); + outer_poly = find_enclosing_poly(inner_poly); if (outer_poly) { pcb_cardinal_t n, end;