package org.reprap.geometry;

import org.reprap.Printer;
import org.reprap.geometry.polygons.Rr2Point;
import org.reprap.geometry.polygons.RrLine;
import org.reprap.geometry.polygons.RrPolygon;
import org.reprap.geometry.polygons.RrPolygonList;
import org.reprap.gui.PreviewPanel;
import org.reprap.gui.RepRapBuild;
import org.reprap.machines.MachineFactory;

public class Producer {
	
	static private final double layerSpacing = 0.3; ///< Vertical spacing in mm

	private Printer reprap;
	private RrLine oddHatchDirection;
	private RrLine evenHatchDirection;
	
	public Producer(PreviewPanel preview, RepRapBuild builder) throws Exception {
		reprap = MachineFactory.create();
		reprap.setPreviewer(preview);
		
		oddHatchDirection = new RrLine(new Rr2Point(0.0, 0.0), new Rr2Point(1.0, 1.0));
		evenHatchDirection = new RrLine(new Rr2Point(0.0, 1.0), new Rr2Point(1.0, 0.0));
	}
	
	public void produce() throws Exception {
	
		reprap.initialise();
		reprap.selectMaterial(0);
		reprap.setSpeed(230);
		reprap.setExtruderSpeed(180);
		reprap.setTemperature(40);

		// This should now split off layers one at a time
		// and pass them to the LayerProducer.  At the moment,
		// we just construct a simple test layer and produce that.

		boolean isEvenLayer = true;
		for(double z = 0.0; z < 5.0; z += layerSpacing) {
			if (reprap.isCancelled())
				break;
			
			Rr2Point p1 = new Rr2Point(10, 10);
			Rr2Point p2 = new Rr2Point(20, 10);
			Rr2Point p3 = new Rr2Point(20, 20);
			Rr2Point p4 = new Rr2Point(10, 20);
	
			RrPolygon a = new RrPolygon();
			a.append(p1, 1);
			a.append(p2, 1);
			a.append(p3, 1);
			a.append(p4, 1);
	
			RrPolygonList list = new RrPolygonList();
			list.append(a);
	
			LayerProducer layer = new LayerProducer(reprap, list,
					isEvenLayer?evenHatchDirection:oddHatchDirection);
			layer.plot();
			
			reprap.moveTo(reprap.getX(), reprap.getY(), z);
			
			isEvenLayer = !isEvenLayer;
		}
		
		reprap.terminate();

	}
	
	public void dispose() {
		reprap.dispose();
	}
	
}

@@ -1309,16 +1309,71 @@
 cing) %7B%0A
+%09%09%09reprap.moveTo(reprap.getX(), reprap.getY(), z);%0A%09%09%09%0A
 %09%09%09if (r
@@ -1886,63 +1886,8 @@
 %09%09%09%0A
-%09%09%09reprap.moveTo(reprap.getX(), reprap.getY(), z);%0A%09%09%09%0A
 %09%09%09i

Fix to prevent two layers printing to start with