Skip to content

Commit

Permalink
Merge pull request #255 from WesGilster/master
Browse files Browse the repository at this point in the history
Customizers worked only in the preview
  • Loading branch information
WesGilster authored Sep 20, 2016
2 parents fe84a87 + d662e8a commit 1537614
Showing 16 changed files with 157 additions and 51 deletions.
29 changes: 23 additions & 6 deletions host/resourcesnew/cwh/js/printables.js
Original file line number Diff line number Diff line change
@@ -34,19 +34,26 @@
};

this.changeCurrentPrintable = function changeCurrentPrintable(newPrintable) {
var newCustomizerName = newPrintable.name + "." + newPrintable.extension + "." + controller.currentPrinter.configuration.name;
var currentPrinterName = controller.currentPrinter != null?controller.currentPrinter.configuration.name:null;
var newCustomizerName = newPrintable.name + "." + newPrintable.extension + (currentPrinterName != null?"." + currentPrinterName: "");
if (controller.currentPrintable != null && newPrintable.name == controller.currentPrintable.name && newPrintable.extension == controller.currentPrintable.extension) {
return;
}

controller.currentPrintable = newPrintable;
controller.errorMsg = null;

if (currentPrinterName == null) {
controller.refreshCurrentPrinter();
return;
}

$http.get("services/customizers/get/" + newCustomizerName + "?externalState=" + cacheControl.previewExternalStateId).success(
function (data) {
if (data == "") {
controller.currentCustomizer = {
name: newCustomizerName,
printerName: controller.currentPrinter.configuration.name,
printerName: currentPrinterName,
printableName: newPrintable.name,
printableExtension: newPrintable.extension,
supportsAffineTransformSettings: true,
@@ -159,17 +166,27 @@
}

this.setProjectImage = function setProjectImage(projectImage) {
controller.projectImage = projectImage;
var serviceCall = null;
if (projectImage) {
$http.get("services/customizers/projectCustomizerOnPrinter/" + encodeURIComponent(controller.currentCustomizer.name));
serviceCall = "services/customizers/projectCustomizerOnPrinter/" + encodeURIComponent(controller.currentCustomizer.name);
} else {
$http.get("services/printers/showBlankScreen/" + encodeURIComponent(controller.currentPrinter.configuration.name));
if (controller.currentPrinter == null) {
return;
}

serviceCall = "services/printers/showBlankScreen/" + encodeURIComponent(controller.currentPrinter.configuration.name);
}

$http.get(serviceCall).success(function (data) {
controller.projectImage = projectImage;
});
}

this.resetTranslation = function resetTranslation() {
controller.currentCustomizer.zscale = 1.0;

controller.currentCustomizer.nextSlice = 0;
controller.currentCustomizer.nextStep = "PerformHeader";

var affineTransformSettings = controller.currentCustomizer.affineTransformSettings;
affineTransformSettings.xtranslate = 0;
affineTransformSettings.ytranslate = 0;
4 changes: 2 additions & 2 deletions host/resourcesnew/printables.html
Original file line number Diff line number Diff line change
@@ -164,12 +164,12 @@ <h6>3d Scale: {{printablesController.currentCustomizer.zscale}}</h6>
<h4>Rotation</h4>
<h6>Degrees: {{printablesController.currentCustomizer.affineTransformSettings.rotation}}</h6>
<span class="btn-group" >
<b class="btn btn-sm btn-danger fa fa-angle-double-down" ng-click="printablesController.changeRotation(-20)">20</b>
<b class="btn btn-sm btn-danger fa fa-angle-double-down" ng-click="printablesController.changeRotation(-45)">45</b>
<a class="btn btn-sm btn-danger fa fa-angle-down" ng-click="printablesController.changeRotation(-10)">10</a>
<a class="btn btn-sm btn-danger fa fa-angle-down" ng-click="printablesController.changeRotation(-1)">1</a>
<a class="btn btn-sm btn-success fa fa-angle-up" ng-click="printablesController.changeRotation(1)">1</a>
<a class="btn btn-sm btn-success fa fa-angle-up" ng-click="printablesController.changeRotation(10)">10</a>
<a class="btn btn-sm btn-success fa fa-angle-double-up" ng-click="printablesController.changeRotation(20)">20</a>
<a class="btn btn-sm btn-success fa fa-angle-double-up" ng-click="printablesController.changeRotation(45)">45</a>
</span>
</div>

Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -17,18 +18,21 @@
import javax.script.ScriptEngine;
import javax.script.ScriptException;

import org.apache.commons.io.FilenameUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.area515.resinprinter.display.InappropriateDeviceException;
import org.area515.resinprinter.exception.NoPrinterFoundException;
import org.area515.resinprinter.exception.SliceHandlingException;
import org.area515.resinprinter.job.Customizer.PrinterStep;
import org.area515.resinprinter.job.render.RenderingCache;
import org.area515.resinprinter.notification.NotificationManager;
import org.area515.resinprinter.printer.Printer;
import org.area515.resinprinter.printer.PrinterConfiguration;
import org.area515.resinprinter.printer.SlicingProfile;
import org.area515.resinprinter.printer.SlicingProfile.InkConfig;
import org.area515.resinprinter.server.HostProperties;
import org.area515.resinprinter.services.CustomizerService;
import org.area515.resinprinter.services.PrinterService;
import org.area515.resinprinter.slice.StlError;
import org.area515.util.Log4jTimer;
@@ -57,9 +61,7 @@ public static class DataAid {
public boolean optimizeWithPreviewMode;
public AffineTransform affineTransform = new AffineTransform();
public RenderingCache cache = new RenderingCache();

//should have affine transform matrix calculated here
//store Affine Transform Object here
public Customizer customizer;

public DataAid(PrintJob printJob) throws JobManagerException {
this.printJob = printJob;
@@ -73,12 +75,23 @@ public DataAid(PrintJob printJob) throws JobManagerException {
yPixelsPerMM = slicingProfile.getDotsPermmY();
xResolution = slicingProfile.getxResolution();
yResolution = slicingProfile.getyResolution();
this.optimizeWithPreviewMode = false;

// Set the affine transform given the customizer from the printJob
Customizer customizer = printJob.getCustomizer();
optimizeWithPreviewMode = false;
customizer = printJob.getCustomizer();
if (customizer != null) {
this.affineTransform = customizer.createAffineTransform(xResolution, yResolution);
} else {
customizer = new Customizer();
customizer.setNextStep(PrinterStep.PerformHeader);
customizer.setNextSlice(0);
customizer.setPrintableName(FilenameUtils.getBaseName(printJob.getJobFile().getName()));
customizer.setPrintableExtension(FilenameUtils.getExtension(printJob.getJobFile().getName()));
customizer.setPrinterName(printer.getName());
customizer.setName(printJob.getJobFile().getName() + "." + printer.getName());
Customizer otherCustomizer = CustomizerService.INSTANCE.getCustomizers().get(customizer.getName());
if (otherCustomizer != null) {
customizer.setName(customizer.getName() + "." + System.currentTimeMillis());
}
CustomizerService.INSTANCE.addOrUpdateCustomizer(customizer);
}

//This file processor requires an ink configuration
@@ -140,6 +153,11 @@ public final DataAid initializeJobCacheWithDataAid(PrintJob printJob) throws Ina
return aid;
}

private void moveToNextPrinterStep(Customizer customizer, PrinterStep newState) {
customizer.setNextStep(newState);
CustomizerService.INSTANCE.addOrUpdateCustomizer(customizer);
}

public void performHeader(DataAid aid) throws InappropriateDeviceException, IOException {
if (aid == null) {
throw new IllegalStateException("initializeDataAid must be called before this method");
@@ -153,8 +171,11 @@ public void performHeader(DataAid aid) throws InappropriateDeviceException, IOEx
aid.printJob.setExposureTime(aid.inkConfiguration.getExposureTime());

//Perform the gcode associated with the printer start function
if (aid.slicingProfile.getgCodeHeader() != null && aid.slicingProfile.getgCodeHeader().trim().length() > 0) {
if (aid.slicingProfile.getgCodeHeader() != null &&
aid.slicingProfile.getgCodeHeader().trim().length() > 0 &&
aid.customizer.getNextStep() == PrinterStep.PerformHeader) {
aid.printer.getGCodeControl().executeGCodeWithTemplating(aid.printJob, aid.slicingProfile.getgCodeHeader());
moveToNextPrinterStep(aid.customizer, PrinterStep.PerformPreSlice);
}

if (aid.inkConfiguration != null) {
@@ -174,10 +195,11 @@ public JobStatus performPreSlice(DataAid aid, List<StlError> errors) throws Inap
if (aid == null) {
throw new IllegalStateException("initializeDataAid must be called before this method");
}

aid.currentSliceTime = System.currentTimeMillis();

//Show the errors to our users if the stl file is broken, but we'll keep on processing like normal
if (errors != null && !errors.isEmpty()) {
if (errors != null && !errors.isEmpty() && aid.customizer.getNextStep() == PrinterStep.PerformPreSlice) {
NotificationManager.errorEncountered(aid.printJob, errors);
}

@@ -189,10 +211,13 @@ public JobStatus performPreSlice(DataAid aid, List<StlError> errors) throws Inap
}

//Execute preslice gcode
if (aid.slicingProfile.getgCodePreslice() != null && aid.slicingProfile.getgCodePreslice().trim().length() > 0) {
if (aid.slicingProfile.getgCodePreslice() != null &&
aid.slicingProfile.getgCodePreslice().trim().length() > 0 &&
aid.customizer.getNextStep() == PrinterStep.PerformPreSlice) {
aid.printer.getGCodeControl().executeGCodeWithTemplating(aid.printJob, aid.slicingProfile.getgCodePreslice());
}

moveToNextPrinterStep(aid.customizer, PrinterStep.PerformExposure);
return null;
}

@@ -205,6 +230,10 @@ public JobStatus printImageAndPerformPostProcessing(DataAid aid, BufferedImage s
throw new IllegalStateException("You must specify a sliceImage to display");
}

if (aid.customizer.getNextStep() != PrinterStep.PerformExposure) {
return null;
}

//Start but don't wait for a potentially heavy weight operation to determine if we are out of ink.
if (aid.inkDetector != null) {
aid.inkDetector.startMeasurement();
@@ -280,6 +309,8 @@ public JobStatus printImageAndPerformPostProcessing(DataAid aid, BufferedImage s
//Notify the client that the printJob has increased the currentSlice
NotificationManager.jobChanged(aid.printer, aid.printJob);

moveToNextPrinterStep(aid.customizer, PrinterStep.PerformPreSlice);

return null;
}

@@ -352,10 +383,6 @@ public BufferedImage applyImageTransforms(DataAid aid, BufferedImage img) throws
}

BufferedImage after = new BufferedImage(aid.xResolution, aid.yResolution, img.getType());

((Graphics2D)img.getGraphics()).setBackground(Color.black);
((Graphics2D)after.getGraphics()).setBackground(Color.black);

AffineTransformOp transOp = new AffineTransformOp(aid.affineTransform, AffineTransformOp.TYPE_BILINEAR);
after = transOp.filter(img, after);

22 changes: 22 additions & 0 deletions host/src/main/java/org/area515/resinprinter/job/Customizer.java
Original file line number Diff line number Diff line change
@@ -24,6 +24,14 @@ public class Customizer {
private SoftReference<BufferedImage> origSliceCache = null;
private Double zScale = 1.0;
private String cacheId;
private int nextSlice = 0;
private PrinterStep nextStep = PrinterStep.PerformHeader;

public static enum PrinterStep {
PerformHeader,
PerformPreSlice,
PerformExposure,
}

public static class AffineTransformSettings {
private Double xTranslate = 0.0;//negative printerwidth for xflip
@@ -138,6 +146,20 @@ public AffineTransform createAffineTransform(double width, double height) {

}

public int getNextSlice() {
return nextSlice;
}
public void setNextSlice(int nextSlice) {
this.nextSlice = nextSlice;
}

public PrinterStep getNextStep() {
return nextStep;
}
public void setNextStep(PrinterStep nextStep) {
this.nextStep = nextStep;
}

public AffineTransform createAffineTransform(double width, double height) {
return this.affineTransformSettings.createAffineTransform(width, height);
}
Original file line number Diff line number Diff line change
@@ -102,7 +102,7 @@ public PrintJob createJob(File job, final Printer printer, Customizer customizer
}

if (customizer != null) {
newJob.setCustomizer(CustomizerService.INSTANCE.getCustomizer(job.getName(), customizer.getExternalImageAffectingState()));
newJob.setCustomizer(customizer);
logger.info(newJob.getCustomizer());
}

Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ public JobStatus processFile(PrintJob printJob) throws Exception {
//Get the slicer queued up for the first image;
dataAid.slicer.setZIndex(slicer.getZMinIndex());
Object nextRenderingPointer = dataAid.cache.getCurrentRenderingPointer();
Future<RenderedData> currentImage = Main.GLOBAL_EXECUTOR.submit(new STLImageRenderer(dataAid, this, nextRenderingPointer));
Future<RenderedData> currentImage = Main.GLOBAL_EXECUTOR.submit(new STLImageRenderer(dataAid, this, nextRenderingPointer, false));

//Everything needs to be setup in the dataByPrintJob before we start the header
performHeader(dataAid);
@@ -92,7 +92,7 @@ public JobStatus processFile(PrintJob printJob) throws Exception {
logger.info("SliceOverheadStart:{}", ()->Log4jTimer.startTimer(STL_OVERHEAD));

//Wait until the image has been properly rendered. Most likely, it's already done though...
BufferedImage image = currentImage.get().getImage();
BufferedImage image = currentImage.get().getPrintableImage();

logger.info("SliceOverhead:{}", ()->Log4jTimer.completeTimer(STL_OVERHEAD));

@@ -111,7 +111,7 @@ public JobStatus processFile(PrintJob printJob) throws Exception {
//Render the next image while we are waiting for the current image to cure
if (z < slicer.getZMaxIndex() + 1) {
slicer.setZIndex(z);
currentImage = Main.GLOBAL_EXECUTOR.submit(new STLImageRenderer(dataAid, this, nextRenderingPointer));
currentImage = Main.GLOBAL_EXECUTOR.submit(new STLImageRenderer(dataAid, this, nextRenderingPointer, false));
}

//Performs all of the duties that are common to most print files
@@ -139,8 +139,8 @@ public BufferedImage renderPreviewImage(DataAid aid) throws SliceHandlingExcepti
//Get the slicer queued up for the first image;
dataAid.slicer.setZIndex(dataAid.slicer.getZMinIndex());
Object nextRenderingPointer = dataAid.cache.getCurrentRenderingPointer();
STLImageRenderer renderer = new STLImageRenderer(dataAid, this, nextRenderingPointer);
return renderer.call().getImage();
STLImageRenderer renderer = new STLImageRenderer(dataAid, this, nextRenderingPointer, true);
return renderer.call().getPrintableImage();
} catch (IOException | JobManagerException e) {
throw new SliceHandlingException(e);
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
package org.area515.resinprinter.job;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import org.area515.resinprinter.job.AbstractPrintFileProcessor.DataAid;
import org.area515.resinprinter.job.render.CurrentImageRenderer;

public class STLImageRenderer extends CurrentImageRenderer {
public STLImageRenderer(DataAid aid, AbstractPrintFileProcessor<?,?> processor, Object imageIndexToBuild) {
private boolean previewMode;

public STLImageRenderer(DataAid aid, AbstractPrintFileProcessor<?,?> processor, Object imageIndexToBuild, boolean previewMode) {
super(aid, processor, imageIndexToBuild);
this.previewMode = previewMode;
}

@Override
public BufferedImage renderImage(BufferedImage imageToDisplay) {
STLDataAid aid = (STLDataAid)this.aid;
aid.slicer.colorizePolygons(null, null);
if (imageToDisplay == null) {
imageToDisplay = buildLargestImageBetweenPrinterAndRenderedImage((int)aid.slicer.getWidthPixels(), (int)aid.slicer.getHeightPixels());
imageToDisplay = buildImage((int)aid.slicer.getWidthPixels(), (int)aid.slicer.getHeightPixels());
}

Graphics2D g2 = (Graphics2D)imageToDisplay.getGraphics();
aid.slicer.paintSlice(g2);
if (previewMode) {
g2.setColor(Color.RED);
g2.drawRect(0, 0, (int)aid.slicer.getWidthPixels() - 1, (int)aid.slicer.getHeightPixels() - 1);
}

//imageToDisplay.setArea((double)aid.slicer.getBuildArea());//TODO: This won't work if affine transforms are applied afterwards!
return imageToDisplay;
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ public JobStatus processFile(PrintJob printJob) throws Exception {
slicePending = false;
}

status = printImageAndPerformPostProcessing(dataAid, imageData.getImage());
status = printImageAndPerformPostProcessing(dataAid, imageData.getPrintableImage());

if (status != null) {
return status;
@@ -111,7 +111,7 @@ public BufferedImage renderPreviewImage(DataAid dataAid) throws SliceHandlingExc

SimpleImageRenderer renderer = new SimpleImageRenderer(dataAid, this, imageFile);
RenderedData stdImage = renderer.call();
return stdImage.getImage();
return stdImage.getPrintableImage();
} catch (IOException | JobManagerException e) {
throw new SliceHandlingException(e);
}
Loading

0 comments on commit 1537614

Please sign in to comment.