summaryrefslogtreecommitdiff
path: root/tags/host/0.8.1/src/org/reprap/geometry/Producer.java
blob: 4389ba41a3b9d125e2a410171d0713e61c66d29c (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package org.reprap.geometry;

import javax.media.j3d.*;
import javax.vecmath.*;
import org.reprap.Preferences;
import org.reprap.Printer;
import org.reprap.geometry.polygons.*;
import org.reprap.gui.PreviewPanel;
import org.reprap.gui.RepRapBuild;
import org.reprap.machines.MachineFactory;
import org.reprap.machines.NullCartesianMachine;

public class Producer {
	
	/**
	 * 
	 */
	protected Printer reprap;
	
	/**
	 * 
	 */
	protected RrHalfPlane oddHatchDirection;
	
	/**
	 * 
	 */
	protected RrHalfPlane evenHatchDirection;
	
	/**
	 * 
	 */
	protected RepRapBuild bld;
	
	/**
	 * @param preview
	 * @param builder
	 * @throws Exception
	 */
	public Producer(PreviewPanel preview, RepRapBuild builder) throws Exception {
		reprap = MachineFactory.create();
		reprap.setPreviewer(preview);
		preview.setMachine(reprap);
		bld = builder;
		oddHatchDirection = new RrHalfPlane(new Rr2Point(0.0, 0.0), new Rr2Point(1.0, 1.0));
		evenHatchDirection = new RrHalfPlane(new Rr2Point(0.0, 1.0), new Rr2Point(1.0, 0.0));
	}
	
//	/**
//	 * @return simple polygon object that represents a 2D square
//	 */
//	public RrPolygon square()
//	{
//		RrPolygon a = new RrPolygon();
//		Rr2Point p1 = new Rr2Point(10, 10);
//		Rr2Point p2 = new Rr2Point(20, 10);
//		Rr2Point p3 = new Rr2Point(20, 20);
//		Rr2Point p4 = new Rr2Point(10, 20);
//		a.add(p1, 1);
//		a.add(p2, 1);
//		a.add(p3, 1);
//		a.add(p4, 1);
//		return a;
//	}
	
//	/**
//	 * @return hexagonal object
//	 */
//	public RrCSGPolygon hex()
//	{
//		double hexSize = 20;
//		double hexX = 15, hexY = 15;
//		
//		RrCSG r = RrCSG.universe();
//		Rr2Point pold = new Rr2Point(hexX + hexSize/2, hexY);
//		Rr2Point p;
//		double theta = 0; 
//		for(int i = 0; i < 6; i++)
//		{
//			theta += Math.PI * 60. / 180.0;
//			p = new Rr2Point(hexX + Math.cos(theta)*hexSize/2, hexY + Math.sin(theta)*hexSize/2);
//			r = RrCSG.intersection(r, new RrCSG(new RrHalfPlane(p, pold)));
//			pold = p;
//		}
//		
//		// Horrid hacks in multipliers next...
//		return new RrCSGPolygon(r, new RrBox(new Rr2Point(hexX - hexSize*0.57, hexY - hexSize*0.61), 
//				new Rr2Point(hexX + hexSize*0.537, hexY + hexSize*0.623)));
//	}
	
//	/**
//	 * @return Adrian's testshape; a 3D object
//	 */
//	public RrCSGPolygon adriansTestShape()
//	{
//		Rr2Point p = new Rr2Point(3, 5);
//		Rr2Point q = new Rr2Point(7, 27);
//		Rr2Point r = new Rr2Point(32, 30);
//		Rr2Point s = new Rr2Point(31, 1);
//		
//		Rr2Point pp = new Rr2Point(12, 21);
//		Rr2Point qq = new Rr2Point(18, 32);
//		Rr2Point rr = new Rr2Point(15, 17);    
//		
//		RrHalfPlane ph = new RrHalfPlane(p, q);
//		RrHalfPlane qh = new RrHalfPlane(q, r);
//		RrHalfPlane rh = new RrHalfPlane(r, s);
//		RrHalfPlane sh = new RrHalfPlane(s, p);
//		
//		RrHalfPlane pph = new RrHalfPlane(pp, qq);
//		RrHalfPlane qqh = new RrHalfPlane(qq, rr);
//		RrHalfPlane rrh = new RrHalfPlane(rr, pp);
//		
//		RrCSG pc = new RrCSG(ph);
//		RrCSG qc = new RrCSG(qh);
//		RrCSG rc = new RrCSG(rh);
//		RrCSG sc = new RrCSG(sh);
//		
//		pc = RrCSG.intersection(pc, qc);
//		rc = RrCSG.intersection(sc, rc);		
//		pc = RrCSG.intersection(pc, rc);
//		
//		RrCSG ppc = new RrCSG(pph);
//		RrCSG qqc = new RrCSG(qqh);
//		RrCSG rrc = new RrCSG(rrh);
//		
//		ppc = RrCSG.intersection(ppc, qqc);
//		ppc = RrCSG.intersection(ppc, rrc);
//		ppc = RrCSG.difference(pc, ppc);
//		
//		pc = ppc.offset(-5);
//		ppc = RrCSG.difference(ppc, pc);
//		
//		RrCSGPolygon result = new RrCSGPolygon(ppc, new 
//				RrBox(new Rr2Point(0,0), new Rr2Point(110,110)));
////		result.divide(1.0e-6, 1);
////		new RrGraphics(result, true);
//		return result;
//	}
	
	/**
	 * @throws Exception
	 */
//	public void produce(boolean testPiece) throws Exception {
	public void produce() throws Exception {

        // Fallback defaults
		//int extrusionSpeed = 200;
		//int extrusionTemp = 40;
		//int movementSpeedXY = 230;
		//int fastSpeedXY = 230;
		//int movementSpeedZ = 230;
		
		//int coolingPeriod = Preferences.loadGlobalInt("CoolingPeriod");
		boolean subtractive = Preferences.loadGlobalBool("Subtractive");
		
//		try {
//			extrusionSpeed = Preferences.loadGlobalInt("ExtrusionSpeed");
//			extrusionTemp = Preferences.loadGlobalInt("ExtrusionTemp");
//			movementSpeedXY = Preferences.loadGlobalInt("MovementSpeed");
//			fastSpeedXY = Preferences.loadGlobalInt("FastSpeed");
//			movementSpeedZ = Preferences.loadGlobalInt("MovementSpeedZ");
//		} catch (Exception ex) {
//			System.out.println("Warning: could not load ExtrusionSpeed/MovementSpeed, using defaults");
//		}
		

//		reprap.setSpeed(movementSpeedXY);
//		reprap.setFastSpeed(fastSpeedXY);
//		reprap.setSpeedZ(movementSpeedZ);
		System.out.println("Intialising reprap");
		reprap.initialise();
		System.out.println("Selecting material 0");
		reprap.selectMaterial(0);
		//reprap.setExtruderSpeed(extrusionSpeed);
		System.out.println("Setting temperature");
		reprap.getExtruder().heatOn();
		
		// A "warmup" segment to get things in working order
		if (!subtractive) {
			System.out.println("Printing warmup segments, moving to (0,5)");
			reprap.setSpeed(reprap.getFastSpeed());
			reprap.moveTo(0, 5, reprap.getExtruder().getExtrusionHeight(), true, false);
			reprap.setSpeed(reprap.getExtruder().getXYSpeed());
			System.out.println("Printing warmup segments, printing to (0,20)");
			reprap.printTo(0, 20, reprap.getExtruder().getExtrusionHeight(), false);
			System.out.println("Printing warmup segments, printing to (2,20)");
			reprap.printTo(2, 20, reprap.getExtruder().getExtrusionHeight(), false);
			System.out.println("Printing warmup segments, printing to (2,5)");
			reprap.printTo(2, 5, reprap.getExtruder().getExtrusionHeight(), true);
			reprap.setSpeed(reprap.getFastSpeed());
		}
		
		// This should now split off layers one at a time
		// and pass them to the LayerProducer.  
		
		boolean isEvenLayer = true;
		STLSlice stlc;
		double zMax;
//		if(testPiece)
//		{
//			stlc = null;
//			zMax = 5;
//		} else
//		{
			bld.mouseToWorld();
			stlc = new STLSlice(bld.getSTLs());
			zMax = stlc.maxZ();
			// zMax = 1.6;  // For testing.
//		}
		
		double startZ;
		double endZ;
		double stepZ;
		if (subtractive) {
			// Subtractive construction works from the top, downwards
			startZ = zMax;
			endZ = 0;
			stepZ = -reprap.getExtruder().getExtrusionHeight();
			reprap.setZManual(startZ);
		} else {
			// Normal constructive fabrication, start at the bottom and work up.
			// Note that we start extruding one layer off the baseboard...
			startZ = reprap.getExtruder().getExtrusionHeight();
			endZ = zMax;
			stepZ = reprap.getExtruder().getExtrusionHeight();
		}
		
		for(double z = startZ; subtractive ? z > endZ : z < endZ; z += stepZ) {
			
			if (reprap.isCancelled())
				break;
			System.out.println("Commencing layer at " + z);

			// Change Z height
			reprap.moveTo(reprap.getX(), reprap.getY(), z, false, false);

			// Layer cooling phase - after we've just raised the head.
			//Only if we're not a null device.
			if ((z != startZ && reprap.getExtruder().getCoolingPeriod() > 0)&&!(reprap instanceof NullCartesianMachine)) {
				System.out.println("Starting a cooling period");
				// Save where we are. We'll come back after we've cooled off.
				double storedX=reprap.getX();
				double storedY=reprap.getY();
				reprap.getExtruder().setCooler(true);	// On with the fan.
				//reprap.homeToZeroX();		// Seek (0,0)
				//reprap.homeToZeroY();
				reprap.setSpeed(reprap.getFastSpeed());
				reprap.moveTo(0, 0, z, true, true);
				Thread.sleep(1000 * reprap.getExtruder().getCoolingPeriod());
				reprap.getExtruder().setCooler(false);
				System.out.println("Brief delay for head to warm up.");
				Thread.sleep(200 * reprap.getExtruder().getCoolingPeriod());
				System.out.println("End of cooling period");
				// TODO: BUG! Strangely, this only restores Y axis!
				//System.out.println("stored X and Y: " + storedX + "   " + storedY);
				
				// The next layer will go where it wants to.
				
				//reprap.moveTo(storedX, storedY, z, true, true);
				//reprap.setSpeed(reprap.getExtruder().getXYSpeed());
				//reprap.moveTo(storedX, reprap.getY(), z, true, true);
			}
			
			if (reprap.isCancelled())
				break;

			LayerProducer layer;
//			if(testPiece)
//			{
//				layer = new LayerProducer(reprap, z, hex(), null,
//						isEvenLayer?evenHatchDirection:oddHatchDirection);
//			} else
//			{
				RrCSGPolygon slice = stlc.slice(z+reprap.getExtruder().getExtrusionHeight()*0.5, 
						LayerProducer.solidMaterial(), LayerProducer.gapMaterial(), bld.getObjectColour());
				Shape3D lowerShell = stlc.getShape3D();
				if(slice != null)
					layer = new LayerProducer(reprap, z, slice, lowerShell,
						isEvenLayer?evenHatchDirection:oddHatchDirection);
				else
					layer = null;

//			}
			
			if(layer != null)
				layer.plot();
		
			isEvenLayer = !isEvenLayer;
		}

		if (subtractive)
			reprap.moveTo(0, 0, startZ, true, true);
		else
			reprap.moveTo(0, 0, reprap.getZ(), true, true);
		
		reprap.terminate();

	}

	/**
	 * The total distance moved is the total distance extruded plus 
	 * plus additional movements of the extruder when no materials 
	 * was deposited
	 * 
	 * @return total distance the extruder has moved 
	 */
	public double getTotalDistanceMoved() {
		return reprap.getTotalDistanceMoved();
	}
	
	/**
	 * @return total distance that has been extruded in millimeters
	 */
	public double getTotalDistanceExtruded() {
		return reprap.getTotalDistanceExtruded();
	}
	
	/**
	 * TODO: This figure needs to get added up as we go along to allow for different extruders
	 * @return total volume that has been extruded
	 */
	public double getTotalVolumeExtruded() {
		return reprap.getTotalDistanceExtruded() * reprap.getExtruder().getExtrusionHeight() * 
		reprap.getExtruder().getExtrusionSize();
	}
	
	/**
	 * 
	 */
	public void dispose() {
		reprap.dispose();
	}

	/**
	 * @return total elapsed time in seconds between start and end of building the 3D object
	 */
	public double getTotalElapsedTime() {
		return reprap.getTotalElapsedTime();
	}
	
}