跳到主要内容

使用SOLIDWORKS API在坐标系之间添加配合关系

使用SOLIDWORKS API在两个选定组件的两个坐标系之间添加重合配合关系。这两个组件必须包含名为Coordinate System1的坐标系特征。

重合配合关系属性管理器页面{ width=640 }

使用IAssemblyDoc::AddMate3 SOLIDWORKS API插入配合关系特征。

Dim swApp As SldWorks.SldWorks
Dim swAssy As SldWorks.AssemblyDoc
Dim swSelMgr As SldWorks.SelectionMgr

Sub main()

Set swApp = Application.SldWorks

Set swAssy = swApp.ActiveDoc

If Not swAssy Is Nothing Then

Set swSelMgr = swAssy.SelectionManager

Dim swCs1 As SldWorks.Feature
Dim swCs2 As SldWorks.Feature

Set swCs1 = GetCoordinateSystemFromSelection(1, "Coordinate System1")
Set swCs2 = GetCoordinateSystemFromSelection(2, "Coordinate System1")

swCs1.Select2 False, 1
swCs2.Select2 True, 1

swAssy.AddMate3 swMateType_e.swMateCOINCIDENT, swMateAlign_e.swMateAlignCLOSEST, False, 0, 0, 0, 0, 0, 0, 0, 0, False, 0

swAssy.EditRebuild

Else

MsgBox "请打开装配体"

End If

End Sub

Function GetCoordinateSystemFromSelection(index As Integer, name As String) As SldWorks.Feature

Dim swComp As SldWorks.Component2
Dim swCoordSys As SldWorks.Feature

Set swComp = swSelMgr.GetSelectedObjectsComponent2(index)

If Not swComp Is Nothing Then

Set swCoordSys = swComp.FeatureByName(name)

If swCoordSys Is Nothing Then
MsgBox "组件 " & swComp.Name2 & " 不包含特征 " & name
End
End If

Else

MsgBox "请选择2个组件"
End

End If

Set GetCoordinateSystemFromSelection = swCoordSys

End Function