fixed profile editing

This commit is contained in:
Teja 2015-01-26 13:31:07 +01:00
parent 040035b7ff
commit 8237d3c98f
6 changed files with 53 additions and 75 deletions

View file

@ -24,9 +24,12 @@ import copy
from octoprint.server.util.flask import restricted_access
from flask import Blueprint, request, jsonify, abort, current_app, session, make_response, url_for
default_settings = {
"current_profile_id": "_mrbeam_junior"
}
s = octoprint.plugin.plugin_settings("lasercutterprofiles", defaults=default_settings)
blueprint = flask.Blueprint("plugin.lasercutterprofiles", __name__)
laserCutterProfileManager = LaserCutterProfileManager()
laserCutterProfileManager = LaserCutterProfileManager(s)
@blueprint.route("/profiles", methods=["GET"])
def laserCutterProfilesList():
@ -73,8 +76,8 @@ def laserCutterProfilesAdd():
return make_response("Profile is invalid", 400)
except CouldNotOverwriteError:
return make_response("Profile already exists and overwriting was not allowed", 400)
except Exception as e:
return make_response("Could not save profile: %s" % e.message, 500)
#except Exception as e:
# return make_response("Could not save profile: %s" % e.message, 500)
else:
return jsonify(dict(profile=_convert_profile(saved_profile)))
@ -126,8 +129,8 @@ def laserCutterProfilesUpdate(identifier):
return make_response("Profile is invalid", 400)
except CouldNotOverwriteError:
return make_response("Profile already exists and overwriting was not allowed", 400)
except Exception as e:
return make_response("Could not save profile: %s" % e.message, 500)
#except Exception as e:
# return make_response("Could not save profile: %s" % e.message, 500)
else:
return jsonify(dict(profile=_convert_profile(saved_profile)))
@ -149,13 +152,6 @@ def _convert_profile(profile):
default_settings = {
"zAxis": False,
"working_area_width": 216,
"working_area_height": 297
}
s = octoprint.plugin.plugin_settings("lasercutterprofiles", defaults=default_settings)
class LaserCutterProfilesPlugin(octoprint.plugin.SettingsPlugin,
octoprint.plugin.StartupPlugin,
@ -184,20 +180,21 @@ class LaserCutterProfilesPlugin(octoprint.plugin.SettingsPlugin,
##~~ SettingsPlugin API
def on_settings_load(self):
return dict(
workingAreaHeight=s.get(["workingAreaHeight"]),
workingAreaWidth=s.get(["workingAreaWidth"]),
zAxis=s.getBoolean(["zAxis"])
cfg = dict(
current_profile_id=s.get(["current_profile_id"]),
)
print("on_settings_load", cfg)
return cfg
def on_settings_save(self, data):
if "workingAreaHeight" in data and data["workingAreaHeight"]:
s.set(["workingAreaHeight"], data["workingAreaHeight"])
if "workingAreaWidth" in data and data["workingAreaWidth"]:
s.set(["workingAreaWidth"], data["workingAreaWidth"])
if "zAxis" in data:
zAxis = data["zAxis"] in octoprint.settings.valid_boolean_trues
s.setBoolean(["zAxis"], zAxis)
selectedProfile = laserCutterProfileManager.get_current_or_default()
print("on_settings_save", selectedProfile)
s.set(["current_profile_id"], selectedProfile['id'])
##~~ TemplatePlugin API

View file

@ -11,7 +11,6 @@ import copy
import re
import logging
from octoprint.settings import settings
from octoprint.util import dict_merge, dict_clean, dict_contains_keys
class SaveError(Exception):
@ -42,9 +41,10 @@ class LaserCutterProfileManager(object):
)
)
def __init__(self):
def __init__(self, settings):
self._current = None
self._folder = settings().getBaseFolder("plugins")+"/lasercutterprofiles"
self.settings = settings
self._folder = self.settings.getBaseFolder("plugins")+"/lasercutterprofiles"
if not os.path.exists(self._folder):
os.makedirs(self._folder)
self._logger = logging.getLogger(__name__)
@ -91,24 +91,23 @@ class LaserCutterProfileManager(object):
profile["id"] = identifier
profile = dict_clean(profile, self.__class__.default)
print("save", profile)
if identifier == "_default":
default_profile = dict_merge(self._load_default(), profile)
if not self._ensure_valid_profile(default_profile):
raise InvalidProfileError()
settings().set(["printerProfiles", "defaultProfile"], default_profile, defaults=dict(printerProfiles=dict(defaultProfile=self.__class__.default)))
settings().save()
self.settings.set(["defaultProfile"], default_profile, defaults=dict(lasercutterprofiles=dict(defaultProfile=self.__class__.default)))
self.settings.save()
else:
self._save_to_path(self._get_profile_path(identifier), profile, allow_overwrite=allow_overwrite)
if make_default:
settings().set(["printerProfiles", "default"], identifier)
self.set_default(identifier)
return self.get(identifier)
def get_default(self):
default = settings().get(["printerProfiles", "default"])
default = self.settings.get(["current_profile_id"])
if default is not None and self.exists(default):
profile = self.get(default)
if profile is not None:
@ -121,8 +120,8 @@ class LaserCutterProfileManager(object):
if identifier is not None and not identifier in all_identifiers:
return
settings().set(["printerProfile", "default"], identifier)
settings().save()
self.settings.set(["current_profile_id"], identifier)
self.settings.save()
def get_current_or_default(self):
if self._current is not None:
@ -191,7 +190,6 @@ class LaserCutterProfileManager(object):
def _save_to_path(self, path, profile, allow_overwrite=False):
validated_profile = self._ensure_valid_profile(profile)
print("_save_to_path", validated_profile, path)
if not validated_profile:
raise InvalidProfileError()
@ -203,7 +201,6 @@ class LaserCutterProfileManager(object):
try:
yaml.safe_dump(profile, f, default_flow_style=False, indent=" ", allow_unicode=True)
except Exception as e:
print (e.message)
raise SaveError("Cannot save profile %s: %s" % (profile["id"], e.message))
def _remove_from_path(self, path):
@ -214,7 +211,7 @@ class LaserCutterProfileManager(object):
return False
def _load_default(self, defaultModel = None):
default_overrides = settings().get(["laserCutterProfiles", defaultModel])
default_overrides = self.settings.get([defaultModel])
default = copy.deepcopy(self.__class__.default)
if(defaultModel is not None and defaultModel == "_mrbeam_senior"):
default['volume']['width'] *= 2
@ -252,7 +249,6 @@ class LaserCutterProfileManager(object):
def _ensure_valid_profile(self, profile):
# ensure all keys are present
if not dict_contains_keys(self.default, profile):
print("key error")
return False
# conversion helper
@ -274,7 +270,6 @@ class LaserCutterProfileManager(object):
try:
convert_value(profile, path, int)
except:
print("int error")
return False
# convert floats
@ -282,7 +277,6 @@ class LaserCutterProfileManager(object):
try:
convert_value(profile, path, float)
except:
print("float error")
return False
# convert booleans
@ -290,7 +284,6 @@ class LaserCutterProfileManager(object):
try:
convert_value(profile, path, bool)
except:
print("bool error")
return False
return profile

View file

@ -3,35 +3,26 @@ $(function() {
function LaserCutterProfilesViewModel(params) {
var self = this;
self.settings = params[0];
self.workingarea = params[0];
self._cleanProfile = function() {
return {
id: "",
name: "",
model: "",
color: "default",
volume: {
formFactor: "rectangular",
width: 200,
depth: 200,
height: 200
width: 216,
depth: 297,
height: 0
},
heatedBed: false,
zAxis: false,
axes: {
x: {speed: 6000, inverted: false},
y: {speed: 6000, inverted: false},
z: {speed: 200, inverted: false},
e: {speed: 300, inverted: false}
},
extruder: {
count: 1,
offsets: [
[0,0]
],
nozzleDiameter: 0.4
z: {speed: 200, inverted: false}
}
}
};
};
self.profiles = new ItemListHelper(
@ -58,26 +49,18 @@ $(function() {
self.editorNew = ko.observable(false);
self.editorName = ko.observable();
// self.editorColor = ko.observable();
self.editorIdentifier = ko.observable();
self.editorModel = ko.observable();
self.editorVolumeWidth = ko.observable();
self.editorVolumeDepth = ko.observable();
self.editorVolumeHeight = ko.observable();
// self.editorVolumeFormFactor = ko.observable();
// self.editorHeatedBed = ko.observable();
self.editorZAxis = ko.observable();
// self.editorNozzleDiameter = ko.observable();
// self.editorExtruders = ko.observable();
// self.editorExtruderOffsets = ko.observableArray();
self.editorAxisXSpeed = ko.observable();
self.editorAxisYSpeed = ko.observable();
self.editorAxisZSpeed = ko.observable();
// self.editorAxisESpeed = ko.observable();
self.editorAxisXInverted = ko.observable(false);
self.editorAxisYInverted = ko.observable(false);
@ -122,6 +105,9 @@ $(function() {
self.defaultProfile(defaultProfile);
self.currentProfile(currentProfile);
self.currentProfileData(currentProfileData);
self.workingarea.workingAreaWidthMM(self.currentProfileData().volume.width());
self.workingarea.workingAreaHeightMM(self.currentProfileData().volume.depth());
};
self.addProfile = function(callback) {
@ -154,6 +140,8 @@ $(function() {
if (profile == undefined) {
profile = self._editorData();
}
console.log("updateProfile", profile);
$.ajax({
url: BASEURL + "plugin/lasercutterprofiles/profiles/" + profile.id,
@ -253,16 +241,14 @@ $(function() {
self.onSettingsShown = self.requestData;
self.onStartup = function(){
console.log("lasercutter profiles onStartup");
self.settings.laserCutterProfiles = self;
self.requestData;
self.requestData();
};
}
// view model class, identifier, parameters for constructor, container to bind to
ADDITIONAL_VIEWMODELS.push([LaserCutterProfilesViewModel, "laserCutterProfilesViewModel",
["settingsViewModel"],
["workingAreaViewModel"],
document.getElementById("laserCutterProfiles")]);
});

View file

@ -21,12 +21,14 @@ function WorkingAreaViewModel(params) {
self.availableHeight = ko.observable(undefined);
self.availableWidth = ko.observable(undefined);
self.px2mm_factor = 1; // initial value
self.workingAreaWidthMM = ko.observable(undefined);
self.workingAreaHeightMM = ko.observable(undefined);
self.hwRatio = ko.computed(function(){
// y/x = 297/216 respectively 594/432
// var h = self.settings.laserCutterProfiles.currentProfileData().volume.depth();
// var w = self.settings.laserCutterProfiles.currentProfileData().volume.width();
var h = self.settings.printerProfiles.currentProfileData().volume.depth();
var w = self.settings.printerProfiles.currentProfileData().volume.width();
var h = self.workingAreaWidthMM();
var w = self.workingAreaHeightMM();
// var h = self.settings.printerProfiles.currentProfileData().volume.depth();
// var w = self.settings.printerProfiles.currentProfileData().volume.width();
var ratio = h / w;
return ratio;
}, self);
@ -50,18 +52,18 @@ function WorkingAreaViewModel(params) {
}
});
self.workingAreaWidth = ko.computed(function(){
self.workingAreaWidthPx = ko.computed(function(){
var dim = self.workingAreaDim();
return dim ? dim[0] : 1;
}, self);
self.workingAreaHeight = ko.computed(function(){
self.workingAreaHeightPx = ko.computed(function(){
var dim = self.workingAreaDim();
return dim ? dim[1] : 1;
}, self);
self.px2mm_factor = ko.computed(function(){
return self.settings.printerProfiles.currentProfileData().volume.width() / self.workingAreaWidth();
return self.workingAreaWidthMM() / self.workingAreaWidthPx();
});
self.scaleMatrix = ko.computed(function(){

View file

@ -289,8 +289,8 @@
data-bind="click: move_laser,
style: {
backgroundPosition: crosshairX()+'px'+' '+crosshairY()+'px',
width: workingAreaWidth()+'px',
height: workingAreaHeight()+'px'
width: workingAreaWidthPx()+'px',
height: workingAreaHeightPx()+'px'
},
attr: { transform: scaleMatrix() }
">

View file

@ -27,7 +27,7 @@
</div>
</div>
</div>
<div class="control-group">
<!-- <div class="control-group">
<label class="control-label" for="settings-movementSpeedE">{{ _('Temperature timeout') }}</label>
<div class="controls">
<div class="input-append">
@ -44,7 +44,7 @@
<span class="add-on">s</span>
</div>
</div>
</div>
</div>-->
<div class="control-group">
<label class="control-label" for="settings-serialTimeoutConnection">{{ _('Connection timeout') }}</label>
<div class="controls">