跳到主要内容

使用SOLIDWORKS API添加移动-复制体特征并使用共面约束

添加了约束的移动-复制体属性管理器页面{ width=150 }

这是一个使用SOLIDWORKS API的C# VSTA宏示例,它可以找到所选体的最大平面面,并在零件中插入移动-复制体特征,并使用SOLIDWORKS API的IFeatureManager::InsertMoveCopyBody2方法添加共面约束。

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Linq;
using System.Runtime.InteropServices;

namespace MoveBodyMate
{
public partial class SolidWorksMacro
{
public void Main()
{
try
{
var model = swApp.IActiveDoc2;
var body = model.ISelectionManager.GetSelectedObject6(1, -1) as IBody2;

if (body == null)
{
throw new NullReferenceException("选择要移动的体");
}

var plane = FindFrontPlane(model);
var face = FindLargestPlanarFace(body);

AddCoincidentMate(model, body, plane as IEntity, face as IEntity);
}
catch(Exception ex)
{
swApp.SendMsgToUser2(ex.Message, (int)swMessageBoxIcon_e.swMbStop, (int)swMessageBoxBtn_e.swMbOk);
}
}

private static void AddCoincidentMate(ModelDoc2 model, IBody2 body, IEntity mateEnt, IEntity mateEntOnBody)
{
var selData = model.ISelectionManager.CreateSelectData();
selData.Mark = 1;

if (!body.Select2(false, selData))
{
throw new InvalidOperationException("选择体失败");
}

var moveCopyBodyFeat = model.FeatureManager.InsertMoveCopyBody2(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, false, 1);

var featData = moveCopyBodyFeat.GetDefinition() as IMoveCopyBodyFeatureData;

if (featData.AccessSelections(model, null))
{
int err;

var mate = featData.AddMate(new DispatchWrapper[]
{
new DispatchWrapper(mateEnt),
new DispatchWrapper(mateEntOnBody)
}, (int)swMateType_e.swMateCOINCIDENT, (int)swMateAlign_e.swMateAlignCLOSEST, 0, 0, out err);

if (mate == null)
{
throw new NullReferenceException(string.Format("添加约束失败: {0}", (swAddMateError_e)err));
}

if (!moveCopyBodyFeat.ModifyDefinition(featData, model, null))
{
throw new InvalidOperationException("应用特征更改失败");
}
}
else
{
throw new InvalidOperationException("无法访问定义");
}
}

private IFeature FindFrontPlane(IModelDoc2 model)
{
var feat = model.IFirstFeature();

while (feat != null && feat.GetTypeName2() != "RefPlane")
{
feat = feat.IGetNextFeature();
}

if (feat == null)
{
throw new NullReferenceException("找不到前平面");
}

return feat;
}

private IFace2 FindLargestPlanarFace(IBody2 body)
{
var faces = body.GetFaces() as object[];

if (faces == null)
{
throw new NullReferenceException("体中不包含面");
}

var face = faces.Cast<IFace2>()
.Where(f => f.IGetSurface().IsPlane())
.OrderBy(f => f.GetArea()).LastOrDefault();

if (face == null)
{
throw new NullReferenceException("该体中没有平面面");
}

return face;
}

public SldWorks swApp;
}
}