      PROGRAM ICUTIL
C
C  This program is a supporting program for ICMATP. The distances of
C  a set of connection is computed and sorted according to distance. The
C  sorted set is transferred to a new diskfile.
C
C  Description of the input.
C
C  The data for the connections and the ic packages is identical with
C  the input for PCMATP. A new connectionfile is written to disk, as well
C  as a descriptive file indicating the absolute connections. The output
C  is numbered, making it easier to interpret connection data.
C  The descriptive file is called XYCOORD.DAT
C  The operation is controlled by three sets of parameters. The first set
C  consists of two factors, as multiplicators for the X and the Y distance.
C  This makes it possible to sort the connections on the basis of a criterion
C  of shortest distances first, or last, when the factors are negative.
C  To the distances a quantity can be added determined by two factors, used
C  as multiplicator for the coordinates nearest to the origin. This has the
C  effect that searching is started with connections nearest to the X or the
C  Y axis, provided that the numerical value of the factors is large enough.
C  One is advised to try a few different principles for the various planes.
C  My advise would be to start with the principle of the shortest connections
C  first, modulated by some influence of the nearness to the X or the Y axis.
C  One plane should have predominantly connections in the X direction, the
C  other in the Y direction. Examples are provided in the manual.
C
C Description of the variables:
C     ICPOS(I,J) (i,1) x coordinate pen 1
C                (i,2) y coordinate pen 2
C                (i,3) number of pins
C     VERB(I,J)  (i,1) ic number connection 1
C                (i,2) pennumber connection 1
C                (i,3) ic number connection 2
C                (i,4) pennumber connection 2
C     WEGLO(I,J) (i) first move from connection 1
C
      DIMENSION F(4),D(300)
      INTEGER ICPOS(60,3),VERB(600,4),WEGLO(600),L(602)
      COMMON /PLDATA/ICPOS,VERB,WEGLO
   1  FORMAT(' enter name file with package positions: ')
   2  FORMAT(' enter name new file with connections: ')
   3  FORMAT(3I4)
   4  FORMAT(4A4)
   5  FORMAT(6I4)
   6  FORMAT(6I4)
   7  FORMAT(2F9.2)
   8  format(' enter name file with connection data: ')
   9  FORMAT(' ENTER WEIGHT OF X AND Y DISTANCE AS .7,.4: ')
  10  FORMAT(2F5.2)
   11 FORMAT(' ENTER FACTORS FOR LOWEST X AND Y COORDINATES AS .6,.3:')
   12 FORMAT(' REVERSE DIRECTIONS IF F1*(X2-X1) .LT. F2*(Y2-Y1)'/
     A' ENTER F1 AND F2 AS .3,.4 (MAY BE ZERO): ')
   13 FORMAT(' NOT MORE THAN 80 PACKAGES ALLOWED, TERMINATED')
   14 FORMAT(' NOT MORE THAN 600 CONNECTIONS ALLOWED, TERMINATED')
   17 FORMAT(' IC:',I3,' X POS PEN 1:',I4,' Y POS:',I4,',',I4,' PENS')
   18 FORMAT(' CONN:',I3,' X1:',I3,' Y1:',I3,' X2:',I3,' Y2:',I3,
     1' CRIT:',G10.3)
   15 FORMAT(' Utility program for sorting connections'/
     1' Absolute coordinates are written into XYCOORD.DAT'/
     1' Select your option from the menu:'/
     a' (1) Sort by shortest connection'/
     b' (2) Sort by shortest connection in X direction'/
     c' (3) Sort by shortest connection in Y direction'/
     d' (4) Sort by starting point nearest to X axis'/
     e' (5) Sort by starting point nearest to Y axis'/
     f' (6) Sort by starting point nearest to origin'/
     g' (7) Make connections run from left to right'/
     h' (8) Make connections run from right to left'/
     i' (9) Make connections run from bottom to top'/
     j' (10) Make connections run from top to bottom'/
     k' (11) Any combination of 1-10 (refresh your geometry first)'/
     l' (12) Sort according to absolute sinus'/
     L' (13) Sort according to absolute cosinus'/
     L' (14) Stop'/
     m' Enter your selection: ')
 100  CONTINUE
      write(5,15)
      read(5,3) NOPTI
      IF(NOPTI.GE.15.OR.NOPTI.LE.0) GOTO 100
      if(NOPTI.EQ.14) STOP
      WRITE(5,1)
      READ(5,4) F
      CALL CPMFMT(F,NDATA)
      call open(8,F,NDATA)
      I=0
 110  I=I+1
      IF(I.GT.60) GOTO 1000
C
C READ IN THE POSITION DATA OF THE IC'S
C
      READ(8,3,END=121) ICPOS(I,1),ICPOS(I,2),ICPOS(I,3)
      IF(ICPOS(I,1).GE.1) GOTO 110
 121  NIC=I-1
      endfile 8
      write(5,8)
      read(5,4) F
      CALL CPMFMT(F,NDATA)
      call open(8,F,NDATA)
      I=0
 120  I=I+1
      IF(I.GT.600) GOTO 1010
C
C READ IN THE CONNECTION DATA
C
      READ(8,5,END=130) (VERB(I,J),J=1,4),WEGLO(I)
      GOTO 120
 130  NVERB=I-1
C
C CLOSE THE INPUTFILE
C
      ENDFILE 8
      FACT1=0.
      FACT2=0.
      FACT3=0.
      FACT4=0.
      F1=0.
      F2=0.
      F3=1.
      F4=0.
      F5=0.
      GOTO(300,310,320,330,340,350,360,370,380,390,400,410,430),NOPTI

C
C COMPUTE THE COORDINATES AND STORE THE DISTANCE IN D
C
  300 FACT1=1.
      FACT2=1.
      GOTO 420
  310 FACT1=1.
      FACT4=0.
      GOTO 420
  320 FACT2=1.
      GOTO 420
  330 FACT3=1.
      GOTO 420
  340 FACT4=1.
      GOTO 420
  350 FACT3=1.
      FACT4=1.
      GOTO 420
  360 F1=1.
      GOTO 420
  370 F1=-1.
      GOTO 420
  380 F2=1.
      GOTO 420
  390 F2=-1
      GOTO 420
  410 F4=1.
      F3=0.
      FACT1=1.
      FACT2=1.
      GOTO 420
  430 F5=1.
      F3=0.
      FACT1=1.
      FACT2=1.
      GOTO 420
 400  WRITE(5,9)
      READ(5,10) FACT1,FACT2
      WRITE(5,11)
      READ(5,10) FACT3,FACT4
      WRITE(5,12)
      READ(5,10) F1,F2
 420  DO 150 I=1,NVERB
      CALL CPEN(LX1,LY1,VERB(I,1),VERB(I,2),IH1,IH2)
      CALL CPEN(LX2,LY2,VERB(I,3),VERB(I,4),IH1,IH2)
      D(I)=MIN0(LY1,LY2)*FACT4+MIN0(LX1,LX2)*FACT3
      D(I)=FACT1*IABS(LX1-LX2)+FACT2*IABS(LY1-LY2)+D(I)
C
C COMPUTING THE CITYBLOCK (CO)SINUS OF THE ANGLE OF THE CONNECTION
C
      D(I)=D(I)*F3+(F4*IABS(LY1-LY2)+F5*IABS(LX1-LX2))/
     A(IABS(LY1-LY2)+IABS(LX1-LX2))
C
C THE SIGN OF TEMP DETERMINES THE REVERSAL OF DIRECTION
C F1 VIZ. F2 POSITIVE MEANS LEFT TO RIGHT OR BOTTOM TO TOP
C
      TEMP=F1*(LX2-LX1)+F2*(LY2-LY1)
      IF(TEMP.GE.0) GOTO 150
      IH=VERB(I,1)
      VERB(I,1)=VERB(I,3)
      VERB(I,3)=IH
      IH=VERB(I,2)
      VERB(I,2)=VERB(I,4)
      VERB(I,4)=IH
  150 CONTINUE
      CALL SORT(D(1),L(1),NVERB)
      WRITE(5,2)
      READ(5,4) F
      CALL CPMFMT(F,NDATA)
      call open(8,F,NDATA)
      CALL OPEN(7,'XYCOORD DAT ',1)
      K=1
      DO 170 J=1,NVERB
      K=L(K)
      I=K-1
      CALL CPEN(LX1,LY1,VERB(I,1),VERB(I,2),IMX,IMY)
      CALL CPEN(LX2,LY2,VERB(I,3),VERB(I,4),IMX,IMY)
      WRITE(7,18) J,LX1,LY1,LX2,LY2,D(I)
  170 WRITE(8,6) (VERB(I,M),M=1,4),WEGLO(I),J
      PAUSE KLAAR?
      ENDFILE 8
      ENDFILE 7
      GOTO 100
 1000 WRITE(5,13)
      STOP
 1010 WRITE(5,14)
      STOP
      END
      SUBROUTINE CPEN(IX,IY,NR,NPEN,IMX,IMY)
C
C THIS ROUTINE GIVES THE GRID COORDINATES OF AN IC PEN
C AS A FUNCTION OF THE IC NUMBER AND OF THE PENNUMBER.
C  PARAMETERS:        NR : NUMBER IC
C                   NPEN : NUMBER OF THE PEN
C  OUTPUT:            IX : X-GRIDCOORDINATE PEN
C                     IY : Y-GRIDCOORDINATE PEN
C                IMX,IMY : VECTOR INDICATING DIRECTION OF DRAWING
C
      LOGICAL ROW2
      INTEGER ICPOS(60,3)
      COMMON /PLDATA/ICPOS
C
C IF AN IC NUMBER IS NEGATIVE, IT IS INTERPRETED AS A DIRECT GRID
C COORDINATE OF THE X DIRECTION. NPEN IS THEN INTERPRETED AS THE Y
C COORDINATE. IMX AND IMY GIVE THE DIRECTION VECTOR OF THE ROW. THE
C DIRECTION VECTOR IS 0,0 IF THE POINT DOES NOT BELONG TO A ROW
C
      ROW2=.FALSE.
      IMX=0
      IMY=0
      IF(NR.LE.0) GOTO 10
      IX=ICPOS(NR,1)
      IY=ICPOS(NR,2)
      IP=ICPOS(NR,3)
C
C CHECK FOR HORIZONTAL OR VERTICAL TYPE
C
      CALL TYPE(IMX,IMY,IY)
      IY=IABS(IY)
      IPP=IABS(IP)
C
C CHECK FOR POSITION ON FIRST OR ON POSSIBLE SECOND ROW OF PENS
C
      IF(IP.GT.0.AND.NPEN.GT.IP/2) ROW2=.TRUE.
C
C CHECK FOR PENNUMBER LARGER THAN NUMBER OF PENS
C
      IF(NPEN.GT.IPP) GOTO 20
      IFAC=0
      IF(ROW2) IFAC=6
      IF(IP.GE.24) IFAC=IFAC*2
      IF(ROW2) NPEN=IPP-NPEN+1
      IX=IX+IMY*IFAC-(NPEN-1)*2*IMX
      IY=IY-IMX*IFAC-(NPEN-1)*2*IMY
      IF(IX.GT.126.OR.IY.GT.80) GOTO 50
      IF(IX.LE.0.OR.IY.LE.0) GOTO 50
      RETURN
   10 IX=-NR
      IY=NPEN
      IF(IX.GT.126.OR.IY.GT.80) GOTO 50
      IF(IX.LE.0.OR.IY.LE.0) GOTO 50
      RETURN
   30 WRITE(5,1)
    1 FORMAT(' PEN-NUMBER',I3,' OF IC',I3,' TOO LARGE, TERMINATED')
   20 WRITE(5,1) NR,NPEN
      STOP
   40 FORMAT(' GRIDCOORDINATES TOO LARGE OR TOO SMALL: X,Y=',I4,',',I4/
     1' IC NUMBER=',I4,' PEN:',I4)
   50 WRITE(5,40) IX,IY,NR,NPEN
      STOP
      END
      SUBROUTINE TYPE(IX,IY,ISEL)
C
C THIS SUBROUTINE SELECTS BETWEEN A HORIZONTAL OR VERTICAL
C POSITIONED IC LAY OUT, USING THE SIGN OF ISEL, FOR ABSOLUTE
C POINTS THE VECTOR IS SET TO ZERO
C
      IX=0
      IY=0
      IF(IX.LT.0) RETURN
      IF(ISEL.LT.0) GOTO 20
      IY=1
      RETURN
   20 IX=1
      RETURN
      END
      SUBROUTINE SORT(A,L,N)
      DIMENSION A(1),L(1)
      J=N-1
      DO 10 I=2,J
      L(I)=-(I+2)
   10 CONTINUE
      L(N+1)=0
      L(N)=0
      L(1)=2
      L(N+2)=3
  110 IS=1
      IT=N+2
      IP=L(IS)
      IQ=L(IT)
      IF(IQ.EQ.0) RETURN
  160 IF(A(IP-1).GT.A(IQ-1)) GOTO 240
  170 L(IS)=ISIGN(IP,L(IS))
      IS=IP
      IP=L(IP)
      IF(IP) 200,200,160
  200 L(IS)=IQ
      IS=IT
  210 IT=IQ
      IQ=L(IQ)
      IF(IQ) 300,300,210
  240 L(IS)=ISIGN(IQ,L(IS))
      IS=IQ
      IQ=L(IQ)
      IF(IQ) 270,270,160
  270 L(IS)=IP
      IS=IT
  280 IT=IP
      IP=L(IP)
      IF(IP) 300,300,280
  300 IP=-IP
      IQ=-IQ
      IF(IQ) 160,320,160
  320 L(IS)=ISIGN(IP,L(IS))
      L(IT)=0
      GOTO 110
      END
~#fo"Ö#+"V#)"^#i"`#*^#"b#*`#"Ü#*b#"Þ#!Ø#"à#!Ú#"â#Ü#˜#!–#Íú1*V#)"^#É"`#*^#y"b#*`#"ä