/* Création d'une grille quadrillée
$VER: Grille.AmiCAD 1.02 (© R.Florac, 26/04/98)
Version 1.00 ©R.Florac, Mardi 3 Mars 1998
Version 1.01, 25 avril 1998 (utilisation d'un rectangle pour définir la zone recevant la grille) */

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
clip=-1
FIRSTSEL; obj=result
if obj>0 then do
    'TYPE(FIRSTSEL)'; type=result
    if type=22 then do
	'CLIPUNIT(5)'; clip=result
	'COORDS(FIRSTSEL)'; coords=result
	PARSE VAR coords x0 ',' y0 ',' x1 ',' y1
	xg=minima(x0,x1); xd=maxima(x0,x1)
	yh=minima(y0,y1); yb=maxima(y0,y1)
	l=xd-x0+1; h=yb-y0+1
	'NEXTSEL('obj')'; obj=result
    end
end
else obj=1

if obj>0 then do
    'MESSAGE("Dessinez et sélectionnez"+CHR(10)+"un rectangle dans"+CHR(10)+"lequel doit être"+CHR(10)+"intégrée la grille")'
    call quitter
end

'ASK("Axe horizontal"+CHR(10)+"Nombre de décades?")'
ndh = result
if ndh<=0 then call quitter
'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)

'SAVEALL(-1)'
if clip>=0 then 'MENU("Couper")'
/* 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 call quitter
end

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

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 call quitter
end
call quitter

minima: procedure
    parse arg v1,v2
    if v1<v2 then return v1
    return v2
end

maxima: procedure
    parse arg v1,v2
    if v1>v2 then return v1
    return v2
end

quitter: procedure expose clip
    if clip>=0 then 'CLIPUNIT('clip')'
    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)'")'
call quitter

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