API converter

Draft version of dxf/dwg conversion API.

C# Code

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace NestingCenter
{

// Nesting Center conversion services.
public class NestingConverter
{

// NestingCenter API url.
private const string serviceUrl = “https://api-converters.nestingcenter.com/dxfconverter/”;

// Convert a DXF or DWG drawing.
public static async Task<JObject> ConvertPart(HttpClient client, byte[] drawingData)
{

JObject conversionData = new JObject();
conversionData[“ContentBase64”] = Convert.ToBase64String(drawingData);
string json = JsonConvert.SerializeObject(conversionData);

string url = NestingConverter.serviceUrl + “convertPart”;
JObject result = await Nesting.PostAsync(client, url, json);
return result;

}

// Convert a DXF or DWG drawing.
public static async Task<JObject> ConvertRawPlate(HttpClient client, byte[] drawingData)
{

JObject conversionData = new JObject();
conversionData[“ContentBase64”] = Convert.ToBase64String(drawingData);
string json = JsonConvert.SerializeObject(conversionData);

string url = NestingConverter.serviceUrl + “convertRawPlate”;
JObject result = await Nesting.PostAsync(client, url, json);
return result;

}

// Create a DXF.
public static async Task<byte[]> CreateDrawing(HttpClient client, string requestData)
{

string url = NestingConverter.serviceUrl + “generateReport”;
JObject result = await Nesting.PostAsync(client, url, requestData);

if (result != null)
{
return Convert.FromBase64String(result[“ContentBase64”].ToString());
}

return null;

}

}

}

Example 1

string nestingData = File.ReadAllText(“..\\..\\example.json”);
JObject nestingDataObj = JObject.Parse(nestingData);
JArray parts = nestingDataObj[“Context”][“Problem”][“Parts”] as JArray;
byte[] drawingData = File.ReadAllBytes(“..\\..\\Rectangle200x100.dwg”);
JObject conversionResult = await NestingConverter.ConvertPart(client, drawingData);

foreach (var part in conversionResult[“Parts”])
{
parts.Add(part);
}

nestingData = JsonConvert.SerializeObject(nestingDataObj);

Example 2

JObject dxfRequest = new JObject();
dxfRequest[“JobId”] = computationUrl.Substring(computationUrl.LastIndexOf(‘/’) + 1);
dxfRequest[“ResultVersion”] = resultId;
string jsonRequest = JsonConvert.SerializeObject(dxfRequest);

byte[] resultDrawing = await NestingConverter.CreateDrawing(client, jsonRequest);