起因:在对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;
}
}
}