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

  Eureka APT post processor configuration.

  $Revision: 44125 f4638cdbcb2ba12ee89990911c9dbf69e7d490f1 $
  $Date: 2024-05-09 15:24:08 $

  FORKID {9B9A3F59-5F1C-49B7-8812-6CEA167A7156}
*/

description = "Eureka";
vendor = "Roboris";
vendorUrl = "http://www.roboris.it";
legal = "Copyright (C) 2012-2024 by Autodesk, Inc.";
certificationLevel = 2;
minimumRevision = 45917;

longDescription = "Post integration with Roboris Eureka. This can be used as APT or cascading post processor to use for simulation of NC Programs in Eureka.";

extension = "apt";
setCodePage("ansi");

capabilities = CAPABILITY_INTERMEDIATE | CAPABILITY_CASCADING;
dependencies = "eureka.hta";

allowHelicalMoves = true;
allowedCircularPlanes = undefined; // allow any circular motion

// user-defined properties
properties = {
  useTrans: {
    title      : "Use Trans command (robot)",
    description: "Use Trans, XYZ command to transforms the current coordinate system.",
    group      : "preferences",
    type       : "boolean",
    value      : false,
    scope      : "post"
  },
  highAccuracy: {
    title      : "High accuracy",
    description: "Specifies short (no) or long (yes) numeric format.",
    group      : "preferences",
    type       : "boolean",
    value      : true,
    scope      : "post"
  },
  onlyAPT: {
    title      : "Generate only APT",
    description: "Generate only APT without opening Eureka",
    group      : "preferences",
    type       : "boolean",
    value      : false,
    scope      : "post"
  },
  exportTools: {
    title      : "Export tools",
    description: "The tools library linked with the Eureka template will be overwritten by the tools exported",
    group      : "preferences",
    type       : "boolean",
    value      : true,
    scope      : "post"
  },
  toolLength: {
    title      : "Disable tool length",
    description: "Export the tool with tool length equal to zero",
    group      : "preferences",
    type       : "boolean",
    value      : false,
    scope      : "post"
  },
  showNotes: {
    title      : "Show notes",
    description: "Writes operation notes as comments in the outputted code.",
    group      : "formats",
    type       : "boolean",
    value      : true,
    scope      : "post"
  },
  useWCS: {
    title      : "Use WCS offset (robot)",
    description: "Use WCS offset in the output code.",
    group      : "preferences",
    type       : "boolean",
    value      : false,
    scope      : "post"
  }
};

var xyzFormat = createFormat({decimals:(unit == MM ? 3 : 4)});
var mainFormat = createFormat({decimals:6, type:FORMAT_REAL});
var ijkFormat = createFormat({decimals:9, type:FORMAT_REAL});
var rpmFormat = createFormat({decimals:0});
var feedFormat = createFormat({decimals:6, type:FORMAT_REAL});

var feedOutput = createOutputVariable({}, feedFormat);

// collected state
var radiusCompensationActive = false;

function writeComment(text) {
  writeBlock("PPRINT/" + filterText(text, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789(.,)_/-+*=: \t"));
}

function writeBlock() {
  var text = formatWords(arguments);
  if (!text) {
    return;
  }
  writeWords(text);
}

function formatSeperatedWords() {
  setWordSeparator(", ");
  var text = formatWords(arguments);
  setWordSeparator("");
  if (!text) {
    return "";
  }
  return text;
}

function onComment(comment) {
  writeComment(comment);
}

function onOpen() {
  var date = new Date(); // output current date and time
  writeBlock("PARTNO/", programName, date.toLocaleDateString());
  writeBlock("MACHIN/", (isMilling() ? "MILL, 01" : "TURN, 01")); // first statement for an operation
  writeBlock("UNITS/", ((unit == IN) ? "INCHES" : "MM"));
  writeBlock("PARTNO/1", programName, date.toLocaleDateString());

  if (!getProperty("highAccuracy")) {
    mainFormat.setNumberOfDecimals(4);
    ijkFormat.setNumberOfDecimals(7);
    feedFormat.setNumberOfDecimals(4);
    feedOutput.setFormat(feedFormat);
  }
}

var mapCommand = {
  COMMAND_STOP                    : "STOP",
  COMMAND_OPTIONAL_STOP           : "OPSTOP",
  COMMAND_STOP_SPINDLE            : "SPINDL/ON",
  COMMAND_START_SPINDLE           : "SPINDL/OFF",
  COMMAND_SPINDLE_CLOCKWISE       : "SPINDL/CLW",
  COMMAND_SPINDLE_COUNTERCLOCKWISE: "SPINDL/CCLW"
};

function onCommand(command) {
  switch (command) {
  case COMMAND_COOLANT_OFF:
    setCoolant(COOLANT_OFF);
    return;
  case COMMAND_COOLANT_ON:
    setCoolant(tool.coolant);
    return;
  case COMMAND_STOP:
    return;
  case COMMAND_OPTIONAL_STOP:
    return;
  case COMMAND_LOCK_MULTI_AXIS:
    return;
  case COMMAND_UNLOCK_MULTI_AXIS:
    return;
  case COMMAND_START_CHIP_TRANSPORT:
    return;
  case COMMAND_STOP_CHIP_TRANSPORT:
    return;
  case COMMAND_BREAK_CONTROL:
    return;
  case COMMAND_TOOL_MEASURE:
    return;
  case COMMAND_PROBE_ON:
    return;
  case COMMAND_PROBE_OFF:
    return;
  }

  var stringId = getCommandStringId(command);
  var mcode = mapCommand[stringId];
  if (mcode != undefined) {
    writeBlock(mFormat.format(mcode));
  } else {
    onUnsupportedCommand(command);
  }
}

var currentCoolantMode = COOLANT_OFF;
function setCoolant(coolant) {
  if (coolant == currentCoolantMode) {
    return;
  }
  writeBlock(coolant == COOLANT_OFF ? "COOLNT/OFF" : "COOLNT/ON");
  currentCoolantMode = coolant;
}

/*
  Define 7-Parameter APT Cutter.
*/
function writeCutter() {
  var d = tool.diameter;
  var r = tool.cornerRadius;
  var e = tool.tipDiameter;
  var f = r;
  var a = 0;
  var b = tool.taperAngle;
  var h = tool.fluteLength;

  // Adjust taper angle for drill
  // It is defined as the full inclusive angle
  if ((tool.type == TOOL_DRILL) ||
      (tool.type == TOOL_DRILL_CENTER) ||
      (tool.type == TOOL_DRILL_SPOT)) {
    b /= 2;
  }

  // Adjust diameter for cone cutter
  if ((b > 0) && (b < Math.PI / 2)) {
    d = e + r;
    d = d - (2 * r) + (2 * (r * Math.tan((Math.PI / 2 - b) / 2)));

  // Adjust diameter for bell cutter
  } else if ((b < 0) && (b > Math.PI / 2)) {
    d = e + r;
    d += 2 * (r * (tan(-b / 2) + 1) / (Math.tan(Math.PI / 2 + b)));
  }

  writeBlock("CUTTER/", formatSeperatedWords(mainFormat.format(d), mainFormat.format(r),
    mainFormat.format(e), mainFormat.format(f), mainFormat.format(a),
    mainFormat.format(toDeg(b)), mainFormat.format(h)));
}

function onSection() {

  writeBlock("WCS/ID, " + (getProperty("useWCS") ? currentSection.workOffset : (currentSection.spindle + 1)));

  setRotation(currentSection.workPlane);

  if (getProperty("useTrans")) {
    var modelOrigine = currentSection.getModelOrigin();
    x = xyzFormat.format(-modelOrigine.x);
    y = xyzFormat.format(-modelOrigine.y);
    z = xyzFormat.format(-modelOrigine.z);

    var pmodelPlane = currentSection.getModelPlane();
    var Vec = pmodelPlane.eulerZYX_R.toDeg();
    rz = xyzFormat.format(-Vec.z);
    ry = xyzFormat.format(-Vec.y);
    rx = xyzFormat.format(-Vec.x);

    writeBlock("TRANS/" + formatSeperatedWords(x, y, z, "ZYX", rz, ry, rx));
  }

  var comment = getParameter("operation-comment", "");
  if (comment) {
    writeln("");
    writeComment("OPERATION NAME : " + comment);
    writeBlock(formatSeperatedWords("PATTERN/ID", currentSection.getPatternId()));
  }

  if (currentSection.isMultiAxis() || !currentSection.isZOriented()) {
    writeBlock("MULTAX/ON");
  } else {
    writeBlock("MULTAX/OFF");
  }

  var mode = currentSection.getType();
  writeBlock("MODE/" + (mode ? "TURN" : "MILL")); // first statement for an operation
  var t = tool.number;

  var toolLengthOffset = tool.isTurningTool() ? tool.getCompensationOffset() : tool.getLengthOffset();
  writeBlock(formatSeperatedWords("LOADTL/" + t, (toolLengthOffset > 0 ? "OSETNO, " + toolLengthOffset : "")));

  if (mode == TYPE_MILLING) {
    writeBlock(formatSeperatedWords("SPINDL/RPM", mainFormat.format(spindleSpeed), (tool.clockwise ? "CLW" : "CCLW")));
  } else {
    if (currentSection.getTool().getSpindleMode() == SPINDLE_CONSTANT_SURFACE_SPEED) {
      var _spindleSpeed = tool.surfaceSpeed * ((unit == MM) ? 1 / 1000.0 : 1 / 12.0);
      writeBlock(formatSeperatedWords("SPINDL/" + (unit == MM ? "SMM" : "SFM"), rpmFormat.format(_spindleSpeed), (true ? "CLW" : "CCLW"), "MAXRPM", rpmFormat.format(tool.maximumSpindleSpeed)));
    } else {
      writeBlock(formatSeperatedWords("SPINDL/RPM", rpmFormat.format(spindleSpeed), (true ? "CLW" : "CCLW")));
    }
  }

  writeBlock(setCoolant(tool.coolant));
}

function getFeedUnit() {
  var feedUnit = (unit == IN) ? "IPM" : "MMPM";
  if (currentSection.feedMode == FEED_PER_REVOLUTION) {
    feedUnit = (unit == IN) ? "IPR" : "MMPR";
  }
  return feedUnit;
}

function onDwell(time) {
  writeBlock("DELAY/", mainFormat.format(time)); // in seconds
}

function onRadiusCompensation() {
  if (radiusCompensation == RADIUS_COMPENSATION_OFF) {
    if (radiusCompensationActive) {
      radiusCompensationActive = false;
      writeBlock("CUTCOM/OFF");
    }
  } else {
    if (!radiusCompensationActive) {
      radiusCompensationActive = true;
      writeBlock("CUTCOM/ON");
    }
    var direction = (radiusCompensation == RADIUS_COMPENSATION_LEFT) ? "LEFT" : "RIGHT";
    if (tool.diameterOffset != 0) {
      writeBlock(formatSeperatedWords("CUTCOM/" + direction, "OSETNO", tool.diameterOffset));
    } else {
      writeBlock("CUTCOM/" + direction);
    }
  }
}

function writeGOTO(x, y, z, i, j, k) {
  var mode = currentSection.getType();
  var _x = mainFormat.format(x);
  var _y = mainFormat.format(y);
  var _z = mainFormat.format(z);
  var _i = ijkFormat.format(i);
  var _j = ijkFormat.format(j);
  var _k = ijkFormat.format(k);

  writeBlock("GOTO/" + formatSeperatedWords(_x, _y, _z, mode == TYPE_MILLING ? formatSeperatedWords(_i, _j, _k) : ""));
}

function onRapid(x, y, z) {
  writeBlock("RAPID");
  writeGOTO(x, y, z, currentSection.workPlane.forward.x, currentSection.workPlane.forward.y, currentSection.workPlane.forward.z);
}

function onLinear(x, y, z, feed) {
  var f = feedOutput.format(feed);
  if (f) {
    writeBlock("FEDRAT/" + formatSeperatedWords(mainFormat.format(feed), getFeedUnit()));
  }
  writeGOTO(x, y, z, currentSection.workPlane.forward.x, currentSection.workPlane.forward.y, currentSection.workPlane.forward.z);
}

function onRapid5D(x, y, z, dx, dy, dz) {
  writeBlock("RAPID");
  writeGOTO(x, y, z, dx, dy, dz);
}

function onLinear5D(x, y, z, dx, dy, dz, feed) {
  var f = feedOutput.format(feed);
  if (f) {
    writeBlock("FEDRAT/" + formatSeperatedWords(mainFormat.format(feed), getFeedUnit()));
  }
  writeGOTO(x, y, z, dx, dy, dz);
}

function onCircular(clockwise, cx, cy, cz, x, y, z, feed) {
  var f = feedOutput.format(feed);
  if (f) {
    writeBlock("FEDRAT/" + formatSeperatedWords(mainFormat.format(feed), getFeedUnit()));
  }
  var n = getCircularNormal();
  var dir = clockwise ? -1 : 1;

  writeBlock(formatSeperatedWords("CIRCLE/" + mainFormat.format(cx), mainFormat.format(cy), mainFormat.format(cz),
    ijkFormat.format(n.x * dir), ijkFormat.format(n.y * dir), ijkFormat.format(n.z * dir), mainFormat.format(getCircularRadius())));

  writeGOTO(x, y, z, currentSection.workPlane.forward.x, currentSection.workPlane.forward.y, currentSection.workPlane.forward.z);
}

function onCycle() {
  var depth = mainFormat.format((cycle.depth !== undefined) ? cycle.depth : 0);
  var feed = mainFormat.format((cycle.feedrate !== undefined) ? cycle.feedrate : 0);
  var clear = mainFormat.format((cycle.clearance !== undefined) ? cycle.clearance : 0);
  var dwl = mainFormat.format((cycle.dwell !== undefined) ? cycle.dwell : 0);
  var increment = mainFormat.format((cycle.incrementalDepth !== undefined) ? cycle.incrementalDepth : 0); // for pecking

  var statement;
  writeBlock(formatSeperatedWords("FEDRAT/" +  mainFormat.format(cycle.feedrate), getFeedUnit()));

  switch (cycleType) {
  case "drilling":
  case "counter-boring":
    statement = formatSeperatedWords("CYCLE/ DRILL", "DEPTH", depth, getFeedUnit(), feed, "CLEAR", clear);
    break;
  case "reaming":
    statement = formatSeperatedWords("CYCLE/ REAM", "DEPTH", depth, getFeedUnit(), feed, "CLEAR", clear);
    break;
  case "boring":
    statement = formatSeperatedWords("CYCLE/ BORE", "DEPTH", depth, getFeedUnit(), feed, "CLEAR", clear, "ORIENT", 0);
    break;
  case "fine-boring":
    statement = formatSeperatedWords("CYCLE/ BORE", "DEPTH", depth, getFeedUnit(), feed, "CLEAR", clear, cycle.shift, "ORIENT", 0);
    break;
  case "deep-drilling":
    statement = formatSeperatedWords("CYCLE/ DEEP", "DEPTH", depth, "STEP", increment, getFeedUnit(), feed, "CLEAR", clear);
    break;
  case "chip-breaking":
    statement = formatSeperatedWords("CYCLE/ BRKCHP", "DEPTH", depth, "STEP", increment, getFeedUnit(), feed, "CLEAR", clear);
    break;
  case "tapping":
  case "right-tapping":
    if (tool.type == TOOL_TAP_LEFT_HAND) {
      cycleNotSupported();
    }
    statement = formatSeperatedWords("CYCLE/TAP", "DEPTH", depth, getgetFeedUnit()(), feed, "CLEAR", clear);
    break;
  default:
    cycleNotSupported();
  }
  if (dwl > 0) {
    statement = formatSeperatedWords(statement, "DWELL", dwl);
  }
  writeBlock(statement);
}

function onCyclePoint(x, y, z) {
  x += cycle.depth * currentSection.workPlane.forward.x;
  y += cycle.depth * currentSection.workPlane.forward.y;
  z += cycle.depth * currentSection.workPlane.forward.z;

  writeGOTO(x, y, z, currentSection.workPlane.forward.x, currentSection.workPlane.forward.y, currentSection.workPlane.forward.z);
}

function onCycleEnd() {
  writeBlock("CYCLE/ OFF");
}

function onSectionEnd() {
  writeln("");
  writeBlock("SPINDL/OFF");
  setCoolant(COOLANT_OFF);
  feedOutput.reset();
}

this.exportStock = true;
this.exportPart = true;
this.exportFixture = true;

var destPath = FileSystem.getFolderPath(getCascadingPath());
var projectPath;
var eurekaInstallationPath;
var fusion2eurekaPath;
var tdbPath = FileSystem.getCombinedPath(destPath, "tools");

function showDialog() {

  if (!FileSystem.isFolder(FileSystem.getTemporaryFolder())) {
    FileSystem.makeFolder(FileSystem.getTemporaryFolder());
  }
  var path = FileSystem.getTemporaryFile("post");

  if (getProperty("exportTools")) {
    execute(findFile("eureka.hta"), "\"" + path + "\"" + "\"-exportTool=TRUE\"", false, "");
  } else {
    execute(findFile("eureka.hta"), "\"" + path + "\"" + "\"-exportTool=FALSE\"", false, "");
  }

  var result = {};
  try {
    var file = new TextFile(path, false, "utf-8");
    while (true) {
      var line = file.readln();
      if (!line) {
        break;
      }
      var index = line.indexOf("=");
      if (index >= 0) {
        var name = line.substr(0, index).trim();
        var value = line.substr(index + 1).trim();
        result[name] = value;
      }
    }
    file.close();
  } catch (e) {
    // Ignore any errors
  }
  FileSystem.remove(path);

  var gotValues = false;
  for (var name in result) {
    gotValues = true;
    break;
  }
  if (!gotValues) {
    error(localize("Aborted by user."));
  }

  for (var name in result) {
    var value = result[name];
    switch (name) {
    case "EurekaProjectPath":
      projectPath = value;
      break;
    case "EurekaInstallationFolder":
      eurekaInstallationPath = value;
      break;
    case "ExportTools":
      setProperty("exportTools", value == "TRUE");
      break;
    }
  }
  return true;
}

function createToolDatabaseFile() {
  FileSystem.makeFolder(tdbPath);

  var tools = getToolTable();
  if (tools.getNumberOfTools() > 0) {
    for (var i = 0; i < tools.getNumberOfTools(); ++i) {
      var tool = tools.getTool(i);
      if (tool.isTurningTool()) {
        continue;
      }

      var toolPath = FileSystem.getCombinedPath(tdbPath, tool.number + ".etf");
      var file = new TextFile(toolPath, true, "ansi");

      var toolType = tool.getType();
      var holder = tool.holder;
      var unitOfMeasurement = tool.unit;
      var eurekatoolType = "";

      switch (toolType) {
      case TOOL_DRILL:
      case TOOL_DRILL_CENTER:
      case TOOL_DRILL_SPOT:
      case TOOL_COUNTER_SINK:
        eurekatoolType = "DRILL";
        break;
      case TOOL_TAP_LEFT_HAND:
      case TOOL_TAP_RIGHT_HAND:
        eurekatoolType = "TAP";
        break;
      case TOOL_COUNTER_BORE:
      case TOOL_REAMER:
      case TOOL_BORING_BAR:
        eurekatoolType = "BORE";
        break;
      case TOOL_MILLING_END_FLAT:
      case TOOL_MILLING_FORM:
      case TOOL_MILLING_THREAD:
      case TOOL_MILLING_END_BULLNOSE:
      case TOOL_MILLING_END_BALL:
      case TOOL_MILLING_CHAMFER:
      case TOOL_MILLING_SLOT:
      case TOOL_MILLING_RADIUS:
      case TOOL_MILLING_FACE:
      case TOOL_MILLING_TAPERED:
      case TOOL_MILLING_DOVETAIL:
      case TOOL_MILLING_LOLLIPOP:
        eurekatoolType = "MILL";
        break;
      default:
        eurekatoolType = "MILL";
      }

      var offsetZ = tool.bodyLength + tool.holderLength;
      offsetZ = (unit == IN) ? offsetZ * 25.4 : offsetZ;
      var diameter = (unit == IN) ? tool.diameter * 25.4 : tool.diameter;
      var fluteLength = (unit == IN) ? tool.fluteLength * 25.4 : tool.fluteLength;
      var holderLength = (unit == IN) ? tool.holderLength * 25.4 : tool.holderLength;
      var shaftDiameter = (unit == IN) ? tool.shaftDiameter * 25.4 : tool.shaftDiameter;

      file.writeln(
        ""
      );
      file.writeln("" + tool.getType() + "");

      if (getProperty("toolLength")) {
        file.writeln("  ");
        file.writeln("    ");
      } else {
        file.writeln("  ");
        file.writeln("    ");
      }

      file.writeln("  ");
      file.writeln("    ");

      var cutter = tool.getCutterProfile();
      var isCuttingElement = true;
      for (var k = 0; k < cutter.getNumberOfEntities() / 2; ++k) {
        var arc = ((cutter.getEntity(k).clockwise == true) || cutter.getEntity(k).center.length > 1e-4);
        var startX = cutter.getEntity(k).start.x;
        var startY = cutter.getEntity(k).start.y;
        var endX = cutter.getEntity(k).end.x;
        var endY = cutter.getEntity(k).end.y;
        var centerX = cutter.getEntity(k).center.x;
        var centerY = cutter.getEntity(k).center.y;
        var arcDir = cutter.getEntity(k).clockwise ? "CW" : "CCW";

        startX = (unit == IN) ? startX * 25.4 : startX;
        startY = (unit == IN) ? startY * 25.4 : startY;
        endX = (unit == IN) ? endX * 25.4 : endX;
        endY = (unit == IN) ? endY * 25.4 : endY;
        centerX = (unit == IN) ? centerX * 25.4 : centerX;
        centerY = (unit == IN) ? centerY * 25.4 : centerY;

        var skip = false;
        if ((endY >= fluteLength) && isCuttingElement) { // split a single arc segment if the fluteLength is smaller than the endY position
          skip = true;
          if (arc) {
            var radius = Vector.diff(cutter.getEntity(k).start, cutter.getEntity(k).center).length;
            radius = (unit == IN) ? radius * 25.4 : radius;
            var p = cutter.getEntity(k).clockwise ? (radius - fluteLength) : fluteLength;
            var q = (2 * Math.sqrt(p * ((radius * 2) - p))) / 2;
            if (cutter.getEntity(k).clockwise) {
              q = startX + radius - q;
            } else {
              q = startX + q;
            }
            file.writeln("      ");
            file.writeln("        " + xyzFormat.format(startX) + " " + xyzFormat.format(startY) + "");
            file.writeln("        " + xyzFormat.format(q) + " " + xyzFormat.format(fluteLength) + "");
            file.writeln("        
" + xyzFormat.format(centerX) + " " + xyzFormat.format(centerY) + "
"); file.writeln("
"); startX = q; startY = fluteLength; } else { file.writeln(" " + "" + xyzFormat.format(startX) + " " + xyzFormat.format(startY) + "" + "" + xyzFormat.format(endX) + " " + xyzFormat.format(endY) + "" ); } isCuttingElement = false; file.writeln("
"); file.writeln("
"); file.writeln(" "); file.writeln(" "); } if (!skip) { if (arc) { file.writeln(" "); file.writeln(" " + xyzFormat.format(startX) + " " + xyzFormat.format(startY) + ""); file.writeln(" " + xyzFormat.format(endX) + " " + xyzFormat.format(endY) + ""); file.writeln("
" + xyzFormat.format(centerX) + " " + xyzFormat.format(centerY) + "
"); file.writeln("
"); } else { file.writeln(" " + "" + xyzFormat.format(startX) + " " + xyzFormat.format(startY) + "" + "" + xyzFormat.format(endX) + " " + xyzFormat.format(endY) + "" ); } } } file.writeln("
"); file.writeln("
"); file.writeln( " "); file.writeln(" "); var hCurrent = 0; if (holder && holder.hasSections()) { var n = holder.getNumberOfSections(); for (var j = 1; j < n; ++j) { if (j == 0) { file.writeln(" 0 0" + xyzFormat.format(shaftDiameter / 2) + " 0"); } else { var getLengthJ = (unit == IN) ? holder.getLength(j) * 25.4 : holder.getLength(j); var getLengthJ1 = (unit == IN) ? holder.getLength(j - 1) * 25.4 : holder.getLength(j - 1); var getDiameterJ = (unit == IN) ? holder.getDiameter(j) * 25.4 : holder.getDiameter(j); var getDiameterJ1 = (unit == IN) ? holder.getDiameter(j - 1) * 25.4 : holder.getDiameter(j - 1); hCurrent += getLengthJ1; file.writeln(" " + "" + xyzFormat.format(getDiameterJ1 / 2) + " " + xyzFormat.format(hCurrent) + "" + "" + xyzFormat.format(getDiameterJ / 2) + " " + xyzFormat.format(hCurrent + getLengthJ) + "" ); } } } file.writeln(" "); file.writeln(" "); file.writeln("
"); file.close(); } } } function createProjectFile() { if (!programName) { error(localize("Program name is not specified.")); } var numberOfSections = getNumberOfSections(); var x = 0; var y = 0; var z = 0; var rx = 0; var ry = 0; var rz = 0; if (hasGlobalParameter("autodeskcam:fixture-path")) { x = xyzFormat.format(getSection(0).getFCSOrigin().x); y = xyzFormat.format(getSection(0).getFCSOrigin().y); z = xyzFormat.format(getSection(0).getFCSOrigin().z); } else { var plane = getSection(0).getFCSPlane(); var vec = plane.eulerZYX.toDeg(); rz = vec.x; ry = vec.y; rx = vec.z; x = xyzFormat.format(getSection(0).getFCSOrigin().x); y = xyzFormat.format(getSection(0).getFCSOrigin().y); z = xyzFormat.format(getSection(0).getFCSOrigin().z); } var ncFrame = new Array(); for (var i = 0; i < numberOfSections; ++i) { var found = false; var section = getSection(i); for (var j = 0; j < ncFrame.length; ++j) { if (ncFrame[j].workOffsetNumber == section.workOffset) { found = true; break; } } if (!found) { ncFrame.push({workOffsetNumber:section.workOffset, fcsPlane:section.getFCSPlane(), fcsOrigin:section.getFCSOrigin()}); } } fusion2eurekaPath = FileSystem.getTemporaryFile("fusion2eureka"); fusion2eurekaPath = FileSystem.replaceExtension(fusion2eurekaPath, "xml"); var file = new TextFile(fusion2eurekaPath, true, "ansi"); file.writeln(""); file.writeln(""); file.writeln("" + ((unit == IN) ? "INCHES" : "MM") + ""); file.writeln("" + projectPath + ""); file.writeln("" + FileSystem.getCombinedPath(destPath, programName + ".epf") + ""); file.writeln("" + destStockPath + ""); file.writeln("" + destFixturePath + ""); file.writeln("" + destPartPath + ""); for (var i = 0; i < ncFrame.length; ++i) { var fcsPlane = ncFrame[i].fcsPlane; var fcsOrigin = ncFrame[i].fcsOrigin; var eurekaOrigin = fcsOrigin; file.writeln("" + ncFrame[i].workOffsetNumber + ""); } file.writeln("" + tdbPath + ""); file.writeln("" + getProperty("exportTools") + ""); var programFilename = FileSystem.getFilename(getCascadingPath()); file.writeln("" + FileSystem.getCombinedPath(destPath, programFilename) + ""); file.writeln("" + FileSystem.getCombinedPath(eurekaInstallationPath, "Eureka.exe") + ""); file.writeln(""); file.close(); } var destStockPath = ""; var destPartPath = ""; var destFixturePath = ""; function createVerificationJob() { var stockPath; var partPath; var fixturePath; if (hasGlobalParameter("autodeskcam:stock-path")) { stockPath = getGlobalParameter("autodeskcam:stock-path"); } if (hasGlobalParameter("autodeskcam:part-path")) { partPath = getGlobalParameter("autodeskcam:part-path"); } if (hasGlobalParameter("autodeskcam:fixture-path")) { fixturePath = getGlobalParameter("autodeskcam:fixture-path"); } if (hasGlobalParameter("autodeskcam:job_useChuck")) { fixturePath = getGlobalParameter("autodeskcam:chuckFront-path"); } if (!FileSystem.isFolder(destPath)) { error(subst(localize("NC verification job folder '%1' does not exist."), destPath)); } if (!programName) { error(localize("Program name is not specified.")); } if (FileSystem.isFile(stockPath)) { destStockPath = FileSystem.getCombinedPath(destPath, programName + "_STOCK.stl"); FileSystem.copyFile(stockPath, destStockPath); } if (FileSystem.isFile(partPath)) { destPartPath = FileSystem.getCombinedPath(destPath, programName + "_PART.stl"); FileSystem.copyFile(partPath, destPartPath); } if (FileSystem.isFile(fixturePath)) { destFixturePath = FileSystem.getCombinedPath(destPath, programName + "_FIXTURE.stl"); FileSystem.copyFile(fixturePath, destFixturePath); } } function onTerminate() { if (!getProperty("onlyAPT")) { if (eurekaInstallationPath == "") { error(localize("Please specify a valid Eureka installation folder.")); } else { if (FileSystem.isFile(FileSystem.getCombinedPath(eurekaInstallationPath, "BatchTest.exe"))) { execute(FileSystem.getCombinedPath(eurekaInstallationPath, "BatchTest.exe"), fusion2eurekaPath + " -plugin=FUSION", true, ""); } else { error(localize("Eureka was not found on your machine. Be sure to specify a valid Eureka installation folder.")); } } FileSystem.remove(fusion2eurekaPath); } } function onClose() { writeln(""); if (!getProperty("onlyAPT")) { showDialog(); createVerificationJob(); createProjectFile(); if (getProperty("exportTools")) { createToolDatabaseFile(); } } }