BowlerKernel
PrintBedManager.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.printbed;
2 
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.lang.reflect.Type;
8 import java.nio.file.Files;
9 import java.util.ArrayList;
10 import java.util.Arrays;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
14 
15 import org.apache.commons.io.FileUtils;
16 import org.eclipse.jgit.api.errors.GitAPIException;
17 import org.eclipse.jgit.api.errors.InvalidRemoteException;
18 import org.eclipse.jgit.api.errors.TransportException;
19 
20 import com.google.gson.Gson;
21 import com.google.gson.GsonBuilder;
22 import com.google.gson.reflect.TypeToken;
23 import com.neuronrobotics.bowlerstudio.creature.UserManagedPrintBedData;
24 import com.neuronrobotics.bowlerstudio.physics.TransformFactory;
25 import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine;
26 import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR;
27 
28 import eu.mihosoft.vrl.v3d.CSG;
29 import eu.mihosoft.vrl.v3d.Cube;
30 import eu.mihosoft.vrl.v3d.Transform;
31 import javafx.scene.paint.Color;
32 
33 public class PrintBedManager {
34 
36  Type type = new TypeToken<UserManagedPrintBedData>() {
37  }.getType();
38  Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
39  public static String file = "printbed.json";
40  List<Color> colors = Arrays.asList(Color.WHITE, Color.GREY, Color.BLUE, Color.TAN);
41  private String url;
42  HashMap<Integer, CSG> bedReps = new HashMap<Integer, CSG>();
43  ArrayList<PrintBedObject> objects = new ArrayList<PrintBedObject>();
44  private ArrayList<CSG> parts;
45  private HashSet<String> names = new HashSet<String>();
46  private boolean hasPrintBed = false;
47  public PrintBedManager(String url, ArrayList<CSG> parts) {
48  this.url=url;
49  File dir = new File(ScriptingEngine.getRepositoryCloneDirectory(url).getAbsolutePath());
50  init(dir, parts);
51 
52  }
53 
54  public PrintBedManager(File dir, ArrayList<CSG> parts) {
55  try {
56  this.url=ScriptingEngine.locateGitUrl(dir);
57  } catch (IOException e) {
58  // TODO Auto-generated catch block
59  e.printStackTrace();
60  }
61  setHasPrintBed(init(dir, parts));
62  }
63  public boolean init(File dir, ArrayList<CSG> parts) {
64  this.parts = parts;
65  if (url == null)
66  return false;
67  File f = new File(dir.getAbsolutePath() + "/" + file);
68  System.out.println("Searching for printbed at "+f);
69  if (f.exists()) {
70  System.out.println("Print Bed file found! "+f.getAbsolutePath());
71  String source;
72  byte[] bytes;
73  try {
74  bytes = Files.readAllBytes(f.toPath());
75  source = new String(bytes, "UTF-8");
76  database = gson.fromJson(source, type);
77  } catch (Exception ex) {
78  ex.printStackTrace();
79  }
80 
81  }else {
82  System.out.println("Print Bed NOT file found! "+f.getAbsolutePath());
83  return false;
84  }
85  if (database == null) {
87  database.init();
88  }
89  names.clear();
90  for (CSG bit : parts) {
91  int index = bit.getPrintBedIndex();
92  int colorIndex = index % 4;
93  double zrot = -90 * (index);
94  double yval = index > 4 ? database.bedX * (index - 4) : 0;
95 
96  CSG bed = new Cube(database.bedX, database.bedY, 1).toCSG().toXMin().toYMin().toZMax().rotz(zrot)
97  .movey(yval);
98  bed.setColor(colors.get(colorIndex));
99  bedReps.put(index, bed);
100  String name = bit.getName();
101 
102  CSG prepedBit = bit.prepForManufacturing();
103  if (prepedBit != null && name.length() > 0) {
104  if(names.contains(name))
105  continue;
106  names.add(name);
107  System.out.println(bit.getName() + " on " + index + " rot " + zrot + " y " + yval);
108  if (database.locations.get(name) == null) {
109  database.locations.put(name, new TransformNR());
110  }
111  PrintBedObject obj = new PrintBedObject(name, prepedBit, bed.getMaxX(), bed.getMinX(), bed.getMaxY(),
112  bed.getMinY(), database.locations.get(name));
113  objects.add(obj);
114  obj.addSaveListener(() -> {
115  obj.checkBounds();
116  save();
117  });
118  }
119  }
120  return true;
121  }
122 
123  public ArrayList<CSG> makePrintBeds() {
124  if (url == null)
125  return parts;
126  HashMap<Integer, ArrayList<CSG>> beds = new HashMap<>();
127  names.clear();
128  for (CSG bit : parts) {
129  ArrayList<String> formats = bit.getExportFormats();
130  String name = bit.getName();
131  int index = bit.getPrintBedIndex();
132  bit = bit.prepForManufacturing();
133  if (bit != null && name.length()>0) {
134  if(names.contains(name))
135  continue;
136  names.add(name);
137  if(bit.getMinZ()<0)
138  bit=bit.toZMin();
139  if (beds.get(index) == null) {
140  beds.put(index, new ArrayList<CSG>());
141  }
142  TransformNR location = database.locations.get(name);
143  if (location != null) {
144  Transform csfMove = TransformFactory.nrToCSG(location);
145  bit = bit.transformed(csfMove);
146  }
147  if (formats != null)
148  for (String s : formats)
149  bit.addExportFormat(s);
150  bit.setName(name);
151  beds.get(index).add(bit);
152  }
153  }
154  ArrayList<CSG> bedsOutputs = new ArrayList<CSG>();
155  for (Integer i : beds.keySet()) {
156  String name = "Print-Bed-" + i;
157  ArrayList<CSG> bedComps = beds.get(i);
158  CSG bed = null;
159  for (CSG p : bedComps) {
160  bedsOutputs.add(p);
161  ArrayList<String> formats = p.getExportFormats();
162  if (bed == null)
163  bed = p;
164  else {
165  bed = bed.dumbUnion(p);
166  }
167  if (formats != null)
168  for (String s : formats)
169  bed.addExportFormat(s);
170  }
171  if (bed != null) {
172  //System.out.println("Mesh fixing for "+name);
173  //bed=bed.union(bed);
174  bed.setName(name);
175  }
176  else {
177  bed = new Cube().toCSG().toZMin();
178  bed.setManufacturing(incoming -> null);
179  }
180  bedsOutputs.add(bed);
181  }
182 
183  return bedsOutputs;
184  }
185 
186  public ArrayList<CSG> get() {
187  ArrayList<CSG> back = new ArrayList<>();
188  for (CSG c : bedReps.values()) {
189  back.add(c);
190  }
191  for (PrintBedObject pbo : objects) {
192  back.addAll(pbo.get());
193  }
194  return back;
195  }
196 
197  private synchronized void saveLocal() {
198  String content = gson.toJson(database);
199  try {
200  write(file,content);
201 // ScriptingEngine.commit(url, ScriptingEngine.getBranch(url), file, content, "Save Print Bed Locations",
202 // true);
203  } catch (Exception e) {
204  // TODO Auto-generated catch block
205  e.printStackTrace();
206  }
207  }
208  private void write(String file, String content)
209  throws InvalidRemoteException, TransportException, GitAPIException, IOException {
210  File f = ScriptingEngine.fileFromGit(url, file);
211  if (!f.exists()) {
212  f.createNewFile();
213  }
214  BufferedWriter writer = new BufferedWriter(new FileWriter(f.getAbsolutePath()));
215  writer.write(content);
216  writer.close();
217  }
218  private void save() {
219  new Thread(() -> {
220  saveLocal();
221  }).start();
222  }
223 
227  public boolean hasPrintBed() {
228  return hasPrintBed;
229  }
230 
234  public void setHasPrintBed(boolean hasPrintBed) {
235  this.hasPrintBed = hasPrintBed;
236  }
237 
238 }
static eu.mihosoft.vrl.v3d.Transform nrToCSG(TransformNR nr)
boolean init(File dir, ArrayList< CSG > parts)
static File fileFromGit(String remoteURI, String fileInRepo)
CSG toYMin(CSG target)
Definition: CSG.java:338
CSG dumbUnion(CSG csg)
Definition: CSG.java:763
CSG setName(String name)
Definition: CSG.java:2382
void addExportFormat(String exportFormat)
Definition: CSG.java:2420
CSG rotz(Number degreesToRotate)
Definition: CSG.java:550
CSG prepForManufacturing()
Definition: CSG.java:179
CSG toXMin(CSG target)
Definition: CSG.java:318
CSG movey(Number howFarToMove)
Definition: CSG.java:429
CSG setManufacturing(PrepForManufacturing manufactuing)
Definition: CSG.java:2225
ArrayList< String > getExportFormats()
Definition: CSG.java:2408
CSG setColor(Color color)
Definition: CSG.java:207
CSG toZMin(CSG target)
Definition: CSG.java:298
CSG toZMax(CSG target)
Definition: CSG.java:308