test_svg_flow.py 5.0 KB

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