Processes SVG files generated by OpenSCAD to prepare for laser cutting
| 48 | |
| 49 | |
| 50 | class SvgProcessor(object): |
| 51 | """ |
| 52 | Processes SVG files generated by OpenSCAD to prepare for laser cutting |
| 53 | """ |
| 54 | |
| 55 | def __init__(self, input_file): |
| 56 | self.dom = minidom.parse(input_file) |
| 57 | self.svg_node = self.dom.documentElement |
| 58 | |
| 59 | def set_dimensions(self, width, height): |
| 60 | self.svg_node.attributes['width'].value = width |
| 61 | self.svg_node.attributes['height'].value = height |
| 62 | |
| 63 | def set_viewbox(self, min_x, min_y, width, height): |
| 64 | view_str = "{:.0f} {:.0f} {:.0f} {:.0f}".format(min_x, min_y, width, height) |
| 65 | self.svg_node.attributes['viewBox'].value = view_str |
| 66 | |
| 67 | def get_viewbox(self): |
| 68 | vb = self.svg_node.attributes['viewBox'].value.replace(',', '').split(' ') |
| 69 | vb = [float(x) for x in vb] |
| 70 | return tuple(vb) |
| 71 | |
| 72 | def apply_laser_cut_style(self): |
| 73 | # Set fill and stroke for laser cutting |
| 74 | for path in self.svg_node.getElementsByTagName('path'): |
| 75 | SvgProcessor._apply_attributes(path, { |
| 76 | 'fill': 'none', |
| 77 | 'stroke': '#0000ff', |
| 78 | 'stroke-width': '0.1', |
| 79 | }) |
| 80 | |
| 81 | def apply_laser_etch_style(self): |
| 82 | # Set fill and stroke for laser etching |
| 83 | for path in self.svg_node.getElementsByTagName('path'): |
| 84 | SvgProcessor._apply_attributes(path, { |
| 85 | 'fill': '#000000', |
| 86 | 'stroke': 'none', |
| 87 | }) |
| 88 | |
| 89 | def apply_raster_render_style(self): |
| 90 | # Set fill and stroke for rasterized rendering |
| 91 | for path in self.svg_node.getElementsByTagName('path'): |
| 92 | SvgProcessor._apply_attributes(path, { |
| 93 | 'fill': 'none', |
| 94 | 'stroke': '#000000', |
| 95 | 'stroke-width': '0.2', |
| 96 | }) |
| 97 | |
| 98 | def import_paths(self, from_svg_processor): |
| 99 | for path in from_svg_processor.svg_node.getElementsByTagName('path'): |
| 100 | output_node = self.dom.importNode(path, True) |
| 101 | self.svg_node.appendChild(output_node) |
| 102 | |
| 103 | vb = self.merge_viewbox(from_svg_processor.get_viewbox()) |
| 104 | self.set_viewbox(*vb) |
| 105 | dimm = "{:.0f}mm" |
| 106 | self.set_dimensions(dimm.format(vb[2]), dimm.format(vb[3])) |
| 107 |
no outgoing calls
no test coverage detected