'jMergeCleanUI_04 (c)
'javier@internarte.com
'info: 
'MergeClean bypasses XSI Merging restrictions allowing:

'1. Branch selection for merging /
'2. Surfmesh merging (automatically performing nurbs2mesh and cleaning up)
'3. 'dirty' selection (automatically filtering nonpolymeshes out)
'4. It then cleans out clusters and freezes modelling relations. 

'UI Control 

'To modify initial values edit: ".values" within paragraph'//UI ***


'********************************************************************************
'option Explicit

ActivateObjectSelTool	' this will show you!

dim oSel, oKidSel, oPolySel, oDelSel, oObj, oItem, oUparam, oVparam, oNit, index
dim oRoot, oInput, oTol, oBlend, oMod, oOrig, oClus, oNote1

Set oRoot = ActiveProject.ActiveScene.Root

set oSel = CreateObject("XSI.Collection")		'collection of Initial Selection
set oKidSel = CreateObject("XSI.Collection")	'collection of Children
set oPolySel = CreateObject("XSI.Collection")	'collection of Polymeshes
set oDelSel = CreateObject("XSI.Collection")	'collection to be deleted

oVparam = Getvalue(oRoot&".geomapprox.gapproxvwvstep")
oUparam = Getvalue(oRoot&".geomapprox.gapproxvwustep")
			
'//convert to Branch Selection if Necessary
oSel.additems selection
FOR index = 0 to oSel.Count -1
		set oItem = oSel(index)
		if oItem.Branchflag = 1 then
			SelectChildNodes
		end if
	oKidSel.additems selection	
	DeselectAll
NEXT

'//discriminate non polymsh , convert surfmsh to polymsh
FOR index = 0 to oKidSel.Count -1
		set oItem = oKidSel(index)
		
		if oItem.type = "polymsh" then
			oPolySel.add oItem
		end if 
		
		if oItem.type = "surfmsh" then
			ApplyOp "NurbsToMesh", oItem, 3, siPersistentOperation
			set oNit = selection(0)
				SetValue ".polymsh.nurbstomesh.stepu", oUparam
				SetValue ".polymsh.nurbstomesh.stepv", oVparam
			FreezeObj , , siUnspecified
			oPolySel.add oNit 
			oDelSel.add oNit
		end if
	Selectobj oPolysel	
NEXT

IF oPolySel.count > 1 THEN

'//UI 
	
	Set oInput =oRoot.AddProperty("custom_parameter_list",,"MERGE PARAMETERS")
	Set oNote1 =oInput.AddParameter2( "THIS MERGES", siString,,,,,,,2) 
	set oTol = oInput.AddParameter2( "Tolerance", siDouble,0.01,0.01,1,0.01,1,siPersistable,4)		
	set oBlend = oInput.AddParameter2( "Blend", siBool,,,,,,,4)	
	set oMod = oInput.AddParameter2( "Frz Modelling Relations", siBool,,,,,,,4)	
	set oClus = oInput.AddParameter2( "Delete Clusters", siBool,,,,,,,4)			
	set oOrig = oInput.AddParameter2( "Delete Original Models", siBool,,,,,,,4)
		'********************* UI DEFAULT VALUES >
		oNote1.value = "poly & surfmeshes, filters selection & cleans up"
		oTol.value = 0.01
		oBlend.value = 0
		oMod.value = 1
		oClus.value = 1
		oOrig.value = 0
		'<********************

'//Open UI up ***comment line out to disable UI>>
	on Error Resume Next
		InspectObj oInput,,"MERGE PARAMETERS",siModal
	if err.Number = 0 then
	'<<***

		logmessage "&Number of Merged polymeshes :"&oPolySel.count 
		
		'//merge

		ApplyOp "MeshMerge", oPolySel, 3, siPersistentOperation
			SetValue ".polymsh.mergemesh.tolerance", oTol.value
			SetValue ".polymsh.mergemesh.blend", oBlend.value
		set oObj = selection(0)

		'//cleanup
		

		if oMod.value = "True" then
			Freezeobj			'freeze modeling relations
			DeleteObj oDelSel	 'delete polymsh converted from surfmeshes
			if oOrig.value = "True" then	'delete orignal models
				DeleteObj oSel		'only if merged object is frozen			
			end if
		elseif  oOrig.value = "True" then			
				logmessage "to delete original objects you must freeze relations!--NO OBJECTS DELETED"
		end if
		if oClus.value = "True" then
			DeleteObj oObj&".cls.*"	'delete clusters		
		end if

	else
		selectobj oSel
	end if
	DeleteObj oInput 			'cleanup UI
ELSE
		msgbox "nothing to merge!!!"
		selectobj oSel
END IF
