polygon1.c: Tidy up label_contour() following node_label() changes
[geda-pcb/gde.git] / src / find.c
blobb24512a1e85ac2e27426362d8a0a364d51b94d1c
1 /* $Id$ */
3 /*
5 * COPYRIGHT
7 * PCB, interactive printed circuit board design
8 * Copyright (C) 1994,1995,1996, 2005 Thomas Nau
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 * Contact addresses for paper mail and Email:
25 * Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
26 * Thomas.Nau@rz.uni-ulm.de
32 * short description:
33 * - lists for pins and vias, lines, arcs, pads and for polygons are created.
34 * Every object that has to be checked is added to its list.
35 * Coarse searching is accomplished with the data rtrees.
36 * - there's no 'speed-up' mechanism for polygons because they are not used
37 * as often as other objects
38 * - the maximum distance between line and pin ... would depend on the angle
39 * between them. To speed up computation the limit is set to one half
40 * of the thickness of the objects (cause of square pins).
42 * PV: means pin or via (objects that connect layers)
43 * LO: all non PV objects (layer objects like lines, arcs, polygons, pads)
45 * 1. first, the LO or PV at the given coordinates is looked up
46 * 2. all LO connections to that PV are looked up next
47 * 3. lookup of all LOs connected to LOs from (2).
48 * This step is repeated until no more new connections are found.
49 * 4. lookup all PVs connected to the LOs from (2) and (3)
50 * 5. start again with (1) for all new PVs from (4)
52 * Intersection of line <--> circle:
53 * - calculate the signed distance from the line to the center,
54 * return false if abs(distance) > R
55 * - get the distance from the line <--> distancevector intersection to
56 * (X1,Y1) in range [0,1], return true if 0 <= distance <= 1
57 * - depending on (r > 1.0 or r < 0.0) check the distance of X2,Y2 or X1,Y1
58 * to X,Y
60 * Intersection of line <--> line:
61 * - see the description of 'LineLineIntersect()'
64 /* routines to find connections between pins, vias, lines...
66 #ifdef HAVE_CONFIG_H
67 #include "config.h"
68 #endif
70 #include <stdlib.h>
71 #ifdef HAVE_STRING_H
72 #include <string.h>
73 #endif
74 #include <math.h>
75 #include <setjmp.h>
76 #include <assert.h>
78 #ifdef HAVE_SYS_TIMES_H
79 #include <sys/times.h>
80 #endif
82 #include "global.h"
84 #include "crosshair.h"
85 #include "data.h"
86 #include "draw.h"
87 #include "error.h"
88 #include "find.h"
89 #include "mymem.h"
90 #include "misc.h"
91 #include "rtree.h"
92 #include "polygon.h"
93 #include "search.h"
94 #include "set.h"
95 #include "undo.h"
96 #include "rats.h"
98 #ifdef HAVE_LIBDMALLOC
99 #include <dmalloc.h>
100 #endif
102 #undef DEBUG
104 RCSID ("$Id$");
109 /* ---------------------------------------------------------------------------
110 * some local macros
113 #define EXPAND_BOUNDS(p) if (Bloat > 0) {\
114 (p)->BoundingBox.X1 -= Bloat; \
115 (p)->BoundingBox.X2 += Bloat; \
116 (p)->BoundingBox.Y1 -= Bloat; \
117 (p)->BoundingBox.Y2 += Bloat;}
119 #define SEPARATE(FP) \
121 int i; \
122 fputc('#', (FP)); \
123 for (i = Settings.CharPerLine; i; i--) \
124 fputc('=', (FP)); \
125 fputc('\n', (FP)); \
128 #define PADLIST_ENTRY(L,I) \
129 (((PadTypePtr *)PadList[(L)].Data)[(I)])
131 #define LINELIST_ENTRY(L,I) \
132 (((LineTypePtr *)LineList[(L)].Data)[(I)])
134 #define ARCLIST_ENTRY(L,I) \
135 (((ArcTypePtr *)ArcList[(L)].Data)[(I)])
137 #define RATLIST_ENTRY(I) \
138 (((RatTypePtr *)RatList.Data)[(I)])
140 #define POLYGONLIST_ENTRY(L,I) \
141 (((PolygonTypePtr *)PolygonList[(L)].Data)[(I)])
143 #define PVLIST_ENTRY(I) \
144 (((PinTypePtr *)PVList.Data)[(I)])
146 #define IS_PV_ON_RAT(PV, Rat) \
147 (IsPointOnLineEnd((PV)->X,(PV)->Y, (Rat)))
149 #define IS_PV_ON_ARC(PV, Arc) \
150 (TEST_FLAG(SQUAREFLAG, (PV)) ? \
151 IsArcInRectangle( \
152 (PV)->X -MAX(((PV)->Thickness+1)/2 +Bloat,0), (PV)->Y -MAX(((PV)->Thickness+1)/2 +Bloat,0), \
153 (PV)->X +MAX(((PV)->Thickness+1)/2 +Bloat,0), (PV)->Y +MAX(((PV)->Thickness+1)/2 +Bloat,0), \
154 (Arc)) : \
155 IsPointOnArc((PV)->X,(PV)->Y,MAX((PV)->Thickness/2.0 + fBloat,0.0), (Arc)))
157 #define IS_PV_ON_PAD(PV,Pad) \
158 ( IsPointInPad((PV)->X, (PV)->Y, MAX((PV)->Thickness/2 +Bloat,0), (Pad)))
160 #define LENGTH_TO_HUMAN(value) (Settings.grid_units_mm ? ((value) / 100000.0 * 25.4) : ((value) / 100.0))
161 #define LENGTH_DIGITS (Settings.grid_units_mm ? 4 : 2)
162 #define LENGTH_UNITS_STRING (Settings.grid_units_mm ? "mm" : "mils")
165 static DrcViolationType
166 *pcb_drc_violation_new (char *title,
167 char *explanation,
168 int x, int y,
169 int angle,
170 int have_measured,
171 double measured_value,
172 double required_value,
173 int value_digits,
174 const char *value_units,
175 int object_count,
176 long int *object_id_list,
177 int *object_type_list)
179 DrcViolationType *violation = malloc (sizeof (DrcViolationType));
181 violation->title = strdup (title);
182 violation->explanation = strdup (explanation);
183 violation->x = x;
184 violation->y = y;
185 violation->angle = angle;
186 violation->have_measured = have_measured;
187 violation->measured_value = measured_value;
188 violation->required_value = required_value;
189 violation->value_digits = value_digits;
190 violation->value_units = value_units;
191 violation->object_count = object_count;
192 violation->object_id_list = object_id_list;
193 violation->object_type_list = object_type_list;
195 return violation;
198 static void
199 pcb_drc_violation_free (DrcViolationType *violation)
201 free (violation->title);
202 free (violation->explanation);
203 free (violation);
206 static char drc_dialog_message[289] = {0};
207 static void
208 reset_drc_dialog_message(void)
210 drc_dialog_message[0] = 0;
211 if (gui->drc_gui != NULL)
213 gui->drc_gui->reset_drc_dialog_message ();
216 #ifdef __GNUC__
217 static void append_drc_dialog_message(const char *fmt, ...)
218 __attribute__ ((format (printf, 1, 2)));
219 #endif
220 static void
221 append_drc_dialog_message(const char *fmt, ...)
223 size_t len = strlen (drc_dialog_message),
224 remained = sizeof (drc_dialog_message) - len - 1;
225 va_list ap;
226 va_start (ap, fmt);
227 #ifdef HAVE_VSNPRINTF
228 vsnprintf (drc_dialog_message + len, remained, fmt, ap);
229 #else
230 vsprintf (drc_dialog_message + len, fmt, ap);
231 #endif
232 va_end (ap);
235 static void GotoError (void);
237 static void
238 append_drc_violation (DrcViolationType *violation)
240 if (gui->drc_gui != NULL)
242 gui->drc_gui->append_drc_violation (violation);
244 else
246 /* Fallback to formatting the violation message as text */
247 append_drc_dialog_message ("%s\n", violation->title);
248 append_drc_dialog_message (_("near (%.*f, %.*f)\n"),
249 LENGTH_DIGITS, LENGTH_TO_HUMAN (violation->x),
250 LENGTH_DIGITS, LENGTH_TO_HUMAN (violation->y));
251 GotoError ();
254 if (gui->drc_gui == NULL || gui->drc_gui->log_drc_violations )
256 Message (_("WARNING! Design Rule error - %s\n"), violation->title);
257 Message (_("near location (%.*f, %.*f)\n"),
258 LENGTH_DIGITS, LENGTH_TO_HUMAN (violation->x),
259 LENGTH_DIGITS, LENGTH_TO_HUMAN (violation->y));
263 * message when asked about continuing DRC checks after next
264 * violation is found.
266 #define DRC_CONTINUE _("Press Next to continue DRC checking")
267 #define DRC_NEXT _("Next")
268 #define DRC_CANCEL _("Cancel")
270 static int
271 throw_drc_dialog(void)
273 int r;
275 if (gui->drc_gui != NULL)
277 r = gui->drc_gui->throw_drc_dialog ();
279 else
281 /* Fallback to formatting the violation message as text */
282 append_drc_dialog_message (DRC_CONTINUE);
283 r = gui->confirm_dialog (drc_dialog_message, DRC_CANCEL, DRC_NEXT);
284 reset_drc_dialog_message();
286 return r;
289 /* ---------------------------------------------------------------------------
290 * some local types
292 * the two 'dummy' structs for PVs and Pads are necessary for creating
293 * connection lists which include the element's name
295 typedef struct
297 void **Data; /* pointer to index data */
298 Cardinal Location, /* currently used position */
299 DrawLocation, Number, /* number of objects in list */
300 Size;
302 ListType, *ListTypePtr;
304 /* ---------------------------------------------------------------------------
305 * some local identifiers
307 static float fBloat = 0.0;
308 static LocationType Bloat = 0;
309 static int TheFlag = FOUNDFLAG;
310 static int OldFlag = FOUNDFLAG;
311 static void *thing_ptr1, *thing_ptr2, *thing_ptr3;
312 static int thing_type;
313 static Boolean User = False; /* user action causing this */
314 static Boolean drc = False; /* whether to stop if finding something not found */
315 static Boolean IsBad = False;
316 static Cardinal drcerr_count; /* count of drc errors */
317 static Cardinal TotalP, TotalV, NumberOfPads[2];
318 static ListType LineList[MAX_LAYER], /* list of objects to */
319 PolygonList[MAX_LAYER], ArcList[MAX_LAYER], PadList[2], RatList, PVList;
321 /* ---------------------------------------------------------------------------
322 * some local prototypes
324 static Boolean LookupLOConnectionsToPVList (Boolean);
325 static Boolean LookupLOConnectionsToLOList (Boolean);
326 static Boolean LookupPVConnectionsToLOList (Boolean);
327 static Boolean LookupPVConnectionsToPVList (void);
328 static Boolean LookupLOConnectionsToLine (LineTypePtr, Cardinal, Boolean);
329 static Boolean LookupLOConnectionsToPad (PadTypePtr, Cardinal);
330 static Boolean LookupLOConnectionsToPolygon (PolygonTypePtr, Cardinal);
331 static Boolean LookupLOConnectionsToArc (ArcTypePtr, Cardinal);
332 static Boolean LookupLOConnectionsToRatEnd (PointTypePtr, Cardinal);
333 static Boolean IsRatPointOnLineEnd (PointTypePtr, LineTypePtr);
334 static Boolean ArcArcIntersect (ArcTypePtr, ArcTypePtr);
335 static Boolean PrepareNextLoop (FILE *);
336 static Boolean PrintElementConnections (ElementTypePtr, FILE *, Boolean);
337 static Boolean ListsEmpty (Boolean);
338 static Boolean DoIt (Boolean, Boolean);
339 static void PrintElementNameList (ElementTypePtr, FILE *);
340 static void PrintConnectionElementName (ElementTypePtr, FILE *);
341 static void PrintConnectionListEntry (char *, ElementTypePtr,
342 Boolean, FILE *);
343 static void PrintPadConnections (Cardinal, FILE *, Boolean);
344 static void PrintPinConnections (FILE *, Boolean);
345 static Boolean PrintAndSelectUnusedPinsAndPadsOfElement (ElementTypePtr,
346 FILE *);
347 static void DrawNewConnections (void);
348 static void ResetConnections (Boolean);
349 static void DumpList (void);
350 static void LocateError (LocationType *, LocationType *);
351 static void BuildObjectList (int *, long int **, int **);
352 static void GotoError (void);
353 static Boolean DRCFind (int, void *, void *, void *);
354 static Boolean ListStart (int, void *, void *, void *);
355 static Boolean LOTouchesLine (LineTypePtr Line, Cardinal LayerGroup);
356 static Boolean PVTouchesLine (LineTypePtr line);
357 static Boolean SetThing (int, void *, void *, void *);
359 /* ---------------------------------------------------------------------------
360 * some of the 'pad' routines are the same as for lines because the 'pad'
361 * struct starts with a line struct. See global.h for details
363 Boolean
364 LinePadIntersect (LineTypePtr Line, PadTypePtr Pad)
366 return LineLineIntersect ((Line), (LineTypePtr)Pad);
369 Boolean
370 ArcPadIntersect (ArcTypePtr Arc, PadTypePtr Pad)
372 return LineArcIntersect ((LineTypePtr) (Pad), (Arc));
375 static Boolean
376 ADD_PV_TO_LIST (PinTypePtr Pin)
378 if (User)
379 AddObjectToFlagUndoList (Pin->Element ? PIN_TYPE : VIA_TYPE,
380 Pin->Element ? Pin->Element : Pin, Pin, Pin);
381 SET_FLAG (TheFlag, Pin);
382 PVLIST_ENTRY (PVList.Number) = Pin;
383 PVList.Number++;
384 #ifdef DEBUG
385 if (PVList.Number > PVList.Size)
386 printf ("ADD_PV_TO_LIST overflow! num=%d size=%d\n", PVList.Number,
387 PVList.Size);
388 #endif
389 if (drc && !TEST_FLAG (SELECTEDFLAG, Pin))
390 return (SetThing (PIN_TYPE, Pin->Element, Pin, Pin));
391 return False;
394 static Boolean
395 ADD_PAD_TO_LIST (Cardinal L, PadTypePtr Pad)
397 if (User)
398 AddObjectToFlagUndoList (PAD_TYPE, Pad->Element, Pad, Pad);
399 SET_FLAG (TheFlag, Pad);
400 PADLIST_ENTRY ((L), PadList[(L)].Number) = Pad;
401 PadList[(L)].Number++;
402 #ifdef DEBUG
403 if (PadList[(L)].Number > PadList[(L)].Size)
404 printf ("ADD_PAD_TO_LIST overflow! lay=%d, num=%d size=%d\n", L,
405 PadList[(L)].Number, PadList[(L)].Size);
406 #endif
407 if (drc && !TEST_FLAG (SELECTEDFLAG, Pad))
408 return (SetThing (PAD_TYPE, Pad->Element, Pad, Pad));
409 return False;
412 static Boolean
413 ADD_LINE_TO_LIST (Cardinal L, LineTypePtr Ptr)
415 if (User)
416 AddObjectToFlagUndoList (LINE_TYPE, LAYER_PTR (L), (Ptr), (Ptr));
417 SET_FLAG (TheFlag, (Ptr));
418 LINELIST_ENTRY ((L), LineList[(L)].Number) = (Ptr);
419 LineList[(L)].Number++;
420 #ifdef DEBUG
421 if (LineList[(L)].Number > LineList[(L)].Size)
422 printf ("ADD_LINE_TO_LIST overflow! lay=%d, num=%d size=%d\n", L,
423 LineList[(L)].Number, LineList[(L)].Size);
424 #endif
425 if (drc && !TEST_FLAG (SELECTEDFLAG, (Ptr)))
426 return (SetThing (LINE_TYPE, LAYER_PTR (L), (Ptr), (Ptr)));
427 return False;
430 static Boolean
431 ADD_ARC_TO_LIST (Cardinal L, ArcTypePtr Ptr)
433 if (User)
434 AddObjectToFlagUndoList (ARC_TYPE, LAYER_PTR (L), (Ptr), (Ptr));
435 SET_FLAG (TheFlag, (Ptr));
436 ARCLIST_ENTRY ((L), ArcList[(L)].Number) = (Ptr);
437 ArcList[(L)].Number++;
438 #ifdef DEBUG
439 if (ArcList[(L)].Number > ArcList[(L)].Size)
440 printf ("ADD_ARC_TO_LIST overflow! lay=%d, num=%d size=%d\n", L,
441 ArcList[(L)].Number, ArcList[(L)].Size);
442 #endif
443 if (drc && !TEST_FLAG (SELECTEDFLAG, (Ptr)))
444 return (SetThing (ARC_TYPE, LAYER_PTR (L), (Ptr), (Ptr)));
445 return False;
448 static Boolean
449 ADD_RAT_TO_LIST (RatTypePtr Ptr)
451 if (User)
452 AddObjectToFlagUndoList (RATLINE_TYPE, (Ptr), (Ptr), (Ptr));
453 SET_FLAG (TheFlag, (Ptr));
454 RATLIST_ENTRY (RatList.Number) = (Ptr);
455 RatList.Number++;
456 #ifdef DEBUG
457 if (RatList.Number > RatList.Size)
458 printf ("ADD_RAT_TO_LIST overflow! num=%d size=%d\n",
459 RatList.Number, RatList.Size);
460 #endif
461 if (drc && !TEST_FLAG (SELECTEDFLAG, (Ptr)))
462 return (SetThing (RATLINE_TYPE, (Ptr), (Ptr), (Ptr)));
463 return False;
466 static Boolean
467 ADD_POLYGON_TO_LIST (Cardinal L, PolygonTypePtr Ptr)
469 if (User)
470 AddObjectToFlagUndoList (POLYGON_TYPE, LAYER_PTR (L), (Ptr), (Ptr));
471 SET_FLAG (TheFlag, (Ptr));
472 POLYGONLIST_ENTRY ((L), PolygonList[(L)].Number) = (Ptr);
473 PolygonList[(L)].Number++;
474 #ifdef DEBUG
475 if (PolygonList[(L)].Number > PolygonList[(L)].Size)
476 printf ("ADD_ARC_TO_LIST overflow! lay=%d, num=%d size=%d\n", L,
477 PolygonList[(L)].Number, PolygonList[(L)].Size);
478 #endif
479 if (drc && !TEST_FLAG (SELECTEDFLAG, (Ptr)))
480 return (SetThing (POLYGON_TYPE, LAYER_PTR (L), (Ptr), (Ptr)));
481 return False;
484 Boolean
485 PinLineIntersect (PinTypePtr PV, LineTypePtr Line)
487 /* IsLineInRectangle already has Bloat factor */
488 return TEST_FLAG (SQUAREFLAG,
489 PV) ? IsLineInRectangle (PV->X - (PV->Thickness + 1) / 2,
490 PV->Y - (PV->Thickness + 1) / 2,
491 PV->X + (PV->Thickness + 1) / 2,
492 PV->Y + (PV->Thickness + 1) / 2,
493 Line) : IsPointInPad (PV->X,
494 PV->Y,
495 MAX (PV->
496 Thickness
498 2.0 +
499 fBloat,
500 0.0),
501 (PadTypePtr)Line);
505 Boolean
506 SetThing (int type, void *ptr1, void *ptr2, void *ptr3)
508 thing_ptr1 = ptr1;
509 thing_ptr2 = ptr2;
510 thing_ptr3 = ptr3;
511 thing_type = type;
512 if (type == PIN_TYPE && ptr1 == NULL)
514 thing_ptr1 = ptr3;
515 thing_type = VIA_TYPE;
517 return True;
520 Boolean
521 BoxBoxIntersection (BoxTypePtr b1, BoxTypePtr b2)
523 if (b2->X2 < b1->X1 || b2->X1 > b1->X2)
524 return False;
525 if (b2->Y2 < b1->Y1 || b2->Y1 > b1->Y2)
526 return False;
527 return True;
530 static Boolean
531 PadPadIntersect (PadTypePtr p1, PadTypePtr p2)
533 return LinePadIntersect ((LineTypePtr) p1, p2);
536 static inline Boolean
537 PV_TOUCH_PV (PinTypePtr PV1, PinTypePtr PV2)
539 float t1, t2;
540 BoxType b1, b2;
542 t1 = MAX (PV1->Thickness / 2.0 + fBloat, 0);
543 t2 = MAX (PV2->Thickness / 2.0 + fBloat, 0);
544 if (IsPointOnPin (PV1->X, PV1->Y, t1, PV2)
545 || IsPointOnPin (PV2->X, PV2->Y, t2, PV1))
546 return True;
547 if (!TEST_FLAG (SQUAREFLAG, PV1) || !TEST_FLAG (SQUAREFLAG, PV2))
548 return False;
549 /* check for square/square overlap */
550 b1.X1 = PV1->X - t1;
551 b1.X2 = PV1->X + t1;
552 b1.Y1 = PV1->Y - t1;
553 b1.Y2 = PV1->Y + t1;
554 t2 = PV2->Thickness / 2.0;
555 b2.X1 = PV2->X - t2;
556 b2.X2 = PV2->X + t2;
557 b2.Y1 = PV2->Y - t2;
558 b2.Y2 = PV2->Y + t2;
559 return BoxBoxIntersection (&b1, &b2);
562 /* ---------------------------------------------------------------------------
563 * releases all allocated memory
565 void
566 FreeLayoutLookupMemory (void)
568 Cardinal i;
570 for (i = 0; i < max_layer; i++)
572 MYFREE (LineList[i].Data);
573 MYFREE (ArcList[i].Data);
574 MYFREE (PolygonList[i].Data);
576 MYFREE (PVList.Data);
577 MYFREE (RatList.Data);
580 void
581 FreeComponentLookupMemory (void)
583 MYFREE (PadList[0].Data);
584 MYFREE (PadList[1].Data);
587 /* ---------------------------------------------------------------------------
588 * allocates memory for component related stacks ...
589 * initializes index and sorts it by X1 and X2
591 void
592 InitComponentLookup (void)
594 Cardinal i;
596 /* initialize pad data; start by counting the total number
597 * on each of the two possible layers
599 NumberOfPads[COMPONENT_LAYER] = NumberOfPads[SOLDER_LAYER] = 0;
600 ALLPAD_LOOP (PCB->Data);
602 if (TEST_FLAG (ONSOLDERFLAG, pad))
603 NumberOfPads[SOLDER_LAYER]++;
604 else
605 NumberOfPads[COMPONENT_LAYER]++;
607 ENDALL_LOOP;
608 for (i = 0; i < 2; i++)
610 /* allocate memory for working list */
611 PadList[i].Data =
612 (void **) MyCalloc (NumberOfPads[i], sizeof (PadTypePtr),
613 "InitComponentLookup()");
615 /* clear some struct members */
616 PadList[i].Location = 0;
617 PadList[i].DrawLocation = 0;
618 PadList[i].Number = 0;
619 PadList[i].Size = NumberOfPads[i];
623 /* ---------------------------------------------------------------------------
624 * allocates memory for component related stacks ...
625 * initializes index and sorts it by X1 and X2
627 void
628 InitLayoutLookup (void)
630 Cardinal i;
632 /* initialize line arc and polygon data */
633 for (i = 0; i < max_layer; i++)
635 LayerTypePtr layer = LAYER_PTR (i);
637 if (layer->LineN)
639 /* allocate memory for line pointer lists */
640 LineList[i].Data =
641 (void **) MyCalloc (layer->LineN, sizeof (LineTypePtr),
642 "InitLayoutLookup()");
643 LineList[i].Size = layer->LineN;
645 if (layer->ArcN)
647 ArcList[i].Data =
648 (void **) MyCalloc (layer->ArcN, sizeof (ArcTypePtr),
649 "InitLayoutLookup()");
650 ArcList[i].Size = layer->ArcN;
654 /* allocate memory for polygon list */
655 if (layer->PolygonN)
657 PolygonList[i].Data = (void **) MyCalloc (layer->PolygonN,
658 sizeof (PolygonTypePtr),
659 "InitLayoutLookup()");
660 PolygonList[i].Size = layer->PolygonN;
663 /* clear some struct members */
664 LineList[i].Location = 0;
665 LineList[i].DrawLocation = 0;
666 LineList[i].Number = 0;
667 ArcList[i].Location = 0;
668 ArcList[i].DrawLocation = 0;
669 ArcList[i].Number = 0;
670 PolygonList[i].Location = 0;
671 PolygonList[i].DrawLocation = 0;
672 PolygonList[i].Number = 0;
675 if (PCB->Data->pin_tree)
676 TotalP = PCB->Data->pin_tree->size;
677 else
678 TotalP = 0;
679 if (PCB->Data->via_tree)
680 TotalV = PCB->Data->via_tree->size;
681 else
682 TotalV = 0;
683 /* allocate memory for 'new PV to check' list and clear struct */
684 PVList.Data = (void **) MyCalloc (TotalP + TotalV, sizeof (PinTypePtr),
685 "InitLayoutLookup()");
686 PVList.Size = TotalP + TotalV;
687 PVList.Location = 0;
688 PVList.DrawLocation = 0;
689 PVList.Number = 0;
690 /* Initialize ratline data */
691 RatList.Data = (void **) MyCalloc (PCB->Data->RatN, sizeof (RatTypePtr),
692 "InitLayoutLookup()");
693 RatList.Size = PCB->Data->RatN;
694 RatList.Location = 0;
695 RatList.DrawLocation = 0;
696 RatList.Number = 0;
699 struct pv_info
701 Cardinal layer;
702 PinType pv;
703 jmp_buf env;
706 static int
707 LOCtoPVline_callback (const BoxType * b, void *cl)
709 LineTypePtr line = (LineTypePtr) b;
710 struct pv_info *i = (struct pv_info *) cl;
712 if (!TEST_FLAG (TheFlag, line) && PinLineIntersect (&i->pv, line))
714 if (ADD_LINE_TO_LIST (i->layer, line))
715 longjmp (i->env, 1);
717 return 0;
720 static int
721 LOCtoPVarc_callback (const BoxType * b, void *cl)
723 ArcTypePtr arc = (ArcTypePtr) b;
724 struct pv_info *i = (struct pv_info *) cl;
726 if (!TEST_FLAG (TheFlag, arc) && IS_PV_ON_ARC (&i->pv, arc))
728 if (ADD_ARC_TO_LIST (i->layer, arc))
729 longjmp (i->env, 1);
731 return 0;
734 static int
735 LOCtoPVpad_callback (const BoxType * b, void *cl)
737 PadTypePtr pad = (PadTypePtr) b;
738 struct pv_info *i = (struct pv_info *) cl;
740 if (!TEST_FLAG (TheFlag, pad) && IS_PV_ON_PAD (&i->pv, pad) &&
741 ADD_PAD_TO_LIST (TEST_FLAG (ONSOLDERFLAG, pad) ? SOLDER_LAYER :
742 COMPONENT_LAYER, pad))
743 longjmp (i->env, 1);
744 return 0;
747 static int
748 LOCtoPVrat_callback (const BoxType * b, void *cl)
750 RatTypePtr rat = (RatTypePtr) b;
751 struct pv_info *i = (struct pv_info *) cl;
753 if (!TEST_FLAG (TheFlag, rat) && IS_PV_ON_RAT (&i->pv, rat) &&
754 ADD_RAT_TO_LIST (rat))
755 longjmp (i->env, 1);
756 return 0;
758 static int
759 LOCtoPVpoly_callback (const BoxType * b, void *cl)
761 PolygonTypePtr polygon = (PolygonTypePtr) b;
762 struct pv_info *i = (struct pv_info *) cl;
764 /* if the pin doesn't have a therm and polygon is clearing
765 * then it can't touch due to clearance, so skip the expensive
766 * test. If it does have a therm, you still need to test
767 * because it might not be inside the polygon, or it could
768 * be on an edge such that it doesn't actually touch.
770 if (!TEST_FLAG (TheFlag, polygon) && (TEST_THERM (i->layer, &i->pv)
771 || !TEST_FLAG (CLEARPOLYFLAG,
772 polygon)
773 || !i->pv.Clearance))
775 float wide = 0.5 * i->pv.Thickness + fBloat;
776 wide = MAX (wide, 0);
777 if (TEST_FLAG (SQUAREFLAG, &i->pv))
779 LocationType x1 = i->pv.X - (i->pv.Thickness + 1 + Bloat) / 2;
780 LocationType x2 = i->pv.X + (i->pv.Thickness + 1 + Bloat) / 2;
781 LocationType y1 = i->pv.Y - (i->pv.Thickness + 1 + Bloat) / 2;
782 LocationType y2 = i->pv.Y + (i->pv.Thickness + 1 + Bloat) / 2;
783 if (IsRectangleInPolygon (x1, y1, x2, y2, polygon)
784 && ADD_POLYGON_TO_LIST (i->layer, polygon))
785 longjmp (i->env, 1);
787 else if (TEST_FLAG (OCTAGONFLAG, &i->pv))
789 POLYAREA *oct = OctagonPoly (i->pv.X, i->pv.Y, i->pv.Thickness / 2);
790 if (isects (oct, polygon, True)
791 && ADD_POLYGON_TO_LIST (i->layer, polygon))
792 longjmp (i->env, 1);
794 else if (IsPointInPolygon (i->pv.X, i->pv.Y, wide,
795 polygon)
796 && ADD_POLYGON_TO_LIST (i->layer, polygon))
797 longjmp (i->env, 1);
799 return 0;
802 /* ---------------------------------------------------------------------------
803 * checks if a PV is connected to LOs, if it is, the LO is added to
804 * the appropriate list and the 'used' flag is set
806 static Boolean
807 LookupLOConnectionsToPVList (Boolean AndRats)
809 Cardinal layer;
810 struct pv_info info;
812 /* loop over all PVs currently on list */
813 while (PVList.Location < PVList.Number)
815 /* get pointer to data */
816 info.pv = *(PVLIST_ENTRY (PVList.Location));
817 EXPAND_BOUNDS (&info.pv);
819 /* check pads */
820 if (setjmp (info.env) == 0)
821 r_search (PCB->Data->pad_tree, (BoxType *) & info.pv, NULL,
822 LOCtoPVpad_callback, &info);
823 else
824 return True;
826 /* now all lines, arcs and polygons of the several layers */
827 for (layer = 0; layer < max_layer; layer++)
829 info.layer = layer;
830 /* add touching lines */
831 if (setjmp (info.env) == 0)
832 r_search (LAYER_PTR (layer)->line_tree, (BoxType *) & info.pv,
833 NULL, LOCtoPVline_callback, &info);
834 else
835 return True;
836 /* add touching arcs */
837 if (setjmp (info.env) == 0)
838 r_search (LAYER_PTR (layer)->arc_tree, (BoxType *) & info.pv,
839 NULL, LOCtoPVarc_callback, &info);
840 else
841 return True;
842 /* check all polygons */
843 if (setjmp (info.env) == 0)
844 r_search (LAYER_PTR (layer)->polygon_tree, (BoxType *) & info.pv,
845 NULL, LOCtoPVpoly_callback, &info);
846 else
847 return True;
849 /* Check for rat-lines that may intersect the PV */
850 if (AndRats)
852 if (setjmp (info.env) == 0)
853 r_search (PCB->Data->rat_tree, (BoxType *) & info.pv, NULL,
854 LOCtoPVrat_callback, &info);
855 else
856 return True;
858 PVList.Location++;
860 return False;
863 /* ---------------------------------------------------------------------------
864 * find all connections between LO at the current list position and new LOs
866 static Boolean
867 LookupLOConnectionsToLOList (Boolean AndRats)
869 Boolean done;
870 Cardinal i, group, layer, ratposition,
871 lineposition[MAX_LAYER],
872 polyposition[MAX_LAYER], arcposition[MAX_LAYER], padposition[2];
874 /* copy the current LO list positions; the original data is changed
875 * by 'LookupPVConnectionsToLOList()' which has to check the same
876 * list entries plus the new ones
878 for (i = 0; i < max_layer; i++)
880 lineposition[i] = LineList[i].Location;
881 polyposition[i] = PolygonList[i].Location;
882 arcposition[i] = ArcList[i].Location;
884 for (i = 0; i < 2; i++)
885 padposition[i] = PadList[i].Location;
886 ratposition = RatList.Location;
888 /* loop over all new LOs in the list; recurse until no
889 * more new connections in the layergroup were found
893 Cardinal *position;
895 if (AndRats)
897 position = &ratposition;
898 for (; *position < RatList.Number; (*position)++)
900 group = RATLIST_ENTRY (*position)->group1;
901 if (LookupLOConnectionsToRatEnd
902 (&(RATLIST_ENTRY (*position)->Point1), group))
903 return (True);
904 group = RATLIST_ENTRY (*position)->group2;
905 if (LookupLOConnectionsToRatEnd
906 (&(RATLIST_ENTRY (*position)->Point2), group))
907 return (True);
910 /* loop over all layergroups */
911 for (group = 0; group < max_layer; group++)
913 Cardinal entry;
915 for (entry = 0; entry < PCB->LayerGroups.Number[group]; entry++)
917 layer = PCB->LayerGroups.Entries[group][entry];
919 /* be aware that the layer number equal max_layer
920 * and max_layer+1 have a special meaning for pads
922 if (layer < max_layer)
924 /* try all new lines */
925 position = &lineposition[layer];
926 for (; *position < LineList[layer].Number; (*position)++)
927 if (LookupLOConnectionsToLine
928 (LINELIST_ENTRY (layer, *position), group, True))
929 return (True);
931 /* try all new arcs */
932 position = &arcposition[layer];
933 for (; *position < ArcList[layer].Number; (*position)++)
934 if (LookupLOConnectionsToArc
935 (ARCLIST_ENTRY (layer, *position), group))
936 return (True);
938 /* try all new polygons */
939 position = &polyposition[layer];
940 for (; *position < PolygonList[layer].Number; (*position)++)
941 if (LookupLOConnectionsToPolygon
942 (POLYGONLIST_ENTRY (layer, *position), group))
943 return (True);
945 else
947 /* try all new pads */
948 layer -= max_layer;
949 if (layer > 1)
951 Message (_("bad layer number %d max_layer=%d in find.c\n"),
952 layer, max_layer);
953 return False;
955 position = &padposition[layer];
956 for (; *position < PadList[layer].Number; (*position)++)
957 if (LookupLOConnectionsToPad
958 (PADLIST_ENTRY (layer, *position), group))
959 return (True);
964 /* check if all lists are done; Later for-loops
965 * may have changed the prior lists
967 done = !AndRats || ratposition >= RatList.Number;
968 for (layer = 0; layer < max_layer + 2; layer++)
970 if (layer < max_layer)
971 done = done &&
972 lineposition[layer] >= LineList[layer].Number
973 && arcposition[layer] >= ArcList[layer].Number
974 && polyposition[layer] >= PolygonList[layer].Number;
975 else
976 done = done
977 && padposition[layer - max_layer] >=
978 PadList[layer - max_layer].Number;
981 while (!done);
982 return (False);
985 static int
986 pv_pv_callback (const BoxType * b, void *cl)
988 PinTypePtr pin = (PinTypePtr) b;
989 struct pv_info *i = (struct pv_info *) cl;
991 if (!TEST_FLAG (TheFlag, pin) && PV_TOUCH_PV (&i->pv, pin))
993 if (TEST_FLAG (HOLEFLAG, pin))
995 SET_FLAG (WARNFLAG, pin);
996 Settings.RatWarn = True;
997 if (pin->Element)
998 Message (_("WARNING: Hole too close to pin.\n"));
999 else
1000 Message (_("WARNING: Hole too close to via.\n"));
1002 if (ADD_PV_TO_LIST (pin))
1003 longjmp (i->env, 1);
1005 return 0;
1008 /* ---------------------------------------------------------------------------
1009 * searches for new PVs that are connected to PVs on the list
1011 static Boolean
1012 LookupPVConnectionsToPVList (void)
1014 Cardinal save_place;
1015 struct pv_info info;
1018 /* loop over all PVs on list */
1019 save_place = PVList.Location;
1020 while (PVList.Location < PVList.Number)
1022 /* get pointer to data */
1023 info.pv = *(PVLIST_ENTRY (PVList.Location));
1024 EXPAND_BOUNDS (&info.pv);
1025 if (setjmp (info.env) == 0)
1026 r_search (PCB->Data->via_tree, (BoxType *) & info.pv, NULL,
1027 pv_pv_callback, &info);
1028 else
1029 return True;
1030 if (setjmp (info.env) == 0)
1031 r_search (PCB->Data->pin_tree, (BoxType *) & info.pv, NULL,
1032 pv_pv_callback, &info);
1033 else
1034 return True;
1035 PVList.Location++;
1037 PVList.Location = save_place;
1038 return (False);
1041 struct lo_info
1043 Cardinal layer;
1044 LineType line;
1045 PadType pad;
1046 ArcType arc;
1047 PolygonType polygon;
1048 RatType rat;
1049 jmp_buf env;
1052 static int
1053 pv_line_callback (const BoxType * b, void *cl)
1055 PinTypePtr pv = (PinTypePtr) b;
1056 struct lo_info *i = (struct lo_info *) cl;
1058 if (!TEST_FLAG (TheFlag, pv) && PinLineIntersect (pv, &i->line))
1060 if (TEST_FLAG (HOLEFLAG, pv))
1062 SET_FLAG (WARNFLAG, pv);
1063 Settings.RatWarn = True;
1064 Message (_("WARNING: Hole too close to line.\n"));
1066 if (ADD_PV_TO_LIST (pv))
1067 longjmp (i->env, 1);
1069 return 0;
1072 static int
1073 pv_pad_callback (const BoxType * b, void *cl)
1075 PinTypePtr pv = (PinTypePtr) b;
1076 struct lo_info *i = (struct lo_info *) cl;
1078 if (!TEST_FLAG (TheFlag, pv) && IS_PV_ON_PAD (pv, &i->pad))
1080 if (TEST_FLAG (HOLEFLAG, pv))
1082 SET_FLAG (WARNFLAG, pv);
1083 Settings.RatWarn = True;
1084 Message (_("WARNING: Hole too close to pad.\n"));
1086 if (ADD_PV_TO_LIST (pv))
1087 longjmp (i->env, 1);
1089 return 0;
1092 static int
1093 pv_arc_callback (const BoxType * b, void *cl)
1095 PinTypePtr pv = (PinTypePtr) b;
1096 struct lo_info *i = (struct lo_info *) cl;
1098 if (!TEST_FLAG (TheFlag, pv) && IS_PV_ON_ARC (pv, &i->arc))
1100 if (TEST_FLAG (HOLEFLAG, pv))
1102 SET_FLAG (WARNFLAG, pv);
1103 Settings.RatWarn = True;
1104 Message (_("WARNING: Hole touches arc.\n"));
1106 if (ADD_PV_TO_LIST (pv))
1107 longjmp (i->env, 1);
1109 return 0;
1112 static int
1113 pv_poly_callback (const BoxType * b, void *cl)
1115 PinTypePtr pv = (PinTypePtr) b;
1116 struct lo_info *i = (struct lo_info *) cl;
1118 /* note that holes in polygons are ok */
1119 if (!TEST_FLAG (TheFlag, pv) && (TEST_THERM (i->layer, pv) ||
1120 !TEST_FLAG (CLEARPOLYFLAG, &i->polygon) ||
1121 !pv->Clearance))
1123 if (TEST_FLAG (SQUAREFLAG, pv))
1125 LocationType x1, x2, y1, y2;
1126 x1 = pv->X - (pv->Thickness + 1 + Bloat) / 2;
1127 x2 = pv->X + (pv->Thickness + 1 + Bloat) / 2;
1128 y1 = pv->Y - (pv->Thickness + 1 + Bloat) / 2;
1129 y2 = pv->Y + (pv->Thickness + 1 + Bloat) / 2;
1130 if (IsRectangleInPolygon (x1, y1, x2, y2, &i->polygon)
1131 && ADD_PV_TO_LIST (pv))
1132 longjmp (i->env, 1);
1134 else if (TEST_FLAG (OCTAGONFLAG, pv))
1136 POLYAREA *oct = OctagonPoly (pv->X, pv->Y, pv->Thickness / 2);
1137 if (isects (oct, &i->polygon, True) && ADD_PV_TO_LIST (pv))
1138 longjmp (i->env, 1);
1140 else
1142 if (IsPointInPolygon
1143 (pv->X, pv->Y, pv->Thickness * 0.5 + fBloat, &i->polygon)
1144 && ADD_PV_TO_LIST (pv))
1145 longjmp (i->env, 1);
1148 return 0;
1151 static int
1152 pv_rat_callback (const BoxType * b, void *cl)
1154 PinTypePtr pv = (PinTypePtr) b;
1155 struct lo_info *i = (struct lo_info *) cl;
1157 /* rats can't cause DRC so there is no early exit */
1158 if (!TEST_FLAG (TheFlag, pv) && IS_PV_ON_RAT (pv, &i->rat))
1159 ADD_PV_TO_LIST (pv);
1160 return 0;
1163 /* ---------------------------------------------------------------------------
1164 * searches for new PVs that are connected to NEW LOs on the list
1165 * This routine updates the position counter of the lists too.
1167 static Boolean
1168 LookupPVConnectionsToLOList (Boolean AndRats)
1170 Cardinal layer;
1171 struct lo_info info;
1173 /* loop over all layers */
1174 for (layer = 0; layer < max_layer; layer++)
1176 /* do nothing if there are no PV's */
1177 if (TotalP + TotalV == 0)
1179 LineList[layer].Location = LineList[layer].Number;
1180 ArcList[layer].Location = ArcList[layer].Number;
1181 PolygonList[layer].Location = PolygonList[layer].Number;
1182 continue;
1185 /* check all lines */
1186 while (LineList[layer].Location < LineList[layer].Number)
1188 info.line = *(LINELIST_ENTRY (layer, LineList[layer].Location));
1189 EXPAND_BOUNDS (&info.line);
1190 if (setjmp (info.env) == 0)
1191 r_search (PCB->Data->via_tree, (BoxType *) & info.line, NULL,
1192 pv_line_callback, &info);
1193 else
1194 return True;
1195 if (setjmp (info.env) == 0)
1196 r_search (PCB->Data->pin_tree, (BoxType *) & info.line, NULL,
1197 pv_line_callback, &info);
1198 else
1199 return True;
1200 LineList[layer].Location++;
1203 /* check all arcs */
1204 while (ArcList[layer].Location < ArcList[layer].Number)
1206 info.arc = *(ARCLIST_ENTRY (layer, ArcList[layer].Location));
1207 EXPAND_BOUNDS (&info.arc);
1208 if (setjmp (info.env) == 0)
1209 r_search (PCB->Data->via_tree, (BoxType *) & info.arc, NULL,
1210 pv_arc_callback, &info);
1211 else
1212 return True;
1213 if (setjmp (info.env) == 0)
1214 r_search (PCB->Data->pin_tree, (BoxType *) & info.arc, NULL,
1215 pv_arc_callback, &info);
1216 else
1217 return True;
1218 ArcList[layer].Location++;
1221 /* now all polygons */
1222 info.layer = layer;
1223 while (PolygonList[layer].Location < PolygonList[layer].Number)
1225 info.polygon =
1226 *(POLYGONLIST_ENTRY (layer, PolygonList[layer].Location));
1227 EXPAND_BOUNDS (&info.polygon);
1228 if (setjmp (info.env) == 0)
1229 r_search (PCB->Data->via_tree, (BoxType *) & info.polygon, NULL,
1230 pv_poly_callback, &info);
1231 else
1232 return True;
1233 if (setjmp (info.env) == 0)
1234 r_search (PCB->Data->pin_tree, (BoxType *) & info.polygon, NULL,
1235 pv_poly_callback, &info);
1236 else
1237 return True;
1238 PolygonList[layer].Location++;
1242 /* loop over all pad-layers */
1243 for (layer = 0; layer < 2; layer++)
1245 /* do nothing if there are no PV's */
1246 if (TotalP + TotalV == 0)
1248 PadList[layer].Location = PadList[layer].Number;
1249 continue;
1252 /* check all pads; for a detailed description see
1253 * the handling of lines in this subroutine
1255 while (PadList[layer].Location < PadList[layer].Number)
1257 info.pad = *(PADLIST_ENTRY (layer, PadList[layer].Location));
1258 EXPAND_BOUNDS (&info.pad);
1259 if (setjmp (info.env) == 0)
1260 r_search (PCB->Data->via_tree, (BoxType *) & info.pad, NULL,
1261 pv_pad_callback, &info);
1262 else
1263 return True;
1264 if (setjmp (info.env) == 0)
1265 r_search (PCB->Data->pin_tree, (BoxType *) & info.pad, NULL,
1266 pv_pad_callback, &info);
1267 else
1268 return True;
1269 PadList[layer].Location++;
1273 /* do nothing if there are no PV's */
1274 if (TotalP + TotalV == 0)
1275 RatList.Location = RatList.Number;
1277 /* check all rat-lines */
1278 if (AndRats)
1280 while (RatList.Location < RatList.Number)
1282 info.rat = *(RATLIST_ENTRY (RatList.Location));
1283 r_search_pt (PCB->Data->via_tree, & info.rat.Point1, 1, NULL,
1284 pv_rat_callback, &info);
1285 r_search_pt (PCB->Data->via_tree, & info.rat.Point2, 1, NULL,
1286 pv_rat_callback, &info);
1287 r_search_pt (PCB->Data->pin_tree, & info.rat.Point1, 1, NULL,
1288 pv_rat_callback, &info);
1289 r_search_pt (PCB->Data->pin_tree, & info.rat.Point2, 1, NULL,
1290 pv_rat_callback, &info);
1292 RatList.Location++;
1295 return (False);
1299 pv_touch_callback (const BoxType * b, void *cl)
1301 PinTypePtr pin = (PinTypePtr) b;
1302 struct lo_info *i = (struct lo_info *) cl;
1304 if (!TEST_FLAG (TheFlag, pin) && PinLineIntersect (pin, &i->line))
1305 longjmp (i->env, 1);
1306 return 0;
1309 static Boolean
1310 PVTouchesLine (LineTypePtr line)
1312 struct lo_info info;
1314 info.line = *line;
1315 EXPAND_BOUNDS (&info.line);
1316 if (setjmp (info.env) == 0)
1317 r_search (PCB->Data->via_tree, (BoxType *) & info.line, NULL,
1318 pv_touch_callback, &info);
1319 else
1320 return True;
1321 if (setjmp (info.env) == 0)
1322 r_search (PCB->Data->pin_tree, (BoxType *) & info.line, NULL,
1323 pv_touch_callback, &info);
1324 else
1325 return True;
1327 return (False);
1330 /* ---------------------------------------------------------------------------
1331 * check if two arcs intersect
1332 * first we check for circle intersections,
1333 * then find the actual points of intersection
1334 * and test them to see if they are on both arcs
1336 * consider a, the distance from the center of arc 1
1337 * to the point perpendicular to the intersecting points.
1339 * a = (r1^2 - r2^2 + l^2)/(2l)
1341 * the perpendicular distance to the point of intersection
1342 * is then
1344 * d = sqrt(r1^2 - a^2)
1346 * the points of intersection would then be
1348 * x = X1 + a/l dx +- d/l dy
1349 * y = Y1 + a/l dy -+ d/l dx
1351 * where dx = X2 - X1 and dy = Y2 - Y1
1355 static Boolean
1356 ArcArcIntersect (ArcTypePtr Arc1, ArcTypePtr Arc2)
1358 register float x, y, dx, dy, r1, r2, a, d, l, t, t2;
1359 register LocationType pdx, pdy;
1360 BoxTypePtr box;
1361 BoxType box1, box2;
1363 pdx = Arc2->X - Arc1->X;
1364 pdy = Arc2->Y - Arc1->Y;
1365 l = pdx * pdx + pdy * pdy;
1366 t = MAX (0.5 * Arc1->Thickness + fBloat, 0.0);
1367 t2 = 0.5 * Arc2->Thickness;
1368 /* concentric arcs, simpler intersection conditions */
1369 if (l == 0.0)
1371 if ((Arc1->Width - t >= Arc2->Width - t2
1372 && Arc1->Width - t <=
1373 Arc2->Width + t2)
1374 || (Arc1->Width + t >=
1375 Arc2->Width - t2 && Arc1->Width + t <= Arc2->Width + t2))
1377 if ((Arc2->BoundingBox.X1 +
1378 MAX (Arc2->Thickness + Bloat,
1379 0) >= Arc1->BoundingBox.X1
1380 && Arc2->BoundingBox.X1 <=
1381 Arc1->BoundingBox.X2
1382 && Arc2->BoundingBox.Y1 +
1383 MAX (Arc2->Thickness + Bloat,
1384 0) >= Arc1->BoundingBox.Y1
1385 && Arc2->BoundingBox.Y1 <=
1386 Arc1->BoundingBox.Y2)
1387 || (Arc2->BoundingBox.X2 +
1388 MAX (Arc2->Thickness +
1389 Bloat,
1390 0) >=
1391 Arc1->BoundingBox.X1
1392 && Arc2->BoundingBox.X2 <=
1393 Arc1->BoundingBox.X2
1394 && Arc2->BoundingBox.Y2 +
1395 MAX (Arc2->Thickness +
1396 Bloat,
1397 0) >=
1398 Arc1->BoundingBox.Y1
1399 && Arc2->BoundingBox.Y2 <= Arc1->BoundingBox.Y2))
1400 return (True);
1402 return (False);
1404 r1 = Arc1->Width + t;
1405 r1 *= r1;
1406 r2 = Arc2->Width + t2;
1407 r2 *= r2;
1408 a = 0.5 * (r1 - r2 + l) / l;
1409 r1 = r1 / l;
1410 /* add a tiny positive number for round-off problems */
1411 d = r1 - a * a + 1e-5;
1412 /* the circles are too far apart to touch */
1413 if (d < 0)
1414 return (False);
1415 d = sqrt (d);
1416 x = Arc1->X + a * pdx;
1417 y = Arc1->Y + a * pdy;
1418 dx = d * pdx;
1419 dy = d * pdy;
1420 if (x + dy >= Arc1->BoundingBox.X1
1421 && x + dy <= Arc1->BoundingBox.X2
1422 && y - dx >= Arc1->BoundingBox.Y1
1423 && y - dx <= Arc1->BoundingBox.Y2
1424 && x + dy >= Arc2->BoundingBox.X1
1425 && x + dy <= Arc2->BoundingBox.X2
1426 && y - dx >= Arc2->BoundingBox.Y1 && y - dx <= Arc2->BoundingBox.Y2)
1427 return (True);
1429 if (x - dy >= Arc1->BoundingBox.X1
1430 && x - dy <= Arc1->BoundingBox.X2
1431 && y + dx >= Arc1->BoundingBox.Y1
1432 && y + dx <= Arc1->BoundingBox.Y2
1433 && x - dy >= Arc2->BoundingBox.X1
1434 && x - dy <= Arc2->BoundingBox.X2
1435 && y + dx >= Arc2->BoundingBox.Y1 && y + dx <= Arc2->BoundingBox.Y2)
1436 return (True);
1438 /* try the end points in case the ends don't actually pierce the outer radii */
1439 box = GetArcEnds (Arc1);
1440 box1 = *box;
1441 box = GetArcEnds (Arc2);
1442 box2 = *box;
1443 if (IsPointOnArc
1444 ((float) box1.X1, (float) box1.Y1, t,
1445 Arc2) || IsPointOnArc ((float) box1.X2, (float) box1.Y2, t, Arc2))
1446 return (True);
1448 if (IsPointOnArc
1449 ((float) box2.X1, (float) box2.Y1,
1450 MAX (t2 + fBloat, 0.0), Arc1)
1451 || IsPointOnArc ((float) box2.X2,
1452 (float) box2.Y2, MAX (t2 + fBloat, 0.0), Arc1))
1453 return (True);
1454 return (False);
1457 /* ---------------------------------------------------------------------------
1458 * Tests if point is same as line end point
1460 static Boolean
1461 IsRatPointOnLineEnd (PointTypePtr Point, LineTypePtr Line)
1463 if ((Point->X == Line->Point1.X
1464 && Point->Y == Line->Point1.Y)
1465 || (Point->X == Line->Point2.X && Point->Y == Line->Point2.Y))
1466 return (True);
1467 return (False);
1470 static void
1471 form_slanted_rectangle(PointType p[4],LineTypePtr l)
1472 /* writes vertices of a squared line */
1474 int dX= l->Point2.X - l->Point1.X, dY = l->Point2.Y - l->Point1.Y,
1475 w = l->Thickness;
1476 double dwx, dwy;
1477 if (dY == 0)
1479 dwx = w / 2; dwy = 0;
1481 else if (dX == 0)
1483 dwx = 0; dwy = w / 2;
1485 else
1486 {double r = sqrt (dX * (double) dX + dY * (double) dY) * 2;
1487 dwx = w / r * dX; dwy = w / r * dY;
1489 p[0].X = l->Point1.X - dwx + dwy; p[0].Y = l->Point1.Y - dwy - dwx;
1490 p[1].X = l->Point1.X - dwx - dwy; p[1].Y = l->Point1.Y - dwy + dwx;
1491 p[2].X = l->Point2.X + dwx - dwy; p[2].Y = l->Point2.Y + dwy + dwx;
1492 p[3].X = l->Point2.X + dwx + dwy; p[3].Y = l->Point2.Y + dwy - dwx;
1494 /* ---------------------------------------------------------------------------
1495 * checks if two lines intersect
1496 * from news FAQ:
1498 * Let A,B,C,D be 2-space position vectors. Then the directed line
1499 * segments AB & CD are given by:
1501 * AB=A+r(B-A), r in [0,1]
1502 * CD=C+s(D-C), s in [0,1]
1504 * If AB & CD intersect, then
1506 * A+r(B-A)=C+s(D-C), or
1508 * XA+r(XB-XA)=XC+s(XD-XC)
1509 * YA+r(YB-YA)=YC+s(YD-YC) for some r,s in [0,1]
1511 * Solving the above for r and s yields
1513 * (YA-YC)(XD-XC)-(XA-XC)(YD-YC)
1514 * r = ----------------------------- (eqn 1)
1515 * (XB-XA)(YD-YC)-(YB-YA)(XD-XC)
1517 * (YA-YC)(XB-XA)-(XA-XC)(YB-YA)
1518 * s = ----------------------------- (eqn 2)
1519 * (XB-XA)(YD-YC)-(YB-YA)(XD-XC)
1521 * Let I be the position vector of the intersection point, then
1523 * I=A+r(B-A) or
1525 * XI=XA+r(XB-XA)
1526 * YI=YA+r(YB-YA)
1528 * By examining the values of r & s, you can also determine some
1529 * other limiting conditions:
1531 * If 0<=r<=1 & 0<=s<=1, intersection exists
1532 * r<0 or r>1 or s<0 or s>1 line segments do not intersect
1534 * If the denominator in eqn 1 is zero, AB & CD are parallel
1535 * If the numerator in eqn 1 is also zero, AB & CD are coincident
1537 * If the intersection point of the 2 lines are needed (lines in this
1538 * context mean infinite lines) regardless whether the two line
1539 * segments intersect, then
1541 * If r>1, I is located on extension of AB
1542 * If r<0, I is located on extension of BA
1543 * If s>1, I is located on extension of CD
1544 * If s<0, I is located on extension of DC
1546 * Also note that the denominators of eqn 1 & 2 are identical.
1549 Boolean
1550 LineLineIntersect (LineTypePtr Line1, LineTypePtr Line2)
1552 register float dx, dy, dx1, dy1, s, r;
1553 if (TEST_FLAG (SQUAREFLAG, Line1))/* pretty reckless recursion */
1555 PointType p[4];form_slanted_rectangle(p,Line1);
1556 return IsLineInQuadrangle(p,Line2);
1558 /* here come only round Line1 because IsLineInQuadrangle()
1559 calls LineLineIntersect() with first argument rounded*/
1560 if (TEST_FLAG (SQUAREFLAG, Line2))
1562 PointType p[4];form_slanted_rectangle(p,Line2);
1563 return IsLineInQuadrangle(p,Line1);
1565 /* now all lines are round */
1567 #if 0
1568 if (Line1->BoundingBox.X1 - Bloat > Line2->BoundBoxing.X2
1569 || Line1->BoundingBox.X2 + Bloat < Line2->BoundingBox.X1
1570 || Line1->BoundingBox.Y1 - Bloat < Line2->BoundingBox.Y2
1571 || Line1->BoundingBox.Y2 + Bloat < Line2->BoundingBox.Y1)
1572 return False;
1573 #endif
1575 /* setup some constants */
1576 dx = (float) (Line1->Point2.X - Line1->Point1.X);
1577 dy = (float) (Line1->Point2.Y - Line1->Point1.Y);
1578 dx1 = (float) (Line1->Point1.X - Line2->Point1.X);
1579 dy1 = (float) (Line1->Point1.Y - Line2->Point1.Y);
1580 s = dy1 * dx - dx1 * dy;
1583 dx * (float) (Line2->Point2.Y -
1584 Line2->Point1.Y) -
1585 dy * (float) (Line2->Point2.X - Line2->Point1.X);
1587 /* handle parallel lines */
1588 if (r == 0.0)
1590 /* at least one of the two end points of one segment
1591 * has to have a minimum distance to the other segment
1593 * a first quick check is to see if the distance between
1594 * the two lines is less then their half total thickness
1596 register float distance;
1598 /* perhaps line 1 is really just a point */
1599 if ((dx == 0) && (dy == 0))
1600 return IsPointInPad
1601 (Line1->Point1.X,
1602 Line1->Point1.Y,
1603 MAX (Line1->Thickness / 2 +
1604 Bloat, 0),
1605 (PadTypePtr) Line2);
1606 s = s * s / (dx * dx + dy * dy);
1609 distance =
1610 MAX ((float) 0.5 *
1611 (Line1->Thickness + Line2->Thickness) + fBloat, 0.0);
1612 distance *= distance;
1613 if (s > distance)
1614 return (False);
1615 if (IsPointInPad (Line2->Point1.
1617 Line2->Point1.
1619 MAX (Line2->
1620 Thickness
1621 / 2 +
1622 Bloat, 0),
1623 (PadTypePtr)
1624 Line1)
1625 || IsPointInPad (Line2->
1626 Point2.X,
1627 Line2->
1628 Point2.Y,
1629 MAX (Line2->
1630 Thickness
1631 / 2 + Bloat, 0), (PadTypePtr) Line1))
1632 return (True);
1633 return ((IsPointInPad (Line1->Point1.
1635 Line1->Point1.
1637 MAX (Line1->
1638 Thickness
1639 / 2 +
1640 Bloat, 0),
1641 (PadTypePtr)
1642 Line2)
1643 || IsPointInPad (Line1->
1644 Point2.X,
1645 Line1->
1646 Point2.Y,
1647 MAX (Line1->
1648 Thickness
1649 / 2 + Bloat, 0), (PadTypePtr) Line2)));
1651 else
1653 s /= r;
1655 (dy1 *
1656 (float) (Line2->Point2.X -
1657 Line2->Point1.X) -
1658 dx1 * (float) (Line2->Point2.Y - Line2->Point1.Y)) / r;
1660 /* intersection is at least on AB */
1661 if (r >= 0.0 && r <= 1.0)
1663 if (s >= 0.0 && s <= 1.0)
1664 return (True);
1666 /* intersection on AB and extension of CD */
1667 return (s < 0.0 ?
1668 IsPointInPad
1669 (Line2->Point1.X,
1670 Line2->Point1.Y,
1671 MAX (0.5 *
1672 Line2->Thickness +
1673 fBloat, 0.0),
1674 (PadTypePtr)Line1) :
1675 IsPointInPad
1676 (Line2->Point2.X,
1677 Line2->Point2.Y,
1678 MAX (0.5 * Line2->Thickness + fBloat, 0.0), (PadTypePtr)Line1));
1681 /* intersection is at least on CD */
1682 if (s >= 0.0 && s <= 1.0)
1684 /* intersection on CD and extension of AB */
1685 return (r < 0.0 ?
1686 IsPointInPad
1687 (Line1->Point1.X,
1688 Line1->Point1.Y,
1689 MAX (Line1->Thickness /
1690 2.0 + fBloat, 0.0),
1691 (PadTypePtr)Line2) :
1692 IsPointInPad
1693 (Line1->Point2.X,
1694 Line1->Point2.Y,
1695 MAX (Line1->Thickness / 2.0 + fBloat, 0.0), (PadTypePtr)Line2));
1698 /* no intersection of zero-width lines but maybe of thick lines;
1699 * Must check each end point to exclude intersection
1701 if (IsPointInPad (Line1->Point1.X, Line1->Point1.Y,
1702 Line1->Thickness / 2.0 + fBloat, (PadTypePtr)Line2))
1703 return True;
1704 if (IsPointInPad (Line1->Point2.X, Line1->Point2.Y,
1705 Line1->Thickness / 2.0 + fBloat, (PadTypePtr)Line2))
1706 return True;
1707 if (IsPointInPad (Line2->Point1.X, Line2->Point1.Y,
1708 Line2->Thickness / 2.0 + fBloat, (PadTypePtr)Line1))
1709 return True;
1710 return IsPointInPad (Line2->Point2.X, Line2->Point2.Y,
1711 Line2->Thickness / 2.0 + fBloat, (PadTypePtr)Line1);
1715 /*---------------------------------------------------
1717 * Check for line intersection with an arc
1719 * Mostly this is like the circle/line intersection
1720 * found in IsPointOnLine (search.c) see the detailed
1721 * discussion for the basics there.
1723 * Since this is only an arc, not a full circle we need
1724 * to find the actual points of intersection with the
1725 * circle, and see if they are on the arc.
1727 * To do this, we translate along the line from the point Q
1728 * plus or minus a distance delta = sqrt(Radius^2 - d^2)
1729 * but it's handy to normalize with respect to l, the line
1730 * length so a single projection is done (e.g. we don't first
1731 * find the point Q
1733 * The projection is now of the form
1735 * Px = X1 + (r +- r2)(X2 - X1)
1736 * Py = Y1 + (r +- r2)(Y2 - Y1)
1738 * Where r2 sqrt(Radius^2 l^2 - d^2)/l^2
1739 * note that this is the variable d, not the symbol d described in IsPointOnLine
1740 * (variable d = symbol d * l)
1742 * The end points are hell so they are checked individually
1744 Boolean
1745 LineArcIntersect (LineTypePtr Line, ArcTypePtr Arc)
1747 register float dx, dy, dx1, dy1, l, d, r, r2, Radius;
1748 BoxTypePtr box;
1750 dx = (float) (Line->Point2.X - Line->Point1.X);
1751 dy = (float) (Line->Point2.Y - Line->Point1.Y);
1752 dx1 = (float) (Line->Point1.X - Arc->X);
1753 dy1 = (float) (Line->Point1.Y - Arc->Y);
1754 l = dx * dx + dy * dy;
1755 d = dx * dy1 - dy * dx1;
1756 d *= d;
1758 /* use the larger diameter circle first */
1759 Radius =
1760 Arc->Width + MAX (0.5 * (Arc->Thickness + Line->Thickness) + fBloat, 0.0);
1761 Radius *= Radius;
1762 r2 = Radius * l - d;
1763 /* projection doesn't even intersect circle when r2 < 0 */
1764 if (r2 < 0)
1765 return (False);
1766 /* check the ends of the line in case the projected point */
1767 /* of intersection is beyond the line end */
1768 if (IsPointOnArc
1769 (Line->Point1.X, Line->Point1.Y,
1770 MAX (0.5 * Line->Thickness + fBloat, 0.0), Arc))
1771 return (True);
1772 if (IsPointOnArc
1773 (Line->Point2.X, Line->Point2.Y,
1774 MAX (0.5 * Line->Thickness + fBloat, 0.0), Arc))
1775 return (True);
1776 if (l == 0.0)
1777 return (False);
1778 r2 = sqrt (r2);
1779 Radius = -(dx * dx1 + dy * dy1);
1780 r = (Radius + r2) / l;
1781 if (r >= 0 && r <= 1
1782 && IsPointOnArc (Line->Point1.X + r * dx,
1783 Line->Point1.Y + r * dy,
1784 MAX (0.5 * Line->Thickness + fBloat, 0.0), Arc))
1785 return (True);
1786 r = (Radius - r2) / l;
1787 if (r >= 0 && r <= 1
1788 && IsPointOnArc (Line->Point1.X + r * dx,
1789 Line->Point1.Y + r * dy,
1790 MAX (0.5 * Line->Thickness + fBloat, 0.0), Arc))
1791 return (True);
1792 /* check arc end points */
1793 box = GetArcEnds (Arc);
1794 if (IsPointInPad (box->X1, box->Y1, Arc->Thickness * 0.5 + fBloat, (PadTypePtr)Line))
1795 return True;
1796 if (IsPointInPad (box->X2, box->Y2, Arc->Thickness * 0.5 + fBloat, (PadTypePtr)Line))
1797 return True;
1798 return False;
1801 static int
1802 LOCtoArcLine_callback (const BoxType * b, void *cl)
1804 LineTypePtr line = (LineTypePtr) b;
1805 struct lo_info *i = (struct lo_info *) cl;
1807 if (!TEST_FLAG (TheFlag, line) && LineArcIntersect (line, &i->arc))
1809 if (ADD_LINE_TO_LIST (i->layer, line))
1810 longjmp (i->env, 1);
1812 return 0;
1815 static int
1816 LOCtoArcArc_callback (const BoxType * b, void *cl)
1818 ArcTypePtr arc = (ArcTypePtr) b;
1819 struct lo_info *i = (struct lo_info *) cl;
1821 if (!arc->Thickness)
1822 return 0;
1823 if (!TEST_FLAG (TheFlag, arc) && ArcArcIntersect (&i->arc, arc))
1825 if (ADD_ARC_TO_LIST (i->layer, arc))
1826 longjmp (i->env, 1);
1828 return 0;
1831 static int
1832 LOCtoArcPad_callback (const BoxType * b, void *cl)
1834 PadTypePtr pad = (PadTypePtr) b;
1835 struct lo_info *i = (struct lo_info *) cl;
1837 if (!TEST_FLAG (TheFlag, pad) && i->layer ==
1838 (TEST_FLAG (ONSOLDERFLAG, pad) ? SOLDER_LAYER : COMPONENT_LAYER)
1839 && ArcPadIntersect (&i->arc, pad) && ADD_PAD_TO_LIST (i->layer, pad))
1840 longjmp (i->env, 1);
1841 return 0;
1844 /* ---------------------------------------------------------------------------
1845 * searches all LOs that are connected to the given arc on the given
1846 * layergroup. All found connections are added to the list
1848 * the notation that is used is:
1849 * Xij means Xj at arc i
1851 static Boolean
1852 LookupLOConnectionsToArc (ArcTypePtr Arc, Cardinal LayerGroup)
1854 Cardinal entry;
1855 LocationType xlow, xhigh;
1856 struct lo_info info;
1858 /* the maximum possible distance */
1859 xlow = Arc->BoundingBox.X1 - MAX (MAX_PADSIZE, MAX_LINESIZE) / 2;
1860 xhigh = Arc->BoundingBox.X2 + MAX (MAX_PADSIZE, MAX_LINESIZE) / 2;
1862 info.arc = *Arc;
1863 EXPAND_BOUNDS (&info.arc);
1864 /* loop over all layers of the group */
1865 for (entry = 0; entry < PCB->LayerGroups.Number[LayerGroup]; entry++)
1867 Cardinal layer, i;
1869 layer = PCB->LayerGroups.Entries[LayerGroup][entry];
1871 /* handle normal layers */
1872 if (layer < max_layer)
1874 PolygonTypePtr polygon;
1876 info.layer = layer;
1877 /* add arcs */
1878 if (setjmp (info.env) == 0)
1879 r_search (LAYER_PTR (layer)->line_tree, &info.arc.BoundingBox,
1880 NULL, LOCtoArcLine_callback, &info);
1881 else
1882 return True;
1884 if (setjmp (info.env) == 0)
1885 r_search (LAYER_PTR (layer)->arc_tree, &info.arc.BoundingBox,
1886 NULL, LOCtoArcArc_callback, &info);
1887 else
1888 return True;
1890 /* now check all polygons */
1891 i = 0;
1892 polygon = PCB->Data->Layer[layer].Polygon;
1893 for (; i < PCB->Data->Layer[layer].PolygonN; i++, polygon++)
1894 if (!TEST_FLAG (TheFlag, polygon) && IsArcInPolygon (Arc, polygon)
1895 && ADD_POLYGON_TO_LIST (layer, polygon))
1896 return True;
1898 else
1900 info.layer = layer - max_layer;
1901 if (setjmp (info.env) == 0)
1902 r_search (PCB->Data->pad_tree, &info.arc.BoundingBox, NULL,
1903 LOCtoArcPad_callback, &info);
1904 else
1905 return True;
1908 return (False);
1911 static int
1912 LOCtoLineLine_callback (const BoxType * b, void *cl)
1914 LineTypePtr line = (LineTypePtr) b;
1915 struct lo_info *i = (struct lo_info *) cl;
1917 if (!TEST_FLAG (TheFlag, line) && LineLineIntersect (&i->line, line))
1919 if (ADD_LINE_TO_LIST (i->layer, line))
1920 longjmp (i->env, 1);
1922 return 0;
1925 static int
1926 LOCtoLineArc_callback (const BoxType * b, void *cl)
1928 ArcTypePtr arc = (ArcTypePtr) b;
1929 struct lo_info *i = (struct lo_info *) cl;
1931 if (!arc->Thickness)
1932 return 0;
1933 if (!TEST_FLAG (TheFlag, arc) && LineArcIntersect (&i->line, arc))
1935 if (ADD_ARC_TO_LIST (i->layer, arc))
1936 longjmp (i->env, 1);
1938 return 0;
1941 static int
1942 LOCtoLineRat_callback (const BoxType * b, void *cl)
1944 RatTypePtr rat = (RatTypePtr) b;
1945 struct lo_info *i = (struct lo_info *) cl;
1947 if (!TEST_FLAG (TheFlag, rat))
1949 if ((rat->group1 == i->layer)
1950 && IsRatPointOnLineEnd (&rat->Point1, &i->line))
1952 if (ADD_RAT_TO_LIST (rat))
1953 longjmp (i->env, 1);
1955 else if ((rat->group2 == i->layer)
1956 && IsRatPointOnLineEnd (&rat->Point2, &i->line))
1958 if (ADD_RAT_TO_LIST (rat))
1959 longjmp (i->env, 1);
1962 return 0;
1965 static int
1966 LOCtoLinePad_callback (const BoxType * b, void *cl)
1968 PadTypePtr pad = (PadTypePtr) b;
1969 struct lo_info *i = (struct lo_info *) cl;
1971 if (!TEST_FLAG (TheFlag, pad) && i->layer ==
1972 (TEST_FLAG (ONSOLDERFLAG, pad) ? SOLDER_LAYER : COMPONENT_LAYER)
1973 && LinePadIntersect (&i->line, pad) && ADD_PAD_TO_LIST (i->layer, pad))
1974 longjmp (i->env, 1);
1975 return 0;
1978 /* ---------------------------------------------------------------------------
1979 * searches all LOs that are connected to the given line on the given
1980 * layergroup. All found connections are added to the list
1982 * the notation that is used is:
1983 * Xij means Xj at line i
1985 static Boolean
1986 LookupLOConnectionsToLine (LineTypePtr Line, Cardinal LayerGroup,
1987 Boolean PolysTo)
1989 Cardinal entry;
1990 struct lo_info info;
1992 info.line = *Line;
1993 info.layer = LayerGroup;
1994 EXPAND_BOUNDS (&info.line)
1995 /* add the new rat lines */
1996 if (setjmp (info.env) == 0)
1997 r_search (PCB->Data->rat_tree, &info.line.BoundingBox, NULL,
1998 LOCtoLineRat_callback, &info);
1999 else
2000 return True;
2002 /* loop over all layers of the group */
2003 for (entry = 0; entry < PCB->LayerGroups.Number[LayerGroup]; entry++)
2005 Cardinal layer;
2007 layer = PCB->LayerGroups.Entries[LayerGroup][entry];
2009 /* handle normal layers */
2010 if (layer < max_layer)
2012 PolygonTypePtr polygon;
2014 info.layer = layer;
2015 /* add lines */
2016 if (setjmp (info.env) == 0)
2017 r_search (LAYER_PTR (layer)->line_tree, (BoxType *) & info.line,
2018 NULL, LOCtoLineLine_callback, &info);
2019 else
2020 return True;
2021 /* add arcs */
2022 if (setjmp (info.env) == 0)
2023 r_search (LAYER_PTR (layer)->arc_tree, (BoxType *) & info.line,
2024 NULL, LOCtoLineArc_callback, &info);
2025 else
2026 return True;
2027 /* now check all polygons */
2028 if (PolysTo)
2030 Cardinal i = 0;
2031 polygon = PCB->Data->Layer[layer].Polygon;
2032 for (; i < PCB->Data->Layer[layer].PolygonN; i++, polygon++)
2033 if (!TEST_FLAG
2034 (TheFlag, polygon) && IsLineInPolygon (Line, polygon)
2035 && ADD_POLYGON_TO_LIST (layer, polygon))
2036 return True;
2039 else
2041 /* handle special 'pad' layers */
2042 info.layer = layer - max_layer;
2043 if (setjmp (info.env) == 0)
2044 r_search (PCB->Data->pad_tree, &info.line.BoundingBox, NULL,
2045 LOCtoLinePad_callback, &info);
2046 else
2047 return True;
2050 return (False);
2053 static int
2054 LOT_Linecallback (const BoxType * b, void *cl)
2056 LineTypePtr line = (LineTypePtr) b;
2057 struct lo_info *i = (struct lo_info *) cl;
2059 if (!TEST_FLAG (TheFlag, line) && LineLineIntersect (&i->line, line))
2060 longjmp (i->env, 1);
2061 return 0;
2064 static int
2065 LOT_Arccallback (const BoxType * b, void *cl)
2067 ArcTypePtr arc = (ArcTypePtr) b;
2068 struct lo_info *i = (struct lo_info *) cl;
2070 if (!arc->Thickness)
2071 return 0;
2072 if (!TEST_FLAG (TheFlag, arc) && LineArcIntersect (&i->line, arc))
2073 longjmp (i->env, 1);
2074 return 0;
2077 static int
2078 LOT_Padcallback (const BoxType * b, void *cl)
2080 PadTypePtr pad = (PadTypePtr) b;
2081 struct lo_info *i = (struct lo_info *) cl;
2083 if (!TEST_FLAG (TheFlag, pad) && i->layer ==
2084 (TEST_FLAG (ONSOLDERFLAG, pad) ? SOLDER_LAYER : COMPONENT_LAYER)
2085 && LinePadIntersect (&i->line, pad))
2086 longjmp (i->env, 1);
2087 return 0;
2090 static Boolean
2091 LOTouchesLine (LineTypePtr Line, Cardinal LayerGroup)
2093 Cardinal entry;
2094 Cardinal i;
2095 struct lo_info info;
2098 /* the maximum possible distance */
2100 info.line = *Line;
2101 EXPAND_BOUNDS (&info.line);
2103 /* loop over all layers of the group */
2104 for (entry = 0; entry < PCB->LayerGroups.Number[LayerGroup]; entry++)
2106 Cardinal layer = PCB->LayerGroups.Entries[LayerGroup][entry];
2108 /* handle normal layers */
2109 if (layer < max_layer)
2111 PolygonTypePtr polygon;
2113 /* find the first line that touches coordinates */
2115 if (setjmp (info.env) == 0)
2116 r_search (LAYER_PTR (layer)->line_tree, (BoxType *) & info.line,
2117 NULL, LOT_Linecallback, &info);
2118 else
2119 return (True);
2120 if (setjmp (info.env) == 0)
2121 r_search (LAYER_PTR (layer)->arc_tree, (BoxType *) & info.line,
2122 NULL, LOT_Arccallback, &info);
2123 else
2124 return (True);
2126 /* now check all polygons */
2127 i = 0;
2128 polygon = PCB->Data->Layer[layer].Polygon;
2129 for (; i < PCB->Data->Layer[layer].PolygonN; i++, polygon++)
2130 if (!TEST_FLAG (TheFlag, polygon)
2131 && IsLineInPolygon (Line, polygon))
2132 return (True);
2134 else
2136 /* handle special 'pad' layers */
2137 info.layer = layer - max_layer;
2138 if (setjmp (info.env) == 0)
2139 r_search (PCB->Data->pad_tree, &info.line.BoundingBox, NULL,
2140 LOT_Padcallback, &info);
2141 else
2142 return True;
2145 return (False);
2148 struct rat_info
2150 Cardinal layer;
2151 PointTypePtr Point;
2152 jmp_buf env;
2155 static int
2156 LOCtoRat_callback (const BoxType * b, void *cl)
2158 LineTypePtr line = (LineTypePtr) b;
2159 struct rat_info *i = (struct rat_info *) cl;
2161 if (!TEST_FLAG (TheFlag, line) &&
2162 ((line->Point1.X == i->Point->X &&
2163 line->Point1.Y == i->Point->Y) ||
2164 (line->Point2.X == i->Point->X && line->Point2.Y == i->Point->Y)))
2166 if (ADD_LINE_TO_LIST (i->layer, line))
2167 longjmp (i->env, 1);
2169 return 0;
2171 static int
2172 PolygonToRat_callback (const BoxType * b, void *cl)
2174 PolygonTypePtr polygon = (PolygonTypePtr) b;
2175 struct rat_info *i = (struct rat_info *) cl;
2177 if (!TEST_FLAG (TheFlag, polygon) && polygon->Clipped &&
2178 (i->Point->X == polygon->Clipped->contours->head.point[0]) &&
2179 (i->Point->Y == polygon->Clipped->contours->head.point[1]))
2181 if (ADD_POLYGON_TO_LIST (i->layer, polygon))
2182 longjmp (i->env, 1);
2184 return 0;
2187 static int
2188 LOCtoPad_callback (const BoxType * b, void *cl)
2190 PadTypePtr pad = (PadTypePtr) b;
2191 struct rat_info *i = (struct rat_info *) cl;
2193 if (!TEST_FLAG (TheFlag, pad) && i->layer ==
2194 (TEST_FLAG (ONSOLDERFLAG, pad) ? SOLDER_LAYER : COMPONENT_LAYER)
2195 && (((pad->Point1.X == i->Point->X && pad->Point1.Y == i->Point->Y)) ||
2196 ((pad->Point2.X == i->Point->X && pad->Point2.Y == i->Point->Y)))
2197 && ADD_PAD_TO_LIST (i->layer, pad))
2198 longjmp (i->env, 1);
2199 return 0;
2202 /* ---------------------------------------------------------------------------
2203 * searches all LOs that are connected to the given rat-line on the given
2204 * layergroup. All found connections are added to the list
2206 * the notation that is used is:
2207 * Xij means Xj at line i
2209 static Boolean
2210 LookupLOConnectionsToRatEnd (PointTypePtr Point, Cardinal LayerGroup)
2212 Cardinal entry;
2213 struct rat_info info;
2215 info.Point = Point;
2216 /* loop over all layers of this group */
2217 for (entry = 0; entry < PCB->LayerGroups.Number[LayerGroup]; entry++)
2219 Cardinal layer;
2221 layer = PCB->LayerGroups.Entries[LayerGroup][entry];
2222 /* handle normal layers
2223 rats don't ever touch
2224 arcs by definition
2227 if (layer < max_layer)
2229 info.layer = layer;
2230 if (setjmp (info.env) == 0)
2231 r_search_pt (LAYER_PTR (layer)->line_tree, Point, 1, NULL,
2232 LOCtoRat_callback, &info);
2233 else
2234 return True;
2235 if (setjmp (info.env) == 0)
2236 r_search_pt (LAYER_PTR (layer)->polygon_tree, Point, 1,
2237 NULL, PolygonToRat_callback, &info);
2239 else
2241 /* handle special 'pad' layers */
2242 info.layer = layer - max_layer;
2243 if (setjmp (info.env) == 0)
2244 r_search_pt (PCB->Data->pad_tree, Point, 1, NULL,
2245 LOCtoPad_callback, &info);
2246 else
2247 return True;
2250 return (False);
2253 static int
2254 LOCtoPadLine_callback (const BoxType * b, void *cl)
2256 LineTypePtr line = (LineTypePtr) b;
2257 struct lo_info *i = (struct lo_info *) cl;
2259 if (!TEST_FLAG (TheFlag, line) && LinePadIntersect (line, &i->pad))
2261 if (ADD_LINE_TO_LIST (i->layer, line))
2262 longjmp (i->env, 1);
2264 return 0;
2267 static int
2268 LOCtoPadArc_callback (const BoxType * b, void *cl)
2270 ArcTypePtr arc = (ArcTypePtr) b;
2271 struct lo_info *i = (struct lo_info *) cl;
2273 if (!arc->Thickness)
2274 return 0;
2275 if (!TEST_FLAG (TheFlag, arc) && ArcPadIntersect (arc, &i->pad))
2277 if (ADD_ARC_TO_LIST (i->layer, arc))
2278 longjmp (i->env, 1);
2280 return 0;
2283 static int
2284 LOCtoPadPoly_callback (const BoxType * b, void *cl)
2286 PolygonTypePtr polygon = (PolygonTypePtr) b;
2287 struct lo_info *i = (struct lo_info *) cl;
2290 if (!TEST_FLAG (TheFlag, polygon) &&
2291 (!TEST_FLAG (CLEARPOLYFLAG, polygon) || !i->pad.Clearance))
2293 if (IsPadInPolygon (&i->pad, polygon) &&
2294 ADD_POLYGON_TO_LIST (i->layer, polygon))
2295 longjmp (i->env, 1);
2297 return 0;
2300 static int
2301 LOCtoPadRat_callback (const BoxType * b, void *cl)
2303 RatTypePtr rat = (RatTypePtr) b;
2304 struct lo_info *i = (struct lo_info *) cl;
2306 if (!TEST_FLAG (TheFlag, rat))
2308 if (rat->group1 == i->layer &&
2309 ((rat->Point1.X == i->pad.Point1.X
2310 && rat->Point1.Y == i->pad.Point1.Y)
2311 || (rat->Point1.X == i->pad.Point2.X
2312 && rat->Point1.Y == i->pad.Point2.Y)))
2314 if (ADD_RAT_TO_LIST (rat))
2315 longjmp (i->env, 1);
2317 else if (rat->group2 == i->layer &&
2318 ((rat->Point2.X == i->pad.Point1.X
2319 && rat->Point2.Y == i->pad.Point1.Y)
2320 || (rat->Point2.X == i->pad.Point2.X
2321 && rat->Point2.Y == i->pad.Point2.Y)))
2323 if (ADD_RAT_TO_LIST (rat))
2324 longjmp (i->env, 1);
2327 return 0;
2330 static int
2331 LOCtoPadPad_callback (const BoxType * b, void *cl)
2333 PadTypePtr pad = (PadTypePtr) b;
2334 struct lo_info *i = (struct lo_info *) cl;
2336 if (!TEST_FLAG (TheFlag, pad) && i->layer ==
2337 (TEST_FLAG (ONSOLDERFLAG, pad) ? SOLDER_LAYER : COMPONENT_LAYER)
2338 && PadPadIntersect (pad, &i->pad) && ADD_PAD_TO_LIST (i->layer, pad))
2339 longjmp (i->env, 1);
2340 return 0;
2343 /* ---------------------------------------------------------------------------
2344 * searches all LOs that are connected to the given pad on the given
2345 * layergroup. All found connections are added to the list
2347 static Boolean
2348 LookupLOConnectionsToPad (PadTypePtr Pad, Cardinal LayerGroup)
2350 Cardinal entry;
2351 struct lo_info info;
2353 if (!TEST_FLAG (SQUAREFLAG, Pad))
2354 return (LookupLOConnectionsToLine ((LineTypePtr) Pad, LayerGroup, False));
2356 info.pad = *Pad;
2357 EXPAND_BOUNDS (&info.pad);
2358 /* add the new rat lines */
2359 info.layer = LayerGroup;
2360 if (setjmp (info.env) == 0)
2361 r_search (PCB->Data->rat_tree, &info.pad.BoundingBox, NULL,
2362 LOCtoPadRat_callback, &info);
2363 else
2364 return True;
2366 /* loop over all layers of the group */
2367 for (entry = 0; entry < PCB->LayerGroups.Number[LayerGroup]; entry++)
2369 Cardinal layer;
2371 layer = PCB->LayerGroups.Entries[LayerGroup][entry];
2372 /* handle normal layers */
2373 if (layer < max_layer)
2375 info.layer = layer;
2376 /* add lines */
2377 if (setjmp (info.env) == 0)
2378 r_search (LAYER_PTR (layer)->line_tree, &info.pad.BoundingBox,
2379 NULL, LOCtoPadLine_callback, &info);
2380 else
2381 return True;
2382 /* add arcs */
2383 if (setjmp (info.env) == 0)
2384 r_search (LAYER_PTR (layer)->arc_tree, &info.pad.BoundingBox,
2385 NULL, LOCtoPadArc_callback, &info);
2386 else
2387 return True;
2388 /* add polygons */
2389 if (setjmp (info.env) == 0)
2390 r_search (LAYER_PTR (layer)->polygon_tree, &info.pad.BoundingBox,
2391 NULL, LOCtoPadPoly_callback, &info);
2392 else
2393 return True;
2395 else
2397 /* handle special 'pad' layers */
2398 info.layer = layer - max_layer;
2399 if (setjmp (info.env) == 0)
2400 r_search (PCB->Data->pad_tree, (BoxType *) & info.pad, NULL,
2401 LOCtoPadPad_callback, &info);
2402 else
2403 return True;
2407 return (False);
2410 static int
2411 LOCtoPolyLine_callback (const BoxType * b, void *cl)
2413 LineTypePtr line = (LineTypePtr) b;
2414 struct lo_info *i = (struct lo_info *) cl;
2416 if (!TEST_FLAG (TheFlag, line) && IsLineInPolygon (line, &i->polygon))
2418 if (ADD_LINE_TO_LIST (i->layer, line))
2419 longjmp (i->env, 1);
2421 return 0;
2424 static int
2425 LOCtoPolyArc_callback (const BoxType * b, void *cl)
2427 ArcTypePtr arc = (ArcTypePtr) b;
2428 struct lo_info *i = (struct lo_info *) cl;
2430 if (!arc->Thickness)
2431 return 0;
2432 if (!TEST_FLAG (TheFlag, arc) && IsArcInPolygon (arc, &i->polygon))
2434 if (ADD_ARC_TO_LIST (i->layer, arc))
2435 longjmp (i->env, 1);
2437 return 0;
2440 static int
2441 LOCtoPolyPad_callback (const BoxType * b, void *cl)
2443 PadTypePtr pad = (PadTypePtr) b;
2444 struct lo_info *i = (struct lo_info *) cl;
2446 if (!TEST_FLAG (TheFlag, pad) && i->layer ==
2447 (TEST_FLAG (ONSOLDERFLAG, pad) ? SOLDER_LAYER : COMPONENT_LAYER)
2448 && IsPadInPolygon (pad, &i->polygon))
2450 if (ADD_PAD_TO_LIST (i->layer, pad))
2451 longjmp (i->env, 1);
2453 return 0;
2456 static int
2457 LOCtoPolyRat_callback (const BoxType * b, void *cl)
2459 RatTypePtr rat = (RatTypePtr) b;
2460 struct lo_info *i = (struct lo_info *) cl;
2462 if (!TEST_FLAG (TheFlag, rat))
2464 if ((rat->Point1.X == (i->polygon.Clipped->contours->head.point[0]) &&
2465 rat->Point1.Y == (i->polygon.Clipped->contours->head.point[1]) &&
2466 rat->group1 == i->layer) ||
2467 (rat->Point2.X == (i->polygon.Clipped->contours->head.point[0]) &&
2468 rat->Point2.Y == (i->polygon.Clipped->contours->head.point[1]) &&
2469 rat->group2 == i->layer))
2470 if (ADD_RAT_TO_LIST (rat))
2471 longjmp (i->env, 1);
2473 return 0;
2477 /* ---------------------------------------------------------------------------
2478 * looks up LOs that are connected to the given polygon
2479 * on the given layergroup. All found connections are added to the list
2481 static Boolean
2482 LookupLOConnectionsToPolygon (PolygonTypePtr Polygon, Cardinal LayerGroup)
2484 Cardinal entry;
2485 struct lo_info info;
2487 if (!Polygon->Clipped)
2488 return False;
2489 info.polygon = *Polygon;
2490 EXPAND_BOUNDS (&info.polygon);
2491 info.layer = LayerGroup;
2492 /* check rats */
2493 if (setjmp (info.env) == 0)
2494 r_search (PCB->Data->rat_tree, (BoxType *) & info.polygon, NULL,
2495 LOCtoPolyRat_callback, &info);
2496 else
2497 return True;
2498 /* loop over all layers of the group */
2499 for (entry = 0; entry < PCB->LayerGroups.Number[LayerGroup]; entry++)
2501 Cardinal layer, i;
2503 layer = PCB->LayerGroups.Entries[LayerGroup][entry];
2505 /* handle normal layers */
2506 if (layer < max_layer)
2508 PolygonTypePtr polygon;
2510 /* check all polygons */
2512 polygon = PCB->Data->Layer[layer].Polygon;
2513 for (i = 0; i < PCB->Data->Layer[layer].PolygonN; i++, polygon++)
2514 if (!TEST_FLAG (TheFlag, polygon)
2515 && IsPolygonInPolygon (polygon, Polygon)
2516 && ADD_POLYGON_TO_LIST (layer, polygon))
2517 return True;
2519 info.layer = layer;
2520 /* check all lines */
2521 if (setjmp (info.env) == 0)
2522 r_search (LAYER_PTR (layer)->line_tree,
2523 (BoxType *) & info.polygon, NULL,
2524 LOCtoPolyLine_callback, &info);
2525 else
2526 return True;
2527 /* check all arcs */
2528 if (setjmp (info.env) == 0)
2529 r_search (LAYER_PTR (layer)->arc_tree, (BoxType *) & info.polygon,
2530 NULL, LOCtoPolyArc_callback, &info);
2531 else
2532 return True;
2534 else
2536 info.layer = layer - max_layer;
2537 if (setjmp (info.env) == 0)
2538 r_search (PCB->Data->pad_tree, (BoxType *) & info.polygon,
2539 NULL, LOCtoPolyPad_callback, &info);
2540 else
2541 return True;
2544 return (False);
2547 /* ---------------------------------------------------------------------------
2548 * checks if an arc has a connection to a polygon
2550 * - first check if the arc can intersect with the polygon by
2551 * evaluating the bounding boxes
2552 * - check the two end points of the arc. If none of them matches
2553 * - check all segments of the polygon against the arc.
2555 Boolean
2556 IsArcInPolygon (ArcTypePtr Arc, PolygonTypePtr Polygon)
2558 BoxTypePtr Box = (BoxType *) Arc;
2560 /* arcs with clearance never touch polys */
2561 if (TEST_FLAG (CLEARPOLYFLAG, Polygon) && TEST_FLAG (CLEARLINEFLAG, Arc))
2562 return False;
2563 if (!Polygon->Clipped)
2564 return False;
2565 if (Box->X1 <= Polygon->Clipped->contours->xmax + Bloat
2566 && Box->X2 >= Polygon->Clipped->contours->xmin - Bloat
2567 && Box->Y1 <= Polygon->Clipped->contours->ymax + Bloat
2568 && Box->Y2 >= Polygon->Clipped->contours->ymin - Bloat)
2570 POLYAREA *ap;
2572 if (!(ap = ArcPoly (Arc, Arc->Thickness + Bloat)))
2573 return False; /* error */
2574 return isects (ap, Polygon, True);
2576 return False;
2579 /* ---------------------------------------------------------------------------
2580 * checks if a line has a connection to a polygon
2582 * - first check if the line can intersect with the polygon by
2583 * evaluating the bounding boxes
2584 * - check the two end points of the line. If none of them matches
2585 * - check all segments of the polygon against the line.
2587 Boolean
2588 IsLineInPolygon (LineTypePtr Line, PolygonTypePtr Polygon)
2590 BoxTypePtr Box = (BoxType *) Line;
2591 POLYAREA *lp;
2593 /* lines with clearance never touch polygons */
2594 if (TEST_FLAG (CLEARPOLYFLAG, Polygon) && TEST_FLAG (CLEARLINEFLAG, Line))
2595 return False;
2596 if (!Polygon->Clipped)
2597 return False;
2598 if (TEST_FLAG(SQUAREFLAG,Line)&&(Line->Point1.X==Line->Point2.X||Line->Point1.Y==Line->Point2.Y))
2600 BDimension wid = (Line->Thickness + Bloat + 1) / 2;
2601 LocationType x1, x2, y1, y2;
2603 x1 = MIN (Line->Point1.X, Line->Point2.X) - wid;
2604 y1 = MIN (Line->Point1.Y, Line->Point2.Y) - wid;
2605 x2 = MAX (Line->Point1.X, Line->Point2.X) + wid;
2606 y2 = MAX (Line->Point1.Y, Line->Point2.Y) + wid;
2607 return IsRectangleInPolygon (x1, y1, x2, y2, Polygon);
2609 if (Box->X1 <= Polygon->Clipped->contours->xmax + Bloat
2610 && Box->X2 >= Polygon->Clipped->contours->xmin - Bloat
2611 && Box->Y1 <= Polygon->Clipped->contours->ymax + Bloat
2612 && Box->Y2 >= Polygon->Clipped->contours->ymin - Bloat)
2614 if (!(lp = LinePoly (Line, Line->Thickness + Bloat)))
2615 return FALSE; /* error */
2616 return isects (lp, Polygon, True);
2618 return False;
2621 /* ---------------------------------------------------------------------------
2622 * checks if a pad connects to a non-clearing polygon
2624 * The polygon is assumed to already have been proven non-clearing
2626 Boolean
2627 IsPadInPolygon (PadTypePtr pad, PolygonTypePtr polygon)
2629 return IsLineInPolygon ((LineTypePtr) pad, polygon);
2632 /* ---------------------------------------------------------------------------
2633 * checks if a polygon has a connection to a second one
2635 * First check all points out of P1 against P2 and vice versa.
2636 * If both fail check all lines of P1 against the ones of P2
2638 Boolean
2639 IsPolygonInPolygon (PolygonTypePtr P1, PolygonTypePtr P2)
2641 if (!P1->Clipped || !P2->Clipped)
2642 return False;
2643 assert (P1->Clipped->contours);
2644 assert (P2->Clipped->contours);
2646 /* first check if both bounding boxes intersect. If not, return quickly */
2647 if (P1->Clipped->contours->xmin - Bloat > P2->Clipped->contours->xmax ||
2648 P1->Clipped->contours->xmax + Bloat < P2->Clipped->contours->xmin ||
2649 P1->Clipped->contours->ymin - Bloat > P2->Clipped->contours->ymax ||
2650 P1->Clipped->contours->ymax + Bloat < P2->Clipped->contours->ymin)
2651 return False;
2653 /* first check un-bloated case */
2654 if (isects (P1->Clipped, P2, False))
2655 return TRUE;
2657 /* now the difficult case of bloated */
2658 if (Bloat > 0)
2660 PLINE *c;
2661 for (c = P1->Clipped->contours; c; c = c->next)
2663 LineType line;
2664 VNODE *v = &c->head;
2665 if (c->xmin - Bloat <= P2->Clipped->contours->xmax &&
2666 c->xmax + Bloat >= P2->Clipped->contours->xmin &&
2667 c->ymin - Bloat <= P2->Clipped->contours->ymax &&
2668 c->ymax + Bloat >= P2->Clipped->contours->ymin)
2671 line.Point1.X = v->point[0];
2672 line.Point1.Y = v->point[1];
2673 line.Thickness = 2 * Bloat;
2674 line.Clearance = 0;
2675 line.Flags = NoFlags ();
2676 for (v = v->next; v != &c->head; v = v->next)
2678 line.Point2.X = v->point[0];
2679 line.Point2.Y = v->point[1];
2680 SetLineBoundingBox (&line);
2681 if (IsLineInPolygon (&line, P2))
2682 return (True);
2683 line.Point1.X = line.Point2.X;
2684 line.Point1.Y = line.Point2.Y;
2690 return (False);
2693 /* ---------------------------------------------------------------------------
2694 * writes the several names of an element to a file
2696 static void
2697 PrintElementNameList (ElementTypePtr Element, FILE * FP)
2699 static DynamicStringType cname, pname, vname;
2701 CreateQuotedString (&cname, EMPTY (DESCRIPTION_NAME (Element)));
2702 CreateQuotedString (&pname, EMPTY (NAMEONPCB_NAME (Element)));
2703 CreateQuotedString (&vname, EMPTY (VALUE_NAME (Element)));
2704 fprintf (FP, "(%s %s %s)\n", cname.Data, pname.Data, vname.Data);
2707 /* ---------------------------------------------------------------------------
2708 * writes the several names of an element to a file
2710 static void
2711 PrintConnectionElementName (ElementTypePtr Element, FILE * FP)
2713 fputs ("Element", FP);
2714 PrintElementNameList (Element, FP);
2715 fputs ("{\n", FP);
2718 /* ---------------------------------------------------------------------------
2719 * prints one {pin,pad,via}/element entry of connection lists
2721 static void
2722 PrintConnectionListEntry (char *ObjName, ElementTypePtr Element,
2723 Boolean FirstOne, FILE * FP)
2725 static DynamicStringType oname;
2727 CreateQuotedString (&oname, ObjName);
2728 if (FirstOne)
2729 fprintf (FP, "\t%s\n\t{\n", oname.Data);
2730 else
2732 fprintf (FP, "\t\t%s ", oname.Data);
2733 if (Element)
2734 PrintElementNameList (Element, FP);
2735 else
2736 fputs ("(__VIA__)\n", FP);
2740 /* ---------------------------------------------------------------------------
2741 * prints all found connections of a pads to file FP
2742 * the connections are stacked in 'PadList'
2744 static void
2745 PrintPadConnections (Cardinal Layer, FILE * FP, Boolean IsFirst)
2747 Cardinal i;
2748 PadTypePtr ptr;
2750 if (!PadList[Layer].Number)
2751 return;
2753 /* the starting pad */
2754 if (IsFirst)
2756 ptr = PADLIST_ENTRY (Layer, 0);
2757 if (ptr != NULL)
2758 PrintConnectionListEntry (UNKNOWN (ptr->Name), NULL, True, FP);
2759 else
2760 printf ("Skipping NULL ptr in 1st part of PrintPadConnections\n");
2763 /* we maybe have to start with i=1 if we are handling the
2764 * starting-pad itself
2766 for (i = IsFirst ? 1 : 0; i < PadList[Layer].Number; i++)
2768 ptr = PADLIST_ENTRY (Layer, i);
2769 if (ptr != NULL)
2770 PrintConnectionListEntry (EMPTY (ptr->Name), ptr->Element, False, FP);
2771 else
2772 printf ("Skipping NULL ptr in 2nd part of PrintPadConnections\n");
2776 /* ---------------------------------------------------------------------------
2777 * prints all found connections of a pin to file FP
2778 * the connections are stacked in 'PVList'
2780 static void
2781 PrintPinConnections (FILE * FP, Boolean IsFirst)
2783 Cardinal i;
2784 PinTypePtr pv;
2786 if (!PVList.Number)
2787 return;
2789 if (IsFirst)
2791 /* the starting pin */
2792 pv = PVLIST_ENTRY (0);
2793 PrintConnectionListEntry (EMPTY (pv->Name), NULL, True, FP);
2796 /* we maybe have to start with i=1 if we are handling the
2797 * starting-pin itself
2799 for (i = IsFirst ? 1 : 0; i < PVList.Number; i++)
2801 /* get the elements name or assume that its a via */
2802 pv = PVLIST_ENTRY (i);
2803 PrintConnectionListEntry (EMPTY (pv->Name), pv->Element, False, FP);
2807 /* ---------------------------------------------------------------------------
2808 * checks if all lists of new objects are handled
2810 static Boolean
2811 ListsEmpty (Boolean AndRats)
2813 Boolean empty;
2814 int i;
2816 empty = (PVList.Location >= PVList.Number);
2817 if (AndRats)
2818 empty = empty && (RatList.Location >= RatList.Number);
2819 for (i = 0; i < max_layer && empty; i++)
2820 empty = empty && LineList[i].Location >= LineList[i].Number
2821 && ArcList[i].Location >= ArcList[i].Number
2822 && PolygonList[i].Location >= PolygonList[i].Number;
2823 return (empty);
2826 /* ---------------------------------------------------------------------------
2827 * loops till no more connections are found
2829 static Boolean
2830 DoIt (Boolean AndRats, Boolean AndDraw)
2832 Boolean new = False;
2835 /* lookup connections; these are the steps (2) to (4)
2836 * from the description
2838 new = LookupPVConnectionsToPVList ();
2839 if (!new)
2840 new = LookupLOConnectionsToPVList (AndRats);
2841 if (!new)
2842 new = LookupLOConnectionsToLOList (AndRats);
2843 if (!new)
2844 new = LookupPVConnectionsToLOList (AndRats);
2845 if (AndDraw)
2846 DrawNewConnections ();
2848 while (!new && !ListsEmpty (AndRats));
2849 if (AndDraw)
2850 Draw ();
2851 return (new);
2854 /* returns True if nothing un-found touches the passed line
2855 * returns False if it would touch something not yet found
2856 * doesn't include rat-lines in the search
2859 Boolean
2860 lineClear (LineTypePtr line, Cardinal group)
2862 if (LOTouchesLine (line, group))
2863 return (False);
2864 if (PVTouchesLine (line))
2865 return (False);
2866 return (True);
2869 /* ---------------------------------------------------------------------------
2870 * prints all unused pins of an element to file FP
2872 static Boolean
2873 PrintAndSelectUnusedPinsAndPadsOfElement (ElementTypePtr Element, FILE * FP)
2875 Boolean first = True;
2876 Cardinal number;
2877 static DynamicStringType oname;
2879 /* check all pins in element */
2881 PIN_LOOP (Element);
2883 if (!TEST_FLAG (HOLEFLAG, pin))
2885 /* pin might have bee checked before, add to list if not */
2886 if (!TEST_FLAG (TheFlag, pin) && FP)
2888 int i;
2889 if (ADD_PV_TO_LIST (pin))
2890 return True;
2891 DoIt (True, True);
2892 number = PadList[COMPONENT_LAYER].Number
2893 + PadList[SOLDER_LAYER].Number + PVList.Number;
2894 /* the pin has no connection if it's the only
2895 * list entry; don't count vias
2897 for (i = 0; i < PVList.Number; i++)
2898 if (!PVLIST_ENTRY (i)->Element)
2899 number--;
2900 if (number == 1)
2902 /* output of element name if not already done */
2903 if (first)
2905 PrintConnectionElementName (Element, FP);
2906 first = False;
2909 /* write name to list and draw selected object */
2910 CreateQuotedString (&oname, EMPTY (pin->Name));
2911 fprintf (FP, "\t%s\n", oname.Data);
2912 SET_FLAG (SELECTEDFLAG, pin);
2913 DrawPin (pin, 0);
2916 /* reset found objects for the next pin */
2917 if (PrepareNextLoop (FP))
2918 return (True);
2922 END_LOOP;
2924 /* check all pads in element */
2925 PAD_LOOP (Element);
2927 /* lookup pad in list */
2928 /* pad might has bee checked before, add to list if not */
2929 if (!TEST_FLAG (TheFlag, pad) && FP)
2931 int i;
2932 if (ADD_PAD_TO_LIST (TEST_FLAG (ONSOLDERFLAG, pad)
2933 ? SOLDER_LAYER : COMPONENT_LAYER, pad))
2934 return True;
2935 DoIt (True, True);
2936 number = PadList[COMPONENT_LAYER].Number
2937 + PadList[SOLDER_LAYER].Number + PVList.Number;
2938 /* the pin has no connection if it's the only
2939 * list entry; don't count vias
2941 for (i = 0; i < PVList.Number; i++)
2942 if (!PVLIST_ENTRY (i)->Element)
2943 number--;
2944 if (number == 1)
2946 /* output of element name if not already done */
2947 if (first)
2949 PrintConnectionElementName (Element, FP);
2950 first = False;
2953 /* write name to list and draw selected object */
2954 CreateQuotedString (&oname, EMPTY (pad->Name));
2955 fprintf (FP, "\t%s\n", oname.Data);
2956 SET_FLAG (SELECTEDFLAG, pad);
2957 DrawPad (pad, 0);
2960 /* reset found objects for the next pin */
2961 if (PrepareNextLoop (FP))
2962 return (True);
2965 END_LOOP;
2967 /* print separator if element has unused pins or pads */
2968 if (!first)
2970 fputs ("}\n\n", FP);
2971 SEPARATE (FP);
2973 return (False);
2976 /* ---------------------------------------------------------------------------
2977 * resets some flags for looking up the next pin/pad
2979 static Boolean
2980 PrepareNextLoop (FILE * FP)
2982 Cardinal layer;
2984 /* reset found LOs for the next pin */
2985 for (layer = 0; layer < max_layer; layer++)
2987 LineList[layer].Location = LineList[layer].Number = 0;
2988 ArcList[layer].Location = ArcList[layer].Number = 0;
2989 PolygonList[layer].Location = PolygonList[layer].Number = 0;
2992 /* reset found pads */
2993 for (layer = 0; layer < 2; layer++)
2994 PadList[layer].Location = PadList[layer].Number = 0;
2996 /* reset PVs */
2997 PVList.Number = PVList.Location = 0;
2998 RatList.Number = RatList.Location = 0;
3000 return (False);
3003 /* ---------------------------------------------------------------------------
3004 * finds all connections to the pins of the passed element.
3005 * The result is written to file FP
3006 * Returns True if operation was aborted
3008 static Boolean
3009 PrintElementConnections (ElementTypePtr Element, FILE * FP, Boolean AndDraw)
3011 PrintConnectionElementName (Element, FP);
3013 /* check all pins in element */
3014 PIN_LOOP (Element);
3016 /* pin might have been checked before, add to list if not */
3017 if (TEST_FLAG (TheFlag, pin))
3019 PrintConnectionListEntry (EMPTY (pin->Name), NULL, True, FP);
3020 fputs ("\t\t__CHECKED_BEFORE__\n\t}\n", FP);
3021 continue;
3023 if (ADD_PV_TO_LIST (pin))
3024 return True;
3025 DoIt (True, AndDraw);
3026 /* printout all found connections */
3027 PrintPinConnections (FP, True);
3028 PrintPadConnections (COMPONENT_LAYER, FP, False);
3029 PrintPadConnections (SOLDER_LAYER, FP, False);
3030 fputs ("\t}\n", FP);
3031 if (PrepareNextLoop (FP))
3032 return (True);
3034 END_LOOP;
3036 /* check all pads in element */
3037 PAD_LOOP (Element);
3039 Cardinal layer;
3040 /* pad might have been checked before, add to list if not */
3041 if (TEST_FLAG (TheFlag, pad))
3043 PrintConnectionListEntry (EMPTY (pad->Name), NULL, True, FP);
3044 fputs ("\t\t__CHECKED_BEFORE__\n\t}\n", FP);
3045 continue;
3047 layer = TEST_FLAG (ONSOLDERFLAG, pad) ? SOLDER_LAYER : COMPONENT_LAYER;
3048 if (ADD_PAD_TO_LIST (layer, pad))
3049 return True;
3050 DoIt (True, AndDraw);
3051 /* print all found connections */
3052 PrintPadConnections (layer, FP, True);
3053 PrintPadConnections (layer ==
3054 (COMPONENT_LAYER ? SOLDER_LAYER : COMPONENT_LAYER),
3055 FP, False);
3056 PrintPinConnections (FP, False);
3057 fputs ("\t}\n", FP);
3058 if (PrepareNextLoop (FP))
3059 return (True);
3061 END_LOOP;
3062 fputs ("}\n\n", FP);
3063 return (False);
3066 /* ---------------------------------------------------------------------------
3067 * draws all new connections which have been found since the
3068 * routine was called the last time
3070 static void
3071 DrawNewConnections (void)
3073 int i;
3074 Cardinal position;
3076 /* decrement 'i' to keep layerstack order */
3077 for (i = max_layer - 1; i != -1; i--)
3079 Cardinal layer = LayerStack[i];
3081 if (PCB->Data->Layer[layer].On)
3083 /* draw all new lines */
3084 position = LineList[layer].DrawLocation;
3085 for (; position < LineList[layer].Number; position++)
3086 DrawLine (LAYER_PTR (layer), LINELIST_ENTRY (layer, position), 0);
3087 LineList[layer].DrawLocation = LineList[layer].Number;
3089 /* draw all new arcs */
3090 position = ArcList[layer].DrawLocation;
3091 for (; position < ArcList[layer].Number; position++)
3092 DrawArc (LAYER_PTR (layer), ARCLIST_ENTRY (layer, position), 0);
3093 ArcList[layer].DrawLocation = ArcList[layer].Number;
3095 /* draw all new polygons */
3096 position = PolygonList[layer].DrawLocation;
3097 for (; position < PolygonList[layer].Number; position++)
3098 DrawPolygon
3099 (LAYER_PTR (layer), POLYGONLIST_ENTRY (layer, position), 0);
3100 PolygonList[layer].DrawLocation = PolygonList[layer].Number;
3104 /* draw all new pads */
3105 if (PCB->PinOn)
3106 for (i = 0; i < 2; i++)
3108 position = PadList[i].DrawLocation;
3110 for (; position < PadList[i].Number; position++)
3111 DrawPad (PADLIST_ENTRY (i, position), 0);
3112 PadList[i].DrawLocation = PadList[i].Number;
3115 /* draw all new PVs; 'PVList' holds a list of pointers to the
3116 * sorted array pointers to PV data
3118 while (PVList.DrawLocation < PVList.Number)
3120 PinTypePtr pv = PVLIST_ENTRY (PVList.DrawLocation);
3122 if (TEST_FLAG (PINFLAG, pv))
3124 if (PCB->PinOn)
3125 DrawPin (pv, 0);
3127 else if (PCB->ViaOn)
3128 DrawVia (pv, 0);
3129 PVList.DrawLocation++;
3131 /* draw the new rat-lines */
3132 if (PCB->RatOn)
3134 position = RatList.DrawLocation;
3135 for (; position < RatList.Number; position++)
3136 DrawRat (RATLIST_ENTRY (position), 0);
3137 RatList.DrawLocation = RatList.Number;
3141 /* ---------------------------------------------------------------------------
3142 * find all connections to pins within one element
3144 void
3145 LookupElementConnections (ElementTypePtr Element, FILE * FP)
3147 /* reset all currently marked connections */
3148 User = True;
3149 TheFlag = FOUNDFLAG;
3150 ResetConnections (True);
3151 InitConnectionLookup ();
3152 PrintElementConnections (Element, FP, True);
3153 SetChangedFlag (True);
3154 if (Settings.RingBellWhenFinished)
3155 gui->beep ();
3156 FreeConnectionLookupMemory ();
3157 IncrementUndoSerialNumber ();
3158 User = False;
3159 Draw ();
3162 /* ---------------------------------------------------------------------------
3163 * find all connections to pins of all element
3165 void
3166 LookupConnectionsToAllElements (FILE * FP)
3168 /* reset all currently marked connections */
3169 User = False;
3170 TheFlag = FOUNDFLAG;
3171 ResetConnections (False);
3172 InitConnectionLookup ();
3174 ELEMENT_LOOP (PCB->Data);
3176 /* break if abort dialog returned True */
3177 if (PrintElementConnections (element, FP, False))
3178 break;
3179 SEPARATE (FP);
3180 if (Settings.ResetAfterElement && n != 1)
3181 ResetConnections (False);
3183 END_LOOP;
3184 if (Settings.RingBellWhenFinished)
3185 gui->beep ();
3186 ResetConnections (False);
3187 FreeConnectionLookupMemory ();
3188 ClearAndRedrawOutput ();
3191 /*---------------------------------------------------------------------------
3192 * add the starting object to the list of found objects
3194 static Boolean
3195 ListStart (int type, void *ptr1, void *ptr2, void *ptr3)
3197 DumpList ();
3198 switch (type)
3200 case PIN_TYPE:
3201 case VIA_TYPE:
3203 if (ADD_PV_TO_LIST ((PinTypePtr) ptr2))
3204 return True;
3205 break;
3208 case RATLINE_TYPE:
3210 if (ADD_RAT_TO_LIST ((RatTypePtr) ptr1))
3211 return True;
3212 break;
3215 case LINE_TYPE:
3217 int layer = GetLayerNumber (PCB->Data,
3218 (LayerTypePtr) ptr1);
3220 if (ADD_LINE_TO_LIST (layer, (LineTypePtr) ptr2))
3221 return True;
3222 break;
3225 case ARC_TYPE:
3227 int layer = GetLayerNumber (PCB->Data,
3228 (LayerTypePtr) ptr1);
3230 if (ADD_ARC_TO_LIST (layer, (ArcTypePtr) ptr2))
3231 return True;
3232 break;
3235 case POLYGON_TYPE:
3237 int layer = GetLayerNumber (PCB->Data,
3238 (LayerTypePtr) ptr1);
3240 if (ADD_POLYGON_TO_LIST (layer, (PolygonTypePtr) ptr2))
3241 return True;
3242 break;
3245 case PAD_TYPE:
3247 PadTypePtr pad = (PadTypePtr) ptr2;
3248 if (ADD_PAD_TO_LIST
3249 (TEST_FLAG
3250 (ONSOLDERFLAG, pad) ? SOLDER_LAYER : COMPONENT_LAYER, pad))
3251 return True;
3252 break;
3255 return (False);
3259 /* ---------------------------------------------------------------------------
3260 * looks up all connections from the object at the given coordinates
3261 * the TheFlag (normally 'FOUNDFLAG') is set for all objects found
3262 * the objects are re-drawn if AndDraw is true
3263 * also the action is marked as undoable if AndDraw is true
3265 void
3266 LookupConnection (LocationType X, LocationType Y, Boolean AndDraw,
3267 BDimension Range, int which_flag)
3269 void *ptr1, *ptr2, *ptr3;
3270 char *name;
3271 int type;
3273 /* check if there are any pins or pads at that position */
3276 type
3277 = SearchObjectByLocation (LOOKUP_FIRST, &ptr1, &ptr2, &ptr3, X, Y, Range);
3278 if (type == NO_TYPE)
3280 type
3282 SearchObjectByLocation
3283 (LOOKUP_MORE, &ptr1, &ptr2, &ptr3, X, Y, Range);
3284 if (type == NO_TYPE)
3285 return;
3286 if (type & SILK_TYPE)
3288 int laynum = GetLayerNumber (PCB->Data,
3289 (LayerTypePtr) ptr1);
3291 /* don't mess with silk objects! */
3292 if (laynum >= max_layer)
3293 return;
3296 else
3298 name = ConnectionName (type, ptr1, ptr2);
3299 hid_actionl ("NetlistShow", name, NULL);
3302 TheFlag = which_flag;
3303 User = AndDraw;
3304 InitConnectionLookup ();
3306 /* now add the object to the appropriate list and start scanning
3307 * This is step (1) from the description
3309 ListStart (type, ptr1, ptr2, ptr3);
3310 DoIt (True, AndDraw);
3311 if (User)
3312 IncrementUndoSerialNumber ();
3313 User = False;
3315 /* we are done */
3316 if (AndDraw)
3317 Draw ();
3318 if (AndDraw && Settings.RingBellWhenFinished)
3319 gui->beep ();
3320 FreeConnectionLookupMemory ();
3323 /* ---------------------------------------------------------------------------
3324 * find connections for rats nesting
3325 * assumes InitConnectionLookup() has already been done
3327 void
3328 RatFindHook
3329 (int type, void *ptr1, void *ptr2, void *ptr3, Boolean undo,
3330 Boolean AndRats)
3332 User = undo;
3333 DumpList ();
3334 ListStart (type, ptr1, ptr2, ptr3);
3335 DoIt (AndRats, False);
3336 User = False;
3339 /* ---------------------------------------------------------------------------
3340 * find all unused pins of all element
3342 void
3343 LookupUnusedPins (FILE * FP)
3345 /* reset all currently marked connections */
3346 User = True;
3347 SaveUndoSerialNumber ();
3348 ResetConnections (True);
3349 RestoreUndoSerialNumber ();
3350 InitConnectionLookup ();
3352 ELEMENT_LOOP (PCB->Data);
3354 /* break if abort dialog returned True;
3355 * passing NULL as filedescriptor discards the normal output
3357 if (PrintAndSelectUnusedPinsAndPadsOfElement (element, FP))
3358 break;
3360 END_LOOP;
3362 if (Settings.RingBellWhenFinished)
3363 gui->beep ();
3364 FreeConnectionLookupMemory ();
3365 IncrementUndoSerialNumber ();
3366 User = False;
3367 Draw ();
3370 /* ---------------------------------------------------------------------------
3371 * resets all used flags of pins and vias
3373 void
3374 ResetFoundPinsViasAndPads (Boolean AndDraw)
3376 Boolean change = False;
3379 VIA_LOOP (PCB->Data);
3381 if (TEST_FLAG (TheFlag, via))
3383 if (AndDraw)
3384 AddObjectToFlagUndoList (VIA_TYPE, via, via, via);
3385 CLEAR_FLAG (TheFlag, via);
3386 if (AndDraw)
3387 DrawVia (via, 0);
3388 change = True;
3391 END_LOOP;
3392 ELEMENT_LOOP (PCB->Data);
3394 PIN_LOOP (element);
3396 if (TEST_FLAG (TheFlag, pin))
3398 if (AndDraw)
3399 AddObjectToFlagUndoList (PIN_TYPE, element, pin, pin);
3400 CLEAR_FLAG (TheFlag, pin);
3401 if (AndDraw)
3402 DrawPin (pin, 0);
3403 change = True;
3406 END_LOOP;
3407 PAD_LOOP (element);
3409 if (TEST_FLAG (TheFlag, pad))
3411 if (AndDraw)
3412 AddObjectToFlagUndoList (PAD_TYPE, element, pad, pad);
3413 CLEAR_FLAG (TheFlag, pad);
3414 if (AndDraw)
3415 DrawPad (pad, 0);
3416 change = True;
3419 END_LOOP;
3421 END_LOOP;
3422 if (change)
3424 SetChangedFlag (True);
3425 if (AndDraw)
3427 IncrementUndoSerialNumber ();
3428 Draw ();
3433 /* ---------------------------------------------------------------------------
3434 * resets all used flags of LOs
3436 void
3437 ResetFoundLinesAndPolygons (Boolean AndDraw)
3439 Boolean change = False;
3442 RAT_LOOP (PCB->Data);
3444 if (TEST_FLAG (TheFlag, line))
3446 if (AndDraw)
3447 AddObjectToFlagUndoList (RATLINE_TYPE, line, line, line);
3448 CLEAR_FLAG (TheFlag, line);
3449 if (AndDraw)
3450 DrawRat (line, 0);
3451 change = True;
3454 END_LOOP;
3455 COPPERLINE_LOOP (PCB->Data);
3457 if (TEST_FLAG (TheFlag, line))
3459 if (AndDraw)
3460 AddObjectToFlagUndoList (LINE_TYPE, layer, line, line);
3461 CLEAR_FLAG (TheFlag, line);
3462 if (AndDraw)
3463 DrawLine (layer, line, 0);
3464 change = True;
3467 ENDALL_LOOP;
3468 COPPERARC_LOOP (PCB->Data);
3470 if (TEST_FLAG (TheFlag, arc))
3472 if (AndDraw)
3473 AddObjectToFlagUndoList (ARC_TYPE, layer, arc, arc);
3474 CLEAR_FLAG (TheFlag, arc);
3475 if (AndDraw)
3476 DrawArc (layer, arc, 0);
3477 change = True;
3480 ENDALL_LOOP;
3481 COPPERPOLYGON_LOOP (PCB->Data);
3483 if (TEST_FLAG (TheFlag, polygon))
3485 if (AndDraw)
3486 AddObjectToFlagUndoList (POLYGON_TYPE, layer, polygon, polygon);
3487 CLEAR_FLAG (TheFlag, polygon);
3488 if (AndDraw)
3489 DrawPolygon (layer, polygon, 0);
3490 change = True;
3493 ENDALL_LOOP;
3494 if (change)
3496 SetChangedFlag (True);
3497 if (AndDraw)
3499 IncrementUndoSerialNumber ();
3500 Draw ();
3505 /* ---------------------------------------------------------------------------
3506 * resets all found connections
3508 static void
3509 ResetConnections (Boolean AndDraw)
3511 if (AndDraw)
3512 SaveUndoSerialNumber ();
3513 ResetFoundPinsViasAndPads (AndDraw);
3514 if (AndDraw)
3515 RestoreUndoSerialNumber ();
3516 ResetFoundLinesAndPolygons (AndDraw);
3519 /*----------------------------------------------------------------------------
3520 * Dumps the list contents
3522 static void
3523 DumpList (void)
3525 Cardinal i;
3527 for (i = 0; i < 2; i++)
3529 PadList[i].Number = 0;
3530 PadList[i].Location = 0;
3531 PadList[i].DrawLocation = 0;
3534 PVList.Number = 0;
3535 PVList.Location = 0;
3537 for (i = 0; i < max_layer; i++)
3539 LineList[i].Location = 0;
3540 LineList[i].DrawLocation = 0;
3541 LineList[i].Number = 0;
3542 ArcList[i].Location = 0;
3543 ArcList[i].DrawLocation = 0;
3544 ArcList[i].Number = 0;
3545 PolygonList[i].Location = 0;
3546 PolygonList[i].DrawLocation = 0;
3547 PolygonList[i].Number = 0;
3549 RatList.Number = 0;
3550 RatList.Location = 0;
3551 RatList.DrawLocation = 0;
3554 /*-----------------------------------------------------------------------------
3555 * Check for DRC violations on a single net starting from the pad or pin
3556 * sees if the connectivity changes when everything is bloated, or shrunk
3558 static Boolean
3559 DRCFind (int What, void *ptr1, void *ptr2, void *ptr3)
3561 LocationType x, y;
3562 int object_count;
3563 long int *object_id_list;
3564 int *object_type_list;
3565 DrcViolationType *violation;
3567 if (PCB->Shrink != 0)
3569 Bloat = -PCB->Shrink;
3570 fBloat = (float) -PCB->Shrink;
3571 TheFlag = DRCFLAG | SELECTEDFLAG;
3572 ListStart (What, ptr1, ptr2, ptr3);
3573 DoIt (True, False);
3574 /* ok now the shrunk net has the SELECTEDFLAG set */
3575 DumpList ();
3576 TheFlag = FOUNDFLAG;
3577 ListStart (What, ptr1, ptr2, ptr3);
3578 Bloat = 0;
3579 fBloat = 0.0;
3580 drc = True; /* abort the search if we find anything not already found */
3581 if (DoIt (True, False))
3583 DumpList ();
3584 /* make the flag changes undoable */
3585 TheFlag = FOUNDFLAG | SELECTEDFLAG;
3586 ResetConnections (False);
3587 User = True;
3588 drc = False;
3589 Bloat = -PCB->Shrink;
3590 fBloat = (float) -PCB->Shrink;
3591 TheFlag = SELECTEDFLAG;
3592 RestoreUndoSerialNumber ();
3593 ListStart (What, ptr1, ptr2, ptr3);
3594 DoIt (True, True);
3595 DumpList ();
3596 ListStart (What, ptr1, ptr2, ptr3);
3597 TheFlag = FOUNDFLAG;
3598 Bloat = 0;
3599 fBloat = 0.0;
3600 drc = True;
3601 DoIt (True, True);
3602 DumpList ();
3603 User = False;
3604 drc = False;
3605 drcerr_count++;
3606 LocateError (&x, &y);
3607 BuildObjectList (&object_count, &object_id_list, &object_type_list);
3608 violation = pcb_drc_violation_new (_("Potential for broken trace"),
3609 _("Insufficient overlap between objects can lead to broken tracks\n"
3610 "due to registration errors with old wheel style photo-plotters."),
3611 x, y,
3612 0, /* ANGLE OF ERROR UNKNOWN */
3613 FALSE, /* MEASUREMENT OF ERROR UNKNOWN */
3614 0, /* MAGNITUDE OF ERROR UNKNOWN */
3615 LENGTH_TO_HUMAN(PCB->Shrink),
3616 LENGTH_DIGITS,
3617 LENGTH_UNITS_STRING,
3618 object_count,
3619 object_id_list,
3620 object_type_list);
3621 append_drc_violation (violation);
3622 pcb_drc_violation_free (violation);
3623 free (object_id_list);
3624 free (object_type_list);
3626 if (!throw_drc_dialog())
3627 return (True);
3628 IncrementUndoSerialNumber ();
3629 Undo (True);
3631 DumpList ();
3633 /* now check the bloated condition */
3634 drc = False;
3635 ResetConnections (False);
3636 TheFlag = FOUNDFLAG;
3637 ListStart (What, ptr1, ptr2, ptr3);
3638 Bloat = PCB->Bloat;
3639 fBloat = (float) PCB->Bloat;
3640 drc = True;
3641 while (DoIt (True, False))
3643 DumpList ();
3644 /* make the flag changes undoable */
3645 TheFlag = FOUNDFLAG | SELECTEDFLAG;
3646 ResetConnections (False);
3647 User = True;
3648 drc = False;
3649 Bloat = 0;
3650 fBloat = 0.0;
3651 RestoreUndoSerialNumber ();
3652 TheFlag = SELECTEDFLAG;
3653 ListStart (What, ptr1, ptr2, ptr3);
3654 DoIt (True, True);
3655 DumpList ();
3656 TheFlag = FOUNDFLAG;
3657 ListStart (What, ptr1, ptr2, ptr3);
3658 Bloat = PCB->Bloat;
3659 fBloat = (float) PCB->Bloat;
3660 drc = True;
3661 DoIt (True, True);
3662 DumpList ();
3663 drcerr_count++;
3664 LocateError (&x, &y);
3665 BuildObjectList (&object_count, &object_id_list, &object_type_list);
3666 violation = pcb_drc_violation_new (_("Copper areas too close"),
3667 _("Circuits that are too close may bridge during imaging, etching,\n"
3668 "plating, or soldering processes resulting in a direct short."),
3669 x, y,
3670 0, /* ANGLE OF ERROR UNKNOWN */
3671 FALSE, /* MEASUREMENT OF ERROR UNKNOWN */
3672 0, /* MAGNITUDE OF ERROR UNKNOWN */
3673 LENGTH_TO_HUMAN(PCB->Bloat),
3674 LENGTH_DIGITS,
3675 LENGTH_UNITS_STRING,
3676 object_count,
3677 object_id_list,
3678 object_type_list);
3679 append_drc_violation (violation);
3680 pcb_drc_violation_free (violation);
3681 free (object_id_list);
3682 free (object_type_list);
3683 User = False;
3684 drc = False;
3685 if (!throw_drc_dialog())
3686 return (True);
3687 IncrementUndoSerialNumber ();
3688 Undo (True);
3689 /* highlight the rest of the encroaching net so it's not reported again */
3690 TheFlag |= SELECTEDFLAG;
3691 Bloat = 0;
3692 fBloat = 0.0;
3693 ListStart (thing_type, thing_ptr1, thing_ptr2, thing_ptr3);
3694 DoIt (True, True);
3695 DumpList ();
3696 drc = True;
3697 Bloat = PCB->Bloat;
3698 fBloat = (float) PCB->Bloat;
3699 ListStart (What, ptr1, ptr2, ptr3);
3701 drc = False;
3702 DumpList ();
3703 TheFlag = FOUNDFLAG | SELECTEDFLAG;
3704 ResetConnections (False);
3705 return (False);
3708 /*----------------------------------------------------------------------------
3709 * set up a temporary flag to use
3711 void
3712 SaveFindFlag (int NewFlag)
3714 OldFlag = TheFlag;
3715 TheFlag = NewFlag;
3718 /*----------------------------------------------------------------------------
3719 * restore flag
3721 void
3722 RestoreFindFlag (void)
3724 TheFlag = OldFlag;
3727 /* DRC clearance callback */
3729 static int
3730 drc_callback (DataTypePtr data, LayerTypePtr layer, PolygonTypePtr polygon,
3731 int type, void *ptr1, void *ptr2)
3733 char *message;
3734 LocationType x, y;
3735 int object_count;
3736 long int *object_id_list;
3737 int *object_type_list;
3738 DrcViolationType *violation;
3740 LineTypePtr line = (LineTypePtr) ptr2;
3741 ArcTypePtr arc = (ArcTypePtr) ptr2;
3742 PinTypePtr pin = (PinTypePtr) ptr2;
3743 PadTypePtr pad = (PadTypePtr) ptr2;
3745 thing_type = type;
3746 thing_ptr1 = ptr1;
3747 thing_ptr2 = ptr2;
3748 thing_ptr3 = ptr2;
3749 switch (type)
3751 case LINE_TYPE:
3752 if (line->Clearance < 2 * PCB->Bloat)
3754 AddObjectToFlagUndoList (type, ptr1, ptr2, ptr2);
3755 SET_FLAG (TheFlag, line);
3756 message = _("Line with insufficient clearance inside polygon\n");
3757 goto doIsBad;
3759 break;
3760 case ARC_TYPE:
3761 if (arc->Clearance < 2 * PCB->Bloat)
3763 AddObjectToFlagUndoList (type, ptr1, ptr2, ptr2);
3764 SET_FLAG (TheFlag, arc);
3765 message = _("Arc with insufficient clearance inside polygon\n");
3766 goto doIsBad;
3768 break;
3769 case PAD_TYPE:
3770 if (pad->Clearance < 2 * PCB->Bloat)
3771 if (IsPadInPolygon(pad,polygon))
3773 AddObjectToFlagUndoList (type, ptr1, ptr2, ptr2);
3774 SET_FLAG (TheFlag, pad);
3775 message = _("Pad with insufficient clearance inside polygon\n");
3776 goto doIsBad;
3778 break;
3779 case PIN_TYPE:
3780 if (pin->Clearance < 2 * PCB->Bloat)
3782 AddObjectToFlagUndoList (type, ptr1, ptr2, ptr2);
3783 SET_FLAG (TheFlag, pin);
3784 message = _("Pin with insufficient clearance inside polygon\n");
3785 goto doIsBad;
3787 break;
3788 case VIA_TYPE:
3789 if (pin->Clearance && pin->Clearance < 2 * PCB->Bloat)
3791 AddObjectToFlagUndoList (type, ptr1, ptr2, ptr2);
3792 SET_FLAG (TheFlag, pin);
3793 message = _("Via with insufficient clearance inside polygon\n");
3794 goto doIsBad;
3796 break;
3797 default:
3798 Message ("hace: Bad Plow object in callback\n");
3800 return 0;
3802 doIsBad:
3803 AddObjectToFlagUndoList (POLYGON_TYPE, layer, polygon, polygon);
3804 SET_FLAG (FOUNDFLAG, polygon);
3805 DrawPolygon (layer, polygon, 0);
3806 DrawObject (type, ptr1, ptr2, 0);
3807 drcerr_count++;
3808 LocateError (&x, &y);
3809 BuildObjectList (&object_count, &object_id_list, &object_type_list);
3810 violation = pcb_drc_violation_new (message,
3811 _("Circuits that are too close may bridge during imaging, etching,\n"
3812 "plating, or soldering processes resulting in a direct short."),
3813 x, y,
3814 0, /* ANGLE OF ERROR UNKNOWN */
3815 FALSE, /* MEASUREMENT OF ERROR UNKNOWN */
3816 0, /* MAGNITUDE OF ERROR UNKNOWN */
3817 LENGTH_TO_HUMAN(PCB->Bloat),
3818 LENGTH_DIGITS,
3819 LENGTH_UNITS_STRING,
3820 object_count,
3821 object_id_list,
3822 object_type_list);
3823 append_drc_violation (violation);
3824 pcb_drc_violation_free (violation);
3825 free (object_id_list);
3826 free (object_type_list);
3827 if (!throw_drc_dialog())
3829 IsBad = True;
3830 return 1;
3832 IncrementUndoSerialNumber ();
3833 Undo (True);
3834 return 0;
3837 /*-----------------------------------------------------------------------------
3838 * Check for DRC violations
3839 * see if the connectivity changes when everything is bloated, or shrunk
3842 DRCAll (void)
3844 LocationType x, y;
3845 int object_count;
3846 long int *object_id_list;
3847 int *object_type_list;
3848 DrcViolationType *violation;
3849 int tmpcnt;
3850 int nopastecnt = 0;
3852 reset_drc_dialog_message();
3854 IsBad = False;
3855 drcerr_count = 0;
3856 SaveStackAndVisibility ();
3857 ResetStackAndVisibility ();
3858 hid_action ("LayersChanged");
3859 InitConnectionLookup ();
3861 TheFlag = FOUNDFLAG | DRCFLAG | SELECTEDFLAG;
3863 ResetConnections (True);
3865 User = False;
3867 ELEMENT_LOOP (PCB->Data);
3869 PIN_LOOP (element);
3871 if (!TEST_FLAG (DRCFLAG, pin)
3872 && DRCFind (PIN_TYPE, (void *) element, (void *) pin, (void *) pin))
3874 IsBad = True;
3875 break;
3878 END_LOOP;
3879 if (IsBad)
3880 break;
3881 PAD_LOOP (element);
3884 /* count up how many pads have no solderpaste openings */
3885 if (TEST_FLAG (NOPASTEFLAG, pad))
3886 nopastecnt++;
3888 if (!TEST_FLAG (DRCFLAG, pad)
3889 && DRCFind (PAD_TYPE, (void *) element, (void *) pad, (void *) pad))
3891 IsBad = True;
3892 break;
3895 END_LOOP;
3896 if (IsBad)
3897 break;
3899 END_LOOP;
3900 if (!IsBad)
3901 VIA_LOOP (PCB->Data);
3903 if (!TEST_FLAG (DRCFLAG, via)
3904 && DRCFind (VIA_TYPE, (void *) via, (void *) via, (void *) via))
3906 IsBad = True;
3907 break;
3910 END_LOOP;
3912 TheFlag = (IsBad) ? DRCFLAG : (FOUNDFLAG | DRCFLAG | SELECTEDFLAG);
3913 ResetConnections (False);
3914 TheFlag = SELECTEDFLAG;
3915 /* check minimum widths and polygon clearances */
3916 if (!IsBad)
3918 COPPERLINE_LOOP (PCB->Data);
3920 /* check line clearances in polygons */
3921 PlowsPolygon (PCB->Data, LINE_TYPE, layer, line, drc_callback);
3922 if (IsBad)
3923 break;
3924 if (line->Thickness < PCB->minWid)
3926 AddObjectToFlagUndoList (LINE_TYPE, layer, line, line);
3927 SET_FLAG (TheFlag, line);
3928 DrawLine (layer, line, 0);
3929 drcerr_count++;
3930 SetThing (LINE_TYPE, layer, line, line);
3931 LocateError (&x, &y);
3932 BuildObjectList (&object_count, &object_id_list, &object_type_list);
3933 violation = pcb_drc_violation_new (_("Line width is too thin"),
3934 _("Process specifications dictate a minimum feature-width\n"
3935 "that can reliably be reproduced"),
3936 x, y,
3937 0, /* ANGLE OF ERROR UNKNOWN */
3938 TRUE, /* MEASUREMENT OF ERROR KNOWN */
3939 LENGTH_TO_HUMAN(line->Thickness),
3940 LENGTH_TO_HUMAN(PCB->minWid),
3941 LENGTH_DIGITS,
3942 LENGTH_UNITS_STRING,
3943 object_count,
3944 object_id_list,
3945 object_type_list);
3946 append_drc_violation (violation);
3947 pcb_drc_violation_free (violation);
3948 free (object_id_list);
3949 free (object_type_list);
3950 if (!throw_drc_dialog())
3952 IsBad = True;
3953 break;
3955 IncrementUndoSerialNumber ();
3956 Undo (False);
3959 ENDALL_LOOP;
3961 if (!IsBad)
3963 COPPERARC_LOOP (PCB->Data);
3965 PlowsPolygon (PCB->Data, ARC_TYPE, layer, arc, drc_callback);
3966 if (IsBad)
3967 break;
3968 if (arc->Thickness < PCB->minWid)
3970 AddObjectToFlagUndoList (ARC_TYPE, layer, arc, arc);
3971 SET_FLAG (TheFlag, arc);
3972 DrawArc (layer, arc, 0);
3973 drcerr_count++;
3974 SetThing (ARC_TYPE, layer, arc, arc);
3975 LocateError (&x, &y);
3976 BuildObjectList (&object_count, &object_id_list, &object_type_list);
3977 violation = pcb_drc_violation_new (_("Arc width is too thin"),
3978 _("Process specifications dictate a minimum feature-width\n"
3979 "that can reliably be reproduced"),
3980 x, y,
3981 0, /* ANGLE OF ERROR UNKNOWN */
3982 TRUE, /* MEASUREMENT OF ERROR KNOWN */
3983 LENGTH_TO_HUMAN(arc->Thickness),
3984 LENGTH_TO_HUMAN(PCB->minWid),
3985 LENGTH_DIGITS,
3986 LENGTH_UNITS_STRING,
3987 object_count,
3988 object_id_list,
3989 object_type_list);
3990 append_drc_violation (violation);
3991 pcb_drc_violation_free (violation);
3992 free (object_id_list);
3993 free (object_type_list);
3994 if (!throw_drc_dialog())
3996 IsBad = True;
3997 break;
3999 IncrementUndoSerialNumber ();
4000 Undo (False);
4003 ENDALL_LOOP;
4005 if (!IsBad)
4007 ALLPIN_LOOP (PCB->Data);
4009 PlowsPolygon (PCB->Data, PIN_TYPE, element, pin, drc_callback);
4010 if (IsBad)
4011 break;
4012 if (!TEST_FLAG (HOLEFLAG, pin) &&
4013 pin->Thickness - pin->DrillingHole < 2 * PCB->minRing)
4015 AddObjectToFlagUndoList (PIN_TYPE, element, pin, pin);
4016 SET_FLAG (TheFlag, pin);
4017 DrawPin (pin, 0);
4018 drcerr_count++;
4019 SetThing (PIN_TYPE, element, pin, pin);
4020 LocateError (&x, &y);
4021 BuildObjectList (&object_count, &object_id_list, &object_type_list);
4022 violation = pcb_drc_violation_new (_("Pin annular ring too small"),
4023 _("Annular rings that are too small may erode during etching,\n"
4024 "resulting in a broken connection"),
4025 x, y,
4026 0, /* ANGLE OF ERROR UNKNOWN */
4027 TRUE, /* MEASUREMENT OF ERROR KNOWN */
4028 LENGTH_TO_HUMAN((pin->Thickness - pin->DrillingHole) / 2),
4029 LENGTH_TO_HUMAN(PCB->minRing),
4030 LENGTH_DIGITS,
4031 LENGTH_UNITS_STRING,
4032 object_count,
4033 object_id_list,
4034 object_type_list);
4035 append_drc_violation (violation);
4036 pcb_drc_violation_free (violation);
4037 free (object_id_list);
4038 free (object_type_list);
4039 if (!throw_drc_dialog())
4041 IsBad = True;
4042 break;
4044 IncrementUndoSerialNumber ();
4045 Undo (False);
4047 if (pin->DrillingHole < PCB->minDrill)
4049 AddObjectToFlagUndoList (PIN_TYPE, element, pin, pin);
4050 SET_FLAG (TheFlag, pin);
4051 DrawPin (pin, 0);
4052 drcerr_count++;
4053 SetThing (PIN_TYPE, element, pin, pin);
4054 LocateError (&x, &y);
4055 BuildObjectList (&object_count, &object_id_list, &object_type_list);
4056 violation = pcb_drc_violation_new (_("Pin drill size is too small"),
4057 _("Process rules dictate the minimum drill size which can be used"),
4058 x, y,
4059 0, /* ANGLE OF ERROR UNKNOWN */
4060 TRUE, /* MEASUREMENT OF ERROR KNOWN */
4061 LENGTH_TO_HUMAN(pin->DrillingHole),
4062 LENGTH_TO_HUMAN(PCB->minDrill),
4063 LENGTH_DIGITS,
4064 LENGTH_UNITS_STRING,
4065 object_count,
4066 object_id_list,
4067 object_type_list);
4068 append_drc_violation (violation);
4069 pcb_drc_violation_free (violation);
4070 free (object_id_list);
4071 free (object_type_list);
4072 if (!throw_drc_dialog())
4074 IsBad = True;
4075 break;
4077 IncrementUndoSerialNumber ();
4078 Undo (False);
4081 ENDALL_LOOP;
4083 if (!IsBad)
4085 ALLPAD_LOOP (PCB->Data);
4087 PlowsPolygon (PCB->Data, PAD_TYPE, element, pad, drc_callback);
4088 if (IsBad)
4089 break;
4090 if (pad->Thickness < PCB->minWid)
4092 AddObjectToFlagUndoList (PAD_TYPE, element, pad, pad);
4093 SET_FLAG (TheFlag, pad);
4094 DrawPad (pad, 0);
4095 drcerr_count++;
4096 SetThing (PAD_TYPE, element, pad, pad);
4097 LocateError (&x, &y);
4098 BuildObjectList (&object_count, &object_id_list, &object_type_list);
4099 violation = pcb_drc_violation_new (_("Pad is too thin"),
4100 _("Pads which are too thin may erode during etching,\n"
4101 "resulting in a broken or unreliable connection"),
4102 x, y,
4103 0, /* ANGLE OF ERROR UNKNOWN */
4104 TRUE, /* MEASUREMENT OF ERROR KNOWN */
4105 LENGTH_TO_HUMAN(pad->Thickness),
4106 LENGTH_TO_HUMAN(PCB->minWid),
4107 LENGTH_DIGITS,
4108 LENGTH_UNITS_STRING,
4109 object_count,
4110 object_id_list,
4111 object_type_list);
4112 append_drc_violation (violation);
4113 pcb_drc_violation_free (violation);
4114 free (object_id_list);
4115 free (object_type_list);
4116 if (!throw_drc_dialog())
4118 IsBad = True;
4119 break;
4121 IncrementUndoSerialNumber ();
4122 Undo (False);
4125 ENDALL_LOOP;
4127 if (!IsBad)
4129 VIA_LOOP (PCB->Data);
4131 PlowsPolygon (PCB->Data, VIA_TYPE, via, via, drc_callback);
4132 if (IsBad)
4133 break;
4134 if (!TEST_FLAG (HOLEFLAG, via) &&
4135 via->Thickness - via->DrillingHole < 2 * PCB->minRing)
4137 AddObjectToFlagUndoList (VIA_TYPE, via, via, via);
4138 SET_FLAG (TheFlag, via);
4139 DrawVia (via, 0);
4140 drcerr_count++;
4141 SetThing (VIA_TYPE, via, via, via);
4142 LocateError (&x, &y);
4143 BuildObjectList (&object_count, &object_id_list, &object_type_list);
4144 violation = pcb_drc_violation_new (_("Via annular ring too small"),
4145 _("Annular rings that are too small may erode during etching,\n"
4146 "resulting in a broken connection"),
4147 x, y,
4148 0, /* ANGLE OF ERROR UNKNOWN */
4149 TRUE, /* MEASUREMENT OF ERROR KNOWN */
4150 LENGTH_TO_HUMAN((via->Thickness - via->DrillingHole) / 2),
4151 LENGTH_TO_HUMAN(PCB->minRing),
4152 LENGTH_DIGITS,
4153 LENGTH_UNITS_STRING,
4154 object_count,
4155 object_id_list,
4156 object_type_list);
4157 append_drc_violation (violation);
4158 pcb_drc_violation_free (violation);
4159 free (object_id_list);
4160 free (object_type_list);
4161 if (!throw_drc_dialog())
4163 IsBad = True;
4164 break;
4166 IncrementUndoSerialNumber ();
4167 Undo (False);
4169 if (via->DrillingHole < PCB->minDrill)
4171 AddObjectToFlagUndoList (VIA_TYPE, via, via, via);
4172 SET_FLAG (TheFlag, via);
4173 DrawVia (via, 0);
4174 drcerr_count++;
4175 SetThing (VIA_TYPE, via, via, via);
4176 LocateError (&x, &y);
4177 BuildObjectList (&object_count, &object_id_list, &object_type_list);
4178 violation = pcb_drc_violation_new (_("Via drill size is too small"),
4179 _("Process rules dictate the minimum drill size which can be used"),
4180 x, y,
4181 0, /* ANGLE OF ERROR UNKNOWN */
4182 TRUE, /* MEASUREMENT OF ERROR KNOWN */
4183 LENGTH_TO_HUMAN(via->DrillingHole),
4184 LENGTH_TO_HUMAN(PCB->minDrill),
4185 LENGTH_DIGITS,
4186 LENGTH_UNITS_STRING,
4187 object_count,
4188 object_id_list,
4189 object_type_list);
4190 append_drc_violation (violation);
4191 pcb_drc_violation_free (violation);
4192 free (object_id_list);
4193 free (object_type_list);
4194 if (!throw_drc_dialog())
4196 IsBad = True;
4197 break;
4199 IncrementUndoSerialNumber ();
4200 Undo (False);
4203 END_LOOP;
4206 FreeConnectionLookupMemory ();
4207 TheFlag = FOUNDFLAG;
4208 Bloat = 0;
4209 fBloat = 0.0;
4211 /* check silkscreen minimum widths outside of elements */
4212 /* XXX - need to check text and polygons too! */
4213 TheFlag = SELECTEDFLAG;
4214 if (!IsBad)
4216 SILKLINE_LOOP (PCB->Data);
4218 if (line->Thickness < PCB->minSlk)
4220 SET_FLAG (TheFlag, line);
4221 DrawLine (layer, line, 0);
4222 drcerr_count++;
4223 SetThing (LINE_TYPE, layer, line, line);
4224 LocateError (&x, &y);
4225 BuildObjectList (&object_count, &object_id_list, &object_type_list);
4226 violation = pcb_drc_violation_new (_("Silk line is too thin"),
4227 _("Process specifications dictate a minimum silkscreen feature-width\n"
4228 "that can reliably be reproduced"),
4229 x, y,
4230 0, /* ANGLE OF ERROR UNKNOWN */
4231 TRUE, /* MEASUREMENT OF ERROR KNOWN */
4232 LENGTH_TO_HUMAN(line->Thickness),
4233 LENGTH_TO_HUMAN(PCB->minSlk),
4234 LENGTH_DIGITS,
4235 LENGTH_UNITS_STRING,
4236 object_count,
4237 object_id_list,
4238 object_type_list);
4239 append_drc_violation (violation);
4240 pcb_drc_violation_free (violation);
4241 free (object_id_list);
4242 free (object_type_list);
4243 if (!throw_drc_dialog())
4245 IsBad = True;
4246 break;
4250 ENDALL_LOOP;
4253 /* check silkscreen minimum widths inside of elements */
4254 /* XXX - need to check text and polygons too! */
4255 TheFlag = SELECTEDFLAG;
4256 if (!IsBad)
4258 ELEMENT_LOOP (PCB->Data);
4260 tmpcnt = 0;
4261 ELEMENTLINE_LOOP (element);
4263 if (line->Thickness < PCB->minSlk)
4264 tmpcnt++;
4266 END_LOOP;
4267 if (tmpcnt > 0)
4269 char *title;
4270 char *name;
4271 char *buffer;
4272 int buflen;
4274 SET_FLAG (TheFlag, element);
4275 DrawElement (element, 0);
4276 drcerr_count++;
4277 SetThing (ELEMENT_TYPE, element, element, element);
4278 LocateError (&x, &y);
4279 BuildObjectList (&object_count, &object_id_list, &object_type_list);
4281 title = _("Element %s has %i silk lines which are too thin");
4282 name = UNKNOWN (NAMEONPCB_NAME (element));
4284 /* -4 is for the %s and %i place-holders */
4285 /* +11 is the max printed length for a 32 bit integer */
4286 /* +1 is for the \0 termination */
4287 buflen = strlen (title) - 4 + strlen (name) + 11 + 1;
4288 buffer = malloc (buflen);
4289 snprintf (buffer, buflen, title, name, tmpcnt);
4291 violation = pcb_drc_violation_new (buffer,
4292 _("Process specifications dictate a minimum silkscreen\n"
4293 "feature-width that can reliably be reproduced"),
4294 x, y,
4295 0, /* ANGLE OF ERROR UNKNOWN */
4296 0, /* MINIMUM OFFENDING WIDTH UNKNOWN */
4297 TRUE, /* MEASUREMENT OF ERROR KNOWN */
4298 LENGTH_TO_HUMAN(PCB->minSlk),
4299 LENGTH_DIGITS,
4300 LENGTH_UNITS_STRING,
4301 object_count,
4302 object_id_list,
4303 object_type_list);
4304 free (buffer);
4305 append_drc_violation (violation);
4306 pcb_drc_violation_free (violation);
4307 free (object_id_list);
4308 free (object_type_list);
4309 if (!throw_drc_dialog())
4311 IsBad = True;
4312 break;
4316 END_LOOP;
4320 if (IsBad)
4322 IncrementUndoSerialNumber ();
4326 RestoreStackAndVisibility ();
4327 hid_action ("LayersChanged");
4328 gui->invalidate_all ();
4330 if (nopastecnt > 0)
4332 Message (_("Warning: %d pad%s the nopaste flag set.\n"),
4333 nopastecnt,
4334 nopastecnt > 1 ? "s have" : " has");
4336 return IsBad ? -drcerr_count : drcerr_count;
4339 /*----------------------------------------------------------------------------
4340 * Locate the coordinatates of offending item (thing)
4342 static void
4343 LocateError (LocationType *x, LocationType *y)
4345 switch (thing_type)
4347 case LINE_TYPE:
4349 LineTypePtr line = (LineTypePtr) thing_ptr3;
4350 *x = (line->Point1.X + line->Point2.X) / 2;
4351 *y = (line->Point1.Y + line->Point2.Y) / 2;
4352 break;
4354 case ARC_TYPE:
4356 ArcTypePtr arc = (ArcTypePtr) thing_ptr3;
4357 *x = arc->X;
4358 *y = arc->Y;
4359 break;
4361 case POLYGON_TYPE:
4363 PolygonTypePtr polygon = (PolygonTypePtr) thing_ptr3;
4364 *x =
4365 (polygon->Clipped->contours->xmin +
4366 polygon->Clipped->contours->xmax) / 2;
4367 *y =
4368 (polygon->Clipped->contours->ymin +
4369 polygon->Clipped->contours->ymax) / 2;
4370 break;
4372 case PIN_TYPE:
4373 case VIA_TYPE:
4375 PinTypePtr pin = (PinTypePtr) thing_ptr3;
4376 *x = pin->X;
4377 *y = pin->Y;
4378 break;
4380 case PAD_TYPE:
4382 PadTypePtr pad = (PadTypePtr) thing_ptr3;
4383 *x = (pad->Point1.X + pad->Point2.X) / 2;
4384 *y = (pad->Point1.Y + pad->Point2.Y) / 2;
4385 break;
4387 case ELEMENT_TYPE:
4389 ElementTypePtr element = (ElementTypePtr) thing_ptr3;
4390 *x = element->MarkX;
4391 *y = element->MarkY;
4392 break;
4394 default:
4395 return;
4400 /*----------------------------------------------------------------------------
4401 * Build a list of the of offending items by ID. (Currently just "thing")
4403 static void
4404 BuildObjectList (int *object_count, long int **object_id_list, int **object_type_list)
4406 *object_count = 0;
4407 *object_id_list = NULL;
4409 switch (thing_type)
4411 case LINE_TYPE:
4412 case ARC_TYPE:
4413 case POLYGON_TYPE:
4414 case PIN_TYPE:
4415 case VIA_TYPE:
4416 case PAD_TYPE:
4417 case ELEMENT_TYPE:
4418 *object_count = 1;
4419 *object_id_list = malloc (sizeof (long int));
4420 *object_type_list = malloc (sizeof (int));
4421 **object_id_list = ((AnyObjectType *)thing_ptr3)->ID;
4422 **object_type_list = thing_type;
4423 return;
4425 default:
4426 return;
4431 /*----------------------------------------------------------------------------
4432 * center the display to show the offending item (thing)
4434 static void
4435 GotoError (void)
4437 LocationType X, Y;
4439 LocateError (&X, &Y);
4441 switch (thing_type)
4443 case LINE_TYPE:
4444 case ARC_TYPE:
4445 case POLYGON_TYPE:
4446 ChangeGroupVisibility (GetLayerNumber
4447 (PCB->Data, (LayerTypePtr) thing_ptr1), True,
4448 True);
4450 CenterDisplay (X, Y, False);
4453 void
4454 InitConnectionLookup (void)
4456 InitComponentLookup ();
4457 InitLayoutLookup ();
4460 void
4461 FreeConnectionLookupMemory (void)
4463 FreeComponentLookupMemory ();
4464 FreeLayoutLookupMemory ();