try destroyDialog ::instantMeshesBridge catch()
rollout instantMeshesBridge "Instant Meshes Bridge" width:185 height:220
(
	local instantMeshesPath = @"C:\Program Files\Instant Meshes\Instant Meshes.exe" -- replace the path inside the quotes
	local outputPath = getDir #temp
	local inputFile = outputPath + "\input.obj"
	local outputFile = outputPath + "\output.obj"
	local objExportConfig = objExp.getIniName()
	local objImportConfig = objImp.getIniName()
	local iniFile = getDir #plugcfg + "\\instantMeshesBridge.ini"

	group "Target Mesh Parameters"
	(
		radioButtons rbMethod pos:[12,21] labels:#("Edge Length", "Vertex Count", "Face Count")
		spinner spnEdgeWidth pos:[110,20] fieldWidth:50 range:[1e-9, 1e9, 10] type:#worldUnits
		spinner spnFaceCount pos:[110,37] fieldWidth:50 range:[1, 1e9, 1000] type:#integer enabled:false
		spinner spnVertexCount pos:[110,53] fieldWidth:50 range:[1, 1e9, 1000] type:#integer enabled:false
	)
	spinner spnReprojectionSteps "Reprojection Steps: " fieldWidth:50 range:[1, 100, 2] type:#integer align:#left
	checkBox chxCreaseAngle "Crease Angle: " offset:[0,-2] across:2
	spinner spnCreaseAngle fieldWidth:50 range:[0, 180, 15] type:#float offset:[1,-2] enabled:false
	checkBox chxDeterministic "Deterministic (slower)"
	checkBox chxIntrinsic "Intrinsic Mode"
	checkBox chxUseBoundaries "Use Borders" checked:true
	button btnCancel "Cancel" height:24 width:75 offset:[0, 5] across:2
	button btnRemesh "Remesh" height:24 width:75 offset:[0, 5]

	local paramSpinners = #(spnEdgeWidth, spnFaceCount, spnVertexCount)
	local single = dotNetClass "System.Single"
	local bool = dotNetClass "System.Boolean"

	
	-- get remesh exepath
	
	fn verifyInstantMesh str:"" =
	(
		local netInfo =dotNetClass "System.Diagnostics.FileVersionInfo"
		local verInfo=netInfo.GetVersionInfo str
		local prodName=verInfo.ProductName
		prodName
	)
	
	fn getRemeshPath paths:"" =
	(
		local boolPath=false  
		if not (doesFileExist paths) then 
		(
			local doOpen = getOpenFileName caption:"Find Instant Mesh"\
			filename:"Instant Meshes.exe" \
			types:"Application(*.exe)|*.exe|"

			if (doOpen!=undefined and (verifyInstantMesh str:doOpen =="Instant Meshes")) do 
			(
				setINISetting iniFile "General" "AppPath" doOpen
				boolPath=true
			)
		)
		boolPath
	) 
	
	
	fn stringEscape str =
		"\"" + str + "\" "

	fn tryParseNum str =
		 str

	fn loadIniFile = if doesFileExist iniFile do
		for ctrl in instantMeshesBridge.controls do case (classOf ctrl) of
		(
			RadioControl : if single.TryParse (getIniSetting iniFile "Controls" ctrl.name) &res1 then (ctrl.state = res1; ctrl.changed res1)
			SpinnerControl : if single.TryParse (getIniSetting iniFile "Controls" ctrl.name) &res2 then ctrl.value = res2
			CheckBoxControl : if bool.TryParse (getIniSetting iniFile "Controls" ctrl.name) &res3 then ctrl.checked = res3
		)

	fn saveIniFile =
		for ctrl in instantMeshesBridge.controls do case (classOf ctrl) of
		(
			RadioControl : setIniSetting iniFile "Controls" ctrl.name (ctrl.state as string)
			SpinnerControl : setIniSetting iniFile "Controls" ctrl.name (ctrl.value as string)
			CheckBoxControl : setIniSetting iniFile "Controls" ctrl.name (ctrl.checked as string)
		)

	fn setupOBJExport =
	(
		setINISetting objExportConfig "General" "Preset" "<NONE>"
		setINISetting objExportConfig "Geometry" "FlipZyAxis" "1"
		setINISetting objExportConfig "Geometry" "FaceType" "0"
		setINISetting objExportConfig "Geometry" "TextureCoords" "0"
		setINISetting objExportConfig "Geometry" "Normals" "0"
		setINISetting objExportConfig "Geometry" "SmoothingGroups" "0"
		setINISetting objExportConfig "Geometry" "ObjScale" "1.0"
		setINISetting objExportConfig "Material" "UseMaterial" "0"
		setINISetting objExportConfig "Optimize" "optVertex" "1"
		setINISetting objExportConfig "Optimize" "optNormals" "1"
		setINISetting objExportConfig "Optimize" "optTextureCoords" "1"
	)

	fn setupOBJImport =
	(
		setINISetting objImportConfig "General" "Preset" "<NONE>"
		setINISetting objImportConfig "General" "ResetScene" "0"
		setINISetting objImportConfig "Geometry" "FlipZyAxis" "1"
		setINISetting objImportConfig "Geometry" "CenterPivots" "1"
		setINISetting objImportConfig "Geometry" "TextureCoords" "0"
		setINISetting objImportConfig "Geometry" "SmoothingGroups" "0"
		setINISetting objImportConfig "Geometry" "NormalsType" "3"
		setINISetting objImportConfig "Units/Scale" "Convert" "0"
		setINISetting objImportConfig "Objects" "Retriangulate" "0"
		setINISetting objImportConfig "Objects" "AsEditablePoly" "1"
	)

	fn resolveConflicts =
	(
		local hWND = dialogMonitorOPS.getWindowHandle()
		
		if UIAccessor.getWindowText hWND == "Import Name Conflict" then
		(
			UIAccessor.PressButtonByName hWND "OK"
			true
		)
		else false
	)

	fn startMonitoring =
	(
		dialogMonitorOps.unRegisterNotification id:#resolveConflicts
		dialogMonitorOps.enabled = true
		dialogMonitorOps.interactive = false
		dialogMonitorOps.registerNotification instantMeshesBridge.resolveConflicts id:#resolveConflicts
	)

	fn stopMonitoring =
	(
		dialogMonitorOps.unRegisterNotification id:#resolveConflicts
		dialogMonitorOps.enabled = false
	)

	on instantMeshesBridge open do
		loadIniFile()

	on rbMethod changed index do
		for i = 1 to paramSpinners.count do paramSpinners[i].enabled = i == index

	on chxCreaseAngle changed checked do
		spnCreaseAngle.enabled = checked

	on btnCancel pressed do
		destroyDialog instantMeshesBridge

	on btnRemesh pressed do
	(
		if selection.count != 1 do
			return messageBox "Select one object"
		if not isProperty selection[1] #mesh do
			return messageBox "Object has to have a mesh"
		--
		local meshAppPath = getINISetting iniFile "General" "AppPath"
		if (doesFileExist meshAppPath) then
		(
			local objName = selection[1].name
			local command = stringEscape instantMeshesPath
			local commandOptions =
				"-S " + spnReprojectionSteps.value as string + \
				(if chxDeterministic.checked then " -d " else "") + \
				(if chxUseBoundaries.checked then " -b " else "") + \
				(if chxIntrinsic.checked then " -i " else "") + \
				(if chxCreaseAngle.checked then " -c " + spnCreaseAngle.value as string else "") + \
				(case rbMethod.state of
				(
					1: " -s " + spnEdgeWidth.value as string
					2: " -f " + spnFaceCount.value as string
					3: " -v " + spnVertexCount.value as string
				);) +
				" -o " + stringEscape outputFile + stringEscape inputFile

			setupOBJExport()
			setupOBJImport()

			exportFile inputFile #noPrompt selectedOnly:true
			hiddenDOSCommand (command + commandOptions)

			startMonitoring()
			importFile outputFile #noPrompt
			stopMonitoring()

			selection[1].name = uniqueName (objName + "_remesh")
		)
		else
		(
			local doAgain = getRemeshPath paths:meshAppPath
			if (doAgain) then 
			(
				btnRemesh.pressed()
			)
			else
			(
				local msg = "Please specify \"Instant Meshes.exe\" location!\n"
				msg += "The operation failed!"
				messageBox msg title:"Error"
				try (DestroyDialog instantMeshesBridge) catch()
			)
		)
	)

	on instantMeshesBridge close do
	(
		stopMonitoring()
		saveIniFile()
	)
)
createDialog instantMeshesBridge