# Geo_Smooth
# Written by Connor Lough
# Tips and Help from Ben Landis
# Parts of MlxWriter and MeshlabServerCaller were written by Will Zaylor


#########LOADING LIBRARIES#########
import os

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

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


#########IMPORTING STL FILE#########
# Allow user to type out filename or call for a dialog box to select the .stl file.
print ''
print 'Select .stl file.'
print ''

answer1 = raw_input('Enter filename path or enter "dialog" for a dialog box to select the file: ')
if answer1 == 'dialog':
    # To show dialog box and select filename
    Tk().withdraw()  # we don't want a full GUI, so keep the root window from appearing
    stlfilename = askopenfilename()  # show an "Open" dialog box and return the path to the selected file
else:
    stlfilename = answer1

print ''


#########DETERMINE VALUES FROM FILE#########
# 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)


#########SELECTING MLX FILE 1#########
# The first .mlx file will be selected, by either typing the filename or calling the dialog box.
print ''
print 'Select .mlx file 1.'
print ''

answer2 = raw_input('Enter filename path or enter "dialog" for a dialog box to select the file: ')
if answer2 == 'dialog':
    # To show dialog box and select filename
    Tk().withdraw()  # we don't want a full GUI, so keep the root window from appearing
    mlx1filename = askopenfilename()  # show an "Open" dialog box and return the path to the selected file
else:
    mlx1filename = answer2

print ''


#########MLX WRITER#########
# The values extracted using DetermineValues will be incorporated into the .mlx file to make it file specific.
output1 = raw_input("Output Filename: ")
print ''
MlxWriter(mlx1filename, output1, side_len) # side_len comes from determine values


#########MESHLAB SERVER CALLER 1#########
# using the Mlx file, the script will call meshlabserver to run the smoothing process, with no specified output.
sourceFileName = stlfilename
mlxFile = output1
SurfaceSmooth1(sourceFileName, mlxFile, callDirectory=None)


#########SELECTING MLX FILE 2#########
# The second .mlx file needs to be selected.
print ''
print 'Select .mlx file 2.'
print ''

answer3 = raw_input('Enter filename path or enter "dialog" for a dialog box to select the file: ')
if answer3 == 'dialog':
    # To show dialog box and select filename
    Tk().withdraw()  # we don't want a full GUI, so keep the root window from appearing
    mlx2filename = askopenfilename()  # show an "Open" dialog box and return the path to the selected file
else:
    mlx2filename = answer3

print ''


#########MESHLAB SERVER CALLER 2#########
# The script will run the second .mlx file on the plymcout.ply file that was automaticaally saved in step one
# and save the resulting file as whatever is specified.
sourceFileName = 'plymcout.ply'
outputFileName = raw_input("Output .stl Filename: ")
mlxFile = mlx2filename
SurfaceSmooth2(sourceFileName, outputFileName, mlxFile, callDirectory=None)


#########DELETE UNNECESSARY FILES#########
# The plymcout.ply files are no longer needed, and will be deleted.
delete_filename1 = '/home/loughc/openknee/oks/oks001/dat/Geometry/plymcout.ply'
delete_filename2 = '/home/loughc/openknee/oks/oks001/dat/Geometry/plymcout.d.ply'

print ''

if os.path.exists(delete_filename1):
    os.remove(delete_filename1)
    print 'plymcout.ply successfully deleted.'
else:
    print 'plymcout.ply does not exist, was not deleted'

if os.path.exists(delete_filename2):
    os.remove(delete_filename2)
    print 'plymcout.d.ply successfully deleted.'
else:
    print 'plymcout.d.ply does not exist, was not deleted'


#########END OF SCRIPT#########
print ''
print 'Smoothing process completed.'
print ''