TclCommandImportSvg.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. description = '%s %s' % ("--", "Import a SVG file as a Geometry (or Gerber) Object.")
  11. # dictionary of types from Tcl command, needs to be ordered
  12. arg_names = collections.OrderedDict([
  13. ('filename', str)
  14. ])
  15. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  16. option_types = collections.OrderedDict([
  17. ('type', str),
  18. ('outname', str)
  19. ])
  20. # array of mandatory options for current Tcl command: required = {'name','outname'}
  21. required = ['filename']
  22. # structured help for current command, args needs to be ordered
  23. help = {
  24. 'main': "Import a SVG file as a Geometry (or Gerber) Object.",
  25. 'args': collections.OrderedDict([
  26. ('filename', 'Absolute path to file to open. Required.\n'
  27. 'WARNING: no spaces are allowed. If unsure enclose the entire path with quotes.'),
  28. ('type', 'Import as a Gerber or Geometry (default) object. Values can be: "geometry" or "gerber"'),
  29. ('outname', 'Name of the resulting Geometry object.')
  30. ]),
  31. 'examples': ['import_svg D:\\my_beautiful_svg_file.SVG']
  32. }
  33. def execute(self, args, unnamed_args):
  34. """
  35. execute current TCL shell command
  36. :param args: array of known named arguments and options
  37. :param unnamed_args: array of other values which were passed into command
  38. without -somename and we do not have them in known arg_names
  39. :return: None or exception
  40. """
  41. # How the object should be initialized
  42. def obj_init(geo_obj, app_obj):
  43. if not isinstance(geo_obj, Geometry):
  44. self.raise_tcl_error('Expected Geometry or Gerber, got %s %s.' % (outname, type(geo_obj)))
  45. geo_obj.import_svg(filename)
  46. filename = args['filename']
  47. if 'outname' in args:
  48. outname = args['outname']
  49. else:
  50. outname = filename.split('/')[-1].split('\\')[-1]
  51. if 'type' in args:
  52. obj_type = args['type'].lower()
  53. else:
  54. obj_type = 'geometry'
  55. if obj_type != "geometry" and obj_type != "gerber":
  56. self.raise_tcl_error("Option type can be 'geometry' or 'gerber' only, got '%s'." % obj_type)
  57. with self.app.proc_container.new("Import SVG"):
  58. # Object creation
  59. self.app.new_object(obj_type, outname, obj_init, plot=False)
  60. # Register recent file
  61. self.app.file_opened.emit("svg", filename)
  62. # GUI feedback
  63. self.app.inform.emit("Opened: " + filename)