def make_image(): step_x = self.raster_step_x step_y = self.raster_step_y bounds = self.bounds try: image = make_raster(list(self.flat()), bounds=bounds, step_x=step_x, step_y=step_y) except AssertionError: raise CutPlanningFailedError("Raster too large.") if image.width == 1 and image.height == 1: # TODO: Solve this is a less kludgy manner. The call to make the image can fail the first time # around because the renderer is what sets the size of the text. If the size hasn't already # been set, the initial bounds are wrong. bounds = self.bounds try: image = make_raster(list(self.flat()), bounds=bounds, step_x=step_x, step_y=step_y) except AssertionError: raise CutPlanningFailedError("Raster too large.") image = image.convert("L") matrix = Matrix.scale(step_x, step_y) matrix.post_translate(bounds[0], bounds[1]) image_node = ImageNode(image=image, matrix=matrix) self.children.clear() self.add_node(image_node)
def test_fill_euler_scale(self): w = 1000 h = 1000 paths = ( ( (w * 0.05, h * 0.05), (w * 0.95, h * 0.05), (w * 0.95, h * 0.95), (w * 0.05, h * 0.95), (w * 0.05, h * 0.05), ), ( (w * 0.25, h * 0.25), (w * 0.75, h * 0.25), (w * 0.75, h * 0.75), (w * 0.25, h * 0.75), (w * 0.25, h * 0.25), ), ) matrix = Matrix.scale(0.005) fill = list(eulerian_fill(settings={}, outlines=paths, matrix=matrix)) self.assertEqual(len(fill), 327) for x, y in fill: self.assertIn(x, (50, 250, 750, 950))
def draw_cutcode(self, cutcode: CutCode, gc: wx.GraphicsContext, x: int = 0, y: int = 0): """ Draw cutcode object into wxPython graphics code. This code accepts x,y offset values. The cutcode laser offset can be set with a command with the rest of the cutcode remaining the same. So drawing the cutcode requires knowing what, if any offset is currently being applied. @param cutcode: flat cutcode object to draw. @param gc: wx.graphics context @param x: offset in x direction @param y: offset in y direction @return: """ p = None last_point = None color = None for cut in cutcode: c = cut.line_color if c is not color: color = c last_point = None if p is not None: gc.StrokePath(p) del p p = gc.CreatePath() self.set_pen(gc, c, width=7.0, alpha=127) start = cut.start end = cut.end if p is None: p = gc.CreatePath() if last_point != start: p.MoveToPoint(start[0] + x, start[1] + y) if isinstance(cut, LineCut): # Standard line cut. Applies to path object. p.AddLineToPoint(end[0] + x, end[1] + y) elif isinstance(cut, QuadCut): # Standard quadratic bezier cut p.AddQuadCurveToPoint(cut.c()[0] + x, cut.c()[1] + y, end[0] + x, end[1] + y) elif isinstance(cut, CubicCut): # Standard cubic bezier cut p.AddCurveToPoint( cut.c1()[0] + x, cut.c1()[1] + y, cut.c2()[0] + x, cut.c2()[1] + y, end[0] + x, end[1] + y, ) elif isinstance(cut, RasterCut): # Rastercut object. image = cut.image gc.PushState() matrix = Matrix.scale(cut.step_x, cut.step_y) matrix.post_translate(cut.offset_x + x, cut.offset_y + y) # Adjust image xy gc.ConcatTransform( wx.GraphicsContext.CreateMatrix(gc, ZMatrix(matrix))) try: cache = cut.cache cache_id = cut.cache_id except AttributeError: cache = None cache_id = -1 if cache_id != id(image): # Cached image is invalid. cache = None if cache is None: # No valid cache. Generate. cut.c_width, cut.c_height = image.size try: cut.cache = self.make_thumbnail(image, maximum=5000) except (MemoryError, RuntimeError): cut.cache = None cut.cache_id = id(image) if cut.cache is not None: # Cache exists and is valid. gc.DrawBitmap(cut.cache, 0, 0, cut.c_width, cut.c_height) else: # Image was too large to cache, draw a red rectangle instead. gc.SetBrush(wx.RED_BRUSH) gc.DrawRectangle(0, 0, cut.c_width, cut.c_height) gc.DrawBitmap(icons8_image_50.GetBitmap(), 0, 0, cut.c_width, cut.c_height) gc.PopState() elif isinstance(cut, RawCut): pass elif isinstance(cut, PlotCut): p.MoveToPoint(start[0] + x, start[1] + y) for px, py, pon in cut.plot: if pon == 0: p.MoveToPoint(px + x, py + y) else: p.AddLineToPoint(px + x, py + y) elif isinstance(cut, DwellCut): pass elif isinstance(cut, WaitCut): pass elif isinstance(cut, InputCut): pass elif isinstance(cut, OutputCut): pass last_point = end if p is not None: gc.StrokePath(p) del p