经典图书
2.在SolidWorks里画一个圆柱,画完后点停止,并将录制结果保存为.csproj
3.点击编辑,打开刚才保存的结果(默认是用vs2005打开的)
4.在项目里添加窗体输入框等
5.修改、添加窗体代码(加粗的部分是必须的)
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSolidWorks.Interop.sldworks;//需要添加的引用
usingSolidWorks.Interop.swconst;//需要添加的引用
namespaceMacro3.csproj
{
public partialclass Form1 : Form
{
SldWorksswApp = null;
ModelDoc2swModel = null;
public Form1(SldWorks iswApp)//把SolidWorksMacro.cs下的swApp传递过来
{
InitializeComponent();
swApp = iswApp;
swModel = (ModelDoc2)swApp.ActiveDoc;
}
private voidForm1_Load(object sender, EventArgs e)
{
}
private voidbutton1_Click(object sender, EventArgs e)
{
longr=0,h=0;
if(swModel == null) swApp.SendMsgToUser("请新建一个零件!");
else
{
if(long.TryParse(textBox1.Text,out r) && long.TryParse(textBox2.Text,out h))
CreatCylinder(r, h);
elseMessageBox.Show("请输入有效的直径和高度!");
}
}
private voidCreatCylinder(double r, double h) //从main()函数下粘贴过来的代码,这里改写成一个方法
{
boolboolstatus = false;
boolstatus =swModel.Extension.SelectByID2("前视基准面","PLANE", 0, 0, 0, false, 0, null, 0);
swModel.SketchManager.InsertSketch(true);
SketchSegmentskSegment = null;
skSegment = ((SketchSegment)(swModel.SketchManager.CreateCircle(0,0, 0, r, 0, 0)));
boolstatus =swModel.Extension.SelectByID2("Arc1","SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
FeaturemyFeature = null;
myFeature = ((Feature)(swModel.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, h, h, false,false, false, false, 0,0, false, false, false, false, true, true, true, 0, 0, false)));
}
}
}
6.修改、添加main()函数代码
usingSolidWorks.Interop.sldworks;
usingSolidWorks.Interop.swconst;
usingSystem.Runtime.InteropServices;
usingSystem;
namespaceMacro3.csproj
{
public partialclass SolidWorksMacro
{
public voidMain()
{
Form1myform = new Form1(swApp);//初始化窗体
myform.Show();
}
///
/// The SldWorks swApp variable is pre-assignedfor you.
///
public SldWorksswApp;
}
}
7. 编译后在SolidWorks 里点击播放宏,选择debug 目录下的dll 文件
最终效果:
2013-9-7 23:44:35 上传
下载次数: 6
2013-9-7 23:44:25 上传
下载次数: 5
2013-9-7 23:44:25 上传
下载次数: 5
2013-9-7 23:44:24 上传
下载次数: 5
2013-9-7 23:44:23 上传
下载次数: 4
2013-9-7 23:44:23 上传
下载次数: 4 |