×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Journal Layer Name

Journal Layer Name

Journal Layer Name

(OP)
So I have a simple journal to give me a name attribute for each component. The reason I do not want to use the one that comes standard in the navigator is because it gives me the Original layer instead of a number. However, this journal does not give me numbers either. Any ideas how to get around the Original layer settings?

CODE --> vb

Option Strict Off
Imports System
Imports System.Collections
Imports NXOpen
Imports NXOpen.Utilities
Imports NXOpen.Assemblies

   Module LayerName

Dim theSession As Session = Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession()

   Sub Main()
Dim displaypart As Part = theSession.Parts.Display
Dim c As Component = displaypart.ComponentAssembly.RootComponent
Dim children As Component() = c.GetChildren()

For Each comp As Component In children
   Comp.SetAttribute("LayerName", comp.layer)
Next

End Sub
End Module 

Denis Huskic
Data Prep NX7.5
Kettering University
Class of '17

RE: Journal Layer Name

Quote (dhusk09)

Any ideas how to get around the Original layer settings?

Short answer: Change the component layer option to "specified layer" rather than "original".

The "original" option will map your component part layers to the assembly part layers. The component is no longer on a single layer that NX can report. Usually, the component is only a single solid body that you are interested in. If this is the case, you can create a unique reference set for this solid and use it in the assembly. In your journal file, you can get the body from this ref set and query it to see what layer it is on.

www.nxjournaling.com

RE: Journal Layer Name

(OP)

Quote (Cowski)

In your journal file, you can get the body from this ref set and query it to see what layer it is on.

How would I go about attempting this? These bodies are already on the "TRUE" reference set. Each component has 1 body on that reference set, I need to grab that layer and make it a new attribute called LayerName. I am stuck on transitioning between finding the reference set and the body.

Denis Huskic
Data Prep NX7.5
Kettering University
Class of '17

RE: Journal Layer Name

(OP)
System.InvalidCastException: Unable to cast object of type 'NXOpen.Part' error.
Any ideas as to why I might be receiving this error? Here is my code:

CODE --> vb

Option Strict Off  
Imports System  
Imports NXOpen
Imports NXOpen.Assemblies 

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim displaypart As Part = theSession.Parts.Display

    Sub Main()
        Dim c As Component = displaypart.ComponentAssembly.RootComponent
        ProcessAssemblyTree(c)
    End Sub

    Sub ProcessAssemblyTree(ByVal c As Component)
        Dim workpart As Part = theSession.Parts.Work
        Dim theReferenceSet As ReferenceSet
        Dim refMembers(-1) As NXObject
        Const whatRefSet As String = "TRUE"
        Dim children As Component() = c.GetChildren()

        For each comp As Component in children
           'Dim component1 As Assemblies.Component = comp
           'Dim partLoadStatus1 As PartLoadStatus
           'theSession.Parts.SetWorkComponent(component1, partLoadStatus1)
           'workPart = theSession.Parts.Work

           Dim MyCompPart As Part = Comp.Prototype
           For Each myRefSet As ReferenceSet In MyCompPart.GetAllReferenceSets()
              If myRefSet.Name = whatRefSet Then
                 theReferenceSet = myRefSet
                 refMembers = myRefSet.AskAllDirectMembers()
                    For Each myObject As DisplayableObject In refMembers
                       If TypeOf myObject Is Body Then
                          Dim myBody As Body = myObject
                          Dim temp As String
                          temp = myObject.Layer
                          Comp.SetAttribute("LayerNumber", temp)
                       End If
                    Next
              End If
           Next
           ProcessAssemblyTree(Comp)
        Next

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

    End Function

End Module 

Denis Huskic
Data Prep NX7.5
Kettering University
Class of '17

RE: Journal Layer Name

CODE

Dim MyCompPart As Part = Comp.Prototype 

A prototype is not a part.

Instead try:

CODE

Dim MyCompPart As Part = Comp.Prototype.OwningPart 

www.nxjournaling.com

RE: Journal Layer Name

(OP)
What does the OwningPart command call?
That gave me a new error at the following line:

CODE --> vb

For Each myRefSet As ReferenceSet In MyCompPart.GetAllReferenceSets() 

The error was NullReferenceException: Object reference not set to an instance of an object.

Denis Huskic
Data Prep NX7.5
Kettering University
Class of '17

RE: Journal Layer Name

Echo the value before the assignment to see what you get:

CODE

msgbox(Comp.Prototype.OwningPart.FullPath) 

We might need to tweak the code a bit to get all the way up the chain to the owning part.

www.nxjournaling.com

RE: Journal Layer Name

(OP)
As of now, I have the journal working. I put in a Try-End Try statement in there to catch those reference errors and it continued running well. It may be a jerry rigged way of doing it but it ends up spitting out a layer number for every object that meets the criteria, including the ones that gave me the errors before. This seemed extremely weird to me so I tested it on different files, it cleared the error and gave me what I wanted. The problem I run into now, is that some components have more than one body on the reference set, but they are on different layers. Any idea how to add a priority/selection filter? My goal is to have it select the body that meets the criteria, that is first on the Part Navigator's list with it sorted by name(Alphabetical). Now, it is selecting the displayable objects that match the Part's reference set. I think if I switch it to a MyCompPart.GetBodies and then sort those for whichever ones come first alphabetically. There is still probably a better way to do this.

CODE --> VB

Option Strict Off  
Imports System  
Imports NXOpen
Imports NXOpen.Assemblies 

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim displaypart As Part = theSession.Parts.Display

    Sub Main()
        Dim c As Component = displaypart.ComponentAssembly.RootComponent
        ProcessAssemblyTree(c)
    End Sub

    Sub ProcessAssemblyTree(ByVal c As Component)
        Dim workpart As Part = theSession.Parts.Work
        Dim theReferenceSet As ReferenceSet
        Dim refMembers(-1) As NXObject
        Const whatRefSet As String = "TRUE"
        Dim children As Component() = c.GetChildren()

        For each comp As Component in children

        Try
           Dim MyCompPart As Part = Comp.Prototype.OwningPart 
           For Each myRefSet As ReferenceSet In MyCompPart.GetAllReferenceSets()
              If myRefSet.Name = whatRefSet Then
                 theReferenceSet = myRefSet
                 refMembers = myRefSet.AskAllDirectMembers()
                    For Each myObject As DisplayableObject In refMembers
                       If TypeOf myObject Is Body Then
                          Dim myBody As Body = myObject
                          Dim temp As String
                          temp = myObject.Layer
                          Comp.SetAttribute("LayerNumber", temp)
                       End If
                    Next
              End If
           Next
        Catch ex As Exception
           'Do Nothing
        End Try
           ProcessAssemblyTree(Comp)
        Next

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    End Function

End Module 

Denis Huskic
Data Prep NX7.5
Kettering University
Class of '17

RE: Journal Layer Name


CODE

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Assemblies

Module Module2

    Dim theSession As Session = Session.GetSession()
    Dim displaypart As Part = theSession.Parts.Display
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()
        lw.Open()
        Dim c As Component = displaypart.ComponentAssembly.RootComponent
        ProcessAssemblyTree(c)
        lw.Close()
    End Sub

    Sub ProcessAssemblyTree(ByVal c As Component)
        'Dim workpart As Part = theSession.Parts.Work
        'Dim theReferenceSet As ReferenceSet
        Dim refMembers() As NXObject
        Const whatRefSet As String = "MODEL"
        Dim children As Component() = c.GetChildren()

        For Each comp As Component In children
            Dim MyCompPart As Part
            Dim firstBody As Body
            Dim firstTimeStamp As Integer
            Dim featureName As String

            Try

                MyCompPart = comp.Prototype.OwningPart
                lw.WriteLine("")
                lw.WriteLine("MyCompPart: " & MyCompPart.FullPath)

                For Each myRefSet As ReferenceSet In MyCompPart.GetAllReferenceSets()
                    lw.WriteLine("ref set name: " & myRefSet.Name)
                    Dim found As Boolean = False
                    If myRefSet.Name = whatRefSet Then
                        firstTimeStamp = MyCompPart.Features.ToArray.Length
                        'lw.WriteLine("'first' time stamp: " & firstTimeStamp.ToString)
                        'lw.WriteLine("num features in part: " & MyCompPart.Features.ToArray.Length.ToString)
                        'theReferenceSet = myRefSet
                        refMembers = myRefSet.AskAllDirectMembers()
                        'lw.WriteLine("num ref set members: " & myRefSet.AskAllDirectMembers.Length.ToString)
                        For Each myObject As DisplayableObject In refMembers
                            If TypeOf myObject Is Body Then
                                Dim myBody As Body = myObject
                                Dim temp As String = myObject.Layer.ToString
                                'comp.SetAttribute("LayerNumber", temp)
                                For Each bodFeature As Features.Feature In myBody.GetFeatures
                                    'lw.WriteLine("num features of body: " & myBody.GetFeatures.Length.ToString)
                                    If bodFeature.Timestamp < firstTimeStamp Then
                                        found = True
                                        'lw.WriteLine("firstTimeStamp: " & firstTimeStamp.ToString)
                                        'lw.WriteLine("bodFeature.Timestamp: " & bodFeature.Timestamp.ToString)
                                        firstTimeStamp = bodFeature.Timestamp
                                        firstBody = myBody
                                        featureName = bodFeature.GetFeatureName
                                        'lw.WriteLine("featureName: " & featureName)
                                    End If
                                Next
                            End If
                        Next
                    End If

                    If found Then
                        lw.WriteLine("first body in feature tree is child of feature: " & featureName)
                        lw.WriteLine("this body is on layer: " & firstBody.Layer.ToString)
                    End If

                Next
            Catch ex As NullReferenceException
                lw.WriteLine("")
                lw.WriteLine("## " & comp.DisplayName & " not loaded")
            Catch ex As NXException
                'Do Nothing
                If ex.ErrorCode = 720074 Then
                    lw.WriteLine("** component not fully loaded")
                Else
                    lw.WriteLine("Error: " & "(" & ex.ErrorCode & ") " & ex.Message)
                End If
            End Try
            ProcessAssemblyTree(comp)
        Next

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    End Function

End Module 

www.nxjournaling.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources