TclCommandImportSvg.py 2.6 KB

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