# Geo_Smooth
# Written by Connor Lough
# Parts of MlxWriter and MeshlabServerCaller were written by Will Zaylor


import os
import sys

# import Tkinter library, use dialog box when selecting files
from Tkinter import Tk
from tkFileDialog import askopenfilename, asksaveasfilename

from DetermineValues import DetermineValues  # import the DetermineValues function from DetermineValues.py
from MlxWriter import MlxWriter  # import MlxWriter function from MlxWriter.py
from MeshlabServerCaller import SurfaceSmooth1, SurfaceSmooth2  # import these two functions from MeshlabServerCaller.py


if len(sys.argv) == 1:
	# file dialogs
	Tk().withdraw()  # we don't want a full GUI, so keep the root window from appearing
	# A slightly fancier dialog than you were using
	#  and all at once for clarity
	stlFilename	= askopenfilename(title='Select STL file', filetypes = (("STL files", ".stl"),	("All files", ".*") ))
	mlx1Filename	= askopenfilename(title='Now Select MLX file', filetypes = (("MLX files", "*.mlx"), ("All files", "*.*") ))
	mlx2Filename	= askopenfilename(title='Select Second MLX file', filetypes = (("MLX files", "*.mlx"), ("All files", "*.*") ))
elif len(sys.argv) == 4:
	# I/You could put some testing here to make sure these are the files we are looking for, but I won't.
	stlFilename	= sys.argv[1]
	mlx1Filename	= sys.argv[2]
	mlx2Filename	= sys.argv[3]
else:
	# Explain how script should be used
	print 'Usage:\npython <script> <mesh.stl> <file.mlx> <file2.mlx>\n'
	print 'OR\npython <script>\n	User will be prompted for file choice'
	print 'Output:\n Smoothed mesh.stl'
	exit()


# Using the DetermineValues function imported earlier, the user can extract the maximum voxel size/ maximum distance
#   and the number of vertices, which is the number of samples to be used.
side_len, samples = DetermineValues(stlFilename)


# I could skip the raw_input and if section
#   but there may be advantages? to having the user type here
output = raw_input("Enter Output Filename or Press ENTER to Browse: ")
if output == '':
	# Show dialog box and select filename
	Tk().withdraw()  # we don't want a full GUI, so keep the root window from appearing
	output = asksaveasfilename(title='Select Output File Name, Browse or Type')
	# You can type the new file name in the dialog box and it will work just fine.

# The values extracted using DetermineValues will be incorporated into the .mlx file to make it file specific.
MlxWriter(mlx1Filename, output, side_len) # side_len comes from determine values

# using the Mlx file, the script will call meshlabserver to run the smoothing process, with no specified output.
SurfaceSmooth1(stlFilename, output, callDirectory=None)

# Does this really save two files?  My outdescription may be wrong...hmmm
outputFileName = raw_input("Enter Output .stl Filename or Press ENTER to Browse: ")
if outputFileName == '':
	# Show dialog box and select filename
	Tk().withdraw()  # we don't want a full GUI, so keep the root window from appearing
	outputFileName = asksaveasfilename(title='Select Output File Name, Browse or Type')

# The script will run the second .mlx file on the plymcout.ply file that was automatically saved in step one
#   and save the resulting file as whatever is specified.
SurfaceSmooth2('plymcout.ply', outputFileName, mlx2Filename, callDirectory=None)

# The plymcout.ply files are no longer needed, and will be deleted.
delete_filename1 = 'plymcout.ply'
delete_filename2 = 'plymcout.d.ply'

# This is more pythonic that if else for something that should usually work.
try:	
	os.remove(delete_filename1)
	# Since the user doesn't need to know if it works I wou;dn't keep this print statement.
	#print '{} successfully deleted.'.format(delete_filename1)
except OSError:
	print '{} does not exist, was not deleted'.format(delete_filename1)

# And then I can shorten this.  I think this is more clean looking, but some may disagree. 
try:		os.remove(delete_filename2)
except OSError:	print '{} does not exist, was not deleted'.format(delete_filename2)


print ''
print 'Smoothing process completed.'

