- updated the SVG parser to take into consideration the 'Close' svg element and paths that are made from a single line (we may need to switch to svgpathtools module)

This commit is contained in:
Marius Stanciu 2020-04-01 00:09:50 +03:00 committed by Marius
parent 9a74139497
commit 1d13b997f2
3 changed files with 37 additions and 6 deletions

View File

@ -5323,8 +5323,8 @@ class App(QtCore.QObject):
# try to quit the Socket opened by ArgsThread class
try:
self.new_launch.listener.close()
except Exception:
pass
except Exception as err:
log.debug("App.quit_application() --> %s" % str(err))
# quit app by signalling for self.kill_app() method
self.close_app_signal.emit()

View File

@ -9,6 +9,10 @@ CAD program, and create G-Code for Isolation routing.
=================================================
1.04.2020
- updated the SVG parser to take into consideration the 'Close' svg element and paths that are made from a single line (we may need to switch to svgpathtools module)
30.03.2020
- working to update the Paint Tool

View File

@ -21,7 +21,7 @@
# import xml.etree.ElementTree as ET
from svg.path import Line, Arc, CubicBezier, QuadraticBezier, parse_path
from svg.path.path import Move
from svg.path.path import Move, Close
from shapely.geometry import LineString, LinearRing, MultiLineString
from shapely.affinity import skew, affine_transform, rotate
import numpy as np
@ -69,6 +69,7 @@ def path2shapely(path, object_type, res=1.0):
geometry = []
geo_element = None
rings = []
closed = False
for component in path:
# Line
@ -88,7 +89,8 @@ def path2shapely(path, object_type, res=1.0):
# How many points to use in the discrete representation.
length = component.length(res / 10.0)
steps = int(length / res + 0.5)
# steps = int(length / res + 0.5)
steps = int(length) * 2
# solve error when step is below 1,
# it may cause other problems, but LineString needs at least two points
@ -109,11 +111,29 @@ def path2shapely(path, object_type, res=1.0):
# Move
if isinstance(component, Move):
if not points:
continue
else:
rings.append(points)
if closed is False:
points = []
else:
closed = False
start = component.start
x, y = start.real, start.imag
points = [(x, y)]
continue
closed = False
# Close
if isinstance(component, Close):
if not points:
continue
else:
rings.append(points)
points = []
closed = True
continue
log.warning("I don't know what this is: %s" % str(component))
continue
@ -122,8 +142,12 @@ def path2shapely(path, object_type, res=1.0):
if points:
rings.append(points)
try:
rings = MultiLineString(rings)
except Exception as e:
log.debug("ParseSVG.path2shapely() MString --> %s" % str(e))
return None
rings = MultiLineString(rings)
if len(rings) > 0:
if len(rings) == 1 and not isinstance(rings, MultiLineString):
# Polygons are closed and require more than 2 points
@ -139,7 +163,10 @@ def path2shapely(path, object_type, res=1.0):
for line in rings:
coords.append(line.coords[0])
coords.append(line.coords[1])
geo_element = Polygon(coords)
try:
geo_element = Polygon(coords)
except Exception:
geo_element = LineString(coords)
geometry.append(geo_element)
return geometry