private void btnSave_Click(object sender, EventArgs e)
        {
            Gear g = new Gear();
            g.Name = txtName.Text;
            g.Alpha = Convert.ToDouble(numAlpha.Value);
            g.GearModule = Convert.ToDouble(numGearModule.Value);
            g.Torque = Convert.ToDouble(numTorque.Value);
            g.WantedRadius = Convert.ToDouble(numWantedRadius.Value);
            g.Shaft = shaft;
            g.Material = material;

            if (g.Name != "" && g.Material != null && g.Shaft != null)
            {
                Database1DataSet1TableAdapters.GearsTableAdapter gta = new Database1DataSet1TableAdapters.GearsTableAdapter();
                gta.Insert(g.Name, g.Alpha, g.GearModule, g.Material.ID, g.Torque, g.Shaft.ID,g.WantedRadius, 0,0,0,0,0,0);
                g.ID = (int)gta.getID(g.Name, g.Alpha, g.GearModule, g.Material.ID, g.Torque, g.Shaft.ID, 0, 0, 0, 0, 0, 0, 0);

                Program.gearList.Add(g);
                DialogResult = DialogResult.OK;
            }
            else
            {
                if (g.Name == "")
                    MessageBox.Show("You have to add a Name to your gear", "Impossible to save the gear", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else if (g.Shaft == null)
                    MessageBox.Show("You have to add a Shaft to your gear", "Impossible to save the gear", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else
                    MessageBox.Show("You have to add a Material to your gear", "Impossible to save the gear", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
        }
        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (listGear.SelectedItems.Count > 0)
            {
                Gear gearToDelete = new Gear();
                gearToDelete = (Gear)listGear.SelectedItems[0];
                foreach (Gear g in Program.gearList)
                {
                    if (g.Equals(gearToDelete))
                    {
                        gearToDelete = g;
                    }
                }
                Program.gearList.Remove(gearToDelete);
                Database1DataSet1TableAdapters.GearsTableAdapter gta = new Database1DataSet1TableAdapters.GearsTableAdapter();
                gta.Delete(gearToDelete.ID, gearToDelete.Name, gearToDelete.Alpha, gearToDelete.GearModule, gearToDelete.Material.ID, gearToDelete.Torque, gearToDelete.Shaft.ID, gearToDelete.WantedRadius, gearToDelete.Z, gearToDelete.Phi, gearToDelete.R_base, gearToDelete.R_foot, gearToDelete.R_pitch, gearToDelete.R_top);

            }
            refreshList();
            treeViewGear.Nodes.Clear();
            propertyGridGear.SelectedObject = null;
            btnRemove.Enabled = false;
            btnEdit.Enabled = false;
            btnSelect.Enabled = false;
        }
        private void btnOutputGear_Click(object sender, EventArgs e)
        {
            GearEditorForm gearEditor = new GearEditorForm();
            gearEditor.menuStripVisible(false);
            if (gearEditor.ShowDialog() == DialogResult.OK)
            {
                // Ajouter le nom du gear
                outputGear = gTemp;

                //Changer le bouton Choose correspondant
                txtOutputGear.Text = outputGear.Name;
                txtOutputGear.Visible = true;
                btnOutputGear.Text = "...";
                btnOutputGear.Size = btnSize;
                btnOutputGear.Location = new Point(188, 220);
            }
        }
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            btnValidate.Visible = false;

            if (txtName.Text == "")
               MessageBox.Show("You have to add a Name to your Gear Box", "Impossible to generate a GearBox ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else if (txtInputMaterial.Tag == null)
                MessageBox.Show("You have to add a Material to your InputGear", "Impossible to generate a GearBox", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else if (txtOutputMaterial.Tag == null)
                MessageBox.Show("You have to add a Material to your OutputGear", "Impossible to generate a GearBox", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                tsStatusLabel.Text = "Starting the generation...";

                numTorqueRatio.Value = numOutputTorque.Value / numInputTorque.Value;
                double axesDistance = Convert.ToDouble(numAxesDistance.Value);
                double torqueRatio = Convert.ToDouble(numTorqueRatio.Value);
                gearBox = new GearBox();
                inputGear = new Gear();
                outputGear = new Gear();
                double alpha = Convert.ToDouble(numAlpha.Value)*Math.PI/180;

                /*
                 * Data extraction
                 */

                // Gear Box Creation
                gearBox.Name = txtName.Text;
                gearBox.Alpha = alpha;
                gearBox.GearModule = Convert.ToDouble(numGearModule.Value);
                gearBox.AxesDistance = axesDistance;
                gearBox.TorqueRatio = torqueRatio;
                gearBox.InputTorque = Convert.ToDouble(numInputTorque.Value);
                gearBox.OutputTorque = Convert.ToDouble(numOutputTorque.Value);
                gearBox.InputGear = inputGear;
                gearBox.OutputGear = outputGear;

                // Input Gear Creation
                inputGear.Name = txtName.Text + " Input Gear";
                inputGear.Alpha = alpha;
                inputGear.GearModule = Convert.ToDouble(numGearModule.Value);
                inputGear.WantedRadius = axesDistance * (1 / (1 + torqueRatio));
                inputGear.Torque = Convert.ToDouble(numInputTorque.Value);
                inputGear.Material = (Material)txtInputMaterial.Tag;

                // Output Gear Creation
                outputGear.Name = txtName.Text + " Output Gear";
                outputGear.Alpha = alpha;
                outputGear.GearModule = Convert.ToDouble(numGearModule.Value);
                outputGear.WantedRadius = axesDistance * (1 - 1 / (1 + torqueRatio));
                outputGear.Torque = Convert.ToDouble(numOutputTorque.Value);
                outputGear.Material = (Material)txtOutputMaterial.Tag;

                /*
                 * Data Calculation
                 */

                //Input Gear Calculation
                gearCalculation(inputGear, true);
                updateGearOnCatia(inputGear, true);

                gearCalculation(outputGear, false);
                updateGearOnCatia(outputGear, false);
                showGearboxOnCatia(gearBox);

                updateTreeView(gearBox);
                this.btnValidate.Visible = true;
            }
        }
        private void updateGearOnCatia(Gear g, Boolean isInput)
        {
            if (isInput)
                tsStatusLabel.Text = "Updating the input Gear on Catia...";
            else
                tsStatusLabel.Text = "Updating the output Gear on Catia...";
            INFITF.Application catiaApp = null;
            try
            {
                catiaApp = System.Runtime.InteropServices.Marshal.GetActiveObject("CATIA.Application") as INFITF.Application;
            }
            catch (Exception ex)
            {
                catiaApp = System.Activator.CreateInstance(Type.GetTypeFromProgID("CATIA.Application")) as INFITF.Application;
            }
            finally
            {
                catiaApp.Visible = true;
                if (isInput == true)
                {
                    catiaApp.Documents.Open(path + "\\inputGear.CATPart");
                }
                else
                    catiaApp.Documents.Open(path + "\\outputGear.CATPart");
            }
            MECMOD.PartDocument partDocument = (MECMOD.PartDocument) catiaApp.ActiveDocument;
            ProductStructureTypeLib.Product product = partDocument.Product;

            KnowledgewareTypeLib.Parameter type = (KnowledgewareTypeLib.Parameter) product.Parameters.GetItem("Alpha");
            type.ValuateFromString(g.Alpha + "rad");

            type = (KnowledgewareTypeLib.Parameter)product.Parameters.GetItem("Module");
            type.ValuateFromString(g.GearModule + "mm");

            type = (KnowledgewareTypeLib.Parameter)product.Parameters.GetItem("Z");
            type.ValuateFromString(g.Z.ToString());

            type = (KnowledgewareTypeLib.Parameter)product.Parameters.GetItem("Radius");
            type.ValuateFromString(g.WantedRadius + "mm");

            type = (KnowledgewareTypeLib.Parameter)product.Parameters.GetItem("r_top");
            type.ValuateFromString(g.R_top+ "mm");

            type = (KnowledgewareTypeLib.Parameter)product.Parameters.GetItem("r_base");
            type.ValuateFromString(g.R_pitch + "mm");

            type = (KnowledgewareTypeLib.Parameter)product.Parameters.GetItem("r_foot");
            type.ValuateFromString(g.R_foot + "mm");

            type = (KnowledgewareTypeLib.Parameter)product.Parameters.GetItem("phi");
            type.ValuateFromString(g.Phi + "rad");

            type = (KnowledgewareTypeLib.Parameter)product.Parameters.GetItem("D_Shaft");
            type.ValuateFromString(g.Shaft.Diameter + "mm");

            type = (KnowledgewareTypeLib.Parameter)product.Parameters.GetItem("KeyWidth");
            type.ValuateFromString(g.Shaft.KeyCutWidth + "mm");

            type = (KnowledgewareTypeLib.Parameter)product.Parameters.GetItem("KeyLength");
            type.ValuateFromString(g.Shaft.KeyCutLength + "mm");

            type = (KnowledgewareTypeLib.Parameter)product.Parameters.GetItem("KeyWidth");
            type.ValuateFromString(g.Shaft.KeyCutWidth + "mm");

            type = (KnowledgewareTypeLib.Parameter)product.Parameters.GetItem("C1");
            if (isInput == true)
                type.ValuateFromString(inputC1 + "mm");
            else
                type.ValuateFromString(outputC1 + "mm");

            type = (KnowledgewareTypeLib.Parameter)product.Parameters.GetItem("C2");
            if (isInput == true)
                type.ValuateFromString(inputC2 + "mm");
            else
                type.ValuateFromString(outputC2 + "mm");

            product.Update();
            partDocument.Save();
        }
        private void gearCalculation(Gear gear, bool isInput)
        {
            if (isInput)
                tsStatusLabel.Text = "Calculation of the input Gear...";
            else
                tsStatusLabel.Text = "Calculation of the output Gear...";
            Mathcad.Application mathCadApp=null;
            try
            {
                mathCadApp = System.Runtime.InteropServices.Marshal.GetActiveObject("MathCAD.Application") as Mathcad.Application;
            }
            catch (Exception ex) {
                mathCadApp = System.Activator.CreateInstance(Type.GetTypeFromProgID("MathCAD.Application")) as Mathcad.Application;
            }
            finally
            {
                mathCadApp.Worksheets.Open(path + "/gear calculation.xmcd");
            }
            mathCadApp.Visible = true;

            Mathcad.Worksheet ws= mathCadApp.ActiveWorksheet;
            ws.SetValue("module", gear.GearModule );
            ws.SetValue("RD", gear.WantedRadius );
            ws.SetValue("Alpha", gear.Alpha );

            String value;
            value = ws.GetValue("z_t").asString;
            gear.Z = Convert.ToDouble(value);
            value = ws.GetValue("phi").asString;
            gear.Phi = Convert.ToDouble(value);
            value = ws.GetValue("r_base").asString;
            gear.R_base = Convert.ToDouble(value);
            value = ws.GetValue("r_foot").asString;
            gear.R_foot = Convert.ToDouble(value);
            value = ws.GetValue("r_pitch").asString;
            gear.R_pitch = Convert.ToDouble(value);
            value = ws.GetValue("r_top").asString;
            gear.R_top = Convert.ToDouble(value);

            gear.Shaft = createShaft(gear.Material, gear.Torque, isInput );
        }
        private void GearBoxGenerator_Load(object sender, EventArgs e)
        {
            // Get the Materials
            Database1DataSet1TableAdapters.MaterialsTableAdapter mta = new Database1DataSet1TableAdapters.MaterialsTableAdapter();
            Database1DataSet1.MaterialsDataTable MaterialsFromDB = new Database1DataSet1.MaterialsDataTable();
            mta.Fill(MaterialsFromDB);

            foreach(Database1DataSet1.MaterialsRow row in MaterialsFromDB.Rows){
                Material mat = new Material();
                mat.ID = row.ID;
                mat.Name = row.MaterialName;
                mat.Density = row.Density;
                mat.yieldStress = row.YieldStress;
                mat.youngsModulus = row.YoungsModulus;

                Program.materialList.Add(mat);

            }

            // Get the Shafts
            Database1DataSet1TableAdapters.Shaft1TableAdapter sta = new Database1DataSet1TableAdapters.Shaft1TableAdapter();
            Database1DataSet1.Shaft1DataTable ShaftsFromDB = new Database1DataSet1.Shaft1DataTable();
            sta.Fill(ShaftsFromDB);

            foreach (Database1DataSet1.Shaft1Row row in ShaftsFromDB.Rows) {
                Shaft shaft = new Shaft();
                shaft.ID = row.ID;
                shaft.Name = row.ShaftName;
                shaft.Diameter = row.Diameter;
                shaft.KeyCutHeigth = row.KeyCutHeight;
                shaft.KeyCutLength = row.KeyCutLenght;
                shaft.KeyCutWidth = row.KeyCutWidth;
                shaft.Material = Util.getMaterialById(row.MaterialID);

                Program.shaftList.Add(shaft);
            }

            Database1DataSet1TableAdapters.GearsTableAdapter gta = new Database1DataSet1TableAdapters.GearsTableAdapter();
            Database1DataSet1.GearsDataTable GearsFromDB = new Database1DataSet1.GearsDataTable();
            gta.Fill(GearsFromDB);

            foreach (Database1DataSet1.GearsRow row in GearsFromDB.Rows)
            {
                Gear gear = new Gear();
                gear.ID = row.ID;
                gear.Alpha = row.Alpha;
                gear.GearModule = row.GearModule;
                gear.Torque = row.Torque;
                gear.WantedRadius = row.WantedRadius;
                gear.Name = row.GearName;
                gear.Z = row.Z;
                gear.Phi = row.Phi;
                gear.R_base = row.RBase;
                gear.R_foot = row.RFoot;
                gear.R_pitch = row.RPitch;
                gear.R_top = row.RTop;
                gear.Material = Util.getMaterialById(row.MaterialID);
                gear.Shaft = Util.getShaftById(row.ShaftID);

                Program.gearList.Add(gear);
            }

            Util.refreshGearBoxList();
        }
        private void btnRemove_Click_1(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("You may delete Gear Boxes associated. Do you want to continue ?", "Caution", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning))
            {
                if (listGear.SelectedItems.Count > 0)
                {
                    Gear gearToDelete = new Gear();
                    gearToDelete = (Gear)listGear.SelectedItems[0];

                    Program.gearList.Remove(gearToDelete);
                    Database1DataSet1TableAdapters.GearsTableAdapter gta = new Database1DataSet1TableAdapters.GearsTableAdapter();
                    Database1DataSet1TableAdapters.GearBoxTableAdapter gbta = new Database1DataSet1TableAdapters.GearBoxTableAdapter();

                    try
                    {
                        gbta.DeleteInputGearQuery(gearToDelete.ID);
                        gbta.DeleteOutputGearQuery(gearToDelete.ID);
                        gta.Delete(gearToDelete.ID, gearToDelete.Name, gearToDelete.Alpha, gearToDelete.GearModule, gearToDelete.Material.ID, gearToDelete.Torque, gearToDelete.Shaft.ID, gearToDelete.WantedRadius, gearToDelete.Z, gearToDelete.Phi, gearToDelete.R_base, gearToDelete.R_foot, gearToDelete.R_pitch, gearToDelete.R_top);
                        Util.refreshGearBoxList();
                    }
                    catch (Exception ex) { }

                }
                refreshList();
                propertyGridGear.SelectedObject = null;
                btnRemove.Enabled = false;
                btnEdit.Enabled = false;
                btnSelect.Enabled = false;
            }
        }