2015-10-31 23:01:01 +00:00
|
|
|
import sys
|
|
|
|
import unittest
|
2019-10-06 12:56:41 +00:00
|
|
|
from PyQt5 import QtGui, QtWidgets
|
2016-06-03 21:15:35 +00:00
|
|
|
from FlatCAMApp import App, tclCommands
|
2015-10-31 23:01:01 +00:00
|
|
|
from FlatCAMObj import FlatCAMGerber, FlatCAMGeometry, FlatCAMCNCjob
|
2019-10-06 12:56:41 +00:00
|
|
|
from flatcamGUI.ObjectUI import GerberObjectUI, GeometryObjectUI
|
2015-10-31 23:01:01 +00:00
|
|
|
from time import sleep
|
|
|
|
import os
|
2016-03-10 18:01:07 +00:00
|
|
|
import tempfile
|
2015-10-31 23:01:01 +00:00
|
|
|
|
2016-06-03 21:15:35 +00:00
|
|
|
|
2015-10-31 23:01:01 +00:00
|
|
|
class GerberFlowTestCase(unittest.TestCase):
|
2016-07-02 20:47:15 +00:00
|
|
|
"""
|
|
|
|
This is a top-level test covering the Gerber-to-GCode
|
|
|
|
generation workflow.
|
|
|
|
|
|
|
|
THIS IS A REQUIRED TEST FOR ANY UPDATES.
|
|
|
|
|
|
|
|
"""
|
2015-10-31 23:01:01 +00:00
|
|
|
|
|
|
|
filename = 'simple1.gbr'
|
|
|
|
|
|
|
|
def setUp(self):
|
2019-10-06 12:56:41 +00:00
|
|
|
self.app = QtWidgets.QApplication(sys.argv)
|
2015-10-31 23:01:01 +00:00
|
|
|
|
|
|
|
# Create App, keep app defaults (do not load
|
|
|
|
# user-defined defaults).
|
|
|
|
self.fc = App(user_defaults=False)
|
|
|
|
|
|
|
|
self.fc.open_gerber('tests/gerber_files/' + self.filename)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
del self.fc
|
|
|
|
del self.app
|
|
|
|
|
|
|
|
def test_flow(self):
|
|
|
|
# Names of available objects.
|
|
|
|
names = self.fc.collection.get_names()
|
2018-05-26 01:43:40 +00:00
|
|
|
print(names)
|
2015-10-31 23:01:01 +00:00
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# --------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
# Total of 1 objects.
|
2019-10-06 12:56:41 +00:00
|
|
|
# --------------------------------------
|
2018-05-26 01:43:40 +00:00
|
|
|
self.assertEqual(len(names), 1,
|
2015-10-31 23:01:01 +00:00
|
|
|
"Expected 1 object, found %d" % len(names))
|
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# --------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
# Object's name matches the file name.
|
2019-10-06 12:56:41 +00:00
|
|
|
# --------------------------------------
|
2018-05-26 01:43:40 +00:00
|
|
|
self.assertEqual(names[0], self.filename,
|
2015-10-31 23:01:01 +00:00
|
|
|
"Expected name == %s, got %s" % (self.filename, names[0]))
|
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# ---------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
# Get object by that name, make sure it's a FlatCAMGerber.
|
2019-10-06 12:56:41 +00:00
|
|
|
# ---------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
gerber_name = names[0]
|
|
|
|
gerber_obj = self.fc.collection.get_by_name(gerber_name)
|
|
|
|
self.assertTrue(isinstance(gerber_obj, FlatCAMGerber),
|
|
|
|
"Expected FlatCAMGerber, instead, %s is %s" %
|
|
|
|
(gerber_name, type(gerber_obj)))
|
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# ----------------------------------------
|
2016-06-03 21:15:35 +00:00
|
|
|
# Object's GUI matches Object's options
|
2019-10-06 12:56:41 +00:00
|
|
|
# ----------------------------------------
|
2016-06-03 21:15:35 +00:00
|
|
|
# TODO: Open GUI with double-click on object.
|
|
|
|
# Opens the Object's GUI, populates it.
|
|
|
|
gerber_obj.build_ui()
|
2018-05-26 01:43:40 +00:00
|
|
|
for option, value in list(gerber_obj.options.items()):
|
2016-06-03 21:15:35 +00:00
|
|
|
try:
|
|
|
|
form_field = gerber_obj.form_fields[option]
|
|
|
|
except KeyError:
|
2018-05-26 01:43:40 +00:00
|
|
|
print(("**********************************************************\n"
|
2016-06-03 21:15:35 +00:00
|
|
|
"* WARNING: Option '{}' has no form field\n"
|
|
|
|
"**********************************************************"
|
2018-05-26 01:43:40 +00:00
|
|
|
"".format(option)))
|
2016-06-03 21:15:35 +00:00
|
|
|
continue
|
|
|
|
self.assertEqual(value, form_field.get_value(),
|
|
|
|
"Option '{}' == {} but form has {}".format(
|
|
|
|
option, value, form_field.get_value()
|
|
|
|
))
|
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# --------------------------------------------------
|
2016-06-03 21:15:35 +00:00
|
|
|
# Changes in the GUI should be read in when
|
|
|
|
# running any process. Changing something here.
|
2019-10-06 12:56:41 +00:00
|
|
|
# --------------------------------------------------
|
2016-06-03 21:15:35 +00:00
|
|
|
|
|
|
|
form_field = gerber_obj.form_fields['isotooldia']
|
|
|
|
value = form_field.get_value()
|
|
|
|
form_field.set_value(value * 1.1) # Increase by 10%
|
2018-05-26 01:43:40 +00:00
|
|
|
print(("'isotooldia' == {}".format(value)))
|
2016-06-03 21:15:35 +00:00
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# --------------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
# Create isolation routing using default values
|
|
|
|
# and by clicking on the button.
|
2019-10-06 12:56:41 +00:00
|
|
|
# --------------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
# Get the object's GUI and click on "Generate Geometry" under
|
|
|
|
# "Isolation Routing"
|
|
|
|
assert isinstance(gerber_obj, FlatCAMGerber) # Just for the IDE
|
2016-06-03 21:15:35 +00:00
|
|
|
# Changed: UI has been build already
|
|
|
|
#gerber_obj.build_ui() # Open the object's UI.
|
2015-10-31 23:01:01 +00:00
|
|
|
ui = gerber_obj.ui
|
|
|
|
assert isinstance(ui, GerberObjectUI)
|
|
|
|
ui.generate_iso_button.click() # Click
|
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# ---------------------------------------------
|
2016-06-03 21:15:35 +00:00
|
|
|
# Check that GUI has been read in.
|
2019-10-06 12:56:41 +00:00
|
|
|
# ---------------------------------------------
|
2016-06-03 21:15:35 +00:00
|
|
|
value = gerber_obj.options['isotooldia']
|
|
|
|
form_value = form_field.get_value()
|
|
|
|
self.assertEqual(value, form_value,
|
|
|
|
"Form value for '{}' == {} was not read into options"
|
|
|
|
"which has {}".format('isotooldia', form_value, value))
|
2018-05-26 01:43:40 +00:00
|
|
|
print(("'isotooldia' == {}".format(value)))
|
2016-06-03 21:15:35 +00:00
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# ---------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
# Check that only 1 object has been created.
|
2019-10-06 12:56:41 +00:00
|
|
|
# ---------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
names = self.fc.collection.get_names()
|
|
|
|
self.assertEqual(len(names), 2,
|
|
|
|
"Expected 2 objects, found %d" % len(names))
|
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# -------------------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
# Make sure the Geometry Object has the correct name
|
2019-10-06 12:56:41 +00:00
|
|
|
# -------------------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
geo_name = gerber_name + "_iso"
|
|
|
|
self.assertTrue(geo_name in names,
|
|
|
|
"Object named %s not found." % geo_name)
|
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# -------------------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
# Get the object make sure it's a geometry object
|
2019-10-06 12:56:41 +00:00
|
|
|
# -------------------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
geo_obj = self.fc.collection.get_by_name(geo_name)
|
|
|
|
self.assertTrue(isinstance(geo_obj, FlatCAMGeometry),
|
|
|
|
"Expected a FlatCAMGeometry, got %s" % type(geo_obj))
|
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# ------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
# Open the UI, make CNCObject
|
2019-10-06 12:56:41 +00:00
|
|
|
# ------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
geo_obj.build_ui()
|
|
|
|
ui = geo_obj.ui
|
|
|
|
assert isinstance(ui, GeometryObjectUI) # Just for the IDE
|
|
|
|
ui.generate_cnc_button.click() # Click
|
|
|
|
|
|
|
|
# Work is done in a separate thread and results are
|
|
|
|
# passed via events to the main event loop which is
|
|
|
|
# not running. Run only for pending events.
|
|
|
|
#
|
|
|
|
# I'm not sure why, but running it only once does
|
|
|
|
# not catch the new object. Might be a timing issue.
|
|
|
|
# http://pyqt.sourceforge.net/Docs/PyQt4/qeventloop.html#details
|
|
|
|
for _ in range(2):
|
|
|
|
sleep(0.1)
|
|
|
|
self.app.processEvents()
|
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# ---------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
# Check that only 1 object has been created.
|
2019-10-06 12:56:41 +00:00
|
|
|
# ---------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
names = self.fc.collection.get_names()
|
|
|
|
self.assertEqual(len(names), 3,
|
|
|
|
"Expected 3 objects, found %d" % len(names))
|
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# -------------------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
# Make sure the CNC Job Object has the correct name
|
2019-10-06 12:56:41 +00:00
|
|
|
# -------------------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
cnc_name = geo_name + "_cnc"
|
|
|
|
self.assertTrue(cnc_name in names,
|
|
|
|
"Object named %s not found." % geo_name)
|
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# -------------------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
# Get the object make sure it's a CNC Job object
|
2019-10-06 12:56:41 +00:00
|
|
|
# -------------------------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
cnc_obj = self.fc.collection.get_by_name(cnc_name)
|
|
|
|
self.assertTrue(isinstance(cnc_obj, FlatCAMCNCjob),
|
|
|
|
"Expected a FlatCAMCNCJob, got %s" % type(geo_obj))
|
|
|
|
|
2019-10-06 12:56:41 +00:00
|
|
|
# -----------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
# Export G-Code, check output
|
2019-10-06 12:56:41 +00:00
|
|
|
# -----------------------------------------
|
2015-10-31 23:01:01 +00:00
|
|
|
assert isinstance(cnc_obj, FlatCAMCNCjob)
|
2016-03-10 18:01:07 +00:00
|
|
|
output_filename = ""
|
2016-03-10 20:45:47 +00:00
|
|
|
# get system temporary file(try create it and delete also)
|
2016-07-02 20:47:15 +00:00
|
|
|
with tempfile.NamedTemporaryFile(prefix='unittest.',
|
|
|
|
suffix="." + cnc_name + '.gcode',
|
|
|
|
delete=True) as tmp_file:
|
2016-03-10 20:45:47 +00:00
|
|
|
output_filename = tmp_file.name
|
2015-10-31 23:01:01 +00:00
|
|
|
cnc_obj.export_gcode(output_filename)
|
|
|
|
self.assertTrue(os.path.isfile(output_filename))
|
|
|
|
os.remove(output_filename)
|
|
|
|
|
2018-05-26 01:43:40 +00:00
|
|
|
print(names)
|