File: irRoute.c

package info (click to toggle)
magic 7.5.220-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 17,860 kB
file content (1187 lines) | stat: -rw-r--r-- 29,052 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
/*
 * irRoute.c --
 *
 * Sets up route and calls maze router to do it.
 *
 *     ********************************************************************* 
 *     * Copyright (C) 1987, 1990 Michael H. Arnold, Walter S. Scott, and  *
 *     * the Regents of the University of California.                      *
 *     * Permission to use, copy, modify, and distribute this              * 
 *     * software and its documentation for any purpose and without        * 
 *     * fee is hereby granted, provided that the above copyright          * 
 *     * notice appear in all copies.  The University of California        * 
 *     * makes no representations about the suitability of this            * 
 *     * software for any purpose.  It is provided "as is" without         * 
 *     * express or implied warranty.  Export of this software outside     * 
 *     * of the United States of America may require an export license.    * 
 *     *********************************************************************
 */

#ifndef lint
static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-7.5/irouter/irRoute.c,v 1.8 2008/03/06 17:56:53 tim Exp $";
#endif  /* not lint */

/* --- Includes --- */

#include <stdio.h>
#include <string.h>

#include "utils/magic.h"
#include "utils/geometry.h"
#include "utils/hash.h"
#include "tiles/tile.h"
#include "database/database.h"
#include "drc/drc.h"
#include "textio/textio.h"
#include "windows/windows.h"
#include "utils/main.h"
#include "dbwind/dbwind.h"
#include "debug/debug.h"
#include "utils/undo.h"
#include "utils/signals.h"
#include "utils/malloc.h"
#include "utils/list.h"
#include "utils/geofast.h"
#include "utils/touchingtypes.h"
#include "select/select.h"
#include "../mzrouter/mzrouter.h"
#include "irouter/irouter.h"
#include "irouter/irInternal.h"

/* --- Routines local to this file that are referenced before they are 
 *     defined --- */
List *irChooseEndPtLayers();

/* -------------------- Structures Local to this File -------------------- */

/* clientdata structure passed to filter functions when searching for
 * start and destination labels.
 */
typedef struct labelSearchData
{
    Rect 	lsd_locRect;	/* set to labels location by filter func */
    char	*lsd_name;	/* label name to search for */
    TileType	lsd_type;	/* layer that label is attached to */
    int 	lsd_result;	/* code giving result of search */
} LabelSearchData;

/* result codes for lsd_result above */
#define LSR_NOTFOUND	10
#define LSR_NOTUNIQUE	20
#define LSR_FOUND	30


/*
 * ----------------------------------------------------------------------------
 *
 * irRoute --
 *
 * Top level procedure for the routing code.  Initializes things, and
 * calls the maze router to make a connection.
 *
 * Results:
 *	Passes back the result code of MZRoute() (see mzrouter.h for codes)
 *
 * Side effects:
 *	Paint route into editcell.
 *
 * ----------------------------------------------------------------------------
 */

int
irRoute(cmdWindow, startType, argStartPt, argStartLabel, argStartLayers, 
		destType, argDestRect, argDestLabel, argDestLayers)
    MagWindow *cmdWindow;	/* window route command issued to */
    int startType;	/* how start is specified */
    Point *argStartPt;	/* location to route from (in edit cell coords) */
    char *argStartLabel;	/* label to route from */
    List *argStartLayers;	/* OK route layers to start route on */
    int destType;	/* how dest is specified */
    Rect *argDestRect;	/* location to route to (in edit cell coords) */
    char *argDestLabel; /* label to route to */
    List *argDestLayers;	/* OK route layers to end route on */
{
    CellUse *routeUse;		/* Toplevel cell visible during routing */
    int expansionMask;		/* Subcell expansion modes to use during 
				 * routing */
    Point startPt; 		/* start and dest terminals */
    List *startLayers = NULL;
    Rect destRect;
    List *destLayers = NULL;
    RoutePath *path = NULL;	/* resulting path */
    TileType startLayer = TT_SPACE;
    int mzResult = MZ_NO_ACTION;

    /* determine routeUse and expansionMask for this route. */
    {
	MagWindow *window = NULL;

	/* find global reference window */
	if(irRouteWid>=0)
	{
	    window = WindSearchWid(irRouteWid);
	    if (window == NULL)
	    {
		TxError("Couldn't find route window (%d),", irRouteWid);
	        TxError("using command window as reference.\n");
	    }
	}

	/* if no global reference window, use window command issued from */
	if(window == NULL)
	{
	    window = cmdWindow;
	}

	/* If reference window is nil, complain and exit */
	if(window==NULL)
	{
	    TxError("Point to a layout window first.\n");
	    return mzResult;
	}

	/* Set expansion mask to window route cmd issued to.  Used 
	 * during searches.  Subcells are treated as expanded if expanded in
	 * window cmd issued to.
	 */
        expansionMask = ((DBWclientRec *)(window->w_clientData))->dbw_bitmask;

	/* Set routeUse to rootuse of reference window - 
	 *  everything "visible" in 
	 *  the reference window is visible
	 *  during routing.  
	 * But resulting route
	 * is painted into edit cell.  This distinction is important only
	 * in the case of a subedit.  If the user subedits a cell, the
	 * context of the parent(s) will guide the route, but the route 
	 * will be painted into the edit cell.
	 */
	routeUse = (CellUse *) (window->w_surfaceID);

	/* make sure cmd issued from window in which edit cell 
	 * is being edited */
	if (!EditCellUse || EditRootDef != routeUse->cu_def)
	{
	    TxError("Nothing being edited in route window.\n");
	    return mzResult;
	}
    }

    /* initialize mzrouter */
    MZInitRoute(irMazeParms, routeUse, expansionMask);

    /* Figure out start coordinates */
    {
	Point irGetStartPoint();

	startPt = irGetStartPoint(startType, 
				  argStartPt, 
				  argStartLabel,
				  &startLayer,
				  routeUse);

	/* check for failure */
	if(startPt.p_x == MINFINITY) goto abort;
    }

    /* Set maze router dest area(s) */

    if(destType == DT_SELECTION)
    /* add destination area for each selected area on an appropriate layer */
    {
	int irSelectedTileFunc();

	if(argDestLayers == NULL)
	/* no layer arg specified, generate dest areas for each active
	 * route layer
	 */
	{
	    RouteLayer *rL;
		
	    for (rL = irRouteLayers; rL != NULL; rL = rL->rl_next)
	    {
		if(rL->rl_routeType.rt_active)
		{
		    /* set dest area for each selected tile 
		     * of type connecting to rL
		     */
		    SelEnumPaint(
				 &(DBConnectTbl[rL->rl_routeType.rt_tileType]),
				 FALSE,	/* TRUE = restricted to edit cell */
				 NULL, /* not used */
				 irSelectedTileFunc, 
				 (ClientData *) rL /* type of destarea */
				 );
		}
	    }
	}
	else
	/* generate dest areas for layers that are both specified and active */
	{
	    List *l;

	    for(l=argDestLayers; l!=NULL; l=LIST_TAIL(l))
	    {
		RouteLayer *rL = (RouteLayer *) LIST_FIRST(l);

		if(rL->rl_routeType.rt_active)
		{
		    /* set dest area for each selected tile 
		     * of type connecting to rL
		     */
		    SelEnumPaint(
				 &(DBConnectTbl[rL->rl_routeType.rt_tileType]),
				 FALSE,	/* TRUE = restricted to edit cell */
				 NULL, /* not used */
				 irSelectedTileFunc, 
				 (ClientData *) rL /* type of destarea */
				 );
		}
	    }
	}
    }
    else
    /* dest is defined by rectangle */
    {
	Rect irGetDestRect();
	TileType destLayer = TT_SPACE;

	destRect = irGetDestRect(destType, 
				 argDestRect, 
				 argDestLabel,
				 &destLayer,
				 routeUse);

	/* check for failure */
	if(destRect.r_xtop == MINFINITY) goto abort;

	/* set a maze router dest area for extent of destRect
	 * on each permitted dest layer.
	 */
	if (destLayer != TT_SPACE)
	{
	    RouteLayer *rL;

	    /* layer type returned by irGetDestRect */

	    for(rL = irRouteLayers; rL != NULL; rL = rL->rl_next)
	    {
		if (rL->rl_routeType.rt_active &&
			TTMaskHasType(&(DBConnectTbl[destLayer]),
                                   rL->rl_routeType.rt_tileType))
		{
		    MZAddDest(&destRect, rL->rl_routeType.rt_tileType);
		    break;
		}
	    }
	}
	else if (argDestLayers == NULL)
	{
	    /* no layer arg specified, permit all active route layers */
	    RouteLayer *rL;
	    
	    for (rL = irRouteLayers; rL != NULL; rL = rL->rl_next)
	    {
		if(rL->rl_routeType.rt_active)
		{
		    MZAddDest(&destRect,rL->rl_routeType.rt_tileType);
		}
	    }
	}
	else
	/* permit only layers that are both specified and active */
	{
	    List *l;
	    
	    for(l=argDestLayers; l!=NULL; l=LIST_TAIL(l))
	    {
		RouteLayer *rL = (RouteLayer *) LIST_FIRST(l);
		
		if(rL->rl_routeType.rt_active)
		{
		    MZAddDest(&destRect,rL->rl_routeType.rt_tileType);
		    
		}
	    }
	}	    
    }

    if (startLayer != TT_SPACE)
    {
	RouteLayer *rL;

	/* layer type returned by irGetStartPoint */

	for (rL = irRouteLayers; rL != NULL; rL = rL->rl_next)
	{
	    if (rL->rl_routeType.rt_active &&
			TTMaskHasType(&(DBConnectTbl[startLayer]),
                                   rL->rl_routeType.rt_tileType))

	    {
		MZAddStart(&startPt, rL->rl_routeType.rt_tileType);
		break;
	    }
	}
    }
    else
    {
	/* Determine OK start layers */

	List *l;

	startLayers = irChooseEndPtLayers(
					  routeUse, 
					  expansionMask, 
					  &startPt,
					  argStartLayers,
					  "start");
	if(SigInterruptPending) goto abort;

	if(DebugIsSet(irDebugID,irDebEndPts))
	{
	    TxPrintf("----- startLayers:\n");
	    MZPrintRLListNames(startLayers);
	}

	/* Set maze router start point(s) - one for each ok layer */

	for(l=startLayers; l!=NULL;l=LIST_TAIL(l))
	{
	    RouteLayer *rL = (RouteLayer *)LIST_FIRST(l);
	    TileType type = rL->rl_routeType.rt_tileType;

	    MZAddStart(&startPt, type);
	}
    }

    /* Do the Route */
    path = MZRoute(&mzResult);
		      /* If MZRoute is interrupted it returns best path
		       * found so far.
		       */
    if(SigInterruptPending)
    {
	if(path==NULL)
	{
	    goto abort;
	}
	else
	{
	    TxError("Search Interrupted!\n");
	    TxPrintf("Using best path found prior to interrupt.\n");

	    /* Clear interrupt to allow paint back of path */
	    SigInterruptPending = FALSE;
	}
    }

    /* paint route back into edit cell */
    if (path) 
    {
	
	RouteLayer *finalRL = path->rp_rLayer;
	CellUse *resultUse;

        /* Have MazeRouter paint path into resultCell */
	MZClean();
        resultUse = MZPaintPath(path);
	if(SigInterruptPending) goto abort;

	/* Copy to edit cell transforming from root to edit
	 * coords.
	 * Also select the entire route.
	 * This paint job is undoable.
	 */
	{
	    SearchContext scx;

	    scx.scx_use = resultUse;
	    scx.scx_area = resultUse->cu_def->cd_bbox;
	    scx.scx_trans = RootToEditTransform;
	    (void) DBCellCopyPaint(&scx, &DBAllButSpaceBits, 0, EditCellUse);
	    DBReComputeBbox(EditCellUse->cu_def);
	}

	/* Select the route */
	{
	    SearchContext scx;

	    /* Clear selection, and set selection display for reference
	     * window and other windows containing routeUse as root.
	     */
	    SelectClear();
	    if(SelectRootDef != routeUse->cu_def)
	    {
		SelectRootDef = routeUse->cu_def;
		SelSetDisplay(SelectUse, SelectRootDef);
	    }

	    /* Copy route to selection cell, notifying undo of change */
	    scx.scx_use = resultUse;
	    scx.scx_area = resultUse->cu_def->cd_bbox;
	    scx.scx_trans = GeoIdentityTransform;
	    SelRememberForUndo(TRUE, (CellDef *) NULL, (Rect *) NULL);
	    (void) DBCellCopyPaint(&scx, &DBAllButSpaceBits, 0, SelectUse);
	    SelRememberForUndo(FALSE, SelectRootDef, &(scx.scx_area));

	    /* Setup redisplay */
	    DBReComputeBbox(SelectDef);
	    DBWHLRedraw(SelectRootDef, &(scx.scx_area), TRUE);
	    DBWAreaChanged(SelectDef, &SelectDef->cd_bbox, DBW_ALLWINDOWS,
			   &DBAllButSpaceBits);
	}

	/* Notify dbwind module (for redisplay), and DRC module 
	 * of changed area */
	{
	    Rect changedArea;

	    GeoTransRect(
			 &RootToEditTransform,
			 &(resultUse->cu_def->cd_bbox),
			 &changedArea);
	    DBWAreaChanged(EditCellUse->cu_def, &changedArea, DBW_ALLWINDOWS,
			   &DBAllButSpaceBits);
	    DRCCheckThis(EditCellUse->cu_def, TT_CHECKPAINT, &changedArea);

	}

	/* Make sure we got here without interruption */
	if(SigInterruptPending) goto abort;

	TxPrintf("Done Routing.\n");
	TxFlushOut();
    }
    else
    {
	TxError("Route Failed.\n");
    }

abort:
    if(SigInterruptPending)
    {
	TxError("Route Interrupted!\n");
    }

    ListDealloc(startLayers);
    ListDealloc(destLayers);

    /* reclaim storage used by mzrouter */
    if(!DebugIsSet(irDebugID, irDebNoClean))
    {
	MZClean();
    }

    return mzResult;
}


/*
 * ---------------------------------------------------------------------
 *
 * irGetStartPoint --
 *
 * Compute start point.
 *
 * Results:
 *	Returns point.
 *
 * Side effects:
 *	Returns start layer type in startLayerPtr.
 *
 * ---------------------------------------------------------------------
 */

Point
irGetStartPoint(startType, argStartPt, argStartLabel, startLayerPtr, routeUse)
    int startType;		/* how start is specified */
    Point *argStartPt;		/* location to route from 
				 * (in edit cell coords) */
    char *argStartLabel;	/* label to route from */
    TileType *startLayerPtr;	/* layer type (returned value) */
    CellUse *routeUse;		/* toplevel cell visible to router */
{
    Point startPt;

    switch (startType)
    {
	case ST_POINT:
	/* start point coords given */
	{
	    /* convert from edit to routeUse coords (= root coordinates) */
	    GeoTransPoint(&EditToRootTransform,argStartPt,&startPt);
	}
	break;

	case ST_CURSOR:
	/* use cursor */
	{
	    MagWindow *pointWindow;

	    pointWindow = ToolGetPoint(&startPt, (Rect *) NULL);

	    if (pointWindow == NULL)
	    {
		TxError("Can not use cursor as start:");
		TxError("  cursor not in layout window.\n");
		goto abort;
	    }

	    if (routeUse->cu_def  != 
		((CellUse *)pointWindow->w_surfaceID)->cu_def)
	    {
		TxError("Can not use cursor as start:");
		TxError("cursor not in routecell.\n");
		goto abort;
	    }
	}
	break;

	case ST_LABEL:
	/* label name given */
	{   
	    int irSelLabelsFunc();
	    int irAllLabelsFunc();
	    LabelSearchData lSD;
	    lSD.lsd_name = argStartLabel;   /* name to match */
	    lSD.lsd_result = LSR_NOTFOUND;

	    /* first search selection */
	    (void) SelEnumLabels(&DBAllTypeBits,
				 FALSE,	/* TRUE = search only edit cell */
				 (bool *) NULL,
				 irSelLabelsFunc,
				 (ClientData) &lSD);

	    if(SigInterruptPending) goto abort;

	    if (lSD.lsd_result == LSR_NOTUNIQUE)
	    {
		TxError("Warning: Start label '%s' not unique.\n", argStartLabel);
	    }
	    else if (lSD.lsd_result == LSR_NOTFOUND)
	    /* No selected label matched, so search all labels */
	    {

		(void) DBSrLabelLoc(routeUse,
				    argStartLabel, 
				    irAllLabelsFunc, 
				    (ClientData) &lSD);

		if(SigInterruptPending) goto abort;
	
		if (lSD.lsd_result == LSR_NOTUNIQUE)
		{
		    TxError("Warning: Start label '%s' not unique.\n", argStartLabel);
		}
		else if (lSD.lsd_result == LSR_NOTFOUND)
		{
		    TxError("Start label '%s' not found.\n",
			    argStartLabel);
		    goto abort;
		}
	    }

	    startPt = lSD.lsd_locRect.r_ll;
	    if (startLayerPtr) *startLayerPtr = lSD.lsd_type;
	}
	break;

	default:
	/* shouldn't happen */
	{
	    ASSERT(FALSE,"irGetStartPoint");
	}
	break;
    }

    return startPt;

abort:
    startPt.p_x = MINFINITY;
    startPt.p_y = MINFINITY;
    return startPt;
}


/*
 * ---------------------------------------------------------------------
 *
 * irGetDestRect --
 *
 * Compute destination rectangle.
 *
 * Results:
 *	Returns rect.
 *
 * Side effects:
 *	Returns layer type in destLayerPtr.
 *
 * ---------------------------------------------------------------------
 */

Rect
irGetDestRect(destType, argDestRect, argDestLabel, destLayerPtr, routeUse)
    int destType;		/* how dest is specified */
    Rect *argDestRect;		/* location to route to 
				 * (in edit cell coords) */
    char *argDestLabel;		/* label to route to */
    TileType *destLayerPtr;	/* layer type (returned value) */
    CellUse *routeUse;		/* toplevel cell visible to router */
{
    Rect destRect;

    switch (destType)
    {
	case DT_RECT:
	/* dest rect coords given */
	{
	    /* convert from edit to routeUse coords (= root coordinates) */
	    GeoTransRect(&EditToRootTransform,argDestRect,&destRect);
	}
	break;

	case DT_LABEL:
	/* dest rect given as label */
	{   
	    int irSelLabelsFunc();
	    int irAllLabelsFunc();
	    LabelSearchData lSD;
	    lSD.lsd_name = argDestLabel;   /* name to match */
	    lSD.lsd_result = LSR_NOTFOUND;

	    /* first search selection */
	    (void) SelEnumLabels(&DBAllTypeBits,
				 FALSE,	/* TRUE = search only edit cell */
				 (bool *) NULL,
				 irSelLabelsFunc,
				 (ClientData) &lSD);
	    if(SigInterruptPending) goto abort;

	    if (lSD.lsd_result == LSR_NOTUNIQUE)
	    {
		TxError("Warning: Destination label '%s' not unique.\n", argDestLabel);
	    }
	    else if (lSD.lsd_result == LSR_NOTFOUND)
	    {
		/* No selected label matched, so search all labels */
		(void) DBSrLabelLoc(routeUse,
				    argDestLabel, 
				    irAllLabelsFunc, 
				    (ClientData) &lSD);
		if(SigInterruptPending) goto abort;

		if (lSD.lsd_result == LSR_NOTUNIQUE)
		{
		    TxError("Warning: Destination label '%s' not unique.\n",
				argDestLabel);
		}
		else if (lSD.lsd_result == LSR_NOTFOUND)
		{
		    TxError("Destination label '%s' not found.\n",
			    argDestLabel);
		    goto abort;
		}
	    }

	    destRect = lSD.lsd_locRect;
	    if (destLayerPtr) *destLayerPtr = lSD.lsd_type;
	}
	break;

	case DT_BOX:
	/* use box as dest rect */
	{
	    CellDef *boxDef;
	    Rect box;

	    if(!ToolGetBox(&boxDef,&box))
	    {
		TxError("Can not use box for dest:  No Box.\n");
		goto abort;
	    }

	    if (boxDef != routeUse->cu_def)
	    {
		TxError("Can not use box for dest:  ");
		TxError("box not in route cell.\n");
		goto abort;
	    }

	    destRect = box;
	}
	break;

	default:
	/* shouldn't happen */
	{
	    ASSERT(FALSE,"irGetDestRect");
	}
	break;
    }

    return destRect;

abort:
    destRect.r_xbot = MINFINITY;
    destRect.r_ybot = MINFINITY;
    destRect.r_xtop = MINFINITY;
    destRect.r_ytop = MINFINITY;
    return destRect;
}

/*
 * ---------------------------------------------------------------------
 *
 * irSelLabelsFunc --
 *
 * Called by SelEnumLabels on behalf of irRoute above, to find selected
 * label of given name.
 *
 * Results:
 *	Returns 0 on first match, 1 on second match (to terminate search)
 *
 * Side effects:
 *	Sets locRect in clientdata arg location off matching label.
 *
 * ---------------------------------------------------------------------
 */

int
irSelLabelsFunc(label, cellUse, transform, clientData)
    Label *label;
    CellUse *cellUse;
    Transform *transform;
    ClientData clientData;
{
    LabelSearchData *lsd = (LabelSearchData *)clientData;
    CellDef *cellDef = cellUse->cu_def;

    if (strcmp(lsd->lsd_name, label->lab_text) != 0)
    {
	/* this label doesn't match, continue search */
	return 0;
    }
    else if (lsd->lsd_result == LSR_FOUND)
    {
	/* second match, set result and terminate search */
	lsd->lsd_result = LSR_NOTUNIQUE;
	return 1;
    }
    else
    {
	/* first match, set location, result, and continue search */
	GeoTransRect(transform,
		&(label->lab_rect), &(lsd->lsd_locRect));
	lsd->lsd_result = LSR_FOUND;
	lsd->lsd_type = label->lab_type;
	return 0;
    }
}


/*
 * ---------------------------------------------------------------------
 *
 * irAllLabelsFunc --
 *
 * Called by DBSrLabelLoc on behalf of irRoute above, to convert labelName
 * to location of label with matching name.
 *
 * Results:
 *	Returns 0 on first match, 1 on second match (to terminate search)
 *
 * Side effects:
 *	Sets locRect in clientdata arg to location of matching label.
 *
 * ---------------------------------------------------------------------
 */

int
irAllLabelsFunc(rect, name, label, clientData)
    Rect *rect;
    char *name;
    Label *label;
    ClientData clientData;
{
    LabelSearchData *lsd = (LabelSearchData *)clientData;

    if (lsd->lsd_result == LSR_FOUND)
    {
	if (GEO_SAMERECT(lsd->lsd_locRect, *rect)) return 0;

	/* second match, so set result and terminate search */
	lsd->lsd_result = LSR_NOTUNIQUE;
	return 1;
    }
    else
    {
	/* first match, so set location, result, and continue search */
	lsd->lsd_locRect = *rect;
	lsd->lsd_type = label->lab_type;
	lsd->lsd_result = LSR_FOUND;
	return 0;
    }
}


/*
 * ---------------------------------------------------------------------
 *
 * irSelectedTileFunc --
 *
 * Called by SelEnumPaint on behalf of irRoute above, to process tile
 * associated with selection rect.
 *
 * Results:
 *	Always returns 0 to continue search.
 *
 * Side effects:
 *	Call MzAddDest on selected area.
 *
 * ---------------------------------------------------------------------
 */

int
irSelectedTileFunc(rect, type, c)
    Rect *rect;
    TileType type;
    ClientData c;
{
    RouteLayer *rL = (RouteLayer *) c;
    MZAddDest(rect, rL->rl_routeType.rt_tileType);
    
    /* return 0 to continue search */
    return 0;
}


/*
 * ----------------------------------------------------------------------------
 *
 * LayerInTouchingContact --
 *
 * A Predicate.  Checks whether the given routeLayer is a component of a 
 * contact type in touchingTypes.  Used by irChooseEndPtLayers below.
 *
 * Results:
 *	TRUE if the RouteLayer is a component of a contact type in 
 *	touchingTypes, else FALSE.
 *
 * Side effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */

bool
LayerInTouchingContact(rL,touchingTypes)
    RouteLayer *rL;
    TileTypeBitMask touchingTypes;
{
    RouteContact *rC;

    for(rC=irRouteContacts; rC!=NULL; rC=rC->rc_next)
    {
	if(TTMaskHasType(&touchingTypes,rC->rc_routeType.rt_tileType) &&
	        (rC->rc_rLayer1==rL || rC->rc_rLayer2==rL))
	    return(TRUE);
    }

    return(FALSE);
}


/*
 * ----------------------------------------------------------------------------
 *
 * irChooseEndPtLayers --
 *
 * Figure out what layers are ok at an endpoint - if ambiguous ask user.
 *
 * Results:
 *	List of ok layers at endpoint.
 *
 * Side effects:
 *	May query user about intended layers at an endpoint.
 *
 * ----------------------------------------------------------------------------
 */

List *
irChooseEndPtLayers(routeUse,expansionMask,endPt,argLayers,endPtName)
    CellUse *routeUse;
    int expansionMask; /* mask of expanded subcells */
    Point *endPt;
    List *argLayers;
    char *endPtName;
{
    List *activeLayers;
    List *presentLayers;
    List *presentContacts;
    List *presentContactLayers;

    static char *actionNames[] = { "no", "yes", 0 };

    List *l;
    RouteLayer *rL;

    if (DebugIsSet(irDebugID, irDebEndPts))
    {
	TxPrintf("----- argLayers:\n");
	MZPrintRLListNames(argLayers);
    }

    /* find activeLayers among argLayers (or irRouteLayers if argLayers NULL) */
    activeLayers = NULL;
    if(argLayers)
    {
	/* layers given in arg, search these */
	for(l=argLayers; l!=NULL; l=LIST_TAIL(l))
	{
	    rL = (RouteLayer*) LIST_FIRST(l);
	    if(rL->rl_routeType.rt_active)
	    {
		LIST_ADD(rL,activeLayers);
	    }
	}
    }
    else
    {
	/* no layers given as arg, so search all route layers */
        for(rL=irRouteLayers; rL!=NULL; rL=rL->rl_next)
	    if(rL->rl_routeType.rt_active)
	    {
		LIST_ADD(rL,activeLayers);
	    }
    }

    if (DebugIsSet(irDebugID, irDebEndPts))
    {
	TxPrintf("----- activeLayers:\n");
	MZPrintRLListNames(activeLayers);
    }

    /* make lists of contacts (connecting two active layers) and
     * active layers which are present at the end point.
     */
    {
	TileTypeBitMask touchingTypes;
	RouteContact *rC;
	
	touchingTypes = TouchingTypes(routeUse, expansionMask, endPt);

        /* Make list of present and active contacts */
	presentContacts = NULL;
	presentContactLayers = NULL;
	for(rC =irRouteContacts; rC!=NULL; rC=rC->rc_next)
	{
	    if(TTMaskHasType(&touchingTypes,rC->rc_routeType.rt_tileType) &&
		    ListContainsP(rC->rc_rLayer1, activeLayers) &&
		    ListContainsP(rC->rc_rLayer2, activeLayers))
	    {
		LIST_ADD(rC, presentContacts);
		LIST_ADD(rC->rc_rLayer1, presentContactLayers);
		LIST_ADD(rC->rc_rLayer2, presentContactLayers);
	    }
	}

	if (DebugIsSet(irDebugID, irDebEndPts))
	{
	    TxPrintf("----- presentContacts:\n");
	    MZPrintRCListNames(presentContacts);

	    TxPrintf("----- presentContactLayers:\n");
	    MZPrintRLListNames(presentContactLayers);
	}

	    
	/* make list of present layers that are not constituents of contacts
	 * above.  If a contact is touching the endpt but one of 
	 * its constituent layers in not active, the other constituent
	 * layer is treated as a presentLayer.
	 */
	presentLayers = NULL;
	for(l=activeLayers; l!=NULL; l=LIST_TAIL(l))
	{
	    rL = (RouteLayer *) LIST_FIRST(l);
	    if((TTMaskHasType(&touchingTypes,rL->rl_routeType.rt_tileType) ||
		    LayerInTouchingContact(rL,touchingTypes)) &&
		    !ListContainsP(rL, presentContactLayers))
	    {
		LIST_ADD(rL,presentLayers);
	    }
	}

	if (DebugIsSet(irDebugID, irDebEndPts))
	{
	    TxPrintf("----- presentLayers:\n");
	    MZPrintRLListNames(presentLayers);
	}
    }

    /* return appropriate layer list. */
    {
	int numContacts, numLayers;

	numContacts = ListLength(presentContacts);
	numLayers = ListLength(presentLayers);

	if(numLayers == 0 && numContacts == 0)
	{
	    /* No Layers present at endpt, return list of all active layers */
	    ListDealloc(presentLayers);
	    ListDealloc(presentContacts);
	    ListDealloc(presentContactLayers);

	    return(activeLayers);
	}
	else if(numLayers == 1 && numContacts == 0)
	{
	    /* Exactly one layer is both active and present, return list
	     * containing only this layer.
	     */
	    ListDealloc(activeLayers);
	    ListDealloc(presentContacts);
	    ListDealloc(presentContactLayers);

	    return(presentLayers); 
	}
	else if(numLayers == 0 && numContacts == 1)
	{
	    /* Just one active contact under endpoint, 
	     * return layers connecting to that contact.
	     */
	    List *l;
	    RouteContact *rC;

	    rC = (RouteContact *) LIST_FIRST(presentContacts);
	    l = (List *) NULL;
	    LIST_ADD(rC->rc_rLayer1,l);
	    LIST_ADD(rC->rc_rLayer2,l);

	    ListDealloc(activeLayers);
	    ListDealloc(presentLayers);
	    ListDealloc(presentContacts);
	    ListDealloc(presentContactLayers);

	    return(l);
	}
	else
	{
	    /* Multiple nodes active and present, ask user which one
	     * he wants to route to.
	     */
	    char answer[100];
	    RouteLayer *rL;
	    RouteLayer *pickedRL;
	    RouteContact *rC;
	    RouteContact *pickedRC;

	    TxPrintf("Multiple nodes present at %s point:",
		    endPtName);
	    for(l=presentContacts; l!=NULL; l=LIST_TAIL(l))
	    {
		rC=(RouteContact *) LIST_FIRST(l);
		TxPrintf("  %s", 
			DBTypeLongNameTbl[rC->rc_routeType.rt_tileType]);
	    }
	    for(l=presentLayers; l!=NULL; l=LIST_TAIL(l))
	    {
		rL=(RouteLayer *) LIST_FIRST(l);
		TxPrintf("  %s", 
			DBTypeLongNameTbl[rL->rl_routeType.rt_tileType]);
	    }
	    TxPrintf("\n");

	    for(pickedRC=FALSE,l=presentContacts; l && !pickedRC; l=LIST_TAIL(l))
	    {
		rC = (RouteContact *) LIST_FIRST(l);
		if (!LIST_TAIL(l) && !presentLayers)
		{
		    /* last choice, so take it */
		    pickedRC = rC;
		}
		else
		{
		    /* ask user */
		    TxPrintf("Connect to %s? [yes] ",
			    DBTypeLongNameTbl[rC->rc_routeType.rt_tileType]);
		    if (TxGetLine(answer, sizeof answer) == NULL || 
			    answer[0] == '\0')
			(void) strcpy(answer,"yes");

		    if(Lookup(answer, actionNames) == 1)
		    {
			/* Yes */
			pickedRC = rC;
		    }
		}
	    }

	    if(pickedRC)
	    {
		List *l;

		l=NULL;
		LIST_ADD(rC->rc_rLayer1,l);
		LIST_ADD(rC->rc_rLayer2,l);
		ListDealloc(activeLayers);
		ListDealloc(presentLayers);
		ListDealloc(presentContacts);
		ListDealloc(presentContactLayers);

		return(l);
	    }

	    for(pickedRL=NULL,l=presentLayers; l && !pickedRL; l=LIST_TAIL(l))
	    {
		rL = (RouteLayer *) LIST_FIRST(l);
	        if(!LIST_TAIL(l))
		{
		    /* Last choice so choose it automatically */
		    pickedRL=rL;
		}
		else
		{
		    /* Ask user */
		    TxPrintf("Connect to %s? [yes] ",
			    DBTypeLongNameTbl[rL->rl_routeType.rt_tileType]);
		    if (TxGetLine(answer, sizeof answer) == NULL || 
			    answer[0] == '\0')
			(void) strcpy(answer,"yes");

		    if(Lookup(answer, actionNames) == 1)
		    {
			/* Yes */
			pickedRL = rL;
		    }
		}
	    }

	    if(pickedRL)
	    {
		l=NULL;
		LIST_ADD(rL,l);
		ListDealloc(activeLayers);
		ListDealloc(presentLayers);
		ListDealloc(presentContacts);
		ListDealloc(presentContactLayers);

		return(l);
	    }

	    /* User didn't pick anything, return null list */
	    {
		ListDealloc(activeLayers);
		ListDealloc(presentLayers);
		ListDealloc(presentContacts);
		ListDealloc(presentContactLayers);

		return(NULL);
	    }

	}
    }
}