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!

Change Journal to add Density

Status
Not open for further replies.

Kenja824

Automotive
Nov 5, 2014
949
NX1872

Currently, GM gives us a material list that is automatically read when we go to tools > Materials > Assign Materials. Then to assign simple Steel 1020 to a solid body, they need to search through a really long list of materials and it gets annoying to them when they have to do this often. So I figured I would try and create a button that lets them select a solid body or multiple solid bodies and add the correct density to it so it will have the correct weight.

I have a journal that allows the user to select a solid body and it will add particular attributes to it. I was hoping to record myself selecting a solid body and changing the solid density to a specific number and add this to that code in place of the attributes. It isnt going so well. lol

In the below code, I have the code I recorded placed between two long rows of asterisks to make it easy to see what I added. I have tried commenting out things and changing words from (Body1) to (theBodies) and everything. I am starting to think I am not even on the right track of how this works.

Currently it gives me a Null Tag on the following bit of code....

nXObject2 = solidDensity1.Commit()

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

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim theUI As UI = UI.GetUI()
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()

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

        lw.Open()

        Dim selobj As NXObject
        Dim type As Integer
        Dim subtype As Integer
        Dim theBodies As New List(Of Body)
        Dim theUI As UI = UI.GetUI
        Dim numsel As Integer = theUI.SelectionManager.GetNumSelectedObjects()

        'process the preselected bodies
        If numsel > 0 Then

            For inx As Integer = 0 To numsel - 1
                selobj = theUI.SelectionManager.GetSelectedTaggedObject(inx)

                theUfSession.Obj.AskTypeAndSubtype(selobj.Tag, type, subtype)

                If type = UFConstants.UF_solid_type Then
                    theBodies.Add(selobj)
                End If
            Next
        Else

            'prompt to select bodies

            If SelectBodies("Select Bodies to add Material to", theBodies) = Selection.Response.Cancel Then
                Return
            End If
        End If

        For Each temp As Body In theBodies
            ' DeleteAllAttributes(temp)
            'AddBodyAttribute(temp, "TOOL_CLASS", "ALT STD")
            'AddBodyAttribute(temp, "STOCK_SIZE", "ANI201")
            'AddBodyAttribute(temp, "PURCH_OPTION", "P")
            'AddBodyAttribute(temp, "DB_PART_NAME", "NC BLANK")


            '******************************************************************************************************
            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 markId1 As NXOpen.Session.UndoMarkId = Nothing
            '           markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

            Dim solidDensity1 As NXOpen.GeometricAnalysis.SolidDensity = Nothing
            solidDensity1 = workPart.AnalysisManager.CreateSolidDensityObject()

            '          solidDensity1.Density = 1.0

            solidDensity1.Units = NXOpen.GeometricAnalysis.SolidDensity.UnitsType.PoundsPerCubicInches

            '           solidDensity1.Density = 1.0

            theSession.SetUndoMarkName(markId1, "Assign Solid Density Dialog")

            '            Dim theUI As UI = UI.GetUI()

            Dim added1 As Boolean = Nothing
            added1 = solidDensity1.Solids.Add(CType(theUI.SelectionManager.GetSelectedObject(0), NXOpen.Body))

            solidDensity1.Density = 15.0

            Dim markId2 As NXOpen.Session.UndoMarkId = Nothing
            markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Assign Solid Density")

            theSession.DeleteUndoMark(markId2, Nothing)

            Dim markId3 As NXOpen.Session.UndoMarkId = Nothing
            markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Assign Solid Density")

            Dim nXObject2 As NXOpen.NXObject = Nothing
            nXObject2 = solidDensity1.Commit()

            theSession.DeleteUndoMark(markId3, Nothing)

            theSession.SetUndoMarkName(markId1, "Assign Solid Density")

            solidDensity1.Destroy()

            '********************************************************************************************************

        Next

        lw.Close()
    End Sub

    Function SelectBodies(ByVal prompt As String, ByRef selBod As List(Of Body)) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select bodies"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim selectionMask_array(0) As Selection.MaskTriple
        Dim selObj() As TaggedObject

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

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj)
        If resp = Selection.Response.Ok Then
            For Each temp As TaggedObject In selObj
                selBod.Add(temp)
            Next
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

    'Sub AddBodyAttribute(ByVal theBody As Body, ByVal attTitle As String, ByVal attValue As String)

    '    Dim markId4 As Session.UndoMarkId
    '    markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "add body attribute")

    '    Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
    '    attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(theSession.Parts.Work, {theBody}, AttributePropertiesBuilder.OperationType.None)

    '    attributePropertiesBuilder1.IsArray = False
    '    attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String

    '    attributePropertiesBuilder1.Title = attTitle
    '    attributePropertiesBuilder1.StringValue = attValue

    '    Dim nXObject1 As NXObject
    '    nXObject1 = attributePropertiesBuilder1.Commit()

    '    Dim id1 As Session.UndoMarkId
    '    id1 = theSession.GetNewestUndoMark(Session.MarkVisibility.Visible)

    '    Dim nErrs1 As Integer
    '    nErrs1 = theSession.UpdateManager.DoUpdate(id1)

    '    attributePropertiesBuilder1.Destroy()

    'End Sub

    'Sub DeleteAllAttributes(ByVal theObject As NXObject)

    '    Dim attributeInfo() As NXObject.AttributeInformation = theObject.GetUserAttributes

    '    For Each temp As NXObject.AttributeInformation In attributeInfo
    '        theObject.DeleteUserAttributes(temp.Type, Update.Option.Now)
    '    Next

    'End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

    End Function


End Module



Some direction would help. Explanations would be appreciated, but keep them in laymans terms if possible. lol




Ken
My brain is like a sponge. A sopping wet sponge. When I use it, I seem to lose more than I soak in.
 
Replies continue below

Recommended for you

Hi

I have modified your code slightly....

Now it works....

Seems like you didn't add the correct Solid, to the DensityObject...
You need to deal with each cycled Body - in your case the variable is "temp"...

your code wrong here: added1 = [highlight #EF2929]solidDensity1.Solids.Add(CType(theUI.SelectionManager.GetSelectedObject(0), NXOpen.Body))[/highlight]
my changes: [highlight #8AE234]solidDensity1.Solids.Add(temp)[/highlight]

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

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim theUI As UI = UI.GetUI()
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()

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

        lw.Open()

        Dim selobj As NXObject
        Dim type As Integer
        Dim subtype As Integer
        Dim theBodies As New List(Of Body)
        Dim theUI As UI = UI.GetUI
        Dim numsel As Integer = theUI.SelectionManager.GetNumSelectedObjects()

        'process the preselected bodies
        If numsel > 0 Then

            For inx As Integer = 0 To numsel - 1
                selobj = theUI.SelectionManager.GetSelectedTaggedObject(inx)

                theUfSession.Obj.AskTypeAndSubtype(selobj.Tag, type, subtype)

                If type = UFConstants.UF_solid_type Then
                    theBodies.Add(selobj)
                End If
            Next
        Else

            'prompt to select bodies

            If SelectBodies("Select Bodies to add Material to", theBodies) = Selection.Response.Cancel Then
                Return
            End If
        End If
  lw.writeline(theBodies.Count)
        For Each temp As Body In theBodies
            ' DeleteAllAttributes(temp)
            'AddBodyAttribute(temp, "TOOL_CLASS", "ALT STD")
            'AddBodyAttribute(temp, "STOCK_SIZE", "ANI201")
            'AddBodyAttribute(temp, "PURCH_OPTION", "P")
            'AddBodyAttribute(temp, "DB_PART_NAME", "NC BLANK")


            '*********************************      Modified section by Lklo             *********************************
            Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
            Dim workPart As NXOpen.Part = theSession.Parts.Work
            Dim displayPart As NXOpen.Part = theSession.Parts.Display

            'prepare a CreateSolidDensityObject
            Dim solidDensity1 As NXOpen.GeometricAnalysis.SolidDensity = Nothing
            solidDensity1 = workPart.AnalysisManager.CreateSolidDensityObject()
            solidDensity1.Units = NXOpen.GeometricAnalysis.SolidDensity.UnitsType.KilogramsPerCubicMeters
            solidDensity1.Density = 15.0
            solidDensity1.Solids.Add(temp)
            Dim nXObject2 As NXOpen.NXObject = Nothing
            nXObject2 = solidDensity1.Commit()
            solidDensity1.Destroy()

            '********************************************************************************************************

        Next

        lw.Close()
    End Sub

    Function SelectBodies(ByVal prompt As String, ByRef selBod As List(Of Body)) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select bodies"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim selectionMask_array(0) As Selection.MaskTriple
        Dim selObj() As TaggedObject

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

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj)
        If resp = Selection.Response.Ok Then
            For Each temp As TaggedObject In selObj
                selBod.Add(temp)
            Next
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

    'Sub AddBodyAttribute(ByVal theBody As Body, ByVal attTitle As String, ByVal attValue As String)

    '    Dim markId4 As Session.UndoMarkId
    '    markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "add body attribute")

    '    Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
    '    attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(theSession.Parts.Work, {theBody}, AttributePropertiesBuilder.OperationType.None)

    '    attributePropertiesBuilder1.IsArray = False
    '    attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String

    '    attributePropertiesBuilder1.Title = attTitle
    '    attributePropertiesBuilder1.StringValue = attValue

    '    Dim nXObject1 As NXObject
    '    nXObject1 = attributePropertiesBuilder1.Commit()

    '    Dim id1 As Session.UndoMarkId
    '    id1 = theSession.GetNewestUndoMark(Session.MarkVisibility.Visible)

    '    Dim nErrs1 As Integer
    '    nErrs1 = theSession.UpdateManager.DoUpdate(id1)

    '    attributePropertiesBuilder1.Destroy()

    'End Sub

    'Sub DeleteAllAttributes(ByVal theObject As NXObject)

    '    Dim attributeInfo() As NXObject.AttributeInformation = theObject.GetUserAttributes

    '    For Each temp As NXObject.AttributeInformation In attributeInfo
    '        theObject.DeleteUserAttributes(temp.Type, Update.Option.Now)
    '    Next

    'End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

    End Function


End Module

Please keep in mind, that your code only works proper when you try assign density to a solid Body, that is NOT assigned a physical Material....
If you will continue in this direction, consider optimize code, so it will remove material id exists.....
----------------------------------------------------------------------------------------------------------------------------------------------
When this being said, try consider develop a NXOpen utility, that can deal with the Materials from the NX material library...
I have developed a Tool (6000+ lines of journal) that can handle this action...
some Features:
* assign materials to solid in MODEL refset
* support multiple parts in same action...
* assign part list attributes in same operation..
* all information is based on a spreadsheet for each product....a Tab in the dialog for each...
see attached screen shots for information...

lklo
 
 https://files.engineering.com/getfile.aspx?folder=7669eb4b-051d-41a8-99b4-164edb5abd4b&file=physical_material_manager_3.zip
Status
Not open for further replies.

Part and Inventory Search

Sponsor