flatcam/tclCommands/TclCommandOpenDXF.py
Marius Stanciu eed7e3d620 - some minor changes in the Python version detection
- added a new Tcl Command named SetPath which will set a path to be used by the Tcl commands. Once set will serve as a fallback path in case that the files fail to be opened first time. It will be persistent, saved in preferences.
- added the GUI for the new Open Example in the FIle -> Scripting menu.
- I am modifying all the open ... handlers to add a parameter that will flag if the method was launched from Tcl Shell. This way if the method will fail to open the filename (which include the path) it will try to open from a set fallback path.
- fixed issue #406, bug introduced recently (leftover changes).
- modified the ImportSVG Tcl command name to OpenSVG (open_svg alias)
- added a new Tcl command named OpenDXF (open_dxf alias)
- fixed some errors in Scripting features
- added a new Tcl command named GetPath as a convenient way to get the current default path stored in App.defaults['global_tcl_path']
2020-04-28 14:37:34 +03:00

95 lines
3.5 KiB
Python

from tclCommands.TclCommand import TclCommandSignaled
import collections
class TclCommandOpenDXF(TclCommandSignaled):
"""
Tcl shell command to open an DXF file as a Geometry/Gerber Object.
"""
# array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
aliases = ['open_dxf']
description = '%s %s' % ("--", "Open a DXF file as a Geometry (or Gerber) Object.")
# dictionary of types from Tcl command, needs to be ordered
arg_names = collections.OrderedDict([
('filename', str)
])
# dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
option_types = collections.OrderedDict([
('type', str),
('outname', str)
])
# array of mandatory options for current Tcl command: required = {'name','outname'}
required = ['filename']
# structured help for current command, args needs to be ordered
help = {
'main': "Open a DXF file as a Geometry (or Gerber) Object.",
'args': collections.OrderedDict([
('filename', 'Absolute path to file to open. Required.\n'
'WARNING: no spaces are allowed. If unsure enclose the entire path with quotes.'),
('type', 'Open as a Gerber or Geometry (default) object. Values can be: "geometry" or "gerber"'),
('outname', 'Name of the resulting Geometry object.')
]),
'examples': ['open_dxf D:\\my_beautiful_svg_file.SVG']
}
def execute(self, args, unnamed_args):
"""
execute current TCL shell command
:param args: array of known named arguments and options
:param unnamed_args: array of other values which were passed into command
without -somename and we do not have them in known arg_names
:return: None or exception
"""
# How the object should be initialized
def obj_init(geo_obj, app_obj):
# if geo_obj.kind != 'geometry' and geo_obj.kind != 'gerber':
# self.raise_tcl_error('Expected Geometry or Gerber, got %s %s.' % (outname, type(geo_obj)))
geo_obj.import_dxf(filename, obj_type, units=units)
filename = args['filename']
if 'outname' in args:
outname = args['outname']
else:
outname = filename.split('/')[-1].split('\\')[-1]
if 'type' in args:
obj_type = str(args['type']).lower()
else:
obj_type = 'geometry'
if obj_type != "geometry" and obj_type != "gerber":
self.raise_tcl_error("Option type can be 'geometry' or 'gerber' only, got '%s'." % obj_type)
units = self.app.defaults['units'].upper()
with self.app.proc_container.new("Open DXF"):
# Object creation
ret_val = self.app.new_object(obj_type, outname, obj_init, plot=False)
if ret_val == 'fail':
filename = self.app.defaults['global_tcl_path'] + '/' + outname
ret_val = self.app.new_object(obj_type, outname, obj_init, plot=False)
self.app.shell.append_output(
"No path provided or path is wrong. Using the default Path... \n")
if ret_val == 'fail':
return "Failed. The OpenDXF command was used but could not open the DXF file"
# Register recent file
self.app.file_opened.emit("dxf", filename)
# GUI feedback
self.app.inform.emit("Opened: " + filename)