ContourCutter.py 2.59 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
# -*- coding: utf-8 -*-
"""
$Id$

Copyright 2008-2010 Lode Leroy

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/>.
"""

23
import pycam.PathProcessors
24
from pycam.Geometry.PolygonExtractor import PolygonExtractor
25
from pycam.Geometry.PointUtils import *
26
from pycam.Toolpath import simplify_toolpath
lode_leroy's avatar
lode_leroy committed
27

28
class ContourCutter(pycam.PathProcessors.BasePathProcessor):
29
    def __init__(self):
30
        super(ContourCutter, self).__init__()
lode_leroy's avatar
lode_leroy committed
31 32
        self.curr_path = None
        self.scanline = None
33
        self.polygon_extractor = None
lode_leroy's avatar
lode_leroy committed
34
        self.points = []
35
        self.__forward = (1, 1, 0)
lode_leroy's avatar
lode_leroy committed
36

37
    def append(self, point):
38 39
        # Sort the points in positive x/y direction - otherwise the
        # PolygonExtractor breaks.
40
        if self.points and (pdot(psub(point, self.points[0]), self.__forward) < 0):
41
            self.points.insert(0, point)
42
        else:
43
            self.points.append(point)
lode_leroy's avatar
lode_leroy committed
44

45
    def new_direction(self, direction):
46 47
        if self.polygon_extractor == None:
            self.polygon_extractor = PolygonExtractor(PolygonExtractor.CONTOUR)
lode_leroy's avatar
lode_leroy committed
48

49
        self.polygon_extractor.new_direction(direction)
lode_leroy's avatar
lode_leroy committed
50 51

    def end_direction(self):
52
        self.polygon_extractor.end_direction()
lode_leroy's avatar
lode_leroy committed
53 54

    def new_scanline(self):
55
        self.polygon_extractor.new_scanline()
lode_leroy's avatar
lode_leroy committed
56 57 58
        self.points = []

    def end_scanline(self):
59 60 61
        for i in range(1, len(self.points) - 1):
            self.polygon_extractor.append(self.points[i])
        self.polygon_extractor.end_scanline()
lode_leroy's avatar
lode_leroy committed
62 63

    def finish(self):
64 65 66 67 68
        self.polygon_extractor.finish()
        if self.polygon_extractor.merge_path_list:
            paths = self.polygon_extractor.merge_path_list
        elif self.polygon_extractor.hor_path_list:
            paths = self.polygon_extractor.hor_path_list
lode_leroy's avatar
lode_leroy committed
69
        else:
70
            paths = self.polygon_extractor.ver_path_list
71
        if paths:
72 73 74
            for path in paths:
                path.append(path.points[0])
                simplify_toolpath(path)
75 76
        if paths:
            self.paths.extend(paths)
77
            self.sort_layered()
78
        self.polygon_extractor = None
lode_leroy's avatar
lode_leroy committed
79