JavaCAD
SVGLoad.java
Go to the documentation of this file.
1 package eu.mihosoft.vrl.v3d.svg;
2 
3 
4 
5 import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
6 import org.apache.batik.anim.dom.SVGOMGElement;
7 import org.apache.batik.anim.dom.SVGOMImageElement;
8 import org.apache.batik.anim.dom.SVGOMPathElement;
9 import org.apache.batik.anim.dom.SVGOMPolylineElement;
10 
11 import java.io.BufferedWriter;
12 import java.io.File;
13 import java.io.FileWriter;
14 import java.io.IOException;
15 import java.net.URI;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 
20 import javax.vecmath.Matrix4d;
21 
22 import org.apache.batik.bridge.BridgeContext;
23 import org.apache.batik.bridge.DocumentLoader;
24 import org.apache.batik.bridge.GVTBuilder;
25 import org.apache.batik.bridge.UserAgent;
26 import org.apache.batik.bridge.UserAgentAdapter;
27 import org.apache.batik.dom.svg.SVGItem;
28 import org.apache.batik.util.XMLResourceDescriptor;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Node;
31 import org.w3c.dom.NodeList;
32 import org.w3c.dom.svg.SVGImageElement;
33 import org.w3c.dom.svg.SVGPathSegList;
34 import org.w3c.dom.svg.SVGPointList;
35 
36 import com.piro.bezier.BezierPath;
37 import eu.mihosoft.vrl.v3d.CSG;
38 import eu.mihosoft.vrl.v3d.Edge;
39 import eu.mihosoft.vrl.v3d.Extrude;
40 import eu.mihosoft.vrl.v3d.Polygon;
41 import eu.mihosoft.vrl.v3d.Transform;
42 import eu.mihosoft.vrl.v3d.Vector3d;
43 import javafx.scene.paint.Color;
44 
45 // CSG.setDefaultOptType(CSG.OptType.CSG_BOUND);
49 public class SVGLoad {
50  private static final String PATH_ELEMENT_NAME = "path";
51  private static final String GROUP_ELEMENT_NAME = "g";
52  private Document svgDocument;
53  boolean hp = true;
54  private HashMap<String,List<Polygon>> polygonByLayers = null;
55  private HashMap<String,ArrayList<CSG>> csgByLayers = new HashMap<String, ArrayList<CSG>>();
56  private HashMap<Polygon,Color> colors=new HashMap<>();
57 // private ArrayList<CSG> sections = null;
58 // private ArrayList<CSG> holes = null;
59 //
60 // private List<Polygon> polygons = null;
61  private ISVGLoadProgress progress = null;
62  private double thickness;
63  private boolean negativeThickness = false;
64  private double height = 0;
65  private double width = 0;
66  private Double scale=null;
67  private HashMap<String,Double> units=new HashMap<>();
68 // static {
69 // units.put("mm", (1/SVGExporter.Scale));
70 // units.put("px", 1.0);
71 // units.put("cm", units.get("mm")/10.0);
72 // units.put("in", units.get("mm")/25.4);
73 // units.put("ft", units.get("in")/12.0);
74 // units.put("m", units.get("mm")/1000.0);
75 //
76 // }
77 
78  private double toPx(String value) {
79 
80  for(String key : units.keySet()) {
81  if(value.endsWith(key)) {
82  String []split = value.split(key);
83  if(key.contentEquals("m")&& split.length>1) {
84  // meters but not meters units
85  break;
86  }
87  //System.out.println("Units set to "+key+" for "+value);
88  return Double.parseDouble(split[0])/ units.get(key);
89  }
90  }
91  return Double.parseDouble(value);
92  }
93  private void setScale(double value) {
94 
95  scale=value;
96  if(scale.isInfinite()||scale.isNaN())
97  throw new RuntimeException("Scale must be real number");
98  units.put("mm", (1/getScale()));
99  units.put("px", 1.0);
100  units.put("cm", units.get("mm")/10.0);
101  units.put("in", units.get("mm")/25.4);
102  units.put("ft", units.get("in")/12.0);
103  units.put("m", units.get("mm")/1000.0);
104  }
105  private Double getScale() {
106  return scale.doubleValue();
107  }
108 
109  private double toMM(String value) {
110  Double px= toPx(value);
111  return px*1/getScale();
112  }
114 
115  @Override
116  public void onShape(CSG newShape) {
117  // TODO Auto-generated method stub
118 
119  }
120  };
121 
122  public void setHolePolarity(boolean p) {
123  hp = p;
124  }
125 
131  class MetaPostPath2 {
132  private SVGOMPathElement pathElement;
133  private String transform;
134 
143  public MetaPostPath2(Node pathNode) {
144  setPathNode(pathNode);
145  }
146 
152  public String toCode() {
153  String sb = "";
154  SVGOMPathElement pathElement = getPathElement();
155  SVGPathSegList pathList = pathElement.getNormalizedPathSegList();
156  // String offset = pathElement.getOwnerSVGElement();
157 
158  int pathObjects = pathList.getNumberOfItems();
159  /*
160  * sb.append( "M "+offset .replaceAll("translate", "") .replaceAll("(", "")
161  * .replaceAll(")", "") +"\n");
162  */
163  // sb.append( "//"+getId()+"\n");
164 
165  for (int i = 0; i < pathObjects; i++) {
166  SVGItem item = (SVGItem) pathList.getItem(i);
167  String itemLine = String.format("%s%n", item.getValueAsString());
168  sb += itemLine;
169  }
170 
171  return sb.toString();
172  }
173 
181  private void setPathNode(Node pathNode) {
182  this.pathElement = (SVGOMPathElement) pathNode;
183  }
184 
191  private SVGOMPathElement getPathElement() {
192  return this.pathElement;
193  }
194  }
195 
204  public SVGLoad(URI uri) throws IOException {
206  }
207 
216  public SVGLoad(File f) throws IOException {
217  setSVGDocument(createSVGDocument(f.toURI()));
218  }
219 
228  public SVGLoad(String data) throws IOException {
229  File tmpsvg = new File(System.getProperty("java.io.tmpdir") + "/" + Math.random());
230  tmpsvg.createNewFile();
231  FileWriter fw = new FileWriter(tmpsvg.getAbsoluteFile());
232  BufferedWriter bw = new BufferedWriter(fw);
233  bw.write(data);
234  bw.close();
235  setSVGDocument(createSVGDocument(tmpsvg.toURI()));
236  tmpsvg.deleteOnExit();
237  }
238 
239  public ArrayList<CSG> extrude(double thickness) throws IOException {
240 
241  return extrude(thickness, 0.005);
242 
243  }
244 
245  public static ArrayList<CSG> extrude(File f, double thickness) throws IOException {
246  return new SVGLoad(f.toURI()).extrude(thickness);
247 
248  }
249 
259  public static HashMap<String,List<Polygon>> toPolygons(File f) throws IOException {
260  return new SVGLoad(f.toURI()).toPolygons();
261 
262  }
263 
264  public HashMap<String,List<Polygon>> toPolygons(double resolution) {
265  if(polygonByLayers==null)
266  try {
267  loadAllGroups(resolution, new Transform());
268  } catch (Exception e) {
269  // TODO Auto-generated catch block
270  e.printStackTrace();
271  }
272 
273  return getPolygonByLayers();
274  }
275 
276  public HashMap<String,List<Polygon>> toPolygons() {
277  return toPolygons(0.001);
278  }
279 
280  public static ArrayList<CSG> extrude(File f, double thickness, double resolution) throws IOException {
281  return new SVGLoad(f.toURI()).extrude(thickness, resolution);
282  }
283 
284  public static ArrayList<CSG> extrude(URI uri, double thickness) throws IOException {
285 
286  return new SVGLoad(uri).extrude(thickness);
287 
288  }
289 
290  public static ArrayList<CSG> extrude(URI uri, double thickness, double resolution) throws IOException {
291  return new SVGLoad(uri).extrude(thickness, resolution);
292  }
293 
294  private void loadAllGroups(double resolution, Transform startingFrame) {
295 
296  NodeList pn = getSVGDocument().getDocumentElement().getChildNodes();// .getElementsByTagName("g");
297  try {
298  String hval = getSVGDocument().getDocumentElement().getAttribute("height");
299  String wval = getSVGDocument().getDocumentElement().getAttribute("width");
300  String viewbox = getSVGDocument().getDocumentElement().getAttribute("viewBox");
301  double viewW = Double.parseDouble(viewbox.split(" ")[2]);
302  setScale( 1);// use to compute bounds
303  height = toMM(hval);
304  width = toMM(wval);
305  double value =viewW/width;
306  //System.out.println("Page size height = "+height+" width ="+width+" with scale "+(int)(value*25.4)+" DPI ");
307  setScale( value);
308  } catch (Throwable t) {
309  t.printStackTrace();
310  height = 0;
311  width = 0;
312  setScale( 3.543307); // Assume 90 DPI and mm
313  }
314  // println "Loading groups from "+pn.getClass()
315  int pnCount = pn.getLength();
316  for (int j = 0; j < pnCount; j++) {
317  Node item = pn.item(j);
318  //System.out.println("\tTOP LEVEL :"+item);
319  if (SVGOMGElement.class.isInstance(item)) {
320 
321  SVGOMGElement element = (SVGOMGElement) item;
322  loadGroup(element, resolution, startingFrame,null);
323  } if(SVGOMPathElement.class.isInstance(item) || SVGOMImageElement.class.isInstance(item)) {
324  try {
325  loadPath(item, resolution, startingFrame,"TOP");
326  } catch (Throwable t) {
327 
328  t.printStackTrace();
329  }
330  }
331  }
332 
333  }
334 
335  private void loadGroup(SVGOMGElement element, double resolution, Transform startingFrame, String encapsulatingLayer) {
336  Node transforms = element.getAttributes().getNamedItem("transform");
337  Transform newFrame = getNewframe(startingFrame, transforms);
338  String layername;
339 
340  try {
341  layername=element.getAttributeNS("http://www.inkscape.org/namespaces/inkscape", "label");
342  if(layername==null|| layername.length()==0)
343  throw new RuntimeException();
344  }catch (Throwable t) {
345  layername=null;
346  }
347  if(layername==null) {
348  layername=encapsulatingLayer;
349  }else {
350  //System.out.println("Updated to layer "+layername+" from "+encapsulatingLayer);
351  }
352  //System.out.println("\tGroup " + element.getAttribute("id") +"\n\t inkscape name: "+layername+ " \n\t root " + newFrame.getX() + " " + newFrame.getY());
353 
354  NodeList children = element.getChildNodes();
355  for (int i = 0; i < children.getLength(); i++) {
356  Node n = children.item(i);
357  if (SVGOMGElement.class.isInstance(n)) {
358  loadGroup((SVGOMGElement) n, resolution, newFrame,layername);
359  } else {
360  //System.out.println("\tNot group:"+n);
361  try {
362  loadPath(n, resolution, newFrame,layername);
363  } catch (Throwable t) {
364 
365  t.printStackTrace();
366  }
367  }
368  }
369  }
370 
371  private Transform getNewframe(Transform startingFrame, Node transforms) {
372  if (transforms == null)
373  return startingFrame;
374  Transform newFrame = new Transform().apply(startingFrame);
375  String transformValue = transforms.getNodeValue();
376  //System.out.println("\tApply " + transformValue + " root " + startingFrame.getX() + " " + startingFrame.getY());
377  if (transformValue.contains("translate")) {
378  String[] transformValues = transformValue.replaceAll("translate", "").replaceAll("\\(", "")
379  .replaceAll("\\)", "").split("\\,");
380  newFrame.apply(new Transform().translate(toPx(transformValues[0]),
381  toPx(transformValues[1]), 0));
382 
383  }else if (transformValue.contains("rotate")) {
384  String[] rotvals = transformValue.replaceAll("rotate", "").replaceAll("\\(", "")
385  .replaceAll("\\)", "").split("\\,");
386  newFrame= startingFrame.inverse()
387  .apply(new Transform().rotZ(-Double.parseDouble(rotvals[0])))
388  .apply(startingFrame);
389 
390  } else if (transformValue.contains("scale")) {
391  String[] transformValues = transformValue.replaceAll("scale", "").replaceAll("\\(", "")
392  .replaceAll("\\)", "").split("\\,");
393  // System.out.println(id.getNodeValue() + " " + transformValues);
394  double scalex = toPx(transformValues[0]);
395  double scaley = toPx(transformValues.length==2?transformValues[1]:transformValues[0]);
396  newFrame.scale(scalex, scaley, 1);
397 
398  } else if (transformValue.contains("matrix")) {
399  String[] transformValues = transformValue.replaceAll("matrix", "").replaceAll("\\(", "")
400  .replaceAll("\\)", "").split("\\,");
401  // System.out.println("Matrix found " +new
402  // ArrayList<>(Arrays.asList(transformValues)));
403  double a = toPx(transformValues[0]);
404  double b = toPx(transformValues[1]);
405  double c = toPx(transformValues[2]);
406  double d = toPx(transformValues[3]);
407  double e = toPx(transformValues[4]);
408  double f = toPx(transformValues[5]);
409  double elemenents[] = { a, c, 0, e, b, d, 0, f, 0, 0, 1, 0, 0, 0, 0, 1 };
410  newFrame.apply(new Transform(new Matrix4d(elemenents)));
411 
412  }
413  return newFrame;
414  }
415 
416  // SVGOMGElement
417  private void loadPath(Node pathNode, double resolution, Transform startingFrame, String encapsulatingLayer) {
418  Transform newFrame;
419  // NodeList pathNodes = element.getElementsByTagName("path");
420  // Node transforms = element.getAttributes().getNamedItem("transform");
421  if (pathNode != null) {
422  // System.out.println("\tPath
423  // "+pathNode.getAttributes().getNamedItem("id").getNodeValue());
424  //System.out.println("Path loading "+pathNode);
425 
426 
427  newFrame = startingFrame;
428  try {
429  Node transforms = pathNode.getAttributes().getNamedItem("transform");
430  newFrame = getNewframe(startingFrame, transforms);
431  }catch(Exception ex) {
432  // no transform
433  }
434  try {
435  if(SVGOMPathElement.class.isInstance(pathNode)) {
436 // NamedNodeMap attribs = pathNode.getAttributes();
437 // for(int i=0;i<attribs.getLength();i++) {
438 // Node n = attribs.item(i);
439 // String namespaceURI = n.getNamespaceURI();
440 // String nodeName = n.getNodeName();
441 // System.out.print("\nName "+nodeName+" namespace "+namespaceURI);
442 // try {
443 // System.out.print(" value "+attribs.getNamedItemNS(namespaceURI, nodeName).getNodeValue());
444 // }catch(Exception ex) {
445 //
446 // }
447 // System.out.println("");
448 //
449 // }
450  Color c = null;
451  //System.out.println("Layer "+encapsulatingLayer);
452  try {
453  String []style = pathNode.getAttributes().getNamedItem( "style").getNodeValue().split(";");
454  for(String s:style) {
455  if(s.startsWith("fill:")) {
456  String string = s.split(":")[1];
457  c=Color.web(string);
458  break;
459  }
460  }
461  }catch(java.lang.IllegalArgumentException ex) {
462  // this means the fill is set to "none"
463  }catch(Exception ex) {
464  //ex.printStackTrace();
465  }
466  if(c==null) {
467  try {
468  String []style = pathNode.getAttributes().getNamedItem( "style").getNodeValue().split(";");
469  for(String s:style) {
470  if(s.startsWith("stroke:")) {
471  String string = s.split(":")[1];
472  c=Color.web(string);
473  break;
474  }
475  }
476  }catch(java.lang.IllegalArgumentException ex1) {
477  // this means the stroke is set to "none" the default green color will be used
478 
479  }catch(Exception ex1) {
480  //ex1.printStackTrace();
481  }
482  }
483  MetaPostPath2 mpp = new MetaPostPath2(pathNode);
484  String code = mpp.toCode();
485  //System.out.println("\tPath "+pathNode.getAttributes().getNamedItem("id").getNodeValue()+" "+newFrame);
486  loadComposite(code, resolution, newFrame,encapsulatingLayer,c);
487  }else if(SVGOMPolylineElement.class.isInstance(pathNode)) {
488  Color c = null;
489  //System.out.println("Layer "+encapsulatingLayer);
490  try {
491  String []style = pathNode.getAttributes().getNamedItem( "style").getNodeValue().split(";");
492  for(String s:style) {
493  if(s.startsWith("fill:")) {
494  String string = s.split(":")[1];
495  c=Color.web(string);
496  break;
497  }
498  }
499  }catch(java.lang.IllegalArgumentException ex) {
500  // this means the fill is set to "none"
501  }catch(Exception ex) {
502  //ex.printStackTrace();
503  }
504  try {
505  c=Color.web(pathNode.getAttributes().getNamedItem( "stroke").getNodeValue());
506  }catch(java.lang.IllegalArgumentException ex) {
507  // this means the fill is set to "none"
508  }catch(Exception ex) {
509  //ex.printStackTrace();
510  }
511  if(c==null) {
512  try {
513  String []style = pathNode.getAttributes().getNamedItem( "style").getNodeValue().split(";");
514  for(String s:style) {
515  if(s.startsWith("stroke:")) {
516  String string = s.split(":")[1];
517  c=Color.web(string);
518  break;
519  }
520  }
521  }catch(java.lang.IllegalArgumentException ex1) {
522  // this means the stroke is set to "none" the default green color will be used
523 
524  }catch(Exception ex1) {
525  //ex1.printStackTrace();
526  }
527  }
528 
529 
530  String sb =null;
531  SVGOMPolylineElement pathElement = (SVGOMPolylineElement)pathNode;
532  SVGPointList pathList = pathElement.getPoints();
533  // String offset = pathElement.getOwnerSVGElement();
534 
535  int pathObjects = pathList.getNumberOfItems();
536 
537  for (int i = 0; i < pathObjects; i++) {
538  SVGItem item = (SVGItem) pathList.getItem(i);
539  String itemLine = String.format("%s%n", item.getValueAsString());
540  if(sb==null) {
541  sb="M "+itemLine;
542  }
543  sb += "L "+itemLine;
544  }
545  sb+="z\n";
546  loadComposite(sb, resolution, newFrame,encapsulatingLayer,c);
547  }else if(SVGOMImageElement.class.isInstance(pathNode)) {
548  SVGImageElement image = (SVGOMImageElement) pathNode;
549  //System.out.println("Loading Image element..");
550  double x=toPx(image.getAttributes().getNamedItem("x").getNodeValue());
551  double y=toPx(image.getAttributes().getNamedItem("y").getNodeValue());
552  double pheight=toPx(image.getAttributes().getNamedItem("height").getNodeValue());
553  double pwidth=toPx(image.getAttributes().getNamedItem("width").getNodeValue());
554  String []imageData = null;
555  for(int i=0;i<image.getAttributes().getLength();i++) {
556  Node n = image.getAttributes().item(i);
557  if(n.getNodeName().contains("href"))
558  try {
559  imageData=n.getNodeValue().split("/");
560 
561  } catch (Exception e) {
562  // TODO Auto-generated catch block
563  e.printStackTrace();
564  }
565  }
566  //TODO parse the Image string into an Image object
567  //for(int i=0;i<10;i++)
568  // System.out.println(imageData[i]);
569  }
570  }catch (java.lang.ClassCastException ex){
571  // attempt to load image
572  System.out.println("Found "+pathNode.getClass());
573  //ex.printStackTrace();
574  }
575 
576  }
577 
578  }
579 
580  private void loadComposite(String code, double resolution, Transform startingFrame, String encapsulatingLayer, Color c) {
581  // Count the occourences of M
582  int count = code.length() - code.replace("M", "").length();
583  if (count < 2) {
584  // println "Single path found"
585 
586  // setHolePolarity(true);
587  try {
588  loadSingle(code, resolution, startingFrame,encapsulatingLayer, c);
589  } catch (Exception ex) {
590  // BowlerStudio.printStackTrace(ex);
591  }
592  } else {
593 
594  // setHolePolarity(false);
595  String[] pathParts = code.split("M");
596  // println "Complex path found "+ pathParts.length ;
597  for (int i = 0; i < pathParts.length; i++) {
598  String sectionedPart = "M" + pathParts[i];
599  // reapply the split char
600  if (sectionedPart.length() > 1) {
601 
602  // println "Seperated complex: "
603  loadSingle(sectionedPart, resolution, startingFrame,encapsulatingLayer, c);
604  }
605  }
606  }
607  // System.out.println("SVG has this many elements loaded: "+sections.size());
608  // BowlerStudioController.setCsg(sections,null);
609  }
610  public static boolean isCCW(Polygon polygon) {
611  double runningTotal=0;
612  List<Edge> edges = Edge.fromPolygon(polygon);
613  for(Edge e:edges) {
614  //runningTotal+=((e.getP1().pos.x-e.getP2().pos.x)*(e.getP1().pos.y-e.getP2().pos.y));
615  runningTotal+=e.getP1().pos.x*e.getP2().pos.y;
616  runningTotal-=e.getP2().pos.x*e.getP1().pos.y;
617  }
618 
619  return runningTotal<0;
620  }
621  private void loadSingle(String code, double resolution, Transform startingFrame, String encapsulatingLayer, Color c) {
622  // println code
623  BezierPath path = new BezierPath();
624  path.parsePathString(code);
625 
626  ArrayList<Vector3d> p = path.evaluate();
627  for (Vector3d point : p) {
628  point.transform(startingFrame);
629  point.transform(new Transform().scale((1.0 / getScale())));
630  point.transform(new Transform().translate(0, -height, 0));
631  point.transform(new Transform().rotZ(-180));
632  point.transform(new Transform().rotY(180));
633  }
634 
635  // System.out.println(" Path " + code);
636  Polygon poly = Polygon.fromPoints(p);
637  if(getPolygonByLayers()==null)
638  setPolygonByLayers(new HashMap<String, List<Polygon>>());
639  if (getPolygonByLayers().get(encapsulatingLayer) == null)
640  getPolygonByLayers().put(encapsulatingLayer, new ArrayList<Polygon>());
641  List<Polygon> list = getPolygonByLayers().get(encapsulatingLayer);
642  poly = Polygon.fromPoints(Extrude.toCCW(poly.getPoints()));
643  if(c!=null)
644  colors.put(poly, c);
645  list.add(poly);
646 
647  }
648  public List<String> getLayers(){
649  ArrayList<String> layers= new ArrayList<String>();
650  if(getPolygonByLayers()==null) {
651  toPolygons();
652  }
653  for(Object key:getPolygonByLayers().keySet().stream().sorted().toArray() )
654  layers.add((String) key);
655  return layers;
656  }
657  public CSG extrudeLayerToCSG(double t,String layer){
658  CSG unionAll = CSG.unionAll(extrudeLayers(t,0.01,layer).get(layer));
659  unionAll.setName(layer);
660 
661  return unionAll;
662  }
663  public ArrayList<CSG> extrudeLayer(double t,String layer){
664  return extrudeLayers(t,0.01,layer).get(layer);
665  }
666  public HashMap<String,ArrayList<CSG>> extrudeLayers(double t){
667  return extrudeLayers(t,0.01,null);
668  }
669  public HashMap<String,ArrayList<CSG>> extrudeLayers(double t, double resolution, String targetLayer){
670  this.thickness = t;
671 
672  if (thickness < 0) {
673  thickness = -thickness;
674  negativeThickness = true;
675  } else {
676  negativeThickness = false;
677  }
678 
679  toPolygons(0.001);
680 
681  for(String key: getPolygonByLayers().keySet()) {
682  if(targetLayer!=null)
683  if(!targetLayer.contentEquals(key))
684  continue;
685  if(csgByLayers.get(key)==null) {
686  csgByLayers.put(key, new ArrayList<CSG>());
687  }
688  ArrayList<CSG> parts = csgByLayers.get(key);
689  parts.clear();
690  for(Polygon p:getPolygonByLayers().get(key)) {
691  CSG newbit;
692  newbit = Extrude.getExtrusionEngine().extrude(new Vector3d(0, 0, thickness), p);
693  if (negativeThickness) {
694  newbit = newbit.toZMax();
695  }
696  if(colors.get(p)!=null) {
697  newbit.setColor(colors.get(p));
698  }
699  parts.add(newbit);
700  }
701  }
702 
703  return csgByLayers;
704  }
705 
706  public ArrayList<CSG> extrude(double t, double resolution) throws IOException {
707 
708 
709  HashMap<String,ArrayList<CSG>> layers =extrudeLayers( t, resolution,null);
710  ArrayList<CSG> all = new ArrayList<CSG>();
711  for(String key:layers.keySet()) {
712  all.addAll(layers.get(key));
713  }
714 
715 
716  return all;
717  }
718 
719 
729  public void setSVGDocument(Document document) {
730  initSVGDOM(document);
731  this.svgDocument = document;
732  }
733 
739  public Document getSVGDocument() {
740  return this.svgDocument;
741  }
742 
751  private void initSVGDOM(Document document) {
752  UserAgent userAgent = new UserAgentAdapter();
753  DocumentLoader loader = new DocumentLoader(userAgent);
754  BridgeContext bridgeContext = new BridgeContext(userAgent, loader);
755  bridgeContext.setDynamicState(BridgeContext.DYNAMIC);
756 
757  // Enable CSS- and SVG-specific enhancements.
758  (new GVTBuilder()).build(bridgeContext, document);
759  }
760 
770  private Document createSVGDocument(URI uri) throws IOException {
771  String parser = XMLResourceDescriptor.getXMLParserClassName();
772  SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
773  return factory.createDocument(uri.toString());
774  }
775 
777  return progress;
778  }
779 
781  this.progress = progress;
782  }
783 
785  return progressDefault;
786  }
787 
788  private HashMap<String,List<Polygon>> getPolygonByLayers() {
789  return polygonByLayers;
790  }
791  private void setPolygonByLayers(HashMap<String,List<Polygon>> polygonByLayers) {
792  this.polygonByLayers = polygonByLayers;
793  }
794 
795 
796 }
void parsePathString(String d)
Definition: BezierPath.java:29
ArrayList< Vector3d > evaluate()
CSG setName(String name)
Definition: CSG.java:2382
static CSG unionAll(CSG... csgs)
Definition: CSG.java:871
CSG setColor(Color color)
Definition: CSG.java:207
CSG toZMax(CSG target)
Definition: CSG.java:308
static List< Edge > fromPolygon(Polygon poly)
Definition: Edge.java:82
static List< Vector3d > toCCW(List< Vector3d > points)
Definition: Extrude.java:224
static IExtrusion getExtrusionEngine()
Definition: Extrude.java:332
List< Vector3d > getPoints()
Definition: Polygon.java:547
static Polygon fromPoints(List< Vector3d > points, PropertyStorage shared)
Definition: Polygon.java:386
Transform apply(Transform t)
Definition: Transform.java:576
Transform scale(Vector3d vec)
Definition: Transform.java:350
static ArrayList< CSG > extrude(URI uri, double thickness)
Definition: SVGLoad.java:284
HashMap< String, List< Polygon > > polygonByLayers
Definition: SVGLoad.java:54
HashMap< Polygon, Color > colors
Definition: SVGLoad.java:56
ISVGLoadProgress getProgress()
Definition: SVGLoad.java:776
Document createSVGDocument(URI uri)
Definition: SVGLoad.java:770
HashMap< String, ArrayList< CSG > > extrudeLayers(double t, double resolution, String targetLayer)
Definition: SVGLoad.java:669
CSG extrudeLayerToCSG(double t, String layer)
Definition: SVGLoad.java:657
static final String GROUP_ELEMENT_NAME
Definition: SVGLoad.java:51
ArrayList< CSG > extrude(double thickness)
Definition: SVGLoad.java:239
ArrayList< CSG > extrudeLayer(double t, String layer)
Definition: SVGLoad.java:663
void loadSingle(String code, double resolution, Transform startingFrame, String encapsulatingLayer, Color c)
Definition: SVGLoad.java:621
static HashMap< String, List< Polygon > > toPolygons(File f)
Definition: SVGLoad.java:259
HashMap< String, List< Polygon > > toPolygons(double resolution)
Definition: SVGLoad.java:264
void setHolePolarity(boolean p)
Definition: SVGLoad.java:122
void setProgress(ISVGLoadProgress progress)
Definition: SVGLoad.java:780
void loadComposite(String code, double resolution, Transform startingFrame, String encapsulatingLayer, Color c)
Definition: SVGLoad.java:580
void loadPath(Node pathNode, double resolution, Transform startingFrame, String encapsulatingLayer)
Definition: SVGLoad.java:417
double toPx(String value)
Definition: SVGLoad.java:78
void setSVGDocument(Document document)
Definition: SVGLoad.java:729
static ISVGLoadProgress progressDefault
Definition: SVGLoad.java:113
List< String > getLayers()
Definition: SVGLoad.java:648
void setScale(double value)
Definition: SVGLoad.java:93
HashMap< String, ArrayList< CSG > > extrudeLayers(double t)
Definition: SVGLoad.java:666
void initSVGDOM(Document document)
Definition: SVGLoad.java:751
HashMap< String, List< Polygon > > getPolygonByLayers()
Definition: SVGLoad.java:788
Transform getNewframe(Transform startingFrame, Node transforms)
Definition: SVGLoad.java:371
HashMap< String, Double > units
Definition: SVGLoad.java:67
static boolean isCCW(Polygon polygon)
Definition: SVGLoad.java:610
HashMap< String, List< Polygon > > toPolygons()
Definition: SVGLoad.java:276
ArrayList< CSG > extrude(double t, double resolution)
Definition: SVGLoad.java:706
HashMap< String, ArrayList< CSG > > csgByLayers
Definition: SVGLoad.java:55
void loadAllGroups(double resolution, Transform startingFrame)
Definition: SVGLoad.java:294
static ISVGLoadProgress getProgressDefault()
Definition: SVGLoad.java:784
static final String PATH_ELEMENT_NAME
Definition: SVGLoad.java:50
static ArrayList< CSG > extrude(File f, double thickness, double resolution)
Definition: SVGLoad.java:280
static ArrayList< CSG > extrude(URI uri, double thickness, double resolution)
Definition: SVGLoad.java:290
static ArrayList< CSG > extrude(File f, double thickness)
Definition: SVGLoad.java:245
ISVGLoadProgress progress
Definition: SVGLoad.java:61
void loadGroup(SVGOMGElement element, double resolution, Transform startingFrame, String encapsulatingLayer)
Definition: SVGLoad.java:335
double toMM(String value)
Definition: SVGLoad.java:109
void setPolygonByLayers(HashMap< String, List< Polygon >> polygonByLayers)
Definition: SVGLoad.java:791
CSG extrude(Vector3d dir, List< Vector3d > points)