/**
  Copyright (C) 2012-2015 by Autodesk, Inc.
  All rights reserved.

  $Revision: 40941 d23033c3af7c1d3a5723876fd4bd23be0cdb24b5 $
  $Date: 2016-01-16 19:30:18 $
  
  FORKID {B9E0E4D8-D548-421b-8F97-CCACF8B1F09F}
*/

description = "XML CLD for KUKA|prc";
vendor = "Autodesk";
vendorUrl = "http://www.autodesk.com";
legal = "Copyright (C) 2012-2015 by Autodesk, Inc.";
certificationLevel = 2;

longDescription = "Post for importing NC data into KUKA|prc for robot milling, based on Autodesk CLD.";

capabilities = CAPABILITY_INTERMEDIATE;
extension = "xml";
setCodePage("utf-8");

allowHelicalMoves = false;
allowedCircularPlanes = (1 << PLANE_XY); // allow any circular motion

properties = {
  useTimeStamp: false // enable to output a time stamp
};

var feedOutput = createVariable({});
var xyzFormat = createFormat({decimals:3});
var TX;
var TY;
var TZ;

var mapRCTable = new Table(
  [" compensation='off'", " compensation='left'", "", " compensation='right'"],
  {initial:RADIUS_COMPENSATION_OFF},
  "Invalid radius compensation"
);

function toPos(x, y, z) {
  return xyzFormat.format(x) + " " + xyzFormat.format(y) + " " + xyzFormat.format(z);
}

function toFeed(feed) {
  var f = feedOutput.format(feed);
  return f ? (" feed='" + f + "'") : "";
}

function toString(value) {
  if (typeof(value) == 'string') {
    return "\"" + value + "\"";
  } else {
    return value;
  }
}

function onOpen() {
  writeln("<?xml version='1.0' encoding='utf-8' standalone='yes'?>");
  writeln("<nc xmlns='http://www.hsmworks.com/xml/2008/nc' version='1.0'>");
  writeln("<!-- http://cam.autodesk.com for KUKA|prc http://www.robotsinarchitecture.org/kukaprc -->");
  if (properties.useTimeStamp) {
    var d = new Date();
    writeln("<meta><date timestamp='" + (d.getTime() * 1000) + "'/></meta>");
  }

}

function onComment(text) {
  // TAG: escape
  writeln("<comment>" + text + "</comment>");
}

function onSection() {
	
	  // convert coordinates into WCS
  //setTranslation(currentSection.workOrigin);
  //setRotation(currentSection.workPlane);
  
  setTranslation(currentSection.getGlobalWorkOrigin());
  setRotation(currentSection.getGlobalWorkPlane());

  var initialPosition = getFramePosition(currentSection.getInitialPosition());
  var ta = getFrameDirection(currentSection.getInitialToolAxis());
  TX = ta.x;
  TY = ta.y;
  TZ = ta.z;
  
    writeln("<!-- Section " +  currentSection.getId() + " -->");
}

function onParameter(name, value) {
	/*
  // TAG: escape
  var type = "float";
  if (typeof(value) == 'string') {
    type = "string";
  } else if ((value % 1) == 0) {
    type = "integer";
  }
  writeln("<parameter name='" + name + "' value='" + toString(value) + "' type='" + type + "'/>");
  */
}

function onDwell(seconds) {
  writeln("<dwell seconds='" + secFormat.format(seconds) + "'/>");
}

function onCycle() {
/*
  writeln("<group id='" + cycleType + "'>");
  for (var name in cycle) {
    writeln("<parameter name='" + name + "' value='" + toString(cycle[name]) + "'/>");
  }
  */
}

function onCyclePoint(x, y, z) {
  writeln("<drill to='" + toPos(x, y, z) + "' feedin='" + cycle.feedrate +"' feedout='" + cycle.retractFeedrate +"'/>");
}

function onCycleEnd() {
	/*
  writeln("</group>");
  */
}

function onRapid(_x, _y, _z) {
  onRapid5D(_x, _y, _z, TX, TY, TZ);
}


function onLinear(_x, _y, _z, feed) {
  onLinear5D(_x, _y, _z, TX, TY, TZ, feed);
}


function onRapid5D(x, y, z, dx, dy, dz) {
    writeln("<rapid5d to='" + toPos(x, y, z) + "' normal='" + toPos(dx, dy, dz) + "'/>");
  previousFeed = undefined;
}

function onLinear5D(x, y, z, dx, dy, dz, feed) {
  writeln("<linear5d to='" + toPos(x, y, z) + "' normal='" + toPos(dx, dy, dz) + "'" + toFeed(feed) + "/>");
}

function onCircular(clockwise, cx, cy, cz, x, y, z, feed) {
  var n = getCircularNormal();
  var block = "";

  block += isClockwise() ? "arc dir='cw'" : "arc dir='ccw'";
  
  block += " to='" + toPos(x, y, z) + "'";
  block += " center='" + toPos(cx, cy, cz) + "'";
  
  //block += " cirpln='" + toPos(n.x, n.y, n.z) + "'";
  block += " normal='" + toPos(TX, TY, TZ) + "'";

  block += toFeed(feed);
  writeln("<" + block + "/>");
}

function onCommand() {
  writeln("<command/>");
}

function onSectionEnd() {
  //writeln("</section>");
}

function onClose() {
  writeln("</nc>");
}
