SolidWorks二次开发:在程序中获取质量特征里的信息

本文提供了一个使用VB在SolidWorks中获取几何体的质量特性信息(如体积、表面积、重心)的示例代码,包括体积、表面积、质量、密度、中心质量、主轴和惯量矩。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 问题描述:在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

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值