/* CMD: Router
 * Bevel Polygons with multi=level bevels
 * By Arnie Cachelin © 1992, 1993 NewTek Inc. */

styles = 'Round Hollow StairStep'
typ=1
steps=5
deep = 0.1
wide = 0.02
call addlib "rexxsupport.library", 0, -30, 0
libadd = addlib("LWModelerARexx.port",0)
signal on error
signal on syntax
MATHLIB="rexxmathlib.library"
IF POS(MATHLIB , SHOW('L')) = 0 THEN
  IF ~ADDLIB(MATHLIB , 0 , -30 , 0) THEN DO
	call notify(1,"!Can't find "MATHLIB)
	exit
	END
call req_begin 'Router'
id_typ = req_addcontrol("Type", "CH",Styles)
id_deep = req_addcontrol("Depth", 'n', 1)
id_wide = req_addcontrol("Edge Width", 'n', 1)
id_steps = req_addcontrol("Steps", 'n')
id_corn = req_addcontrol("Bevel on Corner",'B')
call req_setval id_typ, typ,1
call req_setval id_deep, deep,0.1
call req_setval id_wide, wide,0.02
call req_setval id_steps, steps,5

if (~req_post()) then do
    call req_end
    exit
end
typ = req_getval(id_typ)
wide = req_getval(id_wide)
deep = req_getval(id_deep)
steps = req_getval(id_steps)
corn =  req_getval(id_corn)
/* axis = translate(req_getval(axId),'XYZ','123') */
call req_end
sc45=sin(3.14159/4)
call sel_mode(USER)
select
  when typ = 1 then
    if ~corn then call Bevel_Round
    else call Bevel_CornRound
  when typ = 2 then
    if ~corn then call Bevel_Hollow
    else call Bevel_CornHollow
  when typ = 3 then call Bevel_StairStep
  otherwise nop
  end
if (libadd) then call remlib("LWModelerARexx.port")
exit

syntax:
error:
  call end_all
	t=Notify(1,'!Rexx Script Error','@'ErrorText(rc),'Line 'SIGL)
  if (libadd) then call remlib("LWModelerARexx.port")
	exit

Bevel_Chisel:
    call shapebevel(-wide wide (-wide) deep/2)
    return

Bevel_Round:
    n = steps
    pat = ''
    do i=1 to n
    	a = 3.14159/2 * i / n
    	pat = pat (1-cos(a))*wide (sin(a)*deep)
      end i
    call shapebevel(pat)
    say pat
    return

Bevel_CornRound:
    n = steps
    pat = ''
    do i=1 to n
    	a = 3.14159/4 * i / n
      a = a + 3.14159/4
    	pat = pat 2*(sc45-cos(a))*wide/sc45 2*(sin(a)-sc45)*deep/(sc45)
      end i
    call shapebevel(pat)
    say pat
    return

Bevel_Hollow:
    n = steps
    pat = ''
    do i=1 to n
    	a = 3.14159/2 * i / n
    	pat = pat (sin(a)*wide) (1-cos(a))*wide
      end i
    call shapebevel(pat )
    return

Bevel_CornHollow:
    n = steps
    pat = ''
    do i=1 to n
    	a = 3.14159/4 * i / n
    	pat = pat ( (sin(a)-sc45 )*wide) (sc45-cos(a))*wide
      end i
    call shapebevel(pat)
    return

Bevel_StairStep:
    n = steps
    pat = ''
    w=wide/n
    d=deep/n
    do=0
    wo=0
    do i=1 to n
    	pat = pat w do w d
      do=d
      wo=w
      w=w+wide/n
      d=d+deep/n
      end i
    call shapebevel(pat)
    return


