Skip to content

Commit

Permalink
Merge pull request #25 from Kragrathea/TestBranch
Browse files Browse the repository at this point in the history
Update grid when bed volume changes.
  • Loading branch information
Kragrathea authored May 1, 2020
2 parents bb73e11 + 3608b2a commit fdb6315
Showing 1 changed file with 98 additions and 53 deletions.
151 changes: 98 additions & 53 deletions octoprint_prettygcode/static/js/prettygcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,30 +338,22 @@ $(function () {
if (!viewInitialized) {
viewInitialized = true;

self.printerProfiles.currentProfileData.subscribe(
function(){
//get new build volume.
updateBedVolume();
//update scene if any
updateGridMesh();

//Needed in case center has changed.
resetCamera();
});


//get printer build volume.
//console.log(["self.printerProfiles",self.printerProfiles.currentProfileData()]);
var volume = self.printerProfiles.currentProfileData().volume;
if(typeof volume.custom_box ==="function")//check for custom bounds.
{
bedVolume={
width:volume.width(),
height:volume.height(),
depth:volume.depth(),
origin:volume.origin(),
formFactor:volume.formFactor(),
}
}else{
//console.log(["volume.custom_box",volume.custom_box]);
bedVolume={
width:volume.custom_box.x_max()-volume.custom_box.x_min(),
height:volume.custom_box.z_max()-volume.custom_box.z_min(),
depth:volume.custom_box.y_max()-volume.custom_box.y_min(),
origin:volume.origin(),
formFactor:volume.formFactor(),
}
}
updateBedVolume();

console.log(["bedVolume",bedVolume]);
//console.log(["bedVolume",bedVolume]);

if(true){
//simple gui
Expand Down Expand Up @@ -549,6 +541,34 @@ $(function () {
$("body").prepend(el);
}

function updateBedVolume() {

//var volume = ko.mapping.toJS(self.printerProfiles.currentProfileData().volume);
var volume = self.printerProfiles.currentProfileData().volume;
console.log([arguments.callee.name,volume]);

if (typeof volume.custom_box === "function") //check for custom bounds.
{
bedVolume = {
width: volume.width(),
height: volume.height(),
depth: volume.depth(),
origin: volume.origin(),
formFactor: volume.formFactor(),
};
}
else {
//console.log(["volume.custom_box",volume.custom_box]);
bedVolume = {
width: volume.custom_box.x_max() - volume.custom_box.x_min(),
height: volume.custom_box.z_max() - volume.custom_box.z_min(),
depth: volume.custom_box.y_max() - volume.custom_box.y_min(),
origin: volume.origin(),
formFactor: volume.formFactor(),
};
}
}

function GCodeParser(data) {

var state = { x: 0, y: 0, z: 0, e: 0, f: 0, extruding: false, relative: false };
Expand Down Expand Up @@ -1225,10 +1245,7 @@ $(function () {
cameraControls = new CameraControls(camera, canvas[0]);

//todo handle other than lowerleft
if(bedVolume.origin=="lowerleft")
cameraControls.setTarget(bedVolume.width/2, bedVolume.depth/2, 0, false);
else
cameraControls.setTarget(0, 0, 0, false);
resetCamera();


//for debugging
Expand Down Expand Up @@ -1263,33 +1280,7 @@ $(function () {


//Semi-transparent plane to represent the bed.
var planeGeometry = new THREE.PlaneGeometry( bedVolume.width, bedVolume.depth );
var planeMaterial = new THREE.MeshBasicMaterial( {color: 0x909090,
side: THREE.DoubleSide,
transparent: true,//pgSettings.transparency,
opacity:0.2,
});
var plane = new THREE.Mesh( planeGeometry, planeMaterial );

//todo handle other than lowerleft
if(bedVolume.origin=="lowerleft")
plane.position.set(bedVolume.width/2, bedVolume.depth/2,-0.1);

//plane.quaternion.setFromEuler(new THREE.Euler(- Math.PI / 2, 0, 0));
scene.add( plane );

//make bed sized grid.
var grid = new THREE.GridHelper(bedVolume.width, bedVolume.width/10, 0x000000, 0x888888);
//todo handle other than lowerleft
if(bedVolume.origin=="lowerleft")
grid.position.set(bedVolume.width/2, bedVolume.depth/2,0);

//if (pgSettings.transparency){
grid.material.opacity = 0.6;
grid.material.transparent = true;

grid.quaternion.setFromEuler(new THREE.Euler(- Math.PI / 2, 0, 0));
scene.add(grid);
updateGridMesh();

cubeCamera = new THREE.CubeCamera( 1, 100000, 128 );
cubeCamera.position.set(bedVolume.width/2, bedVolume.depth/2,10);
Expand Down Expand Up @@ -1405,6 +1396,60 @@ $(function () {

animate();
}

function resetCamera() {

if(!cameraControls)//Make sure controls exist.
return;

if (bedVolume.origin == "lowerleft")
cameraControls.setTarget(bedVolume.width / 2, bedVolume.depth / 2, 0, false);
else
cameraControls.setTarget(0, 0, 0, false);
}

function updateGridMesh(){
//console.log("updateGridMesh");
console.log(arguments.callee.name);

if(!scene)//scene loaded yet?
return;

var existingPlane = scene.getObjectByName("plane");
if(existingPlane)
scene.remove( existingPlane );
var existingGrid = scene.getObjectByName("grid");
if(existingGrid)
scene.remove( existingGrid );

console.log([existingPlane,existingGrid]);

var planeGeometry = new THREE.PlaneGeometry(bedVolume.width, bedVolume.depth);
var planeMaterial = new THREE.MeshBasicMaterial({
color: 0x909090,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.2,
});
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.name="plane";
//todo handle other than lowerleft
if (bedVolume.origin == "lowerleft")
plane.position.set(bedVolume.width / 2, bedVolume.depth / 2, -0.1);
//plane.quaternion.setFromEuler(new THREE.Euler(- Math.PI / 2, 0, 0));
scene.add(plane);
//make bed sized grid.
var grid = new THREE.GridHelper(bedVolume.width, bedVolume.width / 10, 0x000000, 0x888888);
grid.name="grid";
//todo handle other than lowerleft
if (bedVolume.origin == "lowerleft")
grid.position.set(bedVolume.width / 2, bedVolume.depth / 2, 0);
//if (pgSettings.transparency){
grid.material.opacity = 0.6;
grid.material.transparent = true;
grid.quaternion.setFromEuler(new THREE.Euler(-Math.PI / 2, 0, 0));
scene.add(grid);
}
}

OCTOPRINT_VIEWMODELS.push({
Expand Down

0 comments on commit fdb6315

Please sign in to comment.