'jMergeClean_01
'javier@internarte.com
'info: 
'
'This version has no UI but you can set default merge values. 
'I find it faster in the long run.
'
'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, optionally 
'	deleting  
'Comment out last 3 lines to stop this.
'********************************************************************************
option Explicit

dim oSel, oKidSel, oPolySel, oDelSel, oObj, oItem, oUparam, oVparam, oNit, index
dim Merge_Blend,Merge_Tolerance,Freeze_Relations,Delete_Clusters,Delete_Original

'<<<*******************
'EDITABLE DEFAULT MERGE SETTINGS 

Merge_Blend = 0			'0=no, 1=yes	'merge parameter
Merge_Tolerance = 0.01		'range 0.01 - 1	'merge parameter
Freeze_Relations = 1		'0=no, 1=yes	'
Delete_Clusters = 1		'0=no, 1=yes	'
Delete_Original = 0		'0=no, 1=yes	'original geometry 

'note:original geometry will only be deteted if relations are frozen
'>>>*******************



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("Scene_Root.geomapprox.gapproxvwvstep")
oUparam = Getvalue("Scene_Root.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

'//filter 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			
NEXT
if oPolysel.count > 1 then
	'//merge
	logmessage oPolysel.count&"objects have been merged"
	ApplyOp "MeshMerge", oPolySel, 3, siPersistentOperation
		SetValue ".polymsh.mergemesh.tolerance", Merge_Tolerance
		SetValue ".polymsh.mergemesh.blend", Merge_Blend
		set oObj = selection(0)


	'//cleanup
	if Freeze_Relations = 1 then
		Freezeobj
		if Delete_Original = 1 then
			DeleteObj oSel
		end if
	end if
	
	if Delete_Clusters = 1 then
		DeleteObj oObj&".cls.*"		'delete clusters
	end if
	
	DeleteObj oDelSel	 'delete polymeshes converted from surfmeshes

		
else 
	Selectobj oSel	
	msgbox "NOTHING MERGED .......     "
END IF

DeleteObj oDelSel	 'delete polymeshes converted from surfmeshes
