起因:在對SolidWorks進行二次開發的過程中,SolidWorks API幫助必不可少,然而其中多數實例是用VB6.0和C++實現的,本文提供一個利用C#進行SolidWorks二次開發的小實例,希望能提供一個敲開這扇門的窗口。
環境:Windows XP + Visual Studio 2005 C# + SolidWorks 2008
目的:用C#打開SolidWorks的.SLDPRT文件。
步驟:
1、安裝SolidWorks2008 API包;
2、添加兩個COM引用:SldWorks 2008 Type Library和SolidWorks 2008 Constant type library;
3、程序代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SldWorks; //COM Reference: SldWorks 2008 Type Library.
using SwConst; //COM Reference: SolidWorks 2008 Constant type library.
namespace swTest
{
public partial class frmTest : Form
{
public frmTest()
{
InitializeComponent();
}
private void btnTest_Click_1(object sender, EventArgs e)
{
int IErrors = 0;
int IWarnings = 0;
SldWorks.SldWorks swApp = new SldWorks.SldWorks();
swApp.OpenDoc6(@"E:\a.SLDPRT",(int)SwConst.swDocumentTypes_e.swDocPART,(int)SwConst.swOpenDocOptions_e.swOpenDocOptions_Silent,null, ref IErrors, ref IWarnings);
swApp.Visible = true;
}
}
}