Package theia :: Module main
[hide private]
[frames] | no frames]

Source Code for Module theia.main

  1  '''Main module of theia, defines the main function.''' 
  2   
  3  # Provides: 
  4  #   main 
  5   
  6  import os 
  7  import sys 
  8  import subprocess 
  9  from .helpers import settings 
 10  from .helpers.interaction import welcome, errorRecursion, errorUnknown 
 11  from .helpers.interaction import errorAtSpecifiedLocation, errorWhereIs 
 12  from .helpers.interaction import errorWhereIsNotFound 
 13  from .helpers.tools import InputError 
 14  from .running import simulation 
 15   
16 -def main(options, args):
17 '''Main function of theia.''' 18 19 # global variables dic 20 dic = { 'info': options.info, 21 'warning': options.warning, 22 'text': options.text, 23 'cad': options.cad, 24 'fname': os.path.splitext(args[1])[0], 25 'fclib': options.fclib, 26 'antiClip': options.antiClip, 27 'short': options.short} 28 29 30 # initiate globals 31 settings.init(dic) 32 33 #exit with error if file not found 34 if not os.path.isfile(settings.fname + '.tia'): 35 print "theia: Error: %s.tia: No such file.\nAborting." %settings.fname 36 sys.exit(1) 37 38 #create simulation object with name the basename (not path) 39 FName = settings.fname.split('/')[-1] 40 simu = simulation.Simulation(FName) 41 42 #welcome to theia 43 print welcome 44 45 #load initial data 46 print "theia: Run: Reading input data." 47 try: 48 simu.load() 49 except InputError as IE: 50 print "theia: Error: %s\nAborting." %IE.Message 51 sys.exit(1) 52 else: 53 print "theia: Run: Done." 54 55 #run simulation 56 print "theia: Run: Running simulation." 57 try: 58 simu.run() 59 except RuntimeError: 60 print "theia: Error: Maximum recursion depth reached.%s\nAborting." \ 61 %errorRecursion 62 sys.exit(1) 63 else: 64 print "theia: Run: Done." 65 66 #write results to .out file 67 if settings.text: 68 print "theia: Run: Writing output file." 69 simu.writeOut() 70 print "theia: Run: Done." 71 72 #write CAD file 73 if settings.cad: 74 if settings.fclib is not None: 75 print "theia: Run: Loading FreeCAD library from specified location." 76 FREECADPATH = settings.fclib 77 sys.path.append(FREECADPATH) 78 try: 79 import FreeCAD as App 80 import Part 81 except ImportError: 82 print errorAtSpecifiedLocation %settings.fclib 83 sys.exit(1) 84 else: 85 print "theia: Run: Done." 86 else: 87 # Search for FreeCADlibs with whereis 88 print "theia: Run: Searching for FreeCAD library." 89 cmd = "whereis freecad" 90 try: 91 output = subprocess.check_output(cmd, shell = True).split() 92 FREECADPATH = output[2] + '/lib' 93 except OSError: 94 print errorWhereIsNotFound 95 sys.exit(1) 96 except IndexError: 97 print errorWhereIs 98 sys.exit(1) 99 else: 100 sys.path.append(FREECADPATH) 101 102 # Import the libs 103 print "theia: Run: Loading FreeCAD library from %s." \ 104 %FREECADPATH 105 try: 106 import FreeCAD as App 107 import Part 108 except ImportError: 109 print errorUnknown %FREECADPATH 110 sys.exit(1) 111 else: 112 print "theia: Run: Done." 113 114 print "theia: Run: Writing CAD file." 115 simu.writeCAD() 116 print "theia: Run: Done." 117 118 sys.exit(0)
119