createSplitPoly	

sub createSplitPoly	

'///store space in memory for 3 custom variables
dim cust_var1, cust_var2, cust_var3

'///store space for a variable for the new name of the duplicated object
dim new_objname

'///store space for the inverse selection of polygons
dim inv_polysel

'///store space for the selection of polygons on the new object
dim new_polysel

'///store space for variable to hold the scene roots name
dim mySceneroot

'///store space for variable to contain the selected polygons
dim polysel

'///store space for variable to store the name of the object with the selected polygons
dim objsel

'///stores all elements below the Project level in mysceneroot
set mySceneRoot = EnumElements("Project", TRUE)

'///filters mysceneroot by scenes, leaving just the scene name
set mySceneRoot = SIFilter(mySceneRoot, "Scene", TRUE )(0)

'///selects all the children of the scene node at a project level
set mySceneRoot = EnumElements( mySceneRoot , TRUE)

'///filters the myscenerootby  sceneobjects - the sceneroot is the object in the selection
'///and hence we are left with the variable mysceneroot holding the name of the scene root
set mySceneRoot = SIFilter(mySceneRoot, "SceneObject", TRUE)(0)

'///store the selection list - ie the selected polygons in the variable polysel
set polysel = getvalue("selectionlist")

'///switch to object selection mode and store the name of the object in the variale objsel
ActivateObjectSelTool
set objsel = getvalue("selectionlist")

'///ADD CUSTOM PARAMETER TO SCENE ROOT AND VARIABLES TO THAT

AddProp "Custom_parameter_list",mySceneRoot ,, "PolyDupe"
SIAddCustomParameter "PolyDupe", "Keep Old Object", siBool, 1, 0.000, 1.000, , 5, 0.000, 1.000
SIAddCustomParameter "PolyDupe", "Create Selected", siBool, 1, 0.000, 1.000, , 5, 0.000, 1.000
SIAddCustomParameter "PolyDupe", "Create UnSelected", siBool, 0, 0.000, 1.000, , 5, 0.000, 1.000


'///call an inspection window of the custom parameter set, with the ok/cancel button enabled.

'///the on error resume next command preceding the inspection window tells XSI to carry on
'///if an error occurs - as would happen if the user hits cancel in the inspection box
'///as this is interperated as an error command

on error resume next
InspectObj mySceneRoot & "." & "PolyDupe",,"Polygon Cutter",4

'///store the custom parameters in memory
cust_var1 = getvalue("PolyDupe"&".Keep_Old_Object") 
cust_var2 = getvalue("PolyDupe"&".Create_Selected") 
cust_var3 = getvalue("PolyDupe"&".Create_UnSelected") 

'///if ok is clicked
if err.number = 0 then 

	'///checks to see if the user has specified an object to make
	if cust_var2 + cust_var3 <> False then 
	
		'///the user wants to generate geometry from selected polygons
		if cust_var2 = True then 
		
			'///duplicate the selected object and store its name in the variable new_obj_name
			set new_obj_name = duplicate(objsel)
			'///rename the new duplicate to the origonally selected object but specifying _sel on the end
			SetValue new_objname&".Name", objsel&"_sel"

			'///select the newly duplicated object, refresh to avoid errors, invert the selection,
			'///and delete the new selected polygons(which have been stored in the inv_polsel variable.)

			SelectObj objsel&"_sel"
			refresh
			SetSelFilter "Polygon"
			InvertSelection "Polygon"
			set inv_polysel = getvalue("selectionlist")
			ApplyTopoOp "DeleteComponent", inv_polysel, siUnspecified, siImmediateOperation
			end if
			
		'///the user wants to make geometry from unselected polys
		'///same as above, but this time the polygon selection is not inverted but the selection
		'///does need to be stored in a new variable as it is not on the origonal object

		if cust_var3 = True then
		
			set new_objname = duplicate(objsel)
			SetValue new_objname&".Name", objsel&"_unsel"
			SelectObj objsel&"_unsel"
			refresh
			SetSelFilter "Polygon"
			set new_polysel = getvalue("selectionlist")
			ApplyTopoOp "DeleteComponent", new_polysel, siUnspecified, siImmediateOperation
			end if
	
		'///the user has indicated that he wants to remove the old object
		if cust_var1 = False then
		
			'///delete the origonal object with the origonal poly selection
			DeleteObj objsel
			end if
		else

		'///the user has neglected to specify either to make geometry from the selected polygons
		'///or from the unselected polygons
		
		logmessage "Hmmmm....You want me to make no objects then eh?"
		end if

	
	'///see if the user has hit the cancel button
	else logmessage "aborted"
	end if
	
	'///remove the parameter set after use
	deleteObj scenename & "PolyDupe"

end sub
