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!

Journal Layer Name 1

Status
Not open for further replies.

DHuskic

Computer
Dec 18, 2012
114
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:
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
 
Replies continue below

Recommended for you

dhusk09 said:
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
 
Cowski said:
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
 
Check out thread561-344591
I posted some code there that will remove sheet bodies from a reference set. As you are iterating through the objects in the reference set you can query the object's layer.

www.nxjournaling.com
 
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:
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
 
Code:
[COLOR=#CC0000]Dim MyCompPart As Part = Comp.Prototype[/color]

A prototype is not a part.

Instead try:
Code:
Dim MyCompPart As Part = Comp.Prototype.OwningPart

www.nxjournaling.com
 
What does the OwningPart command call?
That gave me a new error at the following line:
Code:
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
 
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
 
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:
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
 

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor