--polyExploder 1.0 Final by Snow 2003.8.30
---last update:2004.2.6
--polyExploder could explode your poly objects to several objects.
--note:
--init varibles
tarObj=undefined
tarPieces=50
faceSel=undefined
preName="Object"
thick=1
rReady=true

--fn substract Array1 with Array2
fn arraySub Array1 Array2=
(for i in Array2 do
	(
	ap=findItem Array1 i
	 deleteItem Array1 ap
	)
 Array1
)
--main function
fn polyExp expObj expNum thickness expReady=
( 	
	--init
	n=0
	polyObj=undefined
	objName=""
	objCol=#()
	clearundobuffer()
	rbcSel=#()
	meshDis=undefined
	
	undo "polyExploder" on(
	parti=PArray emitter:expObj particleType:2 speed:0 fragmentMethod:1 Fragment_Thickness:thickness fragChunkMinimum:expNum iconSize:50
    meshObj=Editable_mesh()
	meshObj.mesh=snapshotAsMesh parti
	polyObj=converttoPoly meshObj
	if expReady do
	(aa=reactor.getRigidObject expObj
	expSize=sqrt((expObj.max.x-expObj.min.x)^2+(expObj.max.y-expObj.min.y)^2)
 	expMass=aa.mass
	if expMass==0 do expMass=10
	)
	
	do
	(
	sf = for i in polyObj.faces collect i.index	
	if sf.count==0 do exit
	polyObj.selectedFaces=#(sf[1])
	polyObj.selectElement()
	selElm=polyObj.selectedFaces
	
	--canceled the arraySub function
	--sf=arraySub sf dd
	n=n+1
	objName=preName+(n as string)	
	polyOp.detachFaces polyObj selElm delete:true asNode:true name:objName node:objName
	--polyop.collapseDeadStructs polyObj
	
	--reactor ready part
	if expReady do 
		(
		meshDis=execute ("$"+objName)
		aa=reactor.getRigidObject meshDis
		partiSize=sqrt((meshDis.max.x-meshDis.min.x)^2+(meshDis.max.y-meshDis.min.y)^2)
		--if the size is too small, delete it
		if partiSize<=0.1 do delete meshDis
		aa.mass=expMass*partiSize/expSize	
		append rbcSel meshDis
		)
	)
	while sf.count!=0
	if expReady do
		(
		rbc = RBCollection()
		rbc.rigidBodies=rbcSel
		)
	delete polyObj
	delete expObj
	delete parti
	progressEnd();
	)

)

--fn maxof vals = (local v=vals[1]; for v1 in vals do (if v1 > v do v=v1);v)

rollout polyExpRollout "polyExploder" width:140 height:226
(
	button btn1 "go" pos:[11,189] width:49 height:26 enabled:false
	pickbutton btn2 "Object" pos:[27,32] width:80 height:23
	GroupBox grp1 "polyExploder" pos:[5,6] width:130 height:165
	button btn3 "about" pos:[80,190] width:48 height:24
	spinner spn1 "pieces" pos:[24,78] width:98 height:16 range:[0,10000,50] type:#integer scale:1
	spinner spn2 "thickness" pos:[25,109] width:96 height:16 enabled:true range:[0,100,1] type:#float scale:0.1
	checkbox chk1 "reactor Ready" pos:[34,140] width:87 height:15 checked:true
	checkbox chk2 "element Ready" pos:[34,140] width:87 height:15 checked:true

	on btn1 pressed do
	(
	 polyExp tarObj tarPieces thick rReady
	 )
	on btn2 picked obj do
	(tarObj=obj
	 btn2.caption=obj.name
	 preName=obj.name
	 btn1.enabled=true
	 )
	on btn3 pressed do
	(
	messagebox "polyExploder beta 1 8/30/2003--,ZhangYang, Email:yang.zhang@autodesk.com"
	)
	on spn1 changed val do
		tarPieces=val
	on spn2 changed val do
		thick=val
	on chk1 changed state do
		(rReady=state
		)
)

rolloutSFloater=newRolloutFloater "polyExploder" 150 260
addRollout polyExpRollout rolloutSFloater