TclCommandImportSvg.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from tclCommands.TclCommand import TclCommandSignaled
  2. import collections
  3. from camlib import Geometry
  4. class TclCommandImportSvg(TclCommandSignaled):
  5. """
  6. Tcl shell command to import an SVG file as a Geometry Object.
  7. """
  8. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  9. aliases = ['import_svg']
  10. # dictionary of types from Tcl command, needs to be ordered
  11. arg_names = collections.OrderedDict([
  12. ('filename', str)
  13. ])
  14. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  15. option_types = collections.OrderedDict([
  16. ('type', str),
  17. ('outname', str)
  18. ])
  19. # array of mandatory options for current Tcl command: required = {'name','outname'}
  20. required = ['filename']
  21. # structured help for current command, args needs to be ordered
  22. help = {
  23. 'main': "Import an SVG file as a Geometry Object..",
  24. 'args': collections.OrderedDict([
  25. ('filename', 'Path to file to open.'),
  26. ('type', 'Import as gerber or geometry(default).'),
  27. ('outname', 'Name of the resulting Geometry object.')
  28. ]),
  29. 'examples': []
  30. }
  31. def execute(self, args, unnamed_args):
  32. """
  33. execute current TCL shell command
  34. :param args: array of known named arguments and options
  35. :param unnamed_args: array of other values which were passed into command
  36. without -somename and we do not have them in known arg_names
  37. :return: None or exception
  38. """
  39. # How the object should be initialized
  40. def obj_init(geo_obj, app_obj):
  41. if not isinstance(geo_obj, Geometry):
  42. self.raise_tcl_error('Expected Geometry or Gerber, got %s %s.' % (outname, type(geo_obj)))
  43. geo_obj.import_svg(filename)
  44. filename = args['filename']
  45. if 'outname' in args:
  46. outname = args['outname']
  47. else:
  48. outname = filename.split('/')[-1].split('\\')[-1]
  49. if 'type' in args:
  50. obj_type = args['type']
  51. else:
  52. obj_type = 'geometry'
  53. if obj_type != "geometry" and obj_type != "gerber":
  54. self.raise_tcl_error("Option type can be 'geopmetry' or 'gerber' only, got '%s'." % obj_type)
  55. with self.app.proc_container.new("Import SVG"):
  56. # Object creation
  57. self.app.new_object(obj_type, outname, obj_init, plot=False)
  58. # Register recent file
  59. self.app.file_opened.emit("svg", filename)
  60. # GUI feedback
  61. self.app.inform.emit("Opened: " + filename)