1 package eu.mihosoft.vrl.v3d.svg;
3 import java.io.BufferedWriter;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.Arrays;
10 import eu.mihosoft.vrl.v3d.CSG;
11 import eu.mihosoft.vrl.v3d.Polygon;
12 import eu.mihosoft.vrl.v3d.Slice;
13 import eu.mihosoft.vrl.v3d.Transform;
14 import eu.mihosoft.vrl.v3d.Vector3d;
15 import eu.mihosoft.vrl.v3d.Vertex;
17 @SuppressWarnings(
"restriction")
19 private String footer =
"</svg>";
20 private String section =
"";
23 private static final double Scale = 3.543307;
24 private static final double VueBoxSize = 100;
25 private int colorTicker=0;
26 public static List<String> colorNames = Arrays.asList(
"crimson",
"gray",
"darkmagenta",
"darkolivegreen",
"darkgreen",
35 double min[] = { 0, 0 };
36 double max[] = { VueBoxSize, VueBoxSize };
37 private ArrayList<String> polylines=
new ArrayList<>() ;
38 private ArrayList<String> groups=
new ArrayList<>() ;
39 private ArrayList<String> layers=
new ArrayList<>() ;
40 private int layerCounter = 1;
41 private int groupCounter = 1;
42 private int lineCounter=0;
43 private String name=
"";
53 double totalX = Math.abs(max[0]) + Math.abs(min[0]);
54 double totalY = Math.abs(max[1]) + Math.abs(min[1]);
55 double totalXmm = totalX/Scale;
56 double totalYmm = totalY/Scale;
57 String header =
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
58 +
"<svg xmlns:dc=\"http://purl.org/dc/elements/1.1/\""+
59 " xmlns:cc=\"http://creativecommons.org/ns#\""+
60 " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\""+
61 " xmlns:svg=\"http://www.w3.org/2000/svg\""+
62 " xmlns=\"http://www.w3.org/2000/svg\""+
63 " xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\""+
64 " xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" version=\"1.1\"\n viewBox=\"" + (0 ) +
" "
65 + (0) +
" " + (totalX) +
" "+
68 "\n width=\""+totalXmm+
"mm\""+
69 "\n height=\""+totalYmm+
"mm\""+
72 " id=\"defs4\" /> \n"+
73 " <sodipodi:namedview \n"+
75 " pagecolor=\"#ffffff\" \n"+
76 " bordercolor=\"#666666\" \n"+
77 " borderopacity=\"1.0\" \n"+
78 " inkscape:pageopacity=\"0.0\" \n"+
79 " inkscape:pageshadow=\"2\" \n"+
80 " inkscape:document-units=\"mm\" \n"+
81 " inkscape:current-layer=\"layer1\" \n"+
82 " showgrid=\"false\" \n"+
86 output = header + output;
92 if(colorTicker==colorNames.size())
98 if(polylines.size()==0)
100 String groupsLine=
"<g\nid=\"g37"+groupCounter+
"\">\n"
102 for(String p:polylines){
107 groups.add(groupsLine);
114 String groupsLine=
"<g\n"+
115 "inkscape:label=\""+name+
"Slice "+layerCounter+
"\"\n"+
116 "inkscape:groupmode=\"layer\" \n"+
117 "id=\"layer"+layerCounter+
"\" \n"+
120 for(String p:groups){
125 layers.add(groupsLine);
131 String color = colorNames.get(colorTicker);
132 String section =
" <polyline points=\"";
136 double x = (position.
x * Scale);
137 double y = (position.
y * Scale)+VueBoxSize;
138 section += x +
"," + y +
" ";
142 double x = (position.
x * Scale);
143 double y = (position.
y * Scale)+VueBoxSize;
144 section += x +
"," + y +
" ";
145 section= section +
"\" \nstroke=\""+color+
"\" \nstroke-width=\"1\" \nfill=\"none\"\nid=\"line"+(lineCounter++)+
"\" />\n";
146 polylines.add(section);
152 public static void export(List<Polygon> polygons, File defaultDir,
boolean groupAll)
throws IOException {
161 write(svg.
make(), defaultDir);
163 private static void write(String output, File defaultDir)
throws IOException{
165 if (!defaultDir.exists()) {
166 defaultDir.createNewFile();
168 FileWriter fw =
new FileWriter(defaultDir.getAbsoluteFile());
169 BufferedWriter bw =
new BufferedWriter(fw);
173 public static void export(
CSG currentCsg, File defaultDir)
throws IOException {
175 addCsg(currentCsg,svg);
176 write(svg.
make(), defaultDir);
178 public static void export(List<CSG> currentCsg, File defaultDir)
throws IOException {
180 eu.mihosoft.vrl.v3d.JavaFXInitializer.go();
181 }
catch (Throwable t) {
183 System.err.println(
"ERROR No UI engine availible");
185 if (!eu.mihosoft.vrl.v3d.JavaFXInitializer.errored) {
188 long start = System.currentTimeMillis();
189 for (
CSG tmp : currentCsg) {
190 System.out.println(
"Slicing CSG " + tmp.getName() +
" " + (i + 1) +
" of " + (currentCsg.size()));
195 write(svg.
make(), defaultDir);
196 System.out.println(
"Finished slicing CSGs took "
197 + ((((
double) (System.currentTimeMillis() - start))) / 1000.0) +
" seconds");
199 System.err.println(
"ERROR No UI engine availible, SVG slicing is GPU accelerated and will not work");
203 svg.setName(currentCsg.getName());
204 for(
Transform slicePlane:currentCsg.getSlicePlanes()){
205 List<Polygon> polygons =
Slice.
slice(currentCsg.prepMfg(), slicePlane, 0);
final List< Vertex > vertices
static List< Polygon > slice(CSG incoming, Transform slicePlane, double normalInsetDistance)
static Vector3d y(double y)
Vector3d transformed(Transform transform)
static Vector3d x(double x)
static void export(List< Polygon > polygons, File defaultDir, boolean groupAll)
static void export(List< CSG > currentCsg, File defaultDir)
void setName(String name)
static void addCsg(CSG currentCsg, SVGExporter svg)
static void write(String output, File defaultDir)
void toPolyLine(Polygon p)
static void export(CSG currentCsg, File defaultDir)