    	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
    	IF( NLC.EQ.0 ) GOTO 999		!NOTHING TO SAVE
C
C  	----------	WRITE A LAYOUT FILE	------------
C
     	CALL IFMISC_DEFEXT( PNAME, '.DES' )
    	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')
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)
     	    WRITE(13,40) KWD(8),PEN(ID),(P00(J,ID),J=1,JR),TXT(I)
     	ENDDO
40	FORMAT(A4,'=',I2,<JR>(',',F7.3),',',A75)
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 )

     	IF( MODE.EQ.1 .AND. .NOT.IF_DCL_HARDCOPY() ) WRITE(6,1234)
1234	FORMAT('$Press CTRL/C to cancel plotting...')
     	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, 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 LAYOUT_ERROR(12)		!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
     	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+4), YSS(MNP+4)

     	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 )

     	ID = NID(ISS)
C
C  BAR...
C
     	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)
     	    GOTO 999
     	ENDIF
C
C  BOX...
C
     	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)
     	    GOTO 999
     	ENDIF
C
C  ARC/PIE/CIRCLE/ELLIPSE...
C
     	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)
     	    GOTO 999
     	ENDIF
C
C  OBLIQUE...
C
     	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)
     	    GOTO 999
     	ENDIF
C
C  POLYGON...
C
     	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)
     	    GOTO 999
     	ENDIF
C
C  ARROWHEAD...
C
     	IF( ID.EQ.6 ) THEN
     	    CALL IFARRH( INT(R(7)),R(1),R(2),R(3),R(4),R(5),R(6) )
     	    GOTO 999
     	ENDIF
C
C  MARKER SYMBOL...
C
     	IF( ID.EQ.7 ) THEN
     	    CALL IFMARK( R(1), R(2), R(3), R(4), INT(R(5)) )
     	    GOTO 999
     	ENDIF
C
C  TEXT...
C
     	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))
     	    GOTO 999
     	ENDIF
C
C  LINE/CURVE...
C
     	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
     		XSS(IJ+1) = 0.0
     		XSS(IJ+2) = 1.0
     		XSS(IJ+3) = 0.0
     		YSS(IJ+1) = 0.0
     		YSS(IJ+2) = 1.0
     		YSS(IJ+3) = 0.0
     		CALL IFLINE( 0, 0.0, 0, NPTS, 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 )
     	    GOTO 999
     	ENDIF

999	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

9999	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

