test_svg_flow.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import sys
  2. import unittest
  3. from PyQt4 import QtGui
  4. from FlatCAMApp import App
  5. from FlatCAMObj import FlatCAMGeometry, FlatCAMCNCjob
  6. from ObjectUI import GerberObjectUI, GeometryObjectUI
  7. from time import sleep
  8. import os
  9. import tempfile
  10. class SVGFlowTestCase(unittest.TestCase):
  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.filename = 'drawing.svg'
  17. def tearDown(self):
  18. del self.fc
  19. del self.app
  20. def test_flow(self):
  21. self.fc.import_svg('tests/svg/' + self.filename)
  22. names = self.fc.collection.get_names()
  23. print(names)
  24. #--------------------------------------
  25. # Total of 1 objects.
  26. #--------------------------------------
  27. self.assertEqual(len(names), 1,
  28. "Expected 1 object, found %d" % len(names))
  29. #--------------------------------------
  30. # Object's name matches the file name.
  31. #--------------------------------------
  32. self.assertEqual(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. geo_name = names[0]
  38. geo_obj = self.fc.collection.get_by_name(geo_name)
  39. self.assertTrue(isinstance(geo_obj, FlatCAMGeometry),
  40. "Expected FlatCAMGeometry, instead, %s is %s" %
  41. (geo_name, type(geo_obj)))
  42. #----------------------------------------
  43. # Object's GUI matches Object's options
  44. #----------------------------------------
  45. # TODO: Open GUI with double-click on object.
  46. # Opens the Object's GUI, populates it.
  47. geo_obj.build_ui()
  48. for option, value in list(geo_obj.options.items()):
  49. try:
  50. form_field = geo_obj.form_fields[option]
  51. except KeyError:
  52. print(("**********************************************************\n"
  53. "* WARNING: Option '{}' has no form field\n"
  54. "**********************************************************"
  55. "".format(option)))
  56. continue
  57. self.assertEqual(value, form_field.get_value(),
  58. "Option '{}' == {} but form has {}".format(
  59. option, value, form_field.get_value()
  60. ))
  61. #------------------------------------
  62. # Open the UI, make CNCObject
  63. #------------------------------------
  64. geo_obj.build_ui()
  65. ui = geo_obj.ui
  66. assert isinstance(ui, GeometryObjectUI) # Just for the IDE
  67. ui.generate_cnc_button.click() # Click
  68. # Work is done in a separate thread and results are
  69. # passed via events to the main event loop which is
  70. # not running. Run only for pending events.
  71. #
  72. # I'm not sure why, but running it only once does
  73. # not catch the new object. Might be a timing issue.
  74. # http://pyqt.sourceforge.net/Docs/PyQt4/qeventloop.html#details
  75. for _ in range(2):
  76. sleep(0.1)
  77. self.app.processEvents()
  78. #---------------------------------------------
  79. # Check that only 1 object has been created.
  80. #---------------------------------------------
  81. names = self.fc.collection.get_names()
  82. self.assertEqual(len(names), 2,
  83. "Expected 2 objects, found %d" % len(names))
  84. #-------------------------------------------------------
  85. # Make sure the CNC Job Object has the correct name
  86. #-------------------------------------------------------
  87. cnc_name = geo_name + "_cnc"
  88. self.assertTrue(cnc_name in names,
  89. "Object named %s not found." % geo_name)
  90. #-------------------------------------------------------
  91. # Get the object make sure it's a CNC Job object
  92. #-------------------------------------------------------
  93. cnc_obj = self.fc.collection.get_by_name(cnc_name)
  94. self.assertTrue(isinstance(cnc_obj, FlatCAMCNCjob),
  95. "Expected a FlatCAMCNCJob, got %s" % type(geo_obj))
  96. #-----------------------------------------
  97. # Export G-Code, check output
  98. #-----------------------------------------
  99. assert isinstance(cnc_obj, FlatCAMCNCjob)
  100. output_filename = ""
  101. # get system temporary file(try create it and delete also)
  102. with tempfile.NamedTemporaryFile(prefix='unittest.',
  103. suffix="." + cnc_name + '.gcode',
  104. delete=True) as tmp_file:
  105. output_filename = tmp_file.name
  106. cnc_obj.export_gcode(output_filename)
  107. self.assertTrue(os.path.isfile(output_filename))
  108. os.remove(output_filename)
  109. print(names)