问题描述:在SolidWorks中进行二次开发时,在程序中如何实现获得质量特性里的信息,如体积、表面积、重心等。
问题解决:
程序如下(语言类型是VB)
Option Explicit
Public Enum swMassPropertyMoment_e
swMassPropertyMomentAboutCenterOfMass = 0
swMassPropertyMomentAboutCoordSys = 1
End Enum
Public Enum swMassPropertiesStatus_e
swMassPropertiesStatus_OK = 0
swMassPropertiesStatus_UnknownError = 1
swMassPropertiesStatus_NoBody = 2
End Enum
Public Enum swBodyType_e
swSolidBody = 0
swSheetBody = 1
swWireBody = 2
swMinimumBody = 3
swGeneralBody = 4
swEmptyBody = 5
End Enum
Public Enum swUserPreferenceDoubleValue_e
swMaterialPropertyDensity = 7
End Enum
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDocExt As SldWorks.ModelDocExtension
Dim swMass As SldWorks.MassProperty
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim vBodyArr As Variant
Dim vCoM As Variant
Dim vMoI As Variant
Dim vPrinAoIx As Variant
Dim vPrinAoIy As Variant
Dim vPrinAoIz As Variant
Dim vPrinMoI As Variant
Dim nDensity As Double
Dim bRet As Boolean
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDocExt = swModel.Extension
Set swMass = swDocExt.CreateMassProperty
Set swSelMgr = swModel.SelectionManager
Set swComp = swSelMgr.GetSelectedObjectsComponent2(1)
vBodyArr = swComp.GetBodies2(swSolidBody): Debug.Assert Not IsEmpty(vBodyArr)
bRet = swMass.AddBodies((vBodyArr)): Debug.Assert bRet
vCoM = swMass.CenterOfMass
vMoI = swMass.GetMomentOfInertia(swMassPropertyMomentAboutCenterOfMass)
vPrinAoIx = swMass.PrincipleAxesOfInertia(0)
vPrinAoIy = swMass.PrincipleAxesOfInertia(1)
vPrinAoIz = swMass.PrincipleAxesOfInertia(2)
vPrinMoI = swMass.PrincipleMomentsOfInertia
Debug.Print "File = " & swModel.GetPathName
Debug.Print " Comp = " & swComp.Name2
Debug.Print " Config = " & swComp.ReferencedConfiguration
Debug.Print " Density = " & swMass.Density & " kg/m^3"
Debug.Print ""
Debug.Print " CenterOfMass = (" & vCoM(0) * 1000# & ", " & vCoM(1) * 1000# & ", " & vCoM(2) * 1000# & ") mm"
Debug.Print " Volume = " & swMass.Volume * 1000000000# & " mm^3"
Debug.Print " Area = " & swMass.SurfaceArea * 1000000# & " mm^2"
Debug.Print " Mass = " & swMass.Mass & " kg"
Debug.Print ""
Debug.Print " Ix = (" & vPrinAoIx(0) & ", " & vPrinAoIx(1) & ", " & vPrinAoIx(2) & ")"
Debug.Print " Iy = (" & vPrinAoIy(0) & ", " & vPrinAoIy(1) & ", " & vPrinAoIy(2) & ")"
Debug.Print " Iz = (" & vPrinAoIz(0) & ", " & vPrinAoIz(1) & ", " & vPrinAoIz(2) & ")"
Debug.Print ""
Debug.Print " Px = " & vPrinMoI(0) & " kg*m^2"
Debug.Print " Py = " & vPrinMoI(1) & " kg*m^2"
Debug.Print " Pz = " & vPrinMoI(2) & " kg*m^2"
Debug.Print ""
Debug.Print " Lxx = " & vMoI(0) & " kg*m^2"
Debug.Print " Lxy = " & vMoI(1) & " kg*m^2"
Debug.Print " Lxz = " & vMoI(2) & " kg*m^2"
Debug.Print " Lyx = " & vMoI(3) & " kg*m^2"
Debug.Print " Lyy = " & vMoI(4) & " kg*m^2"
Debug.Print " Lyz = " & vMoI(5) & " kg*m^2"
Debug.Print " Lzx = " & vMoI(6) & " kg*m^2"
Debug.Print " Lzy = " & vMoI(7) & " kg*m^2"
Debug.Print " Lzz = " & vMoI(8) & " kg*m^2"
End Sub