/* Script ARexx permettant le tracé d'un cartouche dans une fenêtre AmiCAD */
/* Chez Corbin, Vendredi 3 Octobre 1997, Version 1.00, © R.Florac */
/* Version 1.01, 2 Novembre 1997, trait vertical rajouté dans cartouche */
/* Version 1.02, 23 janvier 1998, ajout titre, date, etc... */
/* Version 1.03, 12 février 1998, test longueurs chaînes pour ajustement largeur */
/* Version 1.04, 24 mai 1998, test présence rexxlocaldates.library */
/* Version 1.05, 21 juin 1998, ajout test après choix emplacement cartouche pour arrêt */
/* Version 1.06, 14 juillet 1998, modification tracé lignes (lignes quelconques) */
/* Version 1.06a, 15 janvier 1999, remplacement fonction ASK par ASKTEXT */
/* Version 1.07, 13 avril 2000, modification fonctions SAVEALL, WWIDTH, WHEIGHT, ajout SETSCALE */
/* Version 1.08, 4 novembre 2000, localisation anglais/français */
/* $VER: Cartouche.AmiCAD 1.08 (© R.Florac, 4/11/00) */

options results

'LANGUAGE'
if result="français.language" then fr=1
else fr=0

if fr=1 then organisation="LYCÉE Bernard PALISSY SAINTES"
else organisation="AmiCAD © R.Florac 2000"

signal on error
signal on syntax

if (~show('L','rexxlocaldates.library')) then
  call addlib('rexxlocaldates.library',0,-30,0)

if (show('L','rexxlocaldates.library')) then do
    Ib = OpenLocale()
    jour = FormatDate(Ib, ,"%A %e %B %Y")
end
else do
    'DATE(1)'; jour=result
end

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

if fr=1 then 'SELECT("Emplacement du cartouche"+CHR(10)+"En haut à gauche"+CHR(10)+"En haut à droite"+CHR(10)+"En haut au centre"+CHR(10)+"En bas au centre"+CHR(10)+"En bas à droite"+CHR(10)+"En bas à gauche")'
else 'SELECT("Placement of the cartouche"+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
if c<1 then exit    /* Version 1.05 */

if fr=1 then 'ASKTEXT("Titre du cartouche","")'
else 'ASKTEXT("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

/* sauvegarde du contexte, dessin du contour */
'SAVEALL: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,100,100)'
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 Numéro 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
if fr=1 then 'MESSAGE("Script Cartouche.AmiCAD:"+CHR(10)+"Erreur de syntaxe"+CHR(10)+"en ligne 'SIGL'"+CHR(10)+"'errortext(erreur)'")'
else 'MESSAGE("Script Cartouche.AmiCAD:"+CHR(10)+"Syntax error"+CHR(10)+"in line 'SIGL'"+CHR(10)+"'errortext(erreur)'")'
exit

error:
if fr=1 then 'MESSAGE("Script Cartouche.AmiCAD:"+CHR(10)+"Erreur en ligne 'SIGL'")'
else 'MESSAGE("Script Cartouche.AmiCAD:"+CHR(10)+"Error in line 'SIGL'")'
exit
