fix ordering in naming arguments and help

add commands TclCommandAddPolygon(add_poly, add_polygon) and TclCommandAddPolyline(add_polyline)

implement add_polyline in camlib.py
This commit is contained in:
Kamil Sopko
2016-03-17 12:14:12 +01:00
parent 2e51c1e9cd
commit 78854f7fe0
7 changed files with 209 additions and 40 deletions

View File

@@ -136,6 +136,27 @@ class Geometry(object):
log.error("Failed to run union on polygons.") log.error("Failed to run union on polygons.")
raise raise
def add_polyline(self, points):
"""
Adds a polyline to the object (by union)
:param points: The vertices of the polyline.
:return: None
"""
if self.solid_geometry is None:
self.solid_geometry = []
if type(self.solid_geometry) is list:
self.solid_geometry.append(LineString(points))
return
try:
self.solid_geometry = self.solid_geometry.union(LineString(points))
except:
#print "Failed to run union on polygons."
log.error("Failed to run union on polylines.")
raise
def subtract_polygon(self, points): def subtract_polygon(self, points):
""" """
Subtract polygon from the given object. This only operates on the paths in the original geometry, i.e. it converts polygons into paths. Subtract polygon from the given object. This only operates on the paths in the original geometry, i.e. it converts polygons into paths.

View File

@@ -1,31 +1,33 @@
import sys, inspect, pkgutil import sys, inspect, pkgutil
import re import re
import FlatCAMApp import FlatCAMApp
import collections
class TclCommand(object): class TclCommand(object):
app=None app=None
# array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon) # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
aliases = None aliases = []
# dictionary of types from Tcl command: args = {'name': str}, this is for value without optionname # dictionary of types from Tcl command, needs to be ordered
arg_names = {'name': str} arg_names = collections.OrderedDict([
('name', str)
])
# dictionary of types from Tcl command: types = {'outname': str} , this is for options like -optionname value # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
option_types = {} option_types = collections.OrderedDict([])
# array of mandatory options for current Tcl command: required = {'name','outname'} # array of mandatory options for current Tcl command: required = {'name','outname'}
required = ['name'] required = ['name']
# structured help for current command # structured help for current command, args needs to be ordered
help = { help = {
'main': "undefined help.", 'main': "undefined help.",
'args': { 'args': collections.OrderedDict([
'argumentname': 'undefined help.', ('argumentname', 'undefined help.'),
'optionname': 'undefined help.' ('optionname', 'undefined help.')
}, ]),
'examples' : [] 'examples' : []
} }
@@ -41,7 +43,7 @@ class TclCommand(object):
def get_decorated_command(alias): def get_decorated_command(alias):
command_string = [] command_string = []
for key, value in reversed(self.help['args'].items()): for key, value in self.help['args'].items():
command_string.append(get_decorated_argument(key, value, True)) command_string.append(get_decorated_argument(key, value, True))
return "> " + alias + " " + " ".join(command_string) return "> " + alias + " " + " ".join(command_string)
@@ -49,11 +51,18 @@ class TclCommand(object):
option_symbol = '' option_symbol = ''
if key in self.arg_names: if key in self.arg_names:
type=self.arg_names[key] type=self.arg_names[key]
in_command_name = "<" + str(type.__name__) + ">" type_name=str(type.__name__)
else: in_command_name = "<" + type_name + ">"
elif key in self.option_types:
option_symbol = '-' option_symbol = '-'
type=self.option_types[key] type=self.option_types[key]
in_command_name = option_symbol + key + " <" + str(type.__name__) + ">" type_name=str(type.__name__)
in_command_name = option_symbol + key + " <" + type_name + ">"
else:
option_symbol = ''
type_name='?'
in_command_name = option_symbol + key + " <" + type_name + ">"
if in_command: if in_command:
if key in self.required: if key in self.required:
return in_command_name return in_command_name
@@ -61,19 +70,18 @@ class TclCommand(object):
return '[' + in_command_name + "]" return '[' + in_command_name + "]"
else: else:
if key in self.required: if key in self.required:
return "\t" + option_symbol + key + " <" + str(type.__name__) + ">: " + value return "\t" + option_symbol + key + " <" + type_name + ">: " + value
else: else:
return "\t[" + option_symbol + key + " <" + str(type.__name__) + ">: " + value+"]" return "\t[" + option_symbol + key + " <" + type_name + ">: " + value+"]"
def get_decorated_example(example): def get_decorated_example(example):
example_string = '' return "> "+example
return "todo" + example_string
help_string=[self.help['main']] help_string=[self.help['main']]
for alias in self.aliases: for alias in self.aliases:
help_string.append(get_decorated_command(alias)) help_string.append(get_decorated_command(alias))
for key, value in reversed(self.help['args'].items()): for key, value in self.help['args'].items():
help_string.append(get_decorated_argument(key, value)) help_string.append(get_decorated_argument(key, value))
for example in self.help['examples']: for example in self.help['examples']:
@@ -123,10 +131,10 @@ class TclCommand(object):
# check arguments # check arguments
idx=0 idx=0
arg_names_reversed=self.arg_names.items() arg_names_items=self.arg_names.items()
for argument in arguments: for argument in arguments:
if len(self.arg_names) > idx: if len(self.arg_names) > idx:
key, type = arg_names_reversed[len(self.arg_names)-idx-1] key, type = arg_names_items[idx]
try: try:
named_args[key] = type(argument) named_args[key] = type(argument)
except Exception, e: except Exception, e:

View File

@@ -0,0 +1,65 @@
from ObjectCollection import *
import TclCommand
class TclCommandAddPolygon(TclCommand.TclCommand):
"""
Tcl shell command to create a polygon in the given Geometry object
"""
# array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
aliases = ['add_polygon','add_poly']
# dictionary of types from Tcl command, needs to be ordered
arg_names = collections.OrderedDict([
('name', str)
])
# dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
option_types = collections.OrderedDict([])
# array of mandatory options for current Tcl command: required = {'name','outname'}
required = ['name']
# structured help for current command, args needs to be ordered
help = {
'main': "Creates a polygon in the given Geometry object.",
'args': collections.OrderedDict([
('name', 'Name of the Geometry object to which to append the polygon.'),
('xi, yi', 'Coordinates of points in the polygon.')
]),
'examples':[
'add_polygon <name> <x0> <y0> <x1> <y1> <x2> <y2> [x3 y3 [...]]'
]
}
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
"""
name = args['name']
try:
obj = self.app.collection.get_by_name(name)
except:
self.app.raiseTclError("Could not retrieve object: %s" % name)
if obj is None:
self.app.raiseTclError("Object not found: %s" % name)
if not isinstance(obj, Geometry):
self.app.raiseTclError('Expected Geometry, got %s %s.' % (name, type(obj)))
if len(unnamed_args) % 2 != 0:
self.app.raiseTclError("Incomplete coordinates.")
points = [[float(unnamed_args[2*i]), float(unnamed_args[2*i+1])] for i in range(len(unnamed_args)/2)]
obj.add_polygon(points)
obj.plot()

View File

@@ -0,0 +1,65 @@
from ObjectCollection import *
import TclCommand
class TclCommandAddPolyline(TclCommand.TclCommand):
"""
Tcl shell command to create a polyline in the given Geometry object
"""
# array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
aliases = ['add_polyline']
# dictionary of types from Tcl command, needs to be ordered
arg_names = collections.OrderedDict([
('name', str)
])
# dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
option_types = collections.OrderedDict([])
# array of mandatory options for current Tcl command: required = {'name','outname'}
required = ['name']
# structured help for current command, args needs to be ordered
help = {
'main': "Creates a polyline in the given Geometry object.",
'args': collections.OrderedDict([
('name', 'Name of the Geometry object to which to append the polyline.'),
('xi, yi', 'Coordinates of points in the polyline.')
]),
'examples':[
'add_polyline <name> <x0> <y0> <x1> <y1> <x2> <y2> [x3 y3 [...]]'
]
}
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
"""
name = args['name']
try:
obj = self.app.collection.get_by_name(name)
except:
self.app.raiseTclError("Could not retrieve object: %s" % name)
if obj is None:
self.app.raiseTclError("Object not found: %s" % name)
if not isinstance(obj, Geometry):
self.app.raiseTclError('Expected Geometry, got %s %s.' % (name, type(obj)))
if len(unnamed_args) % 2 != 0:
self.app.raiseTclError("Incomplete coordinates.")
points = [[float(unnamed_args[2*i]), float(unnamed_args[2*i+1])] for i in range(len(unnamed_args)/2)]
obj.add_polyline(points)
obj.plot()

View File

@@ -10,22 +10,26 @@ class TclCommandExteriors(TclCommand.TclCommand):
# array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon) # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
aliases = ['exteriors','ext'] aliases = ['exteriors','ext']
# dictionary of types from Tcl command: args = {'name': str}, this is for value without optionname # dictionary of types from Tcl command, needs to be ordered
arg_names = {'name': str,'name2': str} arg_names = collections.OrderedDict([
('name', str)
])
# dictionary of types from Tcl command: types = {'outname': str} , this is for options like -optionname value # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
option_types = {'outname': str} option_types = collections.OrderedDict([
('outname', str)
])
# array of mandatory options for current Tcl command: required = {'name','outname'} # array of mandatory options for current Tcl command: required = {'name','outname'}
required = ['name'] required = ['name']
# structured help for current command # structured help for current command, args needs to be ordered
help = { help = {
'main': "Get exteriors of polygons.", 'main': "Get exteriors of polygons.",
'args': { 'args': collections.OrderedDict([
'name': 'Name of the source Geometry object.', ('name', 'Name of the source Geometry object.'),
'outname': 'Name of the resulting Geometry object.' ('outname', 'Name of the resulting Geometry object.')
}, ]),
'examples':[] 'examples':[]
} }

View File

@@ -9,22 +9,26 @@ class TclCommandInteriors(TclCommand.TclCommand):
# array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon) # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
aliases = ['interiors'] aliases = ['interiors']
# dictionary of types from Tcl command: args = {'name': str}, this is for value without optionname # dictionary of types from Tcl command, needs to be ordered
arg_names = {'name': str} arg_names = collections.OrderedDict([
('name', str)
])
# dictionary of types from Tcl command: types = {'outname': str} , this is for options like -optionname value # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
option_types = {'outname': str} option_types = collections.OrderedDict([
('outname', str)
])
# array of mandatory options for current Tcl command: required = {'name','outname'} # array of mandatory options for current Tcl command: required = {'name','outname'}
required = ['name'] required = ['name']
# structured help for current command # structured help for current command, args needs to be ordered
help = { help = {
'main': "Get interiors of polygons.", 'main': "Get interiors of polygons.",
'args': { 'args': collections.OrderedDict([
'name': 'Name of the source Geometry object.', ('name', 'Name of the source Geometry object.'),
'outname': 'Name of the resulting Geometry object.' ('outname', 'Name of the resulting Geometry object.')
}, ]),
'examples':[] 'examples':[]
} }

View File

@@ -5,6 +5,8 @@ import sys
# allowed command modules # allowed command modules
import tclCommands.TclCommandExteriors import tclCommands.TclCommandExteriors
import tclCommands.TclCommandInteriors import tclCommands.TclCommandInteriors
import tclCommands.TclCommandAddPolygon
import tclCommands.TclCommandAddPolyline
__all__=[] __all__=[]