/* Script permettant d'ajouter un arc à deux lignes qui
   se croisent avec un angle droit.
   Version 1.00: 20 octobre 2001
   Version 1.01: 2 décembre 2001 (localisation anglais)
   Version 1.02, 03-Feb-2002, German localization by A. Greve
     You can now pass an argument to this script, containing
     the radius (in 1/1000mm) for the rounding. If the value
     is not numeric, not integer, or < 1, the value will be
     set to 2540 (= 2,54mm).
   $VER: Arrondir.AmiPCB 1.02, © R.Florac 03-Feb-2002
*******************************************************/

parse arg radius
if verify(radius,"0123456789") > 0 then radius = 2540
if radius < 1 then radius = 2540

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

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

lf='0a'x
fr= 'ARexx/initVars.AmiPCB'()

'FINDOBJ(0,-TRACK,-1,-1)'
l1=result
'FINDOBJ('l1',-TRACK,-1,-1)'
l2=result
if l1<1 | l2<1 then do
    select
	when fr=1 then 'MESSAGE("Sélectionner les deux lignes'lf'dont l''angle doit être arrondi'lf'avant d''appeler ce script.")'
	when fr=2 then 'MESSAGE("Wählen Sie die beiden Linien, deren'lf'gemeinsamer Winkel gerundet werden soll,'lf'vor dem Aufruf dieses Skripts.")'
	otherwise      'MESSAGE("Select the two lines'lf'whose common angle must be rounded'lf'before calling this script.")'
    end
    exit
end

'COORDS('l1')'
parse var result x0 ',' y0 ',' x1 ',' y1
'COORDS('l2')'
parse var result x2 ',' y2 ',' x3 ',' y3
if x0=x2 & y0=y2 then do
    a=test_angle(x0,x1,x2,x3,y0,y1,y2,y3)
    if a=-1 then signal no_rightangle
    xc=x0
    yc=y0
end
else if x0=x3 & y0=y3 then do
    a=test_angle(x0,x1,x2,x3,y0,y1,y2,y3)
    if a=-1 then signal no_rightangle
    xc=x0
    yc=y0
end
else if x1=x2 & y1=y2 then do
    a=test_angle(x0,x1,x2,x3,y0,y1,y2,y3)
    if a=-1 then signal no_rightangle
    xc=x1
    yc=y1
end
else if x1=x3 & y1=y3 then do
    a=test_angle(x0,x1,x2,x3,y0,y1,y2,y3)
    if a=-1 then signal no_rightangle
    xc=x1
    yc=y1
end
else do
    select
	when fr=1 then 'MESSAGE("Les deux lignes n''ont'lf'pas de point commun.'lf'Il est impossible d''arrondir l''angle")'
	when fr=2 then 'MESSAGE("Die beiden Linien haben keinen gemeinsamen Punkt.'lf'Es ist nicht möglich, einen Winkel zu runden.")'
	otherwise      'MESSAGE("The two lines don''t'lf'have a common point.'lf'It''s impossible to round any angle")'
    end
    exit
end

xmini=min(x0,x1,x2,x3)
xmaxi=max(x0,x1,x2,x3)
ymini=min(y0,y1,y2,y3)
ymaxi=max(y0,y1,y2,y3)
'SAVEALL:SETWIDTH(0,SETWIDTH('l1',-1)):SETSIDE(0,SETSIDE('l1',0)):DELETE('l1'):DELETE('l2')'
if xc=xmini then do
    if yc=ymini then do
	'ARC('xc'+'radius','yc'+'radius','radius','radius',3):DRAW('xc','yc'+'radius','xc','ymaxi'):DRAW('xc'+'radius','yc','xmaxi','yc')'
	exit
    end
    'ARC('xc'+'radius','yc'-'radius','radius','radius',2):DRAW('xc','yc'-'radius','xc','ymini'):DRAW('xc'+'radius','yc','xmaxi','yc')'
    exit
end
if yc=ymini then do
    'ARC('xc'-'radius','yc'+'radius','radius','radius',0):DRAW('xc'-'radius','yc','xmini','yc'):DRAW('xc','yc'+'radius','xc','ymaxi')'
    exit
end
'ARC('xc'-'radius','yc'-'radius','radius','radius',1):DRAW('xc'-'radius','yc','xmini','yc'):DRAW('xc','yc'-'radius','xc','ymini')'
exit

test_angle:
    parse arg x0,x1,x2,x3,y0,y1,y2,y3
    if x0=x1 & y2=y3 then return 1
    if y0=y1 & x2=x3 then return 2
    return -1

no_rightangle:
    select
	when fr=1 then 'MESSAGE("Les deux lignes ne'lf'forment pas un angle droit.'lf'Il est impossible d''arrondir l''angle")'
	when fr=2 then 'MESSAGE("Die beiden Linien haben keinen 90°-Winkel.'lf'Es ist nicht möglich, den Winkel zu runden.")'
	otherwise      'MESSAGE("The two lines don''t'lf'do an angle of 90°.'lf'It''s impossible to round the angle")'
    end
    exit

/* Traitement des erreurs, interruption du programme */
syntax:
erreur=RC
Call 'ARexx/_Syntax.rexx'(fr,'Arrondir.AmiPCB',SIGL,erreur)
exit

error:
Call 'ARexx/_Error.rexx'(fr,'Arrondir.AmiPCB',SIGL)
exit
