/* Création d'une grille quadrillée
Version 1.00 ©R.Florac, Mardi 3 Mars 1998 */

options results     /* indispensable pour récupérer le résultat des macros */

signal on error     /* pour l'interception des erreurs */
signal on syntax

'WWIDTH(-1)'; lt = result
'WHEIGHT(-1)'; ht=result
'ASK("Dimensions de la grille?"+CHR(10)+CHR(10)+"Le document fait 'lt','ht'")'
PARSE VALUE RESULT WITH l','h
if l<=0 | h<=0 then do
    'MESSAGE("Donnez les dimensions"+CHR(10)+"sous la forme"+CHR(10)+"largeur,hauteur"+CHR(10)+"Exemple: 'lt-50','ht-50'")'
    exit
end

'ASK("Emplacement de la grille?"+CHR(10)+CHR(10)+"Coordonnées maxi 'lt-l','ht-h'")'
PARSE VALUE RESULT WITH x0','y0
if x0<=0 | y0<=0 then do
    l = (lt-l)%2
    h = (ht-h)%2
    'MESSAGE("Donnez les coordonnées"+CHR(10)+"sous la forme"+CHR(10)+"x_gauche,y_haut"+CHR(10)+"Exemple: 'l','h'")'
    exit
end

if x0+l>=lt then do
    'MESSAGE("Valeur incorrecte")'
    exit
end
if y0+h>=ht then do
    'MESSAGE("Valeur incorrecte")'
    exit
end
'ASK("Axe horizontal"+CHR(10)+"Nombre de décades?")'
ndh = result
if ndh<=0 then exit
'SELECT("Type d''échelle"+CHR(10)+"1- Linéaire"+CHR(10)+"2- Logarithmique"+CHR(10)+"3- Antilogarithmique")'
tt=result
y1=y0+h
x1=x0+ndh*(l%ndh)

/* Tracé des lignes verticales */
select
    when tt=1 then do
	/* Tracé des lignes verticales */
	do i=1 to ndh
	    x2 = (x0)+i*(l/ndh)
	    x2 = x2%1
	    'DRAWMODE(1)'
	    do c=1 to 9
		xc = x2-(l/ndh)/10*c
		xc=xc%1
		'DRAW('xc','y0','xc','y1')'
	    end
	    'DRAWMODE(2):DRAW('x2','y1','x2','y0')'
	end
    end
    when tt=2 then do
	if ~show('L','rexxmathlib.library') then
	   call addlib('rexxmathlib.library',0,-30)
	/* Tracé des lignes verticales */
	x2=x0
	do i=1 to ndh
	    'DRAWMODE(1)'
	    do c=2 to 9
		xc=(l/ndh)*log10(c)
		xc=(x2+xc)%1
		'DRAW('xc','y0','xc','y1')'
	    end
	    x2 = (x0)+i*(l/ndh)
	    x2 = x2%1
	    'DRAWMODE(2):DRAW('x2','y1','x2','y0')'
	end
    end
    when tt=3 then do
	if ~show('L','rexxmathlib.library') then
	   call addlib('rexxmathlib.library',0,-30)
	x2=x1
	do i=1 to ndh
	    'DRAWMODE(2):DRAW('x2','y1','x2','y0')'
	    'DRAWMODE(1)'
	    do c=2 to 9
		xc=(l/ndh)*log10(c)
		xc=(x2-xc)%1
		'DRAW('xc','y0','xc','y1')'
	    end
	    x2 = (x1)-i*(l/ndh)
	    x2 = x2%1
	end
    end
    otherwise exit
end

'ASK("Axe vertical"+CHR(10)+"Nombre de décades?")'
ndv = result
if ndv<=0 then exit

y1=y0+h
x1=x0+ndh*(l%ndh)
/* Tracé du contour */
'DRAWMODE(2):DRAW('x0','y0','x1','y0'):DRAW('x0','y1','x0','y0')'

'SELECT("Type d''échelle"+CHR(10)+"1- Linéaire"+CHR(10)+"2- Logarithmique"+CHR(10)+"3- Antilogarithmique")'
tt=result

/* Tracé des lignes horizontales */
select
    when tt=1 then do
	do i=1 to ndv
	    y2 = (y0)+i*(h/ndv)
	    y2 = y2%1
	    'DRAWMODE(1)'
	    do c=1 to 9
		yc = y2-(h/ndv)/10*c
		yc=yc%1
		'DRAW('x0','yc','x1','yc')'
	    end
	    'DRAWMODE(2):DRAW('x0','y2','x1','y2')'
	end
    end
    when tt=2 then do
	if ~show('L','rexxmathlib.library') then
	   call addlib('rexxmathlib.library',0,-30)
	y2=y1
	do i=1 to ndv
	    'DRAWMODE(2):DRAW('x0','y2','x1','y2')'
	    'DRAWMODE(1)'
	    do c=2 to 9
		yc=(h/ndv)*log10(c)
		yc=(y2-yc)%1
		'DRAW('x0','yc','x1','yc')'
	    end
	    y2 = y1-i*(h/ndv)
	    y2 = y2%1
	end
    end
    when tt=3 then do
	if ~show('L','rexxmathlib.library') then
	   call addlib('rexxmathlib.library',0,-30)

	y2=y0

	do i=1 to ndv
	    'DRAWMODE(1)'
	    do c=2 to 9
		yc=(h/ndv)*log10(c)
		yc=(y2+yc)%1
		'DRAW('x0','yc','x1','yc')'
	    end
	    y2 = (y0)+i*(h/ndv)
	    y2 = y2%1
	    'DRAWMODE(2):DRAW('x0','y2','x1','y2')'
	end
    end
    otherwise exit
end
exit

/* Traitement des erreurs, interruption du programme */
syntax:
erreur=RC
'MESSAGE("Script grille.AmiCAD"+CHR(10)+"Erreur de syntaxe"+CHR(10)+"en ligne 'SIGL'"+CHR(10)+"'errortext(erreur)'")'
exit

error:
'MESSAGE("Script grille.AmiCAD"+CHR(10)+"Erreur en ligne 'SIGL'")'
exit
