无论是装配或零件都有MassProperties属性,其中包含了重量。焊接是基于装配多出来的部分。在用户界面可以看到这个信息。
API里是如何做呢?其实焊接也是一个组件(Occurrence)。能访问到其MassProperties。请看如下代码:
Sub getWeldMass()
Dim oAssDoc As AssemblyDocument
Set oAssDoc = ThisApplication.ActiveDocument
Dim cd As ComponentDefinition
Set cd = oAssDoc.ComponentDefinition
' 通常第一个Occurrence是焊接
Dim oO As ComponentOccurrence
Set oO = cd.Occurrences(1)
If TypeOf oO.Definition Is WeldsComponentDefinition Then
'总重,API里是千克
Dim oTotalWeldsMass As Double
oTotalWeldsMass = oO.MassProperties.Mass
'转换为文档所用的单位制数据
Dim oMassValueOfDocUnit As String
oMassValueOfDocUnit = oAssDoc.UnitsOfMeasure.GetStringFromValue(oO.MassProperties.Mass, oAssDoc.UnitsOfMeasure.MassUnits)
MsgBox ("重量是: " & oMassValueOfDocUnit)
Else
MsgBox ("有错误!")
End If
End Sub