예제 #1
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();
}
예제 #2
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;
}