test_gerber_flow.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import sys
  2. import unittest
  3. from PyQt4 import QtGui
  4. from FlatCAMApp import App, tclCommands
  5. from FlatCAMObj import FlatCAMGerber, FlatCAMGeometry, FlatCAMCNCjob
  6. from ObjectUI import GerberObjectUI, GeometryObjectUI
  7. from time import sleep
  8. import os
  9. import tempfile
  10. class GerberFlowTestCase(unittest.TestCase):
  11. filename = 'simple1.gbr'
  12. def setUp(self):
  13. self.app = QtGui.QApplication(sys.argv)
  14. # Create App, keep app defaults (do not load
  15. # user-defined defaults).
  16. self.fc = App(user_defaults=False)
  17. self.fc.open_gerber('tests/gerber_files/' + self.filename)
  18. def tearDown(self):
  19. del self.fc
  20. del self.app
  21. def test_flow(self):
  22. # Names of available objects.
  23. names = self.fc.collection.get_names()
  24. print names
  25. #--------------------------------------
  26. # Total of 1 objects.
  27. #--------------------------------------
  28. self.assertEquals(len(names), 1,
  29. "Expected 1 object, found %d" % len(names))
  30. #--------------------------------------
  31. # Object's name matches the file name.
  32. #--------------------------------------
  33. self.assertEquals(names[0], self.filename,
  34. "Expected name == %s, got %s" % (self.filename, names[0]))
  35. #---------------------------------------
  36. # Get object by that name, make sure it's a FlatCAMGerber.
  37. #---------------------------------------
  38. gerber_name = names[0]
  39. gerber_obj = self.fc.collection.get_by_name(gerber_name)
  40. self.assertTrue(isinstance(gerber_obj, FlatCAMGerber),
  41. "Expected FlatCAMGerber, instead, %s is %s" %
  42. (gerber_name, type(gerber_obj)))
  43. #----------------------------------------
  44. # Object's GUI matches Object's options
  45. #----------------------------------------
  46. # TODO: Open GUI with double-click on object.
  47. # Opens the Object's GUI, populates it.
  48. gerber_obj.build_ui()
  49. for option, value in gerber_obj.options.iteritems():
  50. try:
  51. form_field = gerber_obj.form_fields[option]
  52. except KeyError:
  53. print ("**********************************************************\n"
  54. "* WARNING: Option '{}' has no form field\n"
  55. "**********************************************************"
  56. "".format(option))
  57. continue
  58. self.assertEqual(value, form_field.get_value(),
  59. "Option '{}' == {} but form has {}".format(
  60. option, value, form_field.get_value()
  61. ))
  62. #--------------------------------------------------
  63. # Changes in the GUI should be read in when
  64. # running any process. Changing something here.
  65. #--------------------------------------------------
  66. form_field = gerber_obj.form_fields['isotooldia']
  67. value = form_field.get_value()
  68. form_field.set_value(value * 1.1) # Increase by 10%
  69. print "'isotooldia' == {}".format(value)
  70. #--------------------------------------------------
  71. # Create isolation routing using default values
  72. # and by clicking on the button.
  73. #--------------------------------------------------
  74. # Get the object's GUI and click on "Generate Geometry" under
  75. # "Isolation Routing"
  76. assert isinstance(gerber_obj, FlatCAMGerber) # Just for the IDE
  77. # Changed: UI has been build already
  78. #gerber_obj.build_ui() # Open the object's UI.
  79. ui = gerber_obj.ui
  80. assert isinstance(ui, GerberObjectUI)
  81. ui.generate_iso_button.click() # Click
  82. #---------------------------------------------
  83. # Check that GUI has been read in.
  84. #---------------------------------------------
  85. value = gerber_obj.options['isotooldia']
  86. form_value = form_field.get_value()
  87. self.assertEqual(value, form_value,
  88. "Form value for '{}' == {} was not read into options"
  89. "which has {}".format('isotooldia', form_value, value))
  90. print "'isotooldia' == {}".format(value)
  91. #---------------------------------------------
  92. # Check that only 1 object has been created.
  93. #---------------------------------------------
  94. names = self.fc.collection.get_names()
  95. self.assertEqual(len(names), 2,
  96. "Expected 2 objects, found %d" % len(names))
  97. #-------------------------------------------------------
  98. # Make sure the Geometry Object has the correct name
  99. #-------------------------------------------------------
  100. geo_name = gerber_name + "_iso"
  101. self.assertTrue(geo_name in names,
  102. "Object named %s not found." % geo_name)
  103. #-------------------------------------------------------
  104. # Get the object make sure it's a geometry object
  105. #-------------------------------------------------------
  106. geo_obj = self.fc.collection.get_by_name(geo_name)
  107. self.assertTrue(isinstance(geo_obj, FlatCAMGeometry),
  108. "Expected a FlatCAMGeometry, got %s" % type(geo_obj))
  109. #------------------------------------
  110. # Open the UI, make CNCObject
  111. #------------------------------------
  112. geo_obj.build_ui()
  113. ui = geo_obj.ui
  114. assert isinstance(ui, GeometryObjectUI) # Just for the IDE
  115. ui.generate_cnc_button.click() # Click
  116. # Work is done in a separate thread and results are
  117. # passed via events to the main event loop which is
  118. # not running. Run only for pending events.
  119. #
  120. # I'm not sure why, but running it only once does
  121. # not catch the new object. Might be a timing issue.
  122. # http://pyqt.sourceforge.net/Docs/PyQt4/qeventloop.html#details
  123. for _ in range(2):
  124. sleep(0.1)
  125. self.app.processEvents()
  126. #---------------------------------------------
  127. # Check that only 1 object has been created.
  128. #---------------------------------------------
  129. names = self.fc.collection.get_names()
  130. self.assertEqual(len(names), 3,
  131. "Expected 3 objects, found %d" % len(names))
  132. #-------------------------------------------------------
  133. # Make sure the CNC Job Object has the correct name
  134. #-------------------------------------------------------
  135. cnc_name = geo_name + "_cnc"
  136. self.assertTrue(cnc_name in names,
  137. "Object named %s not found." % geo_name)
  138. #-------------------------------------------------------
  139. # Get the object make sure it's a CNC Job object
  140. #-------------------------------------------------------
  141. cnc_obj = self.fc.collection.get_by_name(cnc_name)
  142. self.assertTrue(isinstance(cnc_obj, FlatCAMCNCjob),
  143. "Expected a FlatCAMCNCJob, got %s" % type(geo_obj))
  144. #-----------------------------------------
  145. # Export G-Code, check output
  146. #-----------------------------------------
  147. assert isinstance(cnc_obj, FlatCAMCNCjob)
  148. output_filename = ""
  149. # get system temporary file(try create it and delete also)
  150. with tempfile.NamedTemporaryFile(prefix='unittest.', suffix="." + cnc_name + '.gcode', delete=True) as tmp_file:
  151. output_filename = tmp_file.name
  152. cnc_obj.export_gcode(output_filename)
  153. self.assertTrue(os.path.isfile(output_filename))
  154. os.remove(output_filename)
  155. print names