NX Journal Question - Update Weight Data Now
NX Journal Question - Update Weight Data Now
(OP)
I have kludged together a Journal from various sources to assign a mass to a body by calculating the density from the volume. It is pretty clunky, but does what I need. The only hitch is I can't figure out a simple way to execute the "Update Weight Data Now" command. When I record it I get 139 lines of code for that single command. Does anyone know a simplier way to execute that command in a journal? Below is my code (apologies to those who actually know what their doing with VB):
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpenUI
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpen.Annotations
Module AssignMassToBody
Dim theSession As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
Dim dblMass As Double = 10.0
Dim dblVolume as Double = 1
Dim dblDensity_kg_mm3 as Double = 0
Dim dblDensity_g_cm3 as Double = 0
Dim iBodies(0) As IBody
Sub Main()
' Select the Part
Dim theBody As Body = select_a_body("Body:")
If theBody IsNot Nothing Then
Dim mm As MeasureManager = workPart.MeasureManager()
iBodies(0) = theBody
Dim massUnits(4) As Unit
massUnits(0) = workPart.UnitCollection.GetBase("Area")
massUnits(1) = workPart.UnitCollection.GetBase("Volume")
massUnits(2) = workPart.UnitCollection.GetBase("Mass")
massUnits(3) = workPart.UnitCollection.GetBase("Length")
Dim mb As MeasureBodies = mm.NewMassProperties(massUnits, 0.99, iBodies)
Echo("Volume = " & mb.Volume.ToString() & " " & massUnits(1).Abbreviation())
Echo("Current Mass = " & mb.Mass.ToString() & " " & massUnits(2).Abbreviation())
dblVolume = mb.Volume
End If
' Input the Mass
Try
dblMass = NXInputBox.GetInputNumber("Mass (kg):", "Input Mass (kg)", dblMass)
Catch ex as Exception
MsgBox("Something Went Horribly Wrong", MsgBoxStyle.Information)
Return
End Try
' Calculate the New Density
dblDensity_kg_mm3 = dblMass/dblVolume
dblDensity_g_cm3 = dblDensity_kg_mm3 * 1000000
Echo("New Density = " & dblDensity_kg_mm3.ToString() & " " & "kg/mm^3")
Echo("New Density = " & dblDensity_g_cm3.ToString() & " " & "g/cm^3")
' Apply the Density to the body
For Each obj As Body In theSession.Parts.Work.Bodies
If TypeOf obj Is Body Then
ufs.Modl.SetBodyDensity(obj.Tag,UFModl.DensityUnits.GramsCentimeters, dblDensity_g_cm3)
End If
Next
' Update Weight Data
' ????????????????????
' ????????????????????
' ????????????????????
' ????????????????????
End Sub
Function select_a_body(ByVal prompt As String) As Body
Dim mask() As Selection.MaskTriple = {New Selection.MaskTriple( _
UFConstants.UF_solid_type, 0, UFConstants.UF_UI_SEL_FEATURE_BODY)}
Dim cursor As Point3d = Nothing
Dim obj As TaggedObject = Nothing
Dim resp As Selection.Response = _
UI.GetUI().SelectionManager.SelectTaggedObject("Select a body", prompt, _
Selection.SelectionScope.AnyInAssembly, _
Selection.SelectionAction.ClearAndEnableSpecific, _
False, False, mask, obj, cursor)
Return obj
End Function
Public Function GetUnloadOption(ByVal dummy As String) As Integer
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
Sub Echo(ByVal output As String)
theSession.ListingWindow.Open()
theSession.ListingWindow.WriteLine(output)
theSession.LogFile.WriteLine(output)
End Sub
End Module





RE: NX Journal Question - Update Weight Data Now
This code updates the mass of the work part:
CODE --> VB
Hope this helps!
NX8.0.3.4 TC 9.1.2.3.
HP Z820 / QuadroK4000
RE: NX Journal Question - Update Weight Data Now
RE: NX Journal Question - Update Weight Data Now
Option Strict Off Imports System Imports NXOpen Imports NXOpenUI Imports NXOpen.UF ' Note: This is adapted from http://www.nxjournaling.com/content/measure-bodies and ' the GTAC sample called: Report each body density with units.vb Module NXJournal Sub Main() Dim theSession As Session = Session.GetSession() Dim ufs As UFSession = UFSession.GetUFSession() Dim workPart As Part = theSession.Parts.Work Dim lw As ListingWindow = theSession.ListingWindow Dim dblMass As Double = 10.0 Dim dblVolume as Double = 0 Dim dblDensity_kg_mm3 as Double = 0 Dim dblDensity_g_cm3 as Double = 0 Dim strVolumeUnits as String Dim iBodies(0) As IBody lw.Open() lw.WriteLine("Original Body Mass Properties:") For Each aBody As Body In workPart.Bodies Dim mm As MeasureManager = workPart.MeasureManager() iBodies(0) = aBody Dim massUnits(4) As Unit massUnits(0) = workPart.UnitCollection.GetBase("Area") massUnits(1) = workPart.UnitCollection.GetBase("Volume") massUnits(2) = workPart.UnitCollection.GetBase("Mass") massUnits(3) = workPart.UnitCollection.GetBase("Length") Dim mb As MeasureBodies = mm.NewMassProperties(massUnits, 0.99, iBodies) lw.WriteLine(aBody.ToString() & ":") lw.WriteLine("Volume = " & mb.Volume.ToString() & " " & massUnits(1).Abbreviation()) lw.WriteLine("Current Mass = " & mb.Mass.ToString() & " " & massUnits(2).Abbreviation()) dblVolume = dblVolume + mb.Volume strVolumeUnits = massUnits(1).Abbreviation() Next lw.WriteLine("Total Volume of All Bodies = " & dblVolume.ToString() & " " & strVolumeUnits) ' Input the Mass Try dblMass = NXInputBox.GetInputNumber("Mass (kg):", "Input Mass (kg)", dblMass) Catch ex as Exception MsgBox("Okay, be that way. Cancelled!", MsgBoxStyle.Information) Return End Try lw.WriteLine(vbCrLf & "New Density:") ' Calculate the New Density dblDensity_kg_mm3 = dblMass/dblVolume dblDensity_g_cm3 = dblDensity_kg_mm3 * 1000000 lw.WriteLine(dblDensity_kg_mm3.ToString() & " " & "kg/mm^3") lw.WriteLine(dblDensity_g_cm3.ToString() & " " & "g/cm^3") ' Apply the Density to the body For Each obj As Body In theSession.Parts.Work.Bodies If TypeOf obj Is Body Then ufs.Modl.SetBodyDensity(obj.Tag,UFModl.DensityUnits.GramsCentimeters, dblDensity_g_cm3) End If Next ' Update Mass Data ' Adapted from http://www.eng-tips.com/viewthread.cfm?qid=391032 ThorA Posting Dim objects2(0) As NXOpen.NXObject objects2(0) = workPart Dim massPropertiesBuilder1 As NXOpen.MassPropertiesBuilder massPropertiesBuilder1 = workPart.PropertiesManager.CreateMassPropertiesBuilder(objects2) massPropertiesBuilder1.LoadPartialComponents = True massPropertiesBuilder1.Accuracy = 0.99 massPropertiesBuilder1.UpdateOnSave = NXOpen.MassPropertiesBuilder.UpdateOptions.Yes massPropertiesBuilder1.UpdateNow() massPropertiesBuilder1.Destroy() ' Dump out new mass dblVolume = 0 lw.WriteLine(vbCrLf & "New Body Mass Properties:") For Each aBody As Body In workPart.Bodies Dim mm As MeasureManager = workPart.MeasureManager() iBodies(0) = aBody Dim massUnits(4) As Unit massUnits(0) = workPart.UnitCollection.GetBase("Area") massUnits(1) = workPart.UnitCollection.GetBase("Volume") massUnits(2) = workPart.UnitCollection.GetBase("Mass") massUnits(3) = workPart.UnitCollection.GetBase("Length") Dim mb As MeasureBodies = mm.NewMassProperties(massUnits, 0.99, iBodies) lw.WriteLine(aBody.ToString() & ":") lw.WriteLine("Current Mass = " & mb.Mass.ToString() & " " & massUnits(2).Abbreviation()) dblVolume = dblVolume + mb.Volume strVolumeUnits = massUnits(1).Abbreviation() Next End Sub End Module