TclCommandImportSvg.py 2.2 KB

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