/* This ARexx script creates a cartouche in the current AmiCAD window */
/* Version 1.03e, 3 mars 1998 © R.Florac */

options results

organisation="ORGANISATION NAME"
signal on error
signal on syntax

IF (~SHOW('L','rexxlocaldates.library')) THEN
  CALL ADDLIB('rexxlocaldates.library',0,-30,0)

Ib = OpenLocale()

jour = FormatDate(Ib, ,"%A %e %B %Y")

'WWIDTH(-1)'; l = result; lc = result % 3
'WHEIGHT(-1)'; h =result
'ROTATE(0,0):SYMMETRY(0,0)'

'SELECT("Cartouche place"+CHR(10)+"Top left"+CHR(10)+"Top right"+CHR(10)+"Top center"+CHR(10)+"Bottom center"+CHR(10)+"Bottom right"+CHR(10)+"Bottom left")'
c = result

'ASK("Title of the cartouche")'; titre = result
if titre='' then exit

if lc<260 then lc=260
'TXWIDTH("'organisation'")'; lt = result
if lt>lc-10 then lc=lt+10
'TXWIDTH("'titre'")'; lt = result
if lt>lc-10 then lc=lt+10
dx=(lc+5)%4*3
'TXWIDTH("'jour'")'; lt = result
if lt>dx-10 then do
    dx=lt+10
    lc=dx%3*4
end

select
    when c = 1 then do	    /* En haut à gauche */
	x0 = 5
	x1 = lc + 5
	y0 = 5
	y1 = 85
	bh = 0
	bb = 1
	bg = 0
	bd = 1
    end
    when c = 2 then do	    /* En haut à droite */
	x0 = l - lc - 5
	x1 = l - 5
	y0 = 5
	y1 = 85
	bh = 0
	bb = 1
	bg = 1
	bd = 0
    end
    when c = 3 then do	    /* En haut au centre */
	x0 = l % 2 - lc % 2
	x1 = x0 + lc
	y0 = 5
	y1 = 85
	bh = 0
	bb = 1
	bg = 1
	bd = 1
    end
    when c = 4 then do	    /* En bas au centre */
	x0 = l % 2 - lc % 2
	x1 = x0 + lc
	y0 = h - 85
	y1 = h - 5
	bh = 1
	bb = 0
	bg = 1
	bd = 1
    end
    when c = 6 then do	    /* En bas à gauche */
	x0 = 5
	x1 = 5 + lc
	y0 = h - 85
	y1 = h - 5
	bh = 1
	bb = 0
	bg = 0
	bd = 1
    end
    when c = 5 then do	    /* En bas à droite */
	x0 = l - lc - 5
	x1 = l - 5
	y0 = h - 85
	y1 = h - 5
	bh = 1
	bb = 0
	bg = 1
	bd = 0
    end
    otherwise exit
end

/* dessin du contour */
'DRAWMODE(2):DRAW(5,5,'l-5',5):DRAW('l-5',5,'l-5','h-5')'
'DRAW('l-5','h-5',5,'h-5'):DRAW(5,'h-5',5,5)'

/* Tracé du bord horizontal haut */
if bh > 0 then 'DRAW('x0','y0','x1','y0')'
/* Tracé du bord horizontal bas */
if bb > 0 then 'DRAW('x0','y1','x1','y1')'
/* Tracé du bord vertical gauche */
if bg > 0 then 'DRAW('x0','y0','x0','y1')'
/* Tracé du bord vertical droit */
if bd > 0 then 'DRAW('x1','y0','x1','y1')'
/* Tracé des lignes horizontales intermédiaires */
'DRAW('x0','y0+20','x1','y0+20')'
'DRAWMODE(1):DRAW('x0','y0+40','x1','y0+40'):DRAW('x0','y0+60','x1','y0+60')'
/* Tracé de la ligne verticale intermédiaire */
'DRAW('dx+x0','y0+40','dx+x0','y1')'

'SETSCALE(0,1,1)'
call centrer_texte(organisation,x0,x1,y0+15)
call centrer_texte(titre,x0,x1,y0+35)
call centrer_texte(jour,x0,x0+dx,y0+75)
call centrer_texte("Document Number 1/1",x0,x0+dx,y0+55)
call centrer_texte("REV 1",x0+dx,x1,y0+55)
call centrer_texte("AmiCAD",x0+dx,x1,y0+75)

exit

centrer_texte: procedure
    parse arg texte, xg, xd, y
    'TXWIDTH("'texte'")'; l = result
    l = (xd-xg-l)%2
    'WRITE("'texte'",'xg+l','y')'
    return

/* Traitement des erreurs, interruption du programme */
syntax:
erreur=RC
'MESSAGE("Script Cartouche.AmiCAD:"+CHR(10)+"Syntax error"+CHR(10)+"in line 'SIGL'"+CHR(10)+"'errortext(erreur)'")'
exit

error:
'MESSAGE("Script Cartouche.AmiCAD:"+CHR(10)+"Error in line 'SIGL'")'
exit
