Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

NX 10 | Update density of body 1

Status
Not open for further replies.

blaze10

Computer
Nov 27, 2020
3
Hi,

I want to update density of a component in an assembly.

I can't update the density using: setUserAttribute() because the density attribute is read-only.(I have tried to unlock it with setUserAttributeLock(), but I get an error saying I have insufficient privileges to perform this action)

Then I came across this API: UfSession.Modl.SetBodyDensity() which accepts body tag as input to update the density.

To use this API, I will need to convert the object from type component to Body which I am not able to do.

Please can someone help me with this?

Thank you!
 
Replies continue below

Recommended for you

Are you updating the density in the model file of the component itself, or the assembly ? It should not be in the assembly.

Jerry J.
UGV5-NX1884
 
If a material was assigned to the body in the part file, the density will be assigned according to the material properties and locked. The density will be controlled by the material assigned to the body. To change the density, you will need to change the material assignment, or un-assign the material and then you can set the solid density as you wish.

www.nxjournaling.com
 
I want to update the density in the model file of the component.

I am trying the following using a NX Journal:
1. Select a component from an Assembly using theUI.SelectionManager.GetSelectedTaggedObject().
2. Obtain the body associated with the selected component.
3. Update the density with UfSession.Modl.SetBodyDensity() API.

I am not able to fetch the body associated with the selected component.
 
Make sure your selection filter is set to either SOLID BODY or NO SELECTION FILTER

Capture_bermfa.jpg


Also verify all your layers are selectable.

Jerry J.
UGV5-NX1884
 
Try the code below; it allows you to select a body in a part file or assembly file. If the body selected is an occurrence (assembly body), it will get the prototype body (the defining body in the part file), set the part as the work part, and change the density to 1.0 kg/m^3 (change this in the code to your desired value). If there is a material assigned to the body, I don't recommend changing the density in this manner (but the code allows it); in this case, best practice would be to change the material assignment of the body to match the material/density desired.

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module3

    Private theSession As NXOpen.Session = NXOpen.Session.GetSession()
    Private theUfSession As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()

        Dim markId1 As NXOpen.Session.UndoMarkId
        markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "change body density")

        lw.Open()
        Dim theBody As Body = Nothing
        If SelectBody("Select a body", theBody) = Selection.Response.Cancel Then
            'selection cancelled, exit journal
            Return
        End If

        If theBody.IsOccurrence Then
            lw.WriteLine("body is occurrence")
            Dim partLoadStatus1 As NXOpen.PartLoadStatus
            theSession.Parts.SetWorkComponent(theBody.OwningComponent, NXOpen.PartCollection.RefsetOption.Current, NXOpen.PartCollection.WorkComponentOption.Visible, partLoadStatus1)

            theBody = theBody.Prototype
        Else
            lw.WriteLine("body is prototype")
        End If

        If theBody.HasUserAttribute("NX_Material", NXObject.AttributeType.String, -1) Then
            If String.IsNullOrEmpty(theBody.GetUserAttributeAsString("NX_Material", NXObject.AttributeType.String, -1)) Then
                lw.WriteLine("no material assigned")
            Else
                lw.WriteLine("Material: " & theBody.GetUserAttributeAsString("NX_Material", NXObject.AttributeType.String, -1))
            End If
        End If

        Dim bodyDensity As Double
        theUfSession.Modl.AskBodyDensity(theBody.Tag, UFModl.DensityUnits.KilogramsMeters, bodyDensity)
        lw.WriteLine("original body density: " & bodyDensity.ToString & " kg/m^3")

        theUfSession.Modl.SetBodyDensity(theBody.Tag, UFModl.DensityUnits.KilogramsMeters, 1.0)

        theUfSession.Modl.AskBodyDensity(theBody.Tag, UFModl.DensityUnits.KilogramsMeters, bodyDensity)
        lw.WriteLine("new body density: " & bodyDensity.ToString & " kg/m^3")

        lw.Close()

    End Sub

    Function SelectBody(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select a body"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim cursor As Point3d
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = UFConstants.UF_solid_type
            .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
        End With

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt,
        title, scope, selAction,
        includeFeatures, keepHighlighted, selectionMask_array,
        selObj, cursor)
        If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function

End Module

www.nxjournaling.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor