Extract vertex from a svg file

Hello all ! :slight_smile:

In short, I’m trying to extract vertex from a svg but no matter how I do I always get an error:
“getVertexCount() only works with PATH or GEOMETRY shapes”
What I did was:

  1. create a little sketch to draw on the screen and save it in a file as SVG ( I also tried with Inkscape)
  2. I try to open it as explained here https://processing.org/reference/PShape_getChild_.html
void setup() {
  size(1000, 1000);
  PShape ps = loadShape("test.svg");
  shape(ps);

  int children = ps.getChildCount();
  for (int i = 0; i < children; i++) {
    PShape child = ps.getChild(i);
    int total = child.getVertexCount();
    println(total);
  }
}

If anyone has an idea, I’m curious to know …
TIA :wink:

1 Like

Did you try

ps.getVertexCount(); instead?

Maybe, there are no children?

1 Like

I’ve tried but the problem stays the same …

1 Like

Perhaps the child is a GROUP shape (this is valid), itself having multiple children – you may need to recurse over the shape to find all non-group shapes.

1 Like

This is the content of the svg file exported by processing (it’s nothing fancy :wink: )


<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
          'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" style="fill-opacity:1; color-rendering:auto; color-interpolation:auto; text-rendering:auto; stroke:black; stroke-linecap:square; stroke-miterlimit:10; shape-rendering:auto; stroke-opacity:1; fill:black; stroke-dasharray:none; font-weight:normal; stroke-width:1; font-family:'Dialog'; font-style:normal; stroke-linejoin:miter; font-size:12px; stroke-dashoffset:0; image-rendering:auto;" width="1000" height="1000" xmlns="http://www.w3.org/2000/svg"
><!--Generated by the Batik Graphics2D SVG Generator--><defs id="genericDefs"
  /><g
  ><g style="fill:rgb(225,225,225); stroke:rgb(225,225,225);"
    ><rect x="0" width="1000" height="1000" y="0" style="stroke:none;"
    /></g
    ><g
    ><path style="fill:none;" d="M328 259 L300 606 L464 619 L480 432 L516 330 L824 456 L634 785"
    /></g
  ></g
></svg
>
1 Like