C
C  THIS IS A COLLECTION OF ALL LAYOUT SUBROUTINES
C  FOR AUXILIARY LAYOUT (LOADED IN LAYOUT.OLB)
C  AND STANDALONE LAYOUT (FROM LAYOUT_MAIN.FOR).
C
     	SUBROUTINE LAYOUT_AUX( ICODE, XORG, YORG, FNAME )
C
C  PROGRAMMER:	RONG-SHENG ZHOU
C		ALFRED UNIVERSITY, MARCH-1987
C
C	THIS ROUTINE IMPLEMENTS AN INTERACTIVE TWO-DIMENSIONAL
C	LAYOUT ON A TEK-4100 SEGMENT-GRAPHICS TERMINAL.
C
C	ICODE ---- FUNCTION CODE:
C
C		 0 = ENTER TO PLOT "FNAME" AND QUIT.
C		 2 = ENTER AS A AUXILIARY LAYOUT SESSION.
C		     LAYOUT IS SAVED IN "FNAME" UPON EXIT.
C
C	XORG,YORG ---- LAYOUT ORIGIN(X,Y).
C	FNAME -------- FILE TO STORE AND/OR TO PLOT A LAYOUT.
C
     	CHARACTER FNAME*(*)
     	IF( ICODE.EQ.2 ) CALL IFANSI_LEDS(2)
     	ID = LAYOUT_START(ICODE,XORG,YORG,FNAME)
     	IF(ID.EQ.0) CALL LAYOUT_DIALOG
     	IF( ICODE.EQ.2 ) CALL IFANSI_LEDS(0)
     	RETURN
     	END

    	SUBROUTINE LAYOUT_CODE( PF )
C
C  PROGRAMMER: R.S. ZHOU	MAY-1986
C
C	THIS ROUTINE CHANGES THE PICKED LAYOUT SEGMENT "CODE"S
C	TO THAT DESIGNATED BY "PF":
C
C	PF = FILL PATTERN FOR BAR,ARC,OBLIQUE,POLYGON,LINE ENTRIES.
C	PF = ARROWHEAD TYPE FOR ARROWHEAD ENTRIES.
C	PF = SYMBOL NUMBER FOR MARKER SYMBOL ENTRIES.
C
C	FILL-GROUP ENTRIES AND TEXT ENTRIES ARE IGNORED.
C
    	INCLUDE		'LAYOUT.CBS'
     	DIMENSION	IR(9)
     	DATA		IR / 7, 7, 9, 7, 7, 7, 5, 0, 7 /

     	PF1 = PF
    	DO  222 I = 1, NLG
    	    IS = NLS(I)
     	    SKIP = .FALSE.
     	    DO  111 J=1, NLC
     		IF(NSN(J).NE.IS.OR.NID(J).EQ.8.OR.NFL(J).GT.0) GOTO 111
     		IT = IR(NID(J))
     		IF(P00(IT,J).EQ.PF1) GOTO 111
     		P00(IT,J) = PF1
     		CALL IFSEG_OPEN(-IS)
     		CALL IFSEG_PIVOT(P00(1,J),P00(2,J))
     		CALL IFSEG_OPEN(IS)
     		CALL LAYOUT_DRAW(-J,1)
     		CALL IFSEG_OPEN(0)
     		IF(BLINK.GT.0) CALL IFSEG_BLINK( IS )
     		GOTO 222
111	    CONTINUE
222	CONTINUE
     	CALL IFINIT_DUMP
     	RETURN
    	END

    	SUBROUTINE LAYOUT_COLOR( PNN )
C
C  PROGRAMMER: R.S. ZHOU	MAY-1986
C
C	THIS ROUTINE CHANGES THE COLORS OF PICKED LAYOUT SEGMENTS
C	TO THAT DESIGNATED BY "PNN".
C
    	INCLUDE 'LAYOUT.CBS'
     	LOGICAL SKIP

    	IPN = PNN
     	IF( IPN.LT.1 .OR. IPN.GT.8 ) RETURN

    	DO  222 I = 1, NLG
    	    IS = NLS(I)
     	    SKIP = .FALSE.
     	    DO 111 J=1, NLC
     		IF( NSN(J).NE.IS ) GOTO 111
     		IF( IPN.EQ.PEN(J) .AND. NFL(J).EQ.0 ) GOTO 111
     		PEN(J) = IPN
     		IF( NFL(J).EQ.0 ) THEN
     		    CALL IFSEG_OPEN(-IS)
     		    CALL IFSEG_PIVOT(P00(1,J),P00(2,J))
     		    CALL IFSEG_OPEN( IS)
     		    CALL LAYOUT_DRAW( -J, 1 )
     		    CALL IFSEG_OPEN(0)
     		    IF(BLINK.GT.0) CALL IFSEG_BLINK(IS)
     		    GOTO 222
     		ELSE
     		    IF( .NOT.SKIP ) THEN
     			CALL IFSEG_OPEN(-IS)
     			CALL IFSEG_PIVOT(P00(1,J),P00(2,J))
     			CALL IFSEG_OPEN(IS)
     			CALL IFFILL( NFP(NFL(J)) )
     			SKIP = .TRUE.
     		    ENDIF
     		    CALL LAYOUT_DRAW( -J, 0 )
     		ENDIF
111	    CONTINUE
     	    IF( SKIP ) THEN
     		CALL IFFILL( 0 )
     		CALL IFSEG_OPEN( 0 )
     		IF( BLINK.GT.0 ) CALL IFSEG_BLINK(IS)
     	    ENDIF
222	CONTINUE

     	CALL IFINIT_DUMP

     	RETURN
    	END

     	FUNCTION LAYOUT_COM( IK, TEXT )
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY,  JUN-1986
C
C	THIS ROUTINE IMPLEMENTS SOME LAYOUT COMMANDS.
C
    	INCLUDE		'LAYOUT.CBS'
    	CHARACTER	KEY*3, TEXT*(*)

     	IOP = IK	!TERMINATOR CHARACTER
C
C  ENABLE GIN...
C
    	KEY = TEXT(1:3)
     	IF( KEY.EQ.'GIN' .OR. KEY.EQ.'F4' ) THEN
     	    IF( CRT.NE.2 ) GOTO 555
     	    CALL LAYOUT_GIN( TEXT )
C
C  PLOT A NEW LAYOUT ...
C
     	ELSE IF( KEY.EQ.'PLO' ) THEN
     	    IF( MODE.EQ.2 ) GOTO 666
     	    CALL LAYOUT_PLOT( TEXT )
C
C  OVERLAY ANOTHER PLOT FILE...
C
     	ELSE IF( KEY.EQ.'OVE' .OR. KEY.EQ.'INC' ) THEN
     	    IF( CRT.EQ.1 ) GOTO 555
     	    CALL LAYOUT_OVERLAY( TEXT )
C
C  PICK ENTRIES...
C
     	ELSE IF( KEY.EQ.'PIC' .OR. KEY.EQ.'SEL' ) THEN
     	    IF( CRT.NE.2 ) GOTO 555
     	    IF( NLC.EQ.0 ) GOTO 777
     	    CALL LAYOUT_PICK( TEXT )
C
C  REFRESH THE SCREEN...
C
     	ELSE IF( KEY.EQ.'REF' .OR. KEY.EQ.'F3' ) THEN
     	    IF( CRT.NE.2 ) GOTO 555
     	    CALL IFINIT_FRESH
C
C  RELEASE THE PICKED ENTRIES...
C
     	ELSE IF( KEY.EQ.'REL' .OR. KEY.EQ.'F2' ) THEN
     	    IF( CRT.NE.2 ) GOTO 555
     	    IF( NLG.EQ.0 ) GOTO 444
     	    CALL LAYOUT_SET_BLINK( 0 )
C
C  SAVE THE PLOT INTO A FILE...
C
     	ELSE IF( KEY.EQ.'SAV' .OR. KEY.EQ.'FIL'
     + .OR. (IOP.EQ.27.AND.KEY.EQ.'OR') ) THEN
     	    IF( CRT.EQ.1 ) GOTO 555
     	    IF( NLC.EQ.0 ) GOTO 777
     	    CALL LAYOUT_SAVE( TEXT )
C
C  CLEAR THE SCREEN...
C
     	ELSE IF( KEY.EQ.'CLE' ) THEN
     	    IF( CRT.EQ.1 ) GOTO 555
     	    IF( MODE.EQ.2 ) GOTO 666
     	    CALL IFINIT_PLOT( XORG, YORG, LOFILE )
     	CALL IFINIT_FKEYS( 'VISION', 'RELEASE', 'REFRESH',
     + 'GIN-FLIP', 'MOVE', 'ROTATE', 'ZOOM', 'COLOR' )
    	    CALL LAYOUT_INIT
C
C  MAKE MULTIPLE COPIES OF THE PICKED ENTRIES...
C
     	ELSE IF( KEY.EQ.'COP' ) THEN
     	    IF( CRT.NE.2 ) GOTO 555
     	    IF( NLG.EQ.0 ) GOTO 444
     	    CALL LAYOUT_COPY( TEXT )
C
C  DELETE ENTRIES...
C
     	ELSE IF( KEY.EQ.'DEL' ) THEN
     	    IF( CRT.NE.2 ) GOTO 555
     	    IF( NLG.EQ.0 ) GOTO 444
     	    CALL LAYOUT_DELETE
C
C  MAKE A HARDCOPY...
C
     	ELSE IF( KEY.EQ.'HAR' ) THEN
     	    IF( CRT.EQ.1 ) GOTO 555
     	    IF( NLC.EQ.0 ) GOTO 777
     	    CALL IFCOPY( HPAR(1), PLOTID )
     	    CALL IFINIT_LINE( -3 )
     	ELSE IF( IOP.EQ.27 .AND. KEY.EQ.'OP' ) THEN
     	    IF( CRT.EQ.1 ) GOTO 555
     	    IF( NLC.EQ.0 ) GOTO 777
     	    WRITE(6,1234)
 1234	    FORMAT(' Working...',$)
     	    CALL IFFEED( HPAR(1), PLOTID, ' ' )
C
C  SET LAYOUT ATTRIBUTES...
C
     	ELSE IF( KEY.EQ.'SET' ) THEN
     	    IF( CRT.EQ.1 ) GOTO 555
     	    CALL LAYOUT_SET( TEXT )
C
C  SPAWN A SYSTEM SUBPROCESS...
C
     	ELSE IF( KEY.EQ.'DCL' .OR. (IOP.EQ.27.AND.KEY.EQ.'OS') ) THEN
     	    CALL IFMISC_SPAWN( TEXT, 0 )
     	    CALL IFINIT_LINE( -3 )
C
C  NOT IN THE COMMAND LIST...
C
     	ELSE
     	    IF( IOP.EQ.27 ) THEN
     		CALL IFMISC_OOPS( 5, 0, 0, ' ' )
     		GOTO 999
     	    ELSE
     		GOTO 666
     	    ENDIF
     	ENDIF
     	GOTO 999

444	CALL IFMISC_OOPS( 16, 0, 0, ' ' )	!NO PICKED ENTRIES YET
     	GOTO 999
555	CALL IFMISC_OOPS( 17, 0, 0, TEXT )	!INVALID TERMINAL TYPE
     	GOTO 999
777	CALL IFMISC_OOPS( 15, 0, 0, ' ' )	!NOTHING TO WORK ON
999	LAYOUT_COM = 1
     	RETURN
666	LAYOUT_COM = 0			!NOT IN THE LIST
     	RETURN
    	END

    	SUBROUTINE LAYOUT_PLOT( TEXT )
C
C  PROGRAMMER: R.S. ZHOU		MAY-1986
C
C  	THIS ROUTINE DRAWS A LAYOUT.
C
    	INCLUDE		'LAYOUT.CBS'
    	CHARACTER	SNAME*32, TEXT*(*)
     	LOGICAL		YES

    	NCH = IFMISC_NEXT( TEXT )
    	IF( NCH.GT.0 ) GOTO 111
     	WRITE(6,100)
100	FORMAT(' Filename [.DES] ',$)
    	READ(5,101,ERR=999,END=999) SNAME
101	FORMAT(A)
    	IF( SNAME.EQ.' ' ) GOTO 999
    	GOTO 222
111	IF( NCH.GT.32 ) THEN
     	    CALL IFMISC_OOPS( 7, 0, 0, TEXT )
     	    GOTO 999
     	ENDIF
     	SNAME = TEXT
222	CALL IFMISC_DEFEXT( SNAME, '.DES' )
C
C  CHECK IF THE FILE EXIST...
C
     	INQUIRE( FILE=SNAME, EXIST=YES )
     	IF( .NOT.YES ) THEN
     	    CALL IFMISC_OOPS( 1, 0, 0, SNAME )
     	    GOTO 999
     	ENDIF
C
C  FILE EXISTS. PLOT IT...
C
     	LOFILE = SNAME
     	CALL LAYOUT_INIT
        CALL IFINIT_LINE( 3 )
     	CALL IFINIT_PLOT( XORG, YORG, LOFILE )
     	CALL IFINIT_FKEYS( 'VISION', 'RELEASE', 'REFRESH',
     + 'GIN-FLIP', 'MOVE', 'ROTATE', 'ZOOM', 'COLOR' )
    	NCH = LAYOUT_IO( 1, SNAME )
     	IF( NCH.GT.0 ) THEN
     	    IF( CRT.EQ.1 ) CALL IFCOPY( HPAR(1), PLOTID )
     	ELSE
     	    IF( NCH.EQ.0 ) CALL IFMISC_OOPS( 1, 0, 0, SNAME )
     	    IF( NCH.LT.0 ) CALL IFMISC_OOPS( 18, 0, 0, SNAME )
     	ENDIF

999	RETURN
    	END

    	SUBROUTINE LAYOUT_OVERLAY( TEXT )
C
C  PROGRAMMER: R.S. ZHOU		MAY-1986
C
C  	THIS ROUTINE OVERLAYS A LAYOUT PLOT.
C
    	INCLUDE		'LAYOUT.CBS'
    	CHARACTER	SNAME*32, TEXT*(*)
     	LOGICAL		YES

    	NCH = IFMISC_NEXT( TEXT )
    	IF( NCH.GT.0 ) GOTO 111
     	WRITE(6,100)
100	FORMAT(' Filename [.DES] ',$)
    	READ(5,101,ERR=999,END=999) SNAME
101	FORMAT(A)
     	IF( SNAME.EQ.' ' ) GOTO 999
    	GOTO 222

111	IF( NCH.GT.32 ) THEN
     	    CALL IFMISC_OOPS( 7, 0, 0, TEXT )
     	    GOTO 999
     	ENDIF
     	SNAME = TEXT
C
C  CHECK IF THE FILE EXIST...
C
222	CALL IFMISC_DEFEXT( SNAME, '.DES' )
     	INQUIRE( FILE=SNAME, EXIST=YES )
     	IF( .NOT.YES ) THEN
     	    CALL IFMISC_OOPS( 1, 0, 0, SNAME )
     	    GOTO 999
     	ENDIF
C
C  FILE EXISTS. PLOT IT...
C
     	IF( LOFILE.EQ.' ' ) LOFILE = SNAME
    	NCH = LAYOUT_IO( 1, SNAME )
     	IF( NCH.GT.0 ) THEN
     	    IF( CRT.EQ.1 ) CALL IFCOPY( HPAR(1), PLOTID )
     	ELSE
     	    IF( NCH.EQ.0 ) CALL IFMISC_OOPS( 1, 0, 0, SNAME )
     	    IF( NCH.LT.0 ) CALL IFMISC_OOPS( 18, 0, 0, SNAME )
     	ENDIF

999	RETURN
    	END

    	SUBROUTINE LAYOUT_SAVE( TEXT )
C
C   PROGRAMMER: R.S. ZHOU	MAY-1986
C
C  	THIS ROUTINE SAVES THE CURRENT LAYOUT IN A FILE.
C
    	INCLUDE 'LAYOUT.CBS'
    	CHARACTER PNAME*32, SNAME*32, TEXT*(*)

    	NCH = IFMISC_NEXT( TEXT )
    	IF( NCH.GT.32 ) THEN
     	    CALL IFMISC_OOPS( 7, 0, 0, TEXT )
     	    GOTO 999
     	ENDIF
     	SNAME = TEXT
     	IF( NCH.GT.0 ) GOTO 111
C
C  GET THE FILENAME...
C
    	SNAME = 'JUNK.DES'
    	IF( LOFILE.NE.' ' ) SNAME = LOFILE
    	NCH = IFMISC_LENG( SNAME )
    	WRITE(6,1000) SNAME(1:NCH)
1000	FORMAT(' Save layout in [',A,'] ',$)
    	READ(5,1001,ERR=999,END=999) PNAME
1001	FORMAT(A)
    	IF( PNAME.NE.' ' ) SNAME = PNAME

111	NCH = LAYOUT_IO( 0, SNAME )
    	IF( LOFILE.EQ.' ' ) LOFILE = SNAME

999	RETURN
    	END

     	SUBROUTINE LAYOUT_COPY( TEXT )
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY, MARCH-1987
C
C	THIS ROUTINE MAKES COPIES OF THE PICKED LAYOUT SEGMENTS.
C
C	COMMAND SYNTAX: COPY #, px, py, Angle1, Angle2
C
C	Make # copies of the picked entries. #=0 implies 1 copy.
C	If Angle1 = 0.0, px, py are the offsets inches in x, y
C	between copies, or else copies are generated in circular
C	manner with incremental Angle1 around the pivot (px,py) and
C	Angle2 specifies the entry rotation about its own position.
C
C	Example: COPY 3 1 1 90 90<CR>
C
     	INCLUDE		'LAYOUT.CBS'
     	CHARACTER	TEXT*(*)
     	DIMENSION	RP(5), KP(5)

     	IF( NLC.EQ.MNN ) GOTO 999
C
C  DECODE COMMAND PARAMETERS...
C
     	NCH = IFMISC_NEXT( TEXT )
     	IF( NCH.EQ.0 ) THEN
     	    CALL IFMISC_OOPS( 4, 0, 0, ' ' )
     	    RETURN
     	ELSE
    	    NCH = IFMISC_DECODE( TEXT, NCH, 5, RP, KP )
    	    IF( NCH.EQ.0 ) THEN
     		CALL IFMISC_OOPS( 3, 0, 0, TEXT )
     		RETURN
     	    ENDIF
     	ENDIF
C
C  START COPYING...
C
     	NSP = RP(1)
     	IF( NSP.LT.1 ) NSP = 1
     	DO  IC = 1, NSP			!LOOP FOR NUMBER OF COPIES

     	IF( NLC.EQ.MNN ) GOTO 999
C
C  GET THE OFFSET FOR THIS COPY...
C
    	IF( RP(4).EQ.0.0 ) THEN
     	    XOFF = RP(2)*IC
     	    YOFF = RP(3)*IC
     	    ANG0 = 0.0
     	    ANG1 = 0.0
     	ELSE
    	    XOFF = RP(2) + XORS
    	    YOFF = RP(3) + YORS
     	    ANG0 = IC*AMOD( RP(4), 360.0 )
     	    ANG1 = IC*AMOD( RP(5), 360.0 )
        ENDIF
C
C  GENERATE THE COPY...
C
     	CALL LAYOUT_COPY_ONE( XOFF, YOFF, ANG0, ANG1 )

    	ENDDO
     	RETURN
999	CALL IFMISC_OOPS( 28, 0, 0, ' ' ) 	!MAX ENTRY COUNT HIT
     	RETURN
     	END

    	SUBROUTINE LAYOUT_COPY_ONE( XADD, YADD, ANGLE0, ANGLE1 )
C
C  PROGRAMMER: R.S. ZHOU		JUNE-1986
C
C	THIS ROUTINE MAKES ONE COPY OF THE PICKED SEGMENTS
C	WITH AN OFFSET TO XADD,YADD.
C
    	INCLUDE		'LAYOUT.CBS'
     	LOGICAL		FIRST
     	DIMENSION	IR(9)
     	DATA		IR/ 6, 6, 6, 6, 5, 6, 4, 5, 6 /

    	XAD = XADD
    	YAD = YADD
     	AN0 = ANGLE0
     	AN1 = ANGLE1

    	DO  555 I= 1, NLG
    	    IF( NLC.EQ.MNN ) GOTO 111
    	    IS = NLS(I)
     	    FIRST = .TRUE.
     	    DO  444 J= 1, NLC
     		IF( NSN(J).NE.IS ) GOTO 444
     		NLC = NLC + 1
     		IDD = NID(J)
     		NID(NLC) = IDD
     		PEN(NLC) = PEN(J)
     		IF( AN0.EQ.0.0 ) THEN
     		    XPS = P00(1,J) + XAD
     		    YPS = P00(2,J) + YAD
     		ELSE
     		    XPS = P00(1,J)
     		    YPS = P00(2,J)
     		    ANGLE = 0.0
     		    DIST = SQRT((XPS-XAD)**2+(YPS-YAD)**2)
     		    IF( DIST.GT.0.0 ) THEN
     			ANGLE = ASIND((YPS-YAD)/DIST)
     			IF(XPS.LT.XAD) ANGLE = 180.0-ANGLE
     		    ENDIF
     		    ANGLE = AMOD(ANGLE+AN0,360.0)
     		    XPS = XAD + DIST*COSD(ANGLE)
     		    YPS = YAD + DIST*SIND(ANGLE)
     		ENDIF
     		P00(1,NLC) = XPS
     		P00(2,NLC) = YPS
     		DO  K= 3, MNS
     		    P00(K,NLC) = P00(K,J)
     		ENDDO
     		IF( AN1.NE.0.0 ) THEN
     		    K = IR(IDD)
     		    P00(K,NLC) = P00(K,NLC) + AN1
     		ENDIF
     		IF( IDD.EQ.8 ) THEN			!TEXT ENTRY
     		    IF( NTX.EQ.MNT ) GOTO 222
     		    NTX = NTX + 1
     		    LDT(NTX) = NLC
     		    IDD = LAYOUT_LDT(J)
     		    TXT(NTX) = TXT(IDD)
     		ENDIF
     		IF( IDD.EQ.9 .AND. P00(3,NLC).NE.2.0 ) THEN	!LINE ENTRY
     		    IF( NCU.EQ.MNC ) GOTO 333
     		    NCU = NCU + 1
     		    LID(NCU) = NLC
     		    IDD = LAYOUT_LID(J)
     		    DO  K= 1, MNP
     			XSC(K,NCU) = XSC(K,IDD)
     			YSC(K,NCU) = YSC(K,IDD)
     		    ENDDO
     		ENDIF
     		IF( NFL(J).EQ.0 ) THEN
     		    NFL(NLC) = 0
     		    CALL LAYOUT_DRAW( NLC, 1 )
     		    GOTO 555
     		ELSE IF( NFG.EQ.MNF ) THEN
     		    NFL(NLC) = 0
     		    CALL LAYOUT_DRAW( NLC, 1 )
     		    CALL IFMISC_OOPS( 27, 0, 0, ' ' )
     		    GOTO 555
     		ELSE
     		    IF( FIRST ) THEN
     			FIRST = .FALSE.
     			NFG = NFG + 1
     			KSG = KSG + 1
     			CALL IFSEG_PIVOT( XPS, YPS )
     			CALL IFSEG_OPEN( KSG )
     			NFP(NFG) = NFP(NFL(J))
     			CALL IFFILL( NFG )
     		    ENDIF
     		    CALL LAYOUT_DRAW( -NLC, 0 )
     		    NFL(NLC) = NFG
     		    NSN(NLC) = KSG
     		ENDIF
444	    CONTINUE
     	    IF( .NOT.FIRST ) THEN
     		CALL IFFILL( 0 )
     		CALL IFSEG_OPEN( 0 )
     	    ENDIF
555	CONTINUE
     	GOTO 999
111	CALL IFMISC_OOPS( 28, 0, 0, ' ' )	!MAX ENTRY COUNT HIT
     	GOTO 999
222	NLC = NLC - 1
     	CALL IFMISC_OOPS( 25, 0, 0, ' ' )	!MAX TEXT ENTRY COUNT HIT
     	GOTO 999
333	NLC = NLC - 1
     	CALL IFMISC_OOPS( 26, 0, 0, ' ' )	!MAX LINE ENTRY COUNT HIT

999	CALL IFINIT_DUMP
     	IF( KSG.GT.2*MNN ) CALL LAYOUT_PACK_SEG	!REPACK SEGMENT NUMBERS
     	RETURN
    	END

    	SUBROUTINE LAYOUT_DELETE
C
C  PROGRAMMER: R.S. ZHOU		JUNE-1986
C
C	THIS ROUTINE DELETES THE PICKED LAYOUT SEGMENTS.
C
    	INCLUDE 'LAYOUT.CBS'
     	LOGICAL SKIP
C
C  DELETE THE SEGMENTS...
C
     	K = 0
     	DO  I=1, NLG
     	    IS = NLS(I)
     	    SKIP = .FALSE.
     	    DO  J=1, NLC
     		IF( NSN(J).EQ.IS ) THEN
     		    IF( .NOT.SKIP ) CALL IFSEG_OPEN(-IS)
     		    IF( NFL(J).GT.0 ) SKIP = .TRUE.
     		    NSN(J) = 0
     		    K = K + 1
     		ENDIF
     	    ENDDO
     	ENDDO
     	CALL IFINIT_DUMP
C
C  REPACK THE ENTRIES...
C
     	NLG = 0
     	IF( K.EQ.NLC ) THEN	!NOTHING LEFT ON SCREEN
     	    NLC = 0
     	    NFG = 0
     	    NCU = 0
     	    NTX = 0
     	    KSG = NSG
     	ELSE
     	    CALL LAYOUT_PACK_ENTRY( 0 )
     	ENDIF

     	RETURN
    	END

    	SUBROUTINE LAYOUT_DELETE_SEG( ISG )
C
C  PROGRAMMER: R.S. ZHOU		JUNE-1986
C
C	THIS ROUTINE DELETES A LAYOUT SEGMENT OF NUMBER "ISG".
C
    	INCLUDE 'LAYOUT.CBS'

     	ISS = ISG
C
C  IF "ISS" IS IN THE PICKED SEGMENT LIST, IGNORE IT...
C
    	DO I = 1, NLG
    	   IF( NLS(I).EQ.ISS ) RETURN
    	ENDDO
C
C  DELETE THE SEGMENT...
C
     	CALL IFSEG_OPEN( -ISS )
     	CALL IFINIT_DUMP
C
C  REPACK THE ENTRIES...
C
     	CALL LAYOUT_PACK_ENTRY( ISS )

     	RETURN
    	END

    	SUBROUTINE LAYOUT_FILL( FP )
C
C  PROGRAMMER: R.S. ZHOU	MAY-1986
C
C	THIS ROUTINE FILLS THE PICKED LAYOUT ENTRIES
C	WITH A HATCHING PATTERN --- FP.
C
    	INCLUDE 'LAYOUT.CBS'
     	LOGICAL SKIP, FIRST

    	IPN = FP
     	IF( IPN.NE.0 ) GOTO 111
C
C  RELEASE THE FILL GROUP...
C
     	K = NLG
     	FIRST = .TRUE.
     	DO I=1, NLG
     	    SKIP = .TRUE.
     	    IS = NLS(I)
     	    DO J=1, NLC
     		IF( NFL(J).GT.0 .AND. NSN(J).EQ.IS ) THEN
     		    NFL(J) = 0
     	    	    CALL LAYOUT_DRAW( J, 1 )
    	    	    IF(BLINK.GT.0) CALL IFSEG_BLINK(KSG)
     		    K = K + 1
     		    NLS(K) = KSG
     		    FIRST = .FALSE.
     		    SKIP = .FALSE.
     		ENDIF
     	    ENDDO
     	    IF( .NOT.SKIP ) THEN
     		NLS(I) = 0
     		CALL IFSEG_OPEN( -IS )
     	    ENDIF
     	ENDDO
     	NLG = K
     	IF( .NOT.FIRST ) CALL LAYOUT_PACK_NLG   !RESORT PICKED SEGMENT NUMBERS
     	GOTO 222
C
C  ADD A FILL GROUP...
C
111	K = 0
     	FIRST = .TRUE.
     	DO 555 I = 1, NLG
     	    SKIP = .FALSE.
    	    IS = NLS(I)
     	    DO 444 J = 1, NLC
     		IF( NSN(J).NE.IS ) GOTO 444
     		IF( NID(J).LT.6.OR.NID(J).GT.8 ) THEN
     		    IF( FIRST ) THEN
     			FIRST = .FALSE.
     			NFG = NFG + 1
     			NFP(NFG) = IPN
     			KSG = KSG + 1
     			CALL IFSEG_PIVOT(P00(1,J),P00(2,J))
     			CALL IFSEG_OPEN( KSG )
     			CALL IFFILL( IPN )
     		    ENDIF
     		    NSN(J) = KSG
     		    NFL(J) = NFG
     		    CALL LAYOUT_DRAW( -J, 0 )
     		    IF(.NOT.FIRST) K=K+1
     		ELSE
     		    SKIP = .TRUE.
     		    IF(BLINK.GT.0) CALL IFSEG_BLINK(-IS)
     		    GOTO 555
     		ENDIF
444	    CONTINUE
     	    IF(.NOT.SKIP) CALL IFSEG_OPEN(-IS)
555	CONTINUE

     	IF(.NOT.FIRST) THEN
     	    CALL IFFILL( 0 )
     	    CALL IFSEG_OPEN( 0 )
     	    IF(BLINK.GT.0) CALL IFSEG_BLINK(KSG)
     	    NLG = 1
     	    NLS(1) = KSG
     	ENDIF

222	IF(.NOT.FIRST) THEN
     	    CALL IFINIT_DUMP
     	    IF(KSG.GT.2*MNN) CALL LAYOUT_PACK_SEG	!REPACK SEGMENT NUMBERS
     	    IF(K.GT.1) CALL LAYOUT_PACK_NFG		!REPACK FILL GROUPS
     	ENDIF

999	RETURN
    	END

     	SUBROUTINE LAYOUT_GIN( TEXT )
C
C  THIS ROUTINE ENABLES GRAPHICS INPUT (GIN) IN LAYOUT.
C
     	INCLUDE 'LAYOUT.CBS'
     	CHARACTER KEY*3, TEXT*(*)
C
C  GET THE CURSOR COLOR...
C
     	IK = 2
     	NCH = IFMISC_NEXT( TEXT )
    	IF( NCH.GT.0 ) THEN
     	    KEY = TEXT(1:3)
     	    DO IK=1, 7
     	       IF( KEY.EQ.KCS(IK) ) GOTO 111
     	    ENDDO
    	    NCH = IFMISC_DECODE( TEXT, NCH, 1, RP, KP )
     	    IF( NCH.EQ.0 ) THEN
     		CALL IFMISC_OOPS( 3, 0, 0, TEXT )
     		GOTO 999
     	    ENDIF
     	    IK = 2
     	    IF( RP.GT.0 .AND. RP.LT.8.0 ) IK = RP
    	ENDIF
C
C  ENABLE GIN...
C
111	IF( NLG.GT.0 ) THEN
     	    IF(LVISS.LT.0) CALL LAYOUT_SET_VISS	!VISIBLE ENTRIES
     	    CALL LAYOUT_SET_BLINK( 0 )		!RELEASE THEM
     	ENDIF
     	CALL IFMAC_MACROS( 2 )			!GIN KEY MACROS
    	CALL IFGIN_ABLE( IK )			!ENABLE GIN
     	CALL IFSEG_MOVE( 0, 0.0, 0.0 )		!MOVE CURSOR TO ORIGIN
     	CALL IFINIT_DUMP
     	CALL LAYOUT_GIN_IN( IK )		!ENTER GIN LAYOUT
     	CALL IFGIN_ABLE( 0 )			!DISABLE GIN
     	IF( FKEYS.GT.0 ) THEN
     	    CALL IFMAC_MACROS(1)		!COMMAND MACROS
     	ELSE
     	    CALL IFMAC_MACROS(0)		!ERASE GIN MACROS
     	ENDIF

999	RETURN
     	END

      	SUBROUTINE LAYOUT_GIN_IN( IPN )
C
C  PROGRAMMER:  RONG-SHENG ZHOU
C		ALFRED UNIVERSITY, JUNE-1986
C
C	THIS ROUTINE IMPLEMENTS GIN LAYOUT.
C
C		IPN: GIN CURSOR COLOR CODE
C
    	INCLUDE		'LAYOUT.CBS'
     	PARAMETER	(NKS= 21)
    	BYTE 		ESC, CODE, FKEY(NKS)
     	DATA		ESC / 27 /
C
C  ARRAY "FKEY" CONTAINS THE ASCII CODES OF ALL DEFINED GIN KEYS...
C  (REFER TO [.TEKLIB]IFMACS.FOR FOR MACRO DEFINITIONS)...
C
C     	DATA FKEY / 96, 84, 46, 32,126, 67, 77, 80, 76, 70, 72, 
C     +              73, 74, 75, 88, 66, 82, 71, 79, 86, 63 /
C
     	DATA FKEY / 84, 46, 32, 92, 67, 77, 80, 76, 70, 72, 
     +              73, 74, 75, 88, 66, 82, 71, 79, 86, 63, 47 /
C
C  INITIALIZE...
C
     	IGIN = IPN		!GIN CURSOR COLOR
    	CODE = 0		!ENTRY TRANSFORM OFF
    	SIZE = 0.12		!LETTERING SIZE
    	X1ST = 0.0		!X-COORDINATE OF THE 1ST PICKED ENTRY
    	Y1ST = 0.0		!Y-COORDINATE OF THE 1ST PICKED ENTRY
C
C  START THE GIN LOOP...
C
     	WRITE(6,1234) ESC, ESC
1234	FORMAT(' LAYOUT_GIN> WARNING:',A1,'[43;30m Keyboard could',
     +' be jammed easily by QUICK key strokes! ',A1,'[44;37m'/
     +' LAYOUT_GIN> Press ? if you need any help.')
C
C  READ A GIN REPORT FROM TERMINAL...
C
1111	WRITE(6,1112)
1112	FORMAT(' LAYOUT_GIN> ',$)
     	IKEY = IFGIN_REPORT( NSEG, XPOS, YPOS )
      	IF( IKEY.EQ.0 ) GOTO 999
C
C  LOOK UP THE KEY PRESSED...
C
     	DO I = 1, NKS
     	   IF( IKEY.EQ.FKEY(I) ) GOTO 2222
     	ENDDO
     	IERR = 5
     	IF( IKEY.LT.48 .OR. IKEY.GT.57 ) GOTO 777
     	IKEY = IKEY - 48
     	IF( CODE.GT.0 .AND. NLG.GT.0 ) GOTO 200
C
C  SET LETTERING SIZE (0-9)...
C
     	SIZE = 0.04*(IKEY+1)
     	WRITE(6,1115) ESC, SIZE, ESC
1115	FORMAT(' LAYOUT_GIN> Active character size for writing:'
     +, A1,'[43;30m',X,F4.2,X,A1,'[44;37m inch.')
     	GOTO 1111
C
C  FLIP GIN MODE IF NECESSARY...
C
2222	IKEY = I
     	IF( IKEY.EQ.3 .OR. IKEY.EQ.4 ) THEN
     	    IF( IGIN.GT.0 ) THEN
     		IGIN = -IGIN		!ENABLE GIN PICKER
     		CALL IFGIN_ABLE( 0 )
     		CALL IFGIN_ABLE( IGIN )
     		GOTO 1111
     	    ENDIF
     	ELSE
     	    IF( IGIN.LT.0 ) THEN
     		IGIN = -IGIN		!ENABLE GIN LOCATOR
     		CALL IFGIN_ABLE( 0 )
     		CALL IFGIN_ABLE( IGIN )
     	    ENDIF
     	ENDIF
C
C  BRANCH TO THE DESIRED ACTION...
C
     	GOTO( 100, 120, 130, 140, 150, 160, 170,170,170,
     +  170,170,170,170, 220,230,250,260,270,300,900,900 ), IKEY
     	IERR = 5
     	GOTO 777
C
C  ENTER TEXT MODE ( ENTER .OR. T )...
C
100	IERR = 28			!MAX ENTRY COUNT HIT
     	IF( NLC.EQ.MNN ) GOTO 777
     	IERR = 25
     	IF( NTX.EQ.MNT ) GOTO 777
     	CALL LAYOUT_GIN_TEXT( SIZE, IGIN, XPOS, YPOS )
    	GOTO 1111
C
C  ENTER POINT MODE ( . )...
C
120	IKEY = LAYOUT_GIN_DOT( IGIN, XPOS, YPOS )
     	IF( IKEY.NE.0 ) THEN
     	    X1ST = XPOS
     	    Y1ST = YPOS
     	ENDIF
    	GOTO 1111
C
C  PICK FOR TRANSFORM ( SPACEBAR )...
C
130	IERR = 19
     	NSEG = IFGIN_NSEG( NSEG )
    	IF( NSEG.LE.NSG ) GOTO 777
    	IF( NLG.EQ.0 ) THEN
    	    X1ST = XPOS
    	    Y1ST = YPOS
    	ENDIF
    	CALL LAYOUT_PICK_SEG( NSEG )
    	GOTO 1111
C
C  PICK FOR DELETE( <DEL> )...
C
140	IERR = 19
     	NSEG = IFGIN_NSEG( NSEG )
    	IF( NSEG.LE.NSG ) GOTO 777
    	CALL LAYOUT_DELETE_SEG( NSEG )
    	GOTO 1111
C
C  COPY( C )...
C
150	IERR = 16
     	IF( NLG.EQ.0 ) GOTO 777
    	XPOS = XPOS - X1ST
    	YPOS = YPOS - Y1ST
    	CALL LAYOUT_COPY_ONE( XPOS, YPOS, 0.0, 0.0 )
    	GOTO 1111
C
C  MOVE ( M )...
C
160	IERR = 16
     	IF( NLG.EQ.0 ) GOTO 777
    	X1ST = XPOS - X1ST
    	Y1ST = YPOS - Y1ST
    	CALL LAYOUT_MOVE( X1ST, Y1ST )
    	X1ST = XPOS
    	Y1ST = YPOS
    	GOTO 1111
C
C  SET FOR CODE(P)/LINE(L)/FILL(F)/MOVE(F5)/ROTATE(F6)/ZOOM(F7)/COLOR(F8)...
C
170	IERR = 16
     	IF( NLG.EQ.0 ) GOTO 777
    	CODE = IKEY - 6
    	GOTO 1111
C
C  TRANSFORM THE PICKED ENTRIES ( NUMBER KEYS )...
C
200	RKEY = IKEY
     	IF( CODE.EQ.1 ) THEN
     	     CALL LAYOUT_CODE( RKEY )		!CODE CHANGE
     	ELSE IF( CODE.EQ.2 ) THEN
     	     CALL LAYOUT_LINE( RKEY )		!CHANGE LINE TYPE
     	ELSE IF( CODE.EQ.3 ) THEN
     	     CALL LAYOUT_FILL( RKEY )		!POLYGON FILL
     	ELSE IF( CODE.EQ.4 ) THEN
     	     CALL LAYOUT_MOVE_WHERE( IKEY, XFT, YFT )
     	IF(IKEY.NE.0.AND.IKEY.NE.5) CALL LAYOUT_MOVE(XFT,YFT)
     	ELSE IF( CODE.EQ.5 ) THEN
     	     CALL LAYOUT_ROTATE_ANGLE( IKEY, ANG )
    	IF(IKEY.NE.0.AND.IKEY.NE.5) CALL LAYOUT_ROTATE(ANG,XPOS,YPOS)
     	ELSE IF( CODE.EQ.6 ) THEN
     	     CALL LAYOUT_ZOOM_SCALE( IKEY, XFT, YFT )
     	IF(IKEY.NE.0.AND.IKEY.NE.5) CALL LAYOUT_ZOOM(XFT,YFT,XPOS,YPOS)
     	ELSE IF( CODE.EQ.7 ) THEN
     	     CALL LAYOUT_COLOR( RKEY )		!CHANGE COLOR
     	ENDIF
    	GOTO 1111
C
C  RELEASE PICKED ENTRIES( F2 )...
C
220	IF( NLG.GT.0 ) CALL LAYOUT_SET_BLINK( 0 )
    	CODE = 0
    	GOTO 1111
C
C  FLIP-FLOP BLINKING FEATURE ( B )...
C
230	IF( NLG.GT.0 ) CALL LAYOUT_SET_BLINK( 1 )
    	GOTO 1111
C
C  REFRESH THE SCREEN( F3 )...
C
250	CALL IFINIT_FRESH
    	GOTO 1111
C
C  FLIP-FLOP BACKGROUND SCALE ( G )...
C
260	IF( ISCP.EQ.0 ) THEN	!FLIP ON
     	    ISCP = 1
     	ELSE			!FLIP OFF
    	    ISCP = 0
    	ENDIF
     	CALL LAYOUT_SET_RULER
    	CALL IFINIT_DUMP
    	GOTO 1111
C
C  REORIGIN ( O )...
C
270	XORS = XPOS
     	YORS = YPOS
    	CALL IFSEG_MOVE( 3, XORS, YORS )
     	IF( ISCP.GT.0 ) CALL LAYOUT_SET_RULER
     	CALL IFINIT_DUMP
     	GOTO 1111
C
C  SET PICKED ENTRY VISIBILITY ( F1 )...
C
300	IERR = 16
     	IF( NLG.EQ.0 ) GOTO 777
     	CALL LAYOUT_SET_VISS
     	GOTO 1111
C
C  DISPLAY HELP MENU( ? )...
C
900	CALL LAYOUT_GIN_HELP( 0, IGIN, XPOS, YPOS )
     	GOTO 1111
C
C  ERROR TRANSFER...
C
777	CALL IFMISC_OOPS( IERR, 0, 0, ' ' )
     	GOTO 1111
999	RETURN
    	END

    	SUBROUTINE LAYOUT_GIN_TEXT( SIZE, IGIN, XPS, YPS )
C
C  PROGRAMMER: R.S. ZHOU		MARCH-1986
C
C	THIS ROUTINE IMPLEMENTS GIN LETTERING.
C
    	INCLUDE		'LAYOUT.CBS'
    	CHARACTER	TEXT*75

     	XPOS = XPS
     	YPOS = YPS
     	IF( DIFF.GT.0 ) CALL IFINIT_DIF(0)
    	CALL IFSPEN( 3 )
     	CALL IFPLOT( XPOS-0.05, YPOS, 3 )
     	CALL IFPLOT( XPOS+0.05, YPOS, 2 )
     	CALL IFPLOT( XPOS, YPOS-0.05, 3 )
     	CALL IFPLOT( XPOS, YPOS+0.05, 2 )
     	CALL IFMAC_KEYEXP( 0 )
     	CALL IFGIN_ABLE( 0 )
     	IF( DIFF.GT.0 ) CALL IFINIT_DIF(1)
111	WRITE(6,1000)
1000	FORMAT(' LAYOUT_TEXT> ',$)
     	READ(5,1001,ERR=111,END=999) TEXT
1001	FORMAT(A75)
C
C  ENTER A TEXT LINE...
C
     	IF( TEXT.NE.' ' ) THEN
     	    NCH = IFMISC_STRIP(TEXT)
     	    NLC = NLC + 1
     	    NTX = NTX + 1
     	    LDT(NTX) = NLC
     	    TXT(NTX) = TEXT
     	    NFL(NLC) = 0
     	    NID(NLC) = 8
     	    PEN(NLC) = 1
     	    P00(1,NLC) = XPOS
    	    P00(2,NLC) = YPOS
    	    P00(3,NLC) = SIZE
     	    P00(4,NLC) = SIZE
     	    P00(5,NLC) = 0.0
     	    CALL LAYOUT_DRAW( NLC, 1 )
     	ENDIF
999	CALL IFGIN_ABLE( IGIN )
     	CALL IFSEG_MOVE( 0, XPOS, YPOS )
     	CALL IFMAC_KEYEXP( 1 )
     	CALL IFINIT_DUMP
     	RETURN
    	END

     	FUNCTION LAYOUT_GIN_DOT( IGIN, XPS, YPS )
C
C  PROGRAMMER: R.S. ZHOU		MARCH-1986
C
C	THIS ROUTINE IMPLEMENTS GIN LINE/CURVE
C	OR PICK LAYOUT ENTRIES IN BOX MODE.
C
    	INCLUDE	'LAYOUT.CBS'
     	LOGICAL LEFT, RIGHT

     	NPTS = 0
     	LINE = 1
     	LEFT = .FALSE.
     	RIGHT= .FALSE.
     	XPOS = XPS
     	YPOS = YPS
     	WRITE(6,1)
 1	FORMAT(' LAYOUT_DOT> Press ? if you need any help.')
     	GOTO 222
 111	WRITE(6,112)
 112	FORMAT(' LAYOUT_DOT> ',$)
     	IKEY = IFGIN_REPORT( JUNK, XPOS, YPOS )
      	IF( IKEY.EQ.0 ) GOTO 999
     	IF( IKEY.NE.32 .AND. IKEY.NE.46 ) GOTO 333
     	IF( XPOS.EQ.XOLD .AND. YPOS.EQ.YOLD ) GOTO 334	!DISCARD POINT
C
C  ADD A POINT ( SPACEBAR .OR. DOT )...
C
     	IF( NPTS.EQ.MNP ) THEN
     	    CALL IFMISC_OOPS( 24, 0, 0, ' ' )
     	    GOTO 111
     	ENDIF
 222	NPTS = NPTS + 1
     	XOLD = XPOS
     	YOLD = YPOS
     	XSC(NPTS,NCU+1) = XPOS-XPS
    	YSC(NPTS,NCU+1) = YPOS-YPS
     	IKEY = 3
 223	IF( DIFF.GT.0 ) CALL IFINIT_DIF(0)
    	CALL IFSPEN( IKEY )
     	CALL IFPLOT( XPOS-0.05, YPOS, 3 )
     	CALL IFPLOT( XPOS+0.05, YPOS, 2 )
     	CALL IFPLOT( XPOS, YPOS-0.05, 3 )
     	CALL IFPLOT( XPOS, YPOS+0.05, 2 )
    	CALL IFINIT_DUMP
     	IF( DIFF.GT.0 ) CALL IFINIT_DIF(1)
    	GOTO 111
 333	IF( IKEY.NE.44 .AND. IKEY.NE.45 .AND. IKEY.NE.92 ) GOTO 444
 334	IF( NPTS.EQ.0 ) GOTO 111
C
C  DELETE A POINT ( DELETE .OR. MINUS-SIGN .OR. COMMA ) ...
C
     	XPOS = XOLD
     	YPOS = YOLD
     	NPTS = NPTS - 1
     	XOLD = XSC(NPTS,NCU+1) + XPS
     	YOLD = YSC(NPTS,NCU+1) + YPS
     	IKEY = 0
     	GOTO 223
 444	IF( IKEY.NE.80 .AND. IKEY.NE.83 ) GOTO 555
C
C  PICK ENTRIES THAT FALL IN THE BOX DEFINED BY TWO CORNERS ( P .OR. S )...
C
     	IF( NPTS.NE.2 ) GOTO 888
     	X11 = XPS
    	Y11 = YPS
    	X22 = XPS+XSC(2,NCU+1)
    	Y22 = YPS+YSC(2,NCU+1)
     	IF( X22.LT.X11 ) THEN
     	    XPOS= X11
     	    X11 = X22
     	    X22 = XPOS
     	ENDIF
     	IF( Y22.LT.Y11 ) THEN
     	    XPOS= Y11
     	    Y11 = Y22
     	    Y22 = XPOS
     	ENDIF
     	IKEY = NLG
     	DO  I=1, NLC
     	    IF( P00(1,I).GT.X11 .AND. P00(1,I).LT.X22
     1    .AND. P00(2,I).GT.Y11 .AND. P00(2,I).LT.Y22 ) THEN
     		CALL LAYOUT_PICK_SEG( NSN(I) )
     	    ENDIF
     	ENDDO
     	LAYOUT_GIN_DOT = 0
     	IF( IKEY.EQ.0 .AND. NLG.GT.0 ) LAYOUT_GIN_DOT = 1
     	RETURN
555	IF( IKEY.NE.84 ) GOTO 666
C
C  ADD A NEW ENTRY ( ENTER )...
C
     	IF( NPTS.LT.2 ) GOTO 888
     	IF( NLC.EQ.MNN ) THEN
     	    CALL IFMISC_OOPS( 28, 0, 0, ' ' )
     	    GOTO 999
     	ENDIF
     	IF( NPTS.EQ.2 .AND. (LEFT.OR.RIGHT) ) THEN	!LINE ARROW
     	    LINE = 1
     	    IF( LEFT .AND. RIGHT )  LINE = 11		!DOUBLE ARROWHEAD
     	    NLC = NLC + 1
     	    NID(NLC) = 6
     	    X22 = XSC(2,NCU+1)
     	    Y22 = YSC(2,NCU+1)
     	    X11 = SQRT( X22**2 + Y22**2 )
     	    Y11 = ASIND( Y22/X11 )
     	    IF( X22.LT.0.0 ) Y11 = 180.0-Y11
     	    P00(1,NLC) = X22+XPS
     	    P00(2,NLC) = Y22+YPS
     	    P00(3,NLC) = X11
     	    P00(4,NLC) = 0.04
     	    P00(5,NLC) = 0.10
     	    P00(6,NLC) = Y11
     	    P00(7,NLC) = LINE
     	ELSE						!LINE ENTRY
     	    IF( NPTS.GT.2 ) THEN
     		IF( NCU.EQ.MNC ) THEN
     		    CALL IFMISC_OOPS( 26, 0, 0, ' ' )
     		    GOTO 999
     		ENDIF
     		IKEY = 0
     		IF( LEFT ) NPTS = -NPTS		!CURVE
     		IF( RIGHT) IKEY = 1		!AREA FILL ON
     	    ENDIF
     	    NLC = NLC + 1
     	    NID(NLC) = 9
     	    P00(1,NLC) = XPS
     	    P00(2,NLC) = YPS
     	    P00(3,NLC) = NPTS
     	    P00(4,NLC) = 1.0
     	    P00(5,NLC) = 1.0
     	    P00(6,NLC) = 0.0
     	    P00(7,NLC) = IKEY
     	    P00(8,NLC) = LINE
     	    P00(9,NLC) = LWID
     	    IF( NPTS.EQ.2 ) THEN
     		P00(10,NLC) = XSC(2,NCU+1)
     		P00(11,NLC) = YSC(2,NCU+1)
     	    ELSE
     		NCU = NCU + 1
     		LID(NCU) = NLC
     	    ENDIF
     	ENDIF
     	PEN(NLC) = 1
     	NFL(NLC) = 0
     	CALL LAYOUT_DRAW( NLC, 1 )
     	CALL IFINIT_DUMP
     	GOTO 999
C
C  SET FLAGS...
C
 666	IF( IKEY.EQ.60 ) THEN
     	    LEFT = .TRUE.
     	ELSE IF( IKEY.EQ.62 ) THEN
     	    RIGHT = .TRUE.
     	ELSE IF( IKEY.GT.48 .AND. IKEY.LT.58 ) THEN	!LINE-TYPE
     	    LINE = IKEY - 48
     	ELSE IF( IKEY.EQ.63 ) THEN	!HELP
     	    CALL LAYOUT_GIN_HELP( 1, IGIN, XPOS, YPOS )
     	ELSE
     	    GOTO 888
     	ENDIF
     	GOTO 111
 888	CALL IFMISC_OOPS( 5, 0, 0, ' ' )
     	GOTO 111
 999	LAYOUT_GIN_DOT = 0
     	RETURN
     	END

     	SUBROUTINE LAYOUT_GIN_HELP( IT, IGIN, XPOS, YPOS )
C
C	THIS ROUTINE DISPLAYS ON-LINE HELP ON GIN LAYOUT.
C
     	CALL IFGIN_ABLE( 0 )			!DISABLE GIN
     	IF( IT.EQ.0 ) THEN
     	    IKEY=IFMISC_TYPE( 'GRAFIT$HLP:LAYOUT_GIN.HLP', ' ', 21 )
     	ELSE
     	    IKEY=IFMISC_TYPE( 'GRAFIT$HLP:LAYOUT_DOT.HLP', ' ', 21 )
     	ENDIF
     	IF( IKEY.GT.0 ) CALL IFMISC_OOPS( 9, 0, 0, ' ' )
    	CALL IFGIN_ABLE( IGIN )			!ENABLE GIN
     	CALL IFSEG_MOVE( 0, XPOS, YPOS )	!MOVE CURSOR TO ORIGIN
     	CALL IFINIT_LINE(3)
    	RETURN
    	END

     	SUBROUTINE LAYOUT_HELP( HELP )
C
C  PROGRAMMER: R.S. ZHOU		MARCH-1986
C
C	THIS ROUTINE DISPLAYS ON-LINE HELP FOR INTERACTIVE LAYOUT.
C
    	CHARACTER HELP*(*)

	IK = IFMISC_NEXT( HELP )
 	IF( IK.EQ.0 ) THEN
     	IERR = IFMISC_TYPE('GRAFIT$HLP:LAYOUT_HELP.HLP','HELP',-1)
	ELSE IF( HELP(1:3).EQ.'SET' .AND. IK.GT.3 ) THEN
     	    IK = IFMISC_NEXT( HELP )
	    IF( HELP(1:3).EQ.'ALL' ) THEN
     		IERR = IFMISC_TYPE('GRAFIT$HLP:LAYOUT_SET.HLP',' ',0)
	    ELSE
     	IERR = IFMISC_TYPE('GRAFIT$HLP:LAYOUT_SET.HLP',HELP(1:IK),-1)
	    ENDIF
	ELSE
     	IERR = IFMISC_TYPE('GRAFIT$HLP:LAYOUT_HELP.HLP',HELP(1:IK),-1)
	ENDIF
     	IF( IERR.EQ.1 ) CALL IFMISC_OOPS( 9, 0, 0, ' ' )
     	IF( IERR.EQ.2 ) CALL IFMISC_OOPS( 8, 0, 0, HELP )
999	RETURN
     	END

    	SUBROUTINE LAYOUT_HELP_ENTRY( ICODE )
C
C  PROGRAMMER: R.S. ZHOU		MARCH-1986
C
C	THIS ROUTINE DISPLAYS HELP INFORMATION
C	FOR LAYOUT ENTRY PARAMETERS.
C
     	CHARACTER KEYWORD*8

     	ICD = ICODE
     	IF( ICD.EQ.1 ) KEYWORD = 'BAR'
     	IF( ICD.EQ.2 ) KEYWORD = 'BOX'
     	IF( ICD.EQ.3 ) KEYWORD = 'ARC'
     	IF( ICD.EQ.4 ) KEYWORD = 'OBLIQUE'
     	IF( ICD.EQ.5 ) KEYWORD = 'POLYGON'
     	IF( ICD.EQ.6 ) KEYWORD = 'ARROW'
     	IF( ICD.EQ.7 ) KEYWORD = 'SYMBOL'
     	IERR = IFMISC_TYPE('GRAFIT$HLP:LAYOUT_ENTRY.HLP',KEYWORD,-2)
     	IF( IERR.EQ.1 ) CALL IFMISC_OOPS( 9, 0, 0, ' ' )
     	IF( IERR.EQ.2 ) CALL IFMISC_OOPS( 8, 0, 0, KEYWORD )
	RETURN
	END

    	FUNCTION LAYOUT_IO( ICODE, PNAME )
C
C  PROGRAMMER: R.S. ZHOU		MARCH-1986
C
C	THIS ROUTINE WRITES AND READS A LAYOUT FILE.
C
C	ICODE = 0: WRITE OUT A LAYOUT FILE.
C	ICODE = 1: READ IN A LAYOUT FILE AND RETURN WITH:
C
C	LAYOUT_IO =	 1 FOR OK FILE
C		   	 0 FOR OPEN FAILURE.
C			-1 FOR NON-LAYOUT FILE
C
C	PNAME ---- INPUT/OUTPUT FILE.
C
    	INCLUDE		'LAYOUT.CBS'
    	PARAMETER	(NKS= 14, LW= 132)
    	CHARACTER	KWD(NKS)*4, KEY*4, REST*(LW), PNAME*32
    	DIMENSION	KP(20), RP(20), IR(NKS)

    	DATA KWD /
     +  'BARS', 'BOXS', 'ARCS', 'OBLI', 'POLY', 'ARRH', 'MARK',
     +  'TEXT', 'LINE', 'COOX', 'COOY', 'PAR1', 'PAR2', 'IDEN' /
     	DATA IR / 11, 11, 13, 11, 11, 9, 7, 6, 13, 10, 10, 10, 10, 0 /

    	IF( ICODE.EQ.1 ) GOTO 111
C
C  	----------	WRITE A LAYOUT FILE	------------
C
     	CALL IFMISC_DEFEXT( PNAME, '.DES' )
      	CLOSE( UNIT=13 )
      	OPEN( UNIT=13, FILE=PNAME, STATUS='NEW', FORM='FORMATTED',
     +        CARRIAGECONTROL='LIST' )
C
C  WRITE THE FILE HEADER FIRST...
C
     	WRITE(13,1)
  1	FORMAT('%This is a GRAFIT layout file.'/
     + '%PAR1= HARDCOPY PARAMETERS FROM  1 TO 10.'/
     + '%PAR2= HARDCOPY PARAMETERS FROM 11 TO 20.'/
     + '%BARS= PEN,X,Y,LENGTH,HEIGHT,BORDER,ROTATE,FILL,LINE'/
     + '%BOXS= PEN,X,Y,LENGTH,HEIGHT,BORDER,ROTATE,FILL,LINE'/
     + '%ARCS= PEN,X,Y,R1,R2,BORDER,ROTATE,ANGLE1,ANGLE2,FILL,LINE'/
     + '%OBLI= PEN,X,Y,D1,D2,BORDER,ROTATE,FILL,LINE'/
     + '%POLY= PEN,X,Y,SIZE,BORDER,ROTATE,EDGES,FILL,LINE'/
     + '%ARRH= PEN,X,Y,LENGTH,HEIGHT,HEAD,ROTATE,TYPE'/
     + '%MARK= PEN,X,Y,SIZE,ROTATE,NUMBER'/
     + '%TEXT= PEN,X,Y,WIDTH,HEIGHT,ROTATE,STRING.'/
     + '%LINE= PEN,X,Y,NPOINT,XSCALE,YSCALE,ROTATE,FILL,LINE'/
     + '%COOX= X-COORDINATES OF A LINE/CURVE (NPOINT>2)'/
     + '%COOY= Y-COORDINATES OF A LINE/CURVE (NPOINT>2)'/
     + '%IDEN= PLOT IDENTIFICATION LABEL')
    	IF( NLC.EQ.0 ) GOTO 999		!NOTHING TO SAVE
C
C  WRITE THE HARDCOPY PARAMETERS NEXT...
C
     	WRITE(13,10) (HPAR(I),I=1,20), PLOTID
 10	FORMAT('PAR1=',10(F6.2,','),/'PAR2=',10(F6.2,',')/'IDEN=',A32)
C
C  WRITE THE NON-LINE/TEXT ENTRIES NEXT...
C
     	DO  I=1, NLC
     	    ID = NID(I)
     	    IF( ID.NE.8 .AND. ID.NE.9 ) THEN
     		JR = IR(ID)-2
     		WRITE(13,30) KWD(ID),PEN(I),(P00(J,I),J=1,JR),NFL(I)
     	    ENDIF
     	    IF( ID.EQ.9 .AND. P00(3,I).EQ.2.0 ) THEN
     		JR = IR(ID)-2
     		WRITE(13,30) KWD(ID),PEN(I),(P00(J,I),J=1,JR),NFL(I)
     	    ENDIF
     	ENDDO
30	FORMAT(A4,'=',I2,<JR>(',',F7.3),',',I2)
C
C  WRITE THE TEXT ENTRIES NEXT...
C
     	JR = IR(8)-1
     	DO  I=1, NTX
     	    ID = LDT(I)
     	    NCH = IFMISC_LENG(TXT(I))
     	    WRITE(13,40)KWD(8),PEN(ID),(P00(J,ID),J=1,JR),TXT(I)(1:NCH)
     	ENDDO
40	FORMAT(A4,'=',I2,<JR>(',',F7.3),',',A)
C
C  WRITE THE LINE ENTRIES NEXT...
C
     	JR = IR(9)-2
     	DO  I=1, NCU
     	    ID = LID(I)
     	    WRITE(13,30) KWD(9),PEN(ID),(P00(J,ID),J=1,JR),NFL(ID)
     	    DO  J=1, IABS(INT(P00(3,ID))), 10
     		WRITE(13,50) KWD(10),  (XSC(K,I),K=J,J+9)
     		WRITE(13,50) KWD(11), (YSC(K,I),K=J,J+9)
     	    ENDDO
     	ENDDO
50	FORMAT(A4,'=',10(F7.3,','))
C
C  WRITE THE FILL PATTERNS NEXT...
C
     	IF( NFG.GT.0 ) WRITE(13,60) (NFP(I),I=1,NFG)
60	FORMAT('FILL=',<NFG>I3)
    	GOTO 999
C
C  	----------	READ A LAYOUT FILE	-----------
C
111	CALL IFMISC_DEFEXT( PNAME, '.DES' )
     	OPEN( UNIT=13, FILE=PNAME, STATUS='OLD', FORM='FORMATTED',
     +        CARRIAGECONTROL='LIST', READONLY, ERR=888 )

     	NLO = NLC
     	NFO = NFG
222	IF( IFCTRL_C() ) GOTO 889
     	READ(13,333,END=777) KEY, REST
333	FORMAT(A4,X,A)
C
C  CHECK KEYWORD MATCH...
C
    	DO ID=1, NKS
    	   IF( KEY.EQ.KWD(ID) ) GOTO 555
    	ENDDO
     	IF( KEY.EQ.'FILL' ) GOTO 58
    	GOTO 222
C
C  DECODE ENTRY PARAMETERS...
C
555	NCH = IFMISC_STRIP( REST )
     	IF( NCH.EQ.0 ) GOTO 222
    	IF( ID.NE.NKS ) THEN
     	    JR = IFMISC_DECODE( REST, NCH, IR(ID), RP, KP )
     	    IF( JR.EQ.0 ) GOTO 222
     	ENDIF
    	GOTO (51,51,51,51,51,51,51,51,51,52,53,54,55,56), ID
     	GOTO 222
C
C  STORE ENTRY PARAMETERS IN P00(I,J)...
C
51	IF( NLC.EQ.MNN ) GOTO 666
     	IF( NLC.GT.NLO .AND. NFL(NLC).EQ.0 ) CALL LAYOUT_DRAW(NLC,1)
     	NLC = NLC + 1
     	NID(NLC) = ID
     	PEN(NLC) = RP(1)
     	RP(2) = RP(2) + XORS
     	RP(3) = RP(3) + YORS
     	DO I=1, IR(ID)-1
     	   P00(I,NLC) = RP(I+1)
     	ENDDO
     	IF( ID.EQ.8 ) THEN		!GET TEXTSTRING
     	    NCH = NCH - JR + 1
     	    IF( NCH.GT.75 ) NCH = 75
     	    NTX = NTX + 1
     	    LDT(NTX) = NLC
     	    TXT(NTX) = REST(JR:)
     	ENDIF
     	NFL(NLC) = 0
     	IF( ID.LT.6 .OR. ID.GT.8 ) THEN
     	    JR = INT(RP(I)) + NFG
     	    IF( JR.GT.NFG .AND. JR.LE.MNF ) THEN
     		NFL(NLC) = JR
     		IF( JR.GT.NFO ) NFO = JR
     	    ENDIF
     	ENDIF
     	IF( ID.EQ.9 .AND. P00(3,NLC).NE.2.0 ) THEN	!LINE ENTRY
     	    NPX = 0
     	    NPY = 0
     	    NCU = NCU + 1
     	    LID(NCU) = NLC
     	ENDIF
     	GOTO 222
C
C  STORE X-COORDINATES OF THE LINE...
C
52	DO I=1, 10
     	   XSC(NPX+I,NCU) = RP(I)
     	ENDDO
     	NPX = NPX + 10
     	GOTO 222
C
C  STORE Y-COORDINATES OF THE LINE...
C
53	DO I=1, 10
     	   YSC(NPY+I,NCU) = RP(I)
     	ENDDO
     	NPY = NPY + 10
     	GOTO 222
C
C  STORE HARDCOPY ATTRIBUTES...
C
54	DO I=1, 10
     	   HPAR(I) = RP(I)
     	ENDDO
     	GOTO 222
55	DO I=1, 10
     	   HPAR(I+10) = RP(I)
     	ENDDO
     	GOTO 222
C
C  STORE THE PLOT ID...
C
56	PLOTID = REST
     	GOTO 222
C
C  STORE THE FILL PATTERNS...
C
58	READ(REST,59,ERR=222) (NFP(I),I=NFG+1,MNF)
59	FORMAT(<MNF>I3)
     	GOTO 222
666	CALL IFMISC_OOPS(28,0,0,' ')	!MAX ENTRY COUNT HIT
777	IF( NLC.EQ.NLO ) THEN
     	    LAYOUT_IO = -1		!NOTHING READ
     	    RETURN
     	ENDIF
     	IF( NFL(NLC).EQ.0 ) CALL LAYOUT_DRAW(NLC,1)	!DRAW THE LAST ENTRY
     	IF( NFO.GT.NFG ) THEN		!DRAW THE FILL-GROUPS
     	    DO I=NFG+1, NFO
     		CALL LAYOUT_FILL_DRAW( I )
     	    ENDDO
     	    NFG = NFO			!UPDATE THE COUNTER
     	ENDIF
889	CALL IFINIT_PLOT_END
     	GOTO 999
888	LAYOUT_IO = 0
        CLOSE(UNIT=13)
     	RETURN
999	CLOSE( UNIT=13 )
     	LAYOUT_IO = 1
     	RETURN
    	END

    	SUBROUTINE LAYOUT_FILL_DRAW( IFG )
C
C  PROGRAMMER: R.S. ZHOU		MARCH-1987
C
C	THIS ROUTINE DRAWS A LAYOUT FILL-GROUP.
C
C		IFG: FILL GROUP NUMBER
C
    	INCLUDE 'LAYOUT.CBS'
     	LOGICAL FIRST

     	JFG = IFG
     	FIRST = .TRUE.
    	DO I = 1, NLC
     	    IF( NFL(I).EQ.JFG ) THEN
     		IF( FIRST ) THEN
     		    KSG = KSG + 1
     		    CALL IFSEG_PIVOT(P00(1,I),P00(2,I))
     		    CALL IFSEG_OPEN( KSG )
     		    CALL IFFILL( NFP(JFG) )
     		    FIRST = .FALSE.
     		ENDIF
     		CALL LAYOUT_DRAW( -I, 0 )
     		NSN(I) = KSG
     	    ENDIF
     	ENDDO
     	IF( .NOT. FIRST ) THEN
     	    CALL IFFILL( 0 )
     	    CALL IFSEG_OPEN( 0 )
     	ENDIF

999	RETURN
    	END

     	SUBROUTINE LAYOUT_DRAW( LEN, IFL )
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY
C
C	THIS ROUTINE DRAWS A LAYOUT ENTRY.
C
C	LEN ----- LAYOUT ENTRY NUMBER
C	IFL ----- ENTRY FILL FLAG
C
    	INCLUDE		'LAYOUT.CBS'
     	CHARACTER	TEXT*75
    	DIMENSION	R(MNS), XSS(MNP), YSS(MNP)

     	ISS = IABS(LEN)
     	JFL = IFL
     	DO  I=1, MNS
     	    R(I) = P00(I,ISS)			!LOAD ENTRY PARAMETERS
     	ENDDO
     	IF( LEN.GT.0 ) THEN
     	    CALL IFSEG_PIVOT( R(1), R(2) )	!SET SEGEMEMT PIVOT
     	    KSG = KSG + 1			!UPDATE SEGMENT NUMBER
     	    NSN(ISS) = KSG			!UPDATE THE POINTER
     	    CALL IFSEG_OPEN( KSG )		!OPEN A NEW SEGMENT
     	ENDIF
     	IPN = PEN(ISS)
     	CALL IFSPEN( IPN )
C
C  BAR...
C
     	ID = NID(ISS)
     	IF( ID.EQ.1 ) THEN
     	    IT1 = R(7)
     	    IT2 = R(8)
     	    IT3 = R(9)
     	    CALL IFSTYL(IT2,IT3)
	    IF( JFL.NE.0 .AND. IT1.NE.0 ) CALL IFFILL(IT1)
     	    CALL IFBARS( R(1), R(2), R(3), R(4), R(5), R(6) )
	    IF( JFL.NE.0 .AND. IT1.NE.0 ) CALL IFFILL(0)
     	    CALL IFSTYL(LTYP,LWID)
C
C  BOX...
C
     	ELSE IF( ID.EQ.2 ) THEN
     	    IT1 = R(7)
     	    IT2 = R(8)
     	    IT3 = R(9)
     	    CALL IFSTYL(IT2,IT3)
	    IF( JFL.NE.0 .AND. IT1.NE.0 ) CALL IFFILL(IT1)
     	    CALL IFBOXS( R(1), R(2), R(3), R(4), R(5), R(6) )
	    IF( JFL.NE.0 .AND. IT1.NE.0 ) CALL IFFILL(0)
     	    CALL IFSTYL(LTYP,LWID)
C
C  ARC/PIE/CIRCLE/ELLIPSE...
C
     	ELSE IF( ID.EQ.3 ) THEN
     	    IT1 = R(9)
     	    IT2 = R(10)
     	    IT3 = R(11)
     	    CALL IFSTYL(IT2,IT3)
	    IF( JFL.NE.0 .AND. IT1.NE.0 ) CALL IFFILL(IT1)
     	    CALL IFARCS(R(1),R(2),R(3),R(4),R(5),R(6),R(7),R(8),CHORD)
	    IF( JFL.NE.0 .AND. IT1.NE.0 ) CALL IFFILL(0)
     	    CALL IFSTYL(LTYP,LWID)
C
C  OBLIQUE...
C
     	ELSE IF( ID.EQ.4 ) THEN
     	    IT1 = R(7)
     	    IT2 = R(8)
     	    IT3 = R(9)
     	    CALL IFSTYL(IT2,IT3)
	    IF( JFL.NE.0 .AND. IT1.NE.0 ) CALL IFFILL(IT1)
     	    CALL IFOBLI( R(1), R(2), R(3), R(4), R(5), R(6) )
	    IF( JFL.NE.0 .AND. IT1.NE.0 ) CALL IFFILL(0)
     	    CALL IFSTYL(LTYP,LWID)
C
C  POLYGON...
C
     	ELSE IF( ID.EQ.5 ) THEN
     	    IT1 = R(7)
     	    IT2 = R(8)
     	    IT3 = R(9)
     	    CALL IFSTYL(IT2,IT3)
	    IF( JFL.NE.0 .AND. IT1.NE.0 ) CALL IFFILL(IT1)
     	    CALL IFPOLY( INT(R(6)), R(1), R(2), R(3), R(4), R(5) )
	    IF( JFL.NE.0 .AND. IT1.NE.0 ) CALL IFFILL(0)
     	    CALL IFSTYL(LTYP,LWID)
C
C  ARROWHEAD...
C
     	ELSE IF( ID.EQ.6 ) THEN
     	    CALL IFARRH( INT(R(7)),R(1),R(2),R(3),R(4),R(5),R(6) )
C
C  MARKER SYMBOL...
C
     	ELSE IF( ID.EQ.7 ) THEN
     	    CALL IFMARK( R(1), R(2), R(3), R(4), INT(R(5)) )
C
C  TEXT...
C
     	ELSE IF( ID.EQ.8 ) THEN
     	    ID = LAYOUT_LDT( ISS )
     	    TEXT = TXT(ID)
    	    CALL IFTEXT_EXT(R(1),R(2),TEXT,R(3),R(4),R(5))
C
C  LINE/CURVE...
C
     	ELSE IF( ID.EQ.9 ) THEN
     	    IT1 = R(7)
     	    IT2 = R(8)
     	    IT3 = R(9)
     	    CALL IFPLOT( R(1), R(2), -9 )
     	    IF( R(6).NE.0.0 ) THEN
     		CALL IFROTA_NOW( XANG, YANG )
     		CALL IFROTA( XANG+R(6), YANG+R(6) )
     	    ENDIF
     	    CALL IFSTYL(IT2,IT3)
     	    NPTS = R(3)
     	    IF( NPTS.EQ.2 ) THEN
     		CALL IFPLOT(   0.0,   0.0, 3 )
     		CALL IFPLOT( R(4)*R(10), R(5)*R(11), 2 )
     	    ELSE
     		IF( JFL.NE.0 .AND. IT1.NE.0 ) CALL IFFILL(IT1)
     		ID = LAYOUT_LID( ISS )
     		IJ = IABS(NPTS)
     		DO I=1, IJ
     		   XSS(I) = R(4)*XSC(I,ID)
     		   YSS(I) = R(5)*YSC(I,ID)
     		ENDDO
     		ID = 0
     		IF( NPTS.LT.0 ) ID = 1
     		CALL IFCURV( IJ, ID, XSS, YSS )
     		IF( JFL.NE.0 .AND. IT1.NE.0 ) CALL IFFILL(0)
     	    ENDIF
     	    CALL IFSTYL(LTYP,LWID)
     	    IF( R(6).NE.0.0 ) CALL IFROTA(XANG,YANG)
     	    CALL IFPLOT( -R(1), -R(2), -9 )
     	ENDIF
     	IF( LEN.GT.0 ) CALL IFSEG_OPEN(0)	!CLOSE THE SEGMENT
C
C  RESET CUMMULATIVE TRANSFORM...
C
     	RAN(ISS) = 0.0
     	ZXF(ISS) = 1.0
     	ZYF(ISS) = 1.0
999	RETURN
     	END

     	FUNCTION LAYOUT_LID( IT )
C
C	THIS ROUTINE RETURNS WITH POINTER OF LINE ENTRY "IT"
C	IN LINE ENTRY ARRAY "LID".
C
     	INCLUDE 'LAYOUT.CBS'

     	ITT = IT
     	DO I=1, NCU
     	   IF( LID(I) .EQ. ITT ) GOTO 999
     	ENDDO
     	I = 0
999	LAYOUT_LID = I

     	RETURN
     	END

     	FUNCTION LAYOUT_LDT( IT )
C
C	THIS ROUTINE RETURNS WITH POINTER OF TEXT ENTRY "IT"
C	IN TEXT ENTRY ARRAY "LDT".
C
     	INCLUDE 'LAYOUT.CBS'

     	ITT = IT
     	DO I=1, NTX
     	   IF( LDT(I) .EQ. ITT ) GOTO 999
     	ENDDO
     	I = 0
999	LAYOUT_LDT = I

     	RETURN
     	END

    	SUBROUTINE LAYOUT_LINE( PF )
C
C  PROGRAMMER: R.S. ZHOU	MAY-1986
C
C	THIS ROUTINE CHANGES THE LINE TYPES OF PICKED LAYOUT 
C	SEGMENTS TO THAT DESIGNATED BY "PF":
C
C	FILL-GROUP ENTRIES AND TEXT ENTRIES ARE IGNORED.
C
    	INCLUDE		'LAYOUT.CBS'
     	DIMENSION	IR(9)
     	DATA		IR / 8, 8, 10, 8, 8, 0, 0, 0, 8 /

     	PF1 = PF
    	DO  222 I = 1, NLG
    	    IS = NLS(I)
     	    SKIP = .FALSE.
     	    DO  111 J=1, NLC
     		IF( NSN(J).NE.IS .OR. NFL(J).GT.0 ) GOTO 111
     		IT = IR(NID(J))
     		IF( IT.EQ.0 .OR. P00(IT,J).EQ.PF1 ) GOTO 111
     		P00(IT,J) = PF1
     		CALL IFSEG_OPEN(-IS)
     		CALL IFSEG_PIVOT(P00(1,J),P00(2,J))
     		CALL IFSEG_OPEN(IS)
     		CALL LAYOUT_DRAW(-J,1)
     		CALL IFSEG_OPEN(0)
     		IF(BLINK.GT.0) CALL IFSEG_BLINK( IS )
     		GOTO 222
111	    CONTINUE
222	CONTINUE
     	CALL IFINIT_DUMP
     	RETURN
    	END

    	SUBROUTINE LAYOUT_MOVE( XADD, YADD )
C
C  PROGRAMMER: R.S. ZHOU		JUNE-1986
C
C	THIS ROUTINE MOVES PICKED LAYOUT SEGMENTS
C	BY "XADD" AND "YADD" INCHES.
C
    	INCLUDE 'LAYOUT.CBS'
     	LOGICAL SKIP

     	IF( XADD.EQ.0.0 .AND. YADD.EQ.0.0 ) RETURN

     	XAD = XADD
     	YAD = YADD
    	DO  222 I = 1, NLG
    	    IS = NLS(I)
     	    SKIP = .FALSE.
     	    DO  111 J=1, NLC
     		IF( NSN(J).NE.IS ) GOTO 111
     		XPS = P00(1,J) + XAD
     		YPS = P00(2,J) + YAD
     		P00(1,J) = XPS
     		P00(2,J) = YPS
     		IF( .NOT.SKIP ) CALL IFSEG_MOVE(IS,XPS,YPS)
     		IF( NFL(J).EQ.0 ) GOTO 222
     		SKIP = .TRUE.
111	    CONTINUE
222	CONTINUE

     	CALL IFINIT_DUMP

    	RETURN
    	END

    	SUBROUTINE LAYOUT_MOVE_WHERE( IP, XAD, YAD )
C
C  THIS ROUTINE ASSIGNS DIFFERENT INCREMENTAL DISPLACEMENT "XAD,YAD"
C  ACCORDING TO "IP" FOR LAYOUT PLOTTING
C
    	SAVE IOPL

    	IOP = IP
    	XAD = 0.0
    	YAD = 0.0
    	IF( IOP.EQ.1 ) THEN
    	    XAD = -0.02
    	    YAD = -0.02
    	ELSE IF( IOP.EQ.2 ) THEN
     	    YAD =-0.02
     	ELSE IF( IOP.EQ.3 ) THEN
    	    XAD =  0.02
    	    YAD = -0.02
    	ELSE IF( IOP.EQ.4 ) THEN
     	    XAD =-0.02
     	ELSE IF( IOP.EQ.5 ) THEN
    	    IF(IOPL.NE.10) THEN
    	    	IOPL = 10
    	    ELSE
    	    	IOPL = 1
    	    ENDIF
    	ELSE IF( IOP.EQ.6 ) THEN
     	    XAD = 0.02
     	ELSE IF( IOP.EQ.7 ) THEN
    	    XAD = -0.02
    	    YAD =  0.02
    	ELSE IF( IOP.EQ.8 ) THEN
     	    YAD = 0.02
     	ELSE IF( IOP.EQ.9 ) THEN
    	    XAD = 0.02
    	    YAD = 0.02
    	ELSE IF( IOP.EQ.0 ) THEN
    	    IF(IOPL.NE.25) THEN
     		IOPL = 25
    	    ELSE
     		IOPL = 10
    	    ENDIF
    	ENDIF
    	IF( IOPL.EQ.0 ) IOPL = 1
    	XAD = XAD * IOPL
    	YAD = YAD * IOPL

    	RETURN
    	END

     	SUBROUTINE LAYOUT_PACK_ENTRY( ISG )
C
C	THIS ROUTINE REPACKS LAYOUT ENTRIES AFTER ENTRY DELETE.
C
C	ISG: SEGMENT NUMBER TO BE STRIPPED.
C
     	INCLUDE 'LAYOUT.CBS'
     	LOGICAL LOGLID, LOGLDT, LOGFIL
C
C  PACK MAIN ENTRY LIST FIRST...
C
     	K = 0
     	IT= ISG
     	LOGLDT = .FALSE.
     	LOGLID = .FALSE.
     	LOGFIL = .FALSE.
     	DO  111 I=1, NLC
     	    IF( NSN(I).EQ.IT ) THEN
     		IF( NFL(I).GT.0 .AND. .NOT.LOGFIL ) LOGFIL = .TRUE.
     		GOTO 111
     	    ENDIF
     	    K = K + 1
     	    IF( K.EQ.I ) GOTO 111
     	    PEN(K) = PEN(I)
     	    NSN(K) = NSN(I)
     	    NFL(K) = NFL(I)
     	    RAN(K) = RAN(I)
     	    ZXF(K) = ZXF(I)
     	    ZYF(K) = ZYF(I)
     	    DO  J=1, MNS
     		P00(J,K) = P00(J,I)
     	    ENDDO
     	    JK = NID(K)
     	    JI = NID(I)
     	    NID(K) = NID(I)
     	    IF( JK.EQ.8 ) THEN		!TEXT ENTRY
     		IK = LAYOUT_LDT( K )
     		LDT(IK) = 0
     		LOGLDT = .TRUE.
     	    ENDIF
     	    IF( JK.EQ.9 .AND. P00(3,K).NE.2.0 ) THEN	!LINE ENTRY
     		IK = LAYOUT_LID( K )
     		LID(IK) = 0
     		LOGLID = .TRUE.
     	    ENDIF
     	    IF( JI.EQ.8 ) THEN		!TEXT ENTRY
     		IK = LAYOUT_LDT( I )
     		LDT(IK) = K
     	    ENDIF
     	    IF( JI.EQ.9 .AND. P00(3,I).NE.2.0 ) THEN	!LINE ENTRY
     		IK = LAYOUT_LID( I )
     		LID(IK) = K
     	    ENDIF
111	CONTINUE
     	IF( K.LT.NLC ) THEN
     	    DO  I= K+1, NLC
     		JI = NID(I)
     		IF( JI.EQ.8 ) THEN
     		    IK = LAYOUT_LDT( I )
     		    LDT(IK) = 0
     		    LOGLDT = .TRUE.
     		ENDIF
     		IF( JI.EQ.9 ) THEN
     		    IK = LAYOUT_LID( I )
     		    LID(IK) = 0
     		    LOGLID = .TRUE.
     		ENDIF
     	    ENDDO
     	ENDIF
     	NLC = K				!RESET ENTRY COUNTER
C
C  PACK TEXT/LINE/FILL ENTRY LIST NEXT...
C
     	IF( LOGLDT ) CALL LAYOUT_PACK_LDT
     	IF( LOGLID ) CALL LAYOUT_PACK_LID
     	IF( LOGFIL ) CALL LAYOUT_PACK_NFG

     	RETURN
     	END

     	SUBROUTINE LAYOUT_PACK_LDT

     	INCLUDE 'LAYOUT.CBS'

     	K = 0
     	DO  I=1, NTX
     	    IF( LDT(I).NE.0 ) THEN
     		K = K + 1
     		LDT(K) = LDT(I)
     		TXT(K) = TXT(I)
     	    ENDIF
     	ENDDO
     	NTX = K				!RESET TEXT ENTRY COUNTER

     	RETURN
     	END

     	SUBROUTINE LAYOUT_PACK_LID

     	INCLUDE 'LAYOUT.CBS'

     	K = 0
     	DO  I=1, NCU
     	    IF( LID(I).NE.0 ) THEN
     		K = K + 1
     		LID(K) = LID(I)
     		DO  J=1, MNP
     		    XSC(J,K) = XSC(J,I)
     		    YSC(J,K) = YSC(J,I)
     		ENDDO
     	    ENDIF
     	ENDDO
     	NCU = K				!RESET LINE ENTRY COUNTER

     	RETURN
     	END

     	SUBROUTINE LAYOUT_PACK_NFG
C
C	THIS ROUTINE REPACKS THE FILL GROUPS.
C
     	INCLUDE 'LAYOUT.CBS'
     	LOGICAL SKIP

     	IK = 1
     	DO  I=1, NFG
     	    SKIP = .TRUE.
     	    DO  J=1, NLC
     		IF( NFL(J).EQ.I ) THEN
     		    NFL(J) = IK
     		    SKIP = .FALSE.
     		ENDIF
     	    ENDDO
     	    IF( .NOT. SKIP ) THEN
     		NFP(IK) = NFP(I)
     		IK = IK+1
     	    ENDIF
     	ENDDO
     	NFG = IK-1

     	RETURN
     	END

     	SUBROUTINE LAYOUT_PACK_NLG
C
C	THIS ROUTINE RESORTS PICKED SEGMENT NUMBERS.
C
     	INCLUDE 'LAYOUT.CBS'

     	K = 0
     	DO I=1, NLG
     	   IF( NLS(I).GT.0 ) THEN
     	       K = K + 1
     	       NLS(K) = NLS(I)
     	   ENDIF
     	ENDDO
     	NLG = K

     	RETURN
     	END

     	SUBROUTINE LAYOUT_PACK_SEG
C
C	THIS ROUTINE REPACKS ENTRY SEGMENT NUMBERS
C	TO PREVENT SEGMENT NUMBER FROM GETTING TOO BIG.
C
     	INCLUDE 'LAYOUT.CBS'
     	LOGICAL SKIP

     	CALL IFANSI_VIDEO(30)
     	WRITE(6,1234)
1234	FORMAT(' Repacking layout entry pointers... please wait...',$)

     	IK = NSG+1
     	DO  I= NSG+1, KSG
     	    SKIP = .TRUE.
     	    DO  J=1, NLC
     		IS = NSN(J)
     		IF( IS.EQ.I ) THEN
     		    IF( IS.GT.IK ) THEN
     			NSN(I) = IK
     			CALL IFSEG_RENAME( IS, IK )
     			DO  K=1, NLG
     			    IF( NLS(K).EQ.IS ) THEN
     				NLS(K) = IK
     				GOTO 111
     			    ENDIF
     			ENDDO
     		    ENDIF
111     	    SKIP = .FALSE.
     	        ENDIF
     	    ENDDO
     	    IF( .NOT. SKIP ) IK = IK + 1
     	ENDDO
     	IF( .NOT. SKIP ) KSG = IK - 1

     	CALL IFANSI_VIDEO(47)
     	RETURN
     	END

     	SUBROUTINE LAYOUT_PICK( TEXT )
C
C  PROGRAMMER: R.S. ZHOU		JUNE-1986
C
C	THIS ROUTINE PICKS LAYOUT ENTRIES FOR TRANSFORMS AND
C	THEN HIGHLIGHTS THE SEGMENTS BY MAKING THEM BLINKING.
C
    	INCLUDE 'LAYOUT.CBS'
     	PARAMETER (NKS= 11)
     	CHARACTER KEY*3, KWD(NKS)*3, TEXT*(*)
     	DIMENSION KP(2), RP(2)

     	DATA KWD / 'ALL', 'BAR', 'BOX', 'ARC', 'OBL', 'POL',
     +  'ARR', 'SYM', 'TEX', 'LIN', 'FIL' /

     	NCH = IFMISC_NEXT( TEXT )
     	IF( NCH.EQ.0 ) THEN
     	    WRITE(6,1000)
1000	FORMAT(' Select what? ',$)
     	    READ(5,1001,END=999) TEXT
1001	FORMAT(A)
     	    IF( TEXT.EQ.' ' ) GOTO 999
     	    NCH = IFMISC_STRIP( TEXT )
     	ENDIF
     	NLGOLD = NLG			!SAVE THE CURRENT "NLG"
C
C  TRY TO DECODE ENTRY RANGE NUMBERS FIRST...
C
    	NCH = IFMISC_DECODE( TEXT, NCH, 2, RP, KP )
    	IF( NCH.EQ.0 ) GOTO 222
C
C  PICK ENTRIES BY RANGE POINTERS...
C
     	CALL LAYOUT_PICK_NUMBER( INT(RP(1)), INT(RP(2)) )
     	GOTO 999
C
C  TRY THE KEYWORD ENTRIES NEXT...
C
222	KEY = TEXT(1:3)
     	DO IK = 1, 7
     	   IF( KEY.EQ.KCS(IK) ) GOTO 333
     	ENDDO
     	DO IK = 1, NKS
     	   IF( KEY.EQ.KWD(IK) ) GOTO 334
     	ENDDO
     	GOTO 555
C
C  PICK BY KEYWORD...
C
333	CALL LAYOUT_PICK_COLOR( IK )
     	GOTO 999
334	IF( IK.EQ.1 ) THEN
     	    CALL LAYOUT_PICK_NUMBER(1,NLC)
     	ELSE IF( IK.GT.1 .AND. IK.LT.NKS ) THEN
     	    CALL LAYOUT_PICK_ID( IK-1 )
     	ELSE IF( IK.EQ.NKS ) THEN
     	    IF( NFG.EQ.0 ) THEN
     		GOTO 555
     	    ELSE
     		IK = 0
     		NCH = IFMISC_NEXT( TEXT )
     		IF( NCH.GT.0 ) THEN
     		    READ( TEXT(1:NCH), 444, ERR=555 ) IK
C     		    DECODE( NCH, 444, TEXT, ERR=555 ) IK
444		    FORMAT(I)
     		    IF( IK.LT.1 .OR. IK.GT.NFG ) GOTO 555
     		ENDIF
     		CALL LAYOUT_PICK_FILL( IK )
     	    ENDIF
     	ENDIF
999	IF( BLINK.GT.0 .AND. NLG.GT.NLGOLD ) CALL IFINIT_DUMP
     	RETURN
555	CALL IFMISC_OOPS( 3, 0, 0, TEXT )
     	RETURN
    	END

     	SUBROUTINE LAYOUT_PICK_ID( ID )
C
C	THIS ROUTINE SELECTS LAYOUT ENTRIES FOR TRANSFORM
C	BY ENTRY ID.
C
     	INCLUDE 'LAYOUT.CBS'

     	IT = ID
     	DO 111 I = 1, NLC
     	   IF( NID(I).NE.IT ) GOTO 111
     	   IS = NSN(I)
    	   DO K = 1, NLG
    	      IF( NLS(K).EQ.IS ) GOTO 111
    	   ENDDO
    	   NLG = NLG + 1
    	   NLS(NLG) = IS
     	   IF(BLINK.GT.0) CALL IFSEG_BLINK(IS)
111	CONTINUE

     	RETURN
     	END

     	SUBROUTINE LAYOUT_PICK_COLOR( IC )
C
C	THIS ROUTINE SELECTS LAYOUT ENTRIES FOR TRANSFORM
C	BY ENTRY COLOR/PEN CODE.
C
     	INCLUDE 'LAYOUT.CBS'

     	IPN = IC
     	DO 111 I = 1, NLC
     	   IF( PEN(I).NE.IPN ) GOTO 111
     	   IS = NSN(I)
    	   DO K = 1, NLG
    	      IF( NLS(K).EQ.IS ) GOTO 111
    	   ENDDO
    	   NLG = NLG + 1
    	   NLS(NLG) = IS
     	   IF(BLINK.GT.0) CALL IFSEG_BLINK(IS)
111	CONTINUE

     	RETURN
     	END

     	SUBROUTINE LAYOUT_PICK_NUMBER( I1, I2 )
C
C	THIS ROUTINE SELECTS LAYOUT ENTRIES FOR TRANSFORM
C	BY TWO NUMBERS (I1,I2) IDENTIFYING THE SEQUENCE RANGE.
C
     	INCLUDE 'LAYOUT.CBS'
C
C  CONVERT I1,I2 TO ABSOLUTE RANGE IF NOT SO...
C
     	IT1 = I1
     	IT2 = I2
     	IF( IT1.LE.0 ) IT1 = NLC + IT1
     	IF( IT1.LT.1 ) IT1 = 1
     	IF( IT1.GT.NLC ) IT1 = NLC
     	IT2 = IT1 + IT2
     	IF( IT2.LT.1 ) IT2 = 1
     	IF( IT2.GT.NLC ) IT2 = NLC
     	IF( IT2.LT.IT1 ) THEN
     	    ITT = IT2
     	    IT2 = IT1
     	    IT1 = ITT
     	ENDIF
C
C  START THE PICKING...
C
     	DO 111 I = IT1, IT2
     	   IS = NSN(I)
    	   DO K = 1, NLG
    	      IF( NLS(K).EQ.IS ) GOTO 111
    	   ENDDO
    	   NLG = NLG + 1
    	   NLS(NLG) = IS
     	   IF(BLINK.GT.0) CALL IFSEG_BLINK(IS)
111	CONTINUE

     	RETURN
     	END

     	SUBROUTINE LAYOUT_PICK_FILL( IFG )
C
C	THIS ROUTINE SELECTS LAYOUT ENTRIES FOR TRANSFORM
C	BY A FILL GROUP POINTER "IFG".
C	IF IFG=0, ALL EXISTING FILL GROUPS ARE SELECTED.
C
     	INCLUDE 'LAYOUT.CBS'

     	IT1 = IFG
     	IT2 = IT1
     	IF( IT1.EQ.0 ) THEN
     	    IT1 = 1
     	    IT2 = NFG
     	ENDIF

     	DO  111 I=IT1, IT2
     	    DO  J=1, NLC
     		IF( NFL(J).EQ.I ) THEN
     		    IS = NSN(J)
     		    DO  K=1, NLG
     			IF( NLS(K).EQ.IS ) GOTO 111
     		    ENDDO
     		    NLG = NLG + 1
     		    NLS(NLG) = IS
     		    IF(BLINK.GT.0) CALL IFSEG_BLINK(IS)
     		ENDIF
     	    ENDDO
111	CONTINUE

     	RETURN
     	END

     	SUBROUTINE LAYOUT_PICK_SEG( ISEG )
C
C  PROGRAMMER: R.S. ZHOU		JUNE-1986
C
C	THIS ROUTINE SELECTS A LAYOUT SEGMENT FOR TRANSFORM
C	BY ITS SEGMENT NUMBER "ISEG". IF "ISEG" IS ALREADY
C	PICKED, IT'S RELEASED FROM THE PICKED LIST.
C
    	INCLUDE 'LAYOUT.CBS'

     	IS = ISEG
     	DO K=1, NLG
     	   IF( NLS(K).EQ.IS ) GOTO 111
     	ENDDO
C
C  ADD A NEW ENTRY...
C
     	NLG = NLG + 1
     	NLS(NLG) = IS
     	IF( BLINK.GT.0 ) THEN
     	    CALL IFSEG_BLINK( IS )
     	    CALL IFINIT_DUMP
     	ENDIF
     	GOTO 999
C
C  RELEASE AN ENTRY...
C
111	IF( BLINK.GT.0 ) THEN
     	    CALL IFSEG_BLINK(-IS)
     	    CALL IFINIT_DUMP
     	ENDIF
     	DO I=K, NLG-1
     	   NLS(I) = NLS(I+1)
     	ENDDO
     	NLG = NLG - 1

999	RETURN
     	END

    	SUBROUTINE LAYOUT_ROTATE( RANG, XP, YP )
C
C  PROGRAMMER: R.S. ZHOU		JUNE-1986
C
C	THIS ROUTINE ROTATES PICKED LAYOUT SEGMENTS
C	BY "RANG" DEGREES WITH THE PIVOT AT (XP, YP).
C	IF XP OR YP .EQ. 99.0, THE SEGMENTS ARE ROTATED
C	AROUND THEIR OWN INDIVIDUAL PIVOTS.
C
    	INCLUDE		'LAYOUT.CBS'
     	LOGICAL		SKIP, ONE
     	DIMENSION	IR(9)
     	DATA		IR/ 6, 6, 6, 6, 5, 6, 4, 5, 6 /

     	IF( RANG.EQ.0.0 ) RETURN

    	XOP = XP
    	YOP = YP
    	ANG = AMOD( RANG, 360.0 )
    	CTH = COSD( ANG )
    	STH = SIND( ANG )
    	ONE = .FALSE.
    	IF( XOP.EQ.99.0 .OR. YOP.EQ.99.0 ) ONE = .TRUE.

    	DO  222 I = 1, NLG
     	    SKIP = .FALSE.
    	    IS = NLS(I)
     	    DO  111 J = 1, NLC
     		IF( NSN(J).NE.IS ) GOTO 111
     		IF( ONE ) THEN
     		    XPS = P00(1,J)
     		    YPS = P00(2,J)
     		ELSE
     		    XP1 = P00(1,J)
     		    YP1 = P00(2,J)
     		    XPS = XOP+(XP1-XOP)*CTH-(YP1-YOP)*STH
     		    YPS = YOP+(YP1-YOP)*CTH+(XP1-XOP)*STH
     		    P00(1,J) = XPS
     		    P00(2,J) = YPS
     		ENDIF
     		RAN(J) = AMOD( RAN(J)+ANG, 360.0 )
     		IF(.NOT.SKIP) CALL IFSEG_TRAN( IS, XPS, YPS,
     1                             RAN(J), ZXF(J), ZYF(J) )
     		ITT = IR(NID(J))
     		P00(ITT,J) = AMOD( P00(ITT,J)+ANG, 360.0 )
     		IF( NFL(J).EQ.0 ) GOTO 222
     		SKIP = .TRUE.
111	    CONTINUE
222	CONTINUE

     	CALL IFINIT_DUMP

    	RETURN
    	END

    	SUBROUTINE LAYOUT_ROTATE_ANGLE( IP, ANG )
C
C  THIS ROUTINE ASSIGNS DIFFERENT INCREMENTAL ROTATION ANGLES "ANG"
C  ACCORDING TO "IP" FOR LAYOUT PLOTTING
C
    	SAVE IOPL

    	IOP = IP
    	ANG = 0.0
    	IF( IOP.EQ.1 ) THEN
     	    ANG = 1.0
     	ELSE IF( IOP.EQ.2 ) THEN
     	    ANG = 10.0
     	ELSE IF( IOP.EQ.3 ) THEN
     	    ANG = -1.0
     	ELSE IF( IOP.EQ.4 ) THEN
     	    ANG = 2.0
     	ELSE IF( IOP.EQ.5 ) THEN
    	    IF(IOPL.NE.10) THEN
    	        IOPL = 10
    	    ELSE
    	        IOPL = 1
    	    ENDIF
    	ELSE IF( IOP.EQ.6 ) THEN
     	    ANG = -2.0
     	ELSE IF( IOP.EQ.7 ) THEN
     	    ANG = 5.0
     	ELSE IF( IOP.EQ.8 ) THEN
     	    ANG = -10.0
     	ELSE IF( IOP.EQ.9 ) THEN
     	    ANG = -5.0
     	ELSE IF( IOP.EQ.0 ) THEN
    	    IF(IOPL.NE.25) THEN
    	        IOPL = 25
    	    ELSE
    	        IOPL = 10
    	    ENDIF
    	ENDIF
    	IF( IOPL.EQ.0 ) IOPL = 1
    	ANG = ANG * IOPL

    	RETURN
    	END
    	SUBROUTINE LAYOUT_SET( TEXT )
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY,  JUN-1986
C
C	THIS ROUTINE SETS SOME LAYOUT ATTRIBUTES.
C
    	CHARACTER KEY*3, TEXT*(*)

    	NCH = IFMISC_NEXT( TEXT )
     	IF( NCH.EQ.0 ) THEN
     	    CALL IFMISC_OOPS( 4, 0, 0, ' ' )
     	    RETURN
     	ENDIF
    	KEY = TEXT(1:3)
C
C  SET GIN PICK APERTURE...
C
     	IF( KEY .EQ. 'APE' ) THEN
     	    CALL LAYOUT_SET_PAP( TEXT )
C
C  SET BLINKING FEATURE...
C
     	ELSE IF( KEY .EQ. 'BLI' ) THEN
     	    CALL LAYOUT_SET_BLINK( 1 )
C
C  SET DIALOG AREA LINES...
C
     	ELSE IF( KEY .EQ. 'DAL' ) THEN
     	    CALL LAYOUT_SET_LINE( TEXT )
C
C  SET THE 8 FUNCTION KEYS...
C
     	ELSE IF( KEY .EQ. 'FKE' ) THEN
     	    CALL LAYOUT_SET_FKEYS
C
C  SET REFERENCE ORIGIN...
C
     	ELSE IF( KEY .EQ. 'ORI' ) THEN
     	    CALL LAYOUT_SET_ORIGIN( TEXT )
C
C  SET LAYOUT SCALE...
C
     	ELSE IF( KEY .EQ. 'SCA' ) THEN
     	    CALL LAYOUT_SET_SCALE( TEXT )
C
C  SET CHORD ANGLE...
C
     	ELSE IF( KEY .EQ. 'CHO' ) THEN
     	    CALL LAYOUT_SET_CHORD( TEXT )
C
C  SET PROGRAM FRAME ON/OFF...
C
     	ELSE IF( KEY .EQ. 'FRA' ) THEN
     	    CALL LAYOUT_SET_FRAME
C
C  SET PLOTTING STYLUS...
C
     	ELSE IF( KEY .EQ. 'LIN' ) THEN
     	    CALL LAYOUT_SET_STYLUS( TEXT )
C
C  SET DIFILE  OR HARDCOPY ON/OFF...
C
     	ELSE IF( KEY .EQ. 'HAR' ) THEN
     	    CALL LAYOUT_SET_HARDCOPY( TEXT )
     	ELSE
     	    CALL IFMISC_OOPS( 3, 0, 0, TEXT )
     	ENDIF
     	RETURN
    	END

     	SUBROUTINE LAYOUT_SET_BLINK( ICODE )
C
C	THIS ROUTINE SETS ENTRY HIGHLIGHTING FOR LAYOUT.
C
C	ICODE = 0: RELEASE ALL PICKED ENTRIES WITH BLINK OFF.
C	ICODE = 1: FLIP-FLOP BLINKING OF PICKED ENTRIES.
C
     	INCLUDE 'LAYOUT.CBS'

    	IF( ICODE.NE.0 ) THEN
     	    BLINK = -BLINK
    	    DO I=1, NLG
     		CALL IFSEG_BLINK( BLINK*NLS(I) )
    	    ENDDO
    	ELSE
     	    IF( BLINK.GT.0 ) THEN
     		DO I=1, NLG
     		   CALL IFSEG_BLINK( -NLS(I) )
     		ENDDO
     	    ENDIF
    	    NLG = 0
    	ENDIF

     	CALL IFINIT_DUMP

999	RETURN
    	END

     	SUBROUTINE LAYOUT_SET_LINE( TEXT )
C
C  THIS ROUTINE SETS THE DIALOGUE AREA LINES
C
     	CHARACTER TEXT*(*)

     	NCH = IFMISC_NEXT( TEXT )
    	DLINE = 3.0
    	IF( NCH.GT.0 ) THEN
    	    NCH = IFMISC_DECODE( TEXT, NCH, 1, DLINE, IDUM )
    	    IF( NCH.EQ.0 ) THEN
     		CALL IFMISC_OOPS( 3, 0, 0, TEXT )
     	    ELSE
    	        IF( DLINE.GT.30.0 ) DLINE = 30.0
     	    ENDIF
    	ENDIF
    	CALL IFINIT_LINE( -INT(DLINE) )

     	RETURN
     	END

     	SUBROUTINE LAYOUT_SET_FKEYS
C
C	THIS ROUTINE DEFINES/UNDEFINES F KEYS.
C
     	INCLUDE 'LAYOUT.CBS'

     	FKEYS = -FKEYS
     	IF( FKEYS.GT.0 ) CALL IFMAC_MACROS( 1 )
     	IF( FKEYS.LT.0 ) CALL IFMAC_MACROS( 0 )

     	RETURN
     	END

     	SUBROUTINE LAYOUT_SET_FRAME
C
C	THIS ROUTINE SETS THE VISIBILITY OF PROGRAM FRAME.
C
     	INCLUDE 'LAYOUT.CBS'

     	FRAME = -FRAME
     	CALL IFSEG_HIDE( 1*FRAME )	!FRAME
     	CALL IFSEG_HIDE( 2*FRAME )	!REFERENCE ORIGIN
     	CALL IFSEG_HIDE( 3*FRAME )	!WORKING ORIGIN
     	CALL IFSEG_HIDE( 4*FRAME )	!FKEY DEFINITIONS
     	IF( ISCP.NE.0 ) CALL IFSEG_HIDE( 5*FRAME )	!LAYOUT-SCALE
	CALL IFINIT_DUMP

     	RETURN
     	END

     	SUBROUTINE LAYOUT_SET_ORIGIN( TEXT )
C
C  THIS ROUTINE SETS THE REFERENCE ORIGIN FOR LAYOUT.
C
     	INCLUDE 'LAYOUT.CBS'
     	CHARACTER TEXT*(*)
     	DIMENSION RP(2), KP(2)

     	NCH = IFMISC_NEXT( TEXT )
     	IF( NCH.EQ.0 ) THEN
    	    XORS = 0.0
    	    YORS = 0.0
    	ELSE
    	    NCH = IFMISC_DECODE( TEXT, NCH, 2, RP, KP )
    	    IF( NCH.EQ.0 ) THEN
     		CALL IFMISC_OOPS( 3, 0, 0, TEXT )
     	    ELSE
     		IF( KP(1).EQ.1 ) XORS = RP(1)
     		IF( KP(2).EQ.1 ) YORS = RP(2)
     	    ENDIF
    	ENDIF
    	CALL IFSEG_MOVE( 3, XORS, YORS )
     	IF( ISCP.GT.0 ) CALL LAYOUT_SET_RULER
    	CALL IFINIT_DUMP

     	RETURN
     	END

    	SUBROUTINE LAYOUT_SET_SCALE( TEXT )
C
C  PROGRAMMER: R.S. ZHOU		MARCH-1986
C
C	THIS ROUTINE PUTS ON THE BACKGROUND SCALE
C	WHICH HELPS CURSOR LOCATION FOR LAYOUT.
C
    	INCLUDE 'LAYOUT.CBS'
     	CHARACTER KEY*3, TEXT*(*)
     	DIMENSION RP(5), KP(5)

     	NCH = IFMISC_NEXT( TEXT )
     	IF( NCH.GT.0 ) THEN
     	    KEY = TEXT(1:3)
     	    DO IK=1, 7
     	       IF( KEY.EQ.KCS(IK) ) GOTO 100
     	    ENDDO
     	    IR = 1
     	    GOTO 200
100	    IR = 2
     	    ISCP = IK
     	    KP(1) = 0
     	    NCH = IFMISC_NEXT( TEXT )
200	    IF( NCH.GT.0 ) THEN
     	        NCH = IFMISC_DECODE(TEXT,NCH,6-IR,RP(IR),KP(IR))
    	        IF( NCH.EQ.0 ) GOTO 888
    	    IF(KP(1).EQ.1.AND.RP(1).GT.0..AND.RP(1).LT.8.00) ISCP=RP(1)
    	    IF(KP(2).EQ.1.AND.RP(2).GT.0..AND.RP(2).LT.100.) MAXS=RP(2)
    	    IF(KP(3).EQ.1.AND.RP(3).GT.0..AND.RP(3).LT.100.) MIXS=RP(3)
    	    IF(KP(4).EQ.1.AND.RP(4).GT.0..AND.RP(4).LT.100.) MAYS=RP(4)
    	    IF(KP(5).EQ.1.AND.RP(5).GT.0..AND.RP(5).LT.100.) MIYS=RP(5)
     	    ENDIF
     	ELSE IF( ISCP.EQ.0 ) THEN	!FLIP ON
    	    ISCP = 1
     	ELSE				!FLIP OFF
    	    ISCP = 0
    	ENDIF
     	CALL LAYOUT_SET_RULER
    	CALL IFINIT_DUMP
     	GOTO 999
888	CALL IFMISC_OOPS( 3, 0, 0, TEXT )
999	RETURN
    	END

    	SUBROUTINE LAYOUT_SET_RULER
C
C  PROGRAMMER: R.S. ZHOU	MARCH-1986
C
C	THIS ROUTINE PUTS ON THE BACKGROUND SCALE
C	THROUGH THE CURRENT GRAPH ORIGIN FOR LAYOUT PLOTTING.
C
     	INCLUDE 'LAYOUT.CBS'

     	IF( ISCP.EQ.0 ) THEN
     	    CALL IFSEG_OPEN( -5 )
     	    RETURN
     	ENDIF
C
C  REORIGIN TO (XORS,YORS)...
C
     	CALL IFPLOT( XORS, YORS, -9 )
     	CALL IFSEG_OPEN(-5 )
     	CALL IFSEG_OPEN( 5 )
     	CALL IFSPEN( ISCP )
C
C  INITIALIZE...
C
     	MAX = IABS(MAXS)
     	MIX = IABS(MIXS)
     	MAY = IABS(MAYS)
     	MIY = IABS(MIYS)
     	XSP = 10.0/(MAX*MIX)
     	YSP = 10.0/(MAY*MIY)
     	IF( XSP.LT.0.01 ) XSP = 0.01
     	IF( YSP.LT.0.01 ) YSP = 0.01
        XPS1 = -XORG-XORS
    	XPS2 = 9.97-XORG-XORS
    	YPS1 = -YORG-YORS
    	YPS2 = 7.64-YORG-YORS
C
C  PUT A CROSS THROUGH THE ORIGIN...
C
     	CALL IFPLOT( XPS1, 0.0, 3 )
     	CALL IFPLOT( XPS2, 0.0, 2 )
     	CALL IFPLOT( 0.0, YPS1, 3 )
     	CALL IFPLOT( 0.0, YPS2, 2 )
C
C  DRAW THE SCALE...
C
    	XPS = 0.0
    	IMK = 0
1	XPS = XPS - XSP
    	IF( XPS.LT.XPS1 ) GOTO 2
    	ADD = 0.02
    	IMK = IMK + 1
    	IF( IMK.EQ.MIX ) THEN
    	    ADD = 0.06
    	    IMK = 0
    	ENDIF
     	CALL IFPLOT( XPS, -ADD, 3 )
     	CALL IFPLOT( XPS,  ADD, 2 )
    	GOTO 1

2	XPS = 0.0
    	IMK = 0
3	XPS = XPS + XSP
    	IF( XPS.GT.XPS2 ) GOTO 4
    	ADD = 0.02
    	IMK = IMK + 1
    	IF( IMK.EQ.MIX ) THEN
    	    ADD = 0.06
    	    IMK = 0
    	ENDIF
    	CALL IFPLOT( XPS, -ADD, 3 )
     	CALL IFPLOT( XPS,  ADD, 2 )
    	GOTO 3

4	YPS = 0.0
    	IMK = 0
5	YPS = YPS - YSP
    	IF( YPS.LT.YPS1 ) GOTO 6
    	ADD = 0.02
    	IMK = IMK + 1
    	IF( IMK.EQ.MIY ) THEN
    	    ADD = 0.06
    	    IMK = 0
    	ENDIF
     	CALL IFPLOT( -ADD, YPS, 3 )
     	CALL IFPLOT(  ADD, YPS, 2 )
    	GOTO 5

6	YPS = 0.0
    	IMK = 0
7	YPS = YPS + YSP
    	IF( YPS.GT.YPS2 ) GOTO 999
    	ADD = 0.02
    	IMK = IMK + 1
    	IF( IMK.EQ.MIY ) THEN
    	    ADD = 0.06
    	    IMK = 0
    	ENDIF
     	CALL IFPLOT( -ADD, YPS, 3 )
     	CALL IFPLOT(  ADD, YPS, 2 )
    	GOTO 7

999	CALL IFSEG_OPEN( 0 )
     	CALL IFPLOT( -XORS, -YORS, -9 )

     	RETURN
    	END

    	SUBROUTINE LAYOUT_SET_CHORD( TEXT )
C
C  PROGRAMMER: R.S. ZHOU		MARCH-1986
C
C	THIS ROUTINE SETS THE CHORD ANGLE IN DEGREES
C	FOR DRAWING ARCS.
C
     	INCLUDE 'LAYOUT.CBS'
     	CHARACTER TEXT*(*)

     	NCH = IFMISC_NEXT( TEXT )
    	IF( NCH.GT.0 ) THEN
    	    NCH = IFMISC_DECODE( TEXT, NCH, 1, DUMY, IDUM )
    	    IF( NCH.EQ.0 ) THEN
     		CALL IFMISC_OOPS( 3, 0, 0, TEXT )
     	    ELSE IF( DUMY.GE.0.5 .AND. DUMY.LE.15.0 ) THEN
     		CHORD = DUMY
    	    ENDIF
     	ENDIF

     	RETURN
     	END

     	SUBROUTINE LAYOUT_SET_PAP( TEXT )
C
C  THIS ROUTINE SETS THE GIN PICK APERTURE.
C
     	INCLUDE 'LAYOUT.CBS'
     	CHARACTER TEXT*(*)

     	NCH = IFMISC_NEXT( TEXT )
     	APER = 0.1
    	IF( NCH.GT.0 ) THEN
    	    NCH = IFMISC_DECODE( TEXT, NCH, 1, APER, IDUM )
    	    IF( NCH.EQ.0 ) THEN
     		CALL IFMISC_OOPS( 3, 0, 0, TEXT )
     	    ELSE
     		IF( APER.LT.0.01 ) APER = 0.01
     		IF( APER.GT.1.00 ) APER = 1.00
     	    ENDIF
    	ENDIF
    	CALL IFGIN_PAP( APER )
    	CALL IFINIT_DUMP

     	RETURN
     	END

     	SUBROUTINE LAYOUT_SET_STYLUS( TEXT )
C
C	THIS ROUTINE SETS PLOTTING STYLUS --- LINETYPE AND LINEWIDTH.
C
     	INCLUDE 'LAYOUT.CBS'
     	CHARACTER TEXT*(*)
     	DIMENSION RP(2), KP(2)

     	NCH = IFMISC_NEXT( TEXT )
     	IF( NCH.GT.0 ) THEN
    	    NCH = IFMISC_DECODE( TEXT, NCH, 2, RP, KP )
    	    IF( NCH.EQ.0 ) THEN
     		CALL IFMISC_OOPS( 3, 0, 0, TEXT )
     	    ELSE
     		IF( KP(1).EQ.1 ) THEN
     		    IF( RP(1).GT.0.0.AND.RP(1).LT.11.0 ) LTYP=RP(1)
     		ENDIF
     		IF( KP(2).EQ.1 ) THEN
     		    IF( RP(2).GT.0.0.AND.RP(2).LT.11.0 ) LWID=RP(2)
     		ENDIF
     	    ENDIF
    	ENDIF

     	RETURN
     	END

    	SUBROUTINE LAYOUT_SET_VISS
C
C  PROGRAMMER: R.S. ZHOU		JUNE-1986
C
C	THIS ROUTINE SETS PICKED LAYOUT SEGMENT VISIBILITY.
C
    	INCLUDE 'LAYOUT.CBS'

     	LVISS = -LVISS
    	DO I=1, NLG
     	    CALL IFSEG_HIDE( LVISS*NLS(I) )
    	ENDDO
     	CALL IFINIT_DUMP

    	RETURN
    	END

     	SUBROUTINE LAYOUT_SET_HARDCOPY( TEXT )
C
C	THIS ROUTINE SETS FLAG FOR PREPARATION OF HARDCOPY DIFILE.
C
     	CHARACTER TEXT*(*)

     	NCH = IFMISC_NEXT( TEXT )
     	IF( NCH.GT.0 ) THEN
     	    IF( TEXT(1:2).EQ.'ON' ) THEN
     		CALL IFINIT_DIF(1)
     		DIFF = 1
     	    ELSE IF( TEXT(1:3).EQ.'OFF' ) THEN
     		CALL IFINIT_DIF(0)
     		DIFF = 0
     	    ELSE
     		CALL IFMISC_OOPS( 3, 0, 0, TEXT )
     	    ENDIF
     	ELSE
     		CALL IFMISC_OOPS( 4, 0, 0, ' ' )
     	ENDIF
     	RETURN
     	END

     	FUNCTION LAYOUT_START(ICODE,XO,YO,FNAME)
C
C	THIS ROUTINE INITIALIZES A LAYOUT SESSION.
C
     	INCLUDE  'LAYOUT.CBS'
     	CHARACTER FNAME*(*)
     	LOGICAL   YES
     	DATA KCS / 'WHI', 'RED', 'GRE', 'BLU', 'CYA', 'PUR', 'YEL' /

    	MODE  = ICODE
     	XORG  = XO
     	YORG  = YO
     	LOFILE= FNAME
     	IF( MODE.EQ.0 ) THEN	!LAYOUT OVERLAY ONLY
     	    CALL LAYOUT_INIT
     	    INQUIRE( FILE=LOFILE, EXIST=YES )
     	    IF(YES) CALL LAYOUT_IO( 1, LOFILE )
     	    LAYOUT_START = 1
     	    RETURN
     	ENDIF
C
C  INTERACTIVE LAYOUT SESSION...
C
     	CALL IFANSI_CLEAR(2)	!CLEAR BOTTOM SCREEN
     	CRT = IFINIT_ITM(0)	!GET THE TYPE OF TERMINAL
    	IF( MODE.EQ.1 .AND. CRT.GT.1 ) THEN
     	    CALL IFINIT_LINE(3)
     	    CALL IFINIT_PLOT( XORG, YORG, LOFILE )
    	    CALL LAYOUT_INIT
    	ELSE
     	    CALL IFINIT_LINE(-3)
     	    IF( INI.NE.999 ) CALL LAYOUT_INIT
     	ENDIF
     	IF( CRT.EQ.2 ) THEN
     	    CALL IFMAC_MACROS(1)
     	    CALL IFINIT_FKEYS( 'VISION', 'RELEASE', 'REFRESH',
     +     'GIN-FLIP', 'MOVE', 'ROTATE', 'ZOOM', 'COLOR' )
     	ENDIF
     	IF( MODE.EQ.1 ) CALL LAYOUT_DCL
     	WRITE(6,1000)
 1000	FORMAT(/'_LAYOUT_COM> Type HELP (or press PF2)',
     +' if you need any.')
     	LAYOUT_START = 0
     	RETURN
     	END

     	SUBROUTINE LAYOUT_DIALOG
C
C	THIS ROUTINE CARRIES ON A LAYOUT SESSION.
C
    	INCLUDE  'LAYOUT.CBS'
    	CHARACTER KEY*3, KYE*2, TEXT*80
    	EQUIVALENCE (TEXT(1:1),KEY), (TEXT(1:1),KYE)
C
C  START THE INTERACTIVE LOOP...
C
     	IERR= 0		!INPUT ERROR CODE
    	IQQ = 0		!TRANSFORMATION CODE
     	JQQ = 0		!0=COMMAND MODE, >0=ENTRY MODE
    	NCH = 0
111	IF( JQQ.EQ.0 ) THEN
     	    WRITE(6,1000)
     	ELSE IF( JQQ.EQ.1 ) THEN
     	    WRITE(6,1101)
     	ELSE IF( JQQ.EQ.2 ) THEN
     	    WRITE(6,1102)
     	ELSE IF( JQQ.EQ.3 ) THEN
     	    WRITE(6,1103)
     	ELSE IF( JQQ.EQ.4 ) THEN
     	    WRITE(6,1104)
     	ELSE IF( JQQ.EQ.5 ) THEN
     	    WRITE(6,1105)
     	ELSE IF( JQQ.EQ.6 ) THEN
     	    WRITE(6,1106)
     	ELSE IF( JQQ.EQ.7 ) THEN
     	    WRITE(6,1107)
     	ENDIF
1000	FORMAT(' LAYOUT_COM> ',$)
1101	FORMAT(' FORMAT: PEN,X,Y,LENGTH,HEIGHT,BORDER,ROTATE,FILL'/
     +,' LAYOUT_ENTRY_BAR> ',$)
1102	FORMAT(' FORMAT: PEN,X,Y,LENGTH,HEIGHT,BORDER,ROTATE,FILL'/
     +,' LAYOUT_ENTRY_BOX> ',$)
1103	FORMAT(' FORMAT: PEN,X,Y,R1,R2,BORDER,ROTATE,ANGLE1,ANGLE2,FILL'/
     +,' LAYOUT_ENTRY_ARC> ',$)
1104	FORMAT(' FORMAT: PEN,X,Y,D1,D2,BORDER,ROTATE,FILL'/
     +,' LAYOUT_ENTRY_OBLIQUE> ',$)
1105	FORMAT(' FORMAT: PEN,X,Y,SIZE,BORDER,ROTATE,EDGES,FILL'/
     +,' LAYOUT_ENTRY_POLYGON> ',$)
1106	FORMAT(' FORMAT: PEN,X,Y,LENGTH,HEIGHT,HEAD,ROTATE,TYPE'/
     +,' LAYOUT_ENTRY_ARROWHEAD> ',$)
1107	FORMAT(' FORMAT: PEN,X,Y,SIZE,ROTATE,NUMBER'/
     +,' LAYOUT_ENTRY_SYMBOL> ',$)

     	IOP = IFQIOW( IDUM, TEXT )
     	IF( IOP.EQ.26 ) GOTO 888
     	IF( NCH.LT.0 ) CALL IFINIT_LINE(-3)
    	IF( IDUM.EQ.0 ) THEN
     	    NCH = 0
     	    GOTO 111
     	ENDIF
    	NCH = IFMISC_STRIP( TEXT )
    	CALL  IFMISC_UPPER( NCH, TEXT )
     	IF( JQQ.GT.0 ) GOTO 555
C
C  	-----	COMMAND INPUT    ---------
C
     	IERR = 2
     	IF( NCH.EQ.1 ) GOTO 333
     	IF( KYE.EQ.'EX' .OR. KYE.EQ.'QU' ) GOTO 888

     	IF( KEY.EQ.'HEL' .OR. (IOP.EQ.27.AND.KYE.EQ.'OQ') ) THEN
     	    CALL LAYOUT_HELP( TEXT )
     	    NCH = -1
     	    GOTO 111
     	ENDIF
C
C  CHECK FOR ENTRY MODE...
C
     	IF( KEY.EQ.'ENT' ) THEN
     	    IERR = 17
     	    IF( CRT.EQ.1 ) GOTO 777
     	    JQQ = LAYOUT_MODE( TEXT )
     	    IF( JQQ.GT.0 ) THEN
     		IQQ = 0
     		IF( NLG.GT.0 ) CALL LAYOUT_SET_BLINK(0)
     		NLCOLD = NLC
     		CALL IFANSI_VIDEO(30)
     		WRITE(6,1200)
 1200	FORMAT(' *** PF1/F5>EXIT, PF2/F6>HELP, PF4/F8>DELETE ***')
     		CALL IFANSI_VIDEO(47)
    	    ENDIF
     	    GOTO 111
     	ENDIF
C
C  CHECK FOR SOME LAYOUT COMMANDS...
C
     	IQQ = 0
     	IDUM= LAYOUT_COM( IOP, TEXT )
    	IF( IDUM.NE.0 ) GOTO 111
C
C  CHECK FOR TRANSFORM KEYWORDS...
C
     	IF( KEY.EQ.'VIS' .OR. KYE.EQ.'F1' ) THEN
     	    IQQ = 1
     	ELSE IF( KEY.EQ.'COD' ) THEN
     	    IQQ = 2
     	ELSE IF( KEY.EQ.'LIN' ) THEN
     	    IQQ = 3
     	ELSE IF( KEY.EQ.'FIL' ) THEN
     	    IQQ = 4
     	ELSE IF( KEY.EQ.'MOV' .OR. KYE.EQ.'F5' ) THEN
     	    IQQ = 5
     	ELSE IF( KEY.EQ.'ROT' .OR. KYE.EQ.'F6' ) THEN
     	    IQQ = 6
     	ELSE IF( KEY.EQ.'ZOO' .OR. KYE.EQ.'F7' ) THEN
     	    IQQ = 7
     	ELSE IF( KEY.EQ.'COL' .OR. KYE.EQ.'F8' ) THEN
     	    IQQ = 8
     	ELSE
     	    IERR = 2
     	    GOTO 777
     	ENDIF
     	IERR = 17
     	IF( CRT.NE.2 ) GOTO 777		!INVALID TERMINAL
     	IERR = 15
     	IF( NLC.EQ.0 ) GOTO 777		!NOTHING ON THE SCREEN
     	IERR = 16
     	IF( NLG.EQ.0 ) GOTO 777		!NO PICKED ENTRIES
     	IF( IQQ.EQ.1 ) THEN		!FLIP VISIBILITY
     	    CALL LAYOUT_SET_VISS
     	    GOTO 111
     	ENDIF
C
C  DECODE COMMAND PARAMETERS IF AT ALL...
C
     	NCH = IFMISC_NEXT( TEXT )
     	IF( NCH.EQ.0 ) GOTO 111
     	IDUM = LAYOUT_PFS( TEXT, PF1, PF2, PF3, PF4 )
     	IF( IDUM.EQ.0 ) GOTO 111	!ERROR PARAMETERS
     	GOTO 444
C
C  TRANSFORM BIT BY BIT...
C
333	IF( IQQ.EQ.0 ) GOTO 777
     	READ( TEXT(1:1), 334, ERR=777 ) IOP
C     	DECODE( 1, 334, TEXT(1:1), ERR=777 ) IOP
334	FORMAT(I1)
     	IF( IQQ.LT.5 .OR. IQQ.EQ.8 ) THEN
     	    PF1 = IOP
     	ELSE
     	    IF( IQQ.EQ.5 ) THEN
     		CALL LAYOUT_MOVE_WHERE( IOP, PF1, PF2 )
     	    ELSE IF( IQQ.EQ.6 ) THEN
     		CALL LAYOUT_ROTATE_ANGLE( IOP, PF1 )
     	    ELSE IF( IQQ.EQ.7 ) THEN
     		CALL LAYOUT_ZOOM_SCALE( IOP, PF1, PF2 )
     	    ENDIF
     	    IF( IOP.EQ.0 .OR. IOP.EQ.5 ) GOTO 111
     	ENDIF
C
C  PERFORM THE TRANSFORMATION SPECIFIED PREVIOUSLY...
C
444	IF( IQQ.EQ.2 ) THEN
     	    CALL LAYOUT_CODE( PF1 )
     	ELSE IF( IQQ.EQ.3 ) THEN
     	    CALL LAYOUT_LINE( PF1 )
     	ELSE IF( IQQ.EQ.4 ) THEN
     	    CALL LAYOUT_FILL( PF1 )
     	ELSE IF( IQQ.EQ.5 ) THEN
     	    CALL LAYOUT_MOVE( PF1, PF2 )
     	ELSE IF( IQQ.EQ.6 ) THEN
     	    CALL LAYOUT_ROTATE( PF1, PF2+XORS, PF3+YORS )
     	ELSE IF( IQQ.EQ.7 ) THEN
     	    CALL LAYOUT_ZOOM( PF1, PF2, PF3+XORS, PF4+YORS )
     	ELSE IF( IQQ.EQ.8 ) THEN
     	    CALL LAYOUT_COLOR( PF1 )
     	ENDIF
    	GOTO 111
C
C  	------    ENTRY INPUT	 -------
C
555	IF( KYE.EQ.'EX' .OR. KYE.EQ.'QU'
     + .OR. KYE.EQ.'F5' .OR. (IOP.EQ.27.AND.KYE.EQ.'OP') ) THEN
     	    JQQ = 0
     	ELSE IF( KYE.EQ.'F6' .OR. KEY.EQ.'HEL'
     + .OR. (IOP.EQ.27.AND.KYE.EQ.'OQ') ) THEN
     	    CALL LAYOUT_HELP_ENTRY( JQQ )
     	    NCH = -1
     	ELSE IF( KYE.EQ.'F8' .OR. KEY.EQ.'DEL'
     + .OR. (IOP.EQ.27.AND.KYE.EQ.'OS') ) THEN
     	    IF( NLC.GT.NLCOLD ) THEN
     		CALL IFSEG_OPEN( -NSN(NLC) )
     		CALL IFINIT_DUMP
     		NLC = NLC - 1
     	    ELSE
     		CALL IFMISC_OOPS( 15, 0, 0, ' ' )
     	    ENDIF
     	ELSE IF( IOP.EQ.27 ) THEN
     	    CALL IFMISC_OOPS( 5, 0, 0, ' ' )
     	ELSE
C
C  ADD AN ENTRY ...
     	    IERR = 28
     	    IF( NLC.EQ.MNN ) GOTO 777	!MAX ENTRY COUNT HIT
     	    CALL LAYOUT_ENTRY( TEXT, JQQ )
     	ENDIF
     	GOTO 111
C
C  ERROR INPUT ...
C
777	IF( IERR.EQ.2 .OR. IERR.EQ.17 ) THEN
     	     CALL IFMISC_OOPS( IERR, 0, 0, TEXT )
     	ELSE
     	     CALL IFMISC_OOPS( IERR, 0, 0, ' ' )
     	     IF( IERR.EQ.28 ) JQQ = 0
     	ENDIF
    	GOTO 111
C
C  EXIT FROM LAYOUT...
C
888	IF( JQQ.GT.0 ) THEN
     	    JQQ = 0
     	    GOTO 111
     	ENDIF
     	IF( NLC.GT.0 .AND. KYE.EQ.'QU' ) THEN
     	    WRITE(6,3000)
3000	    FORMAT(' Abort the layout? (Y/N) [Y] ',$)
     	    READ(5,3001,END=889) KYE
3001	    FORMAT(A)
     	    IF( KYE(1:1).EQ.'N' .OR. KYE(1:1).EQ.'n' ) GOTO 111
     	ELSE
     	    KYE = 'EX'
     	ENDIF
889	IF( NLG.GT.0 ) CALL LAYOUT_SET_BLINK(0)
     	IF( CRT.EQ.1 .OR. KYE.NE.'EX' ) GOTO 999
C
C  SAVE THE LAYOUT...
C
     	IF( MODE.EQ.1 ) THEN
     	    TEXT = 'SAVE'
     	ELSE
     	    TEXT = 'SAVE '
     	    TEXT(6:) = LOFILE
     	ENDIF
     	CALL LAYOUT_SAVE( TEXT )
999	IF( CRT.EQ.2 ) CALL IFSEG_OPEN(-4)
     	CALL IFMAC_MACROS(0)
     	RETURN
    	END

    	SUBROUTINE LAYOUT_INIT
C
C	THIS ROUTINE INITIALIZES THE COMMON BLACKS FOR LAYOUT.
C
    	INCLUDE 'LAYOUT.CBS'

     	INI = 999
    	NLC = 0
     	KSG = NSG
    	NLG = 0
     	NFG = 0
     	NTX = 0
     	NCU = 0
     	LTYP = 1
     	LWID = 1
     	FKEYS = 1
     	BLINK = 1
     	FRAME = 1
     	LVISS = 1
     	DIFF = 1
    	ISCP = 0
    	MAXS = 10
     	MIXS = 10
     	MAYS = 10
     	MIYS = 10
    	XORS = 0.0
    	YORS = 0.0
     	CHORD = 10.0
     	PLOTID = ' '

999	RETURN
    	END

    	FUNCTION LAYOUT_PFS( TEXT, PF1, PF2, PF3, PF4 )
C
C  PROGRAMMER: R.S. ZHOU	MAY-1986
C
C	THIS ROUTINE DECODES THE TRANSFORMATION PARAMETERS FOR LAYOUT.
C
     	INCLUDE 'LAYOUT.CBS'
    	CHARACTER KEY*3, TEXT*(*)
    	DIMENSION KP(4), RP(4)

    	PF1 = 0.0
    	PF2 = 0.0
    	PF3 = 0.0
    	PF4 = 0.0
     	NCH = IFMISC_LENG( TEXT )
     	NCH = IFMISC_DECODE( TEXT, NCH, 4, RP, KP )
     	IF( NCH.GT.0 ) THEN
     	    IF( KP(1).EQ.1 ) PF1 = RP(1)
     	    IF( KP(2).EQ.1 ) PF2 = RP(2)
     	    IF( KP(3).EQ.1 ) PF3 = RP(3)
     	    IF( KP(4).EQ.1 ) PF4 = RP(4)
     	ELSE
     	    KEY = TEXT(1:3)
     	    DO  IK = 1, 7
     		IF( KEY.EQ.KCS(IK) ) GOTO 111
     	    ENDDO
     	    LAYOUT_PFS = 0
     	    CALL IFMISC_OOPS( 3, 0, 0, TEXT )
     	    RETURN
111	    PF1 = IK
    	ENDIF
999	LAYOUT_PFS = 1
     	RETURN
    	END

     	FUNCTION LAYOUT_MODE( TEXT )
C
C	THIS ROUTINE CHECKS FOR ENTRY MODE CHANGE FOR LAYOUT.
C
     	CHARACTER KEY*3, TEXT*(*)

     	MODE = 0
     	NCHS = IFMISC_NEXT( TEXT )
     	IF( NCHS.EQ.0 ) THEN
     	    WRITE(6,1000)
1000	FORMAT(' Keyword menu: BAR, BOX, ARC, POLYGON, OBLIQUE,',
     +' ARROWHEAD, SYMBOL.'/' Enter keyword: ',$)
     	    READ(5,1001,END=999) KEY
1001	FORMAT(A)
     	    IF( KEY.EQ.' ' ) GOTO 999
     	ELSE
     	    KEY = TEXT(1:3)
     	ENDIF
     	IF( KEY .EQ. 'BAR' ) MODE = 1
     	IF( KEY .EQ. 'BOX' ) MODE = 2
     	IF( KEY .EQ. 'ARC' ) MODE = 3
     	IF( KEY .EQ. 'OBL' ) MODE = 4
     	IF( KEY .EQ. 'POL' ) MODE = 5
     	IF( KEY .EQ. 'ARR' ) MODE = 6
     	IF( KEY .EQ. 'SYM' ) MODE = 7
     	IF( MODE.EQ.0 ) CALL IFMISC_OOPS( 3, 0, 0, TEXT )
999	LAYOUT_MODE = MODE
     	RETURN
     	END

     	SUBROUTINE LAYOUT_ENTRY( TEXT, ICODE )
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY, MARCH-1987
C
C	THIS ROUTINE ADDS A LAYOUT ENTRY.
C
     	INCLUDE 'LAYOUT.CBS'
     	CHARACTER KEY*3, TEXT*(*)
     	DIMENSION K(MNS+1), R(MNS+1)
C
C  INITIALIZE ARRAY R...
C
     	DO I=1, MNS+1
     	   R(I) = 0.0
     	ENDDO
C
C  CHECK FOR COLOR KEYWORD FIRST...
C
     	I1 = 0
     	I2 = 1
     	ICD = ICODE
     	KEY = TEXT(1:3)
     	DO IK = 1, 7
     	   IF( KEY.EQ.KCS(IK) ) GOTO 111
     	ENDDO
     	NCH = IFMISC_LENG( TEXT )
     	GOTO 222
111	I1 = 1
     	I2 = 2
     	R(1) = IK
     	NCH = IFMISC_NEXT( TEXT )
222	IF( NCH.GT.0 ) NCH = IFMISC_DECODE(TEXT,NCH,MNS-I1,R(I2),K(I2))
    	IF( NCH.EQ.0 ) GOTO 888
C
C  CHECK PEN CODE...
C
     	IF( R(1).LT.0.0 .OR. R(1).GT.8.0 ) GOTO 888
C
C  CONVERT TO ABSOLUTE COORDINATES...
C
     	R(2) = R(2) + XORS
     	R(3) = R(3) + YORS
C
C  CHECK THE REST OF THE PARAMETERS...
C
     	IF( ICD.LT.3 ) THEN	!BAR ENTRY...
     	    IF( R(4).LT.0.01 .OR. R(4).GT.10.0 ) GOTO 888
     	    IF( R(5).LT.0.01 .OR. R(5).GT.10.0 ) GOTO 888
     	    IF( R(6).LT.0.00 .OR. R(6).GT.10.0 ) GOTO 888
     	    R(7) = AMOD( R(7), 360.0 )
     	    IF( ABS(R(8)).GT.99.0 ) GOTO 888
     	    R(9) = LTYP
     	    R(10)= LWID
     	ELSE IF( ICD.EQ.3 ) THEN	!ARC ENTRY...
     	    IF( R(4).LT.0.01 .OR. R(4).GT.5.0 ) GOTO 888
     	    IF( R(5).LT.0.01 .OR. R(5).GT.5.0 ) GOTO 888
     	    IF( R(6).LT.0.00 .OR. R(6).GT.5.0 ) GOTO 888
     	    R(7) = AMOD( R(7), 360.0 )
     	    R(8) = AMOD( R(8), 360.0 )
     	    R(9) = AMOD( R(9), 360.0 )
     	    IF( ABS(R(10)).GT.99.0 ) GOTO 888
     	    R(11) = LTYP
     	    R(12) = LWID
     	ELSE IF( ICD.EQ.4 ) THEN	!OBLIQUE ENTRY...
     	    IF( R(4).LT.0.01 .OR. R(4).GT.5.0 ) GOTO 888
     	    IF( R(5).LT.0.01 .OR. R(5).GT.5.0 ) GOTO 888
     	    IF( R(6).LT.0.00 .OR. R(6).GT.5.0 ) GOTO 888
     	    R(7) = AMOD( R(7), 360.0 )
     	    IF( ABS(R(8)).GT.99.0 ) GOTO 888
     	    R(9) = LTYP
     	    R(10)= LWID
     	ELSE IF( ICD.EQ.5 ) THEN	!POLYGON ENTRY...
     	    IF( R(4).LT.0.01 .OR. R(4).GT.5.0 ) GOTO 888
     	    IF( R(5).LT.0.00 .OR. R(5).GT.5.0 ) GOTO 888
     	    R(6) = AMOD( R(6), 360.0 )
     	    IF( R(7).LT.3.0 .OR. R(7).GT.360.0 ) GOTO 888
     	    IF( ABS(R(8)).GT.99.0 ) GOTO 888
     	    R(9) = LTYP
     	    R(10)= LWID
     	ELSE IF( ICD.EQ.6 ) THEN	!ARROWHEAD ENTRY...
     	    IF( R(4).LT.0.01 .OR. R(4).GT.10.0 ) GOTO 888
     	    IF( R(5).LT.0.01 .OR. R(5).GT.10.0 ) GOTO 888
     	    IF( R(6).LT.0.01 .OR. R(6).GT.10.0 ) GOTO 888
     	    R(7) = AMOD( R(7), 360.0 )
     	    IF( R(8).LT.1.0 ) GOTO 888
     	ELSE IF( ICD.EQ.7 ) THEN	!MARKER SYMBOL ENTRY...
     	    IF( R(4).LT.0.01 .OR. R(4).GT.5.0 ) GOTO 888
     	    R(5) = AMOD( R(5), 360.0 )
     	    IF( R(6).LT.1.0 ) GOTO 888
     	ELSE
     	    GOTO 888
     	ENDIF
C
C  STORE THE ENTRY PARAMETERS...
C
     	NLC = NLC + 1
     	NID(NLC) = ICD
     	PEN(NLC) = R(1)
     	DO I=1, MNS
     	   P00(I,NLC) = R(I+1)
     	ENDDO
     	NFL(NLC) = 0
C
C  DISPLAY THE ENTRY...
C
     	CALL LAYOUT_DRAW( NLC, 1 )
     	CALL IFINIT_DUMP
     	RETURN
888	CALL IFMISC_OOPS( 3, 0, 0, TEXT )
     	RETURN
     	END

     	SUBROUTINE LAYOUT_DCL
C
C	THIS ROUTINE CHECKS INPUT DCL COMMAND LINE.
C
     	INCLUDE 'LAYOUT.CBS'

     	CALL IF_DCL_FILE( 1, LOFILE )
     	IF( LOFILE.EQ.' ' ) RETURN
     	CALL LAYOUT_INIT
     	CALL IFINIT_PLOT( XORG, YORG, LOFILE )
     	IERR = LAYOUT_IO( 1, LOFILE )
     	IF( IERR.GT.0 ) THEN
     	    IF( IF_DCL_HARDCOPY() ) CALL IFCOPY( HPAR(1), PLOTID )
     	ELSE IF( IERR.EQ.0 ) THEN
     	    WRITE(6,1000) LOFILE
1000	FORMAT(' %LAYOUT: can not open file ',A32)
     	ELSE
     	    IERR = IFMISC_LENG( LOFILE )
     	    WRITE(6,1001) LOFILE(1:IERR)
1001	FORMAT(' %LAYOUT: ',A,' is not a layout file.')
     	ENDIF
     	IF( .NOT.IF_DCL_HARDCOPY() ) RETURN
     	CALL EXIT
     	END

    	SUBROUTINE LAYOUT_ZOOM( XF, YF, XP, YP )
C
C  PROGRAMMER: R.S. ZHOU		JUNE-1986
C
C	THIS ROUTINE ZOOMS PICKED LAYOUT SEGMENTS
C	BY "XF" AND "YF" WITH THE PIVOT AT (XP, YP).
C	IF XP OR YP .EQ. 99.0, THE SEGMENTS ARE ZOOMED
C	AROUND THEIR OWN INDIVIDUAL PIVOTS.
C
    	INCLUDE		'LAYOUT.CBS'
     	LOGICAL		SKIP, ONE
     	REAL*4		IFPACK_DECTEK
     	DIMENSION	IR1(9), IR2(9), IR3(9)
     	DATA IR1/ 3, 3, 3, 3, 3, 3, 3, 3, 4 /
     	DATA IR2/ 4, 4, 4, 4, 0, 4, 0, 4, 5 /
     	DATA IR3/ 5, 5, 5, 5, 4, 5, 0, 0, 0 /

     	IF( XF.LE.0.0 .OR.  YF.LE.0.0 ) RETURN
     	IF( XF.EQ.1.0 .AND. YF.EQ.1.0 ) RETURN

    	XOP = XP
    	YOP = YP
    	ZFX = IFPACK_DECTEK(XF)
    	ZFY = IFPACK_DECTEK(YF)
    	ONE = .FALSE.
    	IF( XOP.EQ.99.0 .OR. YOP.EQ.99.0 ) ONE = .TRUE.

    	DO  222 I = 1, NLG
    	    IS = NLS(I)
     	    SKIP = .FALSE.
     	    DO  111 J=1, NLC
     		IF( NSN(J).NE.IS ) GOTO 111
     		XPS = P00(1,J)
     		YPS = P00(2,J)
     		ZXO = ZXF(J)
     		ZYO = ZYF(J)
     		ZXF(J) = IFPACK_DECTEK(ZXO*ZFX)
     		ZYF(J) = IFPACK_DECTEK(ZYO*ZFY)
     		ZXO = ZXF(J)/ZXO
     		ZYO = ZYF(J)/ZYO
     		IF( .NOT.ONE ) THEN
     		    XPS = XOP + (XPS-XOP)*ZXO
     		    YPS = YOP + (YPS-YOP)*ZYO
     		    P00(1,J) = XPS
     		    P00(2,J) = YPS
     		ENDIF
     		IT3 = NID(J)
     		IT1 = IR1(IT3)
     		IT2 = IR2(IT3)
     		IT3 = IR3(IT3)
     		IF( .NOT.SKIP ) THEN
     		    IF( IT2.EQ.0 ) THEN
     			CALL IFSEG_TRAN(IS,XPS,YPS,RAN(J),ZXF(J),ZXF(J))
     		    ELSE
     			CALL IFSEG_TRAN(IS,XPS,YPS,RAN(J),ZXF(J),ZYF(J))
     		    ENDIF
     		ENDIF
     		P00(IT1,J) = P00(IT1,J)*ZXO
     		IF( IT2.GT.0 ) P00(IT2,J) = P00(IT2,J)*ZYO
     		IF( IT3.GT.0 ) P00(IT3,J) = P00(IT3,J)*ZXO
     		IF( NFL(J).EQ.0 ) GOTO 222
     		SKIP = .TRUE.
111	    CONTINUE
222	CONTINUE

     	CALL IFINIT_DUMP

    	RETURN
    	END

    	SUBROUTINE LAYOUT_ZOOM_SCALE( IP, XFT, YFT )
C
C  THIS ROUTINE ASSIGNS DIFFERNET ZOOMING SCALE FACTORS "XFT,YFT"
C  ACCORDING TO "IP" FOR LAYOUT PLOTTING.
C
    	SAVE IOPL

    	IOP = IP
    	XFT = 1.0
    	YFT = 1.0
    	IF( IOP.EQ.1 ) THEN
    	    XFT = 0.99
    	    YFT = 0.99
    	ELSE IF( IOP.EQ.2 ) THEN
     	    YFT = 0.99
     	ELSE IF( IOP.EQ.3 ) THEN
    	    XFT = 1.02
    	    YFT = 0.99
    	ELSE IF( IOP.EQ.4 ) THEN
     	    XFT = 0.99
     	ELSE IF( IOP.EQ.5 ) THEN
    	    IF(IOPL.NE.10) THEN
    	        IOPL = 10
    	    ELSE
    	        IOPL = 1
    	    ENDIF
    	ELSE IF( IOP.EQ.6 ) THEN
     	    XFT = 1.02
     	ELSE IF( IOP.EQ.7 ) THEN
    	    XFT = 0.99
    	    YFT = 1.02
    	ELSE IF( IOP.EQ.8 ) THEN
     	    YFT = 1.02
     	ELSE IF( IOP.EQ.9 ) THEN
    	    XFT = 1.02
    	    YFT = 1.02
    	ELSE IF( IOP.EQ.0 ) THEN
    	    IF(IOPL.NE.25) THEN
    	        IOPL = 25
    	    ELSE
    	        IOPL = 10
    	    ENDIF
    	ENDIF
    	IF( IOPL.EQ.0 ) IOPL = 1
    	XFT = XFT ** IOPL
    	YFT = YFT ** IOPL

    	RETURN
    	END

