/*//////////   SCRIPT:   *.mat file to lower version v1.1   //////////*/
/*//////////   WRITTEN BY:   Branko Zivkovic   //////////*/
/*//////////   EMAIL:   barigazy@hotmail.com   //////////*/
(
try(destroyDialog ::libRoll)catch()
rollout libRoll "*.mat file to lower version v1.1"
(
	local libFile
	fn defColor r g b = ((dotNetClass "System.Drawing.Color").FromArgb r g b)
	local BC = defColor 180 180 180, FC = defColor 30 30 30
	fn defLabel ctrl bClr: fClr: txtAlign: txt: =
	(
		ctrl.TextAlign = case txtAlign of
		(
			#left: ctrl.TextAlign.TopLeft
			#center: ctrl.TextAlign.MiddleCenter
		)		
		ctrl.Text = txt ; ctrl.BackColor = bClr ; ctrl.ForeColor = fClr
		ctrl.Font = dotNetObject "System.Drawing.Font" "Verdana" 8 ((dotNetClass "System.Drawing.FontStyle").regular)
	)
	fn msg filePath: state: =
	(
		if state then	("INFO BOX: > > >\nThe file has been successfully saved!\n"+filePath)
		else ("INFO BOX: > > >\nFile can not be saved.\nPlease check the User Permission.")
	)
	fn delFile filePath: =
	(
		local file = dotNetClass "System.IO.File"
		if file.Exists(filePath) do file.Delete(filePath)
	)
	fn exportMtlLibFromHigherVersions ctrl matLibPath: lowerVersion: = 
	(
		if (matLib = loadTempMaterialLibrary matLibPath) != undefined do
		(
			mtlDataCA = attributes mtlData
			(
				parameters libs
				(
					libname type:#string
					maxv type:#integer
					mtls type:#materialTab tabSize:0 tabSizeVariable:on
					maps type:#texturemapTab tabSize:0 tabSizeVariable:on
				)
			)
			if (custattributes.add rootnode mtlDataCA) do 
			(
				rootnode.mtlData.libname = (getFilenameFile matLibPath)
				rootnode.mtlData.maxv = lowerVersion
				for m in matLib do 
				(	
					case superclassof m of
					(
						(material): append rootnode.mtlData.mtls m
						(textureMap): append rootnode.mtlData.maps m
					)
				)
			)
			local tempMaxFile = (getFilenamePath matLibPath) + "\\" + (getFilenameFile matLibPath)
			local result = saveMaxFile tempMaxFile saveAsVersion:lowerVersion
			ctrl.text = msg filePath:tempMaxFile state:result
		)
	)
	fn saveMtlLibToLowerVersion ctrl =
	(
		if not (isProperty rootnode #mtlData) then (messageBox "First Open Previously Saved *max file!" title:"Warning" beep:off) else
		(
			local newMatLibFilename = (maxFilePath + rootnode.mtlData.libname + (rootnode.mtlData.maxv as string) + ".mat"), result = false
			delFile filePath:newMatLibFilename
			if (saveMaterialLibrary newMatLibFilename) do
			(
				matLib = loadTempMaterialLibrary newMatLibFilename
				if matLib.count != 0 do (for i = matLib.count to 1 by -1 do deleteItem matLib i)
				if rootnode.mtlData.mtls.count > 0 do (for mtl in rootnode.mtlData.mtls do append matLib mtl)
				if rootnode.mtlData.maps.count > 0 do for map in rootnode.mtlData.maps do append matLib map
				result = saveTempMaterialLibrary matLib newMatLibFilename
			)
			ctrl.text = msg filePath:newMatLibFilename state:result
		)
	)
	fn getVersions = 
	(
		e = (maxVersion())[1]/1000
		array = case e of
		(
			13: #("2010")
			14: #("2011","2010")
			default: for i = (2000+(e-3)) to (2000+(e-5)) by -1 collect i as string
		)
	)
	dotnetControl bnr1 "Label" pos:[5,5] width:290 height:16
	edittext et_oldlib "" text:"Load *.mat file >>>" pos:[1,25] width:270 height:17 readonly:on
	button btn_oldlib "•••" pos:[271,25] width:25 height:18
	button btn_saveMax "Save File As Version" pos:[5,45] width:190 height:20 enabled:off
	dropdownlist dd_list "" items:(getVersions()) pos:[196,45] width:100
	dotnetControl info1 "Label" pos:[5,67] width:290 height:55
	dotnetControl bnr2 "Label" pos:[5,125] width:290 height:16
	button btn_saveLib "Save MatLib File For This Version" pos:[5,145] width:290 height:20
	dotnetControl info2 "Label" pos:[5,167] width:290 height:55
	on libRoll open do
	(
		defLabel bnr1 bClr:BC fClr:FC txtAlign:#center txt:"[ Collect MatLib Data From Highter Version ]"
		defLabel info1 bClr:FC fClr:BC txtAlign:#left txt:"INFO BOX: > > >\nLoad MatLib file, choose max version and press \n*Save File As Version* button.\nNew *.max file will be created in the same folder."
		defLabel bnr2 bClr:BC fClr:FC txtAlign:#center txt:"[ Save MatLib Data To Lower Version ]"
		defLabel info2 bClr:FC fClr:BC txtAlign:#left txt:"INFO BOX: > > >\nDon't forget to open a previously saved *.max file\nbefore you press *Save MatLib File...* button.\nNew *.mat file will be created in the same folder."
	)
	on btn_oldlib pressed do
	(
		libFile = getOpenFileName types:"Material Libraries(*.mat)|*.mat|All|*.*|"
		if libFile != undefined and doesFileExist libFile then (et_oldlib.text = libFile ; btn_saveMax.enabled = on)
		else (libFile = null ; et_oldlib.text = "Load *.mat file >>>" ; btn_saveMax.enabled = off)
	)
	on btn_saveMax pressed do (exportMtlLibFromHigherVersions info1 matLibPath:libFile lowerVersion:(dd_list.selected as number))
	on btn_saveLib pressed do (saveMtlLibToLowerVersion info2)
)
if (e = (maxVersion())[1]/1000)<13 then messageBox "This tool works in 3dsmax2011 and above!" title:"Warning" beep:off else createDialog libRoll 300 225 style:#(#style_titlebar, #style_sysmenu, #style_toolwindow) ; ok
)