Sat, 07 Nov 2015 18:45:17 +0100
finished poly infill
svg2gcode/svg2gcode.py | file | annotate | diff | comparison | revisions |
--- a/svg2gcode/svg2gcode.py Sat Nov 07 16:57:15 2015 +0100 +++ b/svg2gcode/svg2gcode.py Sat Nov 07 18:45:17 2015 +0100 @@ -2,11 +2,12 @@ # -*- coding: utf-8 -*- import svg, sys +import cairo from gcode import Gcode from optparse import OptionParser from tinycss import CSS21Parser -from shapely.geometry import box, MultiLineString, Point, Polygon +from shapely.geometry import box, MultiLineString, MultiPolygon, Polygon from shapely.affinity import rotate from shapely import speedups from math import sqrt @@ -62,18 +63,6 @@ # return clipped array return rect.intersection(lines) -def get_infill(polygon): - # create shape from polygon: - segments = [] - for pnt in polygon: - x, y = pnt.coord() - segments.append((x, y)) - - shape = Polygon(segments) - if shape.is_valid: - return shape.intersection(INFILL) - else: - return [] parser = OptionParser() parser.add_option("-f", "--file", dest="filename", default=None, @@ -145,18 +134,37 @@ if options.outline: continue - if (isinstance(d, svg.Polygon) or isinstance(d, svg.Path)): - #check if we shoild infill? - style = parse_style(d.style) - if not style: - continue - if not 'fill' in style.keys(): - continue - if style['fill'] == 'none': - continue + if (isinstance(d, svg.Polygon) or isinstance(d, svg.Path)): + #check if we shoild infill? + style = parse_style(d.style) + if not style: + continue + if not 'fill' in style.keys(): + continue + if style['fill'] == 'none': + continue + poly = None + for l in d.segments(1): + # create shape from polygon: + segments = [] + for pnt in l: + x, y = pnt.coord() + segments.append((x, y)) + shape = Polygon(segments) + if shape.is_valid: + if not poly: + poly = shape + else: + if shape.within(poly): + poly = poly.difference(shape) + else: + poly = poly.union(shape) + + lines = poly.intersection(INFILL) + if lines: # THE INFILL - for line in get_infill(l): + for line in lines: start = normalize((line.coords[0][0], line.coords[0][1])) end = normalize((line.coords[1][0], line.coords[1][1])) gcode.move(start[0], start[1])