Skip to content

Commit b561835

Browse files
authored
Merge pull request #15 from Josef-MrBeam/mrbeam2-beta
merge mrbeam2-beta into beta
2 parents 79bba2b + df9ab6b commit b561835

File tree

5 files changed

+110
-58
lines changed

5 files changed

+110
-58
lines changed

octoprint_mrbeam/dependencies.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
iobeam==1.0.0a0
2-
mrb-hw-info==1.0.0a0
3-
mrbeam-ledstrips==1.0.0a0
4-
mrbeamdoc==1.0.0a0
1+
iobeam==1.0.0
2+
mrb-hw-info==1.0.0
3+
mrbeam-ledstrips==1.0.0
4+
mrbeamdoc==1.0.0

octoprint_mrbeam/iobeam/lid_handler.py

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363

6464
SIMILAR_PICS_BEFORE_REFRESH = 20
6565
MAX_PIC_THREAD_RETRIES = 2
66-
66+
CAMERA_SETTINGS_LAST_SESSION_YAML = "/home/pi/.octoprint/cam/last_session.yaml"
67+
CAMERA_SETTINGS_FALLBACK_JSON = "/home/pi/.octoprint/cam/last_markers.json"
6768

6869
from octoprint_mrbeam.iobeam.iobeam_handler import IoBeamEvents
6970
from octoprint.events import Events as OctoPrintEvents
@@ -1317,39 +1318,58 @@ def _createFolder_if_not_existing(self, filename):
13171318
makedirs(folder)
13181319
self._logger.debug("Created folder '%s' for camera images.", folder)
13191320

1320-
def load_camera_settings(self, path="/home/pi/.octoprint/cam/last_session.yaml"):
1321+
def load_camera_settings(self, path=CAMERA_SETTINGS_LAST_SESSION_YAML):
13211322
"""
1322-
Loads the settings saved from the last session.
1323-
The file is located by default at .octoprint/cam/pic_settings.yaml
1323+
Loads and returns the camera settings.
1324+
1325+
Args:
1326+
path (str): path to the camera settings Yaml to load from
1327+
1328+
Returns:
1329+
(list): ["calibMarkers", "shutter_speed"]
1330+
13241331
"""
1325-
backup = "/home/pi/.octoprint/cam/last_markers.json"
1326-
if os.path.isfile(path):
1327-
_path = path
1328-
else:
1329-
self._logger.info(
1330-
"last_session.yaml does not exist, using legacy backup (last_markers.json)"
1332+
1333+
# To Be used in case the default file not there or invalid
1334+
backup_path = CAMERA_SETTINGS_FALLBACK_JSON
1335+
camera_settings = []
1336+
1337+
# 1. Load from default
1338+
try:
1339+
with open(path) as f:
1340+
settings = yaml.safe_load(f)
1341+
for k in ["calibMarkers", "shutter_speed"]:
1342+
camera_settings.append(settings.get(k, None))
1343+
self._logger.debug("lid_handler: Default camera settings loaded from file: {}".format(path))
1344+
return camera_settings
1345+
except(IOError, OSError, yaml.YAMLError, yaml.reader.ReaderError, AttributeError) as e:
1346+
if os.path.isfile(path):
1347+
# An extra step to insure a smooth experience moving forward
1348+
os.remove(path)
1349+
self._logger.warn(
1350+
"lid_handler: File: {} does not exist or invalid, Will try to use legacy backup_path...: {}".format(
1351+
path, backup_path)
13311352
)
1332-
_path = backup
1353+
1354+
# 2. Load from Backup
13331355
try:
1334-
ret = []
1335-
with open(_path) as f:
1336-
settings = yaml.load(f) or {}
1337-
if _path == backup:
1338-
# No shutter speed info
1339-
settings = {k: v[-1] for k, v in settings.items()}
1340-
ret = [settings, None]
1341-
else:
1342-
for k in ["calibMarkers", "shutter_speed"]:
1343-
ret.append(settings.get(k, None))
1344-
return ret
1345-
except (IOError, OSError) as e:
1346-
self._logger.warning("New or Legacy marker memory not found.")
1347-
return [None] * 2
1356+
with open(backup_path) as f:
1357+
settings = yaml.safe_load(f)
1358+
# No shutter speed info present in this file
1359+
settings = {k: v[-1] for k, v in settings.items()}
1360+
camera_settings = [settings, None]
1361+
self._logger.debug("lid_handler: Fallback camera settings loaded from file: {}".format(backup_path))
1362+
return camera_settings
1363+
except(IOError, OSError, yaml.YAMLError, yaml.reader.ReaderError, AttributeError) as e:
1364+
self._logger.exception(
1365+
"lid_handler: File: {} does not exist or invalid, Camera Settings Load failed".format(backup_path)
1366+
)
1367+
return [None, None]
13481368

13491369
# @logme(True)
13501370
def save_camera_settings(
13511371
self,
1352-
path="/home/pi/.octoprint/cam/last_session.yaml",
1372+
path=CAMERA_SETTINGS_LAST_SESSION_YAML,
13531373
markers=None,
13541374
shutter_speed=None,
13551375
):
@@ -1371,10 +1391,10 @@ def save_camera_settings(
13711391
settings = {}
13721392
try:
13731393
with open(path) as f:
1374-
settings = yaml.load(f)
1375-
except (OSError, IOError) as e:
1394+
settings = yaml.safe_load(f)
1395+
except (OSError, IOError, yaml.YAMLError, yaml.reader.ReaderError, AttributeError) as e:
13761396
self._logger.warning(
1377-
"file %s does not exist or could not be read. Overwriting..." % path
1397+
"lid_handler: file %s does not exist or could not be read. Overwriting..." % path
13781398
)
13791399

13801400
settings = dict_merge(
@@ -1389,7 +1409,7 @@ def save_camera_settings(
13891409
try:
13901410
with open(path, "w") as f:
13911411
f.write(yaml.dump(settings))
1392-
except (OSError, IOError) as e:
1412+
except (OSError, IOError, yaml.YAMLError) as e:
13931413
self._logger.error(e)
13941414
except TypeError as e:
13951415
self._logger.warning(

octoprint_mrbeam/static/js/render_fills.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ Snap.plugin(function (Snap, Element, Paper, global) {
127127
let rasterEl = marked[i];
128128
let bbox;
129129
try {
130-
bbox = rasterEl.get_total_bbox();
131-
}
132-
catch(error) {
133-
console.warn(`Getting bounding box for ${rasterEl} failed.`, error);
130+
bbox = rasterEl.get_total_bbox();
131+
} catch (error) {
132+
console.warn(
133+
`Getting bounding box for ${rasterEl} failed.`,
134+
error
135+
);
134136
continue;
135137
}
136138
// find overlaps
@@ -172,13 +174,14 @@ Snap.plugin(function (Snap, Element, Paper, global) {
172174
rasterEl.addClass(`rasterCluster${c}`)
173175
);
174176
let tmpSvg = svg.clone();
175-
tmpSvg.selectAll(`.toRaster:not(.rasterCluster${c})`).forEach((element) => {
176-
let elementToBeRemoved = tmpSvg.select('#' + element.attr('id'));
177-
let elementsToBeExcluded = ["text", "tspan"]
178-
if (elementToBeRemoved && !elementsToBeExcluded.includes(elementToBeRemoved.type)) {
179-
elementToBeRemoved.remove();
180-
}
181-
});
177+
tmpSvg.selectAll(`.toRaster:not(.rasterCluster${c})`).remove();
178+
// tmpSvg.selectAll(`.toRaster:not(.rasterCluster${c})`).forEach((element) => {
179+
// let elementToBeRemoved = tmpSvg.select('#' + element.attr('id'));
180+
// let elementsToBeExcluded = ["text", "tspan"]
181+
// if (elementToBeRemoved && !elementsToBeExcluded.includes(elementToBeRemoved.type)) {
182+
// elementToBeRemoved.remove();
183+
// }
184+
// });
182185
// Fix IDs of filter references, those are not cloned correct (probably because reference is in style="..." definition)
183186
tmpSvg.fixIds("defs filter[mb\\:id]", "mb:id"); // namespace attribute selectors syntax: [ns\\:attrname]
184187
// DON'T fix IDs of textPath references, they're cloned correct.
@@ -335,10 +338,17 @@ Snap.plugin(function (Snap, Element, Paper, global) {
335338
// );
336339
}
337340

338-
// TODO only enlarge on images and fonts
339-
// Quick fix: in some browsers the bbox is too tight, so we just add an extra 10% to all the sides, making the height and width 20% larger in total
340-
const enlargement_x = 0.4; // percentage of the width added to each side
341-
const enlargement_y = 0.4; // percentage of the height added to each side
341+
// only enlarge on fonts, images not necessary.
342+
const doEnlargeBBox =
343+
elem.selectAll("text").filter((e) => {
344+
const bb = e.getBBox();
345+
// this filter is required, as every quick text creates an empty text element (for switching between curved and straight text)
346+
return bb.width > 0 && bb.height > 0;
347+
}).length > 0;
348+
349+
// Quick fix: in some browsers the bbox is too tight, so we just add an extra margin to all the sides, making the height and width larger in total
350+
const enlargement_x = doEnlargeBBox ? 0.4 : 0; // percentage of the width added to each side
351+
const enlargement_y = doEnlargeBBox ? 0.4 : 0; // percentage of the height added to each side
342352
const x1 = Math.max(0, bbox.x - bbox.width * enlargement_x);
343353
const x2 = Math.min(wMM, bbox.x2 + bbox.width * enlargement_x);
344354
const w = x2 - x1;

octoprint_mrbeam/static/js/snap_helpers.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,19 @@ Snap.plugin(function (Snap, Element, Paper, global) {
6666
const bb = el.getBBox();
6767
return Snap.path.getBBoxWithTransformation(bb, mat);
6868
};
69+
70+
/**
71+
* filter method for Snap Set like we know it from Array
72+
* @param f function which decides wether to keep or discard set elements
73+
* @returns {Set<any>} a new Snap set
74+
*/
75+
Snap.Set.prototype.filter = function (f) {
76+
const s = new Snap.Set();
77+
this.forEach((e) => {
78+
if (f(e)) {
79+
s.push(e);
80+
}
81+
});
82+
return s;
83+
};
6984
});

octoprint_mrbeam/static/js/working_area.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,16 +1201,23 @@ $(function () {
12011201
if (generator.generator === "inkscape") {
12021202
let isOldInkscapeVersion = NaN;
12031203
try {
1204-
isOldInkscapeVersion= window.compareVersions(
1205-
// 1.1.2 (1:1.1+202202050950+0a00cf5339) -> 1.1
1206-
generator.version.split('.').slice(0,2).join('.'),
1207-
"0.91"
1208-
) <= 0;
1209-
} catch(e) {
1204+
isOldInkscapeVersion =
1205+
window.compareVersions(
1206+
// 1.1.2 (1:1.1+202202050950+0a00cf5339) -> 1.1
1207+
generator.version
1208+
.split(".")
1209+
.slice(0, 2)
1210+
.join("."),
1211+
"0.91"
1212+
) <= 0;
1213+
} catch (e) {
12101214
let payload = {
12111215
error: e.message,
12121216
};
1213-
self._sendAnalytics("inkscape_version_comparison_error", payload);
1217+
self._sendAnalytics(
1218+
"inkscape_version_comparison_error",
1219+
payload
1220+
);
12141221
console.log("inkscape_version_comparison_error: ", e);
12151222
// In case the comparison fails, we assume the version to be above 0.91
12161223
// This assumption (the scaling) does not have a major impact as it has
@@ -1715,7 +1722,7 @@ $(function () {
17151722
} else if (
17161723
event.target.classList.contains("unit_percent")
17171724
) {
1718-
const newWidth =
1725+
let newWidth =
17191726
((currentWidth / Math.abs(currentSx)) * value) /
17201727
100.0;
17211728
if (Math.abs(newWidth) < 0.1)
@@ -1759,7 +1766,7 @@ $(function () {
17591766
} else if (
17601767
event.target.classList.contains("unit_percent")
17611768
) {
1762-
const newHeight =
1769+
let newHeight =
17631770
((currentHeight / Math.abs(currentSy)) * value) /
17641771
100.0;
17651772
if (Math.abs(newHeight) < 0.1)

0 commit comments

Comments
 (0)