Fix SCL SceneGraph is omitting graphical elements 24/2824/2
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 29 Mar 2019 10:47:11 +0000 (12:47 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 29 Mar 2019 12:01:29 +0000 (14:01 +0200)
gitlab #283

Change-Id: I660a9344ebdcb9aef8adecd712025bd2a623de8d

bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java
bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/BackgroundNode.java

index d3fd1cc..40a5265 100644 (file)
@@ -68,6 +68,7 @@ import org.simantics.scenegraph.g2d.nodes.BackgroundNode;
 import org.simantics.scenegraph.g2d.nodes.BoundsNode;
 import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
 import org.simantics.scenegraph.g2d.nodes.DataNode;
+import org.simantics.scenegraph.g2d.nodes.LinkNode;
 import org.simantics.scenegraph.g2d.nodes.NavigationNode;
 import org.simantics.scenegraph.g2d.nodes.SVGNode;
 import org.simantics.scenegraph.g2d.nodes.SelectionNode;
@@ -673,22 +674,28 @@ public class SCLScenegraph {
                        // get the bounds of the content
                        Rectangle2D content = rtreeBounds;
                        
-                       // To account for dynamic padding of selection rectangles (5 units + stroke width)
-                       int offset = 6;
                        
-                       double scale = width < 0 || height < 0 ? 1.0 : Math.min((width - 2*offset) / content.getWidth(), (height - 2*offset) / content.getHeight());
-               
-                       AffineTransform tr = new AffineTransform();
-                       tr.translate(offset, offset);
-                       tr.scale(scale, scale);
-                       tr.translate(-content.getX(), -content.getY());
-                       tr.getMatrix(matrix);
+                       if (content != null) {
+                        // To account for dynamic padding of selection rectangles (5 units + stroke width)
+                   int offset = 6;
+                   
+                   double scale = width < 0 || height < 0 ? 1.0 : Math.min((width - 2*offset) / content.getWidth(), (height - 2*offset) / content.getHeight());
+               
+                   AffineTransform tr = new AffineTransform();
+                   tr.translate(offset, offset);
+                   tr.scale(scale, scale);
+                           tr.translate(-content.getX(), -content.getY());
+                           tr.getMatrix(matrix);
+                           svgGenerator.setSVGCanvasSize(new Dimension((int)Math.ceil(scale * content.getWidth()) + 2*offset, (int)Math.ceil(scale * content.getHeight()) + 2*offset));
+                       } else {
+                           svgGenerator.setSVGCanvasSize(new Dimension(100, 100));
+                       }
                        //svgGenerator.translate(offset, offset);
                        //svgGenerator.scale(scale, scale);
                        // translate svgGenerator to the x and y coordinates of current content
                        //svgGenerator.translate(-content.getX(), -content.getY());
 
-                       svgGenerator.setSVGCanvasSize(new Dimension((int)Math.ceil(scale * content.getWidth()) + 2*offset, (int)Math.ceil(scale * content.getHeight()) + 2*offset));
+                       
                        //svgGenerator.setClip(content);
                        
                        result.append(MAIN_SECTION, "<g class=\"symbols\">");
@@ -906,6 +913,18 @@ public class SCLScenegraph {
                         parentBuilder.append(ALL_SECTIONS, "\n<g transform=\"" + m + "\">");
                     }
                 }
+            } else if (!(node instanceof RouteGraphNode) && !(node instanceof LinkNode)){
+               try {
+                       Element doc = renderSVGNode(svgGenerator, (IG2DNode)node);
+                       if (doc.getElementsByTagName("g").getLength() == 0)
+                               return;
+                       String svg = printSVGDocument(doc);
+                       parentBuilder.append(MAIN_SECTION, "<g>");
+                       parentBuilder.append(MAIN_SECTION, svg);
+                       parentBuilder.append(MAIN_SECTION, "\n</g>");
+               } catch (Exception e) {
+                       // TODO: There are nodes that do not behave well when rendered to SVG. For backwards compatibility, we don't handle the exceptions.
+               }
             }
 
             //enters.put(node, b.length());
index 46804e6..00c1887 100644 (file)
@@ -47,7 +47,10 @@ public class BackgroundNode extends G2DNode {
     public void render(Graphics2D g) {
        Rectangle2D drawBounds = bounds;
         if(drawBounds == null) {
-               drawBounds = (Rectangle2D)g.getClipBounds().clone(); // Fill whole clip area if bounds is not defined
+               if (g.getClipBounds() != null)
+                       drawBounds = (Rectangle2D)g.getClipBounds().clone(); // Fill whole clip area if bounds is not defined
+               else
+                       return;
         }
         
         if (color!=null) {