Compare commits
11
Commits
d34118520e
..
rofl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5bf9463085 | ||
|
|
10e1f91a2f | ||
|
|
81d3a5e6f0 | ||
|
|
d212fba311 | ||
|
|
02e44ac912 | ||
|
|
0a74e7b39f | ||
|
|
47be43b1d9 | ||
|
|
ccaea45ab2 | ||
|
|
29d89025cb | ||
|
|
1ae3241171 | ||
|
|
99d43d5691 |
No files matched your search
@@ -1,28 +1,27 @@
|
||||
## SVG to Gcode converter for GRBL pen plotter
|
||||
Forked from vishpat/svg2gcode
|
||||
|
||||
Forked from [vishpat/svg2gcode](https://github.com/vishpat/svg2gcode)
|
||||
https://github.com/vishpat/svg2gcode
|
||||
|
||||
I found that the plugins for inkscape and illustrator would crash when I tried to convert SVG's with many thousands of lines to gcode.
|
||||
|
||||
I found that the plugins for inkscape and illustrator would crash when I tried to convert SVG's with many thousands of lines to gcode.
|
||||
So I modified this to generate gcode for GRBL based pen plotter
|
||||
|
||||
**Usage as cli:**
|
||||
|
||||
>python convert.py source.svg destination.gcode
|
||||
_python convert.py source.svg destination.gcode_
|
||||
|
||||
**or in bash**
|
||||
|
||||
copy 's2g' to your path and run from anywhere
|
||||
>s2g source.svg target.gcode
|
||||
_copy 's2g' to your path and run from anywhere_
|
||||
|
||||
**There is a brute force path optimisator algorithm. Very slow for large files, can be disabled in config.py**
|
||||
**There is a brute force path optimisator algarithm. Very slow for large files, can be disabled in config.py**
|
||||
|
||||
**Illustrator exports in points, not mm** regardless of your document settings.
|
||||
If units are set to points in the config file, we convert to mm on the fly.
|
||||
**illustrator exports svg's in points** regardless of your document settings
|
||||
if units are set to points in the config file, we convert to mm on the fly
|
||||
|
||||
There are various settings you can change in config.py. Most notably, you must set an upper and lower position for the z axis.
|
||||
|
||||
* zTravel is the height it will move to before traveling to the next stroke
|
||||
* zDraw should be the height at which the pen will make contact with the paper
|
||||
* zDraw should be the height at which the pen will make contact with the paper
|
||||
zTravel is the height it will move to before traveling to the next stroke
|
||||
|
||||
zDraw should be the height at which the pen will make contact with the paper
|
||||
|
||||
+213
-3
@@ -1,7 +1,7 @@
|
||||
from math import sqrt
|
||||
from datetime import datetime as dt
|
||||
from utils import *
|
||||
|
||||
import json
|
||||
|
||||
def get_distance(a, b, sq=False):
|
||||
x1, y1 = a[0], a[1]
|
||||
@@ -33,8 +33,13 @@ def optimise_path(shapes, sq=False):
|
||||
new_order = []
|
||||
new_order.append(shapes.pop(0))
|
||||
l = len(shapes)
|
||||
lp = 0
|
||||
print(lp)
|
||||
while len(new_order) <= l:
|
||||
|
||||
p = round((1-(len(shapes) / l))*100)
|
||||
if not p == lp:
|
||||
print(p)
|
||||
lp = p
|
||||
shortest = float("Inf")
|
||||
last = new_order[-1][-1]
|
||||
|
||||
@@ -54,11 +59,216 @@ def optimise_path(shapes, sq=False):
|
||||
selection = shape
|
||||
|
||||
if reverse:
|
||||
new_order.append([x for x in reversed(selection)])
|
||||
new_order.append(list(reversed(selection)))
|
||||
#new_order.append([x for x in reversed(selection)])
|
||||
|
||||
else:
|
||||
new_order.append(selection)
|
||||
shapes.remove(selection)
|
||||
|
||||
timer(t1, "optimizing ")
|
||||
|
||||
|
||||
|
||||
return new_order
|
||||
|
||||
|
||||
|
||||
def test_edges(shapes):
|
||||
|
||||
m = get_min_max(shapes)
|
||||
xMin = m[0]
|
||||
yMin = m[1]
|
||||
xMax = m[2]
|
||||
yMax = m[3]
|
||||
|
||||
|
||||
boundry = [ [xMin, yMin], [xMin, yMax], [xMax, yMax], [xMax, yMin], xMin, yMin]
|
||||
return boundry
|
||||
|
||||
# def scale_shapes(shapes, x_scale, y_scale):
|
||||
#
|
||||
# for i in shapes:
|
||||
# for j in i:
|
||||
# shapes[i][j][0] = shapes[i][j][0] * x_scale
|
||||
# shapes[i][j][1] = shapes[i][j][1] * y_scale
|
||||
#
|
||||
# return shapes
|
||||
|
||||
# def lerp(old_min, new_min, old_max, new_max, value):
|
||||
#
|
||||
# OldRange = (old_max - old_min)
|
||||
# NewRange = (new_max - new_min)
|
||||
# return (((value - old_min) * NewRange) / OldRange) + new_min
|
||||
|
||||
def get_min_max(shapes):
|
||||
|
||||
xMin, yMin = float("inf"), float("inf")
|
||||
xMax, yMax = 0, 0
|
||||
|
||||
for i in shapes:
|
||||
for j in i:
|
||||
xMin = min(xMin, j[0])
|
||||
xMax = max(xMax, j[0])
|
||||
yMin = min(yMin, j[1])
|
||||
yMax = max(yMax, j[1])
|
||||
|
||||
print(f"min is {xMin}, {yMin}")
|
||||
print(f"max is {xMax}, {yMax}")
|
||||
|
||||
return (xMin, yMin), (xMax, yMax)
|
||||
|
||||
def auto_scale(shapes,bed_x, bed_y):
|
||||
|
||||
|
||||
def scale(old_min_xy, old_max_xy, bed_xy, value_xy):
|
||||
|
||||
offset_x = old_min_xy[0]
|
||||
offset_y = old_min_xy[1]
|
||||
|
||||
old_max_offsetted_x = old_max_xy[0] - offset_x
|
||||
old_max_offsetted_y = old_max_xy[1] - offset_y
|
||||
|
||||
scale_x = bed_xy[0] / old_max_offsetted_x
|
||||
scale_y = bed_xy[1] / old_max_offsetted_y
|
||||
scale = min(scale_x, scale_y)
|
||||
|
||||
x = value_xy[0] - offset_x
|
||||
y = value_xy[1] - offset_y
|
||||
|
||||
x *= scale
|
||||
y *= scale
|
||||
|
||||
return x, y
|
||||
|
||||
|
||||
min_max = get_min_max(shapes)
|
||||
|
||||
for x, i in enumerate(shapes):
|
||||
for y, j in enumerate(i):
|
||||
shapes[x][y] = scale(min_max[0], min_max[1], (bed_x, bed_y), shapes[x][y])
|
||||
|
||||
return shapes
|
||||
#
|
||||
#
|
||||
# min_max = get_min_max(shapes)
|
||||
# old_min = min(min_max[0], min_max[1])
|
||||
# old_max = max(min_max[2], min_max[3])
|
||||
# new_min = 0
|
||||
# new_max = max(bed_x, bed_y)
|
||||
#
|
||||
# print(f"old min {old_min}")
|
||||
# print(f"old max {old_max}")
|
||||
# print(f"new_min {new_min}")
|
||||
# print(f"new_max {new_max}")
|
||||
#
|
||||
# # print(f"")
|
||||
#
|
||||
# # print(shapes)
|
||||
# for x, i in enumerate(shapes):
|
||||
#
|
||||
# for y, j in enumerate(i):
|
||||
# for z, k in enumerate(j):
|
||||
# this = shapes[x][y][z]
|
||||
# this_lerp = lerp(old_min, new_min, old_max, new_max, this)
|
||||
# # print(f"{this:03} --> {this_lerp:03}")
|
||||
# shapes[x][y][z] = this_lerp
|
||||
#
|
||||
# return shapes
|
||||
|
||||
def concatenate(shapes, threshold):
|
||||
print("concat")
|
||||
new_shapes = []
|
||||
new_shape = []
|
||||
for x, i in enumerate(shapes):
|
||||
|
||||
current_start = shapes[x][0]
|
||||
previous_end = shapes[x-1][-1]
|
||||
current_shape = i
|
||||
|
||||
if len(new_shape) == 0:
|
||||
new_shape = current_shape
|
||||
|
||||
|
||||
if get_distance(current_start, previous_end, True) < threshold:
|
||||
new_shape += current_shape
|
||||
|
||||
else:
|
||||
new_shapes.append(new_shape)
|
||||
new_shape = []
|
||||
|
||||
print(f"{len(shapes)} became {len(new_shapes)} shapes")
|
||||
|
||||
return new_shapes
|
||||
|
||||
def compare_lines(line1, line2, threshold): #return true if lines are too similar
|
||||
|
||||
count = 0
|
||||
|
||||
def check(list): #return true if all values are below threshold
|
||||
|
||||
for i in list:
|
||||
# print(abs(i))
|
||||
if abs(i) >= threshold:
|
||||
# print(abs(i))
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
l1_x1 = line1[0][0]
|
||||
l1_y1 = line1[0][1]
|
||||
l1_x2 = line1[1][0]
|
||||
l1_y2 = line1[1][1]
|
||||
|
||||
l2_x1 = line2[0][0]
|
||||
l2_y1 = line2[0][1]
|
||||
l2_x2 = line2[1][0]
|
||||
l2_y2 = line2[1][1]
|
||||
|
||||
d1 = l1_x1 - l2_x1
|
||||
d2 = l1_y1 - l2_y1
|
||||
d3 = l1_x2 - l2_x2
|
||||
d4 = l1_y2 - l2_y2
|
||||
|
||||
d5 = l1_x1 - l2_x2
|
||||
d6 = l1_y1 - l2_y2
|
||||
d7 = l1_x2 - l2_x1
|
||||
d8 = l1_x2 - l2_y2
|
||||
|
||||
c1 = check([d1, d2, d3, d4])
|
||||
c2 = check([d5, d6, d7, d8])
|
||||
|
||||
if c1 or c2:
|
||||
return True
|
||||
count += 1
|
||||
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def remove_redundant_lines(shapes, threshold):
|
||||
count = 0
|
||||
first = True
|
||||
prev_points = shapes[0][0]
|
||||
prev_line = ()
|
||||
for shape in shapes:
|
||||
for points in shape:
|
||||
|
||||
if first:
|
||||
first = False
|
||||
continue
|
||||
|
||||
line = (prev_points, points)
|
||||
|
||||
if not prev_line:
|
||||
|
||||
prev_line = line
|
||||
continue
|
||||
if compare_lines(prev_line, line, threshold):
|
||||
count += 1
|
||||
prev_points = points
|
||||
prev_line = line
|
||||
print(f"redundant lines: {count}")
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
from optimise import optimise_path, auto_scale, concatenate, remove_redundant_lines
|
||||
from svg2gcode import get_shapes, shapes_2_gcode
|
||||
from os.path import join
|
||||
from json import dump as jdump, load as jload
|
||||
from utils import *
|
||||
|
||||
# def write(output_file_name, data):
|
||||
# with open(output_file_name, 'w+') as output_file:
|
||||
# output_file.write(data)
|
||||
|
||||
def dump(outfile, data):
|
||||
with open(outfile, 'w+') as o:
|
||||
jdump(data, o)
|
||||
|
||||
|
||||
def read(infile):
|
||||
with open(infile) as i:
|
||||
return jload(i)
|
||||
|
||||
proj_location = "/bish/projects/plots/noiseField"
|
||||
svg_location = join(proj_location, "svg")
|
||||
json_location = join(proj_location, "json")
|
||||
gcode_location = join(proj_location, "gcode")
|
||||
#
|
||||
# svg_file = "test_shapes.svg"
|
||||
svg_file = "1.svg"
|
||||
# svg_file = "square.svg"
|
||||
|
||||
svg_path = join(svg_location, svg_file)
|
||||
file_name = svg_file.split(".")[0]
|
||||
|
||||
shapes_file = f"{file_name}_shapes.json"
|
||||
shapes_path = join(json_location, shapes_file)
|
||||
|
||||
optimised_shapes_file = f"{file_name}_optimised.json"
|
||||
optimised_shapes_path = join(json_location, optimised_shapes_file)
|
||||
|
||||
concatenated_shapes_file = f"{file_name}_optimised.json"
|
||||
concatenated_shapes_path = join(json_location, concatenated_shapes_file)
|
||||
|
||||
scaled_shapes_file = f"{file_name}_scaled.json"
|
||||
scaled_shapes_path = join(json_location, scaled_shapes_file)
|
||||
|
||||
version = 9
|
||||
|
||||
gcode_output_file = f"{file_name}_v{version}.gcode"
|
||||
gcode_output_path = join(gcode_location, gcode_output_file)
|
||||
|
||||
print(proj_location)
|
||||
print(svg_location)
|
||||
print(json_location)
|
||||
print(gcode_location)
|
||||
print(svg_path)
|
||||
print(shapes_path)
|
||||
print(optimised_shapes_path)
|
||||
print(scaled_shapes_path)
|
||||
print(concatenated_shapes_path)
|
||||
print(gcode_output_path)
|
||||
|
||||
# auto_scale = False
|
||||
#
|
||||
# shapes = get_shapes(svg_path) #parse svg
|
||||
# dump(shapes_path, shapes) #dump parsed svg to json
|
||||
current = read(shapes_path)
|
||||
|
||||
# tempCurrent = []
|
||||
# for x,i in enumerate(current):
|
||||
# if x%10 == 0:
|
||||
# tempCurrent.append(i)
|
||||
# current = tempCurrent
|
||||
#
|
||||
# new_order = optimise_path(current) #optimise json shapes
|
||||
# dump(optimised_shapes_path, new_order) #dunmp optimised shapes
|
||||
# current = read(optimised_shapes_path)
|
||||
|
||||
# jnew_order = read(optimised_shapes_path) #read optimised shapes
|
||||
#
|
||||
|
||||
#flip xy
|
||||
# for a,i in enumerate(current):
|
||||
# for b,j in enumerate(i):
|
||||
#
|
||||
# c = current[a][b]
|
||||
# c.reverse()
|
||||
# current[a][b] = c
|
||||
# print(current[a][b])
|
||||
|
||||
|
||||
scaled_shapes = auto_scale(current, 205, 268)
|
||||
|
||||
new_maxX = 0
|
||||
new_maxY = 0
|
||||
for i in scaled_shapes:
|
||||
new_maxX = max(new_maxX, max(i[0][0], i[-1][0]))
|
||||
new_maxY = max(new_maxY, max(i[0][-1], i[-1][-1]))
|
||||
print("new max!!!!!!")
|
||||
print(new_maxX, new_maxY)
|
||||
current = scaled_shapes
|
||||
|
||||
# concat_shapes = concatenate(current, 0.35)
|
||||
# current = concat_shapes
|
||||
|
||||
# remove_reundant = remove_redundant_lines(current, 0.4)
|
||||
#
|
||||
commands = shapes_2_gcode(current, test_boundries=False) #generate gcode commands
|
||||
|
||||
|
||||
write_gcode(gcode_output_path, commands) #write gcode file
|
||||
+23
-19
@@ -7,23 +7,21 @@ from shapes import point_generator
|
||||
from config import *
|
||||
import re
|
||||
from datetime import datetime as dt
|
||||
from optimise import optimise_path, get_total_distance
|
||||
from optimise import optimise_path, get_total_distance, test_edges
|
||||
from utils import *
|
||||
|
||||
debug = False
|
||||
|
||||
# todo fix total distance algorithm
|
||||
# todo can we guess the units?
|
||||
# todo add manual scale option
|
||||
# todo add z rise option
|
||||
# todo why do i need to flip?
|
||||
|
||||
def get_shapes(path, auto_scale=True):
|
||||
def get_shapes(svg_file, auto_scale=False):
|
||||
|
||||
t1 = dt.now()
|
||||
svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path'])
|
||||
shapes = []
|
||||
tree = ET.parse(path)
|
||||
tree = ET.parse(svg_file)
|
||||
root = tree.getroot()
|
||||
|
||||
pointRatio = 0.352778
|
||||
@@ -40,12 +38,19 @@ def get_shapes(path, auto_scale=True):
|
||||
print("Unable to get width and height for the svg")
|
||||
sys.exit(1)
|
||||
|
||||
# width = float(re.sub("[^0-9]", "", width))
|
||||
# height = float(re.sub("[^0-9]", "", height))
|
||||
width = float(re.sub("[^0-9]", "", width))
|
||||
height = float(re.sub("[^0-9]", "", height))
|
||||
|
||||
width = float(re.findall(r"[-+]?\d*\.\d+|\d+", width)[0])
|
||||
height = float(re.findall(r"[-+]?\d*\.\d+|\d+", height)[0])
|
||||
print("width / height ", width, height)
|
||||
# if "px" in width:
|
||||
# print("px")
|
||||
# width.replace("px", "")q
|
||||
#
|
||||
# if "px" in height:
|
||||
# print("px")
|
||||
# height.replace("px", "")
|
||||
|
||||
width = float(width)
|
||||
height = float(height)
|
||||
|
||||
if units == "points":
|
||||
width *= pointRatio
|
||||
@@ -127,7 +132,7 @@ def g_string(x, y, z=False, prefix="G1", p=3):
|
||||
return f"{prefix} X{x:.{p}f} Y{y:.{p}f}"
|
||||
|
||||
|
||||
def shapes_2_gcode(shapes):
|
||||
def shapes_2_gcode(shapes, test_boundries = False):
|
||||
|
||||
t1 = dt.now()
|
||||
with open("header.txt") as h:
|
||||
@@ -135,6 +140,12 @@ def shapes_2_gcode(shapes):
|
||||
|
||||
commands = [f"{header}", f'F{feed_rate}']
|
||||
|
||||
if test_boundries:
|
||||
|
||||
# commands.append("(boundry check)")
|
||||
for i in test_edges(shapes):
|
||||
commands.append(g_string(i[0],i[1], zTravel, "G0"))
|
||||
|
||||
for i in shapes:
|
||||
commands += ['', shape_preamble, ""]
|
||||
|
||||
@@ -155,13 +166,7 @@ def shapes_2_gcode(shapes):
|
||||
timer(t1, "shapes_2_gcode ")
|
||||
return commands
|
||||
|
||||
def write_file(output, commands):
|
||||
|
||||
t1 = dt.now()
|
||||
with open(output, 'w+') as output_file:
|
||||
for i in commands:
|
||||
output_file.write(i + "\n")
|
||||
timer(t1, "writing file ")
|
||||
|
||||
def main(file_path, output):
|
||||
|
||||
@@ -191,7 +196,7 @@ def main(file_path, output):
|
||||
|
||||
commands = shapes_2_gcode(shapes)
|
||||
|
||||
write_file(output, commands)
|
||||
write_gcode(output, commands)
|
||||
|
||||
print("done")
|
||||
|
||||
@@ -202,7 +207,6 @@ if __name__ == "__main__":
|
||||
# file_path = "./svg/medium_example.svg"
|
||||
file_path = "./svg/text.svg"
|
||||
# file_path = "./svg/example.svg"
|
||||
# file_path = "./svg/lines.svg"
|
||||
|
||||
output = "./gcode_optimised/1.gcode"
|
||||
|
||||
|
||||
@@ -4,4 +4,13 @@ def timer(t, label):
|
||||
duration = dt.now() - t
|
||||
duration = duration.total_seconds()
|
||||
print("{} took {}".format(label, duration))
|
||||
return duration
|
||||
return duration
|
||||
|
||||
def write_gcode(output_file_name, data):
|
||||
|
||||
t1 = dt.now()
|
||||
with open(output_file_name, 'w+') as output_file:
|
||||
for i in data:
|
||||
output_file.write(i + "\n")
|
||||
print(f"writing to {output_file_name}")
|
||||
timer(t1, "writing file ")
|
||||
Reference in new issue
Block a user