AnsweredAssumed Answered

Get Assembly Mass

Question asked by Luke Frost on Aug 12, 2019
Latest reply on Aug 12, 2019 by Ray McHugh

Good Morning,


I have got the following macro which i have put together based on research from the forums to get the mass of a part and assembly and to write it to a custom property once a simple calculation has been done on the mass.

 

Option Explicit
Dim cpm As CustomPropertyManager
Public Enum swMassPropertiesStatus_e
swMassPropertiesStatus_OK = 0
swMassPropertiesStatus_UnknownError = 1
swMassPropertiesStatus_NoBody = 2
End Enum
Public Enum swUserPreferenceToggle_e
swUpdateMassPropsDuringSave = 30
End Enum
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelExt As SldWorks.ModelDocExtension
Dim swAssy As SldWorks.AssemblyDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim nStatus As Long
Dim vMassProp As Variant

Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swModelExt = swModel.Extension
vMassProp = swModelExt.GetMassProperties(1, nStatus)

If swModel.GetType = swDocASSEMBLY Or swModel.GetType = swDocPART Then
If Not IsEmpty(vMassProp) Then
Dim strMass As String
If vMassProp(5) < 1000 Then
strMass = Round(vMassProp(5), 0) & "g"

Set cpm = swModel.Extension.CustomPropertyManager("")
cpm.Delete "Weight"
cpm.Add2 "Weight", swCustomInfoText, strMass

MsgBox ("Weight Added")
Else
strMass = Round((vMassProp(5) / 1000), 1) & "kg"

Set cpm = swModel.Extension.CustomPropertyManager("")
cpm.Delete "Weight"
cpm.Add2 "Weight", swCustomInfoText, strMass

MsgBox ("Weight Added")
End If
End If
End If
End Sub

 

This is working perfectly for a part and the code does run in an assembly however if i have an assembly who's mass is 62364.07grams the macro is writing 62g to the properties.  When i look at the document properties of the assembly the Mass Unit is set to grams and not kgs. Can someone please help advise where things may be going wrong.

 

Thanks in advanced

 

Luke

Outcomes