# Export Profile to SVG in world coordinates
# Opens in a TextWindow

from java.lang import StringBuilder
from ij.gui import GUI
from ij.text import TextWindow
from ini.trakem2.display import Display, Profile

def export(profile):
  pl, p, pr = profile.getBezierArrays()
  n_points = len(p[0])
  pl = profile.transformPoints(pl)
  p = profile.transformPoints(p)
  pr = profile.transformPoints(pr)
  cal = profile.getLayerSet().getCalibrationCopy()
  pw = float(cal.pixelWidth)
  ph = float(cal.pixelHeight)
  svg = StringBuilder()
  svg.append('<path id="').append(profile.id).append('" d="M')
  for i in range(0, n_points - 1):
    svg.append(' ').append(p[0][i] * pw).append(',').append(p[1][i] * ph)
    svg.append(' C ').append(pr[0][i] * pw).append(',').append(pr[1][i] * ph)
    svg.append(' ').append(pl[0][i+1] * pw).append(',').append(pl[1][i+1] * ph)
  svg.append(' ').append(p[0][n_points -1] * pw).append(',').append(p[1][n_points -1] * ph)
  if profile.isClosed():
    svg.append(' C ').append(pr[0][n_points -1] * pw).append(',').append(pr[1][n_points -1] * ph)
    svg.append(' ').append(pl[0][0] * pw).append(',').append(pl[1][0] * ph)
    svg.append(' ').append(p[0][0] * pw).append(',').append(p[1][0] * ph)
    svg.append(' z')
  svg.append('" \>')
  return svg.toString()

def run():
  front = Display.getFront()
  if front is None:
    print "No displays open!"
    return
  sel = front.getSelection().getSelected(Profile)
  if sel.isEmpty():
    print "No profiles selected!"
    return
  svg = export(sel.get(0))
  tw = TextWindow("Profile as SVG", svg, 500, 500)
  GUI.center(tw)

run()

7c262046d41d8f2ed98245600a39e412348c3caf
# Export Profile to SVG in world coordinates
# Opens in a TextWindow

from java.lang import StringBuilder
from ij.gui import GUI
from ij.text import TextWindow
from ini.trakem2.display import Display, Profile

def export(profile):
  pl, p, pr = profile.getBezierArrays()
  n_points = len(p[0])
  pl = profile.transformPoints(pl)
  p = profile.transformPoints(p)
  pr = profile.transformPoints(pr)
  cal = profile.getLayerSet().getCalibrationCopy()
  pw = float(cal.pixelWidth)
  ph = float(cal.pixelHeight)
  svg = StringBuilder()
  svg.append('<path id="').append(profile.id).append('" d="M')
  for i in range(0, n_points - 1):
    svg.append(' ').append(p[0][i] * pw).append(',').append(p[1][i] * ph)
    svg.append(' C ').append(pr[0][i] * pw).append(',').append(pr[1][i] * ph)
    svg.append(' ').append(pl[0][i+1] * pw).append(',').append(pl[1][i+1] * ph)
  svg.append(' ').append(p[0][n_points -1] * pw).append(',').append(p[1][n_points -1] * ph)
  if profile.isClosed():
    svg.append(' C ').append(pr[0][n_points -1] * pw).append(',').append(pr[1][n_points -1] * ph)
    svg.append(' ').append(pl[0][0] * pw).append(',').append(pl[1][0] * ph)
    svg.append(' ').append(p[0][0] * pw).append(',').append(p[1][0] * ph)
    svg.append(' z')
  svg.append('" \>')
  return svg.toString()

def run():
  front = Display.getFront()
  if front is None:
    print "No displays open!"
    return
  sel = front.getSelection().getSelected(Profile)
  if sel.isEmpty():
    print "No profiles selected!"
    return
  svg = export(sel.get(0))
  tw = TextWindow("Profile as SVG", svg, 500, 500)
  GUI.center(tw)

run()
