Skip to content

Commit

Permalink
SW-3951 Refactor laser cutter profiles viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
khaledsherkawi committed Oct 22, 2023
1 parent eb5ea9c commit 0141b67
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 260 deletions.
19 changes: 2 additions & 17 deletions octoprint_mrbeam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1701,24 +1701,9 @@ def engraveCalibrationMarkers(self, intensity, feedrate):
# return NO_CONTENT

# Laser cutter profiles
@octoprint.plugin.BlueprintPlugin.route("/profiles", methods=["GET"])
@octoprint.plugin.BlueprintPlugin.route("/currentProfile", methods=["GET"])
def laserCutterProfilesList(self):
all_profiles = self.laserCutterProfileManager.converted_profiles()
for profile_id, profile in all_profiles.items():
all_profiles[profile_id]["resource"] = url_for(
".laserCutterProfilesGet", identifier=profile["id"], _external=True
)
return jsonify(dict(profiles=all_profiles))

@octoprint.plugin.BlueprintPlugin.route(
"/profiles/<string:identifier>", methods=["GET"]
)
def laserCutterProfilesGet(self, identifier):
profile = self.laserCutterProfileManager.get(identifier)
if profile is None:
return make_response("Unknown profile: %s" % identifier, 404)
else:
return jsonify(self._convert_profile(profile))
return jsonify(self.laser_cutter_profile_service.get_current_or_default())

# ~ Calibration
def generateCalibrationMarkersSvg(self):
Expand Down
10 changes: 4 additions & 6 deletions octoprint_mrbeam/static/js/app/view-models/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1261,9 +1261,7 @@ $(function () {
(intensity_black_user - intensity_white_user);
var intensity = Math.round(
intensity_user *
self.profile
.currentProfileData()
.laser.intensity_factor()
self.profile.currentProfileData().laser.intensity_factor
);
var feedrate = Math.round(
speed_white + initial_factor * (speed_black - speed_white)
Expand Down Expand Up @@ -1317,7 +1315,7 @@ $(function () {
);
var intensity =
intensity_user *
self.profile.currentProfileData().laser.intensity_factor();
self.profile.currentProfileData().laser.intensity_factor;
var feedrate = parseFloat($(job).find(".param_feedrate").val());
var piercetime = parseFloat(
$(job).find(".param_piercetime").val()
Expand Down Expand Up @@ -1411,11 +1409,11 @@ $(function () {
intensity_black_user: self.imgIntensityBlack(),
intensity_black:
self.imgIntensityBlack() *
self.profile.currentProfileData().laser.intensity_factor(),
self.profile.currentProfileData().laser.intensity_factor,
intensity_white_user: self.imgIntensityWhite(),
intensity_white:
self.imgIntensityWhite() *
self.profile.currentProfileData().laser.intensity_factor(),
self.profile.currentProfileData().laser.intensity_factor,
speed_black: self.imgFeedrateBlack(),
speed_white: self.imgFeedrateWhite(),
dithering: self.imgDithering(),
Expand Down
249 changes: 18 additions & 231 deletions octoprint_mrbeam/static/js/app/view-models/lasercutter-profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,270 +65,57 @@ $(function () {
max_travel_z: 132, // Z Max travel, mm
};

self.profiles = new ItemListHelper(
"laserCutterProfiles",
{
name: function (a, b) {
// sorts ascending
if (
a["name"].toLocaleLowerCase() <
b["name"].toLocaleLowerCase()
)
return -1;
if (
a["name"].toLocaleLowerCase() >
b["name"].toLocaleLowerCase()
)
return 1;
return 0;
},
},
{},
"name",
[],
[],
10
);

self.hasDataLoaded = false;

self.defaultProfile = ko.observable();
self.currentProfile = ko.observable();

self.currentProfileData = ko.observable(
ko.mapping.fromJS(self._cleanProfile())
);

self.editorNew = ko.observable(false);

self.editorName = ko.observable();
self.editorIdentifier = ko.observable();
self.editorModel = ko.observable();

self.editorVolumeWidth = ko.observable();
self.editorVolumeDepth = ko.observable();
self.editorVolumeHeight = ko.observable();

self.editorZAxis = ko.observable();
self.editorFocus = ko.observable();
self.editorGlasses = ko.observable();

self.editorAxisXSpeed = ko.observable();
self.editorAxisYSpeed = ko.observable();
self.editorAxisZSpeed = ko.observable();

self.editorAxisXInverted = ko.observable(false);
self.editorAxisYInverted = ko.observable(false);
self.editorAxisZInverted = ko.observable(false);

self.makeDefault = function (data) {
var profile = {
id: data.id,
default: true,
};

self.updateProfile(profile);
};
self.currentProfileData = ko.observable(self._cleanProfile());

self.requestData = function () {
$.ajax({
url: BASEURL + "plugin/mrbeam/profiles",
url: BASEURL + "plugin/mrbeam/currentProfile",
type: "GET",
dataType: "json",
success: self.fromResponse,
});
};

self.fromResponse = function (data) {
var items = [];
var defaultProfile = undefined;
var currentProfile = undefined;
var currentProfileData = undefined;
_.each(data.profiles, function (entry) {
if (entry.default) {
defaultProfile = entry.id;
}
if (entry.current) {
currentProfile = entry.id;
currentProfileData = ko.mapping.fromJS(
entry,
self.currentProfileData
);
}
entry["isdefault"] = ko.observable(entry.default);
entry["iscurrent"] = ko.observable(entry.current);
items.push(entry);
});
self.profiles.updateItems(items);
self.defaultProfile(defaultProfile);
self.currentProfile(currentProfile);
self.currentProfileData(currentProfileData);

self.currentProfile(data.id);
self.currentProfileData(data);
self.hasDataLoaded = true;

//TODO calculate MaxSpeed without Conversion
// var maxSpeed = Math.min(self.currentProfileData().axes.x.speed(), self.currentProfileData().axes.y.speed());
// var maxSpeed = Math.min(self.currentProfileData().axes.x.speed, self.currentProfileData().axes.y.speed);
// self.conversion.maxSpeed(maxSpeed);
};

self.addProfile = function (callback) {
var profile = self._editorData();
$.ajax({
url: BASEURL + "plugin/mrbeam/profiles",
type: "POST",
dataType: "json",
contentType: "application/json; charset=UTF-8",
data: JSON.stringify({ profile: profile }),
success: function () {
if (callback !== undefined) {
callback();
}
self.requestData();
},
});
};

self.removeProfile = function (data) {
$.ajax({
url: data.resource,
type: "DELETE",
dataType: "json",
success: self.requestData,
});
};

self.updateProfile = function (profile, callback) {
if (profile == undefined) {
profile = self._editorData();
}

$.ajax({
url: BASEURL + "plugin/mrbeam/profiles/" + profile.id,
type: "PATCH",
dataType: "json",
contentType: "application/json; charset=UTF-8",
data: JSON.stringify({ profile: profile }),
success: function () {
if (callback !== undefined) {
callback();
}
self.requestData();
},
});
};

self.showEditProfileDialog = function (data) {
var add = false;
if (data == undefined) {
data = self._cleanProfile();
add = true;
}

self.editorNew(add);

self.editorIdentifier(data.id);
self.editorName(data.name);
self.editorModel(data.model);

self.editorVolumeWidth(data.volume.width);
self.editorVolumeDepth(data.volume.depth);
self.editorVolumeHeight(data.volume.height);

self.editorZAxis(data.zAxis);
self.editorFocus(data.focus);
self.editorGlasses(data.glasses);

self.editorAxisXSpeed(data.axes.x.speed);
self.editorAxisXInverted(data.axes.x.inverted);
self.editorAxisYSpeed(data.axes.y.speed);
self.editorAxisYInverted(data.axes.y.inverted);
self.editorAxisZSpeed(data.axes.z.speed);
self.editorAxisZInverted(data.axes.z.inverted);

var editDialog = $("#settings_laserCutterProfiles_editDialog");
var confirmButton = $("button.btn-confirm", editDialog);
var dialogTitle = $("h3.modal-title", editDialog);

dialogTitle.text(
add
? "Add Profile"
: _.sprintf('Edit Profile "%(name)s"', { name: data.name })
);
confirmButton.unbind("click");
confirmButton.bind("click", function () {
self.confirmEditProfile(add);
});
editDialog.modal("show");
};

self.confirmEditProfile = function (add) {
var callback = function () {
$("#settings_laserCutterProfiles_editDialog").modal("hide");
};

if (add) {
self.addProfile(callback);
} else {
self.updateProfile(undefined, callback);
}
};

self.getMechanicalPerformanceData = function () {
const fx = self.currentProfileData().axes.x.speed();
const fy = self.currentProfileData().axes.y.speed();
const fx = self.currentProfileData().axes.x.speed;
const fy = self.currentProfileData().axes.y.speed;
const maxF = Math.min(fx, fy);
const ax = self
.currentProfileData()
.grbl.settings[self.grblKeys.max_acc_x]();
const ay = self
.currentProfileData()
.grbl.settings[self.grblKeys.max_acc_y]();
const ax =
self.currentProfileData().grbl.settings[
self.grblKeys.max_acc_x
];
const ay =
self.currentProfileData().grbl.settings[
self.grblKeys.max_acc_y
];
const maxAcc = Math.min(ax, ay);
return {
workingAreaWidth: self.currentProfileData().volume.width(),
workingAreaHeight: self.currentProfileData().volume.depth(),
workingAreaWidth: self.currentProfileData().volume.width,
workingAreaHeight: self.currentProfileData().volume.depth,
maxFeedrateXY: maxF,
accelerationXY: maxAcc,
};
};

self._editorData = function () {
var profile = {
id: self.editorIdentifier(),
name: self.editorName(),
model: self.editorModel(),
volume: {
width: parseFloat(self.editorVolumeWidth()),
depth: parseFloat(self.editorVolumeDepth()),
height: parseFloat(self.editorVolumeHeight()),
},
zAxis: self.editorZAxis(),
focus: self.editorFocus(),
glasses: self.editorGlasses(),
axes: {
x: {
speed: parseInt(self.editorAxisXSpeed()),
inverted: self.editorAxisXInverted(),
},
y: {
speed: parseInt(self.editorAxisYSpeed()),
inverted: self.editorAxisYInverted(),
},
z: {
speed: parseInt(self.editorAxisZSpeed()),
inverted: self.editorAxisZInverted(),
},
},
};

return profile;
};

self.onSettingsShown = self.requestData;
self.onStartup = function () {
self.requestData();
self.control.showZAxis = ko.computed(function () {
var has = self.currentProfileData()["zAxis"]();
var has = self.currentProfileData()["zAxis"];
return has;
}); // dependency injection
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ $(function () {
// self.oneButton =
// self.laserCutterProfiles.currentProfileData().start_method !=
// undefined &&
// self.laserCutterProfiles.currentProfileData().start_method() ==
// self.laserCutterProfiles.currentProfileData().start_method ==
// "onebutton";
// if (!self.laserCutterProfiles.hasDataLoaded) {
// self.oneButton = true;
Expand Down
2 changes: 1 addition & 1 deletion octoprint_mrbeam/static/js/app/view-models/mother.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ $(function () {
//self.requestData();

self.control.showZAxis = ko.computed(function () {
// var has = self.currentProfileData()['zAxis']();
// var has = self.currentProfileData()['zAxis'];
// return has;
return false;
});
Expand Down
8 changes: 4 additions & 4 deletions octoprint_mrbeam/static/js/app/view-models/working-area.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ $(function () {
});

self.workingAreaWidthMM = ko.computed(function () {
return self.profile.currentProfileData().volume.width();
return self.profile.currentProfileData().volume.width;
}, self);
self.workingAreaHeightMM = ko.computed(function () {
return self.profile.currentProfileData().volume.depth();
return self.profile.currentProfileData().volume.depth;
}, self);

// QuickShape limits
Expand Down Expand Up @@ -4286,8 +4286,8 @@ $(function () {

// TODO get from https://github.com/mrbeam/MrBeamPlugin/blob/2682b7a2e97373478e6516a98c8ba766d26ff317/octoprint_mrbeam/static/js/lasercutterprofiles.js#L276
// once this branch feature/SW-244... is merged.
const fx = self.profile.currentProfileData().axes.x.speed();
const fy = self.profile.currentProfileData().axes.y.speed();
const fx = self.profile.currentProfileData().axes.x.speed;
const fy = self.profile.currentProfileData().axes.y.speed;
const maxSpeed = Math.min(fx, fy);

crosshairHandle.mousedown(function (event) {
Expand Down

0 comments on commit 0141b67

Please sign in to comment.