def to_svg_path(line_segment_chain: LineSegmentChain, transformation=None, color="black", stroke_width="0.864583px", draw_arrows=False) -> ElementTree.Element: """ A handy debugging function which converts the current line-chain to svg form :param line_segment_chain: The LineSegmentChain to the converted. :param transformation: A transformation to apply to every line before converting it. :param color: The path's color. :param stroke_width: The path's stroke width. :param stroke_width: Whether or not to draw arrows at the end of each segment. Requires placing the output of arrow_defs() in the document. """ start = Vector(line_segment_chain.get(0).start.x, line_segment_chain.get(0).start.y) if transformation: start = transformation.apply_transformation(start) d = f"M{start.x} {start.y}" for line in line_segment_chain: end = Vector(line.end.x, line.end.y) if transformation: end = transformation.apply_transformation(end) d += f" L {end.x} {end.y}" style = f"fill:none;stroke:{color};stroke-width:{stroke_width};stroke-linecap:butt;stroke-linejoin:miter;stroke" \ "-opacity:1 " path = ElementTree.Element("{%s}path" % svg_namespace) path.set("d", d) path.set("style", style) if draw_arrows: path.set("marker-mid", "url(#arrow-346)") return path
def append_line_chain(self, line_chain: LineSegmentChain): """ Draws a LineSegmentChain by calling interface.linear_move() for each segment. The resulting code is appended to self.body """ if line_chain.chain_size() == 0: warnings.warn("Attempted to parse empty LineChain") return [] code = [] start = line_chain.get(0).start # Don't turn off laser if the new start is at the current position if self.interface.position is None or abs( self.interface.position - start) > TOLERANCES["operation"]: code = [ self.interface.laser_off(), self.interface.linear_move(z=self.movement_height), self.interface.set_movement_speed(self.movement_speed), self.interface.linear_move(start.x, start.y), self.interface.set_movement_speed(self.cutting_speed), self.interface.set_laser_power(1), self.interface.linear_move(z=self.cutting_height) ] for line in line_chain: code.append(self.interface.linear_move(line.end.x, line.end.y)) self.body.extend(code)
def append_curves(self, curves: [typing.Type[Curve]]): """ Draws curves by approximating them as line segments and calling self.append_line_chain(). The resulting code is appended to self.body """ for curve in curves: line_chain = LineSegmentChain() approximation = LineSegmentChain.line_segment_approximation(curve) line_chain.extend(approximation) self.append_line_chain(line_chain)
def run_test(svg_file_name, debug_file_name): curves = parse_file(svg_file_name) success = True approximations = [] count = 0 for curve in curves: approximation = LineSegmentChain.line_segment_approximation(curve) approximations.append(approximation) count += approximation.chain_size() generate_debug(approximations, svg_file_name, debug_file_name) return success
def run_test(svg_file_name, debug_file_name): curves = parse_file(svg_file_name) success = True approximations = [] for curve in curves: approximation = LineSegmentChain.line_segment_approximation(curve) approximations.append(approximation) # Todo find a way to automatically determine success. Right now manual revision of debug files is required. generate_debug(approximations, svg_file_name, debug_file_name) return success
def draw_debug_traces(self, curves): """Traces arrows over all parsed paths""" root = self.document.getroot() origin = self.options.machine_origin bed_width = self.options.bed_width bed_height = self.options.bed_height height_str = root.get("height") canvas_height = float(height_str) if height_str.isnumeric() else float( height_str[:-2]) group = etree.Element("{%s}g" % svg_name_space) group.set("id", "debug_traces") group.set("{%s}groupmode" % inkscape_name_space, "layer") group.set("{%s}label" % inkscape_name_space, "debug traces") group.append( etree.fromstring( xml_tree.tostring( debug_methods.arrow_defs( arrow_scale=self.options.debug_arrow_scale)))) for curve in curves: approximation = LineSegmentChain.line_segment_approximation(curve) change_origin = Transformation() if origin != "top-left": change_origin.add_scale(1, -1) change_origin.add_translation(0, -canvas_height) if origin == "center": change_origin.add_translation(bed_width / 2, bed_height / 2) path_string = xml_tree.tostring( debug_methods.to_svg_path( approximation, color="red", stroke_width=f"{self.options.debug_line_width}px", transformation=change_origin, draw_arrows=True)) group.append(etree.fromstring(path_string)) root.append(group)