simplify lasercheck

This commit is contained in:
Mario Voigt 2021-11-07 12:55:58 +01:00
parent 357d358e4d
commit 2787147b23
1 changed files with 72 additions and 82 deletions

View File

@ -7,6 +7,7 @@ import re
import math import math
from math import log from math import log
import datetime import datetime
from email.policy import default
class LaserCheck(inkex.EffectExtension): class LaserCheck(inkex.EffectExtension):
@ -345,14 +346,12 @@ class LaserCheck(inkex.EffectExtension):
if so.checks == "check_all" or so.stroke_colors is True: if so.checks == "check_all" or so.stroke_colors is True:
inkex.utils.debug("\n---------- Stroke colors ({} are allowed)".format(so.stroke_colors_max)) inkex.utils.debug("\n---------- Stroke colors ({} are allowed)".format(so.stroke_colors_max))
strokeColors = [] strokeColors = []
for element in shapes: for element in shapes:
style = element.get('style') strokeColor = element.style.get('stroke')
if style is not None: if strokeColor is None or strokeColor == "none":
stroke = re.search('(;|^)stroke:(.*?)(;|$)', style) strokeColor = "none"
if stroke is not None: if strokeColor not in strokeColors:
strokeColor = stroke[0].split("stroke:")[1].split(";")[0] strokeColors.append(strokeColor)
if strokeColor not in strokeColors:
strokeColors.append(strokeColor)
if so.show_issues_only is False: if so.show_issues_only is False:
inkex.utils.debug("{} different stroke colors in total".format(len(strokeColors))) inkex.utils.debug("{} different stroke colors in total".format(len(strokeColors)))
if len(strokeColors) > so.stroke_colors_max: if len(strokeColors) > so.stroke_colors_max:
@ -367,14 +366,12 @@ class LaserCheck(inkex.EffectExtension):
if so.checks == "check_all" or so.stroke_widths is True: if so.checks == "check_all" or so.stroke_widths is True:
inkex.utils.debug("\n---------- Stroke widths ({} are allowed)".format(so.stroke_widths_max)) inkex.utils.debug("\n---------- Stroke widths ({} are allowed)".format(so.stroke_widths_max))
strokeWidths = [] strokeWidths = []
for element in shapes: for element in shapes:
style = element.get('style') strokeWidth = element.style.get('stroke-width')
if style is not None: if strokeWidth is None or strokeWidth == "none":
stroke_width = re.search('stroke-width:(.*?)(;|$)', style) strokeWidth = "none"
if stroke_width is not None: if strokeWidth not in strokeWidths:
strokeWidth = stroke_width[0].split("stroke-width:")[1].split(";")[0] #possibly w/o units. could contain units from css strokeWidths.append(strokeWidth)
if strokeWidth not in strokeWidths:
strokeWidths.append(strokeWidth)
if so.show_issues_only is False: if so.show_issues_only is False:
inkex.utils.debug("{} different stroke widths in total".format(len(strokeWidths))) inkex.utils.debug("{} different stroke widths in total".format(len(strokeWidths)))
if len(strokeWidths) > so.stroke_widths_max: if len(strokeWidths) > so.stroke_widths_max:
@ -394,14 +391,12 @@ class LaserCheck(inkex.EffectExtension):
if so.checks == "check_all" or so.cosmestic_dashes is True: if so.checks == "check_all" or so.cosmestic_dashes is True:
inkex.utils.debug("\n---------- Cosmetic dashes - should be converted to paths") inkex.utils.debug("\n---------- Cosmetic dashes - should be converted to paths")
strokeDasharrays = [] strokeDasharrays = []
for element in shapes: for element in shapes:
style = element.get('style') strokeDasharray = element.style.get('stroke-dasharray')
if style is not None: if strokeDasharray is None or strokeDasharray == "none":
stroke_dasharray = re.search('stroke-dasharray:(.*?)(;|$)', style) strokeDasharray = "none"
if stroke_dasharray is not None: if strokeDasharray not in strokeDasharrays:
strokeDasharray = stroke_dasharray[0].split("stroke-dasharray:")[1].split(";")[0] strokeDasharrays.append(strokeDasharray)
if strokeDasharray not in strokeDasharrays:
strokeDasharrays.append(strokeDasharray)
if so.show_issues_only is False: if so.show_issues_only is False:
inkex.utils.debug("{} different stroke dash arrays in total".format(len(strokeDasharrays))) inkex.utils.debug("{} different stroke dash arrays in total".format(len(strokeDasharrays)))
for strokeDasharray in strokeDasharrays: for strokeDasharray in strokeDasharrays:
@ -412,63 +407,60 @@ class LaserCheck(inkex.EffectExtension):
Shapes/paths with the same color like the background, 0% opacity, etc. lead to strange Shapes/paths with the same color like the background, 0% opacity, etc. lead to strange
laser cutting results, like duplicated edges, enlarged laser times and more. Please double laser cutting results, like duplicated edges, enlarged laser times and more. Please double
check for such occurences. check for such occurences.
Please transfer styles from layers/groups level to element level! You can use "Cleanup Styles" extension to do that
''' '''
if so.checks == "check_all" or so.invisible_shapes is True: if so.checks == "check_all" or so.invisible_shapes is True:
inkex.utils.debug("\n---------- Invisible shapes") inkex.utils.debug("\n---------- Invisible shapes")
invisibles = [] invisibles = []
for element in shapes: for element in shapes:
if element.tag not in (inkex.addNS('tspan','svg')): if element.tag not in (inkex.addNS('tspan','svg')) and element.get('inkscape:groupmode') != 'layer' and not isinstance(element, inkex.Group):
style = element.get('style') stroke = element.style.get('stroke')
if style is not None: if stroke is None or stroke == "none":
stroke = re.search('stroke:(.*?)(;|$)', style) #filter white on white (we guess the background color of the document is white too but we do not check) strokeVis = 0
if stroke is None: elif stroke in ('#ffffff', 'white', 'rgb(255,255,255)'):
strokeVis = 0 strokeVis = 0
elif stroke[0].split("stroke:")[1].split(";")[0] == 'none': else:
strokeVis = 0 strokeVis = 1
elif stroke[0].split("stroke:")[1].split(";")[0] in ('#ffffff', 'white', 'rgb(255,255,255)'):
strokeVis = 0 stroke_width = element.style.get('stroke-width')
else: if stroke_width is None or stroke_width == "none":
strokeVis = 1 widthVis = 0
elif self.svg.unittouu(stroke_width) < 0.005: #really thin (0,005pc = 0,080px)
widthVis = 0
else:
widthVis = 1
stroke_opacity = element.style.get('stroke-opacity')
if stroke_opacity is None or stroke_opacity == "none":
strokeOpacityVis = 0
elif float(stroke_opacity) < 0.05: #nearly invisible (<5% opacity)
strokeOpacityVis = 0
else:
strokeOpacityVis = 1
if pagecolor == '#ffffff':
invisColors = [pagecolor, 'white', 'rgb(255,255,255)']
else:
invisColors = [pagecolor] #we could add some parser to convert pagecolor to rgb/hsl/cmyk
stroke_width = re.search('stroke-width:(.*?)(;|$)', style) fill = element.style.get('fill')
if stroke_width is None: if fill is None or fill == "none":
widthVis = 0 fillVis = 0
elif self.svg.unittouu(stroke_width[0].split("stroke-width:")[1].split(";")[0]) < 0.005: #really thin (0,005pc = 0,080px) elif fill in invisColors:
widthVis = 0 fillVis = 0
else: else:
widthVis = 1 fillVis = 1
stroke_opacity = re.search('stroke-opacity:(.*?)(;|$)', style) fill_opacity = element.style.get('fill-opacity')
if stroke_opacity is None: if fill_opacity is None or fill_opacity == "none": #always is opaque if not set, so set to 1
strokeOpacityVis = 0 fillOpacityVis = 1
elif float(stroke_opacity[0].split("stroke-opacity:")[1].split(";")[0]) < 0.05: #nearly invisible (<5% opacity) elif float(fill_opacity) < 0.05: #nearly invisible (<5% opacity)
strokeOpacityVis = 0 fillOpacityVis = 0
else: else:
strokeOpacityVis = 1 fillOpacityVis = 1
if pagecolor == '#ffffff': inkex.utils.debug("id={}, strokeVis={}, widthVis={}, strokeOpacityVis={}, fillVis={}, fillOpacityVis={}".format(element.get('id'), strokeVis, widthVis, strokeOpacityVis, fillVis, fillOpacityVis))
invisColors = [pagecolor, 'white', 'rgb(255,255,255)'] if element.style is not None: #f if the style attribute is not set at all, the element will be visible with default black color fill and w/o stroke
else:
invisColors = [pagecolor] #we could add some parser to convert pagecolor to rgb/hsl/cmyk
fill = re.search('fill:(.*?)(;|$)', style)
if fill is None:
fillVis = 0
elif fill[0].split("fill:")[1].split(";")[0] == 'none':
fillVis = 0
elif fill[0].split("fill:")[1].split(";")[0] in invisColors:
fillVis = 0
else:
fillVis = 1
fill_opacity = re.search('fill-opacity:(.*?)(;|$)', style)
if fill_opacity is None:
fillOpacityVis = 0
elif float(fill_opacity[0].split("fill-opacity:")[1].split(";")[0]) < 0.05: #nearly invisible (<5% opacity)
fillOpacityVis = 0
else:
fillOpacityVis = 1
#inkex.utils.debug("strokeVis={}, widthVis={}, strokeOpacityVis={}, fillVis={}, fillOpacityVis={}".format(strokeVis, widthVis, strokeOpacityVis, fillVis, fillOpacityVis))
if (strokeVis == 0 or widthVis == 0 or strokeOpacityVis == 0) and (fillVis == 0 or fillOpacityVis == 0): if (strokeVis == 0 or widthVis == 0 or strokeOpacityVis == 0) and (fillVis == 0 or fillOpacityVis == 0):
if element not in invisibles: if element not in invisibles:
invisibles.append(element) invisibles.append(element)
@ -486,13 +478,11 @@ class LaserCheck(inkex.EffectExtension):
inkex.utils.debug("\n---------- Objects with stroke transparencies < 1.0 - should be set to 1.0") inkex.utils.debug("\n---------- Objects with stroke transparencies < 1.0 - should be set to 1.0")
transparencies = [] transparencies = []
for element in shapes: for element in shapes:
style = element.get('style') stroke_opacity = element.style.get('stroke-opacity')
if style is not None: if stroke_opacity is None or stroke_opacity == "none":
stroke_opacity = re.search('stroke-opacity:(.*?)(;|$)', style) stroke_opacity = "none"
if stroke_opacity is not None: if stroke_opacity not in transparencies:
if float(stroke_opacity[0].split("stroke-opacity:")[1].split(";")[0]) < 1.0: transparencies.append(element)
if element not in transparencies:
transparencies.append(element)
if so.show_issues_only is False: if so.show_issues_only is False:
inkex.utils.debug("{} objects with stroke transparencies < 1.0 in total".format(len(transparencies))) inkex.utils.debug("{} objects with stroke transparencies < 1.0 in total".format(len(transparencies)))
for transparency in transparencies: for transparency in transparencies: