AnsweredAssumed Answered

API MassProperty object does not use assigned coordinate system

Question asked by Andy Abate on Jan 4, 2018
Latest reply on Jan 4, 2018 by Andy Abate

Hi there,


I am writing a macro for SW2017, and I have two problems:

1) For some reason, API MassProperty objects do not use the assigned coordinate system after SetCoordinateSystem() returns True.

2) Mass properties do not update until I open the "Mass Properties" GUI.


With an open part file, I use ModelDocExtension::CreateMassProperty() to create a new MassProperty object. I use ModelDocExtension::GetCoordiniateSystemTransformByName(), the coordinate system is found, and I checked the ArrayData to ensure it is the correct transform. MassProperty::SetCoordinateSystem() returns True, that it set the coordinate system for the MassProperty object.

BUT, when I call CenterOfMass and GetMomentOfInertia(), they are still relative to the default coordinate system! I have checked everything I can think of, and it seems like SetCoordinateSystem is failing somehow.


The documentation says CreateMassProperty() always uses up-to-date mass information, but that does not seem to be the case. Saving and rebuilding have no effect on new MassProperty objects; only opening the "Mass Properties" GUI updates new MassProperty objects.


What am I missing, and how can I get the expected behavior from SW?






     Well, I found the issue: Overriding mass properties in the SW GUI messes everything up. I unchecked that and everything works the way it should.

     So, now I guess this is a bug report.

     If anyone knows a good way to override mass properties in a way that does not break everything, that would be nice!


Full Code:

Option Explicit

Sub main()

    Dim swApp                   As SldWorks.SldWorks

    Dim swModel                 As SldWorks.ModelDoc2

    Dim swModelExt              As SldWorks.ModelDocExtension

    Dim swMass                  As MassProperty

    Dim Ic                      As Variant

    Dim c                       As Variant


    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    If swModel Is Nothing Then

        MsgBox "No active doc."

        Exit Sub

    End If

    Set swModelExt = swModel.Extension


    Dim fso As Object

    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim ofile As Object

    Set ofile = fso.CreateTextFile(swModel.GetPathName() + "-inertia.txt")

    If ofile Is Nothing Then

        MsgBox "Cannot create output file."

        Exit Sub

    End If


    Set swMass = swModelExt.CreateMassProperty()


    ' Set coordinate system

    If swMass.SetCoordinateSystem(swModelExt.GetCoordinateSystemTransformByName("Body-Frame")) = False Then

        MsgBox "Must have a 'Body-Frame' coordinate system."

        Exit Sub

    End If


    ' Get mass properties

    Ic = swMass.GetMomentOfInertia(swMassPropertyMoment_e.swMassPropertyMomentAboutCenterOfMass)

    c = swMass.CenterOfMass


    ' Write to file

    Dim fmt As String: fmt = "0.0000e+00"

    ofile.WriteLine ""

    ofile.WriteLine "m  =  " & Format(swMass.Mass, fmt) & "; % kg"

    ofile.WriteLine "c  = [" & Format(c(0), fmt) _

                      & " " & Format(c(1), fmt) _

                      & " " & Format(c(2), fmt) & "]; % (x,y,z) mm"

    ofile.WriteLine "Ic = [" & Format(Ic(0), fmt) _

                       & " " & Format(Ic(4), fmt) _

                       & " " & Format(Ic(8), fmt) & "   % (Ixx,Iyy,Izz) kg.m^2"

    ofile.WriteLine "      " & Format(-Ic(1), fmt) _

                       & " " & Format(-Ic(2), fmt) _

                       & " " & Format(-Ic(5), fmt) & "]; % (Ixy,Ixz,Iyz) kg.m^2"



End Sub