PathParameters.py 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# -*- coding: utf-8 -*-
"""
$Id$

Copyright 2011 Lars Kruse <devel@sumpfralle.de>

This file is part of PyCAM.

PyCAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PyCAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PyCAM.  If not, see <http://www.gnu.org/licenses/>.
"""


import pycam.Plugins
import pycam.Gui.ControlsGTK
26
import pycam.Toolpath.MotionGrid
27 28 29 30


class PathParamOverlap(pycam.Plugins.PluginBase):

31
    DEPENDS = ["Processes"]
32
    CATEGORIES = ["Process", "Parameter"]
33 34

    def setup(self):
35
        # configure the input/output converter
36 37
        self.control = pycam.Gui.ControlsGTK.InputNumber(lower=0, upper=99,
                digits=0, increment=10, change_handler=lambda widget=None: \
38
                    self.core.emit_event("process-changed"))
39
        self.control.set_conversion(
40 41
                set_conv=lambda float_value: int(float_value * 100.0),
                get_conv=lambda percent: percent / 100.0)
42 43 44 45
        self.core.get("register_parameter")("process", "overlap",
                self.control)
        self.core.register_ui("process_path_parameters", "Overlap [%]",
                self.control.get_widget(), weight=10)
46 47 48
        return True

    def teardown(self):
49 50
        self.core.unregister_ui("process_path_parameters", self.control.get_widget())
        self.core.get("unregister_parameter")("process", "overlap")
51 52 53 54


class PathParamStepDown(pycam.Plugins.PluginBase):

55
    DEPENDS = ["Processes"]
56
    CATEGORIES = ["Process", "Parameter"]
57 58

    def setup(self):
59 60 61
        self.control = pycam.Gui.ControlsGTK.InputNumber(lower=0.01,
                upper=1000, digits=2, start=1,
                change_handler=lambda widget=None: \
62
                    self.core.emit_event("process-changed"))
63 64 65 66
        self.core.get("register_parameter")("process", "step_down",
                self.control)
        self.core.register_ui("process_path_parameters", "Step down",
                self.control.get_widget(), weight=20)
67 68 69
        return True

    def teardown(self):
70 71
        self.core.unregister_ui("process_path_parameters", self.control.get_widget())
        self.core.get("unregister_parameter")("process", "step_down")
72 73 74 75


class PathParamMaterialAllowance(pycam.Plugins.PluginBase):

76
    DEPENDS = ["Processes"]
77
    CATEGORIES = ["Process", "Parameter"]
78 79

    def setup(self):
80 81
        self.control = pycam.Gui.ControlsGTK.InputNumber(start=0, lower=0,
                upper=100, digits=2, change_handler=lambda widget=None: \
82
                    self.core.emit_event("process-changed"))
83 84 85 86
        self.core.get("register_parameter")("process", "material_allowance",
                self.control)
        self.core.register_ui("process_path_parameters", "Material allowance",
                self.control.get_widget(), weight=30)
87 88 89
        return True

    def teardown(self):
90 91
        self.core.unregister_ui("process_path_parameters", self.control.get_widget())
        self.core.get("unregister_parameter")("process", "material_allowance")
92 93 94 95


class PathParamMillingStyle(pycam.Plugins.PluginBase):

96
    DEPENDS = ["Processes", "PathParamPattern"]
97
    CATEGORIES = ["Process", "Parameter"]
98 99

    def setup(self):
100
        self.control = pycam.Gui.ControlsGTK.InputChoice(
101 102 103
                    (("ignore", pycam.Toolpath.MotionGrid.MILLING_STYLE_IGNORE),
                    ("climb / down", pycam.Toolpath.MotionGrid.MILLING_STYLE_CLIMB),
                    ("conventional / up", pycam.Toolpath.MotionGrid.MILLING_STYLE_CONVENTIONAL)),
104
                change_handler=lambda widget=None: self.core.emit_event(
105
                        "process-changed"))
106
        self.core.get("register_parameter")("path_pattern", "milling_style",
107
                self.control)
108 109
        self.core.get("register_parameter")("process", "milling_style",
                self.control)
110 111
        self.core.register_ui("process_path_parameters", "Milling style",
                self.control.get_widget(), weight=50)
112 113 114
        return True

    def teardown(self):
115
        self.core.unregister_ui("process_path_parameters", self.control.get_widget())
116
        self.core.get("unregister_parameter")("path_pattern", "milling_style")
117
        self.core.get("unregister_parameter")("process", "milling_style")
118 119 120 121


class PathParamGridDirection(pycam.Plugins.PluginBase):

122
    DEPENDS = ["Processes", "PathParamPattern"]
123
    CATEGORIES = ["Process", "Parameter"]
124 125

    def setup(self):
126
        self.control = pycam.Gui.ControlsGTK.InputChoice(
127 128 129
                    (("x", pycam.Toolpath.MotionGrid.GRID_DIRECTION_X),
                    ("y", pycam.Toolpath.MotionGrid.GRID_DIRECTION_Y),
                    ("xy", pycam.Toolpath.MotionGrid.GRID_DIRECTION_XY)),
130
                change_handler=lambda widget=None: self.core.emit_event(
131
                        "process-changed"))
132
        self.core.get("register_parameter")("path_pattern", "grid_direction",
133
                self.control)
134 135
        self.core.get("register_parameter")("process", "grid_direction",
                self.control)
136 137
        self.core.register_ui("process_path_parameters", "Direction",
                self.control.get_widget(), weight=40)
138 139 140
        return True

    def teardown(self):
141
        self.core.unregister_ui("process_path_parameters", self.control.get_widget())
142
        self.core.get("unregister_parameter")("path_pattern", "grid_direction")
143
        self.core.get("unregister_parameter")("process", "grid_direction")
144 145


146
class PathParamSpiralDirection(pycam.Plugins.PluginBase):
sumpfralle's avatar
sumpfralle committed
147

148
    DEPENDS = ["Processes", "PathParamPattern"]
sumpfralle's avatar
sumpfralle committed
149 150 151
    CATEGORIES = ["Process", "Parameter"]

    def setup(self):
152
        self.control = pycam.Gui.ControlsGTK.InputChoice(
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
                    (("outside -> center", pycam.Toolpath.MotionGrid.SPIRAL_DIRECTION_IN),
                    ("center -> outside", pycam.Toolpath.MotionGrid.SPIRAL_DIRECTION_OUT)),
                change_handler=lambda widget=None: self.core.emit_event(
                        "process-changed"))
        self.core.get("register_parameter")("path_pattern", "spiral_direction",
                self.control)
        self.core.register_ui("process_path_parameters", "Direction",
                self.control.get_widget(), weight=40)
        return True

    def teardown(self):
        self.core.unregister_ui("process_path_parameters", self.control.get_widget())
        self.core.get("unregister_parameter")("path_pattern", "spiral_direction")


class PathParamPattern(pycam.Plugins.PluginBase):

    DEPENDS = ["Processes", "ParameterGroupManager"]
    CATEGORIES = ["Process", "Parameter"]

    def setup(self):
        self.choices = []
        self.control = pycam.Gui.ControlsGTK.InputChoice([],
sumpfralle's avatar
sumpfralle committed
176 177
                change_handler=lambda widget=None: self.core.emit_event(
                    "process-changed"))
178 179
        self.control.set_conversion(set_conv=self._set_value_converter,
                get_conv=self._get_value_converter)
180 181
        self.core.get("register_parameter")("process", "path_pattern",
                self.control)
182 183 184 185
        self.core.get("register_parameter_group")("path_pattern",
                changed_set_event="process-path-pattern-changed",
                changed_set_list_event="process-path-pattern-list-changed",
                get_current_set_func=self._get_pattern)
186 187
        self.core.register_ui("process_path_parameters", "Pattern",
                self.control.get_widget(), weight=5)
188 189 190 191
        self.control.get_widget().connect("changed", lambda widget: \
                self.core.emit_event("process-path-pattern-changed"))
        self.core.register_event("process-path-pattern-list-changed",
                self._update_selector)
sumpfralle's avatar
sumpfralle committed
192 193
        return True

194 195
    def _get_value_converter(self, value):
        if value:
196 197 198 199 200 201
            pattern_sets = self.core.get("get_parameter_sets")("path_pattern")
            try:
                current_pattern_set = pattern_sets[value]
            except KeyError:
                return None
            parameter_keys = current_pattern_set["parameters"].keys()
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
            all_parameters = self.core.get("get_parameter_values")("path_pattern")
            result = {"name": value, "parameters": {}}
            for parameter_key in parameter_keys:
                result["parameters"][parameter_key] = all_parameters[parameter_key]
            return result
        else:
            return None

    def _set_value_converter(self, value):
        if value:
            self.core.get("set_parameter_values")("path_pattern", value["parameters"])
            return value["name"]
        elif self.choices:
            # use the first entry as the default value
            return self.choices[0][1]
        else:
            return None

sumpfralle's avatar
sumpfralle committed
220
    def teardown(self):
221
        self.core.unregister_ui("process_path_parameters", self.control.get_widget())
Lars Kruse's avatar
Lars Kruse committed
222 223
        self.core.unregister_event("process-path-pattern-list-changed",
                self._update_selector)
224
        self.core.get("unregister_parameter")("process", "path_pattern")
Lars Kruse's avatar
Lars Kruse committed
225 226
        self.core.get("unregister_parameter_group")("path_pattern")

sumpfralle's avatar
sumpfralle committed
227

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    def _update_selector(self):
        patterns = list(self.core.get("get_parameter_sets")(
                "path_pattern").values())
        patterns.sort(key=lambda item: item["weight"])
        self.choices = []
        for pattern in patterns:
            self.choices.append((pattern["label"], pattern["name"]))
        self.control.update_choices(self.choices)
        if not self.control.get_value() and self.choices:
            self.control.set_value({"name": self.choices[0][1], "parameters": {}})

    def _get_pattern(self):
        pattern_set = self.control.get_value()
        if pattern_set:
            return self.core.get("get_parameter_sets")("path_pattern")[pattern_set["name"]]
        else:
            return None

sumpfralle's avatar
sumpfralle committed
246

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
class PathParamRoundedSpiralCorners(pycam.Plugins.PluginBase):

    DEPENDS = ["Processes"]
    CATEGORIES = ["Process", "Parameter"]

    def setup(self):
        self.control = pycam.Gui.ControlsGTK.InputCheckBox(
                change_handler=lambda widget=None: self.core.emit_event(
                    "process-changed"))
        self.core.get("register_parameter")("path_pattern", "rounded_corners",
                self.control)
        self.core.register_ui("process_path_parameters", "Rounded corners",
                self.control.get_widget(), weight=80)
        return True

    def teardown(self):
        self.core.unregister_ui("process_path_parameters",
                self.control.get_widget())
        self.core.get("unregister_parameter")("path_pattern", "rounded_corners")


268 269
class PathParamRadiusCompensation(pycam.Plugins.PluginBase):

270
    DEPENDS = ["Processes"]
271
    CATEGORIES = ["Process", "Parameter"]
272 273

    def setup(self):
274
        self.control = pycam.Gui.ControlsGTK.InputCheckBox(
275
                change_handler=lambda widget=None: self.core.emit_event(
276
                    "process-changed"))
277 278 279 280
        self.core.get("register_parameter")("process", "radius_compensation",
                self.control)
        self.core.register_ui("process_path_parameters", "Radius compensation",
                self.control.get_widget(), weight=80)
281 282 283
        return True

    def teardown(self):
284 285
        self.core.unregister_ui("process_path_parameters", self.control.get_widget())
        self.core.get("unregister_parameter")("process", "radius_compensation")
286 287 288 289


class PathParamTraceModel(pycam.Plugins.PluginBase):

290
    DEPENDS = ["Processes", "Models"]
291
    CATEGORIES = ["Process", "Parameter"]
292 293

    def setup(self):
294
        self.control = pycam.Gui.ControlsGTK.InputTable([],
sumpfralle's avatar
sumpfralle committed
295
                change_handler=lambda widget=None: \
296
                    self.core.emit_event("process-changed"))
297 298 299 300 301 302 303
        # configure the input/output converter
        def get_converter(model_refs):
            models_dict = {}
            for model in self.core.get("models"):
                models_dict[id(model)] = model
            models = []
            for model_ref in model_refs:
304 305
                if model_ref in models_dict:
                    models.append(models_dict[model_ref])
306 307 308
            return models
        def set_converter(models):
            return [id(model) for model in models]
309
        self.control.set_conversion(set_conv=set_converter,
310
                get_conv=get_converter)
311 312 313 314
        self.core.get("register_parameter")("process", "trace_models",
                self.control)
        self.core.register_ui("process_path_parameters", "Trace models (2D)",
                self.control.get_widget(), weight=5)
315 316 317
        self.core.register_event("model-list-changed", self._update_models)
        return True

318 319 320 321 322
    def teardown(self):
        self.core.get("unregister_parameter")("process", "trace_models")
        self.core.unregister_ui("process_path_parameters", self.control.get_widget())
        self.core.unregister_event("model-list-changed", self._update_models)

323 324 325 326
    def _update_models(self):
        choices = []
        models = self.core.get("models")
        for model in models:
327
            if hasattr(model.model, "get_polygons"):
328
                choices.append((model["name"], model))
329
        self.control.update_choices(choices)
330

sumpfralle's avatar
sumpfralle committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

class PathParamPocketingType(pycam.Plugins.PluginBase):

    DEPENDS = ["Processes"]
    CATEGORIES = ["Process", "Parameter"]

    def setup(self):
        self.control = pycam.Gui.ControlsGTK.InputChoice(
                    (("none", pycam.Toolpath.MotionGrid.POCKETING_TYPE_NONE),
                    ("holes", pycam.Toolpath.MotionGrid.POCKETING_TYPE_HOLES),
                    ("material", pycam.Toolpath.MotionGrid.POCKETING_TYPE_MATERIAL)),
                change_handler=lambda widget=None: self.core.emit_event(
                        "process-changed"))
        self.core.get("register_parameter")("process", "pocketing_type",
                self.control)
        self.core.register_ui("process_path_parameters", "Pocketing",
                self.control.get_widget(), weight=60)
        return True

    def teardown(self):
        self.core.unregister_ui("process_path_parameters", self.control.get_widget())
        self.core.get("unregister_parameter")("path_pattern", "pocketing_type")