Saving Products

The Design Atoms Framework 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

The Design Atoms Framework 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.

C#
using Aurigma.DesignAtoms.Configuration;
using Aurigma.DesignAtoms.Serialization;

Injector.CurrentFactory = () => DefaultInjectorFactory.Create();
JsonConvert.DefaultSettings = () => DefaultJsonSettings.Get(Injector.Instance.Resolve<IConfiguration>(), Injector.Instance.Resolve<ILogger>());

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.

C#
using (var stateStream = File.OpenRead("state.st"))
{
    var state = stateFileSerializer.Deserialize(stateStream);
    var product = state.Product;
}

Connecting a Front End to a Back End

To connect your front end to a back end, you need to specify a running back-end URL when creating your canvas viewer.

TypeScript
import { Viewer } from "@aurigma/design-atoms/Viewer/Viewer";
// A URL that links to a back-end instance.
const backendUrl = "http://<yourInstanceUrl>";
// 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 both a custom back end and Customer's Canvas 5, for example, deployed in the Aurigma's cloud. The viewer appears in a DIV element referenced by holderId.

To transfer products between the front end and back end, 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 front end and the ProductSerializer class on the back end.

For example, on the front end, you can serialize your product as follows:

TypeScript
import { JsonProductSerializer } from "@aurigma/design-atoms/Model/Product/Serializer/JsonProductSerializer";
var serializedProduct = new JsonProductSerializer().serialize(product);

After that, on the back end, you can deserialize this product as follows:

C#
using Aurigma.DesignAtoms.Serialization.Json.Product;
var jsonSerializer = Injector.Instance.Resolve<ProductSerializer>();
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 the Design Atoms Framework.

Program.cs
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 Aurigma.DesignAtoms.Services.Logger;
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>(), Injector.Instance.Resolve<ILogger>());

            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[0], 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);
                    }
                }
            }
        }
    }
}

See Also

Manual

Reference

Other