这个是一个VBA样例,运行之前需要打开至少包含2个零件的装配体文件,两个零件最好包含圆孔特征。选择好两个零件的圆孔或者圆边缘。然后运行这个VBA脚本就可以添加相关约束了。
这种代码一般不会直接就拿来用,一般用在自动化装配的情况。参考用。
Public Sub InsertConstraint()
' Set a reference to the assembly component definintion.
Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
' Set a reference to the select set.
Dim oSelectSet As SelectSet
Set oSelectSet = ThisApplication.ActiveDocument.SelectSet
' Validate the correct data is in the select set.
If oSelectSet.Count 2 Then
MsgBox "You must select the two circular edges for the insert."
Exit Sub
End If
If Not TypeOf oSelectSet.Item(1) Is Edge Or Not TypeOf oSelectSet.Item(2) Is Edge Then
MsgBox "You must select the two circular edges for the insert."
Exit Sub
End If
' Get the two edges from the select set.
Dim oEdge1 As Edge
Dim oEdge2 As Edge
Set oEdge1 = oSelectSet.Item(1)
Set oEdge2 = oSelectSet.Item(2)
' Create the insert constraint between the parts.
Dim oInsert As InsertConstraint
Set oInsert = oAsmCompDef.Constraints.AddInsertConstraint(oEdge1, oEdge2, True, 0)
End Sub