Click here to Skip to main content
15,893,337 members
Articles / Programming Languages / C#

Reconstruction of Charts from Images

Rate me:
Please Sign up or sign in to vote.
4.92/5 (13 votes)
18 Mar 2010CPOL7 min read 38K   4.1K   42  
Usage of universal framework for chart reconstruction
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

using CategoryTheory;

using DiagramUI.Labels;
using Diagram.UI.Factory;
using Diagram.UI.Interfaces.Labels;
using Diagram.UI.Interfaces;
using DiagramUI.Interfaces;

namespace DiagramUI.Factory
{
    /// <summary>
    /// Default Factory
    /// </summary>
    public class DefaultFactory : EmptyUIFactory
    {
        #region Fields

        private string ext;

        /// <summary>
        /// Buttons of default objects
        /// </summary>
        public static readonly ButtonWrapper[] DefaultObjectButtons =
            new ButtonWrapper[]
             {
             new ButtonWrapper(typeof(DiagramUI.MultiLibraryObject),
                    "", "Multi library", ResourceImage.MultiInterface, null, true, false)
             };
        
        #endregion

        #region Ctor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="ext">Extention</param>
        public DefaultFactory(string ext)
        {
            this.ext = ext;
        }

        #endregion

        #region Overriden Members

        /// <summary>
        /// Creates object from button
        /// </summary>
        /// <param name="button">The button</param>
        /// <returns>The object</returns>
        public override ICategoryObject CreateObject(IPaletteButton button)
        {
            if (button.ReflectionType.Equals(typeof(LibraryObjectWrapper)))
            {
                return LibraryObjectWrapper.Create(button.Kind);
            }
            if (button.ReflectionType.Equals(typeof(ObjectContainer)))
            {
                return load(button.Kind);
            }
            return null;
        }

        /// <summary>
        /// Creates object label
        /// </summary>
        /// <param name="button">Corresponding button</param>
        /// <returns>The object label</returns>
        public override IObjectLabelUI CreateObjectLabel(IPaletteButton button)
        {
            Type t = button.ReflectionType;
            if (t.Equals(typeof(IObjectContainer)))
            {
                return new ContainerObjectLabel(button);
            }
            return null;
        }

        #endregion

        #region Members

        ICategoryObject load(string str)
        {
            Stream stream = null;
            bool cont = true;
            string fn = ResourceService.Resources.CurrentDirectory + "Containers" + Path.DirectorySeparatorChar + str;
            if (File.Exists(fn))
            {
                stream = File.OpenRead(fn);
                int n = fn.LastIndexOf('.');
                if (fn.Substring(n + 1).ToLower().Equals(ext))
                {
                    cont = false;
                }
            }
           /* else
            {
                string ext = null;
                byte[] buffer = data.GetData(str, ref ext);
                ext = ext.Trim();
                if (!ext.ToLower().Equals("cont"))
                {
                    cont = false;
                }
                stream = new MemoryStream(buffer);
            }*/
            if (cont)
            {
                return loadContainer(stream) as ICategoryObject;
            }
            return loadMultilibrary(stream);
        }

        IObjectContainer loadContainer(Stream stream)
        {
            BinaryFormatter binary = new BinaryFormatter();
            IObjectContainer ob = binary.Deserialize(stream) as IObjectContainer;
            stream.Close();
            return ob;
        }


        MultiLibraryObject loadMultilibrary(Stream stream)
        {
            BinaryFormatter binary = new BinaryFormatter();
            SerializationBinder[] binders = null;
            PureDesktopPeer d = new PureDesktopPeer();
            d.Load(stream, binders, true);
            stream.Close();
            IObjectLabel l = d.Objects[0] as IObjectLabel;
            ICategoryObject ob = l.Object;
            return ob as MultiLibraryObject;
        }



        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions