Exemple #1
0
js::Value GCodeLibrary::speedCB(const js::Arguments &args) {
  // Return spindle info if no arguments were given
  if (!args.getCount()) {
    js::Value array = js::Value::createArray(3);

    spin_mode_t mode;
    double max;
    array.set(0, ctx.machine.getSpeed(&mode, &max));
    array.set(1, mode);
    array.set(2, max);

    return array;
  }

  // Otherwise set spindle
  spin_mode_t mode = REVOLUTIONS_PER_MINUTE;
  double max = 0;

  if (args.has("surface")) {
    mode = CONSTANT_SURFACE_SPEED;
    if (args.has("max")) max = args["max"].toNumber();
  }

  ctx.machine.setSpeed(args["rate"].toNumber(), mode, max);

  return ctx.machine.getSpeed();
}
Exemple #2
0
js::Value GCodeLibrary::feedCB(const js::Arguments &args) {
  // Return feed info if no arguments were given
  if (!args.getCount()) {
    js::Value array = js::Value::createArray(2);

    feed_mode_t mode;
    array.set(0, ctx.machine.getFeed(&mode));
    array.set(1, mode);

    return array;
  }

  // Otherwise set feed
  feed_mode_t mode = MM_PER_MINUTE;
  if (args.has("mode")) {
    mode = (feed_mode_t)args["mode"].toUint32();
    switch (mode) {
    case INVERSE_TIME: case MM_PER_MINUTE: case MM_PER_REVOLUTION: break;
    default: THROW("Feed mode must be FEED_INVERSE_TIME, FEED_UNITS_PER_MIN or "
                   "FEED_UNITS_PER_REV");
    }
  }

  ctx.machine.setFeed(args["rate"].toNumber(), mode);

  return ctx.machine.getFeed();
}
Exemple #3
0
void GCodeLibrary::parseAxes(const js::Arguments &args, Axes &axes,
                             bool incremental) {
  for (const char *axis ="xyzabcuvw"; *axis; axis++) {
    string name = string(1, *axis);
    if (args.has(name)) axes.set(*axis, args.getNumber(name) +
                                 (incremental ? axes.get(*axis) : 0));
  }
}
Exemple #4
0
js::Value GCodeLibrary::toolSetCB(const js::Arguments &args) {
  SmartPointer<OpenSCAM::Tool> tool = ctx.tools->get(args["number"].toUint32());

  uint32_t units;
  double scale = 1;
  if (args.has("units")) units = args["units"].toUint32();
  else units = unitAdapter.getUnits();
  if (units == MachineUnitAdapter::METRIC)
    tool->setUnits(OpenSCAM::ToolUnits::UNITS_MM);
  else {
    tool->setUnits(OpenSCAM::ToolUnits::UNITS_INCH);
    scale = 25.4;
  }

  if (args.has("shape"))
    tool->setShape((OpenSCAM::ToolShape::enum_t)args["shape"].toUint32());

  tool->setLength(scale * args.getNumber("length"));
  tool->setDiameter(scale * args.getNumber("diameter"));
  tool->setSnubDiameter(scale * args.getNumber("snub"));
  tool->setFrontAngle(args.getNumber("front_angle"));
  tool->setBackAngle(args.getNumber("back_angle"));
  tool->setOrientation(args.getNumber("orientation"));

  return js::Value();
}
Exemple #5
0
js::Value GCodeLibrary::cutCB(const js::Arguments &args) {
  Axes axes = ctx.machine.getPosition();
  parseAxes(args, axes, args.getBoolean("incremental"));
  ctx.machine.move(axes);

  return js::Value();
}
Exemple #6
0
MatrixModule::axes_t MatrixModule::parseMatrix(const js::Arguments &args) {
  if (!args.has("matrix")) return XYZ;

  axes_t matrix = (axes_t)args["matrix"].toUint32();
  if (AXES_COUNT <= matrix) THROWS("Invalid matrix number " << matrix);

  return matrix;
}
Exemple #7
0
js::Value GCodeLibrary::probeCB(const js::Arguments &args) {
  bool toward = args.getBoolean("toward");
  bool error = args.getBoolean("error");
  uint32_t index = args.getUint32("index");
  int port = args.getInt32("port");
  bool invert = args.getInt32("invert");

  if (port == -1) port = ctx.machine.findPort(PROBE, index);
  if (port != -1)
    ctx.machine.input(port, toward ^ invert ? STOP_WHEN_HIGH : STOP_WHEN_LOW,
                  error);

  Axes axes = ctx.machine.getPosition();
  parseAxes(args, axes);
  ctx.machine.move(axes);

  return js::Value();
}
Exemple #8
0
js::Value GCodeLibrary::toolCB(const js::Arguments &args) {
  // Return tool number if no arguments were given
  if (!args.getCount()) return ctx.machine.getTool();

  uint32_t number = args["number"].toUint32();
  ctx.tools->get(number); // Make sure it exists
  ctx.machine.setTool(number);

  return ctx.machine.getTool();
}
Exemple #9
0
js::Value GCodeLibrary::gcodeCB(const js::Arguments &args) {
  string path =
    SystemUtilities::makeRelative(ctx.currentPath(), args.getString("path"));

  ctx.pushPath(path);
  SmartFunctor<TPLContext> popPath(&ctx, &TPLContext::popPath);

  Options options;
  OpenSCAM::Controller controller(ctx.machine);
  OpenSCAM::Interpreter(controller).read(path);

  return js::Value();
}
Exemple #10
0
js::Value GCodeLibrary::arcCB(const js::Arguments &args) {
  Vector3D
    offset(args.getNumber("x"), args.getNumber("y"), args.getNumber("z"));
  double angle = args.getNumber("angle");
  plane_t plane = args.has("plane") ? (plane_t)args.getUint32("plane") : XY;

  ctx.machine.arc(offset, angle, plane);

  return js::Value();
}
Exemple #11
0
js::Value GCodeLibrary::unitsCB(const js::Arguments &args) {
  MachineUnitAdapter::units_t units = unitAdapter.getUnits();

  if (args.has("type"))
    switch (args["type"].toUint32()) {
    case MachineUnitAdapter::IMPERIAL:
      unitAdapter.setUnits(MachineUnitAdapter::IMPERIAL);
      break;
    case MachineUnitAdapter::METRIC:
      unitAdapter.setUnits(MachineUnitAdapter::METRIC);
      break;
    default: THROWS("Units type must be one of IMPERIAL or METRIC");
    }
  else return js::Value(units);

  return js::Value();
}
Exemple #12
0
js::Value DXFLibrary::openCB(const js::Arguments &args) {
  string path =
    SystemUtilities::absolute(ctx.getCurrentPath(), args.getString("path"));

  DXFReader reader;
  reader.read(path);

  const DXFReader::layers_t &layers = reader.getLayers();
  js::Value v8Layers = layersTmpl.create();

  DXFReader::layers_t::const_iterator it;
  for (it = layers.begin(); it != layers.end(); it++) {
    const DXFReader::layer_t &layer = it->second;
    js::Value v8Layer = js::Value::createArray(layer.size());

    for (unsigned j = 0; j < layer.size(); j++) {
      const DXFEntity &entity = *layer[j];
      js::Value obj = entityTmpl.create();

      switch (entity.getType()) {
      case DXFEntity::DXF_POINT: {
        const DXFPoint &point = dynamic_cast<const DXFPoint &>(entity);
        obj.set("x", point.x());
        obj.set("y", point.y());
        obj.set("z", point.z());
        break;
      }

      case DXFEntity::DXF_LINE: {
        const DXFLine &line = dynamic_cast<const DXFLine &>(entity);

        js::Value start = entityTmpl.create();
        start.set("x", line.getStart().x());
        start.set("y", line.getStart().y());
        start.set("z", line.getStart().z());
        obj.set("start", start);

        js::Value end = entityTmpl.create();
        end.set("x", line.getEnd().x());
        end.set("y", line.getEnd().y());
        end.set("z", line.getEnd().z());
        obj.set("end", end);

        break;
      }

      case DXFEntity::DXF_ARC: {
        const DXFArc &arc = dynamic_cast<const DXFArc &>(entity);

        js::Value center = entityTmpl.create();
        center.set("x", arc.getCenter().x());
        center.set("y", arc.getCenter().y());
        center.set("z", arc.getCenter().z());
        obj.set("center", center);

        obj.set("radius", arc.getRadius());
        obj.set("startAngle", arc.getStartAngle());
        obj.set("endAngle", arc.getEndAngle());
        obj.set("clockwise", arc.getClockwise());
        break;
      }

      case DXFEntity::DXF_POLYLINE: {
        const DXFPolyLine &polyLine = dynamic_cast<const DXFPolyLine &>(entity);
        const vector<Vector3D> &vertices = polyLine.getVertices();
        js::Value v8Vertices = js::Value::createArray(vertices.size());

        for (unsigned k = 0; k < vertices.size(); k++) {
          js::Value pt = entityTmpl.create();

          pt.set("x", vertices[k].x());
          pt.set("y", vertices[k].y());
          pt.set("z", vertices[k].z());
          pt.set("type", DXFEntity::DXF_POINT);

          v8Vertices.set(k, pt);
        }

        obj.set("vertices", v8Vertices);
        break;
      }

      case DXFEntity::DXF_SPLINE: {
        const DXFSpline &spline = dynamic_cast<const DXFSpline &>(entity);

        obj.set("degree", spline.getDegree());

        // Control points
        const vector<Vector3D> &ctrlPts = spline.getControlPoints();
        js::Value v8CtrlPts = js::Value::createArray(ctrlPts.size());

        for (unsigned k = 0; k < ctrlPts.size(); k++) {
          js::Value pt = entityTmpl.create();

          pt.set("x", ctrlPts[k].x());
          pt.set("y", ctrlPts[k].y());
          pt.set("z", ctrlPts[k].z());
          pt.set("type", DXFEntity::DXF_POINT);

          v8CtrlPts.set(k, pt);
        }
        obj.set("ctrlPts", v8CtrlPts);

        // Knots
        const vector<double> &knots = spline.getKnots();
        js::Value v8Knots = js::Value::createArray(knots.size());

        for (unsigned k = 0; k < knots.size(); k++)
          v8Knots.set(k, knots[k]);

        obj.set("knots", v8Knots);
        break;
      }

      default: THROWS("Invalid DXF entity type " << entity.getType());
      }

      obj.set("type", entity.getType());
      v8Layer.set(j, obj);
    }

    v8Layers.set(it->first, v8Layer);
  }

  return v8Layers;
}
Exemple #13
0
js::Value MatrixModule::rotateCB(const js::Arguments &args) {
  matrix.rotate(args.getNumber("angle"), args.getNumber("x"),
                args.getNumber("y"), args.getNumber("z"), parseMatrix(args));
  return js::Value();
}
Exemple #14
0
js::Value MatrixModule::scaleCB(const js::Arguments &args) {
  matrix.scale(args.getNumber("x"), args.getNumber("y"), args.getNumber("z"),
               parseMatrix(args));
  return js::Value();
}