# HG changeset patch # User mbayer # Date 1446911835 -3600 # Node ID ff679c15cb0e69b0ae539a2a311da78cee143ba9 # Parent b41cdab37aab6599738d66895140d8bcab056222 infill only on Polygon or Path which have fill style attribute diff -r b41cdab37aab -r ff679c15cb0e svg2gcode/svg2gcode.py --- a/svg2gcode/svg2gcode.py Sat Nov 07 15:51:29 2015 +0100 +++ b/svg2gcode/svg2gcode.py Sat Nov 07 16:57:15 2015 +0100 @@ -4,6 +4,7 @@ import svg, sys from gcode import Gcode from optparse import OptionParser +from tinycss import CSS21Parser from shapely.geometry import box, MultiLineString, Point, Polygon from shapely.affinity import rotate @@ -61,12 +62,7 @@ # return clipped array return rect.intersection(lines) -def infill(bbox, polygon, angle=0, spacing=10): - b1, b2 = bbox - x1, y1 = b1.coord() - x2, y2 = b2.coord() - page = box(x1, y1, x2, y2) - hatching = hatchbox(page, angle, spacing) +def get_infill(polygon): # create shape from polygon: segments = [] for pnt in polygon: @@ -74,7 +70,10 @@ segments.append((x, y)) shape = Polygon(segments) - return shape.intersection(hatching) + if shape.is_valid: + return shape.intersection(INFILL) + else: + return [] parser = OptionParser() parser.add_option("-f", "--file", dest="filename", default=None, @@ -110,6 +109,21 @@ height *= gcode.mm_pixel * options.scale print "Print dimension: %.2fmm x %.2fmm" % (width, height) +x1, y1 = b1.coord() +x2, y2 = b2.coord() +page = box(x1, y1, x2, y2) +INFILL = hatchbox(page, 0, 2) + +def parse_style(stylestr): + if stylestr.strip() == '': + return None + parser = CSS21Parser() + style = parser.parse_style_attr(stylestr) + kv = {} + for obj in style[0]: + kv[obj.name] = obj.value[0].value + return kv + def normalize(coord): x = coord[0] y = coord[1] @@ -128,9 +142,21 @@ x, y = normalize(pt.coord()) gcode.engrave(x, y) - if not options.outline: + 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 + # THE INFILL - for line in infill(im.bbox(), l, spacing=2): + for line in get_infill(l): 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])