Skip to content

Commit

Permalink
Merge pull request #38 from EmbroidePy/tatarize-gcode
Browse files Browse the repository at this point in the history
+Gcode, reader and writer.
  • Loading branch information
tatarize authored Oct 29, 2018
2 parents f30dc57 + 60afd80 commit 7096f5c
Show file tree
Hide file tree
Showing 9 changed files with 484 additions and 93 deletions.
197 changes: 116 additions & 81 deletions pyembroidery/CsvWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ def angle(dx, dy):
return angle


def write(pattern, f, settings=None):
def write_data(pattern, f):
names = get_common_name_dictionary()

deltas = settings is not None and "deltas" in settings
displacement = settings is not None and "displacement" in settings

extends = pattern.bounds()
width = extends[2] - extends[0]
height = extends[3] - extends[1]
Expand Down Expand Up @@ -79,6 +75,8 @@ def write(pattern, f, settings=None):

write_string_utf8(f, "\n")


def write_metadata(pattern, f):
if len(pattern.extras) > 0:
csv(f, (
'#',
Expand All @@ -95,6 +93,8 @@ def write(pattern, f, settings=None):
))
write_string_utf8(f, "\n")


def write_threads(pattern, f):
if len(pattern.threadlist) > 0:
csv(f, (
'#',
Expand All @@ -119,83 +119,118 @@ def write(pattern, f, settings=None):
))
write_string_utf8(f, "\n")


def decoded_name(names, data):
command = decode_embroidery_command(data)
try:
name = names[command[0]]
if command[1] is not None:
name = name + " t" + str(command[1])
if command[2] is not None:
name = name + " n" + str(command[2])
if command[3] is not None:
name = name + " o" + str(command[3])
except (IndexError, KeyError):
name = "UNKNOWN " + str(data)
return name


def write_stitches_displacement(pattern, f):
names = get_common_name_dictionary()
csv(f, (
'#',
'[STITCH_INDEX]',
'[STITCH_TYPE]',
'[X]',
'[Y]',
'[DX]',
'[DY]',
'[R]',
'[ANGLE]'
))

current_x = 0
current_y = 0
for i, stitch in enumerate(pattern.stitches):
name = decoded_name(names, stitch[2])
dx = stitch[0] - current_x
dy = stitch[1] - current_y
csv(f, (
'*',
str(i),
name,
str(stitch[0]),
str(stitch[1]),
str(dx),
str(dy),
str(distance(dx, dy)),
str(angle(dx, dy))
))
current_x = stitch[0]
current_y = stitch[1]


def write_stitches_deltas(pattern, f):
names = get_common_name_dictionary()
csv(f, (
'#',
'[STITCH_INDEX]',
'[STITCH_TYPE]',
'[X]',
'[Y]',
'[DX]',
'[DY]'
))
current_x = 0
current_y = 0
for i, stitch in enumerate(pattern.stitches):
name = decoded_name(names, stitch[2])
dx = stitch[0] - current_x
dy = stitch[1] - current_y
csv(f, (
'*',
str(i),
name,
str(stitch[0]),
str(stitch[1]),
str(dx),
str(dy)
))
current_x = stitch[0]
current_y = stitch[1]


def write_stitches(pattern, f):
names = get_common_name_dictionary()
csv(f, (
'#',
'[STITCH_INDEX]',
'[STITCH_TYPE]',
'[X]',
'[Y]'
))
for i, stitch in enumerate(pattern.stitches):
name = decoded_name(names, stitch[2])
csv(f, (
'*',
str(i),
name,
str(stitch[0]),
str(stitch[1]),
))


def write(pattern, f, settings=None):
deltas = settings is not None and "deltas" in settings
displacement = settings is not None and "displacement" in settings
write_data(pattern, f)
write_metadata(pattern, f)
write_threads(pattern, f)

if len(pattern.stitches) > 0:
if displacement:
csv(f, (
'#',
'[STITCH_INDEX]',
'[STITCH_TYPE]',
'[X]',
'[Y]',
'[DX]',
'[R]',
'[ANGLE]'
))
write_stitches_displacement(pattern, f)
elif deltas:
csv(f, (
'#',
'[STITCH_INDEX]',
'[STITCH_TYPE]',
'[X]',
'[Y]',
'[DX]',
'[DY]'
))
write_stitches_deltas(pattern, f)
else:
csv(f, (
'#',
'[STITCH_INDEX]',
'[STITCH_TYPE]',
'[X]',
'[Y]'
))
current_x = 0
current_y = 0
for i, stitch in enumerate(pattern.stitches):
command = decode_embroidery_command(stitch[2])
try:
name = names[command[0]]
if command[1] is not None:
name = name + " t" + str(command[1])
if command[2] is not None:
name = name + " n" + str(command[2])
if command[3] is not None:
name = name + " o" + str(command[3])
except (IndexError, KeyError):
name = "UNKNOWN " + str(stitch[2])
if displacement:
dx = stitch[0] - current_x
dy = stitch[1] - current_y
csv(f, (
'*',
str(i),
name,
str(stitch[0]),
str(stitch[1]),
str(dx),
str(dy),
str(distance(dx, dy)),
str(angle(dx, dy))
))
elif deltas:
dx = stitch[0] - current_x
dy = stitch[1] - current_y
csv(f, (
'*',
str(i),
name,
str(stitch[0]),
str(stitch[1]),
str(dx),
str(dy)
))
else:
csv(f, (
'*',
str(i),
name,
str(stitch[0]),
str(stitch[1]),
))
current_x = stitch[0]
current_y = stitch[1]
write_stitches(pattern, f)
79 changes: 79 additions & 0 deletions pyembroidery/GcodeReader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
def parse(f):
comment = None
code = ""
value = ""
command_map = {}
ord_a = ord('a')
ord_A = ord('A')
ord_z = ord('z')
ord_Z = ord('Z')
while True:
byte = f.read(1)
if byte is None:
break
if len(byte) == 0:
break
if comment is not None:
if byte == b')':
command_map['comment'] = comment
comment = None
else:
comment += str(byte)
continue
if byte == b'(':
comment = ""
continue
elif byte == b'\t':
continue
elif byte == b' ':
continue
elif byte == b'/' and len(code) == 0:
continue
b = ord(byte)
if ord('0') <= b <= ord('9') \
or byte == b'+' \
or byte == b'-' \
or byte == b'.':
value += byte.decode('utf8')
continue

if ord_A <= b <= ord_Z:
b = b - ord_A + ord_a
byte = chr(b)
is_letter = ord_a <= b <= ord_z
is_end = byte == b'\n' or byte == b'\r'
if (is_letter or is_end) and len(code) != 0:
command_map[code] = float(value)
code = ""
value = ""
if is_letter:
code += str(byte)
continue
elif is_end:
if len(command_map) == 0:
continue
yield command_map
command_map = {}
code = ""
value = ""
continue


def read(f, out, settings=None):
scale = -10.0
for gc in parse(f):
if 'comment' in gc:
comment = gc['comment']
if 'Thread' in comment:
split = comment.split(" ")
out.add_thread(split[1])

if 'g' in gc and 'x' in gc and 'y' in gc:
if gc['g'] == 0.0 or gc['g'] == 1.0:
out.stitch_abs(gc['x'] * scale, gc['y'] * scale)
if 'm' in gc:
v = gc['m']
if v == 30 or v == 2:
out.end()
elif v == 0 or v == 1:
out.color_change()
Loading

0 comments on commit 7096f5c

Please sign in to comment.