'******************************************************************************
'HRC_LW_Import.vbs
'Version: 3.0.1
'created by: Jabbar Raisani 2-13-03
'Purpose: Created mainly for importing LightWave files exported as .hrc files
'			and creating separate objects based on clusters
'Description: Asks user to select .hrc file, then pick the actual geoemetry to
'breakup.  Script then creates individual objects based on the polygonal clusters 
'Script will also rename objects based on relative clusters and create a backup
'******************************************************************************

Option Explicit



'*******************************************************************************
'main
'*******************************************************************************

main

function main 
	
	
	Dim oSelection, oItem, oGeom, oClusters, oObject, oCls, i, result
	i = 0
	
	ImportFromSI3D , False 
	
	PickElement "geometry", "Pick PolyMesh", "Pick PolyMesh", oSelection
	
	'exit if nothing is selected
	if oSelection = "Nothing" then
		exit function
	end if
	
	SelectObj oSelection
	
	'Loop over the selection and send valid items to ClusterInspect()
	for each oItem in selection
		if oItem.type = "polymsh" then
			result = DeleteNonClusterPolys(oItem) 'result should be true unless user cancel
			if i=0 then
				i = i+1
				result = DeleteAllClusterPolys(oItem) 'result should be true unless user cancel
			end if
			if result = False then
				exit for
			end if
		end if
	next
end function

'*******************************************************************************
'DeleteNonClusterPolys
'returns true if executes successfully
'*******************************************************************************

function DeleteNonClusterPolys(oItem)

Dim oGeom, oMainClusters, oClusters,k, j, NewItem, oCls, BkpItem, oSelection, oClsSelection, oClsName

set oGeom = oItem.ActivePrimitive.Geometry

	'Store poly clusters in oMainClusters	
	set oMainClusters = oGeom.Clusters.Filter("poly")		
	if typename(oMainClusters) = "Nothing" then
		logmessage "no polygon clusters found"
		exit function
	end if
	
	'create backup (Delete the following 5 lines if you don't need a backup
	Duplicate oItem, , , 1, , , , , 0
	for each BkpItem in selection
		BkpItem.Name = oItem.Name+"_Backup"
		ToggleVisibility
	next

	'create first obj to be edited
	Duplicate oItem, , , 1, , , , , 0
	Set oSelection = GetValue( "SelectionList" )
	
	'loop over clusters
	for k = 0 to oMainClusters.count -1

		for each NewItem in selection
			set oGeom = NewItem.ActivePrimitive.Geometry
		next
		'create cluster storage
		set oClusters = oGeom.Clusters.Filter("poly")		
		
		'set cluster that will create new obj
		set oCls = oClusters(0)	
		
		'rename object based on cluster
		oClsName = oCls.Name
		for each NewItem in selection
			NewItem.Name =oCls.Name 
		next		
		
		'Select cluster to be kept
		SelectObj oCls
		
		'set polygons to be deleted
		SelectMembers
		InvertSelection "Polygon", siIgnoreComponentVisibility
		set oClsSelection = GetValue( "SelectionList" )

		'Delete cls to avoid repeating same cluster
		DeleteObj oCls
		
		'delete all clusters and create next obj
		if k < oMainClusters.count -1 then
			Duplicate oSelection, , , 1, , , , , 0
			Set oSelection = GetValue( "SelectionList" )
			DeleteObj oClusters
		end if
		
			'delete polygon components
			SelectObj oClsSelection
			ApplyTopoOp "DeleteComponent", , siUnspecified, siPersistentOperation
		
			
			'select next obj for poly deletion 
			SelectObj oSelection	
	next

	'Set the return value
	DeleteNonClusterPolys = True   		

end function

'*******************************************************************************
'DeleteAllClusterPolys
'returns true if executes successfully
'*******************************************************************************

function DeleteAllClusterPolys(oItem)

	Dim oGeom, oClusters
	set oGeom = oItem.ActivePrimitive.Geometry
	
	'check for clusters
	set oClusters = oGeom.Clusters.Filter("poly")		
	if typename(oClusters) = "Nothing" then
		logmessage "no polygon clusters found"
		exit function
	end if

	'Delete polys that are members of clusters and clusters
	SelectObj oClusters
	SelectMembers
	ApplyTopoOp "DeleteComponent", , siUnspecified, siPersistentOperation
	DeleteObj oClusters

	DeleteAllClusterPolys = True

end function
