Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Change value of expression with a journal

Status
Not open for further replies.

Goof2014

Automotive
Dec 5, 2013
9
Hello together,

how could I change the value of an expression of a part which is in an assembly?

Assembly
|
|>Part in which I would like to change the value

Name: TypeChange
Value: 1 (Integer)

Thank you very much

Goof
 
Replies continue below

Recommended for you

Do you have any code started? if so, where are you stuck?

General strategy:
[ol][li]Make the component of interest the work part[/li]
[li]Get the desired expression[/li]
[li]Change the expression's .Value or .RightHandSide property[/li]
[/ol]

www.nxjournaling.com
 
The Code for now. But how could I select the part in which the expression is and check it if it exist?

Thank you Goof

Code:
Option Strict Off
Imports System
Imports NXOpen

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

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

Dim displayPart As Part = theSession.Parts.Display

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Make Work Part")

Dim component1 As Assemblies.Component = CType(workPart.ComponentAssembly.RootComponent.FindObject("Part_Where_the_expression_is"), Assemblies.Component)

Dim partLoadStatus1 As PartLoadStatus
theSession.Parts.SetWorkComponent(component1, partLoadStatus1)

workPart = theSession.Parts.Work
partLoadStatus1.Dispose()
theSession.SetUndoMarkName(markId1, "Make Work Part")

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression")

Dim expression1 As Expression = CType(workPart.Expressions.FindObject("TYPAUSWAHL"), Expression)

Dim nullUnit As Unit = Nothing

Dim oValue as String =Inputbox("Number")


'workPart.Expressions.EditWithUnits(expression1, nullUnit, "2")
workPart.Expressions.EditWithUnits(expression1, nullUnit, oValue)

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Update Expression Data")

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId3)

theSession.DeleteUndoMark(markId3, "Update Expression Data")

Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Update Expression Data")

Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId4)

theSession.DeleteUndoMark(markId4, "Update Expression Data")

Dim markId5 As Session.UndoMarkId
markId5 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Make Work Part")

Dim nullAssemblies_Component As Assemblies.Component = Nothing

Dim partLoadStatus2 As PartLoadStatus
theSession.Parts.SetWorkComponent(nullAssemblies_Component, partLoadStatus2)

workPart = theSession.Parts.Work
partLoadStatus2.Dispose()
theSession.SetUndoMarkName(markId5, "Make Work Part")

End Sub
End Module
 
Do you want the user to interactively select the part, or have the journal select the part based on your defined logic/parameters?

www.nxjournaling.com
 
The journal should search for the part with a given name. When the part is missing or the name has changed then the user should select the part with the expression.
 
It could use some polish, but here's some code:

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

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim dispPart As Part = theSession.Parts.Display
    Dim workPart As Part = theSession.Parts.Work
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()

        If IsNothing(theSession.Parts.Work) Then
            'active part required
            Return
        End If

        'lw.SelectDevice(ListingWindow.DeviceType.Window, "")
        lw.SelectDevice(ListingWindow.DeviceType.None, "")
        lw.Open()

        Const undoMarkName As String = "change component expression"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

        '*****
        'change the following line to the desired component display name
        Const compName As String = "WIDGET"
        '*****

        Dim theComponent As Assemblies.Component = Nothing

        Try
            ' TODO: Add your application code here 
            theComponent = FindComponent(compName)

            If IsNothing(theComponent) Then
                lw.WriteLine("component not found")
                'ask user to select component
                If SelectComponent("Select a component", theComponent) = Selection.Response.Cancel Then
                    'user pressed cancel, exit journal
                    Return
                End If
            Else
                lw.WriteLine("component found, tag: " & theComponent.Tag.ToString)
            End If

        Catch ex As NXException
            theSession.UndoToMark(markId1, undoMarkName)
            MsgBox(ex.Message)

        Finally

        End Try

        lw.WriteLine("component.DisplayName: " & theComponent.DisplayName)
        MakeWorkPart(theComponent)

        Dim expToFind As String = "TypeChange"

        Try
            Dim myExp As Expression
            myExp = workPart.Expressions.FindObject(expToFind)
            lw.WriteLine(myExp.Name)
            lw.WriteLine(myExp.RightHandSide)
            myExp.RightHandSide = "1"
        Catch ex As NXException
            'MsgBox(ex.Message)
            If ex.ErrorCode = 3520016 Then
                'no object found with this name
                'code to handle this error
                lw.WriteLine("expression not found with name: " & expToFind)
            Else
                'code to handle other errors
                lw.WriteLine(ex.ErrorCode & ": " & ex.Message)
            End If
        End Try

        lw.Close()

    End Sub

    Function FindComponent(ByVal theName As String) As Assemblies.Component

        Dim tempComp As Assemblies.Component = Nothing
        lw.WriteLine("searching: " & dispPart.Leaf)

        Try
            Dim c As Assemblies.ComponentAssembly = dispPart.ComponentAssembly

            If Not IsNothing(c.RootComponent) Then
                tempComp = SearchComponentChildren(c.RootComponent, theName)
            Else
                'no components
                Return Nothing
            End If
        Catch ex As NXException
            MsgBox("Error: " & ex.Message)
            Return Nothing
        End Try

        Return tempComp

    End Function

    Function SearchComponentChildren(ByVal comp As Assemblies.Component, ByVal theName As String) As Assemblies.Component

        Dim tempComp As Assemblies.Component
        lw.WriteLine("searching through components...")

        For Each child As Assemblies.Component In comp.GetChildren()
            lw.WriteLine("...processing: " & child.DisplayName)
            If child.GetChildren.Length <> 0 Then
                '*** this is a subassembly
                lw.WriteLine("...this is a subassembly, searching through its components...")
                tempComp = SearchComponentChildren(child, theName)

            Else
                'this component has no children (it is a leaf node)
                'add any code specific to bottom level components
                If child.DisplayName.ToUpper = theName.ToUpper Then
                    tempComp = child
                    lw.WriteLine("component found, returning: " & tempComp.DisplayName)
                    Return tempComp
                End If

            End If

        Next

        'we've run through all the children and didn't find it...
        Return Nothing

    End Function

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

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select a component"
        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_component_type
            .Subtype = UFConstants.UF_component_subtype
        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

    Sub MakeWorkPart(ByVal theComponent As Assemblies.Component)

        Dim partLoadStatus1 As PartLoadStatus

        Try
            theSession.Parts.SetWorkComponent(theComponent, PartCollection.RefsetOption.Current, PartCollection.WorkComponentOption.Visible, partLoadStatus1)
            workPart = theSession.Parts.Work

        Catch ex As NXException
            MsgBox("Error: " & ex.Message)

        Finally
            partLoadStatus1.Dispose()

        End Try

    End Sub

End Module

www.nxjournaling.com
 
Hello Cowski,

thank you very much for the code. I try it today.

Goof
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor