/*
***  CADcleaner:  ***
***  Utility Panel Script  ***

Script to weld vertices and clean up 	
smoothing in imported/converted CAD models.  	
Works on Editable_Mesh objects in selection.
Does not work on objects with an Edit_Mesh
or Edit_Poly modifier applied.
Jeff Brown, Fire Mist Media ---		
*/

utility CADcleaner "CAD Cleaner" width:162 height:120
(
-- VARIABLES --
global weldThresh   = 0.001
global smoothThresh = 25

-- FUNCTIONS --

function WeldVerts obj =
	(
	meshOp.weldVertsByThreshold obj obj.verts weldThresh
	)

function smoothMesh obj = 
	(
	meshOp.autoSmooth obj obj.faces smoothThresh
	)

-- UI --
-- create utility panel UI
	spinner spin_weld "weld thresh." pos:[22,7] width:127 height:16 range:[-10,10,0.001]
	spinner spin_smooth "smooth thresh." pos:[10,28] width:139 height:16 range:[0,90,25]
	button but_execute "Execute!" pos:[52,49] width:58 height:21
	progressBar progBar "           progress"
	value:0 pos:[10,90] width:140 height:20 color:(color 230 150 50)


-- create error requestor
rollout ro_noMesh "CAD Cleaner"
	(
	label ro_label "No Editable Mesh objects selected"
	button rmbutCancel    "  --- Cancel ---  "
	on rmbutCancel   pressed do
		(
		destroyDialog ro_noMesh
		)
	)		

-- Script Execution --

	on but_execute pressed do
	(
	weldThresh	 = spin_weld.value
	smoothThresh = spin_smooth.value
	mesh_flag = true
	if $ == undefined then
		(
		createDialog ro_noMesh width:220 modal:true
		)
	else
		( 
		for obj in $ do
			(
			if classOf obj.baseObject == Editable_Mesh then
				(
				WeldVerts obj
				SmoothMesh obj
				mesh_flag = false
				)
			)
		if mesh_flag then
	 		(
			createDialog ro_noMesh width:220 modal:true
			)
		)
	redrawViews()
	)
)-- end of script --
