Click here to Skip to main content
15,893,588 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.Threading;
using System.Windows.Forms;
using System.ComponentModel;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

using DiagramUI;
using DiagramUI.Labels;
using CategoryTheory;

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

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

using DiagramUI.Utils;



using DataPerformer;
using DataPerformerUI;
using DataPerformer.Interfaces;

using ResourceService;

using DataWarehouse;

using EngineeringInitializer;

using BasicEngineeringUIFactory.Interfaces;
using BasicEngineeringUIFactory.Forms;


namespace BasicEngineeringUIFactory
{
    /// <summary>
    /// Basic engineering factory
    /// Contains most important features those uses a lot of enginners
    /// </summary>
    public class EngineeringUIFactory : AssemblyFactory
    {

        #region Fields



 
        event Action cancelProcess;

        /// <summary>
        /// Buttons of Statistical Objects
        /// </summary>
        public static readonly ButtonWrapper[] StatisticalObjectsButtons = new ButtonWrapper[]
        {
                            new ButtonWrapper(typeof(Regression.AliasRegression),
                    "", "Regression", ResourceImage.AliasRegression, MinimalFactory.Object, true, false),
                            new ButtonWrapper(typeof(Regression.IteratorGLM),
                    "", "Data set iterator", ResourceImage.RecoursiveGLM, MinimalFactory.Object, true, false),
                            new ButtonWrapper(typeof(Regression.CombinedSelection),
                    "", "Combined selection", ResourceImage.Sigma, MinimalFactory.Object, true, false),
                            new ButtonWrapper(typeof(Regression.XmlSelectionCollection),
                    "", "Selection from XML file", ResourceImage.XmlData, MinimalFactory.Object, true, false),
                            new ButtonWrapper(typeof(DataPerformer.SeriesIterator),
                    "", "Iterator series", ResourceImage.SeriesIterator, MinimalFactory.Object, true, false),
                            new ButtonWrapper(typeof(DiagramUI.ObjectsCollection),
                   "Regression.AliasRegression,AliasRegression", "Fisher", ResourceImage.Fisher, MinimalFactory.Object, true, false)
        };

        /// <summary>
        /// Button Arrows
        /// </summary>
        public static readonly ButtonWrapper[] ArrowButtons = new ButtonWrapper[]
        {
            new ButtonWrapper(typeof(DiagramUI.BelongsToCollection), "", 
                "Belongs link", ResourceImage.Belong, MinimalFactory.Object, true, true),
            new ButtonWrapper(typeof(DataPerformer.DataLink), "Mv", 
                "Measurement consumer link", ResourceImage.DataLink, MinimalFactory.Object, true, true),
            new ButtonWrapper(typeof(DataPerformer.ObjectTransformerLink), "", 
                "Link of object transformer", ResourceImage.TransformArrow, MinimalFactory.Object, true, true),
            new ButtonWrapper(typeof(Regression.SelectionLink), "", 
                "Link between selection and selection consumer", ResourceImage.SelectionLink, MinimalFactory.Object, true, true),
            new ButtonWrapper(typeof(DataPerformer.IteratorConsumerLink), "", 
                "Iterator link", ResourceImage.IteratorLink, MinimalFactory.Object, true, true),
            new ButtonWrapper(typeof(DataPerformer.UnaryLink), "", 
                "Unary link", ResourceImage.UnaryLink, MinimalFactory.Object, false, true)
        };

        private BackgroundWorker worker;


        /// <summary>
        /// Tools
        /// </summary>
        //protected ToolsDiagram tools;

        /// <summary>
        /// Data
        /// </summary>
        protected static DataWarehouse.DatabaseInterface data;

        /// <summary>
        /// Desktop
        /// </summary>
        protected PanelDesktop desktop;

        /// <summary>
        /// Error handler
        /// </summary>
        protected IErrorHandler errorHandler = null;

        /// <summary>
        /// The start thread
        /// </summary>
        protected ThreadStart start;

        /// <summary>
        /// Number of start step
        /// </summary>
        protected int startStep;

        /// <summary>
        /// Pause in milliseconds
        /// </summary>
        protected int pause;

        /// <summary>
        /// Step of performance
        /// </summary>
        protected double step;

        /// <summary>
        /// Time of start
        /// </summary>
        protected double startTime;

        /// <summary>
        /// Count of steps
        /// </summary>
        protected int stepCount;

        /// <summary>
        /// Number of current step
        /// </summary>
        protected int currentStep;

      /*  /// <summary>
        /// Writer of missing resources
        /// </summary>
        private static StreamWriter missWriter;*/

        /// <summary>
        /// Extention
        /// </summary>
        private string ext;

        /// <summary>
        /// Global exception
        /// </summary>
        private Exception exglo;


        #endregion


        #region Ctor

        public EngineeringUIFactory(IUIFactory[] factories, bool defaultValue, string ext)
            : base(new IUIFactory[0], defaultValue)
        {
            StaticFactoryPerformer.Factory = DefalutLabelFactory.Object;
            factory = StaticFactoryPerformer.Factory;
            this.ext = ext;
            List<IUIFactory> l = new List<IUIFactory>();
            l.Add(new DefaultFactory(ext));
            l.Add(MinimalFactory.Object);
            if (factories != null)
            {
                l.AddRange(factories);
            }
            this.factories = l.ToArray();
        }

        static EngineeringUIFactory()
        {
            Dictionary<Type, System.Drawing.Image> buttonImages =
            ButtonWrapper.CreateImageDictionary(StatisticalObjectsButtons);
            Dictionary<Type, System.Drawing.Image> d = DataPerformerUI.Labels.NamedlSeriesLabel.ImageDictionary;
            foreach (Type t in buttonImages.Keys)
            {
                if (!d.ContainsKey(t))
                {
                    d[t] = buttonImages[t];
                }
            }
        }

        #endregion

        #region Overriden

        public override void ShowError(Exception e)
        {
            if (errorHandler != null)
            {
                errorHandler.ShowError(e);
            }
        }

        #endregion


        #region Specific Members

        public event Action CancelProcess
        {
            add
            {
                cancelProcess += value;
            }
            remove
            {
                cancelProcess -= value;
            }
        }

        /// <summary>
        /// Empty initializer
        /// </summary>
        private static void emptyInit()
        {
        }


        /// <summary>
        /// Project data
        /// </summary>
        public static DataWarehouse.DatabaseInterface Data
        {
            set
            {
                data = value;
            }
        }


        /// <summary>
        /// Error handler
        /// </summary>
        public IErrorHandler ErrorHandler
        {
            set
            {
                errorHandler = value;
                PureDesktop.ErrorHandler = value;
            }
        }


        /// <summary>
        /// Desktop
        /// </summary>
        public PanelDesktop Desktop
        {
            get
            {
                return desktop;
            }
            set
            {
                desktop = value;
            }
        }

        /// <summary>
        /// Start
        /// </summary>
        /// <param name="startTime">Start time</param>
        /// <param name="startStep">Start step</param>
        /// <param name="pause">Pause</param>
        /// <param name="step">Step</param>
        /// <param name="stepCount">Step Count</param>
        /// <param name="desktop">Desktop</param>
        public void Start(double startTime, int startStep, int pause, double step, int stepCount, PanelDesktop desktop)
        {
            this.startTime = startTime;
            this.startStep = startStep;
            this.pause = pause;
            this.step = step;
            this.stepCount = stepCount;
            this.desktop = desktop;
            StartWorker();
        }

        private void work(object sender, DoWorkEventArgs e)
        {
            run();
        }

        public void StartWorker()
        {
            worker = new BackgroundWorker();
            worker.DoWork += work;
            worker.WorkerSupportsCancellation = true;
            worker.RunWorkerCompleted += Cancel;
            worker.RunWorkerAsync();
        }

        public void StopWorker()
        {
            worker.CancelAsync();
        }

        void run()
        {
            try
            {
                TimeProviderBackup.PerformFixed(startTime, step, stepCount, desktop, StaticDataPerformer.Strategy.TimeProvider,
    DifferentialEquationProcessor.Processor, StaticDataPerformer.Strategy, delegate()
    {
        Redraw();
        if (worker.CancellationPending)
        {
            throw new Exception("Stopped");
        }
    }
);

            }
            catch (Exception e)
            {
                e.Log();
                exglo = e;
                //Form f = progressIndicator as Form;
            }

        }

        private void Cancel(object sender, RunWorkerCompletedEventArgs e)
        {
            Exception ex = exglo;
            exglo = null;
            if (ex != null)
            {
                ControlUtilites.ShowError(null, ex, Common.Engineering.Localization.Utils.ControlUtilites.Resources);
            }
            if (cancelProcess != null)
            {
                cancelProcess();
            }
        }



        /// <summary>
        /// Redraws all forms
        /// </summary>
        internal void Redraw()
        {
            foreach (Control c in desktop.Controls)
            {
                if (!(c is IObjectLabelUI))
                {
                    continue;
                }
                IObjectLabelUI label = c as IObjectLabelUI;

                Form f = null;
                if (label is IShowForm)
                {
                    IShowForm sf = label as IShowForm;
                    f = sf.Form as Form;
                }
                if (f == null)
                {
                    Redraw(c);
                    continue;
                }
                if (f.IsDisposed)
                {
                    Redraw(c);
                    continue;
                }
                if (!(f is IRedraw))
                {
                    continue;
                }
                IRedraw r = f as IRedraw;
                r.Redraw();
            }
        }

        private bool Redraw(Control control)
        {
            IRedraw r = CategoryOperations.GetSimpleObject<IRedraw>(control);
            if (r != null)
            {
                r.Redraw();
                return true;
            }
            foreach (Control c in control.Controls)
            {
                if (Redraw(c))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Initialization
        /// </summary>
        /// <returns>True in case of first initialization</returns>
      /*  public static void Initialize(OrdinaryDifferentialEquations.IDifferentialEquationSolver ordSolver,
            DataPerformer.IDataPerformerStrategy strategy, IDatabaseCoordinator coordinator, IApplicationInitializer[] initializers, 
            bool throwsRepeatException)
        {
            if (BasicEngineeringInitializer.IsInitialized)
            {
                return;
            }
            EngineeringInitializer.Initialize(ordSolver,
            strategy, coordinator, initializers, throwsRepeatException);
        }*/


 




        static public string TransformEncoding(string s)
        {
            string per = "ÀÁÂÃÄŨÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÜÛÚÝÞßàáâãäå¸æçèéêëìíîïðñòóôõöøùüûúýþÿ";
            string rus = "\u0410\u0411\u0412\u0413\u0414\u0415\u0401\u0416\u0417\u0418\u0419\u041A\u041B\u041C\u041D\u041E\u041F\u0420\u0421\u0422\u0423\u0424\u0425\u0426\u0427\u0428\u0429\u042C\u042B\u042A\u042D\u042E\u042F\u0430\u0431\u0432\u0433\u0434\u0435\u0451\u0436\u0437\u0438\u0439\u043A\u043B\u043C\u043D\u043E\u043F\u0440\u0441\u0442\u0443\u0444\u0445\u0446\u0448\u0449\u044C\u044B\u044A\u044D\u044E\u044F";
            string str = "";
            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                int j = per.IndexOf(c);
                if (j > -1)
                {
                    c = rus[j];
                }
                str += c;
            }
            return str;
        }
 
        static void copyData()
        {
            return;
            /* string dsc = "AstronomyDB1";
             IUser user = new User("", "", null);
             AstronomyDB.DataCreator.CreateDatabase("3d-monstr", dsc);
             DatabaseInterface src = new DatabaseInterface(user, AstronomyDB.AstronomyWarehouse.GetInterface("3d-monstr", "AstronomyDB"));
             DatabaseInterface dst = new DatabaseInterface(user, AstronomyDB.AstronomyWarehouse.GetInterface("3d-monstr", dsc));
             DatabaseInterface.Copy(src, dst, new ExtentionNameConverter(false, false));
        
             */
        }




        #endregion
    }

    /// <summary>
    /// Initialization 
    /// </summary>
   // public delegate void Initializer();
}

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