Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C#

Grandiose Projects 3. Compatibility with Simulink

Rate me:
Please Sign up or sign in to vote.
4.27/5 (11 votes)
8 Feb 2010CPOL23 min read 47.7K   5.9K   38  
Import of Simulink files
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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


namespace AggregateLibrary
{
    public partial class FormFlyWhell : Form, IUpdatableForm
    {
        private FlyWheel flywheel;

        private ComboBox[] comboAli;

        private ComboBox[] comboForces;
       
        private ComboBox[] comboInertial;

        private TextBox[][] moments;

        private TextBox[] linear;
        private TextBox[] angular;


        private bool entered = false;

        private FormFlyWhell()
        {
            InitializeComponent();
           // ResourceService.Resources.LoadControlResources(this);
        }

        internal FormFlyWhell(FlyWheel flywheel)
            : this()
        {
            this.flywheel = flywheel;
            UpdateFormUI();
            createCombo();
            fillText();
            fillInitial();
            fillTable();
          
        }

        #region IUpdatableForm Members

        public void UpdateFormUI()
        {
            CategoryTheory.IAssociatedObject ao = flywheel as CategoryTheory.IAssociatedObject;
            object o = ao.Object;
            if (o == null)
            {
                return;
            }
            IObjectLabel l = o as IObjectLabel;
            Text = l.Name;
        }

        #endregion

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        void createCombo()
        {
            comboAli = new ComboBox[] {comboBoxXA, comboBoxYA, comboBoxZA,
                comboBoxVxA, comboBoxVyA, comboBoxVzA,
                comboBoxQ0, comboBoxQ1, comboBoxQ2, comboBoxQ3,
                comboBoxOMx, comboBoxOMy, comboBoxOMz};
            comboForces = new ComboBox[]  { comboBoxFxA, comboBoxFyA, comboBoxFzA,
                comboBoxMxA, comboBoxMyA, comboBoxMzA};

            comboInertial = new ComboBox[] { comboBoxFxR, comboBoxFyR, comboBoxFzR };
            fillCombo();
        }

        void fillCombo()
        {
            ICollection<string> ali = flywheel.AllAliases;
            ControlUtilites.FillCombo(comboAli, ali);
            ICollection<string> mea = flywheel.AllMeasurements;
            ControlUtilites.FillCombo(comboForces, mea);
            ControlUtilites.FillCombo(comboInertial, mea);
            ControlUtilites.FillCombo(comboBoxMomentOfFlyWeel, mea);
            selectCombo();
        }

        void selectCombo()
        {
            ControlUtilites.SelectCombo(comboAli, flywheel.AliasNames);
            ControlUtilites.SelectCombo(comboForces, flywheel.Forces);
            ControlUtilites.SelectCombo(comboInertial, flywheel.Inretial);
            ControlUtilites.SelectCombo(comboBoxMomentOfFlyWeel, flywheel.FlyWheelMoment);
        }


        void fillText()
        {
            moments = new TextBox[3][];
            moments[0] = new TextBox[]{textBoxJxx, textBoxJxy, textBoxJxz};
            moments[1] = new TextBox[]    {textBoxJyy, textBoxJyz};
            moments[2] = new TextBox[]     {textBoxJzz};

            double[,] m = flywheel.MomentOfInertia;
            for (int i = 0; i < 3; i++)
            {
                TextBox[] tb = moments[i];
                for (int j = 0; j < tb.Length; j++)
                {
                    tb[j].Text = m[i, j + i] + "";
                }
            }
            textBoxMass.Text = flywheel.Mass + "";
            textBoxFlyWheel.Text = flywheel.FlyWheelMomentValue + "";
            linear = new TextBox[] { textBoxX, textBoxY, textBoxZ, textBoxVx, textBoxVy, textBoxVz };
            angular = new TextBox[] { textBoxOMx, textBoxOMy, textBoxOMz};//, textBoxAngle, textBoxAngular };

        }

        void fillInitial()
        {
            double[] init = flywheel.InitialState;
            double[] q = new double[4];
            Array.Copy(init, 6, q, 0, 4);
            double[,] m = new double[3, 3];
            double[,] qq = new double[4, 4];
            Vector3D.V3DOperations.CalculateMatrix(q, m, qq);
            fillMatrix(m);
            for (int i = 0; i < 6; i++)
            {
                linear[i].Text = init[i] + "";
            }
            for (int i = 0; i < 3; i++)
            {
                angular[i].Text = init[i + 10] + "";
            }
        }


        void fillMatrix(double[,] m)
        {
            DataTable table = dataSetMatrix.Tables[0];
            table.Clear();
            string[] c = new string[] { "X", "Y", "Z" };
            for (int i = 0; i < 3; i++)
            {
                object[] o = new object[] { c[i], m[i, 0], m[i, 1], m[i, 2] };
                table.Rows.Add(o);
            }
        }

        void norm()
        {
            double[,] m = new double[3, 3];
            DataTable t = dataSetMatrix.Tables[0];
            for (int i = 0; i < t.Rows.Count; i++)
            {
                DataRow row = t.Rows[i];
                for (int j = 0; j < 3; j++)
                {
                    m[i, j] = (double)row[j + 1];
                }
            }
            Vector3D.V3DOperations.NormMatrix(m);
            fillMatrix(m);
 
        }

        double[] coordinates
        {
            get
            {
                double[] p = new double[3];
                for (int i = 0; i < 3; i++)
                {
                    p[i] = Double.Parse(linear[i].Text);
                }
                return p;
            }
        }



        void fillTable()
        {
            int n = flywheel.NumberOfConnections;
            entered = true;
            for (int i = 0; i < n; i++)
            {
                DataRow row = dataTableConnections.NewRow();
                row[0] = i + 1;
                double[] conn = flywheel.GenConnection(i);
                for (int j = 0; j < conn.Length; j++)
                {
                    row[j + 1] = conn[j];
                }
                dataTableConnections.Rows.Add(row);
            }
            entered = false;
        }

        void accept()
        {
 //           try
  //          {
                double[,] m = new double[3, 3];

                for (int i = 0; i < 3; i++)
                {
                    TextBox[] tb = moments[i];
                    for (int j = i; j < 3; j++)
                    {
                        double a = Double.Parse(tb[j - i].Text);
                        m[i, j] = a;
                        m[j, i] = a;
                    }
                }
                double mass = Double.Parse(textBoxMass.Text);
                string[] inert = ControlUtilites.GetSelectedStringArray(comboInertial);
                string[] forces = ControlUtilites.GetSelectedStringArray(comboForces);
                Dictionary<int, string> dic = ControlUtilites.GetSelectedDictionary(comboAli);
                double[] inc = new double[15];
                double[] lin = ControlUtilites.GetDoubleTextArray(linear);
                double[] ang = ControlUtilites.GetDoubleTextArray(angular);
                Array.Copy(lin, inc, 6);
                Array.Copy(ang, 0, inc, 10, 3);
                norm();
                double[,] mo = new double[3, 3];
                DataTable t = dataSetMatrix.Tables[0];
                for (int i = 0; i < t.Rows.Count; i++)
                {
                    DataRow row = t.Rows[i];
                    for (int j = 0; j < 3; j++)
                    {
                        mo[i, j] = (double)row[j + 1];
                    }
                }
                double[] q = new double[4];
                double[,] qq = new double[4, 4];
                Vector3D.V3DOperations.ConvertMatrixToQuaternion(q, mo);
                Array.Copy(q, 0, inc, 6, 4);
                double[][] conn = null;
                normConnections();
                int n = dataTableConnections.Rows.Count;
                if (n > 0)
                {
                    conn = new double[n][];
                }
                for (int i = 0; i < n; i++)
                {
                    double[] a = new double[7];
                    conn[i] = a;
                    DataRow row = dataTableConnections.Rows[i];
                    for (int j = 0; j < a.Length; j++)
                    {
                        a[j] = (double)row[j + 1];
                    }
                    double b = 0;
                    for (int j = 3; j < 7; j++)
                    {
                        double x = a[j];
                        b += x * x;
                    }
                    b = 1 / Math.Sqrt(b);
                    for (int j = 3; j < 7; j++)
                    {
                        a[j] *= b;
                    }
                }
                double fm = Double.Parse(textBoxFlyWheel.Text);
                string fs = null;
                object of = comboBoxMomentOfFlyWeel.SelectedItem;
                if (of != null)
                {
                    fs = of + "";
                }
                double[] ic = new double[13];
                Array.Copy(inc, ic, 13);
                flywheel.Set(dic, forces, inert, fm, m, mass, ic, fs);
  //          }
  //          catch (Exception)
  //          {
  //          }
        }

        private void normConnections()
        {
            entered = true;
            List<DataRow> l = new List<DataRow>();
            foreach (DataRow row in dataTableConnections.Rows)
            {
                l.Add(row);
            }
            foreach (DataRow row in l)
            {
                double a = 0;
                for (int i = 3; i < 8; i++)
                {
                    double b = (double)row[i];
                    a += b * b;
                }
                a = 1 / Math.Sqrt(a);
                for (int i = 3; i < 8; i++)
                {
                    double b = (double)row[i];
                    row[i] = b * a;
                }
            }
            entered = false;
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            accept();
        }

        private void dataGridViewConnections_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            if (entered)
            {
                return;
            }
            normConnections();
        }

        private void buttonNorm_Click(object sender, EventArgs e)
        {
            try
            {
                norm();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonAlong_Click(object sender, EventArgs e)
        {
            try
            {
                double[] p = coordinates;
                double a = Double.Parse(textBoxRot.Text);
                double[,] m = Motion6D.ReferenceFrame.CalucateViewMatrix(p, a);
                fillMatrix(m);
            }
            catch (Exception)
            {
            }

        }


    }
}

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