cancel
Showing results for 
Search instead for 
Did you mean: 

Determine if attribute is linked to expression and retrieve said expression

Innovator
Innovator

Hello everyone,

 

I am having a hard time determing if an attribute is linked to an expression.

With the AttributePropertiesBuilder I can easily link an expression to an attribute, but have had no luck so far determining if:

 

  • An attribute is linked to an expression,
  • and if it is linked to an expression, retrieving the expression object (Expression).

Is this even possible? I understand from the AttributePropertiesBuilder there is an Expression property which returns the expression that the attribute is linked to (supposedly), but I am confused if this is only used if the expression has been set during the lifetime of the builder. Is this a correct assumption?

 

I cannot use the solution provided by GTAC (loop through all expressions in the same part as the attribute, compare values with a tolerance), because I have more than one expression that can contain the same value. I need the actual Expression object in this case.

 

Recording a journal has yielded no helpful results.

 

Any help is welcome, thank you!!

Marc Prosl
NX Software Developer
6 REPLIES

Re: Determine if attribute is linked to expression and retrieve said expression

Honored Contributor
Honored Contributor

I opened a PR on this subject a while back, it was converted to an enhancement request (ER 7169336). You can contact GTAC and add your name to the ER; the more people request it, the higher priority it will be given.

Re: Determine if attribute is linked to expression and retrieve said expression

Innovator
Innovator
Thanks for the speedy reply; I will add my name to the ER.
Marc Prosl
NX Software Developer

Re: Determine if attribute is linked to expression and retrieve said expression

Legend
Legend

I do the following as a work around.

 

Asuming that an object is known I set the listing window to output to a file

 

lw.SelectDevice(ListingWindow.DeviceType.File, datafile)

 

Then using information on the object(s)

 

s.Information.DisplayObjectsDetails(selectedObjects1)

 

I then read the datafile to extract required attribute info which allows me to do all I needed.

 

Not sure if this helps you.  If not maybe you can create a small sample test part and I would be happy to look at it to see if we can do a similar work around.

 

Frank Swinkels

Re: Determine if attribute is linked to expression and retrieve said expression

Innovator
Innovator

Hi Frank,

 

thanks for the great tip. I will give it a shot.

Marc Prosl
NX Software Developer

Re: Determine if attribute is linked to expression and retrieve said expression

Valued Contributor
Valued Contributor

I've also sent an IR which is now an ER (1991520).

 

http://community.plm.automation.siemens.com/t5/NX-Languages/Getting-Information-of-part-attribute-NX...

 

Greetings

Herbert Macho
EBNER Industrieofenbau
AUSTRIA

NX10.0.3.5 (TeamCenter10 and Native)

Re: Determine if attribute is linked to expression and retrieve said expression

Legend
Legend

Attached is a NX9 assembly with two ID symbols attached to individual components to demonstrate a partial workaround for this problem area.  I say partial because, for linked expressions the text string must be formatted correctly by having the expression in the display part.  If an attribute has a linked expression in a component part but the ID Symbol (in my case) is inserted to a drawing sheet and the display part is the assembly then it is not possible to get the correct linked expression.

 

Below is the journal for the two cases that can be determined.

 

Option Strict Off
Imports System
Imports System.IO
Imports System.Environment
Imports NXOpen
Imports NXOpenUI
Imports NXOpen.UF
Imports NXOpen.Annotations
Imports NXOpen.Assemblies
Imports NXOpen.Utilities

Module ControllingAttribute1
    Dim s As Session = Session.GetSession()
    Dim ui As UI = ui.GetUI()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = s.ListingWindow
    Dim wp As Part = s.Parts.Work
    Dim dp As Part = s.Parts.Display
    Sub Main()
        Dim symbol As IdSymbol = Nothing
        Dim cursor As Point3d = Nothing
        Dim response1 As Selection.Response = Selection.Response.Cancel
        lw.Open()
        While select_an_IdSymbol(symbol) = Selection.Response.Ok
            ' check for expression or attribute
            Dim IDText As String = Nothing
            Dim idSymbolBuilder1 As Annotations.IdSymbolBuilder
            idSymbolBuilder1 = wp.Annotations.IdSymbols.CreateIdSymbolBuilder(symbol)
            IDText = idSymbolBuilder1.UpperText
            idSymbolBuilder1.Destroy()
            If IDText(1) = "X" Then
                getDataforLinkedExpression(IDText)
            ElseIf IDText(1) = "W" Then
                getDataforAttribute(IDText, symbol)
            Else
                ' do nothing
            End If
        End While
    End Sub
   
    Function select_an_IdSymbol(ByRef obj As IdSymbol)
        Dim mask(0) As Selection.MaskTriple
        With mask(0)
            .Type = UFConstants.UF_drafting_entity_type
            .Subtype = UFConstants.UF_draft_id_symbol_subtype
            .SolidBodySubtype = 0
        End With
        Dim cursor As Point3d = Nothing
        Dim resp As Selection.Response = _
        ui.SelectionManager.SelectTaggedObject("Select a IdSymbol", "Select a IdSymbol", _
            Selection.SelectionScope.AnyInAssembly, _
            Selection.SelectionAction.ClearAndEnableSpecific, _
            False, False, mask, obj, cursor)
        If resp = Selection.Response.ObjectSelected Or _
           resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If
    End Function

    Private Sub getDataforAttribute(ByVal IDText As String, ByVal symbol As IdSymbol)
        Dim compPart As Part = nothing
        Dim pos1 As Integer = IDText.IndexOf("@")
        Dim refString As String = IDText.Substring(2, pos1 - 2)
        Dim selectedObjects1(0) As NXObject
        selectedObjects1(0) = symbol
        ' define temp file name.  Change as required for your system
        Dim datafile As String = "C:\Temp\data1.txt"
        lw.SelectDevice(ListingWindow.DeviceType.File, datafile)
        s.Information.DisplayObjectsDetails(selectedObjects1)
        lw.SelectDevice(ListingWindow.DeviceType.Window, Nothing)
        ReadTempFile(datafile, refString, compPart)
        ' get the attribute of interest 
        Dim detailtitle As String = "DETAIL NO."
        Dim detailvalue As String = Nothing
        getAttributeValue(compPart, detailtitle, detailvalue)
        lw.WriteLine("Attribute linked data")
        lw.WriteLine("---------------------")
        lw.WriteLine("   ID Symbol text string:  " & IDText)
        lw.WriteLine("   Component Part:  " & compPart.FullPath)
        lw.WriteLine("   Attribute Title:  " & detailtitle)
        lw.WriteLine("   Attribute Value:  " & detailvalue)
        lw.WriteLine("   ")
        '' now get the attribute owning expression
        'Dim expcollection As ExpressionCollection = compPart.Expressions
        'Dim exp1 As Expression = Nothing
        'exp1 = expcollection.GetAttributeExpression(compPart, detailtitle, NXObject.AttributeType.String, -1)
        'MsgBox(exp1.Equation.ToString)
    End Sub
    Public Sub getDataforLinkedExpression(ByVal IDText As String)
        Dim pos1 As Integer = IDText.IndexOf("@")
        Dim len1 As Integer = IDText.Length
        Dim refString As String = IDText.Substring(pos1 + 1, len1 - pos1 - 2)
        Dim linkedexp As Expression = dp.Expressions.FindObject(refString)
        Dim exp_equation As String = linkedexp.Equation.ToString
        pos1 = exp_equation.IndexOf(Chr(34))
        Dim pos2 As Integer = exp_equation.LastIndexOf(Chr(34))
        Dim partfileleaf As String = exp_equation.Substring(pos1 + 1, pos2 - pos1 - 1)
        Dim partTag As Tag = ufs.Part.AskPartTag(partfileleaf)
        Dim comppart As Part = NXObjectManager.Get(partTag)
        len1 = exp_equation.Length
        pos1 = exp_equation.LastIndexOf(":")
        Dim expressionname As String = exp_equation.Substring(pos1 + 1, len1 - pos1 - 1)
        Dim exp1 As Expression = comppart.Expressions.FindObject(expressionname)
        lw.WriteLine("Expression linked data")
        lw.WriteLine("---------------------")
        lw.WriteLine("   ID Symbol text string:  " & IDText)
        lw.WriteLine("   Component Part:  " & comppart.FullPath)
        lw.WriteLine("   Expression Name:  " & expressionname)
        lw.WriteLine("   Expression Value:  " & exp1.RightHandSide.ToString)
        lw.WriteLine("   ")

    End Sub
    Public Sub ReadTempFile(ByVal datafile As String, ByVal reftext As String, ByRef comppart As Part)
        Dim linestring As String = Nothing
        Dim linelength As Integer = Nothing
        Dim pos1 As Integer = Nothing
        Dim filename As String = Nothing
        Dim sr As StreamReader = File.OpenText(datafile)
        lw.Open()
        Do While sr.Peek >= 0
            linestring = sr.ReadLine
            Try
                If linestring.Substring(0, 15) = "Attribute Owner" Then
                    linelength = Len(linestring)
                    pos1 = linestring.IndexOf(":")
                    filename = linestring.Substring(pos1 + 1, linelength - pos1 - 1)
                End If
            Catch ex As Exception

            End Try
        Loop
        sr.Close()
        File.Delete(datafile)
        Dim partTag As Tag = Tag.Null
        partTag = ufs.Part.AskPartTag(filename)
        comppart = NXObjectManager.Get(partTag)
    End Sub
    Public Sub getAttributeValue(ByVal thepart As Part, ByVal attrName As String, ByRef attrVal As String)
        Dim theAttr As Attribute = Nothing
        Dim attr_info As NXObject.AttributeInformation
        attr_info = thepart.GetUserAttribute(attrName, NXObject.AttributeType.String, Nothing)
        attrVal = attr_info.StringValue
    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 check the code for the locatation of the temporary datafile.  Also I have commented out some code for the system created linked expression.

 

Hope this is of some help.

 

It may also generate some further discussion regarding other cases I have not needed myself.

 

Frank Swinkels