macroScript slicer_v5 category:"zoa scripts"

--------------------------------------
--	slicer v5
--	2011 by zoa                    
--	Balogh Andras Miklos (balogh.andras@zoa.hu)
--	www.zoa3d.com                   
--------------------------------------
--	v 5.00
--	Last Modified On: 02/03/2012
--	Created On: 02/11/2009
---------------------------------------


-------------------------------------------------------------------------------
--   	Description:
--	
--	Slices an editable poly along its x, y or z axis by distance or by a number of parallel planes.
--	You can also slice along a spline. Alternatively you can split - cap the resulting elements.
-------------------------------------------------------------------------------
--	Notes and limitations:
--
--	No limitations yet... Found a bug? Report it!
-------------------------------------------------------------------------------
--	Revision History:
--
--	v 5.00 - 07.11.2011 - Created this text file for the first well-working version
-------------------------------------------------------------------------------





(
undo off
local v_bb	= #([-100,-100,-100], [100,100,100])
if $!=undefined then v_bb= nodeLocalBoundingBox $	
try(destroyDialog zoaslicer)catch()

rollout zoaslicer "slicer" width:180 height:400
	(
	
		
	label lb_1	"slice by:" pos:[5,5]
	
	checkbox chk_1 "pick line (z)" pos:[5,30] checked: true
		pickbutton pb_1 "pick line" pos:[90,28] width:80 --height:20	
	checkbox chk_2 "count" pos:[5,55]	
		spinner s_count  type:#integer range:[1,3000,10] width:80 pos: [90,57]	
	checkbox chk_3 "distance" pos:[5,80]
		spinner s_dist  type:#float range:[0.01,30000,(v_bb[2][1]-v_bb[1][1])/(s_count.value+1)] width:80  pos:[90,82]
	
	checkbox chk_4 "x" pos:[5,120]
	checkbox chk_5 "y" pos:[5,145]
	checkbox chk_6 "z" pos:[5,170]	
		
	checkbox chk_7 "split" pos:[90,120]
	checkbox chk_8 "cap" pos:[90,145]
	checkbox chk_9 "create line" pos:[90,170]	
		
	button doit "slice!" width:80  pos:[90,210]
		
	edittext nam_e "name:" text:"name_" pos:[5,250]
	button btn_exp "explode"  pos:[90,275] width:80
		
		
		
	fn create_line i =
		(
		polyop.createShape $ (polyop.getedgeselection $) smooth:false name:($.name+"_spline_"+i as string)
		)
		
		
	fn slice_by_line =
		(
		if pb_1.object!=undefined and (classof pb_1.object)==line then
			(
			i=0
			undo on
			for s = 1 to (numSplines pb_1.object) do
				(
				for k = 1 to (numKnots pb_1.object s)-1 do
					(
					i+=1
					polyop.setedgeselection $ #() 
					p1= (getKnotPoint pb_1.object s k)
					p2 = (getKnotPoint pb_1.object s (k+1))
					$.slice [p1[2]-p2[2],p2[1]-p1[1], 0] [p1[1]-$.pos[1], p1[2]-$.pos[2],-$.pos[3]]
					if chk_9.state==true then  ( create_line i )
					if chk_7.state==true then
						(
						$.EditablePoly.splitEdges ()	
						if chk_8.state==true then
							(	
							if chk_8.state==true then $.capHoles #Object
							)
						)	
					)
				)
			)
		)
		
	fn slice_by_count arg typ=
		(
		print v_bb
		local n
		local d
		local d_0
		l=v_bb[2][arg]-v_bb[1][arg]
		if typ==1 then 
			(
			n=s_count.value
			d=[l/(n+1),l/(n+1),l/(n+1)]
			d_0=v_bb[1]-$.pos
			)
		if typ==2 then
			(
			d=[s_dist.value,s_dist.value,s_dist.value]
			n=ceil (l/d[1])
			d_0=v_bb[1]-$.pos+(l-n*d)/2
			)
		pr=[0,0,0]
		if arg==1 then pr=[1,0,0]
		if arg==2 then pr=[0,1,0]
		if arg==3 then pr=[0,0,1]

		if (polyop.getFaceSelection $ as array).count==0 then f=off else f= on
		
		polyop.setedgeselection $ #() 
		undo on
		for i= 1 to n do
			(
			$.EditablePoly.slice pr (i*d+d_0) flaggedFacesOnly:f
			)	
			
		if chk_9.state==true then  ( create_line i )
		if chk_7.state==true then
			(
			$.EditablePoly.splitEdges ()	
			if chk_8.state==true then
				(	
				if chk_8.state==true then $.capHoles #Object
				)
			)	
		)
	
		

	fn p1 =
		(	
		chk_1.state= true	
		chk_2.state= false	
		chk_3.state= false		
		pb_1.enabled=true
		s_count.enabled=false
		s_dist.enabled=false
		chk_4.enabled= false	
		chk_5.enabled= false	
		chk_6.enabled= false		
			
		)
		
	fn p2 =
		(	
		chk_1.state= false	
		chk_2.state= true	
		chk_3.state= false		
		pb_1.enabled=false
		s_count.enabled=true
		s_dist.enabled=false
		chk_4.enabled= true	
		chk_5.enabled= true	
		chk_6.enabled= true		
		)
			
	fn p3 =
		(	
		chk_1.state= false	
		chk_2.state= false	
		chk_3.state= true	
		pb_1.enabled=false
		s_count.enabled=false
		s_dist.enabled=true
		chk_4.enabled= true	
		chk_5.enabled= true	
		chk_6.enabled= true		
		)
		
	on zoaslicer open do
		(
		p1()	
		chk_8.enabled=false
		)	
		
	on chk_1 changed arg do	
		(
		if arg=true then (p1()) else (p2())
		)
		
	on chk_2 changed arg do	
		(
		if arg=true then (p2()) else (p1())
		)
		
	on chk_3 changed arg do	
		(
		if arg=true then	(p3()) else (p1())
		)
		
	on s_count changed arg do
		(
		x=(v_bb[2][1]-v_bb[1][1])
		y=(v_bb[2][1]-v_bb[1][1])	
		z=(v_bb[2][1]-v_bb[1][1])	
		aver=(x+y+z)/3
		s_dist.value=(aver)/(s_count.value+1)
		)
	
	on s_dist changed arg do
		(
		x=(v_bb[2][1]-v_bb[1][1])
		y=(v_bb[2][1]-v_bb[1][1])	
		z=(v_bb[2][1]-v_bb[1][1])	
		aver=(x+y+z)/3
		s_count.value=(ceil (aver/arg)-1)
		)
		
	on pb_1 picked obj do
		( 
		if obj != undefined do
			(
			pb_1.text = obj.name
			)
		)
		
	on chk_7 changed arg do	
		(
		chk_8.enabled=arg
		if arg== false then chk_8.state=false
		)

	on doit pressed do
		(
		v_bb= nodeLocalBoundingBox $	
		if (classof $)==Editable_Poly then
			(
			polyop.setEdgeSelection $ #()
			if chk_8.state==true then $.capHoles #Object
			
			if chk_1.state == true then	
				(
				slice_by_line ()
				)
				
			if chk_2.state == true then	
				(
				if chk_4.state == true  then slice_by_count 1 1
				if chk_5.state == true  then slice_by_count 2 1	
				if chk_6.state == true  then slice_by_count 3 1
				)
				
			if chk_3.state == true then	
				(
				if chk_4.state == true  then slice_by_count 1 2
				if chk_5.state == true  then slice_by_count 2 2	
				if chk_6.state == true  then slice_by_count 3 2
				)
			)
		)
		
		
	on btn_exp pressed do 
		(
		if (((getcurrentselection()).count == 1) and ($ != undefined)) then 
			(
			if classof $.baseobject == Editable_Poly do
				(
				subobjectlevel=4	
				local temp_poly =#() as bitarray
				local temp_elements =#() as bitarray
				local temp_count = 0
			
				if (((getcurrentselection()).count == 1) and ($ != undefined)) then 
					(
					if classof $.baseobject == Editable_Poly do
						(
						temp_poly_selection = polyop.getFaceSelection $
						 for p = 1 to polyop.getnumfaces $ do append temp_poly p
						
						 for i = 1 to polyop.getnumfaces $ do
							(
							if (findItem temp_poly i)!=0 do
								(
								temp_count += 1
								append temp_elements i
								polyop.setFaceSelection $ i
								$.editablepoly.SelectElement()
								temp_poly = temp_poly - (polyop.getFaceSelection $)
								)
							)
						j1=0
						for i in temp_elements do
							(
							j1+=1
							if j1<1000 then
								(
								if j1<100 then
									(
									if j1<10 then
										(
										j2="000"+j1 as string
										)else(
										j2="00"+j1 as string
										)	
									)else(
									j2="0"+j1 as string
									)
								)else(
								j2=j1 as string
								)
							polyop.setFaceSelection $ i
							$.editablepoly.SelectElement()
							polyop.detachFaces $ (polyop.getFaceSelection $) delete:false asnode:true name:(nam_e.text+j2)
							)
							delete $
						)
					)
				)
			)
		)
		

	)
	
createdialog zoaslicer style:#(#style_toolwindow, #style_sysmenu) pos:[20,100]
)