alj722
Aerospace
- Nov 4, 2004
- 39
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):
[pre]
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
[/pre]
[pre]
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
[/pre]