C# sample code to call the load calculation web service for container and truck loads

This sample code shows how to pass the shipment information including the size of the 20FT container and the cargo list to the server and take the calculation back from the server. The web service calculates the load plan from the shipment information and the load plan is sent to your computer after the calculation finishes. The client take the load plan and shows the calculation to the console of your computer and saves the 3D graphics as GIF picture to a folder at your computer.

Download full source code of Microsoft C# project

Picture 1> Result of the Console at your computer

Picture 2> Result of a folder 'C:\Temp\Images' at your computer

   

Picture 3> Sample source codes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//for the shake to save JPG
using System.Drawing;
using System.Drawing.Imaging;

namespace ConsoleApplication1
{
      class Program
      {
            static void Main(string[] args)
            {
                        //Create caculation
                        WebService.CalculationClient client = new WebService.CalculationClient();

                        //Create account
                        WebService.Account Account = new WebService.Account();
                        Account.UserID = "TRIAL1";//replace with your account
                        Account.Password = "0000";//replace with your account
                        Account.Company = "Trials";//replace with your account

                        //Create options
                        WebService.Options Options = new WebService.Options();
                        Options.CalculationSaved = true;
                        Options.GraphicsCreated = true;
                        Options.GraphicsImageWidth = 300;
                        Options.GraphicsImageDepth = 300;

                        Options.UOM = WebService.Options.UOMEnum.UnitMetric;//0=UnitEnglish, 1=UnitMetric, 2=UnitHighMetric

                        //Create shipment
                        WebService.Shipment Shipment = client.GetShipment();

                        Shipment.Title = "New Shipment Sample for Vehicle Load";
                        Shipment.Description = "in C# code";

                        //Create cargoes
                        WebService.Cargo NewCargo1 = new WebService.Cargo();
                        NewCargo1.Name = "First Cargo";
                        NewCargo1.Length = 500;
                        NewCargo1.Width = 500;
                        NewCargo1.Height = 500;
                        NewCargo1.Qty = 40;

                        Shipment.Cargoes.Add(NewCargo1);

                        WebService.Cargo NewCargo2 = new WebService.Cargo();
                        NewCargo2.Name = "Second Cargo";
                        NewCargo2.Length = 700;
                        NewCargo2.Width = 800;
                        NewCargo2.Height = 700;
                        NewCargo2.Qty = 56;

                        Shipment.Cargoes.Add(NewCargo2);

                        //Define containers
                        WebService.Container NewContainer = new WebService.Container();
                        NewContainer.ContainerType = WebService.Container.ContainerTypeEnum.SeaVan;
                        NewContainer.Name = "20FT Dry";
                        NewContainer.Length = 5890;
                        NewContainer.Width = 2330;
                        NewContainer.Height = 2380;


                        Shipment.Containers.Add(NewContainer);

                        //Define Rules
                        Shipment.Rules.IsWeightLimited = false;

                        //Run
                        WebService.LoadPlan LoadPlan = client.Run(Shipment, Account, Options);

                        //Show the return
                        Console.WriteLine(LoadPlan.Status);

                        if (LoadPlan.Status == "OK")
                        {
                            //Access the restuls
                            Console.WriteLine("# of Containers =" + LoadPlan.FilledContainers.Count);
                            try
                            {
                                  foreach (WebService.FilledContainer Container in LoadPlan.FilledContainers)
                                 {
                                         Console.WriteLine(Container.Name);

                                         byte[] bytes = Convert.FromBase64String(Container.BytesImage3D);
                                         System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);

                                         Image img = Image.FromStream(ms);

                                         img.Save("c:\\temp\\images\\" + Container.Name + ".gif", System.Drawing.Imaging.ImageFormat.Gif);

                                         //Spaces
                                         foreach (WebService.SpaceInContainer Space in Container.Spaces)
                                         {
                                               Console.WriteLine(Space.IsEmpty);

                                               if (Space.IsEmpty == false)
                                               {
                                                    bytes = Convert.FromBase64String(Space.BytesImage3D);
                                                    ms = new System.IO.MemoryStream(bytes);

                                                    img = Image.FromStream(ms);

                                                    img.Save("c:\\temp\\images\\" + Container.Name + Space.Sequence + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
                                                }
                                         }

                                         //Manifest (Load List)
                                         foreach (WebService.ManifestLine ManifestLine in Container.Manifest)
                                         {
                                             Console.WriteLine(ManifestLine.Sequence + ":" + ManifestLine.Cargo.Name + " x " + ManifestLine.CargoQty);
                                         }
                                 }
                           }
                           catch (Exception ex)
                           {
                                Console.WriteLine(ex.Message);
                           }
                        }//LoadPlan.Status == "OK"

                        Console.WriteLine("Calculation saved at www.cubemaster.net = " + (Options.CalculationSaved == true ? "Yes" : "No"));
                        Console.WriteLine("3D graphics created = " + (Options.GraphicsCreated == true ? "Yes" : "No"));

                        Console.WriteLine("Press any key to exit...");
                        Console.ReadKey(true);

                        client.Close();
              }
      }
}

   
Copyright ©2013 Logen Solutions, Co., Ltd. All rights reserved.