summaryrefslogtreecommitdiff
path: root/tags/host/0.8.1/src/org/reprap/gui/PreviewPanel.java
blob: 809e7d398bfd63d9a497261689d5d9d452533171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package org.reprap.gui;

import javax.media.j3d.*;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.vecmath.Color3f;
import org.reprap.Extruder;
import org.reprap.Printer;

//import javax.vecmath.Vector3d;

public class PreviewPanel extends Panel3D implements Previewer {
	private Printer reprap;
//	private Extruder extruder = null;
	private double previousZ = Double.NaN;
	private JCheckBoxMenuItem layerPauseCheckbox = null, segmentPauseCheckbox = null;
	private BranchGroup extrusionsNew;
	private BranchGroup extrusionsOld = null;
    private BranchGroup lowerShell = null;
    
	private StatusMessage statusWindow;
	
//	private double extrusionSize = 1.0;
//	private double extrusionHeight = 1.0;
		
	/**
	 * Constructor
	 */
	public PreviewPanel() throws Exception {
		initialise();
		statusWindow = new StatusMessage(new JFrame());
		extrusionsOld = null;
		lowerShell = null;
	}
	
	/**
	 *
	 */
	public void setMachine(Printer p) { reprap = p; }
	
	/**
	 * Set bg light grey
	 */
	protected Background createBackground() {
		Background back = new Background(bgColour);
		back.setApplicationBounds(createApplicationBounds());
		return back;
	}

	protected BranchGroup createViewBranchGroup(TransformGroup[] tgArray,
			ViewPlatform vp) {
		BranchGroup vpBranchGroup = new BranchGroup();

		if (tgArray != null && tgArray.length > 0) {
			Group parentGroup = vpBranchGroup;
			TransformGroup curTg = null;

			for (int n = 0; n < tgArray.length; n++) {
				curTg = tgArray[n];
				parentGroup.addChild(curTg);
				parentGroup = curTg;
			}

			tgArray[tgArray.length - 1].addChild(vp);
		} else
			vpBranchGroup.addChild(vp);

		return vpBranchGroup;
	}

	/**
	 * Set stuff up for the constructors - called by all of them that actually
	 * do anything.
	 */


	/**
	 * Set up the RepRap working volume
	 */
	protected BranchGroup createSceneBranchGroup() throws Exception {
		sceneBranchGroup = new BranchGroup();

		BranchGroup objRoot = sceneBranchGroup;

		Bounds lightBounds = getApplicationBounds();

		AmbientLight ambLight = new AmbientLight(true, new Color3f(1.0f, 1.0f,
				1.0f));
		ambLight.setInfluencingBounds(lightBounds);
		objRoot.addChild(ambLight);

		DirectionalLight headLight = new DirectionalLight();
		headLight.setInfluencingBounds(lightBounds);
		objRoot.addChild(headLight);

		wv_and_stls.setCapability(Group.ALLOW_CHILDREN_EXTEND);
		extrusionsNew = new BranchGroup();
		extrusionsNew.setCapability(Group.ALLOW_CHILDREN_EXTEND);
		extrusionsNew.setCapability(Group.ALLOW_CHILDREN_WRITE);
		extrusionsNew.setCapability(BranchGroup.ALLOW_DETACH);
		wv_and_stls.addChild(extrusionsNew);
		
		// Load the STL file for the working volume

		world = new STLObject(wv_and_stls, worldName);

		String stlFile = getStlBackground();

		workingVolume = new STLObject();
		workingVolume.addSTL(stlFile, wv_offset, wv_app);
		wv_and_stls.addChild(workingVolume.top);

		// Set the mouse to move everything
		MouseObject mouse = new MouseObject(getApplicationBounds(), mouse_tf, mouse_zf);
		mouse.move(world, false);
		
		objRoot.addChild(world.top);

		return objRoot;
	}

	/**
	 * Set the current extrusion material (or equivalently, the extruder head)
	 */
//	public void setMaterial(Extruder ext) {
//		extruder = ext;
//	}

	/**
	 * Called to add a new segment of extruded material to the preview
	 */
	public void addSegment(double x1, double y1, double z1, double x2, double y2, double z2) {
		if (layerPauseCheckbox != null && layerPauseCheckbox.isSelected() &&
				z2 != previousZ)
			layerPause();
		
		if (segmentPauseCheckbox != null && segmentPauseCheckbox.isSelected())
			segmentPause();
		
		if (isCancelled()) return;
		
		BranchGroup group = new BranchGroup();
		group.setCapability(BranchGroup.ALLOW_DETACH);
		addBlock(group, reprap.getExtruder().getAppearance(),
				x1, y1, z1,
				x2, y2, z2,
				(float)(reprap.getExtruder().getExtrusionSize() * 0.5), (float)(reprap.getExtruder().getExtrusionHeight() * 0.5));
		extrusionsNew.addChild(group);
		previousZ = z2;
	}

	/**
	 * Clear and prepare for a new preview
	 *
	 */
	public void reset() {
		extrusionsNew.removeAllChildren();
		if(extrusionsOld != null)
			extrusionsOld.removeAllChildren();
		
		if(lowerShell != null)
			lowerShell.removeAllChildren();

		extrusionsOld = null;
		lowerShell = null;
		previousZ = Double.NaN;
		setCancelled(false);
	}
	
	/**
	 * Display a message indicating a segment is about to be
	 * printed and wait for the user to acknowledge
	 */
	private void segmentPause() {
		ContinuationMesage msg =
			new ContinuationMesage(null, "A new segment is about to be produced",
					segmentPauseCheckbox, layerPauseCheckbox);
		msg.setVisible(true);
		try {
			synchronized(msg) {
				msg.wait();
			}
		} catch (Exception ex) {
		}
		if (msg.getResult() == false)
			setCancelled(true);
		msg.dispose();
	}

	/**
	 * Display a message indicating a layer is about to be
	 * printed and wait for the user to acknowledge
	 */
	private void layerPause() {
		ContinuationMesage msg =
			new ContinuationMesage(null, "A new layer is about to be produced",
					segmentPauseCheckbox, layerPauseCheckbox);
		msg.setVisible(true);
		try {
			synchronized(msg) {
				msg.wait();
			}
		} catch (Exception ex) {
		}
		if (msg.getResult() == false)
			setCancelled(true);
		msg.dispose();
	}

	/**
	 * Set the source checkbox used to determine if there should
	 * be a pause between segments.
	 * 
	 * @param segmentPause The source checkbox used to determine
	 * if there should be a pause.  This is a checkbox rather than
	 * a boolean so it can be changed on the fly. 
	 */
	public void setSegmentPause(JCheckBoxMenuItem segmentPause) {
		segmentPauseCheckbox = segmentPause;
	}

	/**
	 * Set the source checkbox used to determine if there should
	 * be a pause between layers.
	 * 
	 * @param layerPause The source checkbox used to determine
	 * if there should be a pause.  This is a checkbox rather than
	 * a boolean so it can be changed on the fly.
	 */
	public void setLayerPause(JCheckBoxMenuItem layerPause) {
		layerPauseCheckbox = layerPause;
	}

	public void setMessage(String message) {
		if (message == null)
			statusWindow.setVisible(false);
		else {
			statusWindow.setMessage(message);
			statusWindow.setVisible(true);
		}
	}
	
	public boolean isCancelled() {
		return statusWindow.isCancelled();
	}

	public void setCancelled(boolean isCancelled) {
		statusWindow.setCancelled(isCancelled);
	}
	
	public void setLowerShell(Shape3D ls)
	{
		if(extrusionsOld != null)
			extrusionsOld.removeAllChildren();
		
		if(lowerShell != null)
			lowerShell.removeAllChildren();

		extrusionsOld = extrusionsNew;	
		
		lowerShell = new BranchGroup();
		BranchGroup subLower = new BranchGroup();
		if(ls != null)
		{
			ls.setAppearance(reprap.getExtruder().getAppearance());
			subLower.addChild(ls);
			lowerShell.addChild(subLower);
		}
		lowerShell.setCapability(BranchGroup.ALLOW_DETACH);
		lowerShell.setCapability(Group.ALLOW_CHILDREN_WRITE);
		subLower.setCapability(BranchGroup.ALLOW_DETACH);
		subLower.setCapability(Group.ALLOW_CHILDREN_WRITE);
		
		extrusionsNew = new BranchGroup();
		extrusionsNew.setCapability(Group.ALLOW_CHILDREN_EXTEND);
		extrusionsNew.setCapability(Group.ALLOW_CHILDREN_WRITE);
		extrusionsNew.setCapability(BranchGroup.ALLOW_DETACH);
		wv_and_stls.addChild(extrusionsNew);
		wv_and_stls.addChild(lowerShell);
	}
}