Solidworks二次开发—06—在装配体中添加配合

Solidworks二次开发—06—在装配体中添加配合

**

**

折腾了三天终于完成了计划中的功能模块。在一个装配体中自动判断插入合适的零件,并添加配合。

在前面几篇文章中我已经基本上说明了如何得到零部件的数据信息、如何插入零部件、如何得到已经选择的特征等。

下面只介绍怎样进行配合

在做配合时,需要经常选择到零件的面、线等,这是一个问题,还有就是介绍一下 addmate2 函数的使用:

一般进行配合我们按照下面的次序来进行:

1-ModelDoc.ClearSelection2 ‘ 取消所有选择

2- 选择需要配合的实体( entity )

3- 使用 AddMate2 函数进行配合

4- 再次使用 ModelDoc.ClearSelection2 ‘ 取消所有选择

主要的问题在于如何选择合适的面:

由于面的命名没有什么规律,很多时候是程序自动来命名的,这样,不方便使用 selectbyID 来选择,我也不想使用坐标值来选择一个面,那样做更加糟糕。

在得到一个组件( component )或者一个特征( feature )时,我们有 getfaces 、 getfirstface 、 getnextface 等方法,我们可以使用这些方法遍历一个组件或特征等的各个面,来达到选择面的目的,看下面程序:

Private Function selectface(dcom As SldWorks.Component2, tp As Integer) As Boolean

Set swdowelbody = dcom.GetBody()

If swdowelbody Is Nothing Then ' 错误处理

MsgBox " 选择零件失败 "

selectface = False

Exit Function

End If

Set swDCface = swdowelbody.GetFirstFace ‘ 得到第一个面

Do While Not swDCface Is Nothing ‘ 遍历各个面

Set swDsurface = swDCface.GetSurface ‘ 得到表面对象

If swDsurface.IsCylinder Then ‘ 如果是圆柱面

If tp = 0 Then 'means cylinder

Set swDEnt = swDCface

swDEnt.Select4 True, selDdata

selectface = True

Exit Function

End If

Else ‘ 如果是其它,当然实际中我们可能需要使用 select 来定义好多分支

If tp = 1 Then 'means plane

Set swDEnt = swDCface

swDEnt.Select4 True, selDdata

selectface = True

Exit Function

End If

End If

Set swDCface = swDCface.GetNextFace

Loop

End Function

此函数接受两个参数,第一个是一个 component 对象,第二个用来标识选择类型: 0 表示圆柱面, 1 表示平面。此函数运行完成后将 选择 指定组件的指定类型的一个面。需要注意的是我们需要在判断面类型时候需要转换到 surface 对象。而且选择需要定义一个 entity 对象,用来 select4 ,达到选择的目的。可能这个过程有些复杂,大家按照这个顺序多测试几次,就明白了它的工作原理。

上面的函数写的并不好,是我从我的工程中截取的一段。

下面介绍一下 addmate2 函数:

** Syntax ** (OLE Automation) OLE 语法:

** pMateObjOut = AssemblyDoc.AddMate2 ( mateTypeFromEnum, alignFromEnum, flip, distance, distAbsUpperLimit, distAbsLowerLimit, gearRatioNumerator, gearRatioDenominator, angle, angleAbsUpperLimit, angleAbsLowerLimit, errorStatus )

**

参数:

Input:

|

(long) mateTypeFromEnum

|

Type of mate as defined in swMateType_e

|

配合类型

---|---|---|---

Input:

|

(long) alignFromEnum

|

Type of alignment as defined in swMateAlign_e

|

对齐选项

Input:

|

(VARIANT_BOOL) flip

|

TRUE to flip the component, FALSE otherwise

|

是否翻转

Input:

|

(double) distance

|

Distance value to use with distance or limit mates

|

距离

Input:

|

(double) distAbsUpperLimit

|

Absolute maximum distance value (see Remarks )

|

距离限制 max

Input:

|

(double) distAbsLowerLimit

|

Absolute minimum distance value (see Remarks )

|

距离限制 min

Input:

|

(double) gearRatioNumerator

|

Gear ratio numerator value for gear mates

|

齿轮配合分子值

Input:

|

(double) gearRatioDenominator

|

Gear ratio denominator value for gear mates

|

齿轮配合分母值

Input:

|

(double) angle

|

Angle value to use with angle mates

|

角度

Input:

|

(double) angleAbsUpperLimit

|

Absolute maximum angle value

|

角度限制 max

Input:

|

(double) angleAbsLowerLimit

|

Absolute minimum angle value

|

角度限制 min

Output:

|

(long) errorStatus

|

Success or error as defined by swAddMateError_e

|

错误报告

Return:

|

(LPMATE2) pMateObjOut

|

Pointer to the Mate2 object

|

返回指向配合的指针

** Remarks

**

To specify a distance mate without limits, set the distAbsUpperLimit and distAbsLowerLimit arguments equal to the distance argument's value.

指定一个没有限制的距离,设定距离限制的最大、最小值和距离值相等

If mateTypeFromEnum is swMateDISTANCE or swMateANGLE when the mate is applied to the closest position that meets the mate condition specified by distance or angle, then setting flip to TRUE moves the assembly to the other possible mate position.

如果是距离或角度配合,配合将从符合条件的最近端进行配合,我们可以设定 flip 为 true ,改变配合至另一个合适的位置

Use: 使用配合的步骤

ModelDoc2::ClearSelection2 (VARIANT_TRUE) before selecting entities to mate.

ModelDocExtension::SelectByID2 with Mark = 1 to select entities to mate.

ModelDoc2::ClearSelection2 (VARIANT_TRUE) after the mate is created.

If mateTypeFromEnum is swMateCAMFOLLOWER, then use a selection mark of 8 for the cam-follower face.

如果配合类型为 凸轮,在表面标示 8. 注:这个我也不太明白哈哈

If nothing is preselected, then errorStatus is swAddMateError_IncorrectSeletions and pMateObjOut is NULL/Nothing.

如果实现没有限定实体来配合,将会抱错 swAddMateError_IncorrectSeletions ,函数返回 NULL 或者 Nothing

上面就是 API 帮助所说的话,下面给出一段示例程序,假设之前我们已经选择了两个半径一样的圆柱面,那么我们来定义一个同心配合:

** Set swmatefeat = swassy.AddMate2(1, 0, False, 0, 0, 0, 0, 0, 0, 0, 0, nErrors)

**

其中的 Dim swassy As SldWorks.AssemblyDoc

Dim swmatefeat As Object

注:在编程中有时候不能实现确定一个对象的类型,我们可以声明一个 Object 对象,让 VB 自己去匹配。但这样做是影响了效率。

要完成一个距离或者角度要麻烦一些,就像上面的 remark 中说明的:

** <sp **

Published At
Categories with Web编程
Tagged with
comments powered by Disqus