/* CMD: Slice

 By GLYPH
 E-Mail: y82s1@unb.ca

 Slice Object on Z axis and make a scene of it

 The pivot points of the pieces are
 in their centers for proper rotations.

*/


JUNK:
	mxx="LWModelerARexx.port"
	signal on error
	signal on syntax
	mxx_add = addlib(mxx,0)
	call main
	if (mxx_add) then call remlib(mxx)
	exit
	syntax:
	error:
	t=Notify(1,'!Rexx Script Error','@'ErrorText(rc),'Line 'SIGL)
	if (mxx_add) then call remlib(mxx)
	exit
return

main:
Address 'LWModelerARexx.port'
call random(,,time('s'))
scenename = 'toaster:3d/scenes/test.scn'
objbase = 'toaster:3d/objects/test/testobj'
call Menu
call GetSize
call CutObject
call SavePieces
call SaveScene
return

Menu:
cutx = 4
cuty = 4
call req_begin 'Slice Object by GLYPH'
req1 = req_addcontrol('X divisions',N,0)
req2 = req_addcontrol('Y divisions',N,0)
call req_setval req1, cutx, cutx
call req_setval req2, cuty, cuty
if (~req_post()) then exit(0)
cutx = req_getval(req1)
cuty = req_getval(req2)

scenename = getfilename('Scene File','toaster:3d/scenes')
if scenename = '(none)' then exit(0)
objbase = getfilename('Object Prefix','toaster:3d/objects')
if objbase = '(none)' then exit(0)
return


GetSize:
curlay=curlayer()
emp = emptylayers()
if (words(emp) = 0) then do
    ok = notify(1,'!No empty layer.')
    break
end
if (words(emp) > 0) then do
    freelay = word(emp,1)
end
box=boundingbox()
parse var box n x1 x2 y1 y2 z1 z2
dx = x2-x1
dy = y2-y1
return


CutObject:
/* make slicers */
call setlayer(freelay)
do xn = 1 to cutx/2
    call makebox(x1+(xn-0.5)*dx/cutx*2 y1-1 0,x1+xn*dx/cutx*2 y2+1 0)
end
do yn = 1 to cuty/2
    call makebox(x1-1 y1+(yn-0.5)*dy/cuty*2 0,x2+1 y1+yn*dy/cuty*2 0)
end
call setlayer(curlay)
call setblayer(freelay)
call axisdrill('slice','z')
call setlayer(freelay)
call cut()
return


SavePieces:
call sel_mode('user')
curobj = 0
do xn=1 to cutx
  do yn=1 to cuty
    call setlayer(curlay)
    call sel_polygon('clear')
    call sel_polygon('set','volexcl',x1+(xn-1)*dx/cutx-dx/1000 y1+(yn-1)*dy/cuty-dy/1000 z1-1,x1+xn*dx/cutx+dx/1000 y1+yn*dy/cuty+dy/1000 z2+1)
    call copy()
    call setlayer(freelay)
    call paste()
    call SaveIt
    call cut()
  end
end
return


SaveIt:
box=boundingbox()
parse var box n d1 d2 d3 d4 d5 d6
if n=0 then return
if d1x1+xn*dx/cutx+dx/1000 then return
if d3y1+yn*dy/cuty+dy/1000 then return
curobj=curobj+1
cx.curobj = (d1+d2)/2
cy.curobj = (d3+d4)/2
cz.curobj = (d5+d6)/2
call save(objbase||'.'||curobj)
return


SaveScene:
call open(scene, scenename, 'W')
call writeln(scene, 'LWSC')
call writeln(scene, 1)
call writeln(scene, '')

do o = 1 to curobj
 objname = objbase||'.'||o
 call writeln(scene, 'LoadObject '||objname)
 call writeln(scene, 'ObjectMotion (unnamed)')
 call writeln(scene, '  '||9)
 call writeln(scene, '  '||1)
 x=cx.o
 y=cy.o
 z=cz.o
 h=0
 p=0
 b=0
 xs=1
 ys=1
 zs=1
 linear = 1
 frame = 0
 call WriteKey
 call writeln(scene, 'EndBehavior 1')
 call writeln(scene, 'PivotPoint '||cx.o||' '||cy.o||' '||cz.o)
 call writeln(scene, 'ShadowOptions 7')
 call writeln(scene, '')
end
call close scene
return


WriteKey:
call writeln(scene, '  '||x||' '||y||' '||z||' '||h||' '||p||' '||b||' '||xs||' '||ys||' '||zs)
call writeln(scene, '  '||frame||' '||linear||' 0.0 0.0 0.0')
return

    Source: geocities.com/g_fyffe/lw

               ( geocities.com/g_fyffe)