test_gerber_flow.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import sys
  2. import unittest
  3. from PyQt4 import QtGui
  4. from FlatCAMApp import App
  5. from FlatCAMObj import FlatCAMGerber, FlatCAMGeometry, FlatCAMCNCjob
  6. from ObjectUI import GerberObjectUI, GeometryObjectUI
  7. from time import sleep
  8. import os
  9. class GerberFlowTestCase(unittest.TestCase):
  10. filename = 'simple1.gbr'
  11. def setUp(self):
  12. self.app = QtGui.QApplication(sys.argv)
  13. # Create App, keep app defaults (do not load
  14. # user-defined defaults).
  15. self.fc = App(user_defaults=False)
  16. self.fc.open_gerber('tests/gerber_files/' + self.filename)
  17. def tearDown(self):
  18. del self.fc
  19. del self.app
  20. def test_flow(self):
  21. # Names of available objects.
  22. names = self.fc.collection.get_names()
  23. print names
  24. #--------------------------------------
  25. # Total of 1 objects.
  26. #--------------------------------------
  27. self.assertEquals(len(names), 1,
  28. "Expected 1 object, found %d" % len(names))
  29. #--------------------------------------
  30. # Object's name matches the file name.
  31. #--------------------------------------
  32. self.assertEquals(names[0], self.filename,
  33. "Expected name == %s, got %s" % (self.filename, names[0]))
  34. #---------------------------------------
  35. # Get object by that name, make sure it's a FlatCAMGerber.
  36. #---------------------------------------
  37. gerber_name = names[0]
  38. gerber_obj = self.fc.collection.get_by_name(gerber_name)
  39. self.assertTrue(isinstance(gerber_obj, FlatCAMGerber),
  40. "Expected FlatCAMGerber, instead, %s is %s" %
  41. (gerber_name, type(gerber_obj)))
  42. #--------------------------------------------------
  43. # Create isolation routing using default values
  44. # and by clicking on the button.
  45. #--------------------------------------------------
  46. # Get the object's GUI and click on "Generate Geometry" under
  47. # "Isolation Routing"
  48. assert isinstance(gerber_obj, FlatCAMGerber) # Just for the IDE
  49. gerber_obj.build_ui() # Open the object's UI.
  50. ui = gerber_obj.ui
  51. assert isinstance(ui, GerberObjectUI)
  52. ui.generate_iso_button.click() # Click
  53. #---------------------------------------------
  54. # Check that only 1 object has been created.
  55. #---------------------------------------------
  56. names = self.fc.collection.get_names()
  57. self.assertEqual(len(names), 2,
  58. "Expected 2 objects, found %d" % len(names))
  59. #-------------------------------------------------------
  60. # Make sure the Geometry Object has the correct name
  61. #-------------------------------------------------------
  62. geo_name = gerber_name + "_iso"
  63. self.assertTrue(geo_name in names,
  64. "Object named %s not found." % geo_name)
  65. #-------------------------------------------------------
  66. # Get the object make sure it's a geometry object
  67. #-------------------------------------------------------
  68. geo_obj = self.fc.collection.get_by_name(geo_name)
  69. self.assertTrue(isinstance(geo_obj, FlatCAMGeometry),
  70. "Expected a FlatCAMGeometry, got %s" % type(geo_obj))
  71. #------------------------------------
  72. # Open the UI, make CNCObject
  73. #------------------------------------
  74. geo_obj.build_ui()
  75. ui = geo_obj.ui
  76. assert isinstance(ui, GeometryObjectUI) # Just for the IDE
  77. ui.generate_cnc_button.click() # Click
  78. # Work is done in a separate thread and results are
  79. # passed via events to the main event loop which is
  80. # not running. Run only for pending events.
  81. #
  82. # I'm not sure why, but running it only once does
  83. # not catch the new object. Might be a timing issue.
  84. # http://pyqt.sourceforge.net/Docs/PyQt4/qeventloop.html#details
  85. for _ in range(2):
  86. sleep(0.1)
  87. self.app.processEvents()
  88. #---------------------------------------------
  89. # Check that only 1 object has been created.
  90. #---------------------------------------------
  91. names = self.fc.collection.get_names()
  92. self.assertEqual(len(names), 3,
  93. "Expected 3 objects, found %d" % len(names))
  94. #-------------------------------------------------------
  95. # Make sure the CNC Job Object has the correct name
  96. #-------------------------------------------------------
  97. cnc_name = geo_name + "_cnc"
  98. self.assertTrue(cnc_name in names,
  99. "Object named %s not found." % geo_name)
  100. #-------------------------------------------------------
  101. # Get the object make sure it's a CNC Job object
  102. #-------------------------------------------------------
  103. cnc_obj = self.fc.collection.get_by_name(cnc_name)
  104. self.assertTrue(isinstance(cnc_obj, FlatCAMCNCjob),
  105. "Expected a FlatCAMCNCJob, got %s" % type(geo_obj))
  106. #-----------------------------------------
  107. # Export G-Code, check output
  108. #-----------------------------------------
  109. assert isinstance(cnc_obj, FlatCAMCNCjob)
  110. output_filename = "tests/tmp/" + cnc_name + ".gcode"
  111. cnc_obj.export_gcode(output_filename)
  112. self.assertTrue(os.path.isfile(output_filename))
  113. os.remove(output_filename)
  114. print names