Save design results
Design Atoms allows you to save products to state files for long-term storage. For example, you can transfer these files to another computer for editing and rendering print-ready files.
You can also use the client-side and server-side code for serialization and deserialization of the object model in the JSON format. For example, you can serialize your object model on the client side and pass it to the server within an HTTP request.
Saving products to state files
Design Atoms provides the State class to store the internal product representation and the viewer's configuration. You can save these states for long-term storage by using the IStateFileSerializer interface. The following example illustrates how you can get an instance of the StateFileSerializer and save your state to a file.
using Aurigma.DesignAtoms.Configuration;
using Aurigma.DesignAtoms.Serialization;
using Newtonsoft.Json;
Injector.CurrentFactory = () => DefaultInjectorFactory.Create();
JsonConvert.DefaultSettings = () => DefaultJsonSettings.Get(Injector.Instance.Resolve<IConfiguration>());
var stateFileSerializer = Injector.Instance.Resolve<IStateFileSerializer>();
var state = new State(product);
using (var stateStream = File.OpenWrite("state.st"))
{
stateFileSerializer.Serialize(state, stateStream);
}
To load a product from a state file, you can use the following code.
using (var stateStream = File.OpenRead("state.st"))
{
var state = stateFileSerializer.Deserialize(stateStream);
var product = state.Product;
}
Connecting a frontend to a backend
To connect your frontend to a backend, you need to specify a running backend URL when creating your canvas viewer.
import { Viewer } from "@aurigma/design-atoms/Viewer/Viewer";
// A URL that links to a backend instance.
const backendUrl = "http://<instance_url>";
// An HTML element that contains the viewer.
const holder = document.querySelector(holderId) as HTMLDivElement;
// Initialization of the viewer.
const viewer = new Viewer({
holderElement: holder,
backendUrl: backendUrl,
surface: product.surfaces.get(0),
canvasBackground: { color: "white" }
});
Here, backendUrl may refer to either a custom backend or a running Design Editor instance. The viewer appears in a DIV element referenced by holderId.
To transfer products between the frontend and backend, for example, in HTTP requests, you can use the JSON serialization API. To serialize and deserialize a Product object to or from the JSON format, you can use the JsonProductSerializer class on the frontend and the IProductSerializer interface on the backend.
For example, on the frontend, you can serialize your product as follows:
import { JsonProductSerializer } from "@aurigma/design-atoms-model/Product/Serializer/JsonProductSerializer";
var serializedProduct = new JsonProductSerializer().serialize(product);
After that, on the backend, you can deserialize this product as follows:
using Aurigma.DesignAtoms.Serialization.Json.Product;
var jsonSerializer = Injector.Instance.Resolve<IProductSerializer>();
var product = jsonSerializer.Deserialize(serializedProduct);
The sample
This sample illustrates how you can save a product to a state file and then load this state file to render a proof image by using Design Atoms.
using System.IO;
using Aurigma.DesignAtoms.Configuration;
using Aurigma.DesignAtoms.Configuration.RenderingConfig;
using Aurigma.DesignAtoms.Model;
using Aurigma.DesignAtoms.Rendering;
using Aurigma.DesignAtoms.Serialization;
using Autofac;
using Newtonsoft.Json;
namespace DesignAtomsConsole
{
class Program
{
static void Main(string[] args)
{
// Setting up the dependency injection.
Injector.CurrentFactory = () => DefaultInjectorFactory.Create();
// Setting up the serialization.
JsonConvert.DefaultSettings = () => DefaultJsonSettings.Get(Injector.Instance.Resolve<IConfiguration>());
CreateState(@"C:\CustomersCanvas\states\one-page.st");
RenderState(@"C:\CustomersCanvas\states\one-page.st", @"C:\CustomersCanvas\previews\one-page.jpg");
}
static void CreateState(string newStateFile)
{
// Create a product.
var product = new Product();
var surface = new Surface(330, 600);
product.Surfaces.Add(surface);
// Instantiate IStateFileSerializer.
var stateFileSerializer = Injector.Instance.Resolve<IStateFileSerializer>();
var state = new Aurigma.DesignAtoms.Model.State(product);
using (var stateStream = File.OpenWrite(newStateFile))
{
// Save the product to a state file.
stateFileSerializer.Serialize(state, stateStream);
}
}
static void RenderState(string stateFileName, string resultFileName)
{
using (var fstream = File.OpenRead(stateFileName))
{
// Instantiate IStateFileSerializer.
var stateFileSerializer = Injector.Instance.Resolve<IStateFileSerializer>();
// Load a product from a state file.
var state = stateFileSerializer.Deserialize(fstream);
var product = state.Product;
// Instantiate IProductRenderer.
var productRenderer = Injector.Instance.Resolve<IProductRenderer>();
using (var memoryStream = new MemoryStream())
{
// Render the product to a stream.
productRenderer.RenderProof(memoryStream, product.Surfaces.First(), RenderingConfig.GetDefault());
// Write the resulting memory stream to a file.
memoryStream.Position = 0;
using (FileStream outputStream = File.OpenWrite(resultFileName))
{
outputStream.Write(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
}
}
}
}
}