工作需求須用到 MathCad 本來想用 C# 來處理,無奈太多數學 function 不知道如何轉,如 cspline,interp,genfit..想把這些轉成C#,饒了我吧.

轉個彎,C#好像能控制MathCad.那就開始囉.

拜一下孤狗,資料真的是很少.幾乎所有的資料都在 PlanePTC Community 這網站.

 Mathcad.Application mc = new Mathcad.Application();
            Mathcad.Worksheets wk;
            Mathcad.Worksheet ws = new Mathcad.Worksheet();

            mc.Visible = true;

            wk = mc.Worksheets;
            ws = wk.Open(@"C:\Users\Documents\MatrixP.xmcd");
            try
            {
                //Set Value
                ws.SetValue("ina", 1000);
                //ws.SetValue("inb", 500);
              
                ws.SetValue("ff",@"C:\Users\Documents\55.txt");
                ws.Recalculate();
                var svar = (Mathcad.IStringValue)(ws.GetValue("ff"));

                //Read value
                 //var b = (Mathcad.INumericValue)(ws.GetValue("ina"));

                //  double intpp =((Mathcad.INumericValue)(ws.GetValue("PP"))).Real;

                //Read Matrix
                Mathcad.IMatrixValue Matrix = (Mathcad.IMatrixValue)(ws.GetValue("U") as Mathcad.MatrixValue);
                int col = Matrix.Cols;
                int row = Matrix.Rows;
                int idx, idy;
                double[,] matrix = new double[row, col];
                
                Mathcad.INumericValue element;
                for (idx = 0; idx < row; idx++)
                {
                    for (idy = 0; idy < col; idy++)
                    {
                        element = (Mathcad.INumericValue)Matrix.GetElement(idx, idy);
                        matrix[idx, idy] = element.Real;
                    }
                }
                //MessageBox.Show(((INumericValue)b.GetElement(0,0)).ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                mc.Visible = false;
               
                ws.Close(Mathcad.MCSaveOption.mcSaveChanges);
                mc.CloseAll(Mathcad.MCSaveOption.mcDiscardChanges);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wk);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(mc);
            }

 

MathCad Input File setting as:

File.jpg  

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 linear 的頭像
    linear

    李泥兒

    linear 發表在 痞客邦 留言(0) 人氣()