/*
	Subobjects converter
	Created 2014-02-24
	Modifed 2016-03-24
	tested in 2012 max
	Author: Nikolay Litvinov
	Converts a subobjects as well as poke on the subobject with Crtl or Shift on the modify panel
*/
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [2/3/4/5]Ctrl[1]
macroScript Ctrl_1ToVertex Category:"Subobjects Converter" Tooltip:"Ctrl+1 To Vertex"
(
------------------FUNCTIONS------------------

fn Ctrl_1toVert_fn=(
for i in selection do
(
	case  subobjectlevel of 
	(
	2: try i.baseObject.ConvertSelection #Edge #Vertex 		catch (modPanel.getCurrentObject()).ConvertSelection #Edge #Vertex
	3: try i.baseObject.ConvertSelection #Border #Vertex 	catch (modPanel.getCurrentObject()).ConvertSelection #Border #Vertex
	4: try i.baseObject.ConvertSelection #Face #Vertex 		catch (modPanel.getCurrentObject()).ConvertSelection #Face #Vertex
	5: try i.baseObject.ConvertSelection #Element #Vertex 	catch (modPanel.getCurrentObject()).ConvertSelection #Element #Vertex
	)
)
subobjectLevel=1
)

fn Spl_2_Knot_Ctrl_1_fn =(
	spl_index_int=(getSplineSelection $)[1]
	KnotCount=numKnots $ spl_index_int
	knot_idx_ar= for i=1 to KnotCount collect i
	subObjectLevel=1
	for spl=1 to numSplines $ do
	setKnotSelection $ spl #()
	setKnotSelection $ spl_index_int knot_idx_ar
)

fn Seg_2_Knot_Ctrl_1_fn =(
ARarSeg=for spl=1 to numSplines $ collect  getSegSelection $ spl
arKnot=(
for i=1 to ARarSeg.count where ARarSeg[i].count!=0 do
for j=1 to ARarSeg[i].count  do appendIfUnique ARarSeg[i] (ARarSeg[i][j]+1)
for i=1 to ARarSeg.count do	if ARarSeg[i].count>numKnots $ i do deleteItem ARarSeg[i] ARarSeg[i].count
ARarSeg
)
for spl=1 to ARarSeg.count do
setKnotSelection $ spl arKnot[spl]
subObjectLevel=1
)

fn KnotType_CorBizcor_fn =(
knots_cur=for i=1 to numSplines $ collect getKnotSelection $ i as BitArray
global kntp
case kntp of	
(
undefined: (for i=1 to numSplines $ where not knots_cur[i].isempty do for j in knots_cur[i] do setKnotType $ i j #bezierCorner; kntp=true)
	true: (for i=1 to numSplines $ where not knots_cur[i].isempty do for j in knots_cur[i] do setKnotType $ i j #corner; kntp=undefined)
); updateShape $; CompleteRedraw()
)
-------------------------------SCRIPT-------------------------------
local gco = modPanel.getCurrentObject()
	case classOf gco of
	(
		Edit_Poly:	Ctrl_1toVert_fn()
	Editable_poly:	Ctrl_1toVert_fn()
	   Unwrap_UVW: (
						case SubobjectLevel of
							(
								1:	polyop.setVertSelection $ (gco.getSelectedGeomVertsByNode $)
								2: 	actionMan.executeAction 2077580866 "40099"	-- All Commands: Edge to Vertex
								3: 	actionMan.executeAction 2077580866 "40101"	-- All Commands: Face to Vertex
							);	subobjectLevel = 1
					)
		line: case SubobjectLevel of
				(
					1: KnotType_CorBizcor_fn()
					2: Seg_2_Knot_Ctrl_1_fn()
					3: Spl_2_Knot_Ctrl_1_fn()
				)
 SplineShape: case SubobjectLevel of
				(
					1: KnotType_CorBizcor_fn()
					2: Seg_2_Knot_Ctrl_1_fn()
					3: Spl_2_Knot_Ctrl_1_fn()
				)
	)
)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [1/4/5]Ctrl[2]
macroScript Ctrl_2ToEdge Category:"Subobjects Converter" Tooltip:"Ctrl+2 To Edge"
(
------------------FUNCTIONS------------------
fn Ctrl_2ToEdge_fn=(
for i in selection do
(
	case  subobjectlevel of
	(
	1: try i.baseObject.ConvertSelection #Vertex #Edge 	catch (modPanel.getCurrentObject()).ConvertSelection #Vertex #Edge
	4: try i.baseObject.ConvertSelection #Face #Edge 	catch (modPanel.getCurrentObject()).ConvertSelection #Face #Edge
	5: try i.baseObject.ConvertSelection #Element #Edge catch (modPanel.getCurrentObject()).ConvertSelection #Element #Edge
	)
)
subobjectLevel=2
)


fn Spl_2_Seg_Ctrl_2_fn =(
		spl_index_int=(getSplineSelection $)[1]
		splCount=numSegments $ spl_index_int
		arSpl=for i=1 to splCount collect i
		for i=1 to (numSplines $) do
		setSegSelection $ i #()
		setSegSelection $ spl_index_int arSpl keep: on
		subObjectLevel=2)

fn Knot_2_Seg_Ctrl_2_fn =(
ARarKnot=for spl=1 to numSplines $ collect  getKnotSelection $ spl
arSeg=(
for i=1 to ARarKnot.count where ARarKnot[i].count!=0 do
		(
	endKnot=numKnots $ i
		if isClosed $ i == off
		then
			for j=1 to ARarKnot[i].count  do
			(
				SecondSpl=ARarKnot[i][j]-1
				if SecondSpl !=0 do appendIfUnique ARarKnot[i] SecondSpl
				if ARarKnot[i][j]==endKnot do deleteItem ARarKnot[i] (findItem ARarKnot[i] endKnot)
			)
		else
			for j=1 to ARarKnot[i].count  do
			(
				SecondSpl=ARarKnot[i][j]-1
				if SecondSpl !=0 do appendIfUnique ARarKnot[i] SecondSpl
				if ARarKnot[i][j]==1 do append ARarKnot[i] endKnot
			)
		)
		ARarKnot
	)
for spl=1 to ARarKnot.count do
setSegSelection $ spl arSeg[spl]
subObjectLevel=2
)
------------------SCRIPT------------------
local gco = modPanel.getCurrentObject()
case classOf gco of
	(
		Edit_Poly:		Ctrl_2ToEdge_fn()
	Editable_poly:		Ctrl_2ToEdge_fn()
	   Unwrap_UVW:	(case SubobjectLevel of
	   					(
	   						1: 	actionMan.executeAction 2077580866 "40080"  -- All Commands: Vertex to Edge
	   						3: 	actionMan.executeAction 2077580866 "40102"	-- All Commands: Face to Edge
	   					);	subobjectLevel = 2
	   				)
		line: case SubobjectLevel of
				(
					1: Knot_2_Seg_Ctrl_2_fn()
					3: Spl_2_Seg_Ctrl_2_fn()
				)
 SplineShape: case SubobjectLevel of
				(
					1: Knot_2_Seg_Ctrl_2_fn()
					3: Spl_2_Seg_Ctrl_2_fn()
				)
	)
)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [1/2/4/5]Ctrl[3]
macroScript Ctrl_3ToBorder Category:"Subobjects Converter" Tooltip:"Ctrl+3 To Border"
(
------------------FUNCTIONS------------------
fn Ctrl_3ToBorder_fn=(
for i in selection do
(
	case  subobjectlevel of 
	(
	1: try i.baseObject.ConvertSelection #Vertex #Border 	catch (modPanel.getCurrentObject()).ConvertSelection #Vertex #Border
	2: try i.baseObject.ConvertSelection #Edge #Border 		catch (modPanel.getCurrentObject()).ConvertSelection #Edge #Border
	4: try i.baseObject.ConvertSelection #Face #Border 		catch (modPanel.getCurrentObject()).ConvertSelection #Face #Border
	5: try i.baseObject.ConvertSelection #Element #Border 	catch (modPanel.getCurrentObject()).ConvertSelection #Element #Border
	)
)
subobjectLevel=3
)



fn Knot_2_Spl_Ctrl_3_fn = (
ARarKnot=for spl=1 to numSplines $ collect  getKnotSelection $ spl
arSpl=for i=1 to ARarKnot.count where ARarKnot[i].count!=0 collect i
setSplineSelection $ arSpl
subObjectLevel=3
)

fn Seg_2_Spl_Ctrl_3_fn=(
ARarSeg=for spl=1 to numSplines $ collect  getSegSelection $ spl
arSpl=for i=1 to ARarSeg.count where ARarSeg[i].count!=0 collect i
setSplineSelection $ arSpl
subObjectLevel=3
)
------------------SCRIPT------------------
local gco = modPanel.getCurrentObject()
case classOf gco of
	(
	Edit_Poly: Ctrl_3ToBorder_fn()
Editable_poly: Ctrl_3ToBorder_fn()
   Unwrap_UVW:	(
					case SubobjectLevel of (
						1: 	actionMan.executeAction 2077580866 "40081"  -- All Commands: Vertex to Face
						2: 	actionMan.executeAction 2077580866 "40100"  -- All Commands: Edge to Face	
						);	subobjectLevel = 3
				)
		line: case SubobjectLevel of
				(
					1: Knot_2_Spl_Ctrl_3_fn()
					2: Seg_2_Spl_Ctrl_3_fn()
				)
 SplineShape: case SubobjectLevel of
				(
					1: Knot_2_Spl_Ctrl_3_fn()
					2: Seg_2_Spl_Ctrl_3_fn()
				)
	)
)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [1/2/3]Ctrl[4]
macroScript Ctrl_4ToFace Category:"Subobjects Converter" Tooltip:"Ctrl+4 To Face"
(
------------------FUNCTIONS------------------
fn Ctrl_4ToFace_fn=(
for i in selection do
(
	case  subobjectlevel of 
	(
	1: try i.baseObject.ConvertSelection #Vertex #Face 	catch (modPanel.getCurrentObject()).ConvertSelection #Vertex #Face
	2: try i.baseObject.ConvertSelection #Edge #Face 	catch (modPanel.getCurrentObject()).ConvertSelection #Edge #Face
	3: try i.baseObject.ConvertSelection #Border #Face 	catch (modPanel.getCurrentObject()).ConvertSelection #Border #Face
	)
)
subobjectLevel=4
)


------------------SCRIPT------------------
local gco = modPanel.getCurrentObject()
case classOf gco of
	(
		Edit_Poly: Ctrl_4ToFace_fn()
	Editable_poly: Ctrl_4ToFace_fn()
	)
)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [1/2/3/4]Ctrl[5]
macroScript Ctrl_5ToElement Category:"Subobjects Converter" Tooltip:"Ctrl+5 To Element"
(
------------------FUNCTIONS------------------
fn Ctrl_5ToElement_fn=(
for i in selection do
(
	case  subobjectlevel of 
	(
	1: try i.baseObject.ConvertSelection #Vertex #Element 	catch (modPanel.getCurrentObject()).ConvertSelection #Vertex #Element
	2: try i.baseObject.ConvertSelection #Edge #Element 	catch (modPanel.getCurrentObject()).ConvertSelection #Edge #Element
	3: try i.baseObject.ConvertSelection #Border #Element 	catch (modPanel.getCurrentObject()).ConvertSelection #Border #Element
	4: try i.baseObject.ConvertSelection #Face #Element 	catch (modPanel.getCurrentObject()).ConvertSelection #Face #Element
	)
)
subobjectLevel=5
)

------------------SCRIPT------------------
local gco = modPanel.getCurrentObject()
case classOf gco of 
	(
		Edit_Poly: Ctrl_5ToElement_fn()
	Editable_poly: Ctrl_5ToElement_fn()
	)
)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [2/4/5]Shift[1]
macroScript Shift_1ToVertex Category:"Subobjects Converter" Tooltip:"Shift+1 To Vertex"
(
------------------FUNCTIONS------------------
fn Shift_1ToVertex_fn=(
for i in selection do
(
	case  subobjectlevel of 
	(
	2: try i.baseObject.ConvertSelectionToBorder #Edge #Vertex 		catch (modPanel.getCurrentObject()).ConvertSelectionToBorder #Edge #Vertex
	4: try i.baseObject.ConvertSelectionToBorder #Face #Vertex 		catch (modPanel.getCurrentObject()).ConvertSelectionToBorder #Face #Vertex
	5: try i.baseObject.ConvertSelectionToBorder #Element #Vertex 	catch (modPanel.getCurrentObject()).ConvertSelectionToBorder #Element #Vertex
	)
)
subobjectLevel=1
)



fn KnotType_BizSmo_fn=(
knots_cur=for i=1 to numSplines $ collect getKnotSelection $ i as BitArray
global kntp1
case kntp1 of
(
undefined: (for i=1 to numSplines $ where not knots_cur[i].isempty do for j in knots_cur[i] do setKnotType $ i j #smooth; kntp1=true)
		true: (for i=1 to numSplines $ where not knots_cur[i].isempty do for j in knots_cur[i] do (setKnotType $ i j #smooth; setKnotType $ i j #bezier); kntp1=undefined)
); updateShape $; CompleteRedraw()
)

------------------SCRIPT------------------
local gco = modPanel.getCurrentObject()
case classOf gco of
	(
		Edit_Poly: Shift_1ToVertex_fn()
		Editable_poly: Shift_1ToVertex_fn()
		Unwrap_UVW:	(
						case SubobjectLevel of (
						1: polyop.setVertSelection $ (gco.getSelectedGeomVertsByNode $)
						2: actionMan.executeAction 2077580866 "40099"	-- All Commands: Edge to Vertex	
						3: actionMan.executeAction 2077580866 "40101"	-- All Commands: Face to Vertex
						);	subobjectLevel = 1
					)
		line: if SubobjectLevel==1 do KnotType_BizSmo_fn()
 SplineShape: if SubobjectLevel==1 do KnotType_BizSmo_fn()
	)
)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [1/4/5]Shift[2]
macroScript Shift_2ToEdge Category:"Subobjects Converter" Tooltip:"Shift+2 To Edge"
(
------------------FUNCTIONS------------------
fn Shift_2ToEdge_fn=(
for i in selection do
(
	case  subobjectlevel of 
	(
	1: try i.baseObject.ConvertSelectionToBorder #Vertex #Edge 		catch (modPanel.getCurrentObject()).ConvertSelectionToBorder #Vertex #Edge
	4: try i.baseObject.ConvertSelectionToBorder #Face #Edge 		catch (modPanel.getCurrentObject()).ConvertSelectionToBorder #Face #Edge
	5: try i.baseObject.ConvertSelectionToBorder #Element #Edge 	catch (modPanel.getCurrentObject()).ConvertSelection #Element #Edge
	)
)
subobjectLevel=2
)

------------------SCRIPT------------------
local gco = modPanel.getCurrentObject()
case classOf gco of
	(
Edit_Poly: Shift_2ToEdge_fn()
Editable_poly: Shift_2ToEdge_fn()
Unwrap_UVW: (
				case SubobjectLevel of (
				1: actionMan.executeAction 2077580866 "40080"  -- All Commands: Vertex to Edge
				3: actionMan.executeAction 2077580866 "40102"	-- All Commands: Face to Edge
				);	subobjectLevel = 2
			)
	)
)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [1/2]Shift[3]
macroScript Shift_3ToUVFace Category:"Subobjects Converter" Tooltip:"Shift+3 To UVFace + Cap-SelFace"
(
local gco = modPanel.getCurrentObject()
if classOf gco == Unwrap_UVW do
	case SubobjectLevel of (
		1: 	(actionMan.executeAction 2077580866 "40081"  -- All Commands: Vertex to Face
				subobjectLevel = 3)
		2: 	(actionMan.executeAction 2077580866 "40100"  -- All Commands: Edge to Face
				subobjectLevel = 3)
		)
)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [1][2][3]Shift[4]
macroScript Shift_4ToFace Category:"Subobjects Converter" Tooltip:"Shift+4 To Face"
(
------------------FUNCTIONS------------------
fn Shift_4ToFace_fn=(
for i in selection do
(
	case  subobjectlevel of 
	(
	1: try i.baseObject.ConvertSelection #Vertex #Face 	catch (modPanel.getCurrentObject()).ConvertSelection #Vertex #Face
	2: try i.baseObject.ConvertSelection #Edge #Face 	catch (modPanel.getCurrentObject()).ConvertSelection #Edge #Face
	3: try (i.baseObject.ConvertSelection #Border #Edge; i.baseObject.ConvertSelection #Edge #Face) 	
		catch ((modPanel.getCurrentObject()).ConvertSelection #Border #Edge; (modPanel.getCurrentObject()).ConvertSelection #Edge #Face)
	)
)
subobjectLevel=4
)

------------------SCRIPT------------------
local gco = modPanel.getCurrentObject()
	case classOf gco of (
	Edit_Poly: Shift_4ToFace_fn()
	Editable_poly: Shift_4ToFace_fn()
	)
)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [1/2/3/4]Shift[5]
macroScript Shift_5ToElement Category:"Subobjects Converter" Tooltip:"Shift+5 To Element"
(
local gco = modPanel.getCurrentObject()
	case classOf gco of (
	Edit_Poly:		macros.run "Subobjects Converter" "VEBF_Ctrl_Elem"
	Editable_poly:	macros.run "Subobjects Converter" "VEBF_Ctrl_Elem"
	)
)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [2/4]CtrlShift[1]
macroScript Ctrl_Shift_1ToVertex Category:"Subobjects Converter" Tooltip:"Ctrl+Shift+1 To Vertex"
(
------------------FUNCTIONS------------------
fn Ctrl_Shift_1ToVertex_fn=(
for i in selection do
(
	case  subobjectlevel of 
	(
	2: try i.baseObject.ConvertSelection #Edge #Vertex requireAll:on catch (modPanel.getCurrentObject()).ConvertSelection #Edge #Vertex
	4: try i.baseObject.ConvertSelection #Face #Vertex requireAll:on catch (modPanel.getCurrentObject()).ConvertSelection #Face #Vertex
	)
)
subobjectLevel=1
)


------------------SCRIPT------------------
local gco = modPanel.getCurrentObject()
case classOf gco of (
		Edit_Poly: Ctrl_Shift_1ToVertex_fn()
		Editable_poly: Ctrl_Shift_1ToVertex_fn()
	)
)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [1/4]CtrlShift[2]
macroScript Ctrl_Shift_2ToEdge Category:"Subobjects Converter" Tooltip:"Ctrl+Shift+2 To Edge"
(
------------------FUNCTIONS------------------
fn Ctrl_Shift_2ToEdge_fn=(
for i in selection do
(
	case  subobjectlevel of 
	(
	1: try i.baseObject.ConvertSelection #Vertex #Edge requireAll:on 	catch (modPanel.getCurrentObject()).ConvertSelection #Vertex #Edge
	4: try i.baseObject.ConvertSelection #Face #Edge requireAll:on 		catch (modPanel.getCurrentObject()).ConvertSelection #Face #Edge
	)
)
subobjectLevel=2
)


------------------SCRIPT------------------
local gco = modPanel.getCurrentObject()
case classOf gco of (
		Edit_Poly: Ctrl_Shift_2ToEdge_fn()
		Editable_poly: Ctrl_Shift_2ToEdge_fn()
	)
)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [1/2]CtrlShift[4]
macroScript Ctrl_Shift_4ToFace Category:"Subobjects Converter" Tooltip:"Ctrl+Shift+4 To Face"
(
------------------FUNCTIONS------------------
fn Ctrl_Shift_4ToFace_fn=(
for i in selection do
(
	case  subobjectlevel of 
	(
	1: try i.baseObject.ConvertSelection #Vertex #Face requireAll:on 	catch (modPanel.getCurrentObject()).ConvertSelection #Vertex #Face
	2: try i.baseObject.ConvertSelection #Edge #Face requireAll:on 		catch (modPanel.getCurrentObject()).ConvertSelection #Edge #Face
	)
)
subobjectLevel=4
)


------------------SCRIPT------------------
local gco = modPanel.getCurrentObject()
case classOf gco of (
		Edit_Poly: Ctrl_Shift_4ToFace_fn()
		Editable_poly: Ctrl_Shift_4ToFace_fn()
	)
)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- [4]CtrlAlt[2]
macroScript Ctrl_Alt_2FaceExtEdge Category:"Subobjects Converter" Tooltip:"Ctrl+Alt+2 FaceExt Edge"
(
gco=modpanel.GetcurrentObject()
if classof gco==Editable_poly and subobjectlevel==4 do
	(
	arF=gco.GetSelection #Face
	arE0=polyop.getEdgesUsingFace gco arF
	gco.SetSelection #Edge arE0
	gco.GrowSelection selLevel:#Edge
	arE1=gco.GetSelection #Edge
	detE=arE1-arE0
	gco.SetSelection #Edge detE
	subobjectlevel=2
	)
)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
macroScript SubObjectLevel_1_UseSoftSelection Category:"Subobjects Converter" Tooltip:"SubObjectLevel 1 UseSoftSelection"
(
    gco = modPanel.getCurrentObject()
    
    if (classof gco == SliceModifier) then 
    (
        case subobjectLevel of 
        (
            0: subobjectLevel = 1  -- Если уровень подобъекта 0, переключаемся на 1 (Slice plane)
            1: subobjectLevel = 0  -- Если уровень подобъекта 1, переключаемся на 0
        )
    )
    else
    (
        if (selection as array).count != 0 do
        (
            if (classOf gco != Editable_Poly) and (classof selection[1].baseobject == Editable_Poly) do 
                modPanel.setCurrentObject selection[1].baseobject node:selection[1]
            
            if subObjectLevel == 1 and keyboard.escPressed and (classOf gco == Editable_Poly or classOf gco == Edit_Poly or classof (selection as array)[1] == Editable_Poly or classof (selection as array)[1] == PolyMeshObject) 
            then gco.useSoftSel = (if gco.useSoftSel == off then true else false)
            else
            try macros.run "Modifier Stack" "SubObject_1"
            catch try (modPanel.setCurrentObject selection[1].baseobject node:selection[1]) 
            catch (if selection[1].category == #Standard_Primitives do 
                (
                    converttopoly selection[1]
                    max modify mode
                    subobjectlevel = 1
                )
            )
        )
    )
)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
macroScript SubObjectLevel_2_EdgeHardSmooth Category:"Subobjects Converter" Tooltip:"SubObjectLevel 2 EdgeHardSmooth"
(
	if (selection as array).count !=0 do
	(
/*
	global SetVertTicksParam_fn()
	fn SetVertTicksParam_fn = (nodeSelectionSet.setDisplayPropState  #DisplayProp_VertexTicks false; SetUIColor 65 [0,0,1])
	callbacks.removeScripts id:#SetVertTicksClr;
	callbacks.addScript #ModPanelSubObjectLevelChanged "SetVertTicksParam_fn()" id:#SetVertTicksClr
*/

gco=modPanel.getCurrentObject()

--if (classOf gco!=Editable_Poly) and (classof selection[1].baseobject == Editable_Poly) do modPanel.setCurrentObject selection[1].baseobject node:selection[1]

if classOf gco==Editable_Poly or classOf gco==Edit_Poly or classof (selection as array)[1]==Editable_Poly or classof (selection as array)[1]==PolyMeshObject
then
	if subObjectLevel==2
	then
		if (not (gco.GetSelection #Edge).isEmpty)
		then
		(
		if keyboard.escPressed 
		then gco.ButtonOp #MakeSmoothEdges 
		else gco.ButtonOp #MakeHardEdges
		max select
		)
		else 
			if keyboard.escPressed 
			then
			(
				gco=modPanel.getCurrentObject()
				if classof gco == Editable_Poly or classof gco == Edit_Poly do
				case gco.hardedgedisplay of
				(
				0: gco.hardedgedisplay=1
				1: gco.hardedgedisplay=0
				)
			)
			else (if classof $ == Editable_Poly then $.ButtonOp #SelectHardEdges else gco.ButtonOp #SelectHardEdges) 
	else (max modify mode; subobjectlevel = 2)
else 
try macros.run "Modifier Stack" "SubObject_2"
catch   try (modPanel.setCurrentObject selection[1].baseobject node:selection[1]) 
		catch (if selection[1].category==#Standard_Primitives do converttopoly selection[1]; max modify mode; subobjectlevel = 2)
	)
)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
macroScript SubObjectLevel_3_Cap Category:"Subobjects Converter" Tooltip:"SubObjectLevel 3 Cap"
(
if (selection as array).count != 0 do
(
    with undo on
    (
        local gco = modPanel.getCurrentObject()
        
        -- Задержка для стабилизации стека undo
        sleep 0.1
        
        if (classOf gco != Editable_Poly) and (classof selection[1].baseobject == Editable_Poly) do 
            modPanel.setCurrentObject selection[1].baseobject node:selection[1]
        
        if classOf gco == Editable_Poly or classOf gco == Edit_Poly or classof (selection as array)[1] == Editable_Poly or classof (selection as array)[1] == PolyMeshObject then
        (
            if (subobjectLevel == 3 and (not (gco.GetSelection #Edge).isEmpty)) then
            (
                -- Регистрируем операцию в стеке undo
                macros.run "Editable Polygon Object" "EPoly_Cap"
                -- Добавляем паузу для стабилизации стека undo
                sleep 0.1
                max select
            )
            else 
            (
                max modify mode
                sleep 0.05
                subobjectlevel = 3
            )
        )
        else 
        (
            try 
            (
                macros.run "Modifier Stack" "SubObject_3"
            ) 
            catch 
            (
                try 
                (
                    modPanel.setCurrentObject selection[1].baseobject node:selection[1]
                ) 
                catch 
                (
                    if selection[1].category == #Standard_Primitives do 
                    (
                        converttopoly selection[1]
                        sleep 0.05
                        max modify mode
                        sleep 0.05
                        subobjectlevel = 3
                    )
                )
            )
        )
    )
)
)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
macroScript SubObjectLevel_4_AutoSmooth Category:"Subobjects Converter" Tooltip:"SubObjectLevel 4 AutoSmooth"
(
	if (selection as array).count !=0 do
	(
gco=modPanel.getCurrentObject()
if (classOf gco!=Editable_Poly) and (classof selection[1].baseobject == Editable_Poly) do modPanel.setCurrentObject selection[1].baseobject node:selection[1]

if classOf gco==Editable_Poly or classOf gco==Edit_Poly or classof (selection as array)[1]==Editable_Poly or classof (selection as array)[1]==PolyMeshObject
then (
	--if subobjectlevel==2 do (macros.run "Subobjects Converter" "Ctrl_4ToFace"; macros.run "Modifier Stack" "SubObject_4")
	if (subObjectLevel==4) and (not (fsel=getFaceSelection selection[1].mesh).isEmpty) and not(fsel.numberset==fsel.count)
	then
		(
			case classof gco of
			(
			Editable_Poly: 
			if keyboard.escPressed
			then (gco.ConvertSelectionToBorder #Face #Edge; gco.ButtonOp #MakeSmoothEdges)
			else (gco.autoSmoothThreshold = 180; gco.autosmooth() gco.autoSmoothThreshold = 45)
			Edit_Poly:
			if keyboard.escPressed 
			then (gco.ConvertSelectionToBorder #Face #Edge; gco.ButtonOp #MakeSmoothEdges)
			else (gco.autoSmoothThreshold = 180; gco.ButtonOp #Autosmooth; gco.autoSmoothThreshold = 45)
			)
		max select
		)
	else 
		if (subObjectLevel==4) and fsel.numberset==fsel.count and keyboard.escPressed
		then try(selection[1].modifiers[1].SmGrpFloater()) catch $.SmGrpFloater()
		else (max modify mode; subobjectlevel = 4)
	)
else 
try macros.run "Modifier Stack" "SubObject_4"
catch (if selection[1].category==#Standard_Primitives do converttopoly selection[1]; max modify mode; subobjectlevel = 4)
	)
)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
macroScript SubObjectLevel_5_forUnwrapUVW Category:"Subobjects Converter"	Tooltip:"SubObjectLevel 5 for UnwrapUVW"
(
if classof (selection as array)[1]==Editable_Poly or classof (selection as array)[1]==PolyMeshObject
then(
	fsel=getFaceSelection selection[1].mesh
	if (fsel.numberset==fsel.count and keyboard.escPressed)
	then 

(
AllFaces=#{1..$.getNumFaces()}
SG_level= for i=1 to 32 collect 2^i
sg=1
n=1
while AllFaces.numberset>0 do
(
polyop.setFaceSelection $ (AllFaces as array)[1]
$.selectElement()
FtoEl=polyop.getFaceSelection $
polyop.setFaceSmoothGroup $ FtoEl 0 add: false
polyop.setFaceSmoothGroup $ FtoEl sg add: false
AllFaces-=FtoEl
sg=SG_level[n]
n+=1
sg=(if sg==SG_level[32] then (n=1; 1) else sg)
)
)
	
	else 
	try macros.run "Modifier Stack" "SubObject_5"
	catch (if selection[1].category==#Standard_Primitives do converttopoly selection[1]; max modify mode; subobjectlevel = 5)
	)
else
if classof (modpanel.getcurrentobject())==Unwrap_UVW then
(
fn EditUVWs_isOpen_fn =
(
	EditUVWs_isOpen = false
	ChildWindows = UIAccessor.GetChildWindows (DialogMonitorOPS.GetWindowHandle())
	--for i in ChildWindows do format "%\n" (UIAccessor.GetWindowText i)
	for i in ChildWindows where (UIAccessor.GetWindowText i)=="Edit UVWs" do exit with EditUVWs_isOpen = true
	EditUVWs_isOpen
)

	desktopHWND = windows.getDesktopHWND()
	desktopChildren = windows.getChildrenHWND desktopHWND
	if EditUVWs_isOpen_fn()
	then
	(
	Toolbar_InternalSelect_hwnd=for i in desktopChildren where (UIAccessor.GetWindowText i[7])=="_InternalSelect" and i[4]=="CustToolbar" do (exit with i[1])
	Toolbar_InternalSelect_Elements=(windows.getChildrenHWND Toolbar_InternalSelect_hwnd)
	SelectByElementUVToggle_Button_hwnd=Toolbar_InternalSelect_Elements[5][1]
	UIAccessor.PressButton SelectByElementUVToggle_Button_hwnd
	)
	else
	(SelectByElementUVToggle_Button_hwnd=for i=1 to desktopChildren.count where desktopChildren[i][5]=="Selection" do (exit with desktopChildren[(i+7)][1])
	UIAccessor.PressButton SelectByElementUVToggle_Button_hwnd)
)


)