cancel
Showing results for 
Search instead for 
Did you mean: 

View Orientation Fit NX Journal for Assemblies

Experimenter
Experimenter

I am new to NX 7.5 and I am writing a Journal to simply fit the part within the view and then set the view to isometric, and save. My only difficulty is applying this to a large assembly that has 100+ Components and sub Assemblies. Currently I have the view orient code below. How do I apply this to multiple parts/assemblies?

Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main

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

Dim displayPart As Part = theSession.Parts.Display

workPart.ModelingViews.WorkView.Fit()

workPart.ModelingViews.WorkView.Orient(View.Canned.Isometric, View.ScaleAdjustment.Fit)

Dim partSaveStatus1 As PartSaveStatus
partSaveStatus1 = workPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)

partSaveStatus1.Dispose()

End Sub
End Module

6 REPLIES

Re: View Orientation Fit NX Journal for Assemblies

Experimenter
Experimenter

Update:
I've compiled code that works for the top assembly and the 1st level parts.
Is there anyway to modify this to work on subassemblies as well?



Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Imports System.IO
Imports NXOpenUI
Imports System.Windows.Forms

Module NXJournal

Public theSession As Session = Session.GetSession()
Public ufs As UFSession = UFSession.GetUFSession()
Public lw As ListingWindow = theSession.ListingWindow

Sub Main()
Dim workPart As Part = theSession.Parts.Work
Dim dispPart As Part = theSession.Parts.Display
'lw.Open
Try
Dim c As ComponentAssembly = dispPart.ComponentAssembly
'to process the work part rather than the display part,
' comment the previous line and uncomment the following line
'Dim c As ComponentAssembly = workPart.ComponentAssembly
if not IsNothing(c.RootComponent) then
'*** insert code to process 'root component' (assembly file)
'lw.WriteLine("Assembly: " & c.RootComponent.DisplayName)
'lw.WriteLine(" + Active Arrangement: " & c.ActiveArrangement.Name)
'*** end of code to process root component
'lw.WriteLine("")
'lw.WriteLine("comps")
'lw.WriteLine("")
dim comps() as component = c.RootComponent.getchildren()
Dim part2 As Part = CType(theSession.Parts.FindObject(c.RootComponent.DisplayName), Part)
for i as integer = 0 to ( comps.length - 1 )
'lw.writeline( comps(i).name )
'-----------------------------------------------------------
Dim component1 As Assemblies.Component = comps(i)
Dim components1(0) As Assemblies.Component
components1(0) = component1
Dim errorList1 As ErrorList
errorList1 = component1.DisplayComponentsExact(components1)
errorList1.Clear()
Dim part1 As Part = CType(theSession.Parts.FindObject(comps(i).displayname), Part)
part1.Preferences.Modeling.CutViewUpdateDelayed = True
Dim partLoadStatus1 As PartLoadStatus
Dim status1 As PartCollection.SdpsStatus
status1 = theSession.Parts.SetDisplay(part1, True, False, partLoadStatus1)
workPart = theSession.Parts.Work
dispPart = theSession.Parts.Display
partLoadStatus1.Dispose()
'------------------------------------------------------------
'Dim part2 As Part = CType(theSession.Parts.FindObject(c.RootComponent.DisplayName), Part)
workPart.Preferences.Modeling.CutViewUpdateDelayed = True
Dim partLoadStatus2 As PartLoadStatus
Dim status2 As PartCollection.SdpsStatus
Dim partSaveStatus1 As PartSaveStatus
dispPart.ModelingViews.WorkView.Fit()
dispPart.ModelingViews.WorkView.Orient(NXOpen.View.Canned.Isometric, NXOpen.View.ScaleAdjustment.Fit)
dispPart.ModelingViews.WorkView.RenderingStyle = NXOpen.View.RenderingStyleType.Shaded
partSaveStatus1 = dispPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
status2 = theSession.Parts.SetDisplay(part2, True, False, partLoadStatus2)
workPart = theSession.Parts.Work
dispPart = theSession.Parts.Display
partLoadStatus2.Dispose()
'------------------------------------------------------------
next
workPart.Preferences.Modeling.CutViewUpdateDelayed = True
Dim partLoadStatus6 As PartLoadStatus
Dim status6 As PartCollection.SdpsStatus
Dim partSaveStatus2 As PartSaveStatus
status6 = theSession.Parts.SetDisplay(part2, True, False, partLoadStatus6)
workPart = theSession.Parts.Work
dispPart = theSession.Parts.Display
Dim nullAssemblies_Component As Assemblies.Component = Nothing
dispPart.Preferences.Modeling.CutViewUpdateDelayed = True
Dim partLoadStatus7 As PartLoadStatus
theSession.Parts.SetWorkComponent(nullAssemblies_Component, PartCollection.RefsetOption.Current, PartCollection.WorkComponentOption.Visible, partLoadStatus7)
dispPart.ModelingViews.WorkView.Fit()
dispPart.ModelingViews.WorkView.Orient(NXOpen.View.Canned.Isometric, NXOpen.View.ScaleAdjustment.Fit)
dispPart.ModelingViews.WorkView.RenderingStyle = NXOpen.View.RenderingStyleType.Shaded
partSaveStatus2 = dispPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
workPart = theSession.Parts.Work
partLoadStatus7.Dispose()
else
'*** insert code to process piece part
lw.WriteLine("Part has no components")
end if
Catch e As Exception
theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
End Try
'lw.Close

End Sub

'**********************************************************
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
'**********************************************************

End Module





(Thanks to Carlo Tony Daristotile)
http://www.nxjournaling.com/content/creating-subro...

Regards,

JEHILL

Re: View Orientation Fit NX Journal for Assemblies

Phenom
Phenom

The first block of code in the link you posted gives you what you need; it will process every component in an assembly. I'd suggest creating a subroutine that does what you want (given a part, orient the view and save it) and adding it to the journal in the link. Insert a call to your subroutine below the line:

'*** insert code to process component or subassembly

found in the "reportComponentChildren" subroutine.

Re: View Orientation Fit NX Journal for Assemblies

Experimenter
Experimenter

cowski,

 

If I post the code as displayed it is only view orient and saving the top level assembly when ran in NX. This is inserting lines after the comment code.

 

Option Strict Off
 
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
 
Module NXJournal
 
    Public theSession As Session = Session.GetSession()
    Public ufs As UFSession = UFSession.GetUFSession()
    Public lw As ListingWindow = theSession.ListingWindow
 
    Sub Main()
    Dim workPart As Part = theSession.Parts.Work
    Dim dispPart As Part = theSession.Parts.Display
 
    lw.Open
    Try
        Dim c As ComponentAssembly = dispPart.ComponentAssembly
        'to process the work part rather than the display part,
        '  comment the previous line and uncomment the following line
        'Dim c As ComponentAssembly = workPart.ComponentAssembly
        if not IsNothing(c.RootComponent) then
            '*** insert code to process 'root component' (assembly file)
            lw.WriteLine("Assembly: " & c.RootComponent.DisplayName)
            lw.WriteLine(" + Active Arrangement: " & c.ActiveArrangement.Name)
            '*** end of code to process root component
            ReportComponentChildren(c.RootComponent, 0)
        else
            '*** insert code to process piece part
            lw.WriteLine("Part has no components")
        end if
    Catch e As Exception
        theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
    End Try
    lw.Close
 
    End Sub
 
'**********************************************************
    Sub reportComponentChildren( ByVal comp As Component, _
        ByVal indent As Integer)
        Dim dispPart As Part = theSession.Parts.Display

        For Each child As Component In comp.GetChildren()
            '*** insert code to process component or subassembly
            lw.WriteLine(New String(" ", indent * 2) & child.DisplayName())
                dispPart.ModelingViews.WorkView.Fit()
                dispPart.ModelingViews.WorkView.Orient(View.Canned.Isometric, View.ScaleAdjustment.Fit)
                Dim partSaveStatus1 As PartSaveStatus
                partSaveStatus1 = dispPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
                partSaveStatus1.Dispose()
            '*** end of code to process component or subassembly
            if child.GetChildren.Length <> 0 then
                '*** this is a subassembly, add code specific to subassemblies
                    dispPart.ModelingViews.WorkView.Fit()
                    dispPart.ModelingViews.WorkView.Orient(View.Canned.Isometric, View.ScaleAdjustment.Fit)
                   Dim partSaveStatus2 As PartSaveStatus
                   partSaveStatus2 = dispPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
                   partSaveStatus2.Dispose()
                lw.WriteLine(New String(" ", indent * 2) & _
                    "* subassembly with " & _
                    child.GetChildren.Length & " components")
                lw.WriteLine(New String(" ", indent * 2) & _
                    " + Active Arrangement: " & _
                    child.OwningPart.ComponentAssembly.ActiveArrangement.Name)
                                    '*** end of code to process subassembly
            else
                'this component has no children (it is a leaf node)
                'add any code specific to bottom level components
            end if
            reportComponentChildren(child, indent + 1)
        Next
    End Sub
'**********************************************************
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function
'**********************************************************
 
End Module

Re: View Orientation Fit NX Journal for Assemblies

Phenom
Phenom

Let's take a look at what you have:

 

    Sub reportComponentChildren( ByVal comp As Component, _
        ByVal indent As Integer)
         Dim dispPart As Part = theSession.Parts.Display

        For Each child As Component In comp.GetChildren()
            '*** insert code to process component or subassembly
            lw.WriteLine(New String(" ", indent * 2) & child.DisplayName())
                dispPart.ModelingViews.WorkView.Fit()
                dispPart.ModelingViews.WorkView.Orient(View.Canned.Isometric, View.ScaleAdjustment.Fit)
                Dim partSaveStatus1 As PartSaveStatus
                partSaveStatus1 = dispPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
                partSaveStatus1.Dispose()
            '*** end of code to process component or subassembly

 

the variable dispPart is defined as whatever the current display part is.

 

    Sub reportComponentChildren( ByVal comp As Component, _
        ByVal indent As Integer)
         Dim dispPart As Part = theSession.Parts.Display

        For Each child As Component In comp.GetChildren()
            '*** insert code to process component or subassembly
            lw.WriteLine(New String(" ", indent * 2) & child.DisplayName())
                dispPart.ModelingViews.WorkView.Fit()
                dispPart.ModelingViews.WorkView.Orient(View.Canned.Isometric, View.ScaleAdjustment.Fit)
                Dim partSaveStatus1 As PartSaveStatus
                partSaveStatus1 = dispPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
                partSaveStatus1.Dispose()
            '*** end of code to process component or subassembly

For every component found, the following actions will be taken:

    Sub reportComponentChildren( ByVal comp As Component, _
        ByVal indent As Integer)
        Dim dispPart As Part = theSession.Parts.Display

        For Each child As Component In comp.GetChildren()
            '*** insert code to process component or subassembly
            lw.WriteLine(New String(" ", indent * 2) & child.DisplayName())
                dispPart.ModelingViews.WorkView.Fit()
                dispPart.ModelingViews.WorkView.Orient(View.Canned.Isometric, View.ScaleAdjustment.Fit)
                Dim partSaveStatus1 As PartSaveStatus
                partSaveStatus1 = dispPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
                partSaveStatus1.Dispose()
            '*** end of code to process component or subassembly

The current display part's view orientation changes and the display part is saved.

 

No where in the code does the display part ever get changed. What this means is the current display part (most likely the top level assembly at the time the journal is run) gets saved every time a component is found.

 

One way to fix this is to get the parent part file of the component geometry and make it the display part before performing those actions. The code below shows how to get the parent part from the component object.

Dim thePart as Part
thePart = child.prototype.owningpart

Try recording a journal while changing the display part to see what code would be needed to do that.

 

p.s. you will run into issues if the file cannot be found with your current search options or if the file is read-only. You might want to wrap the "change display" and "save" operations in a Try block to catch and handle errors.

Re: View Orientation Fit NX Journal for Assemblies

Experimenter
Experimenter

I think I understand what your saying, but I am having trouble trying to set a displayed part with the FindObject(" ") function.

 

If I run the record it selects a specific file name with the FindObject ("xx-part name-xx")

 

What I really need is an identifier for that FindObject function that will open the current component found. Does this exist? what would be a work-around?

 

Thank you for the help.

 

Option Strict Off
 
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
 
Module NXJournal
 
    Public theSession As Session = Session.GetSession()
    Public ufs As UFSession = UFSession.GetUFSession()
    Public lw As ListingWindow = theSession.ListingWindow
 
    Sub Main()
    Dim workPart As Part = theSession.Parts.Work
    Dim dispPart As Part = theSession.Parts.Display
 
    lw.Open
    Try
        Dim c As ComponentAssembly = dispPart.ComponentAssembly
        if not IsNothing(c.RootComponent) then
            '*** insert code to process 'root component' (assembly file)
            lw.WriteLine("Assembly: " & c.RootComponent.DisplayName)
            lw.WriteLine(" + Active Arrangement: " & c.ActiveArrangement.Name)
            '*** end of code to process root component
            ReportComponentChildren(c.RootComponent, 0)
        else
            '*** insert code to process piece part
            lw.WriteLine("Part has no components")
        end if
    Catch e As Exception
        theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
    End Try
    lw.Close
 
    End Sub
 
'**********************************************************
    Sub reportComponentChildren( ByVal comp As Component, _
        ByVal indent As Integer)
        Dim dispPart As Part = theSession.Parts.Display
        Dim c As ComponentAssembly = dispPart.ComponentAssembly
       
        For Each child As Component In comp.GetChildren()
'** insert code to process component or subassembly
            lw.WriteLine(New String(" ", indent * 2) & child.DisplayName())
'***Sets Display part to the Component
               Dim part1 As Part = CType(theSession.Parts.FindObject("Part Number"), Part)
               Dim partLoadStatus1 As PartLoadStatus
               Dim status1 As PartCollection.SdpsStatus
               status1 = theSession.Parts.SetDisplay(part1, True, True, partLoadStatus1)
               dispPart = theSession.Parts.Display
               partLoadStatus1.Dispose()               
'****Sets views to Display part
                Try
                dispPart.ModelingViews.WorkView.Fit()
                dispPart.ModelingViews.WorkView.Orient(View.Canned.Isometric, View.ScaleAdjustment.Fit)
                Dim partSaveStatus1 As PartSaveStatus
                partSaveStatus1 = dispPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
                partSaveStatus1.Dispose()
                Catch e As Exception
                theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
                End Try
'***Sets Display part to Root Component
               Dim part2 As Part = CType(theSession.Parts.FindObject(c.RootComponent.DisplayName), Part)
               Dim partLoadStatus2 As PartLoadStatus
               'Dim status1 As PartCollection.SdpsStatus
               status1 = theSession.Parts.SetDisplay(part2, True, True, partLoadStatus2)
               dispPart = theSession.Parts.Display
               partLoadStatus2.Dispose()
'** end of code to process component or subassembly
            if child.GetChildren.Length <> 0 then
'** this is a subassembly, add code specific to subassemblies
'****Sets views to Display part
                   Try
                   dispPart.ModelingViews.WorkView.Fit()
                   dispPart.ModelingViews.WorkView.Orient(View.Canned.Isometric, View.ScaleAdjustment.Fit)
                   Dim partSaveStatus2 As PartSaveStatus
                   partSaveStatus2 = dispPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
                   partSaveStatus2.Dispose()
                   Catch e As Exception
                   theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
                   End Try
'**** WriteLine
                   lw.WriteLine(New String(" ", indent * 2) & _
                    "* subassembly with " & _
                    child.GetChildren.Length & " components")
                lw.WriteLine(New String(" ", indent * 2) & _
                    " + Active Arrangement: " & _
                    child.OwningPart.ComponentAssembly.ActiveArrangement.Name)
'** end of code to process subassembly
            else
                'this component has no children (it is a leaf node)
                'add any code specific to bottom level components
            end if
            reportComponentChildren(child, indent + 1)
        Next
    End Sub
'**********************************************************
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function
'**********************************************************
 
End Module

 

 

 

 

Re: View Orientation Fit NX Journal for Assemblies

Phenom
Phenom

"What I really need is an identifier for that FindObject function that will open the current component found. Does this exist? what would be a work-around?"

 

Don't use the FindObject function. Given a reference to a component, you can get a reference to the part that owns the component geometry; I showed how to do that in my previous post.

 

Dim part1 as Part
part1 = child.prototype.owningpart

Where child is the reference to the current component in the reportComponentChildren subroutine.