2017-11-13 02:47:53 +01:00
|
|
|
import _ from 'lodash';
|
2017-11-11 20:23:45 +01:00
|
|
|
import React from 'react';
|
2017-12-06 11:54:09 +01:00
|
|
|
import { Quaternion } from 'three/src/math/Quaternion.js';
|
|
|
|
import { Vector3 } from 'three/src/math/Vector3.js';
|
2017-12-19 12:38:58 +01:00
|
|
|
import { Mesh } from 'three/src/objects/Mesh.js';
|
2017-11-11 20:23:45 +01:00
|
|
|
import PropTypes from 'proptypes';
|
2017-12-04 17:44:08 +01:00
|
|
|
import { placeOnGround, createScene, fetchProgress, slice, TabTemplate } from './utils.js';
|
2017-11-11 20:23:45 +01:00
|
|
|
import injectSheet from 'react-jss';
|
2017-11-12 16:58:59 +01:00
|
|
|
import RaisedButton from 'material-ui/RaisedButton';
|
2017-12-04 19:31:15 +01:00
|
|
|
import FlatButton from 'material-ui/FlatButton';
|
2017-11-12 16:58:59 +01:00
|
|
|
import Slider from 'material-ui/Slider';
|
2017-12-04 15:08:29 +01:00
|
|
|
import LinearProgress from 'material-ui/LinearProgress';
|
2017-12-04 17:51:56 +01:00
|
|
|
import { grey50, grey300, grey800, red500 } from 'material-ui/styles/colors';
|
2017-12-24 17:18:33 +01:00
|
|
|
import Popover from 'material-ui/Popover/Popover';
|
|
|
|
import Menu from 'material-ui/Menu';
|
|
|
|
import MenuItem from 'material-ui/MenuItem';
|
2017-12-04 17:44:08 +01:00
|
|
|
import { Tabs, Tab } from 'material-ui/Tabs';
|
2017-11-13 02:09:39 +01:00
|
|
|
import Settings from './Settings.js';
|
2017-12-24 14:46:00 +01:00
|
|
|
import defaultSettings from '../settings/default.yml';
|
2017-11-13 02:47:53 +01:00
|
|
|
import printerSettings from '../settings/printer.yml';
|
|
|
|
import materialSettings from '../settings/material.yml';
|
|
|
|
import qualitySettings from '../settings/quality.yml';
|
2017-11-13 12:42:35 +01:00
|
|
|
import ReactResizeDetector from 'react-resize-detector';
|
2017-11-13 02:47:53 +01:00
|
|
|
|
2017-12-04 15:08:29 +01:00
|
|
|
const MAX_FULLSCREEN_WIDTH = 720;
|
|
|
|
|
2017-11-11 20:23:45 +01:00
|
|
|
const styles = {
|
|
|
|
container: {
|
2017-11-12 18:41:00 +01:00
|
|
|
position: 'relative',
|
2017-11-13 12:42:35 +01:00
|
|
|
display: 'flex',
|
|
|
|
height: '100%',
|
2017-12-04 17:51:56 +01:00
|
|
|
backgroundColor: grey50,
|
|
|
|
color: grey800,
|
2017-12-04 15:08:29 +01:00
|
|
|
overflow: 'hidden',
|
|
|
|
fontFamily: 'roboto, sans-serif'
|
2017-11-11 20:23:45 +01:00
|
|
|
},
|
|
|
|
controlBar: {
|
|
|
|
position: 'absolute',
|
2017-11-12 16:58:59 +01:00
|
|
|
bottom: '10px',
|
|
|
|
left: '10px'
|
2017-11-11 20:23:45 +01:00
|
|
|
},
|
2017-11-13 12:42:35 +01:00
|
|
|
d3View: {
|
2017-12-04 15:08:29 +01:00
|
|
|
flexGrow: 1,
|
|
|
|
flexBasis: 0
|
2017-11-13 12:42:35 +01:00
|
|
|
},
|
|
|
|
canvas: {
|
|
|
|
position: 'absolute'
|
|
|
|
},
|
2017-12-04 17:44:08 +01:00
|
|
|
settingsBar: {
|
2017-12-04 15:08:29 +01:00
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
|
|
|
maxWidth: '380px',
|
|
|
|
boxSizing: 'border-box',
|
2017-11-13 13:37:36 +01:00
|
|
|
padding: '10px',
|
2017-11-13 12:42:35 +01:00
|
|
|
backgroundColor: 'white',
|
|
|
|
borderLeft: `1px solid ${grey300}`
|
2017-11-12 12:34:50 +01:00
|
|
|
},
|
|
|
|
sliceActions: {
|
2017-12-04 15:08:29 +01:00
|
|
|
flexShrink: 0,
|
|
|
|
},
|
|
|
|
sliceButtons: {
|
|
|
|
justifyContent: 'flex-end',
|
|
|
|
display: 'flex'
|
2017-11-12 18:41:00 +01:00
|
|
|
},
|
|
|
|
button: {
|
2017-12-04 15:08:29 +01:00
|
|
|
margin: '5px 0 5px 5px'
|
2017-11-13 11:01:57 +01:00
|
|
|
},
|
|
|
|
controlButton: {
|
|
|
|
marginRight: '2px'
|
2017-12-04 15:08:29 +01:00
|
|
|
},
|
|
|
|
buttonContainer: {
|
|
|
|
width: '100%',
|
|
|
|
padding: '10px'
|
|
|
|
},
|
|
|
|
error: {
|
|
|
|
color: red500
|
2017-12-04 17:51:56 +01:00
|
|
|
},
|
|
|
|
title: {
|
|
|
|
position: 'absolute'
|
2017-11-11 20:23:45 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Interface extends React.Component {
|
2017-11-16 14:54:47 +01:00
|
|
|
static propTypes = {
|
2017-12-19 12:38:58 +01:00
|
|
|
mesh: PropTypes.shape({ isMesh: PropTypes.oneOf([true]) }).isRequired,
|
2017-11-16 14:54:47 +01:00
|
|
|
classes: PropTypes.objectOf(PropTypes.string),
|
|
|
|
defaultSettings: PropTypes.object.isRequired,
|
|
|
|
printers: PropTypes.object.isRequired,
|
2017-12-05 13:03:00 +01:00
|
|
|
defaultPrinter: PropTypes.string,
|
2017-11-16 14:54:47 +01:00
|
|
|
quality: PropTypes.object.isRequired,
|
|
|
|
defaultQuality: PropTypes.string.isRequired,
|
|
|
|
material: PropTypes.object.isRequired,
|
|
|
|
defaultMaterial: PropTypes.string.isRequired,
|
2017-12-04 19:31:15 +01:00
|
|
|
pixelRatio: PropTypes.number.isRequired,
|
2017-12-05 13:03:00 +01:00
|
|
|
onCancel: PropTypes.func,
|
|
|
|
name: PropTypes.string.isRequired
|
2017-11-16 14:54:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
2017-12-24 14:46:00 +01:00
|
|
|
defaultSettings: defaultSettings,
|
2017-11-16 14:54:47 +01:00
|
|
|
printers: printerSettings,
|
|
|
|
quality: qualitySettings,
|
|
|
|
defaultQuality: 'medium',
|
|
|
|
material: materialSettings,
|
|
|
|
defaultMaterial: 'pla',
|
2017-12-05 13:03:00 +01:00
|
|
|
pixelRatio: 1,
|
|
|
|
name: 'Doodle3D'
|
2017-11-16 14:54:47 +01:00
|
|
|
};
|
|
|
|
|
2017-11-12 01:41:05 +01:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-11-13 10:40:58 +01:00
|
|
|
const { defaultPrinter, defaultQuality, defaultMaterial, printers, quality, material, defaultSettings } = props;
|
2017-11-12 01:41:05 +01:00
|
|
|
this.state = {
|
2017-12-04 17:44:08 +01:00
|
|
|
showFullScreen: false,
|
2017-11-12 16:58:59 +01:00
|
|
|
isSlicing: false,
|
2017-12-04 15:08:29 +01:00
|
|
|
error: null,
|
2017-11-13 15:47:19 +01:00
|
|
|
printers: defaultPrinter,
|
2017-11-13 15:32:23 +01:00
|
|
|
quality: defaultQuality,
|
|
|
|
material: defaultMaterial,
|
2017-12-24 17:18:33 +01:00
|
|
|
popover: {
|
|
|
|
element: null,
|
|
|
|
open: false
|
|
|
|
},
|
2017-11-13 10:40:58 +01:00
|
|
|
settings: _.merge(
|
|
|
|
{},
|
|
|
|
defaultSettings,
|
|
|
|
printers[defaultPrinter],
|
|
|
|
quality[defaultQuality],
|
|
|
|
material[defaultMaterial]
|
|
|
|
)
|
2017-11-12 01:41:05 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-11 20:23:45 +01:00
|
|
|
componentDidMount() {
|
|
|
|
const { canvas } = this.refs;
|
2017-12-18 16:37:03 +01:00
|
|
|
|
2017-11-11 20:23:45 +01:00
|
|
|
const scene = createScene(canvas, this.props, this.state);
|
2017-11-16 14:54:47 +01:00
|
|
|
this.setState({ ...scene });
|
2017-11-11 20:23:45 +01:00
|
|
|
}
|
|
|
|
|
2017-12-04 15:08:29 +01:00
|
|
|
componentWillUnmount() {
|
2018-01-15 14:21:42 +01:00
|
|
|
const { scene: { editorControls, mesh: { material } }, renderer } = this.state;
|
2017-12-19 12:38:58 +01:00
|
|
|
editorControls.dispose();
|
|
|
|
material.dispose();
|
|
|
|
renderer.dispose();
|
2017-12-04 15:08:29 +01:00
|
|
|
}
|
|
|
|
|
2017-11-11 20:23:45 +01:00
|
|
|
resetMesh = () => {
|
2017-12-24 14:52:24 +01:00
|
|
|
if (isSlicing) return;
|
2018-01-15 14:21:42 +01:00
|
|
|
const { scene: { mesh, render }, isSlicing, isLoading } = this.state;
|
2017-11-11 20:23:45 +01:00
|
|
|
if (mesh) {
|
|
|
|
mesh.position.set(0, 0, 0);
|
|
|
|
mesh.scale.set(1, 1, 1);
|
|
|
|
mesh.rotation.set(0, 0, 0);
|
|
|
|
mesh.updateMatrix();
|
|
|
|
placeOnGround(mesh);
|
|
|
|
render();
|
|
|
|
}
|
2017-11-12 01:04:26 +01:00
|
|
|
};
|
|
|
|
|
2017-12-04 15:08:29 +01:00
|
|
|
scaleUp = () => this.scaleMesh(0.9);
|
|
|
|
scaleDown = () => this.scaleMesh(1.0 / 0.9);
|
|
|
|
scaleMesh = (factor) => {
|
2017-12-24 14:52:24 +01:00
|
|
|
if (isSlicing) return;
|
2018-01-15 14:21:42 +01:00
|
|
|
const { scene: { mesh, render }, isSlicing, isLoading } = this.state;
|
2017-12-04 15:08:29 +01:00
|
|
|
if (mesh) {
|
|
|
|
mesh.scale.multiplyScalar(factor);
|
|
|
|
mesh.updateMatrix();
|
|
|
|
placeOnGround(mesh);
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
};
|
2017-11-12 01:04:26 +01:00
|
|
|
|
2017-12-06 11:54:09 +01:00
|
|
|
rotateX = () => this.rotate(new Vector3(0, 0, 1), Math.PI / 2.0);
|
|
|
|
rotateY = () => this.rotate(new Vector3(1, 0, 0), Math.PI / 2.0);
|
|
|
|
rotateZ = () => this.rotate(new Vector3(0, 1, 0), Math.PI / 2.0);
|
2017-12-05 11:10:17 +01:00
|
|
|
rotate = (axis, angle) => {
|
2017-12-24 14:52:24 +01:00
|
|
|
if (isSlicing) return;
|
2018-01-15 14:21:42 +01:00
|
|
|
const { scene: { mesh, render }, isSlicing, isLoading } = this.state;
|
2017-12-04 15:08:29 +01:00
|
|
|
if (mesh) {
|
2017-12-06 11:54:09 +01:00
|
|
|
mesh.rotateOnWorldAxis(axis, angle);
|
2017-12-04 15:08:29 +01:00
|
|
|
placeOnGround(mesh);
|
|
|
|
render();
|
|
|
|
}
|
2017-11-12 01:04:26 +01:00
|
|
|
};
|
2017-11-11 20:23:45 +01:00
|
|
|
|
2017-12-24 17:18:33 +01:00
|
|
|
slice = async (target) => {
|
2018-01-15 14:21:42 +01:00
|
|
|
const { isSlicing, isLoading, settings, printers, quality, scene: { material, mesh: { matrix } } } = this.state;
|
2017-12-19 12:38:58 +01:00
|
|
|
const { name, mesh } = this.props;
|
2017-11-12 00:46:00 +01:00
|
|
|
|
2017-12-04 15:08:29 +01:00
|
|
|
if (isSlicing) return;
|
2017-11-12 00:46:00 +01:00
|
|
|
|
2017-12-24 17:18:33 +01:00
|
|
|
this.closePopover();
|
|
|
|
|
|
|
|
this.setState({ isSlicing: true, progress: { action: '', percentage: 0, step: 0 }, error: null });
|
2017-11-12 12:34:50 +01:00
|
|
|
|
2017-12-19 12:38:58 +01:00
|
|
|
const exportMesh = new Mesh(mesh.geometry, mesh.material);
|
|
|
|
exportMesh.applyMatrix(matrix);
|
|
|
|
|
2017-12-04 15:08:29 +01:00
|
|
|
try {
|
2017-12-24 17:18:33 +01:00
|
|
|
await slice(target, name, exportMesh, settings, printers, quality, material, progress => {
|
2017-12-04 15:08:29 +01:00
|
|
|
this.setState({ progress: { ...this.state.progress, ...progress } });
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
this.setState({ error: error.message });
|
2017-12-18 16:37:03 +01:00
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
this.setState({ isSlicing: false });
|
2017-12-04 15:08:29 +01:00
|
|
|
}
|
2017-11-12 00:11:05 +01:00
|
|
|
};
|
|
|
|
|
2017-12-24 17:18:33 +01:00
|
|
|
openPopover = (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
popover: {
|
|
|
|
element: event.currentTarget,
|
|
|
|
open: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
closePopover = () => {
|
|
|
|
this.setState({
|
|
|
|
popover: {
|
|
|
|
element: null,
|
|
|
|
open: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-13 02:47:53 +01:00
|
|
|
onChangeSettings = (settings) => {
|
2017-11-13 15:12:59 +01:00
|
|
|
this.setState(settings);
|
2017-11-13 02:47:53 +01:00
|
|
|
};
|
|
|
|
|
2017-11-11 20:23:45 +01:00
|
|
|
componentWillUpdate(nextProps, nextState) {
|
2018-01-15 14:21:42 +01:00
|
|
|
if (!this.state.scene) return;
|
|
|
|
const { scene: { box, render, setSize } } = this.state;
|
2017-12-04 15:08:29 +01:00
|
|
|
let changed = false;
|
2017-11-13 02:47:53 +01:00
|
|
|
if (box && nextState.settings.dimensions !== this.state.settings.dimensions) {
|
|
|
|
const { dimensions } = nextState.settings;
|
|
|
|
box.scale.set(dimensions.y, dimensions.z, dimensions.x);
|
2017-12-04 15:08:29 +01:00
|
|
|
box.updateMatrix();
|
|
|
|
changed = true;
|
2017-11-13 02:47:53 +01:00
|
|
|
}
|
2017-12-04 15:08:29 +01:00
|
|
|
if (changed) render();
|
2017-11-11 20:23:45 +01:00
|
|
|
}
|
|
|
|
|
2017-12-04 17:44:08 +01:00
|
|
|
componentDidUpdate() {
|
2018-01-15 14:21:42 +01:00
|
|
|
const { scene: { updateCanvas } } = this.state;
|
2017-12-04 17:44:08 +01:00
|
|
|
const { canvas } = this.refs;
|
|
|
|
if (updateCanvas && canvas) updateCanvas(canvas);
|
|
|
|
}
|
|
|
|
|
2017-12-04 15:08:29 +01:00
|
|
|
onResize3dView = (width, height) => {
|
2017-11-13 12:42:35 +01:00
|
|
|
window.requestAnimationFrame(() => {
|
2018-01-15 14:21:42 +01:00
|
|
|
const { scene: { setSize } } = this.state;
|
2017-11-13 12:42:35 +01:00
|
|
|
const { pixelRatio } = this.props;
|
2017-12-19 12:38:58 +01:00
|
|
|
if (setSize) setSize(width, height, pixelRatio);
|
2017-11-13 12:42:35 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-12-04 15:08:29 +01:00
|
|
|
onResizeContainer = (width) => {
|
2017-12-04 17:44:08 +01:00
|
|
|
this.setState({ showFullScreen: width > MAX_FULLSCREEN_WIDTH });
|
2017-12-04 15:08:29 +01:00
|
|
|
};
|
|
|
|
|
2017-11-11 20:23:45 +01:00
|
|
|
render() {
|
2017-12-04 19:31:09 +01:00
|
|
|
const { classes, defaultPrinter, defaultQuality, defaultMaterial, onCancel } = this.props;
|
|
|
|
const { isSlicing, progress, settings, printers, quality, material, showFullScreen, error } = this.state;
|
2017-12-04 15:08:29 +01:00
|
|
|
|
2017-12-19 12:38:58 +01:00
|
|
|
const style = { ...(showFullScreen ? {} : { maxWidth: 'inherit', width: '100%', height: '100%' }) };
|
2017-12-04 15:08:29 +01:00
|
|
|
|
2017-12-04 17:44:08 +01:00
|
|
|
const settingsPanel = (
|
2017-12-19 12:38:58 +01:00
|
|
|
<div className={classes.settingsBar} style={style}>
|
2017-12-04 17:44:08 +01:00
|
|
|
<Settings
|
|
|
|
disabled={isSlicing}
|
|
|
|
printers={printerSettings}
|
|
|
|
defaultPrinter={defaultPrinter}
|
|
|
|
quality={qualitySettings}
|
|
|
|
defaultQuality={defaultQuality}
|
|
|
|
material={materialSettings}
|
|
|
|
defaultMaterial={defaultMaterial}
|
|
|
|
initialSettings={settings}
|
|
|
|
onChange={this.onChangeSettings}
|
|
|
|
/>
|
|
|
|
<div className={classes.sliceActions}>
|
|
|
|
{error && <p className={classes.error}>{error}</p>}
|
|
|
|
{isSlicing && <p>{progress.action}</p>}
|
2017-12-24 17:18:33 +01:00
|
|
|
{isSlicing && <LinearProgress mode="determinate" value={progress.percentage * 100.0} />}
|
2017-12-04 17:44:08 +01:00
|
|
|
<div className={classes.sliceButtons}>
|
2017-12-04 19:31:15 +01:00
|
|
|
{onCancel && <RaisedButton
|
|
|
|
label="Cancel"
|
|
|
|
className={`${classes.button}`}
|
|
|
|
onTouchTap={onCancel}
|
|
|
|
/>}
|
2017-12-04 17:44:08 +01:00
|
|
|
<RaisedButton
|
|
|
|
label="Print"
|
2017-12-24 17:18:33 +01:00
|
|
|
ref="button"
|
2017-12-04 17:44:08 +01:00
|
|
|
primary
|
|
|
|
className={`${classes.button}`}
|
2017-12-24 17:18:33 +01:00
|
|
|
onTouchTap={this.openPopover}
|
2017-12-04 17:44:08 +01:00
|
|
|
disabled={isSlicing}
|
|
|
|
/>
|
2017-12-24 17:18:33 +01:00
|
|
|
<Popover
|
|
|
|
open={this.state.popover.open}
|
|
|
|
anchorEl={this.state.popover.element}
|
|
|
|
anchorOrigin={{horizontal: 'left', vertical: 'bottom'}}
|
|
|
|
targetOrigin={{horizontal: 'left', vertical: 'bottom'}}
|
|
|
|
onRequestClose={this.closePopover}
|
|
|
|
>
|
|
|
|
<Menu>
|
|
|
|
<MenuItem primaryText="Send over WiFi" onTouchTap={() => this.slice('WIFI')} />
|
|
|
|
<MenuItem primaryText="Download GCode" onTouchTap={() => this.slice('DOWNLOAD')} />
|
|
|
|
</Menu>
|
|
|
|
</Popover>
|
2017-12-04 15:08:29 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2017-11-11 20:23:45 +01:00
|
|
|
</div>
|
|
|
|
);
|
2017-12-04 17:44:08 +01:00
|
|
|
|
|
|
|
const d3Panel = (
|
|
|
|
<div className={classes.d3View}>
|
|
|
|
<ReactResizeDetector handleWidth handleHeight onResize={this.onResize3dView} />
|
|
|
|
<canvas className={classes.canvas} ref="canvas" />
|
2017-12-24 14:52:24 +01:00
|
|
|
<div className={classes.controlBar}>
|
|
|
|
<RaisedButton disabled={isSlicing} className={classes.controlButton} onTouchTap={this.resetMesh} label="reset" />
|
|
|
|
<RaisedButton disabled={isSlicing} className={classes.controlButton} onTouchTap={this.scaleUp} label="scale down" />
|
|
|
|
<RaisedButton disabled={isSlicing} className={classes.controlButton} onTouchTap={this.scaleDown} label="scale up" />
|
|
|
|
<RaisedButton disabled={isSlicing} className={classes.controlButton} onTouchTap={this.rotateX} label="rotate x" />
|
|
|
|
<RaisedButton disabled={isSlicing} className={classes.controlButton} onTouchTap={this.rotateY} label="rotate y" />
|
|
|
|
<RaisedButton disabled={isSlicing} className={classes.controlButton} onTouchTap={this.rotateZ} label="rotate z" />
|
|
|
|
</div>
|
2017-12-04 17:44:08 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (showFullScreen) {
|
|
|
|
return (
|
|
|
|
<div className={classes.container}>
|
|
|
|
<ReactResizeDetector handleWidth handleHeight onResize={this.onResizeContainer} />
|
2017-12-04 17:51:56 +01:00
|
|
|
<h1 className={classes.title}>Print</h1>
|
2017-12-04 17:44:08 +01:00
|
|
|
{d3Panel}
|
|
|
|
{settingsPanel}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<div className={classes.container}>
|
|
|
|
<ReactResizeDetector handleWidth handleHeight onResize={this.onResizeContainer} />
|
|
|
|
<Tabs
|
|
|
|
style={{ width: '100%', display: 'flex', flexDirection: 'column' }}
|
|
|
|
tabItemContainerStyle={{ flexShrink: 0 }}
|
|
|
|
contentContainerStyle={{ flexGrow: 1, display: 'flex' }}
|
|
|
|
tabTemplateStyle={{ display: 'flex' }}
|
|
|
|
tabTemplate={TabTemplate}
|
|
|
|
>
|
|
|
|
<Tab label="Settings">
|
|
|
|
{settingsPanel}
|
|
|
|
</Tab>
|
2017-12-04 17:45:28 +01:00
|
|
|
<Tab label="Edit Model">
|
2017-12-04 17:44:08 +01:00
|
|
|
{d3Panel}
|
|
|
|
</Tab>
|
|
|
|
</Tabs>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2017-11-11 20:23:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default injectSheet(styles)(Interface);
|