Click here to Skip to main content
15,884,472 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.Utils;
using DiagramUI.Aliases;

namespace DiagramUI
{
    /// <summary>
    /// Standard alias editor
    /// </summary>
    public partial class FormStandardAliasEditor : Form
    {
        private AliasItem item;

        private int[] dim;

        private object bt = null;


        private static readonly Double d = 0;

        private static readonly Boolean b = false;


        private static readonly Int32 i = 0;


        private FormStandardAliasEditor()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Consructor
        /// </summary>
        /// <param name="item">The item</param>
        public FormStandardAliasEditor(AliasItem item)
            : this()
        {
            ResourceService.Resources.LoadControlResources(this, ControlUtilites.Resources);
            this.item = item;
            Text = Text + " [" + item.Name + "]";
            bt = AliasTypeDetector.Detector.DetectType(item.Value);
            dim = AliasTypeDetector.Detector.GetDimension(bt);
            if (dim != null)
            {
                bt = AliasTypeDetector.Detector.GetBaseType(bt);
            }
            Set();
            fill();
        }


        object itemType
        {
            get
            {
                return item.Alias.GetType(item.Name);
            }
        }

        object itemValue
        {
            get
            {
                return item.Alias[item.Name];
            }
            set
            {
                item.Alias[item.Name] = value;
            }
        }


        private bool isBool
        {
            set
            {
               checkBoxBool.Visible = value;
                textValue.Visible = !value;
            }
            get
            {
                return checkBoxBool.Visible;
            }
        }


        void Set()
        {
            isBool = bt.Equals(b);
            if (dim == null)
            {
                tabInput.SelectTab(tabSimple);
                return;
            }
            if (dim.Length == 1)
            {
                tabInput.SelectTab(tabVector);
                numericUpDownDim.Value = dim[0];
            }
            if (dim.Length == 2)
            {
                tabInput.SelectTab(tabMatrix);
                numericUpDownRows.Value = dim[0];
                numericUpDownColumns.Value = dim[1];
                return;
            }
        }

        object val
        {
            get
            {
                if (dim == null)
                {
                    if (bt.Equals(b))
                    {
                        return checkBoxBool.Checked;
                    }
                    if (bt.Equals(d))
                    {
                        return Double.Parse(textValue.Text);
                    }
                    if (bt.Equals(i))
                    {
                        return Int32.Parse(textValue.Text);
                    }
                }
                if (dim.Length == 1)
                {
                    return vectValue;
                }
                if (dim.Length == 2)
                {
                    return matrValue;
                }
                return null;
            }
        }

        object vectValue
        {
            get
            {
                if (bt.Equals(b))
                {
                    bool[] bo = new bool[dim[0]];
                    for (int i = 0; i < bo.Length; i++)
                    {
                       DataGridViewRow row = dataGridViewVector.Rows[i];
                       bo[i] = (bool)row.Cells[1].FormattedValue;
                    }
                    return bo;
                }
                if (bt.Equals(d))
                {
                    double[] dd = new double[dim[0]];
                    for (int i = 0; i < dd.Length; i++)
                    {
                        DataGridViewRow row = dataGridViewVector.Rows[i];
                        dd[i] = Double.Parse(row.Cells[1].FormattedValue + "");
                    }
                    return dd;
                }
                if (bt.Equals(FormStandardAliasEditor.i))
                {
                    int[] ii = new int[dim[0]];
                    for (int i = 0; i < ii.Length; i++)
                    {
                        DataGridViewRow row = dataGridViewVector.Rows[i];
                        ii[i] = Int32.Parse(row.Cells[1].FormattedValue + "");
                    }
                    return ii;
                }
                return null;
            }
        }


        object matrValue
        {
            get
            {
                if (bt.Equals(b))
                {
                    bool[,] bo = new bool[dim[0], dim[1]];
                    for (int i = 0; i < bo.GetLength(0); i++)
                    {
                        DataGridViewRow row = dataGridViewMatrix.Rows[i];
                        for (int j = 0; j < bo.GetLength(1); j++)
                        {
                            bo[i, j] = (bool)row.Cells[j + 1].FormattedValue;
                        }
                    }
                    return bo;
                }
                if (bt.Equals(d))
                {
                    double[,] dd = new double[dim[0], dim[1]];
                    for (int i = 0; i < dd.GetLength(0); i++)
                    {
                        DataGridViewRow row = dataGridViewMatrix.Rows[i];
                        for (int j = 0; j < dd.GetLength(1); j++)
                        {
                            dd[i, j] = Double.Parse(row.Cells[j + 1].FormattedValue + "");
                        }
                    }
                    return dd;
                }
                if (bt.Equals(FormStandardAliasEditor.i))
                {
                    int[,] ii = new int[dim[0], dim[1]];
                    for (int i = 0; i < ii.GetLength(0); i++)
                    {
                        DataGridViewRow row = dataGridViewMatrix.Rows[i];
                        for (int j = 0; j < ii.GetLength(1); j++)
                        {
                            ii[i, j] = Int32.Parse(row.Cells[j + 1].FormattedValue + "");
                        }
                    }
                    return ii;
                }
                return null;
            }
        }

        void defaultFill()
        {
            clearGrids();
            if (dim == null)
            {
                if (isBool)
                {
                    
                }
                return;
            }
            if (dim.Length == 1)
            {
                defaultFillVector();
            }
            if (dim.Length == 2)
            {
                defaultFillMatrix();
            }
        }

        void fill()
        {
            if (bt.Equals(d))
            {
                comboBoxType.SelectedIndex = 0;
            }
            if (bt.Equals(i))
            {
                comboBoxType.SelectedIndex = 1;
            }
            if (bt.Equals(b))
            {
                comboBoxType.SelectedIndex = 2;
            }
            clearGrids();
            if (dim == null)
            {
                if (isBool)
                {
                    checkBoxBool.Checked = (bool)item.Value;
                }
                else
                {
                    textValue.Text = item.Value + "";
                }
                return;
            }
            if (dim.Length == 1)
            {
                fillVector();
            }
            if (dim.Length == 2)
            {
                fillMatrix();
            }
        }


        void defaultFillVector()
        {
            DataGridViewColumn c = newColumn;
            dataGridViewVector.Columns.Add(c);
            for (int k = 0; k < dim[0]; k++)
            {
                object[] o = new object[] { (k + 1) + "", bt };
                dataGridViewVector.Rows.Add(o);
            }
        }

        void fillVector()
        {
            Array arr = item.Value as Array;
            DataGridViewColumn c = newColumn;
            dataGridViewVector.Columns.Add(c);
            for (int i = 0; i < arr.Length; i++)
            {
                object[] o = new object[] { (i + 1) + "", arr.GetValue(i) };
                dataGridViewVector.Rows.Add(o);

            }
        }

        void defaultFillMatrix()
        {
            for (int k = 0; k < dim[1]; k++)
            {
                DataGridViewColumn dc = createColumn(k);
                dataGridViewMatrix.Columns.Add(dc);
            }
            for (int k = 0; k < dim[0]; k++)
            {
                object[] o = new object[dim[1] + 1];
                o[0] = (k + 1) + "";
                for (int l = 0; l < dim[1]; l++)
                {
                    o[l + 1] = bt;
                }
                dataGridViewMatrix.Rows.Add(o);
            }
        }


        void fillMatrix()
        {
            Array arr = item.Value as Array;
            for (int k = 0; k < dim[1]; k++)
            {
                DataGridViewColumn dc = createColumn(k);
                dataGridViewMatrix.Columns.Add(dc);
            }
            int[] n = new int[2];
            for (int k = 0; k < dim[0]; k++)
            {
                object[] o = new object[dim[1] + 1];
                o[0] = (k + 1) + "";
                n[0] = k;
                for (int l = 0; l < dim[1]; l++)
                {
                    n[1] = l;
                    o[l + 1] = arr.GetValue(n);
                }
                dataGridViewMatrix.Rows.Add(o);
            }

        }


        void clearGrids()
        {
            for (int k = dataGridViewMatrix.ColumnCount - 1; k > 0; k--)
            {
                dataGridViewMatrix.Columns.RemoveAt(k);
            }
            if (dataGridViewVector.Columns.Count == 2)
            {
                dataGridViewVector.Columns.RemoveAt(1);
            }
            dataGridViewMatrix.Rows.Clear();
            dataGridViewVector.Rows.Clear();
        }

        private DataGridViewColumn createColumn(int i)
        {
            DataGridViewColumn c = newColumn;
            c.HeaderText = (i + 1) + "";
            return c;
        }


        private DataGridViewColumn newColumn
        {
            get
            {
                if (bt.Equals(b))
                {
                    return new DataGridViewCheckBoxColumn();
                }
                return new DataGridViewTextBoxColumn();
            }
         }

/*
        private DataGridViewCell viewCell
        {
            get
            {
                if (bt.Equals(b))
                {
                    return new DataGridViewCheckBoxColumn();
                }
                return new DataGridViewTextBoxCell();
            }
        }*/

        void defOld()
        {
            if (dim == null)
            {
                if (isBool)
                {
                    //comboBoxBool.SelectedIndex = 0;
                }
                return;
            }
            IList<DataColumn> lc = new List<DataColumn>();
            foreach (DataColumn dc in dataTable.Columns)
            {
                if (dc != dataColumnRow)
                {
                    lc.Add(dc);
                }
            }
            foreach (DataColumn dc in lc)
            {
                dataTable.Columns.Remove(dc);
            }
            Type type = bt.GetType();
            for (int k = 0; k < dim[1]; k++)
            {
                DataColumn dc = new DataColumn();
                dc.ColumnName = (k + 1) + "";
                dc.DataType = type;
                dataTable.Columns.Add(dc);
            }
            for (int k = 0; k < dim[1]; k++)
            {
                object[] o = new object[dim[1] + 1];
                o[0] = (k + 1) + "";
                for (int l = 0; l < dim[1]; l++)
                {
                    o[l + 1] = bt;
                }
                dataTable.Rows.Add(o);
            }
            dataGridViewMatrix.Refresh();


        }


        object selectedType
        {
            get
            {
                if (tabInput.SelectedTab == tabSimple)
                {
                    dim = null;
                }
                if (tabInput.SelectedTab == tabVector)
                {
                    dim = new int[] { (int)numericUpDownDim.Value };
                }
                if (tabInput.SelectedTab == tabMatrix)
                {
                    dim = new int[] { (int)numericUpDownRows.Value, (int)numericUpDownColumns.Value };
                }
                int j = comboBoxType.SelectedIndex;
                if (j == 0)
                {
                    return d;
                }
                if (j == 1)
                {
                    return i;
                }
                return b;
            }
        }



        

        private void buttonApplyType_Click(object sender, EventArgs e)
        {
            bt = selectedType;
            Set();
            defaultFill();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                itemValue = val;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

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

    }
}

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