Commit feb4a796 authored by Lars Kruse's avatar Lars Kruse

code deduplication

Use a separate function (get_non_conflicting_name) for determining the
name of new objects (models, processes, bounds, tasks and toolpaths).
parent 488f60c9
...@@ -23,6 +23,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -23,6 +23,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
import pycam.Plugins import pycam.Plugins
# TODO: move Toolpath.Bounds here? # TODO: move Toolpath.Bounds here?
import pycam.Toolpath import pycam.Toolpath
from pycam.Utils import get_non_conflicting_name
_RELATIVE_UNIT = ("%", "mm") _RELATIVE_UNIT = ("%", "mm")
...@@ -362,11 +363,9 @@ class Bounds(pycam.Plugins.ListPluginBase): ...@@ -362,11 +363,9 @@ class Bounds(pycam.Plugins.ListPluginBase):
self.core.emit_event("bounds-changed") self.core.emit_event("bounds-changed")
def _bounds_new(self, *args): def _bounds_new(self, *args):
bounds_names = [bounds["name"] for bounds in self] name = get_non_conflicting_name("Bounds #%d",
bounds_id = 1 [bounds["name"] for bounds in self])
while ("Bounds #%d" % bounds_id) in bounds_names: new_bounds = BoundsDict(self.core, name)
bounds_id += 1
new_bounds = BoundsDict(self.core, "Bounds #%d" % bounds_id)
self.append(new_bounds) self.append(new_bounds)
self.select(new_bounds) self.select(new_bounds)
......
...@@ -24,6 +24,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -24,6 +24,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
#import gtk #import gtk
import pycam.Plugins import pycam.Plugins
from pycam.Utils import get_non_conflicting_name
_GTK_COLOR_MAX = 65535.0 _GTK_COLOR_MAX = 65535.0
...@@ -173,11 +174,8 @@ class Models(pycam.Plugins.ListPluginBase): ...@@ -173,11 +174,8 @@ class Models(pycam.Plugins.ListPluginBase):
color=None): color=None):
model_dict = ModelEntity(model) model_dict = ModelEntity(model)
if not name: if not name:
model_id = 1 name = get_non_conflicting_name(name_template,
model_names = [model["name"] for model in self] [model["name"] for model in self])
while (name_template % model_id) in model_names:
model_id += 1
name = name_template % model_id
model_dict["name"] = name model_dict["name"] = name
if not color: if not color:
color = self.core.get("color_model") color = self.core.get("color_model")
......
...@@ -21,6 +21,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -21,6 +21,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
""" """
import pycam.Plugins import pycam.Plugins
from pycam.Utils import get_non_conflicting_name
class Processes(pycam.Plugins.ListPluginBase): class Processes(pycam.Plugins.ListPluginBase):
...@@ -243,14 +244,11 @@ class Processes(pycam.Plugins.ListPluginBase): ...@@ -243,14 +244,11 @@ class Processes(pycam.Plugins.ListPluginBase):
strategies = self.core.get("get_parameter_sets")("process").values() strategies = self.core.get("get_parameter_sets")("process").values()
strategies.sort(key=lambda item: item["weight"]) strategies.sort(key=lambda item: item["weight"])
strategy = strategies[0] strategy = strategies[0]
strategy_names = [process["name"] for process in self] name = get_non_conflicting_name("Process #%d",
process_id = 1 [process["name"] for process in self])
name_template = "Process #%d"
while (name_template % process_id) in strategy_names:
process_id += 1
new_process = ProcessEntity({"strategy": strategy["name"], new_process = ProcessEntity({"strategy": strategy["name"],
"parameters": strategy["parameters"].copy(), "parameters": strategy["parameters"].copy(),
"name": name_template % process_id}) "name": name})
self.append(new_process) self.append(new_process)
self.select(new_process) self.select(new_process)
......
...@@ -25,6 +25,7 @@ import time ...@@ -25,6 +25,7 @@ import time
import pycam.Plugins import pycam.Plugins
import pycam.Utils import pycam.Utils
from pycam.Exporters.GCodeExporter import GCodeGenerator from pycam.Exporters.GCodeExporter import GCodeGenerator
from pycam.Utils import get_non_conflicting_name
class Tasks(pycam.Plugins.ListPluginBase): class Tasks(pycam.Plugins.ListPluginBase):
...@@ -248,14 +249,11 @@ class Tasks(pycam.Plugins.ListPluginBase): ...@@ -248,14 +249,11 @@ class Tasks(pycam.Plugins.ListPluginBase):
types = self.core.get("get_parameter_sets")("task").values() types = self.core.get("get_parameter_sets")("task").values()
types.sort(key=lambda item: item["weight"]) types.sort(key=lambda item: item["weight"])
one_type = types[0] one_type = types[0]
task_id = 1 name = get_non_conflicting_name("Task #%d",
name_template = "Task #%d" [task["name"] for task in self])
task_names = [task["name"] for task in self]
while (name_template % task_id) in task_names:
task_id += 1
new_task = TaskEntity({"type": one_type["name"], new_task = TaskEntity({"type": one_type["name"],
"parameters": one_type["parameters"].copy(), "parameters": one_type["parameters"].copy(),
"name": name_template % task_id}) "name": name})
self.append(new_task) self.append(new_task)
self.select(new_task) self.select(new_task)
......
...@@ -23,6 +23,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -23,6 +23,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
import pycam.Plugins import pycam.Plugins
import pycam.Toolpath import pycam.Toolpath
from pycam.Utils import get_non_conflicting_name
class Toolpaths(pycam.Plugins.ListPluginBase): class Toolpaths(pycam.Plugins.ListPluginBase):
...@@ -156,11 +157,9 @@ class Toolpaths(pycam.Plugins.ListPluginBase): ...@@ -156,11 +157,9 @@ class Toolpaths(pycam.Plugins.ListPluginBase):
parameters = new_tp.get_params() parameters = new_tp.get_params()
else: else:
moves, parameters = new_tp moves, parameters = new_tp
toolpath_id = len(self) + 1 name = get_non_conflicting_name("Toolpath #%d",
name_template = "Toolpath #%d" [tp["name"] for tp in self])
while (name_template % toolpath_id) in [tp["name"] for tp in self]: attributes= {"visible": True, "name": name}
toolpath_id += 1
attributes= {"visible": True, "name": name_template % toolpath_id}
new_tp = ToolpathEntity(toolpath_path=moves, new_tp = ToolpathEntity(toolpath_path=moves,
toolpath_parameters=parameters, attributes=attributes) toolpath_parameters=parameters, attributes=attributes)
self.append(new_tp) self.append(new_tp)
......
...@@ -59,6 +59,7 @@ def get_platform(): ...@@ -59,6 +59,7 @@ def get_platform():
else: else:
return PLATFORM_UNKNOWN return PLATFORM_UNKNOWN
def get_case_insensitive_file_pattern(pattern): def get_case_insensitive_file_pattern(pattern):
""" Convert something like "*.svg" into "*.[sS][vV][gG]" - as it is """ Convert something like "*.svg" into "*.[sS][vV][gG]" - as it is
required for GTK's FileFilter. required for GTK's FileFilter.
...@@ -73,6 +74,36 @@ def get_case_insensitive_file_pattern(pattern): ...@@ -73,6 +74,36 @@ def get_case_insensitive_file_pattern(pattern):
return "".join(result) return "".join(result)
def get_non_conflicting_name(template, conflicts, start=None,
get_next_func=None):
""" Find a string containing a number that is not in conflict with any of
the given strings. A name template (containing "%d") is required.
You may use non-numbers (strings, floats, ...) as well. In this case
you need to override "start" and "get_next_func".
@value template: a string template containing "%d" (e.g. "Object %d")
@type template: basestr
@value conflicts: a list of strings that need may not be used
@type conflicts: list(basestr)
@value start: optional initial value (default: len(conflicts) + 1)
@type start: undefined
@value get_next_func: function used for determining the next value to
be tested. This function defaults to "lambda value: value + 1".
@returns: a usable name that was not found in "conflicts"
@rtype: basestr
"""
if start is None:
index = len(conflicts) + 1
else:
index = start
if get_next_func is None:
get_next_func = lambda current: current + 1
while (template % index) in conflicts:
index = get_next_func(index)
return template % index
class URIHandler(object): class URIHandler(object):
DEFAULT_PREFIX = "file://" DEFAULT_PREFIX = "file://"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment