Sunday, November 9, 2008

solidwork 二次開發 C# (1)

最近在研究solidwork如何用C#把常用的零件自動產生出來,

今天總於搞出一個最簡單的如何改變尺寸。

檔案000.SLDPRT內含一個圓球,我要改變它的直徑。

image

private void btn_change_Click(object sender, EventArgs e)
        {
            double x,

//轉換文字為數字

            try
            {

                x = Convert.ToDouble(tb_xval.Text);

            }

            catch
            {

                swApp.SendMsgToUser2("You must input valid number", (int)SwConst.swMessageBoxIcon_e.swMbStop, (int)SwConst.swMessageBoxBtn_e.swMbOk);

                tb_xval.Text = "1";

                return;

            }

            swApp = new SldWorks.SldWorksClass();

            int er = 0;

            int wa = 0;

            SldWorks.ModelDoc2 swModel;

//打開零件檔

            swModel = swApp.OpenDoc6(tb_docPath.Text, (int)SwConst.swDocumentTypes_e.swDocPART, (int)SwConst.swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref er, ref wa);

//開啟檔案是否有誤         

  if (er != 0 | wa != 0 | swModel == null)
            {

                swApp.SendMsgToUser2("Error opening file", (int)SwConst.swMessageBoxIcon_e.swMbStop, (int)SwConst.swMessageBoxBtn_e.swMbOk);

                swApp.ExitApp();

                return;

            }

//是否是指定的檔案。

            if (swModel.GetTitle() != "000.sldprt")
            {

                swApp.SendMsgToUser2("This is wrong file", (int)SwConst.swMessageBoxIcon_e.swMbStop, (int)SwConst.swMessageBoxBtn_e.swMbOk);

                swApp.QuitDoc(swModel.GetTitle());

                swApp.ExitApp();

                return;

            }

//宣告尺寸型別

            SldWorks.Dimension reval;

//取得D1@草圖 Object

            reval=(SldWorks.Dimension) swModel.Parameter("D1@草圖1");

//設定D1@草圖 的屬性
            reval.SetSystemValue3(x/1000,(int) SwConst.swSetValueInConfiguration_e.swSetValue_InThisConfiguration,"");

//重新計算
            swModel.EditRebuild3();

//儲存
            swModel.Save3((int)SwConst.swSaveAsOptions_e.swSaveAsOptions_Silent, ref er, ref wa);

            if (er != 0 | wa != 0 )
            {

                swApp.SendMsgToUser2("Errors saving part file.", (int)SwConst.swMessageBoxIcon_e.swMbStop, (int)SwConst.swMessageBoxBtn_e.swMbOk);
                swApp.QuitDoc("000.sldprt");
                swApp.ExitApp();
                return;

            }

//離開文件
            swApp.QuitDoc("000.sldprt");
            swApp.SendMsgToUser2("Finished", (int)SwConst.swMessageBoxIcon_e.swMbInformation,(int) SwConst.swMessageBoxBtn_e.swMbOk);
            swApp.ExitApp();

        }

image

image

No comments:

Post a Comment