ToolpathGrid.py 4.68 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
# -*- 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
25
from pycam.Geometry.PointUtils import *
26 27 28 29 30


class ToolpathGrid(pycam.Plugins.PluginBase):

    UI_FILE = "toolpath_grid.ui"
31
    DEPENDS = ["Toolpaths"]
32
    CATEGORIES = ["Toolpath"]
33

sumpfralle's avatar
sumpfralle committed
34 35
    def setup(self):
        if self.gui:
36
            self._gtk_handlers = []
37 38 39 40 41
            self._frame = self.gui.get_object("ToolpathGridFrame")
            self.core.register_ui("toolpath_handling", "Clone grid",
                    self._frame, 30)
            for objname in ("GridYCount", "GridXCount"):
                self.gui.get_object(objname).set_value(1)
sumpfralle's avatar
sumpfralle committed
42 43
            for objname in ("GridYCount", "GridXCount", "GridYDistance",
                    "GridXDistance"):
44 45 46 47
                self._gtk_handlers.append((self.gui.get_object(objname),
                        "value-changed", self._update_widgets))
            self._gtk_handlers.append((self.gui.get_object("GridCreate"),
                    "clicked", self.create_toolpath_grid))
48 49
            self.core.register_event("toolpath-selection-changed",
                    self._update_widgets)
50
            self.register_gtk_handlers(self._gtk_handlers)
51
            self._update_widgets()
sumpfralle's avatar
sumpfralle committed
52 53
        return True

54
    def teardown(self):
55 56
        if self.gui:
            self.core.unregister_ui("toolpath_handling", self._frame)
57
            self.unregister_gtk_handlers(self._gtk_handlers)
58 59
            self.core.unregister_event("toolpath-selection-changed",
                    self._update_widgets)
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    def _get_toolpaths_dim(self, toolpaths):
        if toolpaths:
            maxx = max([tp.maxx for tp in toolpaths])
            minx = min([tp.minx for tp in toolpaths])
            maxy = max([tp.maxy for tp in toolpaths])
            miny = min([tp.miny for tp in toolpaths])
            return (maxx - minx), (maxy - miny)
        else:
            return None, None

    def _update_widgets(self, widget=None):
        toolpaths = self.core.get("toolpaths").get_selected()
        if toolpaths:
            x_dim, y_dim = self._get_toolpaths_dim(toolpaths)
            x_count = self.gui.get_object("GridXCount").get_value()
76
            x_space = self.gui.get_object("GridXDistance").get_value()
77
            y_count = self.gui.get_object("GridYCount").get_value()
78
            y_space = self.gui.get_object("GridYDistance").get_value()
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
            x_width = x_dim * x_count + x_space * (x_count - 1)
            y_width = y_dim * y_count + y_space * (y_count - 1)
            self.gui.get_object("LabelGridXWidth").set_label("%g%s" % \
                    (x_width, self.core.get("unit_string")))
            self.gui.get_object("LabelGridYWidth").set_label("%g%s" % \
                    (y_width, self.core.get("unit_string")))
            self._frame.show()
        else:
            self._frame.hide()

    def create_toolpath_grid(self, widget=None):
        toolpaths = self.core.get("toolpaths").get_selected()
        x_count = int(self.gui.get_object("GridXCount").get_value())
        y_count = int(self.gui.get_object("GridYCount").get_value())
        x_space = self.gui.get_object("GridXDistance").get_value()
        y_space = self.gui.get_object("GridYDistance").get_value()
        x_dim, y_dim = self._get_toolpaths_dim(toolpaths)
        for toolpath in toolpaths:
            new_paths = []
98 99
            for x in range(x_count):
                for y in range(y_count):
100
                    shift = (x * (x_space + x_dim), y * (y_space + y_dim), 0, 'v')
101
                    for path in toolpath.paths:
102
                        new_path = pycam.Geometry.Path.Path()
103
                        new_path.points = [padd(shift, p) for p in path.points]
104 105 106
                        new_paths.append(new_path)
            if not self.gui.get_object("KeepOriginal").get_active():
                toolpath.paths = new_paths
107
                self.core.emit_event("toolpath-changed")
108
            else:
109 110
                new_toolpath = toolpath.copy()
                new_toolpath.paths = new_paths
111 112
                self.core.get("toolpaths").append(new_toolpath)
        self.core.get("toolpaths").select(toolpaths)
113