Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
# Copyright 2015 Scott Bezek
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from xml.dom import minidom
"""
Processes SVG files generated by OpenSCAD to prepare for laser cutting
"""
REGISTRATION_MARK = "M -49,49 L -50,49 L -50,50 L -49,50 z"
REGISTRATION_MARK2 = "M -47,47 L -48,47 L -48,48 L -47,48 z"
class SvgProcessor(object):
def __init__(self, input_file):
self.dom = minidom.parse(input_file)
self.svg_node = self.dom.documentElement
def fix_dimens(self):
# Add mm units to the document dimensions
self.svg_node.attributes['width'].value += 'mm'
self.svg_node.attributes['height'].value += 'mm'
def apply_laser_cut_style(self):
# Set fill and stroke for laser cutting
for path in self.svg_node.getElementsByTagName('path'):
SvgProcessor._apply_attributes(path, {
'fill': 'none',
'stroke': '#0000ff',
'stroke-width': '0.01',
})
def apply_laser_etch_style(self):
# Set fill and stroke for laser etching
for path in self.svg_node.getElementsByTagName('path'):
SvgProcessor._apply_attributes(path, {
'fill': '#000000',
'stroke': 'none',
})
def apply_raster_render_style(self):
# Set fill and stroke for rasterized rendering
for path in self.svg_node.getElementsByTagName('path'):
SvgProcessor._apply_attributes(path, {
'fill': 'none',
'stroke': '#000000',
'stroke-width': '0.2',
})
def delete_registration_mark(self):
for path in self.svg_node.getElementsByTagName('path'):
data = path.attributes['d']
if REGISTRATION_MARK not in data.value:
raise ValueError("Registration mark not found!")
if REGISTRATION_MARK2 not in data.value:
raise ValueError("Registration mark not found!")
data.value = data.value.replace(REGISTRATION_MARK, '')
data.value = data.value.replace(REGISTRATION_MARK2, '')
if data.value.strip() == '':
self.svg_node.removeChild(path)
return len(self.svg_node.getElementsByTagName('path')) > 0
def import_paths(self, from_svg_processor):
for path in from_svg_processor.svg_node.getElementsByTagName('path'):
output_node = self.dom.importNode(path, True)
self.svg_node.appendChild(output_node)
def write(self, filename):
with open(filename, 'wb') as output_file:
self.svg_node.writexml(output_file)
@staticmethod
def _apply_attributes(node, values):
for (k,v) in values.items():
node.attributes[k] = v
047780e4fThis file is pinned to the selected release. Download the original to open it in your design tool.