×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

NX Journal Question - Update Weight Data Now

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

Hi,

This code updates the mass of the work part:

CODE --> VB

' NX 10.0.1.4
' Journal created by jevtho04 on Fri Jul 10 21:49:42 2015 Romance Daylight Time
'
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String) 

Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work

Dim displayPart As NXOpen.Part = theSession.Parts.Display

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()

End Sub
End Module 

Hope this helps!

NX8.0.3.4 TC 9.1.2.3.
HP Z820 / QuadroK4000

RE: NX Journal Question - Update Weight Data Now

(OP)
Bingo! That works (in a lot less than 139 lines). Thank you.

RE: NX Journal Question - Update Weight Data Now

(OP)
Here is an updated version of the NX Journal file so that it now assigns a mass to a body or multiple bodies in a part. It calculates the volume of all the bodies in the part, calculates a density and then assigns the density to all the bodies.

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
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources