def test_split_produces_2_lines(self): p1 = Point() p2 = Point(1, 0) l = Line(p1, p2) lines = l.split() count = sum([1 for _ in lines]) assert_equals(count, 2)
def test_as_points_into_1(self): p1 = Point() p2 = Point(1, 0) l = Line(p1, p2) points = list(l.as_points(1)) assert_equals(len(points), 1) assert_equals(points[0], l.midpoint())
def test_midpoint(self): p1 = Point() p2 = Point(1, 0) l = Line(p1, p2) midpoint = l.midpoint() expected = Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2) distance = midpoint.distance(expected) assert_equals(distance, 0)
def test_split_1_produces_same_line(self): p1 = Point() p2 = Point(1, 0) l = Line(p1, p2) lines = list(l.split(1)) count = sum([1 for _ in lines]) assert_equals(count, 1) assert_equals(l, lines[0])
def test_as_points_into_2(self): p1 = Point() p2 = Point(1, 0) l = Line(p1, p2) points = list(l.as_points(2)) assert_equals(len(points), 2) assert_equals(points[0], p1) assert_equals(points[1], p2)
def test_as_points_into_3(self): p1 = Point() p2 = Point(1, 0) l = Line(p1, p2) points = list(l.as_points(3)) assert_equals(len(points), 3) assert_equals(points[1], Point(0.5, 0)) assert_equals(points[0], p1) assert_equals(points[2], p2)
def test_points(self): s = Square(Point(0, 0), 2) top_right_distance = Point(1, 1).distance(s.top_right_point) assert_almost_equals(top_right_distance, 0) top_left_distance = Point(-1, 1).distance(s.top_left_point) assert_almost_equals(top_left_distance, 0) bottom_right_distance = Point(1, -1).distance(s.bottom_right_point) assert_almost_equals(bottom_right_distance, 0) bottom_left_distance = Point(-1, -1).distance(s.bottom_left_point) assert_almost_equals(bottom_left_distance, 0)
def generate_paths(file_as_string): svg_shapes = { 'rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path' } tree = ET.ElementTree(ET.fromstring(file_as_string)) root = tree.getroot() width = root.get('width') height = root.get('height') if width is None or height is None: view_box = root.get('viewBox') if view_box: _, _, width, height = view_box.split() if width is None or height is None: print("Unable to get width and height for the svg") sys.exit(1) numeric = '0123456789-.' for i, c in enumerate(width): if c not in numeric: break width = width[:i] for i, c in enumerate(height): if c not in numeric: break height = height[:i] width = float(width) height = float(height) scale_x = bed_max_x / max(width, height) scale_y = bed_max_y / max(width, height) for elem in root.iter(): try: _, tag_suffix = elem.tag.split('}') except ValueError: continue if tag_suffix in svg_shapes: shape_class = getattr(shapes_pkg, tag_suffix) shape_obj = shape_class(elem) d = shape_obj.d_path() m = shape_obj.transformation_matrix() if d: path = Path() p = point_generator(d, m, smoothness) for x, y in p: #x = scale_x * x #y = scale_y * y if 0 < x < bed_max_x and 0 < y < bed_max_y: path.add_point(Point(x, y)) yield path
def as_points(self, count_of_points): if count_of_points == 1: yield self.midpoint() else: count = count_of_points - 1 yield self.start for i in range(1, count): # http://www.dummies.com/education/math/trigonometry/how-to-divide-a-line-segment-into-multiple-parts/ x = self.start.x + (i/count)*(self.end.x - self.start.x) y = self.start.y + (i/count)*(self.end.y - self.start.y) yield Point(x, y) yield self.end
def path(self): if self.line_count != 0: ends_on_top = self.line_count % 2 == 0 if ends_on_top: top_points = [ p.copy() for p in self.bounding_box.top_line.as_points( self.line_count) ] bottom_points = [ p.copy() for p in self.bounding_box.bottom_line.as_points( self.line_count + 1) ][1:-1] else: top_points = [ p.copy() for p in self.bounding_box.top_line.as_points( self.line_count + 1) ][:-1] bottom_points = [ p.copy() for p in self.bounding_box.bottom_line.as_points( self.line_count + 1) ][1:] change = ((self.bounding_box.size * self.v_scale) - self.bounding_box.size) / 2 if change: for p in top_points: p.translate(Point(0, change)) for p in bottom_points: p.translate(Point(0, change * -1)) q = [] while top_points: q.append(top_points.pop(0)) if bottom_points: q.append(bottom_points.pop(0)) if self.reverse: q.reverse() for p in q: yield p
def __init__(self, origin=None, size=1, angle=0): self.origin = origin or Point() self.size = size self.angle = angle self._points = {} self._bl = None self._tl = None self._point_lookup = { "tl": self.top_left_point, "tr": self.top_right_point, "bl": self.bottom_left_point, "br": self.bottom_right_point, } self.mirror_x = False self.mirror_y = False
def draw(drawing, center=Point()): if not drawing: return first = True for point in drawing.path(): if not point: continue point = point.copy() point.translate(center) if first: if point.distance(bot) > 2: bot.penUp() first = False bot.goto(point.x, point.y) bot.penDown()
def lines(data, conf, shades=10.0): shades -= 1 x, y, pixel, reverse = data pixel = 255 - pixel # invert the color 255 is now black and 0 is white pixel = int(pixel * (shades / 255.0)) pixel = pixel - 2 if pixel < 0: pixel = 0 if pixel: x = x * conf.pixel_size y = y * conf.pixel_size center = Point(x + conf.pixel_size / 2, y + conf.pixel_size / 2) bounding_box = Square(origin=center, size=conf.pixel_size) return WShader(bounding_box=bounding_box, line_count=pixel, reverse=reverse, v_scale=0.8)
def test_bottom_line(self): s = Square(Point(0, 0), 2) expected = Line(Point(-1, -1), Point(1, -1)) assert_equal(s.bottom_line, expected)
def test_top_line(self): s = Square(Point(0, 0), 2) expected = Line(Point(-1, 1), Point(1, 1)) assert_equal(s.top_line, expected)
def _unit_point(self): return self._get_point( "unit", lambda: Point(self.size / 2, self.size / 2).rotate(self.angle))
def test_split_produces_2_lines_with_equal_slope(self): p1 = Point() p2 = Point(1, 0) l = Line(p1, p2) lines = list(l.split()) assert_almost_equals(lines[0].slope, lines[1].slope)
img = image.getImage('C:\\Users\\jimmv\\Desktop\\mountain.jpeg') conf = image.setup(img) return image.gcode_lines(img.pixels, conf) if __name__ == '__main__': freeze_support() from input import Pad pad = Pad() from drawbot import DrawBot bot = DrawBot() control_mode(bot) print("Starting print") center = Point(bot.x, bot.y) from svg2gcode.svg2gcode import generate_paths with open('C:\\Users\\jimmv\\Desktop\\Dornier.svg', 'r') as file: data = file.read() for p in generate_paths(data): draw(p, center) # for p in run_image(): # draw(p, center) #for i in range(0, 90): # draw(Square(center, 50-(i/2.0), i*3)) bot.penUp() bot.goto(0, 0)
def test_wshader_v_scale(self): s = Square() shader = WShader(bounding_box=s, line_count=1, v_scale=2) point_list = [p for p in shader.path()] assert_equal(Point(-0.5, 1), point_list[0])
def midpoint(self): return Point((self.start.x + self.end.x) / 2, (self.start.y + self.end.y) / 2)