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

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

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用 SolidWorks API 的二次开发获取模型树的最顶层节点的 Component2。首先,你需要遍历整个模型树,找到最顶层的组件节点。以下是一个示例代码: ```C# using SolidWorks.Interop.sldworks; using SolidWorks.Interop.swconst; using System; namespace SolidWorksAPI { class Program { static void Main(string[] args) { // 创建 SolidWorks 应用程序对象 SldWorks swApp = new SldWorks(); // 打开 SolidWorks 模型 ModelDoc2 swModel = swApp.OpenDoc("C:\\Path\\To\\Your\\Model.sldprt", (int)swDocumentTypes_e.swDocPART); // 获取模型的特征管理器 FeatureManager swFeatMgr = swModel.FeatureManager; // 获取模型树的顶层特征 Feature swTopFeat = swFeatMgr.GetFirstFeature(); // 遍历模型树,找到最顶层的组件节点 while (swTopFeat != null) { if (swTopFeat.GetTypeName2() == "Reference") { // 获取组件的 Component2 接口 Component2 swComponent = (Component2)swTopFeat.GetSpecificFeature2(); Console.WriteLine("顶层组件名称: " + swComponent.Name); break; } swTopFeat = swTopFeat.GetNextFeature(); } // 关闭 SolidWorks 模型 swApp.CloseDoc("Model.sldprt"); // 退出 SolidWorks 应用程序 swApp.ExitApp(); } } } ``` 请注意,你需要确保已经正确安装了 SolidWorks 开发环境并添加了对应的 COM 引用。此外,你需要将代码中的文件路径替换为你的实际文件路径。这段代码将打开一个 SolidWorks 零件文件,遍历模型树并输出顶层组件的名称。 希望对你有帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值