Skip to content

Commit

Permalink
Merge pull request #27 from flux3dp/dev
Browse files Browse the repository at this point in the history
Release 0.8b14
  • Loading branch information
yen-von committed Mar 23, 2016
2 parents 099d6b8 + 6526cd8 commit d7e40fa
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 41 deletions.
2 changes: 1 addition & 1 deletion fluxclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ def check_pcl():
return False


__version__ = "0.8b13"
__version__ = "0.8b14"
SUPPORT_PCL = check_pcl()
13 changes: 10 additions & 3 deletions fluxclient/encryptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@


class KeyObject(object):
_size = None

@classmethod
def load_keyobj(cls, pem_or_der):
ref = RSA.importKey(pem_or_der)
Expand All @@ -31,7 +33,7 @@ def __init__(self, ref_keyobj):

def encrypt(self, message):
chip = PKCS1_OAEP.new(self._key)
size = ((self._key.size() + 1) // 8) - 42
size = self.size - 42
in_buf = BytesIO(message)
out_buf = BytesIO()

Expand All @@ -44,7 +46,7 @@ def encrypt(self, message):

def decrypt(self, buf):
chip = PKCS1_OAEP.new(self._key)
size = (self._key.size() + 1) // 8
size = self.size
in_buf = BytesIO(buf)
out_buf = BytesIO()

Expand All @@ -57,7 +59,12 @@ def decrypt(self, buf):

@property
def size(self):
return (self._key.size() + 1) // 8
if self._size is None:
s = self._key.size()
while s % 8 > 0:
s += 1
self._size = s // 8
return self._size

@property
def public_key_pem(self):
Expand Down
28 changes: 15 additions & 13 deletions fluxclient/laser/laser_bitmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,41 +48,43 @@ def gcode_generate(self, res=1):
t = np.vectorize(lambda x: x if x <= self.thres else 255)
self.image_map = t(self.image_map)

itera_o = list(range(0, len(self.image_map)))
itera_r = list(reversed(range(0, len(self.image_map))))
itera_o = list(range(0, len(self.image_map))) # iterate left to right
itera_r = list(reversed(range(0, len(self.image_map)))) # iterate right to left

#row iteration
for h in range(0, len(self.image_map)):
#column iteration
if h % res != 0:
continue
if self.one_way:

if self.one_way or h & 1 == 0:
itera = itera_o
# this 0.5 is for fixing tiny difference between iter from "left to right " and "right to left"
abs_shift_x = len(self.image_map) / 2 + 0.5
else:
if h & 1 == 0:
itera = itera_o
# this 0.5 is for fixing tiny difference between iter from "left to right " and "right to left"
abs_shift_x = len(self.image_map) / 2 + 0.5
elif h & 1 == 1:
itera = itera_r
abs_shift_x = len(self.image_map) / 2 - 0.5
elif h & 1 == 1:
itera = itera_r
abs_shift_x = len(self.image_map) / 2 - 0.5

final_x = itera[-1]

w = 0
back = True
while w < len(itera):
if w % res != 0:
continue
# record starting point and value for this pixel
# record starting point and the value of this pixel
w_record = w
this = self.image_map[h][itera[w]]
while w < len(itera) and self.image_map[h][itera[w]] == this:
w += 1
if this != 255:
if back:
back = False
gcode += self.moveTo(itera[w_record] - abs_shift_x, abs_shift - h, speed=8000)
# gcode += self.moveTo(itera[w_record] - abs_shift_x, abs_shift - h, speed=8000)
gcode += self.moveTo(itera[w_record] - abs_shift_x - 40, abs_shift - h, speed=5000)
gcode += self.turnOff()
gcode += self.moveTo(itera[w_record] - abs_shift_x, abs_shift - h)

else:
gcode += self.moveTo(itera[w_record] - abs_shift_x, abs_shift - h)
gcode += self.turnTo(255 - this)
Expand Down
2 changes: 1 addition & 1 deletion fluxclient/laser/laser_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def gcode_generate(self, names, ws=None):

progress += offset
if ws:
ws.send_progress('generating gcode on svg %d' % name_index, progress)
ws.send_progress('generating fcode on svg %d' % name_index, progress)
for each_path in path_data:
moveTo = True # flag that means extruder should move to rather than drawto
for x, y in each_path:
Expand Down
2 changes: 1 addition & 1 deletion fluxclient/printer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def float_or_percent(key, value, percent_start=float('-inf'), percent_end=float(
'bridge_fan_speed': [percentage],
'bridge_flow_ratio': False,
'bridge_speed': [int_range, 1, 150],
'brim_width': False,
'brim_width': [int_range, 0, 99],
'complete_objects': False,
'cooling': [binary],
'default_acceleration': False,
Expand Down
5 changes: 3 additions & 2 deletions fluxclient/printer/stl_slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def gcode_generate(self, names, ws, output_type):
else:
False, [error message]
"""
raise

# check if names are all seted
for n in names:
Expand Down Expand Up @@ -371,6 +372,7 @@ def begin_slicing(self, names, ws, output_type):
m_mesh = _printer.MeshObj(points, faces)
m_mesh.apply_transform(self.parameter[n])
m_mesh_merge.add_on(m_mesh)
m_mesh_merge = m_mesh_merge.cut(float(self.config['flux_floor']))

bounding_box = m_mesh_merge.bounding_box()
cx, cy = (bounding_box[0][0] + bounding_box[1][0]) / 2., (bounding_box[0][1] + bounding_box[1][1]) / 2.
Expand Down Expand Up @@ -545,8 +547,7 @@ def report_slicing(self):
"""
ret = []
if self.working_p:
l = len(self.working_p[-1][2])
for _ in range(l):
for _ in range(len(self.working_p[-1][2])):
message = self.working_p[-1][2][0]
self.working_p[-1][2].pop(0)
if type(message) == str:
Expand Down
10 changes: 7 additions & 3 deletions fluxclient/upnp/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ def __init__(self, uuid=None, ipaddr=DEFAULT_IPADDR, port=DEFAULT_PORT):
(h.sock for h in self.handlers if hasattr(h, "sock")))

def poke(self, ipaddr):
payload = struct.pack("<4sBB16s", b"FLUX", MULTICAST_VERSION, 0,
UUID(int=0).bytes)
self.touch_sock.sendto(payload, (ipaddr, DEFAULT_PORT))
# TODO
self.handlers[-1].poke(ipaddr)

def limited_uuid(self, uuid):
if self.uuid:
Expand Down Expand Up @@ -172,6 +171,11 @@ def __init__(self, server):
def fileno(self):
return self.sock.fileno()

def poke(self, ipaddr):
payload = struct.pack("<4sBB16s", b"FLUX", MULTICAST_VERSION, 0,
UUID(int=0).bytes)
self.sock.sendto(payload, (ipaddr, DEFAULT_PORT))

def handle_message(self, endpoint, action_id, payload):
if action_id == 0:
return self.handle_discover(endpoint, payload)
Expand Down
22 changes: 15 additions & 7 deletions fluxclient/utils/svg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def angle_between(v1, v2):
return [[]]

# there can be several set of parameter using same command ex: L12,34,56,78 -> L12,34 L56,78
# note that: M, m command is special case
# note that: M, m command is special case, M12,34,56,78 -> M12,34 L56,78
# https://www.w3.org/TR/SVG/paths.html#PathDataMovetoCommands

parameter_list = {'M': 2, 'm': 2, 'L': 2, 'l': 2, 'H': 1, 'h': 1, 'V': 1, 'v': 1, 'Z': 0, 'z': 0, 'C': 6, 'c': 6, 'S': 4, 's': 4, 'Q': 4, 'q': 4, 'T': 2, 't': 2, 'A': 7, 'a': 7}
Expand Down Expand Up @@ -649,7 +649,9 @@ def process(path_data, params, viewBox, radius):
# scale: go through and find x, y
# transform: rotate the points
w, h, x1_real, y1_real, x2_real, y2_real, rotation = params
dis = lambda x, y: (x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2
dis = lambda x, y: (x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2 # distance function
rotate_vector = lambda v, r: [(v[0] * cos(r) - v[1] * sin(r)), (v[0] * sin(r) + v[1] * cos(r))]
# put all points in viewBox
for path in range(len(path_data)):
new_path = []
for p in range(len(path_data[path]) - 1):
Expand Down Expand Up @@ -762,13 +764,17 @@ def process(path_data, params, viewBox, radius):
new_path = tmp_new_path

# transformation
vx = [w, 0]
vx = [(vx[0] * cos(rotation) - vx[1] * sin(rotation)), (vx[0] * sin(rotation) + vx[1] * cos(rotation))]
# reverse v_diagonal to find vx, vy as if no rotation
v_diagonal = [x2_real - x1_real, y2_real - y1_real]
v_diagonal = rotate_vector(v_diagonal, -rotation)
vx = [v_diagonal[0], 0]
vy = [0, v_diagonal[1]]

vy = [0, -h]
vy = [(vy[0] * cos(rotation) - vy[1] * sin(rotation)), (vy[0] * sin(rotation) + vy[1] * cos(rotation))]
# rotete vx, vy for rotation degree
vx = rotate_vector(vx, rotation)
vy = rotate_vector(vy, rotation)

# make points into real world coordinate
# make points into real world coordinate by mapping them with vector
for i in range(len(new_path)):
if new_path[i][0] != '\n':
new_path[i][0] -= viewBox[0]
Expand All @@ -782,6 +788,7 @@ def process(path_data, params, viewBox, radius):
new_path[i] = [x, y]
else:
pass

# make every points inside the boundary circle -> (cx, cy, r) = (0, 0, radius)
in_path = []
for i in range(1, len(new_path)):
Expand All @@ -808,6 +815,7 @@ def process(path_data, params, viewBox, radius):
x2, y2 = new_path[i]
if x1 == x2 and y1 == y2:
continue
# (-b +- sqrt(b^2-4ac)) / 2a
a = (x2 - x1) ** 2 + (y2 - y1) ** 2
b = 2 * ((x1 * x2) - (x1 ** 2) + (y1 * y2) - (y1 ** 2))
c = (x1 ** 2) + (y1 ** 2) - (radius ** 2)
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[aliases]
test=pytest
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3

from setuptools import setup, find_packages
import sys
from setuptools import setup

import setup_utils

Expand Down Expand Up @@ -38,6 +37,8 @@
test_suite="tests.main.everything",
entry_points=setup_utils.get_entry_points(),
install_requires=setup_utils.get_install_requires(),
setup_requires=['pytest-runner'],
tests_require=['pytest'],
cmdclass={'build_ext': setup_utils.build_ext},
ext_modules=ext_modules,
)
57 changes: 55 additions & 2 deletions tests/laser/test_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def svg_buf(request):
@pytest.fixture(scope="module")
def clean_svg_buf(request):
buf = open('tests/laser/data/Achtung.svg', 'rb').read()
data, w, h = SVGParser.preprocess(buf)
w, d = SVGParser.preprocess(buf)
data, w, h = d
return data


Expand Down Expand Up @@ -119,8 +120,60 @@ def test_polyline(self):
node = root.findall('*')[0]
coordinates = SVGParser.polyline(node)

def test_warning(self):
testcase = [[
"""<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 19.2.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 595.3 841.9" style="enable-background:new 0 0 595.3 841.9;" xml:space="preserve">
<text transform="matrix(1 0 0 1 139 286.9536)" class="st0 st1">Carpenter</text>
</svg>""", ['TEXT_TAG', 'EMPTY']],

["""<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 19.2.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 595.3 841.9" style="enable-background:new 0 0 595.3 841.9;" xml:space="preserve">
</svg>""", ['EMPTY']],
["""<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 19.2.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 595.3 841.9" style="enable-background:new 0 0 595.3 841.9;" xml:space="preserve">
<text transform="matrix(1 0 0 1 139 286.9536)" class="st0 st1">Carpenter</text>
<rect fill="none" width="60" height="60" x="0" y="0" stroke="#000" stroke-width="2" />
</svg>
""", ['TEXT_TAG']],
["""<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 19.2.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 595.3 841.9" style="enable-background:new 0 0 595.3 841.9;" xml:space="preserve">
<defs>
<g id="g1">
<rect id="rect1" width="100" height="50" x="10" y="10" fill="#c00"/>
<circle id="circle1" cx="30" cy="30" r="10" fill="#00c"/>
</g>
</defs>
</svg>
""", ['DEFS_TAG', 'EMPTY']],
["""<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 19.2.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 595.3 841.9" style="enable-background:new 0 0 595.3 841.9;" xml:space="preserve">
<clipPath id="b2">
<use x="0" y="0" width="200" height="200" xlink:href="#a1Shape" />
<use x="0" y="0" width="200" height="200" xlink:href="#a2Shape" />
</clipPath>
</svg>
""", ['CLIP_TAG', "EMPTY"]]
]

for s, w in testcase:
s = s.encode()
warning, d = SVGParser.preprocess(s)
assert set(warning) == set(w)

def test_preprocess(self, svg_buf):
data, w, h = SVGParser.preprocess(svg_buf)
warning, d = SVGParser.preprocess(svg_buf)
data, w, h = d

print(len(data), w, h)
assert w == 618.858
Expand Down
23 changes: 17 additions & 6 deletions tests/printer/test_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#!/usr/bin/env python3
import pytest
import sys
import os
from time import sleep

from fluxclient.printer.stl_slicer import StlSlicer
Expand Down Expand Up @@ -29,35 +30,45 @@ def test_read_stl(self, stl_binary):
StlSlicer.read_stl(stl_binary)

def test_upload(self, stl_binary):
_stl_slicer = StlSlicer('../Slic3r/slic3r.pl')
if not 'slic3r' in os.environ:
os.environ['slic3r'] = '../Slic3r/slic3r.pl'
_stl_slicer = StlSlicer(os.environ['slic3r'])
_stl_slicer.upload('tmp', stl_binary)

def test_duplicate(self, stl_binary):
_stl_slicer = StlSlicer('../Slic3r/slic3r.pl')
if not 'slic3r' in os.environ:
os.environ['slic3r'] = '../Slic3r/slic3r.pl'
_stl_slicer = StlSlicer(os.environ['slic3r'])
_stl_slicer.upload('tmp', stl_binary)
_stl_slicer.duplicate('tmp', 'tmp2')

def test_upload_image(self, img_buf):
_stl_slicer = StlSlicer('../Slic3r/slic3r.pl')
if not 'slic3r' in os.environ:
os.environ['slic3r'] = '../Slic3r/slic3r.pl'
_stl_slicer = StlSlicer(os.environ['slic3r'])
_stl_slicer.upload_image(img_buf)

def test_delete(self, stl_binary):
_stl_slicer = StlSlicer('../Slic3r/slic3r.pl')
if not 'slic3r' in os.environ:
os.environ['slic3r'] = '../Slic3r/slic3r.pl'
_stl_slicer = StlSlicer(os.environ['slic3r'])
_stl_slicer.upload('tmp', stl_binary)
a, b = _stl_slicer.delete('tmp')
assert a is True
a, b = _stl_slicer.delete('tmp')
assert a is False

def test_slicing(self, stl_binary):
_stl_slicer = StlSlicer('../Slic3r/slic3r.pl')
if not 'slic3r' in os.environ:
os.environ['slic3r'] = '../Slic3r/slic3r.pl'
_stl_slicer = StlSlicer(os.environ['slic3r'])
_stl_slicer.upload('tmp', stl_binary)
_stl_slicer.set('tmp', [0, 0, 4.5, 0, 0, 0, 1, 1, 1])
_stl_slicer.begin_slicing(['tmp'], None, '-f')
sleep(1)
while True:
a = _stl_slicer.report_slicing()
if a[-1].startswith('{"status": "complete"'):
if a and a[-1].startswith('{"status": "complete"'):
break
sleep(0.5)

Expand Down
Loading

0 comments on commit d7e40fa

Please sign in to comment.